# Efficient evaluation of functions defined by NIntegrate

I have a complicated function $f$ and I want to plot the function $F(x)$ defined by the definite integral of $f$ from $0$ to $x$: $$F(x) = \int_0^x f(y)\mathrm dy.$$ Apparently $f$ cannot be integrated in closed-form, and I use NIntegrate[] instead

F[x_] := NIntegrate[f[y], {y, 0, x}];
Plot[F[x], {x, 0, 100}]


I would like to improve the efficiency of this computation by telling Mathematica that F[s + t] is simply F[s] + NIntegrate[f[y], {y, s, t}] so that (for example) Mathematica can save the value of F and whenever F or F[3.2] is needed, Mathematica can substitute the value of F and compute only the integral of the remaining interval.

Essentially I am looking for the continuous version of the trick one uses to compute the Fibonacci sequence:

f[n_] := f[n] = f[n-1] + f[n-2]


Is there a simple way to implement this? Any help is much appreciated.

• This is a relevant question. I have run into this problem more than once, and it's clear that NIntegrate is going to do the same work again and again ... I always ended up precomputing $F(x)$ and storing it as an interpolating function. This you can do with NIntegrate in one step, or as Sasha said, more efficiently and more precisely using NDSolve (more precisely because of automatic step control). Anyway, the general idea is to precompute an InterpolatingFunction, no matter how you do it. – Szabolcs Nov 9 '12 at 17:34

You can define function to only evaluate on explicit reals:

F[x_Real] := NIntegrate[f[y], {y, 0, x}];
Plot[F[x], {x, 0, 100}]


But a more efficient way would be to use NDSolve instead of NIntegrate:

F[x_] = F1[x] /. NDSolve[F1'[x] == f[x] && F1 == 0, F1, {x, 0, 100}];
Plot[F[x], {x, 0, 100}]

• why is the second method more efficient? Probably a dumb question... – chris Nov 8 '12 at 21:45
• Because it builds a running value of the integral in one go (returning the interpolating function, containing these values) using the additivity property of the integral. The integral function does satisfy $F^\prime(x) = f(x), F(0)=0$. – Sasha Nov 8 '12 at 21:48
• right. I am dumb ;-) – chris Nov 8 '12 at 21:49
• When using the NDSolve method to get the indefinite integral in this way, please be aware of the different meaning of the target precision and accuracy of the result. For NIntegrate it is the error in the value of the integral (which you probably want). For NDSolve it is the error in the numerical satisfaction of the differential equation at each time step. I recommend doing a few of the more expensive NIntegrate calculations to verify you are happy with the NDSolve result at all points in the domain. – Andrew Moylan Nov 12 '12 at 4:19

As noted by Szabolcs, here is another way to approximate the definite integral of a function:

f = FunctionInterpolation[Exp[-x] Sin[x], {x, 0, 3 π}];
F = Derivative[-1][f]; (* the integral *)

Plot[{F[x], 1/2 - E^-x (Cos[x] + Sin[x])/2}, {x, 0, 3 π}, PlotRange -> All] An example of the use of the approach in this answer and Sasha's answer is in this question; my answer in that other question used Sasha's approach, while Szabolcs's answer used the approach in this answer.