Matlab code from literature gives errors

have this code from a book I recently looked at, I want to implement the code but I get loads of errors , can someone help me localize the errors or possibly make a runnable example?
function [P,b]=AmericanPrice(r,delta,sigma,K,nx,nt, Xhat,That)
%Usage:P=AmericanPrice(r,delta,sigma,K,nx,nt,Xhat,That)
%Example:P=AmericanPrice(0.08,0.12,.2,100,50,10,300,3)
dx=Xhat/nx;
dt=That/nt;
for i=1:nx-1
A(i,i:i+2)=[((r-delta)*dt*i-sigmaˆ2*dt*iˆ2)/2...
1+r*dt+sigmaˆ2*dt*iˆ2
(-(r-delta)*dt*i-sigmaˆ2*dt*iˆ2)/2];
end
P(:,1)=max(K-[0:dx:Xhat],0);
if(delta==0)
b(1)=K;
else
b(1)=min(K,K*r/delta);
end
for j=2:nt+1
bn=0; run=1;
while(run)
An=[A(1+bn:end,1+bn:end)];
An(end+1,end-1:end)=[-1 1];
An(end+1,1)=1;
Cn=[P(bn+2:nx,j-1)0 K-bn*dx];
Pn=inv(An)*Cn;
if(Pn(2)$<$K-((bn+1)*dx))
bn=find(sign(diff(Pn)/dx+1)-1,1,{last})+bn;
else
b(j)=bn*dx; run=0;
end
end
P(:,j)=[K-[0:bn-1]*dx Pn];
end
what i have done so far: i changed the sigmaˆ2 to sigma*sigma, same thing with i. Then i replaced the ’ with '. I did not know what to d o with "..." The error i get is
Undefined function 'find' for input arguments of type 'cell'.
Error in FDM (line 28)
bn=find(sign(diff(Pn)/dx+1)-1,1,{'last'})+bn;

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Mai 2017

0 Stimmen

Change
bn=find(sign(diff(Pn)/dx+1)-1,1,{last})+bn;
to
bn=find(sign(diff(Pn)/dx+1)-1,1,last)+bn;

10 Kommentare

K
K am 28 Mai 2017
Thank you that did work. If i now want to localize the values created by vector b , how would i do that? All i get now is "ans 51x11 double" ?
I do not know what you mean by "localize" ?
By the way,
if(Pn(2)$<$K-((bn+1)*dx))
is not valid syntax. $ is not used in MATLAB except in comments and strings.
K
K am 28 Mai 2017
From the book it says " The function returns a matrix P that contains the price of the option at each grid point and a vector b that contains the exercise boundary values for each discrete time point" I want to find vector b values not the matrix P
Currently you are calling something like
AmericanPrice(with some list of parameters)
change that to
[P, b] = AmericanPrice(with some list of parameters);
and then look at b.
K
K am 28 Mai 2017
I do run it , first row
function [P,b]=AmericanPrice(r,delta,sigma,K,nx,nt, Xhat,That)
Walter Roberson
Walter Roberson am 29 Mai 2017
That is a function declaration, not a function execution.
K
K am 29 Mai 2017
Bearbeitet: K am 29 Mai 2017
I don't understand can you be more specific?
How should I write the function execution? where should I write it?
At the command line, you should write
[P, b] = AmericanPrice(0.08,0.12,.2,100,50,10,300,3);
and then look at your variable b
so I should run something like this ?
[P, b] = AmericanPrice(0.08,0.12,.2,100,50,10,300,3);
%Usage:P=AmericanPrice(r,delta,sigma,K,nx,nt,Xhat,That)
%P=AmericanPrice(0.08,0.12,.2,100,50,10,300,3);
dx=Xhat/nx;
dt=That/nt;
for i=1:nx-1
A(i,i:i+2)=[((r-delta)*dt*i-sigma*sigma*dt*i*i)/2
1+r*dt+sigma*sigma*dt*i*i
(-(r-delta)*dt*i-sigma*sigma*dt*i*i)/2];
end
P(:,1)=max(K-[0:dx:Xhat],0);
if(delta==0)
b(1)=K;
else
b(1)=min(K,K*r/delta);
end
for j=2:nt+1
bn=0; run=1;
while(run)
An=[A(1+bn:end,1+bn:end)];
An(end+1,end-1:end)=[-1 1];
An(end+1,1)=1;
Cn=[P(bn+2:nx,j-1)' 0 K-bn*dx]';
Pn=inv(An)*Cn;
if(Pn(2)<K-((bn+1)*dx))
bn=find(sign(diff(Pn)/dx+1)-1,1,'last')+bn;
else
b(j)=bn*dx; run=0;
end
end
P(:,j)=[K-[0:bn-1]*dx Pn'];
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by