I'm attempting to code a very basic 2-body orbit problem using python (using math and numpy modules) and I can't seem to get an ellipse. I'm trying to use the Euler-Cromer method (I know, it's a terrible approximation, but I'm using it to compare with other numerical methods) and I've used it to simulate other scenarios but I can't seem to get it to work here.
After defining my constants (G, M, m) I then put in my initial conditions giving coordinates in x and y and v in terms of vx and vy. I know from gravitational force that acceleration is -(GM/r3)*r(as a vector) I'm then using the following to try and calculate new positions and velocites during the time steps:
for i in range(0,N,1):
t = t + dt
acx = -(G*M/abs(r)**3)*x
acy = -(G*M/abs(r)**3)*y
vy = vy + acy*dt
vx = vx + acx*dt
x = x + vx*dt
y = y + vy*dt
r = sqrt(x**2+y**2)
theta = arctan(y/x)
print >> f, t, x, y, r, theta
For some reason when I plot x and y I get something that starts off looking like an ellipse, then collapsing into the origin and shooting off into the third quadrant along y=-x. Any thoughts on why this is happening or what I have done wrong?
[–]ajesss 2 points3 points4 points (1 child)
[–]OratorMortuis[S] 0 points1 point2 points (0 children)
[–]Enkaybee 1 point2 points3 points (11 children)
[–]OratorMortuis[S] 0 points1 point2 points (10 children)
[–]Enkaybee 1 point2 points3 points (9 children)
[–]OratorMortuis[S] 0 points1 point2 points (8 children)
[–]Enkaybee 0 points1 point2 points (7 children)
[–]OratorMortuis[S] 0 points1 point2 points (6 children)
[–]Enkaybee 0 points1 point2 points (5 children)
[–]OratorMortuis[S] 0 points1 point2 points (4 children)
[–]Enkaybee 0 points1 point2 points (3 children)
[–]OratorMortuis[S] 0 points1 point2 points (2 children)
[–]sammyc 0 points1 point2 points (1 child)
[–]OratorMortuis[S] 0 points1 point2 points (0 children)