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 comentario

Stephen23
Stephen23 el 29 de Mzo. de 2018
@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.

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 29 de Mzo. de 2018
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')

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Mzo. de 2018

Respondida:

Rik
el 29 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by