Solving the two body problem in Matlab

In summary, the conversation discusses the use of the Euler method to solve the trajectory of a basic two body problem. The equation for the problem is provided and the participants discuss the possibility of converting the equation into three separate equations for each component of the acceleration, which can then be solved using the Euler method. The use of a Runge-Kutta method and Verlet integration is also suggested. The conversation ends with a discussion about the integration time step and the use of ode45 in Matlab.
  • #1
patric44
296
39
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
  • #2
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 patric44
  • #3
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
 
  • #4
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 patric44
  • #5
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 🤔
 
  • #6
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
 
  • #7
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.
 

1. What is the "two body problem" in Matlab?

The two body problem in Matlab refers to the mathematical challenge of predicting the motion of two objects, such as planets or satellites, as they orbit each other under the influence of their mutual gravitational attraction.

2. What are the key steps in solving the two body problem in Matlab?

The key steps in solving the two body problem in Matlab include defining the initial conditions, setting up the governing equations of motion, selecting a numerical integration method, and running the simulation to obtain the position and velocity of the objects at each time step.

3. How accurate is the solution obtained from solving the two body problem in Matlab?

The accuracy of the solution depends on factors such as the chosen numerical integration method, the time step size, and the complexity of the system. In general, the solution can be made more accurate by decreasing the time step size and using higher-order integration methods.

4. Can the two body problem be solved in Matlab for more than two objects?

Yes, the two body problem can be extended to include multiple objects, but it becomes increasingly complex as the number of objects increases. In some cases, simplifications or approximations may need to be made to obtain a feasible solution.

5. Are there any limitations to solving the two body problem in Matlab?

One limitation is that the two body problem assumes that the objects are point masses, which may not be accurate for real-life scenarios. Additionally, the simulation may become computationally intensive for systems with a large number of objects or over long time periods.

Similar threads

  • Introductory Physics Homework Help
Replies
5
Views
3K
  • Introductory Physics Homework Help
Replies
8
Views
705
Replies
1
Views
552
Replies
8
Views
236
  • Introductory Physics Homework Help
Replies
11
Views
227
  • Introductory Physics Homework Help
Replies
15
Views
264
  • Introductory Physics Homework Help
Replies
6
Views
234
  • Introductory Physics Homework Help
Replies
3
Views
205
  • Introductory Physics Homework Help
Replies
25
Views
279
  • Introductory Physics Homework Help
Replies
2
Views
632
Back
Top