From what I understand from your code, you wanted to create a list to save the results of a third-degree function, right?
To do this, you would have to use the append method of the List object. However, first of all, we would have to change the code a little:
x=[]
for i in range (101):
x.append(2.6868*i - 0.2462*i**3)
arq = open('saida.txt','a')
for n in x:
arq.write(str(n)+'\n')
arq.close()
Running this code, you would get the following result if you printed the list x:
[0.0, 2.4406, 3.404, 1.4129999999999994, -5.009600000000001, -17.341, -37.058400000000006, -65.63900000000001, -104.56, -155.29860000000002, -219.332, -298.1374, -393.192, -505.97299999999996, -637.9576000000001, -790.6229999999999, -965.4464, -1163.905, -1387.476, -1637.6366, -1915.8639999999998, -2223.6354, -2562.428, -2933.719, -3338.9856, -3779.705, -4257.3544, -4773.411, -5329.352, -5926.6546, -6566.795999999999, -7251.2534000000005, -7981.504, -8759.025, -9585.2936, -10461.787, -11389.9824, -12371.357, -13407.387999999999, -14499.552599999999, -15649.328, -16858.1914, -18127.62, -19459.091, -20854.0816, -22314.069, -23840.530400000003, -25434.943, -27098.784, -28833.5306, -30640.66, -32521.649400000002, -34477.975999999995, -36511.117, -38622.5496, -40813.751000000004, -43086.1984, -45441.369000000006, -47880.74, -50405.7886, -53017.992, -55718.8274, -58509.772000000004, -61392.303, -64367.897600000004, -67438.033, -70604.18639999999, -73867.835, -77230.456, -80693.5266, -84258.524, -87926.92540000001, -91700.208, -95579.849, -99567.3256, -103664.115, -107871.6944, -112191.541, -116625.132, -121173.9446, -125839.45599999999, -130623.1434, -135526.484, -140550.955, -145698.0336, -150969.19700000001, -156365.9224, -161889.687, -167541.968, -173324.2426, -179237.98799999998, -185284.6814, -191465.80000000002, -197782.821, -204237.22160000002, -210830.479, -217564.0704, -224439.473, -231458.164, -238621.6206, -245931.32]
The "output.txt" file would be in the folder that you are manipulating your script.
For more inputs of the open function, follow the link.
Also, what python function are you using?
*******Refactoring: Adding a y list to store the result of an x function that starts with the value 0.1 and loops 100 times**
x = 0.1
y=[]
for i in range(101):
x = 2.6868*x - 0.2462*x**3
print(x)
y.append(x)
First, let’s understand the problem, because the code didn’t make much sense. You have two variables called
x? What exactly do you need to do? Calculate2.6868*x - 0.2462*x**3for all values ofxfrom 0 to 100 and save the results in a file?– Woss
Then, x=0.1 is an initial value, from this value computes the recursive function x = 2.6868x - 0.2462x**3
– Priscila Guedes
Okay, so at the end
xwill have a value, which in this case would be 0.31539. What would bex=[]and what exactly you want to record in the file?– Woss
To "iterate" with broken values and evaluate the polynomial, I used a solution in this answer: https://answall.com/a/248505/64969 the function
rangeonly accepts integers, so if you don’t do a transformation functionf:Z-->Rto obtain the value ofxon the value of any integer index (theioffor i in range), then better use awhile– Jefferson Quesado
Taking advantage, how does this recursive function work? It does not seem to converge
– Jefferson Quesado
I want to create a list of values for the function 'x', the initial value is 0.1, and from that value, I make the iterations to get the sequence of values of 'x', would be a sequence of type [0.1, 0.2684, 0.7164, ...]
– Priscila Guedes