# How to plot an orbit around the Earth in MATLAB

• MATLAB
• saeede-
In summary, the conversation is about plotting an orbit around Earth with a sphere in MATLAB. The code provided includes the necessary steps to plot the orbit in 3D and also adds a sphere representing Earth. It also includes a function to calculate the position and velocity of the satellite.
saeede-
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

I woud do it like this:
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

saeede-
Arjan82 said:
I woud do it like this:
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
oh.. thanks. it works.

## 1. How do I plot an orbit around the Earth in MATLAB?

To plot an orbit around the Earth in MATLAB, you will need to first define the parameters of the orbit such as the orbital period, semi-major axis, eccentricity, and inclination. Then, you can use the built-in orbit plotting functions in MATLAB such as "plot3" or "polarplot" to generate the orbit.

## 2. What is the difference between a circular and elliptical orbit in MATLAB?

A circular orbit in MATLAB has an eccentricity of 0, which means the orbit is a perfect circle. An elliptical orbit, on the other hand, has an eccentricity greater than 0, which results in a more elongated orbit.

## 3. Can I plot multiple orbits around the Earth in MATLAB?

Yes, you can plot multiple orbits around the Earth in MATLAB by using a loop to define the parameters for each orbit and then plotting them all on the same graph. This can be useful for visualizing the orbits of multiple satellites or spacecraft.

## 4. How can I add labels and titles to my orbit plot in MATLAB?

To add labels and titles to your orbit plot in MATLAB, you can use the "xlabel", "ylabel", and "title" functions. These functions allow you to specify the text and position of the labels and titles on your plot.

## 5. Is there a way to animate an orbit plot in MATLAB?

Yes, you can animate an orbit plot in MATLAB by using the "animation" function. This function allows you to specify the number of frames, time interval, and other parameters to create a dynamic visual representation of the orbit.

• MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
• MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
6
Views
953
• MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
817
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K