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

1 次查看(过去 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 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by