I am trying to evaluate a function with fzero and getting the following error message - I am not sure why its throwing an error:
Error using fzero (line 246)
FZERO cannot continue because user-supplied function_handle ==> xsld failed with the error below.
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix
powers, use '.^'.
Error in moments (line 80)
[w,fv]= fzero(@xsld,w0,options);
The codes are below:
tol=10^-5; %tolerance
w0=[0.1,200]; %wage interval
options=optimset('TolX',tol,'Display','iter');
options=optimset(options,'PlotFcns',{@optimplotx,@optimplotfval});
try
[w,fv]= fzero(@xsld,w0,options);
catch
cr = 50;
return
end
And the function @xsld has the following codes, saved as a function file (I am only showing the codes that have the argument w):
function[xld] = xsld(w)
some codes...
RW = repmat(w.*(s.^gamma),1,ns); %wage income
A = (1-((1-nu)*(1-alpha))).*(((1-nu)*(1-alpha))/w)^(((1-nu)*(1-alpha))/(1-((1-nu)*(1-alpha))));
lu = (((theta_r.^nu).*(1-nu).*(1-alpha).*(k.^(alpha*(1-nu))))/w).^(1/(nu + alpha*(1-nu)));
Ld = sum(mean(lu,2)) + ((B*psi)/w)^(1/(1-psi));
xld = (sum(mean((((theta_r.^nu).*((1-nu)*(1-alpha)).*(k.^(alpha*(1-nu))))/w).^(1/(nu + alpha*(1-nu))),2)) + ((B*psi)/w)^(1/(1-psi))) - Ls;

 채택된 답변

Matt J
Matt J 2021년 4월 14일

0 개 추천

You can trap the occurrence of the error using
dbstop if caught error

댓글 수: 4

Girija Bahety
Girija Bahety 2021년 4월 15일
Hi Matt,
Using dbstop helped to fix the issue but now I am getting another error.
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 444)
while fb ~= 0 && a ~= b
Error in moments (line 79)
[w,fv] = fzero(@xsld,w0,fzerooptions);
What should I do here?
Girija Bahety
Girija Bahety 2021년 4월 15일
@Matt J I tried evaluating the xsld function using a scalar value for "w" and it gives me a scalar value for the function @xsld. So convinced the function is not vector valued.
Matt J
Matt J 2021년 4월 15일
편집: Matt J 2021년 4월 19일
So convinced the function is not vector valued.
To make sure I would add a line to the end of xsld()
assert( isscalar(xld) , 'Unexpected vector-valued xld')
Now run the code with dbstop and see if the assert() is triggered.
Girija Bahety
Girija Bahety 2021년 4월 19일
Thank you Matt!

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

추가 답변 (0개)

카테고리

태그

질문:

2021년 4월 14일

편집:

2021년 4월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by