Simulating earth rotating the sun with eulers method.
Afficher commentaires plus anciens
Hi! I’m trying to make a simulation of earths rotation around the sun, and for some weird reason my angle does not change. I’m using a formula I found from the internet, but it seems to work for that person. I think its something wrong with my constants, because I just get a linear movement when I plot it.
%% Initial values
%Constants and initialising vectors
G=6.674*10^(-11); %gravitational constant
M=1.989e30; %Mass of sun
m=5.927e24; %Mass of earth
h=1; %timestep
t=[0:h:12];
%%t=[0:h:3.154e7];
distance=[];
speed=[];
angle=[];
angular_velocity=[];
x=[];
y=[];
%Initial conditions for speed
init_distance=1.496e11;
init_speed=0;
%Intial conditions for angle
init_angle=deg2rad(23.5);
init_angular_velocity=1.99e-7;
%% Settings first values
distance(1)=init_distance;
speed(1)=init_speed;
angle(1)=0;
angular_velocity(1)=init_angular_velocity;
dvdt=0;
dsdt=0;
x(1)=distance(1)*sin(angle(1));
y(1)=distance(1)*cos(angle(1));
%% Eulers Method
for i=2:length(t) %eulers method
dvdt=P(G,M,distance(i-1),angular_velocity(i-1));
speed(i)=speed(i-1)+dvdt*h;
distance(i)=distance(i-1)+speed(i)*h;
dsdt=S(angular_velocity(i-1),speed(i-1),distance(i-1));
angular_velocity(i)=angular_velocity(i-1)+dsdt*h;
angle(i)=angle(i-1)+angular_velocity(i)*h;
x(i)=distance(i)*sin(angle(i));
y(i)=distance(i)*cos(angle(i));
end
plot(0,0,'o','LineWidth',25,'markersize',5,'color','k'); %sun
hold on
%%plot(radius,t,'r') %earth orbiting around the sun using euler
plot(y,x);
function vel = P(Gravity,Mass,radius,ang_velocity)
vel=(radius*(ang_velocity^2) - (Gravity*Mass)/radius);
end
function angle= S(ang_velocity,velocity,radius)
angle=-((2*velocity*ang_velocity)/radius);
end
Réponse acceptée
Plus de réponses (1)
When I run your code, I do not see a linear movement:
%Constants and initialising vectors
G=6.674*10^(-11); %gravitational constant
M=1.989e30; %Mass of sun
m=5.927e24; %Mass of earth
h=1; %timestep
t=0:h:12;
%%t=[0:h:3.154e7];
distance=[];
speed=[];
angle=[];
angular_velocity=[];
x=[];
y=[];
%Initial conditions for speed
init_distance=1.496e11;
init_speed=0;
%Intial conditions for angle
init_angle=deg2rad(23.5);
init_angular_velocity=1.99e-7;
%% Settings first values
distance(1)=init_distance;
speed(1)=init_speed;
angle(1)=0;
angular_velocity(1)=init_angular_velocity;
dvdt=0;
dsdt=0;
x(1)=distance(1)*sin(angle(1));
y(1)=distance(1)*cos(angle(1));
%% Eulers Method
for i=2:length(t) %eulers method
dvdt=P(G,M,distance(i-1),angular_velocity(i-1));
speed(i)=speed(i-1)+dvdt*h;
distance(i)=distance(i-1)+speed(i)*h;
dsdt=S(angular_velocity(i-1),speed(i-1),distance(i-1));
angular_velocity(i)=angular_velocity(i-1)+dsdt*h;
angle(i)=angle(i-1)+angular_velocity(i)*h;
x(i)=distance(i)*sin(angle(i));
y(i)=distance(i)*cos(angle(i));
end
plot(0,0,'o','LineWidth',25,'markersize',5,'color','y'); %sun
hold on
plot(y,x);
function vel = P(Gravity,Mass,radius,ang_velocity)
vel=(radius*(ang_velocity^2) - (Gravity*Mass)/radius);
end
function angle= S(ang_velocity,velocity,radius)
angle=-((2*velocity*ang_velocity)/radius);
end
4 commentaires
Roble Noor
le 9 Déc 2021
William Rose
le 9 Déc 2021
Roble Noor
le 9 Déc 2021
William Rose
le 10 Déc 2021
@Roble Noor, you're welcome! Good luck with your work.
Catégories
En savoir plus sur Axes Transformations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

