I shall plot the earth surrounding the sun. Therefore the task is splitted into 2 subtasks. At the first task I shall approximate that the motion is one a circle.
I used following code to get the solution but somehow the programm will edit one dot instead of several. Can you pls help me with my algorithm.
So my code:
npoints = 360
x= np.zeros((npoints,1))
y= np.zeros((npoints,1))
v_x=np.zeros((npoints,1))
v_y=np.zeros((npoints,1))
r=1
dt=1
x[0]=1.
y[0]=0.
v_x[0]=-1.
v_y[90]=-1.
v_x[180]=1.
v_y[270]=1.
for step in range(0,npoints-1):
v_x[step+1]=v_x[step]-4*pi**2*x[step]/(r**3)*dt
x[step+1]=x[step]+v_x[step+1]*dt
v_y[step+1]=v_y[step]-4*pi**2*y[step]/(r**3)*dt
y[step+1]=y[step]+v_y[step+1]*dt
plt.plot(x, y)
plt.axis([-100, 100, -100, 100])
plt.ylabel('y-axis')
plt.xlabel('x-axis')
plt.show()
Thanks for your help :)
I think that there are two mistakes in your code:
As far as I can tell from the code you posted, the distance from the Sun should be measured in Astronomical Units and time in fractions of a year, so that
r == 1.
means a distance of 1AU (~1.49e8 km) anddt == 1.
is a year. (Asdt == 1.
is too large, you can divide it bynpoints
:dt = 1./npoints
. Ifnpoints == 360
, the time step will be in the order of a day.)Moreover, you will need to express the gravitational parameter
mu
, too, into a consistent unit of measure. Using the expression for the orbital periodT = 2*pi*sqrt(r**3 / mu)
and imposingT=1.
andr=1.
, we obtainmu = 4 * pi**2
.Let's assume (as you did) that the initial position of the Earth has coordinates (x=1 AU, y=0 AU). The speed is tangent to the orbit, so in the chosen reference frame it has only a vertical component (
v_y
) and its module is given by the equation for the Circular Velocity. So you impose (v_x=0 AU/yr, v_y=sqrt(mu/r) AU/yr). Note that if you impose this set of initial conditions, you do not have to impose any other condition because the problem is already well posed. (Moreover, conditions likev_y[90]=-1.
are overwritten in thefor
-loop and do not affect your computations at all.)Here is the complete code: