Hi everybody! My name is Julien, I'm new on the forum :) I am trying (for a homework in computational physics) to plot the trajectories of the Earth and of the Moon around the Sun in a xy-plane. The Sun is a fixed point in (0,0), therefore I end up having 4 coupled differential equations (2 for the Earth, 2 for the Moon) with initial conditions, and I am trying to solve them numerically using ode45 (which I have never used before). I read the documentation, but something is still not working: my Earth and Moon are strangely flying away from the Sun in an (almost) straight line. Would you mind taking a look at my script and telling me if this is a conceptual problem (maybe the equations are wrong?) or if it is a programming issue?
The equations are pure Newton's 2nd law and are indicated in the code. I have a feeling that the problem might lie in an incorrect handling of the 2nd order equations in my definition of f. Please let me know what you guys think.
close all, clear all
G=6.67408e-11;
m=[1.989e30...
5.972e24...
7.349e22];
r0=[1.496e11 0;...
1.496e11 -3.844e8];
v0=[0 29.78e3;...
1.023e3 0];
ic=[r0(1,1);r0(1,2);v0(1,1);v0(1,2);...
r0(2,1);r0(2,2);v0(2,1);v0(2,2)];
f=@(t,x) [x(3);-G*(m(1)*x(1)/(x(1)^2+x(2)^2)^(3/2)+m(3)*(x(1)-x(5))/((x(1)-x(5))^2+(x(2)-x(6))^2)^(3/2));...
x(4);-G*(m(1)*x(2)/(x(1)^2+x(2)^2)^(3/2)+m(3)*(x(2)-x(6))/((x(1)-x(5))^2+(x(2)-x(6))^2)^(3/2));...
x(7);-G*(m(1)*x(5)/(x(5)^2+x(6)^2)^(3/2)+m(2)*(x(5)-x(1))/((x(5)-x(1))^2+(x(6)-x(2))^2)^(3/2));...
x(8);-G*(m(1)*x(6)/(x(5)^2+x(6)^2)^(3/2)+m(2)*(x(6)-x(2))/((x(5)-x(1))^2+(x(6)-x(2))^2)^(3/2))];
tspan=0:1000:10000;
[t,x]=ode45(f,tspan,ic);
figure
plot(0,0,'o','Color',[0.8 0.8 0])
hold on
plot(x(:,1),x(:,2),'o','Color','blue')
hold on
plot(x(:,5),x(:,6),'o','Color',[0.3 0.3 0.3]);
xlim([-2.5e11 2.5e11])
ylim([-2.5e11 2.5e11])
The problem is that the y-coordinates of both the Earth (x(:,2)) and the Moon (x(:6)) are barely changing, while the velocities in x-direction (x(:,3) for the Earth, x(:,7) for the Moon) evolve rapidly!
Thank you very much in advance for your answers.
Julien.