Question about how solve 2 body problem in orbit using MATLAB ode45

Click For Summary

Discussion Overview

The discussion revolves around solving and plotting the equations of motion for the two-body problem using MATLAB's ode45 function. Participants share code snippets, error messages, and suggestions for improvements, focusing on both technical aspects of the code and conceptual understanding of orbital mechanics.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant shares their MATLAB code for simulating the two-body problem and expresses confusion over error messages encountered.
  • Another participant asks for clarification on the specific error messages being received.
  • Errors related to unrecognized variables in the code are identified, particularly concerning the definition of position and velocity variables.
  • Suggestions are made to treat certain lines in the code as comments rather than executable statements, which may resolve the errors.
  • After fixing the code, one participant notes that the output is a line instead of a circular orbit, attributed to the initial conditions set for position and velocity.
  • Further suggestions include scaling distances and times to simplify the problem and improve the simulation's accuracy.
  • Another participant emphasizes the importance of varying the satellite's velocity to observe changes in the orbit and confirms the necessity of including the satellite's mass in the calculations.
  • One participant expresses a desire to learn how to plot the orbit around Earth, indicating a need for additional guidance on visualization techniques.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the initial conditions to achieve a proper orbital simulation. However, there are multiple competing views on how to best approach the scaling of units and the implications of including the satellite's mass in the calculations. The discussion remains unresolved regarding the best method for plotting the orbit around Earth.

Contextual Notes

Participants mention limitations in the current code, such as the initial conditions leading to a linear trajectory instead of a circular orbit. There are also discussions about the scaling of distances and times, which may depend on specific definitions and assumptions that have not been fully articulated.

saeede-
Messages
8
Reaction score
0
hi guys, I'm trying to write a program in MATLAB to solve and plot equation of motion of 2 body problem but it errors as i don't know what it says!
do you know how to help me? please!

The main equation is r(double dot)=-(mu)*r(hat)/r^3
The code that i wrote is:
Matlab:
clear all
close all
clc

R=6371e+3;
r0=3e+5;
p0=[R+r0;0;0;7000;0;0];

[t,p]=ode45(@sattelite,[0,3.15581e+07],p0);
xout=p(:,1);
yout=p(:,2);
zout=p(:,3);
plot3d(xout,yout,zout)

function dp=sattelite(t,p)
p=[x position;y position;z position;x velocity(vx);y velocity(vy);z velocity(vz)];
G=6.67e-11;
Me=5.972e+24;
m=50;
mu=(Me+m)*G;
R=6371e+3;
r0=3e+5;

r=sqrt(p(1)^2+p(2)^2+p(3)^2);
dp=zeros(6,1);
dp(1)=p(4);
dp(2)=p(5);
dp(3)=p(6);
dp(4)=-(mu/r^3)*p(1);
dp(5)=-(mu/r^3)*p(2);
dp(6)=-(mu/r^3)*p(3);
end
<Moderator's note: code tags added. Please use them.>
 
Last edited by a moderator:
Physics news on Phys.org
What is the error you get?
 
DrClaude said:
What is the error you get?
all the errors are:

Unrecognized function or variable 'x'.

Error in T3>sattelite (line 16)
p=[x position;y position;z position;x velocity(vx);y velocity(vy);z velocity(vz)];

Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in T3 (line 9)
[t,p]=ode45(@sattelite,[0,3.15581e+07],p0);
 
Line 16 looks like it should be a comment, not a statement. Fixing that will probably make all those errors go away.
 
  • Like
Likes   Reactions: pasmith and saeede-
DrClaude said:
Line 16 looks like it should be a comment, not a statement. Fixing that will probably make all those errors go away.
Oh yes! thanks. all errors gone .although i wanted to plot a circle 3d but it shows a line :)
 
Now that you have a program which runs, there are some other improvements you should consider.

saeede- said:
hi guys, I'm trying to write a program in MATLAB to solve and plot equation of motion of 2 body problem but it errors as i don't know what it says!
do you know how to help me? please!

The main equation is r(double dot)=-(mu)*r(hat)/r^3
The code that i wrote is:
Matlab:
clear all
close all
clc

R=6371e+3;
r0=3e+5;
p0=[R+r0;0;0;7000;0;0];

You are seeing a line because your initial position is in the x direction and your initial velocity is also in the x direction. Thus the angular velocity is zero, and the satellite will remain on the x axis.
Code:
[t,p]=ode45(@sattelite,[0,3.15581e+07],p0);
xout=p(:,1);
yout=p(:,2);
zout=p(:,3);
plot3d(xout,yout,zout)

function dp=sattelite(t,p)
p=[x position;y position;z position;x velocity(vx);y velocity(vy);z velocity(vz)];
G=6.67e-11;
Me=5.972e+24;
m=50;
mu=(Me+m)*G;
R=6371e+3;
r0=3e+5;

In terms of floating point arithmetic: 5.972 \times 10^{24} + 50 = 5.972 \times 10^{24}. You might as well set m = 0.

I would suggest scaling distances with r + R_0 and times with ((r+R_0)^3/G(M_e+m))^{1/2} so that in scaled units <br /> \ddot{\mathbf{r}} = - \frac{1}{r^2} \hat{\mathbf{r}}<br /> with \mathbf{r}(0) = (1,0,0). Velocities then scale as the length unit divided by the time unit, so the initial scaled velocity is \dot{\mathbf{r}}(0) = \left(0, 7000 \left(\frac{R + r_0}{G(M_e + m)}\right)^{1/2}, 0\right)<br /> \approx (0, 0.889, 0). (The problem is entirely two-dimensional, so you could get rid of the z components of position and velocity and save the computer some work.)

In these units, a circular orbit with initial velocity (0,1,0) has a period of 2\pi scaled time units. So your maximum time doesn't need to go beyond 10 scaled units, rather than the 3.15 \times 10^7\,\mathrm{s} you have allowed for.
 
  • Like
Likes   Reactions: saeede-
pasmith said:
Now that you have a program which runs, there are some other improvements you should consider.
You are seeing a line because your initial position is in the x direction and your initial velocity is also in the x direction. Thus the angular velocity is zero, and the satellite will remain on the x axis.In terms of floating point arithmetic: 5.972 \times 10^{24} + 50 = 5.972 \times 10^{24}. You might as well set m = 0.

I would suggest scaling distances with r + R_0 and times with ((r+R_0)^3/G(M_e+m))^{1/2} so that in scaled units <br /> \ddot{\mathbf{r}} = - \frac{1}{r^2} \hat{\mathbf{r}}<br /> with \mathbf{r}(0) = (1,0,0). Velocities then scale as the length unit divided by the time unit, so the initial scaled velocity is \dot{\mathbf{r}}(0) = \left(0, 7000 \left(\frac{R + r_0}{G(M_e + m)}\right)^{1/2}, 0\right)<br /> \approx (0, 0.889, 0). (The problem is entirely two-dimensional, so you could get rid of the z components of position and velocity and save the computer some work.)

In these units, a circular orbit with initial velocity (0,1,0) has a period of 2\pi scaled time units. So your maximum time doesn't need to go beyond 10 scaled units, rather than the 3.15 \times 10^7\,\mathrm{s} you have allowed for.

in my problem that i solve it , I would take different velocity and see how change orbit. and i should use m because it's mass of satellite.
I put the velocity that gave me in y component and become Ok !
Now , I don't know how to plot orbit around Earth . this said in different topic.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 45 ·
2
Replies
45
Views
6K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 12 ·
Replies
12
Views
33K
  • · Replies 10 ·
Replies
10
Views
11K
Replies
1
Views
4K