Y-BH

pendulum_waves

Mar 26th, 2022 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.49 KB | None | 0 0
  1. function pendulum_waves
  2. % pendulum waves
  3. clc;clear;close all
  4. load('matlab.mat');
  5. g=10;T=180;dt=0.02;t=0:dt:T-dt;
  6. opts=odeset('RelTol',1e-10,'AbsTol',1e-10, ...
  7.     'MaxStep',1e-2,'InitialStep',1e-4);
  8. for n=1:length(L)
  9.     [~,u]=ode45(@(t,u)pw(t,u,g,L(n)),t,[pi/6 0],opts);
  10.     y(:,n)=-L(n)*cos(u(:,1));
  11.     x(:,n)=L(n)*sin(u(:,1));
  12. end
  13. % 绘图
  14. f=figure;f.ToolBar='none';f.MenuBar='none';
  15. f.Renderer='painters';% 提升2D渲染性能
  16. pos1=[0 0 1 1];pos2=[0.05 0.05 0.9 0.9];
  17. subplot('Position',pos2)
  18. dpi=1.25;scr=[1920 1080];res=[1280 960];
  19. fig=gcf;fig.Color='k';fig.Visible='on';
  20. fig.Position=[(scr-res)/2,res]/dpi;
  21. % 稳定画面辅助线
  22. plot([-2 -2 2],[-2.75 0.25 0.25],'k');hold on
  23. % 摆线
  24. for n=1:length(L)
  25.     lines(n)=line([0 x(1,n)],[0 y(1,n)],'LineWidth',1.5);
  26. end
  27. % 摆球
  28. p=plot(x(1,:),y(1,:),'.','markersize',40,'Color','y');
  29. axis equal;axis off
  30. % 动画
  31. mv = 0;% 是否录制视频,1录制,其它不录制
  32. if mv==1
  33. v=VideoWriter('D:\Programs\MATLAB\蛇摆.mp4','MPEG-4');
  34. v.Quality=100;v.FrameRate=30;
  35. open(v);
  36. end
  37. tic
  38. for ii=1:length(t)
  39.     p.XData=x(ii,:);p.YData=y(ii,:);
  40.     for n=1:length(L)
  41.         lines(n).XData=[0 x(ii,n)];
  42.         lines(n).YData=[0 y(ii,n)];
  43.     end
  44.     drawnow
  45.     if mv==1;F=getframe(fig);writeVideo(v,F);end
  46.     clc;fprintf(['当前进度', ...
  47.         num2str(100*ii/length(t),'%.1f'),'%%\n']);
  48. end
  49. if mv==1;close(v);end
  50. fprintf(['绘图用时',num2str(toc),'秒\n']);
  51. %% 函数
  52. function du=pw(~,u,g,L)
  53. du=[u(2);-g/L*sin(u(1))];
Add Comment
Please, Sign In to add comment