       Re: NIntegrate and FindRoot error

• To: mathgroup at smc.vnet.net
• Subject: [mg75688] Re: NIntegrate and FindRoot error
• From: dh <dh at metrohm.ch>
• Date: Tue, 8 May 2007 06:03:48 -0400 (EDT)
• References: <20070507050017.M68851@phys.cts.nthu.edu.tw> <463EC1B7.1080102@smc.vnet.net> <f1msjl\$rok\$1@smc.vnet.net>

```
Hi Judy,

this is so common a mistake and not really described in the Mathematica book

that nearly everybody steps into this trap at least once.

Although NIntegrate has the attribute HoldAll, it nevertheless evaluates

its first argument once with a symbolic integration variable. To

prevent this evaluation, the function has to be declared for numerical

arguments only. The definition of WG would then look like:

WG[kx1_/;NumericQ[kx1],kx2_/;NumericQ[kx2]]:=

hope this helps, Daniel

Judith Bunder wrote:

> Hi,

>

> I have to perform a double integral. To evaluate the integrand I have to use

> FindRoot and this seems to work as I can evaluate the integrand at

> specific points and even make a 3D plot over the whole range. However, when

> I try to use NIntegrate I get a FindRoot::"nlnum" error message:

>

> Here is a simplified version of my code:

>

> Ly = 3;

> w = 5;

> t2 = 1;

> t1[kx_] := 2 t2 Cos[kx/2];

>

> py[kx_, m_ /; m >= 1 && m <= Ly] := p/. FindRoot[(2 Ly + 1) p +ArcTan[ Tan

> [p] (t1[kx] - t2)/(t1[kx] + t2)]==m Pi,{p,1}]

>

> WG[kx1_, kx2_] := Sum[Sum[py[kx1, m1] py[kx2, m2],{m1,1,Ly-1}],{m2,1,Ly-1}]

>

> Plot3D[Re[Exp[ I w (a2 - a1)] WG[a1, a2]], {a2, -Pi, Pi}, {a1, -Pi, Pi}]

> Plot3D[Im[Exp[ I w (a2 - a1)] WG[a1, a2]], {a2, -Pi, Pi}, {a1, -Pi, Pi}]

>

> NIntegrate[Exp[ I w (a2 - a1)] WG[a1, a2], {a2, -Pi, Pi}, {a1, -Pi, Pi}]

>

> The NIntegrate command fails. It appears to be objecting to my Cos[2 kx]

> function in the FindRoot command. I don't understand why it would object as

> Plot3D over the same integrand worked.

>

> Thanks a lot,

> Judy Bunder

>

>

```

• Prev by Date: Re: Format->Magnification does not work
• Next by Date: Re: [Mathematica 6] Save As HTML does not produce any output
• Previous by thread: Re: NIntegrate and FindRoot error
• Next by thread: Struve functions