Advertisement
Y-BH

pendulum_period

Mar 27th, 2022
1,734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | None | 0 0
  1. function pendulum_period
  2. % pendulum waves-寻找合适的摆长
  3. clc;clear;close all
  4. g=10;         % 重力加速度
  5. Tend=200;     % 大于最长摆长的周期/4的一个时间
  6. T=80;        % 蛇摆周期
  7. theta=pi/6;   % 最大摆角
  8. L=0.5:0.01:3; % 摆长范围
  9. % ode设置
  10. opts=odeset('RelTol',1e-13,'AbsTol',1e-14, ...
  11.     'MaxStep',1e-3,'InitialStep',1e-4, ...
  12.     'Stats','off','Events',@(t,u)evns(t,u));
  13. for ii=1:length(L)
  14.     [t,~]=ode45(@(t,u)pw(t,u,g,L(ii)), ...
  15.         [0 Tend],[theta 0],opts);
  16.     w(ii)=T/4/t(end); % w:各摆在T内摆动次数
  17. end
  18. w_max=floor(max(w)); % 最大摆动次数,整数
  19. w_min=ceil(min(w));  % 最小摆动次数,整数
  20. % 计算整数摆动次数对应摆长
  21. L=interp1(w,L,w_min:w_max);
  22. % 保存变量用以模拟调取
  23. save matlab.mat L T theta g
  24. %% 微分方程与事件函数函数
  25. function du=pw(~,u,g,L)
  26. du=[u(2);-g/L*sin(u(1))];
  27. function [position,isterminal,direction] = evns(~,u)
  28. position = u(1);isterminal = 1;direction = -1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement