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?
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.
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
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/
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
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
In[186]:= NIntegrate[w^2/(s - w), {s, 1, 5}, {w, -5, -1}]
Out[186]= 25.8364
JM
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
inner[w_?NumericQ] := NIntegrate[1/(s - w), {s, 1, 5}]
NIntegrate[w^2*inner[w], {w, -5, -1}]
?
Regards
Jens
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
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
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
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}]
>