0

As an engineering student, I am doing a project about Kalman filtering and the mission Apollo 11. Now I tried to simulate the trajectory of the moon and sun for 3 days (the duration in which the spacecraft flight from the earth to the moon). But to my surprise, when I solved the motion equations of the spacecraft, I did not get good results.

How does this trajectory look like?

Here are blocks of the code:

function dydt = ode(t, y)

% Extract the state variables X = y(1);

Y = y(2);

Z = y(3);

Xdot = y(4);

Ydot = y(5);

Zdot = y(6);

%% date du départ 16 juillet 1969

mois = 7;

jours = [16 17 18];

ann = 1969;

%d = 367Y - (7(Y + ((M+9)/12)))/4 + (275*M)/9 + D - 730530

d1 = 367ann - (7(ann + ((mois+9)/12)))/mois + (275*mois)/9 + jours(1) - 730530;

d2 = 367ann - (7(ann + ((mois+9)/12)))/mois + (275*mois)/9 + jours(2) - 730530;

d3 = 367ann - (7(ann + ((mois+9)/12)))/mois + (275*mois)/9 + jours(3) - 730530;

%% sun position

if t <= 2460 d = d1; elseif t>2460 & t <= 4860 d = d1 + (t - 2460) / (4860 - 2460) * (d2 - d1); else d = d2 + (t - 4860) / (7260 - 4860) (d3 - d2); end

w = 282.9404 + 4.70935e-5* d ;% (longitude of perihelion)

a = 1.000000 ; % (mean distance, a.u.)

e = 0.016709 - 1.15e-9* d ; %% (eccentricity)

M = 356.0470 + 0.9856002585* d ; % (mean anomaly)

oblecl = 23.4393 - 3.563e-7 * d;

L = w + M ;

E = M + (180/pi) * e * sin(M) * (1 + e * cos(M));

x = cos(E) - e;

y = sin(E) * sqrt(1 - e*e);

r = sqrt(xx + yy);

v = atan2( y, x );

lon = v + w;

x = r * cos(lon);

y = r * sin(lon);

z = 0.0;

xequat = 0.881048;

yequat = 0.482098 * cos(oblecl) - 0.0 * sin(oblecl);

zequat = 0.482098 * sin(oblecl) + 0.0 * cos(oblecl);

Xs = xequat*1.496e+11;

Ys = yequat*1.496e+11;

Zs = zequat*1.496e+11;

%% lune position !!!!

N = 125.1228 - 0.0529538083 * d; % (Long asc. node)

inc = 5.1454; % (Inclination)

w = 318.0634 + 0.1643573223 * d; % (Arg. of perigee)

a = 60.2666 ; % (Mean distance)

e = 0.054900 ; % (Eccentricity)

M = 115.3654 + 13.0649929509 * d; %(Mean anomaly)

% normalisation

M = M + 129*360;

w = w + 360;

E0 = M + (180/pi) * e * sin(M) * (1 + e * cos(M));

E1 = E0 - (E0 - (180/pi) * e * sin(E0) - M) / (1 - e * cos(E0));

E = E1;

x = a * (cos(E) - e);

y = a * sqrt(1 - ee) sin(E);

r = sqrt( xx + yy );

v = atan2( y, x );

xeclip = r * ( cos(N) * cos(v+w) - sin(N) * sin(v+w) * cos(inc) );

yeclip = r * ( sin(N) * cos(v+w) + cos(N) * sin(v+w) * cos(inc) );

zeclip = r * sin(v+w) * sin(inc);

xeq = xeclip;

yeq = yeclip * cos(oblecl) - zeclip * sin(oblecl);

zeq = yeclip * sin(oblecl) + zeclip * cos(oblecl);

Xm = xeq*1.496e+11 ;

Ym = yeq*1.496e+11;

Zm = zeq*1.496e+11;

%% constantes

mu_e = 3.986135e14; %sets the gravitational parameter for the Earth.

mu_m = 4.89820e12;%sets the gravitational parameter for the Moon.

mu_s = 1.3253e20;%sets the gravitational parameter for the Sun.

a = 6.37826e6; %the equatorial radius of the Earth.

J = 1.6246e-3;%sets the Earth's second dynamic form factor.

% Calculate the distances and other parameters

r = sqrt(X^2 + Y^2 + Z^2);

rs = sqrt(Xs^2 + Ys^2 + Zs^2);

rm = sqrt(Xm^2 + Ym^2 + Zm^2);

delta_m = sqrt((X - Xm)^2 + (Y - Ym)^2 + (Z - Zm)^2);

