I want to calculate cumulative integrals of the form $$ f(x) = \int_{-\infty}^x dt\ F(t) $$ efficiently in Mathematica. $F(t)$ could be a simple function but also some random function that does not have a primitive integral.

tmax = 100;
f[x_] := NIntegrate[Sin[t], {t, 0, x}];
Plot[f[x], {x, 0, tmax}, PlotLabel -> "Solution to ODE (Smoothing)"]

This takes a very long time to execute and I assume, that when it plots, the cumulative sum is calculated for each single x-value independently, while it could just store the result of the previous computation and add the new term to it. My question is: Is there a way to calculate the cumulative sum more efficiently here?

An idea is to map the values of $F(t)$ on some choosen $x$-values values and then use Mathematica's Accumulate function. But that becomes cumbersome as soon as several nested integrals are involved(e.g. the cumulative sum of the cumulative sum...). That is why, I hope, that you have better ideas.

  • 5
    $\begingroup$ Over a finite interval, f = NDSolve[{y'[x] == F[x], y[a] == 0}, y, {x, a, b}], where a and b are given numeric values, can't be beat, I don't think. You can plot it with LineLinePlot[f] or Plot[f[x], {x, a, b}]. $\endgroup$
    – Michael E2
    Commented Nov 13, 2021 at 14:31
  • $\begingroup$ Related/duplicate: mathematica.stackexchange.com/a/59892/4999 $\endgroup$
    – Michael E2
    Commented Nov 13, 2021 at 14:33