Solving the two body problem in Matlab

Click For Summary

Homework Help Overview

The discussion revolves around coding an algorithm in Matlab to compute the trajectory of a two-body problem using the equation of motion. Participants are exploring the conversion of this second-order differential equation into a set of first-order equations suitable for numerical integration methods.

Discussion Character

  • Exploratory, Mathematical reasoning, Problem interpretation, Assumption checking

Approaches and Questions Raised

  • Participants discuss the feasibility of separating the second-order equation into three components for each spatial direction and whether it is appropriate to solve them individually using the Euler method. Some suggest reducing the system to first-order equations and using alternative methods like Runge-Kutta or Verlet integration.

Discussion Status

There is an ongoing examination of the numerical stability of the Euler method, particularly concerning time step size. Some participants have noted discrepancies in the calculated velocity components, indicating potential issues with the implementation of the algorithm. Suggestions for using Matlab's ode45 for integration have been made, but no consensus has been reached regarding the best approach.

Contextual Notes

Participants mention constraints related to the initial data set derived from a simulation, which imposes a specific time interval for integration. The discussion also highlights the challenges of using a large time step in conjunction with the Euler method, which is known for its instability.

patric44
Messages
308
Reaction score
40
Homework Statement
solve the two body problem using a Matlab script ?
Relevant Equations
a = -mu/r^3
hi guys
i am trying to code an algorithm for computing the trajectory of a basic two body problem situation according to the equation
$$\ddot{r} = \frac{-\mu}{r^3} \vec{r}$$
i am trying to use the Euler method , but the problem is in converting this problem into a 3 separate equations one for each component
of ##r## .
can i separate this equation for each component of ## \ddot{r}## as
$$\ddot{x} = \frac{-\mu}{r^3} \vec{x} \quad \ddot{y} = \frac{-\mu}{r^3} \vec{y} \quad \ddot{z} = \frac{-\mu}{r^3} \vec{z} \quad $$
and solve each part using euler method separately , or i cannot do that ?
 
Physics news on Phys.org
patric44 said:
can i separate this equation for each component of ## \ddot{r}## as
$$\ddot{x} = \frac{-\mu}{r^3} \vec{x} \quad \ddot{y} = \frac{-\mu}{r^3} \vec{y} \quad \ddot{z} = \frac{-\mu}{r^3} \vec{z} \quad $$
and solve each part using euler method separately , or i cannot do that ?
Yes you can, and should.

The usual approach is to reduce the system to a set of coupled first order differential equations, by introducing new variables corresponding to ##\dot{x}##, etc. Note that the Euler method is unstable. You should try a Runge-Kutta method.

You can also directly integrate the second-order equations using Verlet integration.
 
  • Like
Likes   Reactions: patric44
DrClaude said:
Yes you can, and should.

The usual approach is to reduce the system to a set of coupled first order differential equations, by introducing new variables corresponding to ##\dot{x}##, etc. Note that the Euler method is unstable. You should try a Runge-Kutta method.

You can also directly integrate the second-order equations using Verlet integration.

i tried to that but it only works in the first iteration from my initial vector i provide , i checked every thing but for some reason the velocity resulting from each solution individually is not correct , can some one revise my code :
Matlab:
clc
clear all
x0 = [6569.944337;-1.387381];   % inital condition  [x,v_x]
y0 = [1152.258896;6.658808];   % inital condition  [y,v_y]
z0 = [390.787865;3.658486];   % inital condition  [y,v_z]
r1 = [6569.944337;1152.258896;390.787865];
R = 6.371e6;    %%meters
M = 5.972e24;  %%kg
G = 6.67e-11;   %%% some SI unit
mu = G*M;
dt  = 60;   % time step
T =3*dt;    % amount of time to integrate
% the forward euler
xF(:,1) = x0; %  [.....] tow of them above each other
yF(:,1) = y0; 
zF(:,1) = z0;
rn(:,1) =[xF(1,1);yF(1,1);zF(1,1)]; %% all the elements in the first array contain r1
tF(1) = 0;
for k = 1:T/dt
     A = [0 1;-mu/(norm(rn(:,k)))^3 0];
     tF(k+1) = k*dt;    
     xF(:,k+1) = (eye(2)+A*dt)*xF(:,k);
     yF(:,k+1) = (eye(2)+A*dt)*yF(:,k);
     zF(:,k+1) = (eye(2)+A*dt)*zF(:,k);
rn(:,k+1) = [xF(1,k+1);yF(1,k+1);zF(1,k+1)];
end
rn
 
that is a huge time step... and the total integration time is only 3 time steps. I'd start there. Euler is very unstable wrt time step size
 
  • Like
Likes   Reactions: patric44
Dr Transport said:
that is a huge time step... and the total integration time is only 3 time steps. I'd start there. Euler is very unstable wrt time step size
i have no choice here i have an initial data set came from a simulation , the interval between the data is 60 sec
, i know it seems the main issue but i guess something else causes an error in the velocitiy components 🤔
 
The program seems that the velocity components calculated is way way off but the positions seems to be correct, so when the second loop starts it begins with the vectors containing the incorrect velocity and every thing goes wrong, can anyone help
 
patric44 said:
i have no choice here i have an initial data set came from a simulation , the interval between the data is 60 sec
The integration time step does not have to correspond to the output time step.

patric44 said:
The program seems that the velocity components calculated is way way off but the positions seems to be correct, so when the second loop starts it begins with the vectors containing the incorrect velocity and every thing goes wrong, can anyone help
As you are using Matlab, you should really convert the system to first-order equations and use ode45 to integrate.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 24 ·
Replies
24
Views
2K
Replies
1
Views
1K
Replies
5
Views
1K
  • · Replies 17 ·
Replies
17
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K