It is my first question on stack overflow and python is not my native language. Please forgive me for any confusions.
I am finding the positions of the sun and earth in my iterative algorithm below using euler's method. I am running into an issue in the bottom of my code. My original array declarations (positions and velocities) are a list of lists. If you read below, I'm having trouble appending these updated lists to my arrays (positions_list and velocities_list), which is to store the history of these updates.
G = 6.67e-11
Ms = 1.99e30
Me = 5.972e24
AU = 1.496e11
positions = [[0,0], [1.0167*AU,0]]
velocities = [[0,0], [0,29290]]
masses = [Ms, Me]
day_sec = 24*60*60
dt = day_sec*1
num_Iterations = 10
num_planets = 2
positions_list = []
velocities_list = []
for i in range(3):
forces = []
for j in range(num_planets): #calculate at planet j
fx = 0
fy = 0
for k in range(num_planets): #calculate pairs
if j != k:
rx = positions[j][0] - positions[k][0]
ry = positions[j][1] - positions[k][1]
r = (rx**2 + ry**2)**0.5
f = (G * masses[j] * masses[k]) / (r**2)
fx += f * (rx / r)
fy += f * (ry / r)
forces.append([fx, fy])
for j in range(num_planets):
positions[j][0] += velocities[j][0] * dt
positions[j][1] += velocities[j][1] * dt
velocities[j][0] += forces[j][0] * dt / masses[j]
velocities[j][1] += forces[j][1] * dt / masses[j]
print(positions.copy())
positions_list.append(positions.copy())
An output of print(positions.copy()) is...
[[0, 0], [152098320000.0, 2530656000]]
[[-128.53585292903838, 0.0], [152141150935.58752, 5061312000.0]]
[[-385.5542028593587, -2.137729081631383], [152226795027.40958, 7592680337.721441]]
However, my last line of code positions_list.append(positions.copy()) is...
[[[-385.5542028593587, -2.137729081631383],
[152226795027.40958, 7592680337.721441]],
[[-385.5542028593587, -2.137729081631383],
[152226795027.40958, 7592680337.721441]],
[[-385.5542028593587, -2.137729081631383],
[152226795027.40958, 7592680337.721441]]]
It essentially appends the last list made in the code. I want it to append each list separately, storing all lists over the iteration. No matter how long the iteration is, it will only store the last list made however many iterations the loop is.
Therefore, my desired output of positions_list is...
[[[0, 0], [152098320000.0, 2530656000]],
[[-128.53585292903838, 0.0], [152141150935.58752, 5061312000.0]],
[[-385.5542028593587, -2.137729081631383], [152226795027.40958, 7592680337.721441]]]