【问题标题】:Animating 3D Random Walk Matlab动画 3D 随机游走 Matlab
【发布时间】:2021-10-21 04:21:51
【问题描述】:

我正在编写代码以在 Matlab 上模拟 3D 空间中的随机游走。但是,我的模拟数量 M 似乎存在问题。我想在同一个图表上为多个模拟设置动画,但我只得到 1 个模拟。我输入的 M 变成了步数。如何修复此代码?谢谢。

我希望它看起来像这个视频中的动画:https://www.youtube.com/watch?v=7A83lXbs6Ik 但在每次模拟完成后,另一个模拟从同一张图开始,但颜色不同。 最终结果应该是这样的 final

clc;
clearvars;
N = input('Enter the number of steps in a single run: '); % Length of the x-axis and random walk.
M = input('Enter the number of simulation runs to do: '); % The number of random walks.
x_t(1) = 0;
y_t(1) = 0;
z_t(1) = 0;

for m=1:M
    for n = 1:N % Looping all values of N into x_t(n).
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        x_t(n+1) = x_t(n) + A;
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        y_t(n+1) = y_t(n) + A;
        A = sign(randn);
        z_t(n+1) = z_t(n) + A;
    end
    plot3([x_t(1) x_t(n+1)], [y_t(1) y_t(n+1)], [z_t(1) z_t(n+1)], 'g');
    hold on
    grid on
    x_t = x_t(n+1);
    y_t = y_t(n+1);
    z_t = z_t(n+1);
    drawnow;
end

【问题讨论】:

  • 有什么问题?我猜您需要重新启动初始点,并更改颜色,否则一切都很好。
  • 嘿安德:D 我认为我的措辞不太好。我在帖子中添加了我想要的图片。基本上,我希望代码为每个模拟设置动画,最后,它应该看起来像图片。现在发生的事情是它将在 1 次模拟后完成。

标签: matlab animation random random-walk


【解决方案1】:

你在错误的地方绘图:

clc;
clearvars;
N = 100
M = 5
x_t(1) = 0;
y_t(1) = 0;
z_t(1) = 0;

c=lines(M) % save colors so each m has its own
for m=1:M
    
    for n = 1:N % Looping all values of N into x_t(n).
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        x_t(n+1) = x_t(n) + A;
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        y_t(n+1) = y_t(n) + A;
        A = sign(randn);
        z_t(n+1) = z_t(n) + A;
        
        plot3([x_t(n) x_t(n+1)], [y_t(n) y_t(n+1)], [z_t(n) z_t(n+1)],'color',c(m,:));
        hold on
        grid on
        drawnow;

    end  
end

【讨论】:

  • 哇,这样一个简单的修复,但它完成了工作。非常感谢安德:D
猜你喜欢
  • 1970-01-01
  • 2015-06-16
  • 2014-02-19
  • 2018-05-06
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多