function out=fxn1(x)
if x>=0 && x<= 100
out=x;
end
end
If I call it using a handle in quad, as follows:
quad(@fxn1,0,10)
I get the error message:
??? Operands to the || and && operators must be convertible
to logical scalar values.
The example above is a trivial function, but I have some
very complex functions, which take different algebraic
forms for different ranges of their arguments. Defining
them as function m-files, using relational operators in the
m-files, seems convenient. Can anyone suggest how I might
be able to call these functions in quad or other similar
matlab functions, without getting this error message?
>function out=fxn1(x)
>if x>=0 && x<= 100
>out=x;
>end
>end
In the case that x is not in that range, you don't assign the
output argument.
>If I call it using a handle in quad, as follows:
>quad(@fxn1,0,10)
>I get the error message:
>??? Operands to the || and && operators must be convertible
>to logical scalar values.
quad and other similar functions call the given function with
an array of argument values (different x's), and your function
must return one vector element for each input value. So your
statement
if x>=0 && x<= 100
is being applied with x a vector of values. The x>=0
evaluates to a vector of logical results, and the x<= 100 part
evaluates to a vector of logical results, so conceptually
you have two logical vectors with the && operator between them.
But && is only defined for scalars (the operands must be
converible to logical *scalar* values), so You Have A Problem.
You might think of substituting the vector "and" operator, &,
in place of the no-vector &&, something like,
if x>=0 & x<= 100
Now you would have a logical vector on the left, a logical vector
on the right, and a logical operator between them. The expression
after the "if" would then become evaluatable, with a logical vector
as the result. But now we have to consider the question,
"What is the meaning of an 'if' statement whose controlling expression
is a logical vector rather than a scalar?". The result *is* well defined
it turns out: when you have an "if" followed by something that evaluates
to a vector, then the "if" is considered true only if -all- members
of the vector would be considered true. So the above would be equivilent
to
if all(x>=0 & x<= 100)
Chances are that isn't exactly what you want, though.
So, this leaves you with two basic approaches: you can rewrite your
functions taking into account the vectorized nature (e.g., using
logical indexing to control which output values are set to which result);
or you can rewrite your functions with an enclosing "for" loop over
all of the x, assigning to out(K) each time (presuming K is the loop index)
rather than just naked out.
--
"A scientist who cannot prove what he has accomplished,
has accomplished nothing." -- Walter Reisch
Look up the difference between && and || versus & and | ...
hth
Jos
"Jos " <DEL...@jasenDEL.nl> wrote in message
<g14a3q$m6d$1...@fred.mathworks.com>...
Thanks again for your help.
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g14a0f$jcl$1...@canopus.cc.umanitoba.ca>...
Oh, just use the extended symbolic toolbox and feed it the
literal Mathematica code (as a string) and ask it to convert it
to the equivilent maple code and run that. You might even be able
to convince it to convert it all to equivilent Matlab code;
the success there would depend upon how heavily the formulae
change form with different input parameters.
I am no stranger to the equivilent of the Mathematica setDelayed
operator: maple has the same thing, and there are some kinds of
programming that it is very valuable for. Sometimes it gets used to
build up complex expressions and functions that are then evaluated
with actual parameters substituted in, but sometimes it gets used
to build up expressions and functions whose number of terms or structure
might depend upon the current value of a parameter, and the next
trip through the loop might end with an expression with a different
number of terms or whatever but built up using the same meta-methods.
It's all the same to maple, but the latter case is a lot harder to
convert into traditional programming.
--
"The slogans of an inadequate criticism peddle ideas to fashion"
-- Walter Benjamin
Hi Walter,
I haven't tried the extended symbolic toolbox, but I've
been told that it probably won't help with our code. We
specifically need Matlab code, and our functions do change
substantially dependng on argument values. I might take a
look at the extended symbolic toolbox if I can't make
progress longhand, but your earlier suggestion seems to be
working fine for now - for loops in the m-file handle the
vector input and output with no problems.
Thanks again.