I keep getting the following error when I run this code: "Out of memory. The likely cause is an infinite recursion within the program." It refers to this part of the code: "Error in AmericanPrice (line 4) P=AmericanPrice(0.08,0.12,0.2,100,50,10,300,3);" How can I fix this?
Update1: My bad, there was some typos in the code, it should be updated now! After entering the input arguments , i get this error "Undefined function 'find' for input arguments of type 'cell'. Error in AmericanPrice (line 32) bn=find(sign(diff(Pn)/dx+1)-1,1,{'last'})+bn;"
Update2: After changing "bn=find(sign(diff(Pn)/dx+1)-1,1,{'last'})+bn;" to "bn=find(sign(diff(Pn)/dx+1)-1,1,['last'])+bn;" i was able to get an output , im checking atm if it is the appropriate output
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,0.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

 채택된 답변

Image Analyst
Image Analyst 2017년 4월 3일

0 개 추천

Make a file called test.m and put this in it (assuming you have R2016b or later):
P=AmericanPrice(0.08,0.12,0.2,100,50,10,300,3);
function P = AmericanPrice(input1, input2, input3, input4, input5, input6, input7, input8);
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
end
If you have an antique version of MATLAB you can't put that all into one file, so either have the first line be
function test()
P=AmericanPrice(0.08,0.12,0.2,100,50,10,300,3);
end
OR put this into test.m
P=AmericanPrice(0.08,0.12,0.2,100,50,10,300,3);
and the rest into an m-file called AmericanPrice.m

댓글 수: 3

K
K 2017년 4월 3일
편집: K 2017년 4월 3일
i have r2017a, i did update the code , there was some typos in there
Image Analyst
Image Analyst 2017년 4월 3일
So it is working now?
K
K 2017년 4월 3일
yes i think so

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

John D'Errico
John D'Errico 2017년 4월 3일
편집: John D'Errico 2017년 4월 3일

1 개 추천

Is AmericanPrice the name of this function? If so, then it calls itself. Note the error message is:
"Error in AmericanPrice (line 4)
P=AmericanPrice(0.08,0.12,0.2,100,50,10,300,3);"
So, line 4 of the AmericanPrice function is a call to AmericanPrice. That will go on forever, therefore the infinite recursion.

댓글 수: 2

Star Strider
Star Strider 2017년 4월 3일
I considered that.
I opted to explore the hypothesis that ‘run’ isn’t changing first, since we don’t know if the function is calling itself.
K
K 2017년 4월 3일
i updated the code , there was some typos

댓글을 달려면 로그인하십시오.

Star Strider
Star Strider 2017년 4월 2일

0 개 추천

By your description, ‘run’ never changes. Since it appears that ‘bn’ is always going to be positive and increasing, one way to keep your while loop from becoming infinite (until you can determine what the problem is) would be to change the while condition to include a limit on ‘bn’:
while(run | (bn < 1E+3))
Remember not to use the ‘short circuit’ operator or the ‘bn’ test will never execute.
Note This is UNTESTED CODE. You will have to test it.

댓글 수: 5

K
K 2017년 4월 2일
so you dont know what the problem is ?
Star Strider
Star Strider 2017년 4월 2일
No.
I’m not sure what you’re doing, how your code is supposed to work, or what you’re calculating. All I did was to include a ‘fail-safe’ so you can figure out what the problem is. When the ‘fail-safe’ stops your code, you’ll have access to the values of all your variables, so you can see what they are.
You’re the only one who actually can troubleshoot your code and see what isn’t working correctly.
If you can’t figure it out otherwise, use the debugger to understand what your code is doing and what your variable values are between iterations.
K
K 2017년 4월 3일
편집: K 2017년 4월 3일
i tried your failsafe it gave me the same error.
Put a lower threshold on ‘bn’ and see what the result is:
while(run | (bn < 1E+1))
Either ‘run’ isn’t being toggled, or we’re only seeing part of your code, and as John mentioned, your function is calling itself.
Definitely a typo here:
bn=find(sign(diff(Pn)/dx+1)-1,1,{'last'})+bn;
↑ ← DONT USE A CELL STRUCTURE HERE
Try this instead:
bn = find(sign(diff(Pn)/dx+1)-1,1,'last')+bn;

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

K
K
2017년 4월 2일

댓글:

K
K
2017년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by