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[1] and whenever F[2] or F[3.2] is needed, Mathematica can substitute the value of F[1] 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.

  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Nov 9, 2012 at 17:34

2 Answers 2


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] == 0, F1, {x, 0, 100}];
Plot[F[x], {x, 0, 100}]
  • $\begingroup$ why is the second method more efficient? Probably a dumb question... $\endgroup$
    – chris
    Nov 8, 2012 at 21:45
  • 1
    $\begingroup$ 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$. $\endgroup$
    – Sasha
    Nov 8, 2012 at 21:48
  • $\begingroup$ right. I am dumb ;-) $\endgroup$
    – chris
    Nov 8, 2012 at 21:49
  • 4
    $\begingroup$ 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. $\endgroup$ Nov 12, 2012 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]

plots of exact and approximate definite integrals

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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