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.
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 withNIntegrate
in one step, or as Sasha said, more efficiently and more precisely usingNDSolve
(more precisely because of automatic step control). Anyway, the general idea is to precompute anInterpolatingFunction
, no matter how you do it. $\endgroup$