Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nested numerical integration

599 views
Skip to first unread message

tsg....@googlemail.com

unread,
Apr 28, 2009, 4:44:38 AM4/28/09
to
Hi, I'd like to compute a nested integral like,

NIntegrate[ w^2 * NIntegrate[1/(s-w), {s, 1, 5}], {w, -5, -1}]

Unfortunately, mathematica gives me an error (NIntegrate::inumr) and
it also outputs the wrong value. Is there a way to do these types of
integrals?

Bill Rowe

unread,
Apr 29, 2009, 3:47:05 AM4/29/09
to

It gives you an error since the inner integral won't yield a
numerical value since as far as it is concerned w is undefined.
The way to do this is to use more appropriate syntax as shown in
the second example when you look up NIntegrate in the
Documentation Center. That is:

In[2]:= NIntegrate[w^2*1/(s - w), {s, 1, 5}, {w, -5, -1}]

Out[2]= 25.8364

You can easily verify this gives the correct value using
Integrate. That is using the syntax you attempted to use above:

In[3]:= Integrate[w^2*Integrate[1/(s - w), {s, 1, 5}], {w, -5, -1}]

Out[3]= -16-84 log(3)+(250 log(5))/3

which is the same result obtained with:

In[4]:= Integrate[w^2*1/(s - w), {s, 1, 5}, {w, -5, -1}]

Out[4]= -16-84 log(3)+(250 log(5))/3

and

In[5]:= %1 // N

Out[5]= 25.8364

gives the same result as NIntegrate using the appropriate syntax.

Bob Hanlon

unread,
Apr 29, 2009, 3:47:28 AM4/29/09
to
$Version

6.0 for Mac OS X PowerPC (32-bit) (June 19, 2007)

Integrate[w^2*Integrate[1/(s - w), {s, 1, 5}], {w, -5, -1}]

-16-84 log(3)+(250 log(5))/3

% // N

25.8364

f[w_] = Assuming[{w <= 1}, Integrate[1/(s - w), {s, 1, 5}]]

log((w-5)/(w-1))

Plot[w^2*f[w], {w, -5, -1}]

Visually, the integral is roughly 12*4/2 = 24

Integrate[w^2*Integrate[1/(s - w), {s, 1., 5}], {w, -5, -1}]

25.8364

Integrate[w^2*Integrate[1/(s - w), {s, 1, 5}], {w, -5., -1}]

25.83639373697124 + 0.*I

Integrate[w^2*Integrate[1./(s - w), {s, 1, 5}], {w, -5, -1}]

25.83639378805382 + 0.*I

NIntegrate[w^2*Integrate[1/(s - w), {s, 1, 5}], {w, -5, -1}]

25.8364

fn[w_?NumericQ] := NIntegrate[1/(s - w), {s, 1, 5}];

NIntegrate[w^2*fn[w], {w, -5, -1}]

25.8364

Off[NIntegrate::inumr]

NIntegrate[w^2*NIntegrate[1/(s - w), {s, 1, 5}], {w, -5, -1}]

25.8364

Bob Hanlon

David Park

unread,
Apr 29, 2009, 3:49:06 AM4/29/09
to
Mathematica can't do a numerical integration on the inner integral because w
is left as a symbol. So either do a double integral all at once:

NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1}]
25.8364

Or do a symbolic integration on the inner integral and a numerical integral
on the outer:

Assuming[w <= 1, Integrate[1/(s - w), {s, 1, 5}]]
NIntegrate[w^2 %, {w, -5, -1}]
Log[(-5 + w)/(-1 + w)]
25.8364


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/

Leonid Shifrin

unread,
Apr 29, 2009, 6:05:08 AM4/29/09
to
Hi,

direct 2D integration:

In[1] = NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1}]

Out[1] = 25.8364

Step-by-step integration:

In[2] =

Clear[int];
int[w_?NumericQ] := NIntegrate[w^2/(s - w), {s, 1, 5}];
NIntegrate[int[w], {w, -5, -1}]

Out[2] = 25.8364

The second method is more flexible since you may use it for
non-rectangular domains.

