MATLAB中单摆怎么保存成gif格式的
%制作动画%挂摆横梁plot([-0.3;0.3],[0;0],'color','y','linestyle','-',...'linewidth',10);g=0.98...
%制作动画
%挂摆横梁
plot([-0.3;0.3],[0;0],'color','y','linestyle','-',...
'linewidth',10);
g=0.98;
%重力加速度,可以调节摆的摆速
l=1;theta0=pi/4;x0=l*sin(theta0);y0=(-1)*l*cos(theta0);
axis([-0.75,0.75,-1.25,0]);
axis('on');
%不显示坐标轴
%创建摆锤
head=line(x0,y0,'color','r','linestyle','.',...
'erasemode','xor','markersize',40);
%创建摆杆
body=line([0;x0],[0;y0],'color','b','linestyle','-',...
'erasemode','xor');
%摆的运动
t=0;
dt=0.01;
while 1
t=t+dt;
theta=theta0*cos(sqrt(g/l)*t);
x=l*sin(theta);
y=(-1)*l*cos(theta);
set(head,'xdata',x,'ydata',y);
set(body,'xdata',[0;x],'ydata',[0;y]);
drawnow
end 展开
%挂摆横梁
plot([-0.3;0.3],[0;0],'color','y','linestyle','-',...
'linewidth',10);
g=0.98;
%重力加速度,可以调节摆的摆速
l=1;theta0=pi/4;x0=l*sin(theta0);y0=(-1)*l*cos(theta0);
axis([-0.75,0.75,-1.25,0]);
axis('on');
%不显示坐标轴
%创建摆锤
head=line(x0,y0,'color','r','linestyle','.',...
'erasemode','xor','markersize',40);
%创建摆杆
body=line([0;x0],[0;y0],'color','b','linestyle','-',...
'erasemode','xor');
%摆的运动
t=0;
dt=0.01;
while 1
t=t+dt;
theta=theta0*cos(sqrt(g/l)*t);
x=l*sin(theta);
y=(-1)*l*cos(theta);
set(head,'xdata',x,'ydata',y);
set(body,'xdata',[0;x],'ydata',[0;y]);
drawnow
end 展开
2个回答
展开全部
参考自:MATLAB CENTRAL - How can I create animated GIF images in MATLAB ?
将那个循环的代码改成:
outfile = 'x.gif';
for t = 0:.1:2*pi
theta=theta0*cos(sqrt(g/l)*t);
x=l*sin(theta);
y=(-1)*l*cos(theta);
set(head,'xdata',x,'ydata',y);
set(body,'xdata',[0;x],'ydata',[0;y]);
% drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im, 256);
if t == 0
imwrite(imind,cm,outfile,'gif','DelayTime',0,'loopcount',inf);
else
imwrite(imind,cm,outfile,'gif','DelayTime',0,'writemode','append');
end
end
博思aippt
2024-07-20 广告
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景。ai生...
点击进入详情页
本回答由博思aippt提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询