【问题标题】:Make a movie of trajectories in MATLAB在 MATLAB 中制作轨迹电影
【发布时间】:2015-12-26 12:08:40
【问题描述】:

我想在 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 循环,在每次迭代中,创建一帧的图,并使用 getframe 保存当前图。最后,您有一组图像,可以将其保存为视频。如果您可以编辑您的答案并包含创建这样一个轨迹图的代码,我可以发布一个答案来说明如何做到这一点。
  • 谢谢,我添加了代码。但我不知道如何同时绘制两条不同颜色的轨迹,并同时绘制 x(t),它像剪辑中一样移动。

标签: matlab movie differential-equations


【解决方案1】:

在您的示例代码中,您已经有一个 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 在 MATLAB 中观看此视频

implay(output_video)

或使用VideoWriter 类将其保存到磁盘:

v = VideoWriter('my_trajectory_video.avi')
open(v)
writeVideo(v,output_video)
close(v)

【讨论】:

  • 一般来说,更新lineseries 对象的XDataYDataZData 属性(在适当的情况下)通常至少比更新快一点反复拨打plot
  • 是的,你绝对是对的。我的解决方案效率不高,因为在每次迭代中都会完全重新创建该图形。
猜你喜欢
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
相关资源
最近更新 更多