function [y]=AmericanPutClassic (St,t)
% S0 = 100;
K = 100;
r = 0.05;
sigma = 0.3;
T = 2;
nsteps = 5;
% St
dt = T / nsteps;
u=exp(sigma*sqrt(dt));
d=1/u;
Pu=(exp(r*dt)-d)/(u-d);
Pd=1-Pu;
if t==T
y=max(K-St,0);
return
elseif t<T
upPrice=AmericanPutClassic(St*u,t+dt);
downPrice=AmericanPutClassic(St*d,t+dt);
PrevPrice=(Pu*upPrice+Pd*downPrice)*exp(-r*dt);
if max(K-St,0) > PrevPrice
y=max(K-St,0);
else
y=PrevPrice;
end
return
end
end
I think my code does the job, but when I make 'nsteps' higher than 5, it crushes... I keep getting different errors... now it just says there is problem with my code when its higher than 5... before it would say: "??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer."
Can anybody spot the problem? I start by calling AmericanPutClassic(100,0)...