Storing repeated values from a loop

I am unsure how to store the values, then take the average, from my output St into a large matrix so that I can work with the numbers more easily. I have looked through other responses, and cant seem to find a solution. The code is as follows. Any help is greatly appreciated.

1 件のコメント

Stephen23
Stephen23 2018 年 3 月 29 日
@Quinn Hayward: we cannot run a screenshot, we cannot edit a screenshot, we cannot search a screenshot, we cannot use a screenshot. Screenshots as basically useless as a way of giving us your code. If you want help then please delete the screenshot and give your code as text.

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

 採用された回答

Rik
Rik 2018 年 3 月 29 日

0 投票

Please, never post your code as an image. It actively discourages people to help, because we can't copy and paste code to fix it. The code below should be something like what you mean.
Pay attention to the m-lint warnings. They alert you to potential bugs and possible places to improve your code. Use functions to keep your workspace clean (or at least consider using clearvars instead), and don't use clc if you're not planning on using disp or fprintf.
mu=0.004;
sigma=0.05;
S1=72;
N=261;
K=5;
dt=sigma/sqrt(N);
M=zeros(N,K);
for k=1:K
St=zeros(N,1);St(1)=S1;
for i=2:N
epsilon=randn;%don't overwrite the internal eps function
St(i)=St(i-1)+St(i-1)*(mu*dt+sigma*epsilon*sqrt(dt));
end
M(:,k)=St;
end
plot(M)
xlim([1 N])
xlabel('days')
ylabel('value')

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 3 月 29 日

回答済み:

Rik
2018 年 3 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by