Python files

Asked

Viewed 85 times

0

I have the following code:

x=[]
x=0.1

for i in range (101):
    x = 2.6868*x - 0.2462*x**3

I want to save my data 'x' to manipulate it into another software, I did the following:

with open( 'saida.txt', 'w' ) as arq:
    for n in x:
        arq.write( str(n) + '\n' )

I do not know if it is correct, and also do not know in which folder this 'output.txt' was saved, how I solve this problem

  • 4

    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? Calculate 2.6868*x - 0.2462*x**3 for all values of x from 0 to 100 and save the results in a file?

  • Then, x=0.1 is an initial value, from this value computes the recursive function x = 2.6868x - 0.2462x**3

  • Okay, so at the end x will have a value, which in this case would be 0.31539. What would be x=[] and what exactly you want to record in the file?

  • To "iterate" with broken values and evaluate the polynomial, I used a solution in this answer: https://answall.com/a/248505/64969 the function range only accepts integers, so if you don’t do a transformation function f:Z-->R to obtain the value of x on the value of any integer index (the i of for i in range), then better use a while

  • Taking advantage, how does this recursive function work? It does not seem to converge

  • 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, ...]

Show 1 more comment

1 answer

1


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)

  • Alex, she commented that the x = 0.1 is an initial value and that the for is to make a recursive calculation, ie the i of for is not used; it exists only to determine that the loop runs 100 times. That is, the calculation you made on for would be wrong. How much is it x = [] is still uncertain as she did not explain in the question.

  • I got it, Anderson! I hadn’t seen her comment about x = 0.1 being an initial value, but it makes sense to throw the recursive calculus for the for. From what Priscilla just posted, we’d have to use this list(x) to store the values.

  • @Andersoncarloswoss Now with Priscilla’s response (knowing that she wants to store the value in a list), it’s easier. And if we changed the name of the list to y, I think it would, right? I thought I’d just add a list y: x = 0.1
y=[]

for i in range(101):
 x = 2.6868*x - 0.2462*x**3
 y.append(x) What do you think? It makes sense now?

  • Yes, change the name of the list, continue assigning the value x to the recursive mathematical operation and then add the value of x to the list.

  • Great! Thanks a lot! =]

  • Right, it’s possible to save the data with all the decimals, because it looks like it’s truncating

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.