Plotting graphs over each other for N iterations

1 回表示 (過去 30 日間)
Katie Brewer
Katie Brewer 2019 年 2 月 21 日
コメント済み: Rena Berman 2019 年 4 月 2 日
I have the follow code to model a brownian motion (from http://ac.aua.am/tigran_bunarjyan/Public/NA_Monte%20Carlo%20Presentation.pdf) but I want to plot for all the steps, ie have step=500; lines of brownian motions plotted on the same graph. I have read and watched so many tutorials but everything I have tried crashed matlab.
clear all
clc
S=100;
sigma=0.3;
T=1.2;
r=0.05;
step=500;
dt=T/step;
sqdt=sqrt(dt);
rr=randn(1, step);
St=S;
stockPrices=[];
ts=[];
stockPrices(1)=S;
ts(1)=0;
for st=1:step
St=St*(1+r*dt+sigma*sqdt*rr(1,st));
stockPrices(st+1)=St;
ts(st+1)=st;
end
%plot(ts,stockPrices)
title("Stock Price for Day =0...500");
xlabel=("Day");
ylabel=("Price");
  3 件のコメント
Jan
Jan 2019 年 3 月 21 日
編集済み: Jan 2019 年 3 月 21 日
Katie Brewer has removed the contents of the question twice. She has removed the contents of her other 3 questions also. It seems, like she is not interested in a cooperative usage of the forum. What a pity.
Rena Berman
Rena Berman 2019 年 4 月 2 日
(Answers Dev) Restored edit

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

回答 (1 件)

Gani
Gani 2019 年 2 月 21 日
編集済み: Gani 2019 年 2 月 21 日
Check this.
clear all
clc
S=100;
sigma=0.3;
T=1.2;
r=0.05;
step=500;
dt=T/step;
sqdt=sqrt(dt);
rr=randn(1, step);
St=S;
stockPrices(1)=S;
ts= 1:step+1;
for st=1:step
St=St*(1+r*dt+sigma*sqdt*rr(1,st));
stockPrices(st+1)=St;
end
plot(ts,stockPrices)
title('Stock Price for Day =0...500');
xlabel('Day');
ylabel('Price');
  2 件のコメント
Katie Brewer
Katie Brewer 2019 年 2 月 21 日
This doesn't work, it just prints what I had before.
where each random path is printed over the top of the old one?
Gani
Gani 2019 年 2 月 22 日
In Mathworks File Exchange there are some Brownian Motion functions uploaded. You can use them. The graphs are in 3-D. You can modify it to 2D if necessary.

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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by