Regards,
Leonid

Daniel Lichtblau

unread,
Apr 29, 2009, 6:05:29 AM4/29/09
to
tsg....@googlemail.com wrote:
> Hi, I'd like to compute a nested integral like,
>
> NIntegrate[ w^2 * NIntegrate[1/(s-w), {s, 1, 5}], {w, -5, -1}]
>
> Unfortunately, mathematica gives me an error (NIntegrate::inumr) and
> it also outputs the wrong value. Is there a way to do these types of
> integrals?

Can use one NIntegrate, to avoid the inner one having a "symbolic"
parameter w.

In[3]:= NIntegrate[ w^2 * 1/(s-w), {s, 1, 5}, {w, -5, -1}]
Out[3]= 25.8364

Alternatively, define a black box integrand that only is computable for
numeric input.

In[4]:= f[w_Real] := NIntegrate[1/(s-w), {s, 1, 5}]

In[5]:= NIntegrate[w^2*f[w], {w, -5, -1}]
Out[5]= 25.8364

Daniel Lichtblau
WOlfram Research


Jason Merrill

unread,
Apr 29, 2009, 6:38:10 AM4/29/09
to

In[186]:= NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1}]

Out[186]= 25.8364

JM

Peter Breitfeld

unread,
Apr 29, 2009, 6:38:55 AM4/29/09
to
tsg....@googlemail.com wrote:

Mathematica complains, because your inner integral can't be evaluated
numerically because it depends on the parameter w in it.

You can try this:

NIntegrate[w^2/(s - w), {w, -5, -1}, {s, 1, 5}]

Out= 25.8364

This value seems to be correct (and will be given with your input)
because you can integrate symbolically:

Integrate[w^2 Integrate[1/(s - w), {s, 1, 5}], {w, -5, -1}]

Out= -16 - 84 Log[3] + (250 Log[5])/3

%//N

Out= 25.8364

--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

Jens-Peer Kuska

unread,
Apr 29, 2009, 6:39:28 AM4/29/09
to
Hi,

inner[w_?NumericQ] := NIntegrate[1/(s - w), {s, 1, 5}]
NIntegrate[w^2*inner[w], {w, -5, -1}]

?

Regards
Jens

dh

unread,
Apr 29, 2009, 6:39:50 AM4/29/09
to

Hi,

what makes you think that you get a wrong result?

The error message comes from the fact that mathematica first tries to do

the inner integral symbolically. You may prevent this by defining a

function with numerical arguments: f[x_?NumericQ]:=...

However, this is not the way to double integrals. Further, as long as

you can calculate accurate results you should do so. You should use

Integrate instead of NIntegrate. Here is your example:

Integrate[w^2 1/(s - w), {s, 1, 5}, {w, -5, -1}]

Daniel

Sjoerd C. de Vries

unread,
Apr 29, 2009, 6:41:38 AM4/29/09
to
The answer seems to be correct, at least, it is the same as when
calculated with the synbolic Integrate instead of Nintegrate. You can
remove the error in the following way:

In[266]:= ni[w_?NumberQ] := NIntegrate[1/(s - w), {s, 1, 5}]

In[268]:= NIntegrate[w^2*ni[w], {w, -5, -1}]

Out[268]= 25.8364

In[270]:= Integrate[w^2*Integrate[1/(s - w), {s, 1, 5}], {w, -5, -1}]

Out[270]= -16 - 84 Log[3] + (250 Log[5])/3

In[250]:= -16 - 84 Log[3] + (250 Log[5])/3 // N

Out[250]= 25.8364

Cheers -- Sjoerd

anton...@gmail.com

unread,
May 2, 2009, 5:56:05 AM5/2/09
to
On Apr 29, 6:05 am, Leonid Shifrin <lsh...@gmail.com> wrote:
> Hi,
>
> direct 2D integration:
>
> In[1] = NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1}]
>
> Out[1] = 25.8364
>
> Step-by-step integration:
>
> In[2] =
>
> Clear[int];
> int[w_?NumericQ] := NIntegrate[w^2/(s - w), {s, 1, 5}];
> NIntegrate[int[w], {w, -5, -1}]
>
> Out[2] = 25.8364
>
> The second method is more flexible since you may use it for
> non-rectangular domains.

