function SunEarthMoon
load planets;
[sun, earth, moon] = deal(18, 3, 25);%单个赋值,sun=18应该是质量吧,很多数据,18 3 25应该是对应所在位置
list = [sun, earth, moon]; % 12 24 lis成为一个数组
G = 6.67e-11; %gravitational constant引力常量
dt = 24*3600;%一天为一个基本常量
N = length(list);%有3个星球
mass = mass(list);
position = position(list,:);%应该是初始位置
velocity = velocity(list,:);
h = plotplanets(position);
for t = 1:365
plotplanets(position,h);
force = zeros(N,3);
for i = 1 : N
Pi = position(i,:);
Vi = velocity(i,:);
Mi = mass(i);
for j = (i+1):N; %the i+1 is in to create diagonal
Mj = mass(j);
Pj = position(j,:);
dr = Pj - Pi; %方向的指向应该是从i指向j
forceij = G*Mi*Mj./(norm(dr).^3).*dr;%norm函数是用来求向量的摸
force(i,:) = force(i,:) + forceij;
force(j,:) = force(j,:) - forceij;
end;
end
velocity = velocity + force./repmat(mass,1,3)*dt;%重构函数,将mass变成一行3列
position = position + velocity*dt;
end
% -
function h = plotplanets(pos,h)
scale = 50;
total_planets = size(pos,1);
[sun, earth, moon] = deal(1, 2, 3);
radius = [50, 30, 20];
marker = {'.r', 'b.','m.'};
pos(moon,:) = pos(earth,:) + scale*(pos(moon,:)-pos(earth,:));
if nargin==1
hold on; axis image
axis( [-2 2 -2 2]*1e11 );
for i = 1:total_planets
if any(i == [sun, earth, moon])
h(i) = plot(pos(i,1),pos(i,2),marker{i},'markersize',radius(i));
plot(pos(i,1), pos(i,2), marker{i}, 'markersize',5);
else
h(i) = plot(pos(i,1), pos(i,2), 'k.', 'markersize', 20);
plot(pos(i,1), pos(i,2), 'k.', 'markersize',5);
end
end
else
for i = 1:total_planets
set(h(i), 'Xdata', pos(i,1) , 'Ydata', pos(i,2) )
if any(i == [sun, earth, moon])
plot(pos(i,1), pos(i,2), marker{i}, 'markersize',5);
else
plot(pos(i,1), pos(i,2), 'k.', 'markersize',5);
end
end
drawnow
end
在这里面,第二个函数h = plotplanets(pos,h),以形式参数作为返回参数不太懂,还有画图的这个问题,这段函数有谁能来探讨一下吗?