Y-BH

half_cycle_line

May 13th, 2020
1,392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.64 KB | None | 0 0
  1. % 质点离开竖直光滑半圆轨道的轨迹
  2. % 如运行有问题,建议使用R2020a或较新版本
  3. % R2016a或较旧版本请将微分方程函数、事件函数单独存为脚本函数
  4. % 如需转载此脚本请注明出处:
  5. % @YBH、ssrs@bilibili - https://space.bilibili.com/9943102
  6. clc;clear
  7. close all
  8. path=char('.\pic_seqs\');
  9. if exist(path,'dir')==7
  10.     rmdir(path,'s')% 删除旧文件
  11. end
  12. mkdir(path)
  13. dpi=1.25;% 系统缩放125%
  14. scr=[1920 1080];% 屏幕分辨率
  15. res=[1280 720];% 图像分辨率
  16. R=1;%轨道半径
  17. L=4*R;%水平轨长
  18. g=10;%重力加速度
  19. vu=sqrt(g*R);%单位速度
  20. tu=sqrt(R/g);%单位时间
  21. T=100*tu;%最大时间
  22. %半圆轨道
  23. tht=-0.5*pi:0.01:0.5*pi;
  24. xc=R*cos(tht);yc=R+R*sin(tht);
  25. %圆轨道
  26. tht=-pi:0.01:pi;
  27. xo=R*cos(tht);yo=R+R*sin(tht);
  28. odest=odeset('RelTol',1e-7,'AbsTol',1e-7,'NormControl','on',...
  29.     'MaxStep',tu*1e-1,'InitialStep',tu*1e-2,...
  30.     'Stats','off','Events',@(t,y) luodi_evns(t,y,R));
  31. v1=sqrt(2);%恰好到达圆轨道一半高度的速度
  32. v2=sqrt(5);%恰好到达圆轨道顶点的速度
  33. v3=sqrt(7/2);%恰好到达圆轨道底部的速度
  34. % 恰好通过轨道顶点时的轨迹
  35. v=v2*vu;
  36. [~,yd] = ode45(@(t,y) half_c(t,y,g,R),[0 T],[0 0 -0.5*pi v v 0]',odest);
  37. % 恰好落至圆轨道底部轨迹
  38. v=v3*vu;
  39. [~,yb] = ode45(@(t,y) half_c(t,y,g,R),[0 T],[0 0 -0.5*pi v v 0]',odest);
  40. for n=1:320
  41.     vv=n/40;
  42.     v=sqrt(n/40)*vu;
  43.     [~,y]=ode45(@(t,y) half_c(t,y,g,R),[0 T],[0 0 -0.5*pi v v 0]',odest);
  44.     %半圆轨道
  45.     plot(xc,yc,'Color','k','LineWidth',3)
  46.     hold on
  47.     axis equal
  48.     axis tight
  49. %     axis off
  50.     % 圆轨道
  51.     plot(xo,yo,'-.','Color','k','LineWidth',0.5)
  52.     %水平轨道
  53.     line([-L 0],[0 0],'linestyle','-','Color','k','LineWidth',3)
  54.     % 恰好通过最高点轨迹
  55.     pd=plot(yd(:,1),yd(:,2),'--','Color','#444444','LineWidth',1);
  56.     % 恰好落至圆轨道底部轨迹
  57.     pb=plot(yb(:,1),yb(:,2),'-.','Color','#444444','LineWidth',1);
  58.  
  59.     % 小球位置及运动轨迹
  60.     p=plot(y(:,1),y(:,2),'-','Color','r','LineWidth',2);
  61.     legend([pd,pb,p],{'恰好通过最高点轨迹','恰好落至圆轨道底部轨迹','小球轨迹'},...
  62.         'FontSize',16,'FontName','黑体','Location','northwest');
  63.     legend('boxoff')
  64.     a = gca;
  65.     a.TickLabelInterpreter = 'latex';
  66.     title(['$ v_0= \sqrt{',num2str(vv,'%10.3f'),'gR} $'],'position',[-2.6 1.42],...
  67.         'FontName','黑体','FontSize',16,'Color','#000000','Interpreter','latex');
  68.     hold off
  69.     set(gcf,'Color','w');% 设置白色背景
  70.     fig=gcf;
  71.     fig.Units = 'pixel';
  72.     fig.Position = [(scr-res)/2,res]/dpi;% 居中显示
  73.     m(n)=getframe(fig);% 获取当前图像
  74.     imwrite(m(n).cdata,strcat(path,num2str(n),'.png'));% 保存图像为png
  75.     n=n+1;
  76. end
  77. % 微分方程
  78. function dydt = half_c(~,y,g,R)
  79. a=y(4)^2/R;% 向心加速度
  80. if y(2)==0 && y(1)<0
  81.     dx=y(4);
  82.     dy=0;
  83.     dtheta=0;
  84.     dv=0;
  85.     dvx=0;
  86.     dvy=0;
  87. else
  88.     if y(1)>=0 && g*sin(y(3))<a
  89.         dx=-y(4)*sin(y(3));
  90.         dy=y(4)*cos(y(3));
  91.         dtheta=y(4)/R;
  92.         dv=-g*cos(y(3));
  93.         dvx=-a*cos(y(3))-dv*sin(y(3));
  94.         dvy=-a*sin(y(3))+dv*cos(y(3));
  95.     else
  96.         dx=y(5);
  97.         dy=y(6);
  98.         dtheta=0;%记录脱离时角度,同dv=0
  99.         dv=0;%不再计算v,v保持脱离时的速度,同dtheta=0
  100.         dvx=0;
  101.         dvy=-g;
  102.     end
  103. end
  104. dydt=[dx dy dtheta dv dvx dvy]';% 需为列向量
  105. end
  106. % 事件函数
  107. function [position,isterminal,direction] = luodi_evns(~,y,R)
  108. if y(1)>0 && y(2)<R && y(3)>0
  109.     position = y(2)+sqrt(R^2-y(1)^2)-R;% 落在圆轨道
  110. else
  111.     position = y(2);% 落回水平面
  112. end
  113. isterminal = 1;
  114. direction = -1;
  115. end
Add Comment
Please, Sign In to add comment