Find the Error - FZero Function. Constant loop.

3 次查看(过去 30 天)
[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 个评论
Sean de Wolski
Sean de Wolski 2011-5-4
First: Don't call your variable diff since it's MATLAB's builtin difference function.
Andrew Newell
Andrew Newell 2011-5-14
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.

请先登录,再进行评论。

采纳的回答

Andrew Newell
Andrew Newell 2011-5-4
The root of the problem is that you're trying to solve for NCons = NFirm by minimizing each of these functions at each step. The minimization is failing each time, leaving huge residuals. Here is a better way to solve it. Define your functions as
function F = Consumer2(x,a,R,K,B) %f1=N %f2=n %f3=k
f1= (a*x(4))/(R*K + x(4)*x(1) - x(3))+(a-1)/(1-x(1));
f2= B*((a*x(5))/(x(6)*x(3) + x(5)*x(2))+((a-1)/(1-x(2))));
f3= (-a/(R*K + x(4)*x(1) - x(3))) + B*((a*x(6))/(x(6)*x(3) + x(5)*x(2)));
F= f1^2 + f2^2 + f3^2;
and
function F = Firm2(x,K,p)
F1=.6*p*(K^.3)*((x(1))^-.4)-x(4);
F2=.6*p*((x(3))^.3)*((x(2))^-.4)-x(5);
F3=.3*p*((x(3))^-.7)*((x(2))^.6)-x(6);
F = F1^2 + F2^2 + F3^2;
(I have replaced W, w, r by x(4), x(5), x(6)). Then run:
a=2/3; R=.1409; K=2; W=.8; p=1; B=.95; r=.5; w=.7;
residual=1; residual1=0; residual2=0; residual3=0;
f = @(x) Consumer2(x,a,R,K,B)-Firm2(x,K,p);
xsol=fsolve(f,[.2,.1,.5, W, w, r]);
The line f = @(x) ... is what Matt means by points 2 and 3.

更多回答(1 个)

Matt Tearle
Matt Tearle 2011-5-4
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...
  1. As Sean says, don't use diff
  2. Use anonymous function handles to embed parameters, rather than global
  3. Use function handles, rather than strings for function names
  1 个评论
Jeremy
Jeremy 2011-5-4
Ty for the answer. I'm relatively new to the game here. This is my first time ever using matlab. It's for a class of mine. The professor used diff so I think I just sort of went with it. Not quite sure what the 2 and 3 mean... my apologies.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by