Você está na página 1de 3

11/8/14 3:37 PM C:\Users\A7madKhairy\Documents\MATLAB\Orbi...\Nbody.

m 1 of 3

function Nbody(N,M,Names,D0 , tspan)


%This program presents the solution of the motion of N bodies under
%gravitational effects in cartesian co-ordinates

% N number of bodies
% M nust be column vector of size N,1 containing the masses
% DI a matrix of size (N , 6 ) providing the initial values for positions
% and velocities of each body in a row in the order [x0 y0 z0 Vx0 Vy0 Vz0]
% tspan on the form [t0 tf ]
Nvec = 1:N;
G = 6.67259e-20;
m= sum(M);
dummy = D0';
y0 = dummy(:);
%% Calling The Integrator rkf45
[t,yout] = ode45(@rates, tspan, y0);
Lt = length(t);
% for better handling the output %see testcode1.m
TheOutput = reshape(yout',[6 N Lt]);

%% Center Of Mass
%...Locate the center of mass at each time step:
XG = zeros(Lt,1);
YG = zeros(Lt,1);
ZG = zeros(Lt,1);
for ii = 1:Lt
for jj = 1:length(M)
XG(ii)= XG(ii)+ (M(jj)* TheOutput(1,jj,ii));
YG(ii)= YG(ii)+ (M(jj)* TheOutput(2,jj,ii));
ZG(ii)= ZG(ii)+ (M(jj)* TheOutput(3,jj,ii));
end
end
XG = XG/m ;
YG = YG/m ;
ZG = ZG/m ;
CG = [t XG YG ZG ];
save('CGNbody.txt','CG','-ascii')

% visualising the result

PlotVector = 1:N;
PlotResult(PlotVector)

%% the differential equation function rates


function N_1vec = excludeI(N,i)
% returns a row vector from 1: N and another 1 excluding certain nuber i
N_1vec = zeros(1,N-1);
cntr = 1;
for uu=1:N
if Nvec(uu)~=i
N_1vec(cntr)=Nvec(uu);
cntr=cntr+1;
end % if
end %for
11/8/14 3:37 PM C:\Users\A7madKhairy\Documents\MATLAB\Orbi...\Nbody.m 2 of 3

end %function

function dydt = rates(t,y)


% evaluates acceleration of each member from their positions and velocities
% returns the derivative of the state vector y to be used in an integratot

YY = reshape(y,[6 N]);
YY = YY'; %N.6
Mout=zeros(size(YY));
% reshaping to a matrix whose rows contans 6 state information[Rx Ry Rz Vx Vy Vz] of body n

%RIJ3(i,j) cube of the distance between mi and mj (km ?3)


RIJ3 = zeros(N);
for i = 1:N
for j = 1:N
if j~= i
if RIJ3(i,j)==0 % the matrix is symmetric so we don't need to calculate both Rij & Rji
Rij=[(YY(j,1)-YY(i,1)) ; (YY(j,2)-YY(i,2)) ; (YY(j,3)-YY(i,3))];
RIJ3(i,j)=norm(Rij)^3;
RIJ3(j,i)=RIJ3(i,j); dumVariable= norm ()
end %if then assign to save time of going to index
end %if

end
end

% calculate output matrix of N rows containing [Vx Vy Vz AX AY AZ] for each body

for i = 1:N
Mout(i,1)=YY(i,4) ;
Mout(i,2)=YY(i,5) ;
Mout(i,3)=YY(i,6) ;
N_1vec = excludeI(N,i);

for j = N_1vec
AXji = M(j)*(YY(j,1)-YY(i,1))/RIJ3(i,j);
Mout(i,4) = Mout(i,4)+ AXji;
AYji = M(j)*(YY(j,2)-YY(i,2))/RIJ3(i,j);
Mout(i,5) = Mout(i,5)+ AYji;
AZji = M(j)*(YY(j,3)-YY(i,3))/RIJ3(i,j);
Mout(i,6) = Mout(i,6)+ AZji;
end
Mout(:,[4 5 6])=G*Mout(:,[4 5 6]);
% multiply acceleration terms by G once to save time
end % for
Moutt = transpose(Mout); % size (6,N)
dydt = Moutt(:); % on the form of derivative of state vector y size (6*N ,1)

end % rates

%% ThE Plot Function


function PlotResult (PV)

figure(1)
title(' Motion relative to the inertial frame', ...
'Fontweight','bold','FontSize', 16)
11/8/14 3:37 PM C:\Users\A7madKhairy\Documents\MATLAB\Orbi...\Nbody.m 3 of 3

hold on
plot3(XG, YG, ZG )
for kk = 1:N
RX = reshape(TheOutput(1,kk,:),Lt,1);
RY = reshape(TheOutput(2,kk,:),Lt,1);
RZ = reshape(TheOutput(3,kk,:),Lt,1);
plot3(RX,RY,RZ)
end
xlabel('X(km)'); ylabel('Y(km)') ; zlabel('Z(km)')
% legend ('CG',Names)
% axis equal
% grid on
common_axis_settings

%%%%%%% relative to bodies desired in plotVector


for hh = 1:length(PV)
figure(hh+1)
Bndx=PV(hh); %body Index
BodyName = Names(Bndx);
title([' Motion relative to ', BodyName], ...
'Fontweight','bold','FontSize', 16)
hold on
B_1vec = excludeI(N,Bndx);
for kk = B_1vec
xrel = reshape(TheOutput(1,kk,:),Lt,1)-reshape(TheOutput(1,Bndx,:),Lt,1);
yrel = reshape(TheOutput(2,kk,:),Lt,1)-reshape(TheOutput(2,Bndx,:),Lt,1);
zrel = reshape(TheOutput(3,kk,:),Lt,1)-reshape(TheOutput(3,Bndx,:),Lt,1);
plot3(xrel, yrel ,zrel)
end
xlabel('X(km)'); ylabel('Y(km)') ; zlabel('Z(km)')
% legend (Names(B_1vec))
common_axis_settings
% grid on

end

end
function common_axis_settings
% wwwwwwwwwwwwwwwwwwwwwwwwwww
%{
This function establishes axis properties common to the several plots.
%}
% ---------------------------
text(0, 0, 0, 'o')
axis('equal')
view([2,4,1.2])
grid on
axis equal
xlabel('X (km)')
ylabel('Y (km)')
zlabel('Z (km)')
end %common_axis_settings
end % function
%~~~~~~~~~~~~~~~~~~~~~~~~

Você também pode gostar