This statement is not correct. Using the multi-dimentional NIntegrate
specfication gives more flexibility.

1. Multi-dimensional NIntegrate can be used non-rectangular domains.
In the example below the second variable has functional boundary:

In[2]:= NIntegrate[w^2/(s - w), {s, 1, 5}, {w, Sqrt[s], -1}]

Out[2]= -5.07779

You can use sampling points plot to see what the domain looks like:

Needs["Integration`NIntegrateUtilities`"]
NIntegrateSamplingPoints[NIntegrate[w^2/(s - w), {s, 1, 5}, {w, Sqrt
[s], -1}]]


2. Multi-dimensional NIntegrate can be used for integration over parts
of the integration region (or their exclusion).
In the example below a disk with center {3,-3} and radius 1 is
excluded from the integration:

In[12]:= NIntegrate[w^2/(s - w)*Boole[ (s - 3)^2 + (w + 3)^2 > 1], {s,


1, 5}, {w, -5, -1}]

Out[12]= 21.0579

Again you can see the integration domain with

NIntegrateSamplingPoints[
NIntegrate[
w^2/(s - w)*Boole[ (s - 3)^2 + (w + 3)^2 >= 1], {s, 1,
5}, {w, -5, -1}]]


3. The examples above used a mutlti-dimensional integration rule. If
want to use one dimensional rule or, moreover, if you want to use
different one-dimensional rule in each dimension, you can specify this
with the Method option. This specification is closer to an integration
with nested NIntegrate's.

In[19]:= NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1},
Method -> {"GaussKronrodRule", "LobattoKronrodRule"}]

Out[19]= 25.8364

Here are the sampling points:

In[20]:= NIntegrateSamplingPoints[
NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1},
Method -> {"GaussKronrodRule", "LobattoKronrodRule"}]]


Anton Antonov

Leonid Shifrin

unread,
May 4, 2009, 4:42:06 PM5/4/09
to
Hi Anton,

Thanks, I was not aware of that (despite having worked with NIntegrate
quite a bit - there are always things to learn). Taking my words back, and
sorry for the confusion.

Regards,
Leonid


On Sat, May 2, 2009 at 2:56 AM, <anton...@gmail.com> wrote:

> On Apr 29, 6:05 am, Leonid Shifrin <lsh...@gmail.com> wrote:

> > Hi,
> >
> > direct 2D integration:
> >
> > In[1] = NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1}]
> >
> > Out[1] = 25.8364
> >
> > Step-by-step integration:
> >
> > In[2] =
> >
> > Clear[int];
> > int[w_?NumericQ] := NIntegrate[w^2/(s - w), {s, 1, 5}];
> > NIntegrate[int[w], {w, -5, -1}]
> >
> > Out[2] = 25.8364
> >
> > The second method is more flexible since you may use it for
> > non-rectangular domains.
>

> This statement is not correct. Using the multi-dimentional NIntegrate
> specfication gives more flexibility.
>
> 1. Multi-dimensional NIntegrate can be used non-rectangular domains.
> In the example below the second variable has functional boundary:
>
> In[2]:= NIntegrate[w^2/(s - w), {s, 1, 5}, {w, Sqrt[s], -1}]
>
> Out[2]= -5.07779
>
> You can use sampling points plot to see what the domain looks like:
>
> Needs["Integration`NIntegrateUtilities`"]
> NIntegrateSamplingPoints[NIntegrate[w^2/(s - w), {s, 1, 5}, {w, Sqrt
> [s], -1}]]
>
>
> 2. Multi-dimensional NIntegrate can be used for integration over parts
> of the integration region (or their exclusion).
> In the example below a disk with center {3,-3} and radius 1 is
> excluded from the integration:
>

> In[12]:= NIntegrate[w^2/(s - w)*Boole[ (s - 3)^2 + (w + 3)^2 > 1], {s,


> 1, 5}, {w, -5, -1}]
>

0 new messages