What changes should I apply to this code?

조회 수: 1 (최근 30일)
veru
veru 2013년 5월 10일
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.

채택된 답변

Stephan M. H.
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 CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by