Escolar Documentos
Profissional Documentos
Cultura Documentos
m 1 of 3
% 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')
PlotVector = 1:N;
PlotResult(PlotVector)
end %function
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
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
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
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
%~~~~~~~~~~~~~~~~~~~~~~~~