Simulating earth rotating the sun with eulers method.
이전 댓글 표시
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
채택된 답변
추가 답변 (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
Roble Noor
2021년 12월 9일
William Rose
2021년 12월 9일
Roble Noor
2021년 12월 9일
William Rose
2021년 12월 10일
@Roble Noor, you're welcome! Good luck with your work.
카테고리
도움말 센터 및 File Exchange에서 Gravitation, Cosmology & Astrophysics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

