jeudi 19 août 2021

Animating 3D Random Walk Matlab

I am writing a code to simulate random walk in 3D space on Matlab. However, there seems to be a problem with my number of simulations, M. I want to animate multiple simulations on the same graph but I am only get 1 simulation. My input of M instead becomes the number of steps. How can I fix this code? Thank you.

I want it to look like the animation in this video: https://www.youtube.com/watch?v=7A83lXbs6Ik But after each simulation is complete, another one starts on the same graph but a different color.

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



Aucun commentaire:

Enregistrer un commentaire