Why doesn't MATLAB plot?

조회 수: 8 (최근 30일)
Tiancong Sui
Tiancong Sui 2013년 10월 8일
댓글: Tiancong Sui 2013년 10월 8일
stock = 100;
delta_t = 1 / 365;
volatility = 0.1;
for i = 1 : 5
stock = stock .* exp( volatility .* sqrt ( delta_t ) .* randn(1) );
disp( stock )
end
t = 1 : 5 plot(stock,t)
------------------------
Anyone could help please? Thank you
Tony

채택된 답변

Wayne King
Wayne King 2013년 10월 8일
편집: Wayne King 2013년 10월 8일
You are not assigning the output stock to a vector, so you are just getting the latest result from the for loop.
stock = zeros(5,1);
delta_t = 1 / 365;
volatility = 0.1;
initstock = 100;
for ii = 1 : 5
stock(ii) = initstock .* exp( volatility .* sqrt ( delta_t ) .* randn(1) );
initstock = stock(ii);
end
t = 1:length(stock);
plot(t,stock)
  댓글 수: 1
Tiancong Sui
Tiancong Sui 2013년 10월 8일
Thank you !!!!! I started to use Matlab 2 days ago and this is the first task for me, thank you for your great help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Tiancong Sui
Tiancong Sui 2013년 10월 8일
stock = 100;
delta_t = 1 / 365;
volatility = 0.1;
for i = 1 : 5
stock = stock .* exp( volatility .* sqrt ( delta_t ) .* randn(1) );
disp( stock )
end
t = 1 : 5
plot(stock,t)
--------------------
A better version, the one above is crumbled up.
  댓글 수: 2
Matthew Crema
Matthew Crema 2013년 10월 8일
I'm guessing you want to save the value of stock in each iteration of the for loop. Try:
for i = 2 : 5
stock(i) = stock(i-1) .* exp( volatility .* sqrt ( delta_t ) .* randn(1) );
disp( stock )
end
Tiancong Sui
Tiancong Sui 2013년 10월 8일
To Matthew, thank you! yes you are right, I was trying to do the iteration. Thanks you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by