在 MATLAB 中制作轨迹电影

Make a movie of trajectories in MATLAB

我想在 MATLAB 中创建一个如下所示的电影: https://www.youtube.com/watch?v=8z_tSVeEFTA

在数学上,我有微分方程和数值求解它们的代码:

% lorenz - Program to compute the trajectories of the Lorenz
% equations using the adaptive Runge-Kutta method.
clear;  help lorenz;

%* Set initial state x,y,z and parameters r,sigma,b
state = input('Enter the initial position [x y z]: ');
r = input('Enter the parameter r: '); 
sigma = 10.;   % Parameter sigma
b = 8./3.;     % Parameter b
param = [r sigma b];  % Vector of parameters passed to rka
tau = 1;       % Initial guess for the timestep
err = 1.e-3;   % Error tolerance

%* Loop over the desired number of steps
time = 0;
nstep = input('Enter number of steps: ');
for istep=1:nstep

%* Record values for plotting
x = state(1); y = state(2); z = state(3);
tplot(istep) = time;  tauplot(istep) = tau;       
xplot(istep) = x;  yplot(istep) = y;  zplot(istep) = z;
if( rem(istep,50) < 1 )
fprintf('Finished %g steps out of %g\n',istep,nstep);
end

%* Find new state using adaptive Runge-Kutta
[state, time, tau] = rka(state,time,tau,err,'lorzrk',param);

end

%* Print max and min time step returned by rka
fprintf('Adaptive time step: Max = %g,  Min = %g \n', ...
       max(tauplot(2:nstep)), min(tauplot(2:nstep)));

      %* Graph the time series x(t)
      figure(1); clf;        % Clear figure 1 window and bring forward
      plot(tplot,xplot,'-')
     xlabel('Time');  ylabel('x(t)')
 title('Lorenz model time series')
 pause(1)  % Pause 1 second

%* Graph the x,y,z phase space trajectory
figure(2); clf;  % Clear figure 2 window and bring forward
% Mark the location of the three steady states
x_ss(1) = 0;              y_ss(1) = 0;       z_ss(1) = 0;
x_ss(2) = sqrt(b*(r-1));  y_ss(2) = x_ss(2); z_ss(2) = r-1;
x_ss(3) = -sqrt(b*(r-1)); y_ss(3) = x_ss(3); z_ss(3) = r-1;
plot3(xplot,yplot,zplot,'-',x_ss,y_ss,z_ss,'*')
view([30 20]);  % Rotate to get a better view 
grid;           % Add a grid to aid perspective
xlabel('x'); ylabel('y'); zlabel('z');
title('Lorenz model phase space');

但是我不知道如何用不同的颜色一次绘制两个不同的轨迹,并制作一个看起来像这样的剪辑(同时显示 X 坐标解的侧图)。

谁能帮我解决这个问题?

谢谢!

在您的示例代码中,您已经有一个 for 循环,它遍历所有时间步长。现在,要生成这样的动画,我们需要在每个时间步绘制图形。这是由

完成的
for istep=1:nstep

    %* Record values for plotting
    x = state(1); y = state(2); z = state(3);
    tplot(istep) = time;  tauplot(istep) = tau;       
    xplot(istep) = x;  yplot(istep) = y;  zplot(istep) = z;

    %* Find new state using adaptive Runge-Kutta
    [state, time, tau] = rka(state,time,tau,err,'lorzrk',param);

    %* Create Plot
    figure(2);
    x_ss(1) = 0;              y_ss(1) = 0;       z_ss(1) = 0;
    x_ss(2) = sqrt(b*(r-1));  y_ss(2) = x_ss(2); z_ss(2) = r-1;
    x_ss(3) = -sqrt(b*(r-1)); y_ss(3) = x_ss(3); z_ss(3) = r-1;
    plot3(xplot,yplot,zplot,'-',x_ss,y_ss,z_ss,'*')
    view([30 20]);  % Rotate to get a better view 
    grid;           % Add a grid to aid perspective
    xlabel('x'); ylabel('y'); zlabel('z');
    title('Lorenz model phase space');

    %* Save frame
    output_video(istep) = getframe;

end

这将创建一个结构数组 output_video,其大小为 1 x nstep,每个帧包含 cdata(图像数据)和 colormap

您可以使用

在 MATLAB 中观看此视频
implay(output_video)

或使用 VideoWriter class:

将其保存到磁盘
v = VideoWriter('my_trajectory_video.avi')
open(v)
writeVideo(v,output_video)
close(v)