Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
error using fzero

Subject: error using fzero

From: Learner

Date: 15 Aug, 2008 23:21:00

Message: 1 of 4

,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:485775

The error message is

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

Error in ==> fzero at 308
    elseif ~isfinite(fx) || ~isreal(fx)"


Could anyone tell me what does this mean and how i should correct it?

Thanks!

Subject: error using fzero

From: Walter Roberson

Date: 16 Aug, 2008 00:27:29

Message: 2 of 4

Learner wrote:
> The error message is
>
> "Operands to the || and && operators must be convertible to logical
> scalar values.
>
> Error in ==> fzero at 308
> elseif ~isfinite(fx) || ~isreal(fx)"

> Could anyone tell me what does this mean and how i should correct it?

fzero does not evaluate your function one point at a time.
Instead, fzero passes your function a vector of values, and expects
your function to return a vector of the same length for the results.
So your elseif is trying to process
~isfinite(vector of x) || ~isread(vector of x)
which does not work because || can only be used for scalar values.

You cannot fix this problem by simply changing the || to | as
then it would become elseif <some logical vector> which
Matlab would treat the same as if you had written
elseif all(~isfinite(fx) | ~isreal(fx))
which is either true or false for the entire vector, whereas you
want to operate on the individual elements of the vector.

What you will have to do is learn to use logical indexing. As
a simple example, consider

if x > 5
  y = x.^2;
else
  y = x.^3 / 7;
end

To make this suitable for use with a loop, do:

y = zeros(size(x));
toobig = x > 5; %this will result in a logical vector
y(toobig) = x(toobig).^2; %this will set y(K) only if toobig(K) is true
y(~toobig) = x(~toobig) .^3 ./ 7; %this does the equivilent of the else

--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

Subject: error using fzero

From: Learner

Date: 17 Aug, 2008 21:24:04

Message: 3 of 4

,gzip(gfe),gzip(gfe)
Bytes: 3607
Xref: news.mathworks.com comp.soft-sys.matlab:485934

On Aug 15, 8:27=EF=BF=BDpm, Walter Roberson <rober...@hushmail.com> wrote:
> Learner wrote:
> > The error message is
>
> > "Operands to the || and && operators must be convertible to logical
> > scalar values.
>
> > Error in =3D=3D> fzero at 308
> > =EF=BF=BD =EF=BF=BD elseif ~isfinite(fx) || ~isreal(fx)"
> > Could anyone tell me what does this mean =EF=BF=BDand how i should corr=
ect it?
>
> fzero does not evaluate your function one point at a time.
> Instead, fzero passes your function a vector of values, and expects
> your function to return a vector of the same length for the results.
> So your elseif is trying to process
> ~isfinite(vector of x) || ~isread(vector of x)
> which does not work because || can only be used for scalar values.
>
> You cannot fix this problem by simply changing the || to | as
> then it would become elseif <some logical vector> which
> Matlab would treat the same as if you had written
> elseif all(~isfinite(fx) | ~isreal(fx))
> which is either true or false for the entire vector, whereas you
> want to operate on the individual elements of the vector.
>
> What you will have to do is learn to use logical indexing. As
> a simple example, consider
>
> if x > 5
> =EF=BF=BD y =3D x.^2;
> else
> =EF=BF=BD y =3D x.^3 / 7;
> end
>
> To make this suitable for use with a loop, do:
>
> y =3D zeros(size(x));
> toobig =3D x > 5; =EF=BF=BD =EF=BF=BD%this will result in a logical vecto=
r
> y(toobig) =3D x(toobig).^2; =EF=BF=BD %this will set y(K) only if toobig(=
K) is true
> y(~toobig) =3D x(~toobig) .^3 ./ 7; =EF=BF=BD%this does the equivilent of=
 the else
>
> --
> Q =3D quotation(rand);
> if isempty(Q); error('Quotation server filesystem problems')
> else sprintf('%s',Q), end

Should I change all the loops into logical indexing to fix this
problem? I do not quite understand what you said. Do you mean that I
should change all the if conditions to logical indexing?

I have some vectors as the parameter of the function for fzero to
solve x. The size of these vectors are variable depending on some
conditions. I don't know if this is the reason that failed the fzero
function. Is it ok to use vector as the parameter/ input of fzero?
I didn't see a clear answer from the matlab documentation.

Thanks a lot!

Subject: error using fzero

From: John D'Errico

Date: 17 Aug, 2008 22:21:01

Message: 4 of 4

Learner <cnfengshuang@gmail.com> wrote in message <0d1b39b6-fc7a-
40d7-87bd-e420e973af19@a1g2000hsb.googlegroups.com>...
> ,gzip(gfe),gzip(gfe)
> Xref: news.mathworks.com comp.soft-sys.matlab:485775
>
> The error message is
>
> "Operands to the || and && operators must be convertible to logical
> scalar values.
>
> Error in ==> fzero at 308
> elseif ~isfinite(fx) || ~isreal(fx)"
>
>
> Could anyone tell me what does this mean and how i should correct it?


Actually, I believe that Walter is wrong.
Consider this simple example.

fun = @(X) [1 2];

fzero(fun,2)
??? Operands to the || and && operators must be convertible to logical scalar
values.

Error in ==> fzero at 333
    elseif ~isfinite(fx) || ~isreal(fx)

See that fun returns a vector, instead of
a scalar result as it must for fzero. fzero
generates an error, testing the output of
your function, which it expects to be a
scalar.

fzero does not expect a vector valued
result, and that is what your function
generated. Finally, while the error I
generated from my example happened
at a different line, 333 versus 308, this
is explained since the OP has a different
MATLAB release.

John

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us