,gzip(gfe),gzip(gfe)
Bytes: 3607
Xref: news.mathworks.com comp.softsys.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!
