# fzero line 398 error; debugger not solving...

1 view (last 30 days)
Sergio Quesada on 15 Oct 2018
Edited: Sergio Quesada on 15 Oct 2018
I am having a problem with fzero in this code :
prueba = r_teor (8e-3)
function Arg = r_teor (t0)
FC = 4.24E-1;
a = 6.05;
FD = 8.17E-4;
Io = 15.53;
V = 25.00;
texp=0.009:0.0001:0.0099;
Arg = zeros (N,1); % prellocating
for i = 1 : N
options=optimset('Display','iter','TolX',1e-5);
% Display: off / iter / final / notify
% TolX: Default 2.204 e-16
Arg (i) = fzero (@(r) trapecios (r) - (texp(i)-t0), 5e-3, options);
end
function Int = trapecios(r)
if r <= 1
Int = 0;
return
end
F = @(x) FC .* exp( -(V.*a) ./ (30.*x.*log(x)) );
Int = 0;
part = 1e-3;
L = 1:part:r;
me = F(L);
sum_me = sum (me);
ue = F( max( L ) );
Int = zeros (N,1); % prellocating
i = 1:N;
Int (i) = part.* (sum_me - (ue/2));
end % trapecios
end % r_teor
The error is:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 398)
if ~isfinite(fb) || ~isreal(fb) || ~isfinite(b)
, but the debugger sais that both b and fb are real and finite:
K>> b, fb, whos b, whos fb
b =
1.163523750296039
fb =
1.0e-03 *
-0.999999999999276
-0.999999999999276
-0.999999999999276
-0.999999999999276
-0.999999999999276
-0.999999999999276
-0.999999999999276
-0.999999999999276
-0.999999999999276
-0.999999999999276
Name Size Bytes Class Attributes
b 1x1 8 double
Name Size Bytes Class Attributes
fb 10x1 80 double
My function trapecios has no problem when executing it on b = 1.163523750296039.
Could it be because fzero cannot find zeros if the function does not change it sign (as x^2) ? Could it be because I must enter t0 as a 1:10 array (t0 is the variable of r_teor, so how could I do it) ?
Any idea? Thanks !!!

Steven Lord on 15 Oct 2018
What requirement does fzero impose on the function you pass to it?
Function to solve, specified as a handle to a scalar-valued function or the name of such a function. fun accepts a scalar x and returns a scalar fun(x).
Does your function satisfy that requirement? In particular, does it return a scalar fun(x)?
If you want to solve a nonlinear system of equations, see fsolve in Optimization Toolbox.
Sergio Quesada on 15 Oct 2018
Thanks, Steven Lord!!
I used fzero because I am not trying to solve a system of equations, but an array of single equations...What is the right way to declaring it?
I have also tried to enter "t0" as an 1:10 array, but the same problem does not change...
I have also changed fzero with fsolve, but it solve the equations at the starting point:
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 2 3.61e-06 0 1
Equation solved at initial point.
fsolve completed because the vector of function values at the initial point
is near zero as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
prueba =
0.005000000000000
0.005000000000000
0.005000000000000
0.005000000000000
0.005000000000000
0.005000000000000
0.005000000000000
0.005000000000000
0.005000000000000
0.005000000000000
Thanks !!