fsolve - solving for a steady state
조회 수: 11 (최근 30일)
이전 댓글 표시
I have a system of 4 unknowns and 4 equations of which I can cancel down to a system of 2 equations and 2 unknowns which leads to a trivial solution to the other 2 unknowns.
The two equations are:
(0.99)*(0.3*(k^0.3)*(n^0.7) + 0.975) = 1
and
((0.21)*(k^0.3)*(n^-0.3))/((k^0.3)*(n^0.7) - (0.025*k)) = 0.7/(1-n)
I am obviously looking to solve for k and h and I know that h is bounded by 0 and 1 (labour-leisure choice per period). I attempt to solve this via fsolve and this is my code:
function [ F ] = myfun(k,n)
% the system of 2 equations to solve for in vector F
F = [((((0.21)*(k.^0.3)*(n.^-0.3))/((k.^0.3)*(n.^0.7)- 0.025*k)) - ((0.7)/(1-n)));
(((0.99)*(0.3*(k.^-0.7)*(n.^0.7) + 0.975)) - 1)];
end
%%why was I told to use .^ for powers since I thought k and n were both scalar and I am looking to be returned a scalar?
and the script:
F0 = [5; 0.5]';
options = optimoptions('fsolve','Display','iter'); % Option to display output
X = fsolve(@myfun,F0,options); % Call solver
When I run this through Matlab I get this error:
Error using myfun (line 5)
Not enough input arguments.
Error in fsolve (line 219)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
And quite honestly, I'm not sure where it's going wrong and why it is saying there are not enough input arguments, any guidance is appreciated.
댓글 수: 0
채택된 답변
Star Strider
2014년 12월 3일
The fsolve (and fminsearch and others) solve for a vector of parameters, so you have to express the arguments to them as such.
This works:
function [ F ] = myfun(p)
k = p(1);
n = p(2);
% the system of 2 equations to solve for in vector F
F = [((((0.21)*(k.^0.3)*(n.^-0.3))/((k.^0.3)*(n.^0.7)- 0.025*k)) - ((0.7)/(1-n)));
(((0.99)*(0.3*(k.^-0.7)*(n.^0.7) + 0.975)) - 1)];
end
F0 = [5; 0.5]';
options = optimoptions('fsolve','Display','iter'); % Option to display output
X = fsolve(@myfun,F0,options) % Call solver
and successfully completes with this result:
X =
5.9198e+000 276.1589e-003
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!