I haven't looked at your derivative equations yet, but the order is definitely wrong. E.g., take the first two elements:
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)];
Clearly, the first two elements of your state vector are the position elements for the Earth. But then in your derivative function:
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))];
in that 2nd spot you have the velocity derivative, not the position derivative. This does not match your state vector ic. So at the very least, change the order of your derivative. E.g.,
f=@(t,x) [x(3);...
x(4);...
-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));...
-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);...
x(8);...
-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));...
-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))];
And then as a side comment I would suggest you double check your initial conditions for the moon velocity. Seems to me that it should be close to the earth velocity but that is not the case in your ic vector.
Best Answer