Find the Error - FZero Function. Constant loop.
Show older comments
[EDIT: Thu May 12 22:17:54 UTC 2011 Duplicate Removed, Reformat - MKF]
Okay so I can't seem to find this last error in my program. I just get a constant loop.
My first function file for Consumers reads:
function F = Consumer2(x)
%f1=N
%f2=n
%f3=k
global a R K W B w r
f1= (a*W)/(R*K + W*x(1) - x(3))+(a-1)/(1-x(1));
f2= B*((a*w)/(r*x(3) + w*x(2))+((a-1)/(1-x(2))));
f3= (-a/(R*K + W*x(1) - x(3))) + B*((a*r)/(r*x(3) + w*x(2)));
F= f1^2 + f2^2 + f3^2;
end
The second function file for firm profit reads:
function F = Firm2(x)
%F1=N
%F2=n
%F3=k
global p r w K W
F1=.6*p*(K^.3)*((x(1))^-.4)-W;
F2=.6*p*((x(3))^.3)*((x(2))^-.4)-w;
F3=.3*p*((x(3))^-.7)*((x(2))^.6)-r;
F = F1^2 + F2^2 + F3^2;
end
Finally my executable is:
global a R K W p B r w
a=2/3;
R=.1409;
K=2;
W=.8;
p=1;
B=.95;
r=.5;
w=.7;
diff=1;
diff1=0;
diff2=0;
diff3=0;
while diff>.001
NCons=fminsearch('Consumer2',[.2,.1,.5])
NFirm=fminsearch('Firm2',[.2,.1,.5])
diff1=NCons(1)-NFirm(1);
W=W-.05*diff1;
diff2=NCons(2)-NFirm(2);
w=w-.05*diff2;
diff3=NCons(3)-NFirm(3);
r=r-.05*diff3;
diff=diff1^2+diff2^2+diff3^2;
end
When I run the executable i get an infinite loop. Any help?
2 Comments
Sean de Wolski
on 4 May 2011
First: Don't call your variable diff since it's MATLAB's builtin difference function.
Andrew Newell
on 14 May 2011
I believe that this code is an attempt to answer this question: http://www.mathworks.com/matlabcentral/answers/6442-optimization-problem-completely-lost-and-need-any-type-of-help. It would be nice if the two were merged.
Accepted Answer
More Answers (1)
Matt Tearle
on 4 May 2011
0 votes
You're calling fminsearch with the same initial guess every time. So you get the same NCons and NFirm every time. So you get the same diff every time.
While I'm here...
- As Sean says, don't use diff
- Use anonymous function handles to embed parameters, rather than global
- Use function handles, rather than strings for function names
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!