Help regarding 2 body planetary simulation
Show older comments
Hi,
I've been tasked to create a simple 2D planetary simulation involving a stationary sun and 3 orbiting planets. I started out trying to create a simulation involving the sun and 1 planet only and simplified their masses and the constant G. I believe the acceleration should be constant which means once I find the acceleration I will reuse it to find the new velocities and positions.
I'm quite new to MatLab and as such the code I want should be simple. We are tasked to solve this using Newton's gravitational laws. Furthermore the planets should only be affected by the gravitational force from the sun (and the sun shouldn't be affected by any gravitational force from the planets).
Here is my attempt so far:
%%Constants
global G m_0 m_1
m_0=1000; % Sun mass
m_1=1; % Earth mass
G=10^(-3); %Gravitational constant
n=1000; % Steps
dt=0.1; % Stepsize
%%Equations
% R_se = sqrt(x^2 + y^2); % Distance between earth and sun
% F_se = G*m_0*m_1/R_se^2; % The force the sun affects the earth with
% a_se =G*m_0/R_se^2; % a_es = F_es/m_1 Found with Newtons 2. law f=m*a
% xdotdot= -G*m_0*x/R_se^(3/2) % The acc. in x
% ydotdot= -G*m_0*y/R_se^(3/2) % The acc. in y
%%Start pos. and velocity for sun and earth
%Sun - stationary
x_sun=0;
y_sun=0;
vx_sun=0;
vy_sun=0;
%%Earth - these values have been randomly chosen but I wish to use Ephimerides (?) from Nasa with actual values once I get my code working
x=-10;
y=11;
vx=9;
vy=-5;
Calculations
%%Finding start values for radius and acc. (Because im assuming acc. to be constant - which im afraid may not actually be true)
R_se = sqrt(x^2 + y^2);
ax= -G*m_0*x/R_se^(3/2);
ay= -G*m_0*y/R_se^(3/2);
for n=1:n-1
%%Finding new values for radius, velocity and position.
vx_new=vx+ax*dt;
vy_new=vy+ay*dt;
x_new=x+vx_new*dt;
y_new=y+vy_new*dt;
R_se_new=sqrt(x_new^+y_new^2);
vx=vx_new;
vy=vy_new;
x=x_new;
y=y_new;
R_se=R_se_new;
hold on
plot(x_new,y_new),'.');
axis([-100 100 -100 100]);
drawnow;
end
So my thinking here is that first I find the acceleration which will be constant. Then I find the new velocity in the x and y plane by using the old velocity. From that I find the new positions using the old position and the new velocity times the stepsize. And then i find a new radius between the earth and sun by using the new positions.
Once this is over I say that the old values are the new ones so that when it loops back the old values will be the new ones and the newer values will be determined by the new ones. (If you know what I mean).
The problem:
When I run this I do get a figure but depending on the start values i choose for earth i get 2 moving lines of dots only going in the x plane. I have tried changing the values so many times with no luck.
First of all the sun shouldn't be moving and should only appear as one stationary dot and second of all the earth should be moving in a circle around the sun.
Sorry for the big wall of text!
3 Comments
Image Analyst
on 17 Dec 2016
REA
on 17 Dec 2016
Image Analyst
on 17 Dec 2016
Yes that's better, though you might have gotten rid of the double spacing which you probably put in when you didn't know how to format it properly. Be sure to read John's answer below.
Accepted Answer
More Answers (0)
Categories
Find more on Earth and Planetary Science in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!