How do I draw the graph of the same function several times?

4 ビュー (過去 30 日間)
Delshad Ayoubi
Delshad Ayoubi 2017 年 10 月 6 日
コメント済み: Delshad Ayoubi 2017 年 10 月 6 日
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.
  2 件のコメント
KSSV
KSSV 2017 年 10 月 6 日
randn generates normally distributed random numbers.
sign gives it sign..if positive +1 and if negative -1.
Delshad Ayoubi
Delshad Ayoubi 2017 年 10 月 6 日
編集済み: Delshad Ayoubi 2017 年 10 月 6 日
Yeah, and that is why every time it's plotted it looks different, random chance for +1/-1. I just want to know how to plot it several times while retaining them.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2017 年 10 月 6 日
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.
  1 件のコメント
Delshad Ayoubi
Delshad Ayoubi 2017 年 10 月 6 日
I don't really understand that code very well. I'm new to programming. What is:
x_t(:,1) = rand;?
x_t = zeros(M,N);?
Seems very different compared with my code, isn't it possible to do a double loop or something with my and get the same results?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by