Why doesn't MATLAB plot?
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
2013-10-8
2 个评论
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!