What changes should I apply to this code?
1 次查看(过去 30 天)
显示 更早的评论
Let S(t) be the price of one share of a particular company at time t. The price S(t+1) at time t+1 can either take the value of uS(t) with probability p1 (where u>1), remain the same with probability p2 or go down to dS(t) with probability 1-p1-p2 (where 0<d<1). I have to create a function which stimulates S(t) for t from 0 up to 20.
The following is the code I have written, but in this code each S(t) depends on the Value S0 which I input at the beginning. How can I change this in such a way that the value S(t+1) depends on the value S(t)? i.e for example S(5) depending on S(4)
function S=question5(u,d,p1,p2)
S0=input('Enter price of share: ') % price of share at time 0
u=input('Enter a number greater than 1: ')
d=input('Enter a number between 0 and 1: ' )
p1=input('Enter probability p1: ') % p1 is the probability of price of share increasing
p2=input('Enter probability p2: ')% p2 is the probability of price of share decreasing
for t=0:1:20
g=rand(1) % random number whose exact value is not important
if g>0 && g<p1
S=u*S0
elseif g>p1 && g<p1+p2
S=S0
elseif g>p1+p2
S=d*S0
end
end
Thanks in advance.
0 个评论
采纳的回答
Stephan M. H.
2013-5-10
Hi,
if I got it right, the introduction of a auxiliary variable should be all you need:
s_t = S0
for ...
if ...
S = u*s_t
elseif ...
S = s_t
elseif ...
S = d*s_t
end
s_t = S
end
best, Stephan
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!