MATLAB中单摆怎么保存成gif格式的
来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/10 06:07:31
MATLAB中单摆怎么保存成gif格式的
%制作动画
%挂摆横梁
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
参考自: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
将那个循环的代码改成:
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
MATLAB中单摆怎么保存成gif格式的
PPT中做好的幻灯片怎么保存为gif
请问matlab中怎么将数据保存为.dat格式
怎么将matlab中数据保存为txt或dat格式
MATLAB 中有两个变量的函数怎么保存数据?
怎么把matlab中FOR循环得到的值保存到矩阵中
求matlab程序for循环中怎么保存中间变量
用mathtype编辑的公式怎么保存为文字格式
怎么将这个matlab命令保存成可运行的M文件?
MATLAB怎么在保存结果的EXCEL里面添加内容?
MATLAB 中计算矩阵的循环里 想加一段语句保存所得矩阵 怎么加 用什么语句
找一张GIF格式的动物捕食片段 最好是 狮子之类的