动画 3D 随机游走 Matlab

Animating 3D Random Walk Matlab

我正在编写代码以在 Matlab 上模拟 3D space 中的随机游走。但是,我的模拟次数 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

你画错地方了:

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