MATLAB: How to draw the graph of the same function several times

for loopfunctionhold onplottingrandom walk

clear clc
x_t(1) = 0;
N = 10^2;
for t = 1:N
xlabel('t'), ylabel('x_t'), title('Random Walk')
a = sign(randn);
x_t(t+1) = x_t(t) + a;
plot(x_t,'b-')
hold on
pause(0.05);
end
How do I plot this same function several times? This is a random walk, every time it gets plotted I want it to look differently, which is exactly what sign(randn) does.

Best Answer

M = 10 ;
N = 10^2;
x_t = zeros(M,N) ;
x_t(:,1) = rand ;
for i = 1:M
for t = 2:N
a = sign(randn);
x_t(i,t) = x_t(i,t-1) + a;
end
end
plot(x_t)
xlabel('t'), ylabel('x_t'), title('Random Walk')
It can be easily vectorised.