17
$\begingroup$

I believe this question is best illustrated with a simple example. If I run

FunctionInterpolation[NIntegrate[a + b, {a, 0, 1}], {b, 0, 1}]

I get errors of the form

NIntegrate::inumr: The integrand a+b has evaluated to non-numerical values for all 
sampling points in the region with boundaries {{0,1}}.

So I guess Mathematica is evaluating the expression before plugging in values and trying to interpolate it. Admittedly the code does yield apparently correct results, so I could just turn off the error message and be done with it, but I'd like to address the root cause if possible. Is there some more correct way to do what I'm trying to do that would not spit out an error message in the first place?

$\endgroup$

1 Answer 1

18
$\begingroup$

You could define a function which is evaluated only when its argument is numeric :

int[b_?NumericQ] := NIntegrate[a + b, {a, 0, 1}]

FunctionInterpolation[int[b], {b, 0, 1}]

Here you can find a Wolfram support article explaining why NumericQ helps here and how to use it, in detail.

$\endgroup$
4
  • $\begingroup$ Ah, good idea, that could work. It'd be nice to avoid having to define a separate function, though. $\endgroup$
    – David Z
    Jul 31, 2012 at 9:00
  • 4
    $\begingroup$ There is an article here support.wolfram.com/kb/3820 that helps explain why you should use NumericQ. $\endgroup$
    – Searke
    Jul 31, 2012 at 19:19
  • $\begingroup$ Sadly the link to the aforementioned link is now broken. $\endgroup$
    – Corvus
    Oct 9, 2017 at 4:09
  • $\begingroup$ @Corvus A quick search gives http://support.wolfram.com/kb/12502, possibly the same article. $\endgroup$ Oct 9, 2017 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.