# FZERO error with matrix dimensions

32 views (last 30 days)
Girija Bahety on 14 Apr 2021 at 23:03
Edited: Matt J on 19 Apr 2021 at 22:27
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 on 14 Apr 2021 at 23:26
You can trap the occurrence of the error using
dbstop if caught error
Girija Bahety on 19 Apr 2021 at 21:26
Thank you Matt!

VBBV on 14 Apr 2021 at 23:39
Edited: VBBV on 14 Apr 2021 at 23:40
%if true
A = (1-((1-nu)*(1-alpha))).*(((1-nu)*(1-alpha))/w).^(((1-nu)*(1-alpha))/(1-((1-nu)*(1-alpha))));
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;
These two lines require element wise power operators
Girija Bahety on 14 Apr 2021 at 23:44
Hi, I tried these but then the FZERO throws an error saying that the matrix dimensions must agree.