I need to integrate $f(t)$, a complicated function of time, over a very large period $T$. The code is simple:


but what I also would like to do is to extract the result of NIntegrate at regular intervals, during same the evaluation, when $t=n\, k <T$, where $k$ is some number and $n=1,2..5$.

Is there a simple way to do this in Mathematica? or do I have to use Nintegrate over again for each value of $n$ ? Thanks.


2 Answers 2


You can try to use NDSolve instead. But without an example, hard to say, so I made up an example. Lets say your complicated function is

myComplicatedFunction[t_] := t

The following will do repeated NIntegration on the sampling points, just to show that we get same result with NDSolve

pts = Reap[
    NIntegrate[myComplicatedFunction[t], {t, -3, 5}, 
     EvaluationMonitor :> Sow[t]]][[2, 1]];

data = {#, NIntegrate[myComplicatedFunction[t], {t, -3, #}]} & /@ pts;

ListStepPlot[data, Mesh -> All,  AxesLabel -> {"t", "Area under the curve"}, 
     GridLines -> Automatic, GridLinesStyle -> LightGray, 
     PlotStyle -> Blue, MeshStyle -> Red]

Mathematica graphics

Now using NDSolve

sol = First@
   NDSolve[{f'[t] == myComplicatedFunction[t], f[-3] == 0}, f, {t, -3, 5}];
data = {#, Evaluate[f[#] /. sol]} & /@ pts;

Plotting the data above gives same plot as above, which is the value of the integral at each sample point. I think using NDSolve and evaluating the solution at the sampling point is more efficient than integrating repeatedly for different ranges.

  • $\begingroup$ Thanks to both of you that helped. $\endgroup$
    – user91411
    Commented Feb 20, 2017 at 17:39

I would turn your problem into an ODE. For example, suppose f is:

f[t_] := Sin[LogGamma[t+1]]

Then, you could use NDSolveValue to get an interpolating function for g over the range of interest:

sol = NDSolveValue[{g'[t] == f[t], g[0] == 0}, g, {t, 0, 10}]

InterpolatingFunction[Domain: {{0.,10.}} Output: scalar]

The intermediate values are then obtained by evaluating sol at the desired points. For example, suppose you wanted the values of g for 1 .. 5:



This is basically equivalent to evaluating NIntegrate for each intermediate value of interest:




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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