How to plot an orbit around the Earth in MATLAB

Click For Summary
SUMMARY

This discussion provides a solution for plotting an orbit around the Earth using MATLAB. The user initially shared a code snippet that successfully plotted a 3D orbit but did not include a representation of the Earth. The final code incorporates the MATLAB function surf to create a sphere representing Earth with a radius of 6371 kilometers, while the orbit is plotted using plot3. The implementation utilizes the ode45 function for numerical integration of the satellite's motion.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of numerical methods, specifically ode45 for solving differential equations
  • Knowledge of basic physics concepts related to gravitational forces
  • Experience with 3D plotting functions in MATLAB, such as plot3 and surf
NEXT STEPS
  • Explore MATLAB's ode45 function for solving more complex differential equations
  • Learn about gravitational modeling and simulation techniques in MATLAB
  • Investigate advanced 3D visualization techniques in MATLAB for enhanced graphical representation
  • Study the physics of orbital mechanics to better understand satellite motion
USEFUL FOR

This discussion is beneficial for MATLAB users, aerospace engineers, and students studying orbital mechanics who are interested in visualizing satellite trajectories and gravitational effects in a 3D environment.

saeede-
Messages
8
Reaction score
0
Hi. can you help me to plot orbit around Earth with sphere in MATLAB?
I wrote the code that plot 3D orbit but without earth.

the code:
Matlab:
clear all
close all
clc

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

[t,p]=ode45(@sattelite,[0,12000],p0);
xout=p(:,1);
yout=p(:,2);
zout=p(:,3);
vx=p(:,4);
vy=p(:,5);
vz=p(:,6)

plot3(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
 
Physics news on Phys.org
I woud do it like this:
[CODE lang="matlab" title="Matlab"]
clear all
close all
clc

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

[t,p]=ode45(@sattelite,[0,12000],p0);
xout=p(:,1);
yout=p(:,2);
zout=p(:,3);
vx=p(:,4);
vy=p(:,5);
vz=p(:,6);

[X,Y,Z] = sphere;
X = X*R;
Y = Y*R;
Z = Z*R;
surf(X,Y,Z);
hold on

plot3(xout,yout,zout)
axis equal vis3d

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[/CODE]
 
  • Like
Likes   Reactions: saeede-
Arjan82 said:
I woud do it like this:
[CODE lang="matlab" title="Matlab"]
clear all
close all
clc

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

[t,p]=ode45(@sattelite,[0,12000],p0);
xout=p(:,1);
yout=p(:,2);
zout=p(:,3);
vx=p(:,4);
vy=p(:,5);
vz=p(:,6);

[X,Y,Z] = sphere;
X = X*R;
Y = Y*R;
Z = Z*R;
surf(X,Y,Z);
hold on

plot3(xout,yout,zout)
axis equal vis3d

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[/CODE]
oh.. thanks. it works.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K