matlab制作圆摆线动画

前言

matlab是一款非常著名的科学计算和仿真软件,以其强大的数学库、数据可视化功能和扩展工具箱而闻名,这里利用matlab绘制了圆的四种摆线的形成动画,话不多言,直接上代码。

圆滚线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

%画圆滚线
t=0:pi/100:2*pi;
r=1; %圆的半径
v=1.5; %圆心滚动的速度
w=v/r; %圆滚动的角速度
s=0;
figure;
title('圆滚线');
hold on;
axis equal;
axis([-r 2*pi*r+r -r r]);
for time=0:0.1:2*pi/w
s=v*time;
x=s+r*cos(t);
point_x = s+r*cos(-pi/2-w*time);
y=r*sin(t);
point_y = r*sin(-pi/2-w*time);
h=plot(x,y,'r');
l=plot([s point_x],[0 point_y],'-r');
if(abs(time-0)<1e-3)
plot(point_x,point_y,'.g');
res_x=point_x;
res_y=point_y;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'圆滚线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x point_x],[res_y point_y],'-g');
res_x=point_x;
res_y=point_y;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'圆滚线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.1); %制作gif时建议去掉时延
delete(h);
delete(l);
end
hold off;

运行后生成的gif图片:
使用matlab生成的圆滚线图片

螺旋线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
%画螺旋线
v=2; %圆心的增长速度
w=1.5; %旋转的角速度
figure;
title('螺旋线');
hold on;
axis equal;
axis([-15 15 -15 15]);
for time=0:0.1:2*pi
x=v*time*cos(w*time);
y=v*time*sin(w*time);
l=plot([0 x],[0 y],'-r');
if(abs(time-0)<1e-3)
plot(x,y,'.g');
res_x=x;
res_y=y;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'螺旋线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x x],[res_y y],'-g');
res_x=x;
res_y=y;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'螺旋线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.1); %制作gif时建议去掉时延
delete(l);
end
hold off;

执行后生成的gif图片:
在这里插入图片描述

外圆摆线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
%画外圆摆线
w1 = 3; %外圆圆心旋转的角速度
r1 = 4; %内圆的半径
r2 = 1; %外圆的半径
w2 = 9; %外圆滚动的角速度
pp = 0:pi/100:2*pi;
figure;
title('外圆摆线');
hold on;
axis equal;
axis([-(r1+2*r2) r1+2*r2 -(r1+2*r2) r1+2*r2]);
x=r1*cos(pp);
y=r1*sin(pp);
plot(x,y,'r');
for time=0:0.01:2*pi/w1
xx = (r1+r2)*cos(w1*time);
yy = (r1+r2)*sin(w1*time);
x = xx+r2*cos(pp);
y = yy+r2*sin(pp);
px = xx+r2*cos(w2*time);
py = yy+r2*sin(w2*time);
h=plot(x,y,'r');
l=plot([xx,px],[yy,py],'-r') ;
if(abs(time-0)<1e-3)
plot(px,py,'.g');
res_x=px;
res_y=py;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'外圆滚线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x px],[res_y py],'-g');
res_x=px;
res_y=py;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'外圆滚线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.01); %制作gif时建议去掉时延
delete(h);
delete(l);
end
hold off;

执行后生成的图片:
在这里插入图片描述

内圆摆线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
%画外圆摆线
w1 = 3; %内圆圆心旋转的角速度
r1 = 4; %外圆的半径
r2 = 1; %内圆的半径
w2 = 9; %内圆滚动的角速度
pp = 0:pi/100:2*pi;
figure;
title('内圆摆线');
hold on;
axis equal;
axis([-(r1+2*r2) r1+2*r2 -(r1+2*r2) r1+2*r2]);
x=r1*cos(pp);
y=r1*sin(pp);
plot(x,y,'r');
for time=0:0.01:2*pi/w1
xx = (r1-r2)*cos(w1*time);
yy = (r1-r2)*sin(w1*time);
x = xx+r2*cos(pp);
y = yy+r2*sin(pp);
px = xx+r2*cos(w2*time);
py = yy+r2*sin(w2*time);
h=plot(x,y,'r');
l=plot([xx,px],[yy,py],'-r');
if(abs(time-0)<1e-3)
plot(px,py,'.g');
res_x=px;
res_y=py;
%生成第一张图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'内圆滚线.gif','gif','Loopcount',inf,'DelayTime',1e-3);
else
plot([res_x px],[res_y py],'-g');
res_x=px;
res_y=py;
%生成后续图片,并写入gif文件
frame=getframe(gcf);
imind=frame2im(frame);
[imind,cm]=rgb2ind(imind,256);
imwrite(imind,cm,'内圆滚线.gif','gif','WriteMode','append','DelayTime',1e-3);
end
%pause(0.01); %制作gif时建议去掉时延
delete(h);
delete(l);
end
hold off;

执行后生成的gif图片:
在这里插入图片描述