Find the Error - FZero Function. Constant loop.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
[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 Kommentare
Sean de Wolski
am 4 Mai 2011
First: Don't call your variable diff since it's MATLAB's builtin difference function.
Andrew Newell
am 14 Mai 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.
Akzeptierte Antwort
Andrew Newell
am 4 Mai 2011
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.
0 Kommentare
Weitere Antworten (1)
Matt Tearle
am 4 Mai 2011
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
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!