How to trace the path

14 views (last 30 days)
Juan Palacios
Juan Palacios on 9 May 2019
Commented: Adam Danz on 9 May 2019
I have written the code below for orbits of celetial bodies. However i am unable trace the path of the bodies. How would i do that? can anyone help please
function [p, v] = solarsystemM(p, v, mass, stop_time, hide_animation)
% SOLARSYSTEM Solve the gravitational dynamics of n bodies.
%
% SOLARSYSTEM(p, v, mass, stop_time) receives the initial position, initial
% velocity, and mass, and simulate for stop_time seconds. The inputs p and
% v are N-by-2 matrices where the Nth row refers to the Nth celestial
% body. The two columns are the X and Y values of position and velocity,
% respectively. The input mass is an N-by-1 vector where the Nth item is
% the mass of the Nth celestial body.
%
% [p, v] = SOLARSYSTEM(...) returns the final position and final velocity
% of each object in the same format as the input. This will be used during
% marking to test the accuracy of your program.
%
% SOLARSYSTEM(..., hide_animation) is an optional flag to indicate whether
% the graphical animation may be hidden. This is an advanced feature for
% students who are aiming for a high level of achievement. During marking,
% when the computation speed of your program is being tested, it will be
% run with the hide_animation flag set to true. This allows your program to
% skip the drawing steps and run more quickly.
%
if nargin < 5
hide_animation = false;
end
% Below is example code showing how to read the initial position and
% velocity into the same notation as the assignment sheet, to help you get
% started on the two-body simulation:
% How to pull values out of p and v:
% p1 = p(1,:); % planet 1 position vector
% p2 = p(2,:); % planet 2 position vector
%
% v1 = v(1,:); % planet 1 velocity vector
% v2 = v(2,:); % planet 2 velocity vector
%
% m1 = mass(1); % planet 1 mass
% m2 = mass(2); % planet 2 mass
% Write your code here
% Constants
t = 0;
dT = 4800;
G = 6.673e-11;
N = size(p,1);
a = zeros(N,2);
hist = [0:dT:stop_time];
% L = zeros((N),numel(hist));
% v = v + (a*dT);
% p = p + (v*dT);
% r12 = p2 - p1
% F12 = (-G.*m1.*m2).*r12/norm(r12)^3
% Creating vectors for different color celestial bodies
C(1,:)=[1 0 0];
C(2,:)=[0 1 0];
C(3,:)=[0 0 1];
% Caculating speed velocity and and acceleration
% a1 = 1/m2.*F12
% V = v + a1*dT
% P = p2 + (V*dT)
% Plotting of the figures
if ~hide_animation
figure (1);
clf;
hold on;
xlim([-2e11 2e11]);
ylim([-2e11 2e11]);
xlabel('X axis')
ylabel('Y axis')
title ('Orbit simulations of clestial bodies')
%Add for loop for the extra celestial bodies to be plotted
for i = 1:N
h(i) = plot(p(i,1),p(i,2), '*','markerfacecolor',C(i,:),'markersize',5);
end
end
while t < stop_time
for i = 1:N
a(i,:) = 0;
for j = 1:N
if i~=j
rij = p(j,:) - p(i,:);
Fij = G.*((mass(i).*mass(j))./(norm(rij)^3)).*rij;
Fji = -Fij;
a(i,:) = a(i,:) +(1/mass(i)).*Fij;
end
end
end
%Updating my velocity and position
v = v + (a*dT);
p = p + (v*dT);
if ~hide_animation
for i = 1:N
set(h(i),'Xdata', p(i,1),'Ydata', p(i,2));
end
end
t = t + dT;
drawnow limitrate
end
% How to pack the results back into the return values
p = p(1:N,:);
v = v(1:N,:);
end
  9 Comments
Juan Palacios
Juan Palacios on 9 May 2019
do you have both the codes open when you run it?
My function simulates the orbit of two "planets" orbiting the sun. I just want to be able to add a trace to them, so it leaves a path of their orbit.
Adam Danz
Adam Danz on 9 May 2019
Assuming you have the (x,y) coordinates of the orbits, why doesn't this work?
plot(x,y, '-')

Sign in to comment.

Answers (0)

Categories

Find more on Earth and Planetary Science in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!