Matrix Loop and Labelling

11 ビュー (過去 30 日間)
Joe Bannen
Joe Bannen 2018 年 5 月 30 日
コメント済み: Joe Bannen 2018 年 5 月 31 日
Hi
I am trying to generate paths for stock prices via Brownian Motion. The code works okay (I hope!), but I can't get it to output correctly:
mu=0.10;
sigma=0.05;
N=5; % Number of time steps
NSim=3; % Number of simulations
dt=sigma/sqrt(N);
S=zeros(NSim,N+1);
S0=100;
S(:,1)=100; % initial value
for k=1:NSim
for i=2:N+1
epsilon=randn;
S(k,i)=S(k,i-1)+S(k,i-1)*(mu*dt+sigma*epsilon*sqrt(dt));
end
end
plot(S')
xlim([0 N+1]);
xlabel('Trading Days');
ylabel('Simulated Asset Price (£)');
However, the plot starts at 1 and finishes at 6. How can I make it start at 0 and stop at 5?
I think something has gone wrong with my loops!
Cheers
Joe

採用された回答

dpb
dpb 2018 年 5 月 30 日
You didn't supply an x to PLOT() so it plots versus ordinal number...
...
x=[0:N].'; % define range of x axis
plot(x,S')
...
  1 件のコメント
Joe Bannen
Joe Bannen 2018 年 5 月 31 日
Brilliant!
Many thanks
Joe

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by