Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 13 down vote accepted

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}]
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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