Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pendulum_waves
- % pendulum waves
- clc;clear;close all
- load('matlab.mat');
- g=10;T=180;dt=0.02;t=0:dt:T-dt;
- opts=odeset('RelTol',1e-10,'AbsTol',1e-10, ...
- 'MaxStep',1e-2,'InitialStep',1e-4);
- for n=1:length(L)
- [~,u]=ode45(@(t,u)pw(t,u,g,L(n)),t,[pi/6 0],opts);
- y(:,n)=-L(n)*cos(u(:,1));
- x(:,n)=L(n)*sin(u(:,1));
- end
- % 绘图
- f=figure;f.ToolBar='none';f.MenuBar='none';
- f.Renderer='painters';% 提升2D渲染性能
- pos1=[0 0 1 1];pos2=[0.05 0.05 0.9 0.9];
- subplot('Position',pos2)
- dpi=1.25;scr=[1920 1080];res=[1280 960];
- fig=gcf;fig.Color='k';fig.Visible='on';
- fig.Position=[(scr-res)/2,res]/dpi;
- % 稳定画面辅助线
- plot([-2 -2 2],[-2.75 0.25 0.25],'k');hold on
- % 摆线
- for n=1:length(L)
- lines(n)=line([0 x(1,n)],[0 y(1,n)],'LineWidth',1.5);
- end
- % 摆球
- p=plot(x(1,:),y(1,:),'.','markersize',40,'Color','y');
- axis equal;axis off
- % 动画
- mv = 0;% 是否录制视频,1录制,其它不录制
- if mv==1
- v=VideoWriter('D:\Programs\MATLAB\蛇摆.mp4','MPEG-4');
- v.Quality=100;v.FrameRate=30;
- open(v);
- end
- tic
- for ii=1:length(t)
- p.XData=x(ii,:);p.YData=y(ii,:);
- for n=1:length(L)
- lines(n).XData=[0 x(ii,n)];
- lines(n).YData=[0 y(ii,n)];
- end
- drawnow
- if mv==1;F=getframe(fig);writeVideo(v,F);end
- clc;fprintf(['当前进度', ...
- num2str(100*ii/length(t),'%.1f'),'%%\n']);
- end
- if mv==1;close(v);end
- fprintf(['绘图用时',num2str(toc),'秒\n']);
- %% 函数
- function du=pw(~,u,g,L)
- du=[u(2);-g/L*sin(u(1))];
Add Comment
Please, Sign In to add comment