delta_s = sqrt((X - Xs)^2 + (Y - Ys)^2 + (Z - Zs)^2);

Y_s = Ys;

Z_s = Zs;

% Calculate the derivatives

X2dot = -((mu_eY)/r^3) (1 + (J(a/r)^2)(1 - 5*Z^2/r^2)) ...

- (mu_m*(X - Xm)/delta_m^3) ...

- (mu_m*Xm/rm^3) ...

- (mu_s*(X - Xs)/delta_s^3) ...

- (mu_s*Xs/rs^3);

Y2dot = -((mu_eY)/r^3) (1 + (J(a/r)^2)(1 - 5*Z^2/r^2)) ...

- (mu_m*(Y - Ym)/delta_m^3) ...

- (mu_m*Ym/rm^3) ...

- (mu_s*(Y - Y_s)/delta_s^3) ...

- (mu_s*Ys/rs^3);

Z2dot = -((mu_eZ)/r^3) (1 + (J(a/r)^2)(3 - 5*Z^2/r^2)) ...

- (mu_m*(Z - Zm)/delta_m^3) ...

- (mu_m*Zm/rm^3) ...

- (mu_s*(Z - Z_s)/delta_s^3) ...

- (mu_s*Zs/rs^3);


% Return the derivative vector

%X2dot = interp1(Xdot,X2dot,t);

dydt = [Xdot; Ydot; Zdot; X2dot; Y2dot; Z2dot];

end

Then we have the ODE45 to resolve the equation

close all clear all; clc;

%% Set the initial conditions

X0 =100 + 6.37826e6;

Y0 = 100 + 6.37826e6;

Z0 = 100 + 6.37826e6; 

 Xdot0 =  -2.0225e+14;    %Xm/72*3600, repère géocentrique %8.9919e+10  ;

 Ydot0 =  -1.8023e+14;%-1.0908e+11;

 Zdot0 =  3.4945e+14; %-4.8960e+10;

y0 = [X0; Y0; Z0; Xdot0; Ydot0; Zdot0];

tspan = 0:1:72*60*60;

[t, y] = ode45(@ode, tspan, y0);

plot3(y(:,1),y(:,2),y(:,3))

title('Solution of the Motion Equation with ODE45');

Glorfindel
  • 4,790
  • 3
  • 25
  • 40
Blobmou
  • 55
  • 2
  • 3
    A few comments. Firstly, are you aware of [space.se] it is about human exploration of space and may be more appropriate. Secondly we probably can't help in debugging code. You'll need to break down the code into testable sections and test each part. However, There are folks here who can discuss the mathematics of orbital trajectories. Thirdly, you didn't attach the code. – James K May 06 '23 at 15:49
  • 1
    You can compare your results to JPL Horizons: https://ssd.jpl.nasa.gov/horizons/app.html#/ – Greg Miller May 06 '23 at 15:49
  • The code works! it is the calculation of the initial conditions that I am not sure of – Blobmou May 06 '23 at 18:19
  • @GregMiller I calculated the Moon and Sun positions using the equations in this website https://stjarnhimlen.se/comp/tutorial.html#7 – Blobmou May 06 '23 at 18:21
  • As Uhoh said "please debug" doesn't often get answered. The linked page for calculating the position of the moon looks sound, (though remember you are really finding the position of the Earth, not the position of the sun) But your code is one big unstructured mass of assignments. If it isn't doing what you expect it to do, you need to split it up into testable functions, and test them – James K May 06 '23 at 22:15
  • Ok, but how do I verify of my calculations are correct (initial positions, sun and moon position)? – Blobmou May 06 '23 at 22:24
  • 2
    See the comment above by Greg, you can compare your positions to those calculated by the high accuracy formulae used by the Horizons system – James K May 06 '23 at 22:31
  • 1
    Thanks for the edit! Hopefully the drive-by downvoting will stop :-) – uhoh May 07 '23 at 08:47
  • 1
    @uhoh I think I found what is wrong, the initial velocity of the spacecraft, does anyone know how do I calculate it to reach the moon? – Blobmou May 07 '23 at 17:10
  • 1
    @Blobmou based on Greg Miller's comment I looked in Horizons at the trajectory of the first four days of Apollo 12's trajectory for example, see https://i.stack.imgur.com/n2hTv.png and https://i.stack.imgur.com/fZccK.png where state vectors (x, y, z, vx, vy, vz) can be obtained. – uhoh May 07 '23 at 19:59
  • You can use my script at the end of this answer to view these trajectories in 3D. But you won't see much detail if you include the Sun. The ID for Apollo 11 is -399110 – PM 2Ring May 09 '23 at 19:18

0 Answers0