在Matlab中围绕身体模拟轨道运动

时间:2014-11-10 13:02:53

标签: matlab simulation gravity satellite

我正在研究一个非常简单的程序来模拟身体绕另一个身体的轨道运动,就像地球周围的卫星一样。我已经遵循了书籍和互联网中可用的指南和方程式,但对象似乎根本没有轨道运行。如果有人可以,请帮助我。提前致谢。代码在

之下
v0=80;
theta=45*pi/180;

vx(1)=v0*cos(theta);
vy(1)=v0*sin(theta);
px(1)=0;
py(1)=0;

mass=100; %kg
cmass=400; % mass of the body at 400,500
    ax=0;
%
g=9.8 ;%m/s^2
ay=-g;

p2x=400; % x co-ordinate of the stationary body
p2y=500; % y co-ordinate of the stationary body

G=6.674*10^-11; % the Gravitational Constant
figure(1)
plotsize=800;
i=1;
dt=.1;
t=0;
while(t<20)

%a2x=a2x-0.10;
%a2y=a2y+0.50;
r=sqrt((p2x-px(i))^2+(p2y-py(i))^2); % distance between the two bodies
F=((G*mass*cmass)/r^2);   % force by formula f=(G*m1*m2)/r^2
a=-(1/cmass^2)*F;    % acceleraion  a=1/m1^2*F

vx(i+1)=vx(i)+(ax)*dt;
vy(i+1)=vy(i)+(ay)*dt;

px(i+1)=px(i)+vx(i)*dt;
py(i+1)=py(i)+vy(i)*dt;

hold off
plot(px(i+1),py(i+1),'o','MarkerSize',15)

hold on
plot(px,py,'r')
plot(p2x,p2y);
axis([0 plotsize 0 plotsize])
pause(.1) %pause for graphics
i=i+1;
t=t+dt;

1 个答案:

答案 0 :(得分:0)

我相当肯定你的逻辑是关闭的。你想以dt的步长迭代时间t,n次,并且在时间达到20个单位(年?)之后停止。在t <20的迭代期间,您希望根据时间适当调整参数。所以我认为你应该使用for循环并在for循环中嵌套while条件以检查t是否确实小于20.换句话说,对于i = 1:20,而t <20做东西,否则停止做的东西!