Integration of function containing if-test
Torquil Macdonald Sørensen
torquil at gmail.com
Thu Feb 5 11:17:59 CST 2009
Hi!
I am having some problems with integration of the following function
(this is really a simplification of my function, but the problem I
illustrate is the same).
I have a function nu(x) that is defined using an if-test. It has a
special value (0) at x = 0, and is defined in terms of an ordinary
function f(x) everywhere else. Therefore I have:
function y = nu(x)
if (x == 0)
y = 0;
else
y = f(x);
endif
endfunction
When I pass it a 0 it returns a zero. But when I pass it a vector [0 1]
it doesn't work, because the if-test is apparently not executed on each
element of the vector individually, but on the vector x as a whole.
Since x = [0 1] is not equal to 0, the first part of the if-test is
never entered, and therefore f(x) is evaluated at both 0 and 1, thereby
returning the wrong result. In my case it returns [ NaN 1], since f(0) =
Nan and f(1) = 1.
So I get problems when integrating this function e.g. with quadl, if the
list of values at which it is evaluated contains 0.
Is there an elegant way to define this function nu(x) so that is
actually returns a 0 for each zero element of its input vector argument
that is given to it by quadl?
Best regards
Torquil Sørensen
Norway
More information about the Help-octave
mailing list