Advertisement
Y-BH

Lorenz_attractor

Apr 10th, 2022
2,447
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. % 洛伦兹的蝴蝶
  2. %% 洛伦兹方程参数与计算
  3. r=28;s=10;b=8/3;
  4. % 初始数据点数,演示时间
  5. N=16;ti=0:0.005:108;
  6. % ode设置
  7. opts=odeset('RelTol',1e8,'AbsTol',1e10,'NormControl','on',...
  8.     'MaxStep',1e-2,'InitialStep',1e-3);
  9. for ii=1:N
  10. [~,u] = ode45(@(t,u)Lorenz(t,u,r,s,b),ti,[0 ii/N 0],opts);
  11. x(:,ii)=u(:,1);y(:,ii)=u(:,2);z(:,ii)=u(:,3);
  12. end
  13. %% 绘图
  14. f=figure;f.MenuBar='none';
  15. dpi=1.25;scr=[1920 1080];res=[1280 960];
  16. fig=gcf;fig.Color='k';
  17. fig.Position = [(scr-res)/2,res]/dpi;% 居中显示
  18. cj=jet;% 颜色集
  19. for ii=1:N
  20.     a(ii)=animatedline('MaximumNumPoints',1250, ...
  21.         'Color',cj(ii*16,:),'LineWidth',1.5);
  22. end
  23. axis off;axis equal
  24. ax=gca;ax.Clipping='off';
  25. ax.CameraViewAngle = 22.5;%视野范围
  26. ax.CameraTarget = [0 0 25];%目标位置
  27. %% 动画
  28. mv = 1;% 是否录制视频,1录制,其它不录制
  29. if mv == 1
  30. v = VideoWriter('D:\Programs\MATLAB\美丽的蝴蝶.mp4','MPEG-4');
  31. v.Quality = 100;v.FrameRate = 60;
  32. open(v);
  33. end
  34. for n=1:length(ti)
  35.     for ii=1:N
  36.         addpoints(a(ii),x(n,ii),y(n,ii),z(n,ii));
  37.     end
  38.     ax.CameraPosition = [100*cos(ti(n)/2) 100*sin(ti(n)/2) 25];%相机位置
  39.     drawnow% limitrate
  40.     if mv==1;F=getframe(fig);writeVideo(v,F);end
  41. end
  42. if mv == 1;close(v);end
  43. %% 洛伦兹方程
  44. function du=Lorenz(~,u,r,s,b)
  45. x=u(1);y=u(2);z=u(3);
  46. dx=s*(y-x);
  47. dy=r*x-y-x*z;
  48. dz=-b*z+x*y;
  49. du=[dx dy dz]';
  50. end
Advertisement
RAW Paste Data Copied
Advertisement