# 【运动学】基于matlab嫦娥奔月仿真【含Matlab源码 1238期】

## 二、部分源代码

``````function TaiYang_DiQiu_YueLiang_ChangE
clf;cla;clear all;close all;
figure('color','k');
hold on;
axis equal ;
axis off;
set(gcf,'doublebuffer','on');
axis([-70 70 -70 70]);
theta=0:0.01*pi:2*pi;
t=0.005;
R1=42;%设置太阳，地球，月亮，嫦娥之间的距离
R2=20;
R3=6;
W1=5;%设置地球，月亮，嫦娥的角速度
W2=25;
W3=65;
state1=0;state2=0;state3=0;
plot(0,0,'marker','o','markersize',20,'markerfacecolor','r','markeredgecolor','r');%作太阳
X_DiQiu_GuiJi=R1*cos(theta);
Y_DiQiu_GuiJi=R1*sin(theta);
plot(X_DiQiu_GuiJi,Y_DiQiu_GuiJi,'-b','linewidth',2);%作地球轨迹
%做地球,月亮，嫦娥的初始位置
H_DiQiu=plot(R1*cos(state1),R1*sin(state1),'marker','o','markersize',12,'markerfacecolor','b');
%作月亮运动轨迹线
H_YueQiu=plot(R1*cos(state1)+R2*cos(state2),R1*sin(state1)+R2*sin(state2),'marker','o','markersize',8,'markerfacecolor','m','markeredgecolor','m');
xx2=R1*cos(state1)+R2*cos(state2);yy2=R1*sin(state1)+R2*sin(state2);
YueLiang_GuiJi=plot(xx2,yy2,'color','w');
%作嫦娥运动轨迹线
H_ChangE=plot(R1*cos(state1)+R2*cos(state2)+R3*cos(state3),R1*sin(state1)+R2*sin(state2)+R3*sin(state3),'marker','o','markersize',4,'markerfacecolor','c','markeredgecolor','c');

xx3=R1*cos(state1)+R2*cos(state2)+R3*cos(state3);yy3=R1*sin(state1)+R2*sin(state2)+R3*sin(state3);
ChangE_GuiJi=plot(xx3,yy3,'color','m');
%保存GIF文件
f=getframe(gcf);
f=frame2im(f);
[f map]=rgb2ind(f,256);
imwrite(f,map,'ChangE_GuiJi.gif','delaytime',0.1,'loopcount',inf);

for i=1:1.3*length(theta)
x1=R1*cos(state1);y1=R1*sin(state1);
x2=R1*cos(state1)+R2*cos(state2);y2=R1*sin(state1)+R2*sin(state2);
xx2=[xx2 x2];yy2=[yy2 y2];
x3=R1*cos(state1)+R2*cos(state2)+R3*cos(state3);y3=R1*sin(state1)+R2*sin(state2)+R3*sin(state3);
xx3=[xx3 x3];yy3=[yy3 y3];
set(H_DiQiu,'xdata',x1,'ydata',y1) ;
set(H_YueQiu,'xdata',x2,'ydata',y2) ;
set(H_ChangE,'xdata',x3,'ydata',y3) ;
set(YueLiang_GuiJi,'xdata',xx2,'ydata',yy2) ;
set(ChangE_GuiJi,'xdata',xx3,'ydata',yy3) ;
state1=state1+W1*t;
state2=state2+W2*t;
state3=state3+W3*t;
pause(0.1);
drawnow;

%保存GIF文件
f=getframe(gcf);
f=frame2im(f);
[f map]=rgb2ind(f,256);
imwrite(f,map,'ChangE_GuiJi.gif','delaytime',0.1,'writemode','append');
end

## 四、matlab版本及参考文献

1 matlab版本 2014a

2 参考文献 [1] 门云阁.MATLAB物理计算与可视化[M].清华大学出版社，2013. [2]梅中磊,李月娥,马阿宁.MATLAB电磁场与微波技术仿真[M].清华大学出版社，2020. [3]徐国保,张冰,石丽梅,吴凡.MATLAB/Simulink权威指南——开发环境、程序设计、系统仿真与案例实战[M].清华大学出版社，2019. [4]周群益,侯兆阳,刘让苏.MATLAB可视化大学物理学(第2版)[M].清华大学出版社，2015.