fzero Operands to the || and && operators must be convertible to logical scalar values.

조회 수: 4 (최근 30일)
I saw the other two posts on this community about this issue, and they are both dimensional issues. However, I believe I do not have a dimensional issue here in my problem but still get the same error message. I tried 'fsolve' and symbolic equation 'solve', none of them worked (for the former, I got the initial value as the solution, while for the latter, I got another error). I paste my whole codes here just for reference, but the issue comes from the last 3 lines. I would very much appreciate your help!
%% Exogenous parameters
gamma = 0.181;
zeta = 10.63;
nu = 4/3;
chi = 0.233;
pi_r = 0.55;
pi_n = 1 - pi_r;
A = 1;
alpha = 0.53;
a=alpha;
phi_0 = 0.4226;
tau = 0;
g = 0.0083;
phi_1 = phi_0;
%% Objective function
obj = @(x) -1*(pi_r*(log(x(1)) - zeta*x(2)^(1 + nu) / (1 + nu) + chi*log(x(5))) + ...
pi_n*(log(x(3)) - zeta*x(4)^(1 + nu) / (1 + nu) + chi*log(x(5))));
nonlcon = @nonlinear_cons;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0.13, 0.38, 0.185, 0.41, 0.1];
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(obj,x0,A,b,Aeq,beq,lb,ub,nonlcon);
lr = x(2);
ln = x(4);
mu = lambda.ineqnonlin;
func = @(p) p./ (1+p) - a./(1-a) .* ...
pi_r.*phi_1.*lr ./ (pi_n.*(a.*A.^(1 ./ a).*(1 - a)^((1-a) ./ a) ./ ...
(((1+p).*phi_1).^((1-a)./a))).*ln) .* (1/mu .* (zeta .* lr.^nu ./ phi_1) - 1);
p0 = 0.1;
solx = fzero(func,p0)
<stopping criteria details>
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 327)
elseif ~isfinite(fx) || ~isreal(fx)
Error in simpleTax (line 55)
solx = fzero(func,p0)
  댓글 수: 2
dpb
dpb 2019년 8월 31일
>> func(p0)
ans =
[]
>> func
func =
function_handle with value:
@(p)p./(1+p)-a./(1-a).*pi_r.*phi_1.*lr./(pi_n.*(a.*A.^(1./a).*(1-a)^((1-a)./a)./(((1+p).*phi_1).^((1-a)./a))).*ln).*(1/mu.*(zeta.*lr.^nu./phi_1)-1)
>> func(0.1)
ans =
[]
>> ~isfinite(ans) || ~isreal(fx)
Operands to the || and && operators must be convertible to logical scalar values.
>> mu
mu =
0×1 empty double column vector
>>
Your functional is returning empty vector because if I futz around enough to get the fmincon call run it returns
>> lambda
lambda =
struct with fields:
eqlin: [0×1 double]
eqnonlin: [0×1 double]
ineqlin: [0×1 double]
lower: [5×1 double]
upper: [5×1 double]
ineqnonlin: [0×1 double]
>>
so your value for mu in the functional is empty which causes an empty vector for the result. That then causes the internal error inside fzero
Hideto Koizumi
Hideto Koizumi 2019년 9월 1일
Thank you guys for your prompt reaction! It looks like I made a silly mistake in the definition of 'A' as discussed below! Sorry that I forgot to attach the 'nonlinear_cons.m' file!

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 9월 1일
A = [];
So A is empty.
func = @(p) p./ (1+p) - a./(1-a) .* ...
pi_r.*phi_1.*lr ./ (pi_n.*(a.*A.^(1 ./ a).*(1 - a)^((1-a) ./ a) ./ ...
(((1+p).*phi_1).^((1-a)./a))).*ln) .* (1/mu .* (zeta .* lr.^nu ./ phi_1) - 1);
The computation invovles A, but A is empty. Computations involving empty arrays almost always end up giving empty results.

추가 답변 (1개)

Hideto Koizumi
Hideto Koizumi 2019년 9월 1일
편집: Hideto Koizumi 2019년 9월 1일
@Walter Robinson, that was the problem, it was my silly mistake that I replaced A with empty set for the optimization and reused it for fzero. Now the problem is fixed, and the fzero runs!

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by