I want to NIntegrate
a List
valued function foo[x]
which is only defined for numerical arguments x
.
Here is a simple toy example:
foo[x_?NumericQ] := {x^2, x^3};
NIntegrate[foo[x], {x, 0, 1}]
It gives the following error
NIntegrate::inum: Integrand foo[x] is not numerical at {x} = {0.00795732}. >> NIntegrate::inum: Integrand foo[x] is not numerical at {x} = {0.00795732}. >>.
Note that the symbolic definition fooS
works fine:
fooS[x_] := {x^2, x^3};
NIntegrate[fooS[x], {x, 0, 1}]
(*{0.333333, 0.25}*)
What causes the error?
It seems to be crucial that the function foo
is List
valued.
When I NIntegrate
each component separatly, the error does not occur:
foo1[x_?NumericQ] := foo[x][[1]];
foo2[x_?NumericQ] := foo[x][[2]];
{NIntegrate[foo1[x], {x, 0, 1}], NIntegrate[foo2[x], {x, 0, 1}]}
(*{0.333333, 0.25}*)
and even
NIntegrate[{#1, #2} & @@ {foo1[x], foo2[x]}, {x, 0, 1}]
(*{0.333333, 0.25}*)