matlab制作圆摆线动画
生活随笔
收集整理的這篇文章主要介紹了
matlab制作圆摆线动画
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
matlab制作圓擺線動畫
- 前言
- 圓滾線
- 螺旋線
- 外圓擺線
- 內圓擺線
前言
matlab是一款非常著名的科學計算和仿真軟件,以其強大的數學庫、數據可視化功能和擴展工具箱而聞名,這里利用matlab繪制了圓的四種擺線的形成動畫,話不多言,直接上代碼。
圓滾線
%畫圓滾線 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/ws=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);elseplot([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圖片:
螺旋線
%畫螺旋線 v=2; %圓心的增長速度 w=1.5; %旋轉的角速度 figure; title('螺旋線'); hold on; axis equal; axis([-15 15 -15 15]); for time=0:0.1:2*pix=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);elseplot([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圖片:
外圓擺線
%畫外圓擺線 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/w1xx = (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);elseplot([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;執行后生成的圖片:
內圓擺線
%畫外圓擺線 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/w1xx = (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);elseplot([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圖片:
總結
以上是生活随笔為你收集整理的matlab制作圆摆线动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言应用(3)——Base64编码/解
- 下一篇: 计算机与制药工程论文,制药工程论文范文