Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: NIntegrate and FindRoot error

  • To: mathgroup at
  • Subject: [mg75687] Re: NIntegrate and FindRoot error
  • From: Szabolcs <szhorvat at>
  • Date: Tue, 8 May 2007 06:03:15 -0400 (EDT)
  • Organization: University of Bergen
  • References: <> <> <f1msjl$rok$>

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

The problem is that while Plot3D holds its first argument, NIntegrate 
evaluates it immediately. So WG[a1, a2] is evaluated before a1 and a2 
get a numeric value.

Change the definition of WG like this:

WG[kx1_?NumericQ, kx2_?NumericQ] := ...

and make sure that when you evaluate WG[x,y] (symbolic arguments), it 
returns unevaluated. (First you need to clear the old definition of WG, 
otherwise the WG[kx1_, kx2_] and the WG[kx1_?NumericQ, kx2_?NumericQ] 
definitions will coexist.)


  • Prev by Date: can I keep old version running after update to Ver 6?
  • Next by Date: Re: Re: Re: MathPlayer???
  • Previous by thread: NIntegrate and FindRoot error
  • Next by thread: Re: NIntegrate and FindRoot error