0
$\begingroup$

I have a complicated function $f(x)$ whose analytical form does not exist for the following integral $$g(x)=\int_0^x\mathrm{d}x'\,f(x'). $$ I finally need to evaluate the following integral $$\int_0^a\mathrm{d}x\,g(x)~,$$ where $a$ is some finite real number like 1000. Therefore, essentially I need two NIntegrate. However, NIntegrate needs finite integration limit, not some abstract $x$. How do I deal with this problem?

$\endgroup$
6
  • 1
    $\begingroup$ NIntegrate[f[y], {x, 0, a}, {y, 0, x}]? $\endgroup$ Jul 18, 2016 at 13:37
  • $\begingroup$ @J.M., There is a spelling mistake in NIntegrate. $\endgroup$
    – Sumit
    Jul 18, 2016 at 13:40
  • $\begingroup$ @J.M. That does not work: In[25]:= NIntegrate[Exp[-x], {x, 0, y}, {y, 0, 10}] Out[25]= NIntegrate[Exp[-x], {x, 0, y}, {y, 0, 10}] $\endgroup$
    – titanium
    Jul 18, 2016 at 13:45
  • 1
    $\begingroup$ @titanium, look at my code carefully again, and note the order of the second and third arguments. $\endgroup$ Jul 18, 2016 at 13:46
  • 1
    $\begingroup$ For this case, you might use the identity Integrate[(a - x) f[x], {x, 0, a}] == Integrate[f[x], {y, 0, a}, {x, 0, y}] $\endgroup$
    – mikado
    Jul 18, 2016 at 22:18

1 Answer 1

2
$\begingroup$

This can be written down as the parametric iterated integral $$I(a)=\int_0^a\left( \int_0^x f(y)\mathrm dy\right) \mathrm dx$$ NIntegrate supports the syntax

NIntegrate[f,{x,xm,xM},{y,ym,yM}]

(where $x_m,x_M$ are the bounds of the outer integral, $y_m,y_M$ of the inner one) to compute this kind of integrals. This means that you can evaluate your result numerically through the following line of code:

I[a_]:=NIntegrate[f[y],{x,0,a},{y,0,x}]

Just substitute your favourite $f(y)$ inside to obtain the result: mine is

In: I1[a_]:=NIntegrate[1,{x,0,a},{y,0,x}];
In: I1[1]
Out: 0.5
$\endgroup$

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