0

I want to be able to do the integral below completely numerically.

equation

where n, epsilon and a, b and beta are constants which for simplicity, can all be set to 1.

The integral over x can be done analytically by hand or using Mathematica, and then the integral over y can be done numerically using NIntegrate, but these two methods give different answers.

Analytically:

In[160]:= ex := 2 (1 - Cos[x])

In[149]:= ey := 2 (1 - Cos[y])

In[161]:= kx := 1/(1 + Exp[ex])

In[151]:= ky := 1/(1 + Exp[ey])

In[162]:= Fn1 := 1/(2 \[Pi]) ((Cos[(x + y)/2])^2)/(ex - ey)

In[163]:= Integrate[Fn1, {x, -Pi, Pi}]

Out[163]= -(1/(4 \[Pi]))
 If[Re[y] >= \[Pi] || \[Pi] + Re[y] <= 0 || 
   y \[NotElement] Reals, \[Pi] Cos[y] - Log[-Cos[y/2]] Sin[y] + 
   Log[Cos[y/2]] Sin[y], 
  Integrate[Cos[(x + y)/2]^2/(Cos[x] - Cos[y]), {x, -\[Pi], \[Pi]}, 
   Assumptions -> ! (Re[y] >= \[Pi] || \[Pi] + Re[y] <= 0 || 
       y \[NotElement] Reals)]]

In[164]:= Fn2 := -1/(
  4 \[Pi]) ((\[Pi] Cos[y] - Log[-Cos[y/2]] Sin[y] + 
     Log[Cos[y/2]] Sin[y]) (1 - ky) ky )/(2 \[Pi])

In[165]:= NIntegrate[Fn2, {y, -Pi, Pi}]

Out[165]= -0.0160323 - 2.23302*10^-15 I

Numerical method 1:

In[107]:= Fn4 := 
 1/(4 \[Pi]^2) ((Cos[(x + y)/2])^2) (1 - ky) ky/(ex - ey)

In[109]:= NIntegrate[Fn4, {x, -Pi, Pi}, {y, -Pi, Pi}]

During evaluation of In[109]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. >>

During evaluation of In[109]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 18 recursive bisections in x near {x,y} = {0.0000202323,2.16219}. NIntegrate obtained 132827.66472461013` and 19442.543606302774` for the integral and error estimates. >>

Out[109]= 132828.

Numerical 2:

In[113]:= delta = .001;
pw[x_, y_] := Piecewise[{{1, Abs[Abs[x] - Abs[y]] > delta}}, 0]

In[116]:= Fn5 := (Fn4)*pw[Cos[x], Cos[y]]

In[131]:= NIntegrate[Fn5, {x, -Pi, Pi}, {y, -Pi, Pi}]

During evaluation of In[131]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. >>

During evaluation of In[131]:= NIntegrate::eincr: The global error of the strategy GlobalAdaptive has increased more than 2000 times. The global error is expected to decrease monotonically after a number of integrand evaluations. Suspect one of the following: the working precision is insufficient for the specified precision goal; the integrand is highly oscillatory or it is not a (piecewise) smooth function; or the true value of the integral is 0. Increasing the value of the GlobalAdaptive option MaxErrorIncreases might lead to a convergent numerical integration. NIntegrate obtained 0.013006903336304906` and 0.0006852739534086272` for the integral and error estimates. >>

Out[131]= 0.0130069

So neither of the numerical methods give -0.0160323. I understand why - the first method has trouble with the infinities caused by the denominator, and the second method effectively deletes the part of the integral that is causing problems. But I'd like to be able to integrate another integral (a harder one over x, y and z) which can't be simplified analytically. The above integral gives me a way to test any new method since I know what the answer should be.

Amro
  • 123,847
  • 25
  • 243
  • 454
Calvin
  • 225
  • 3
  • 7
  • 1
    not directly related to the question, but it would be useful (ie, it would save you some effort) if you learnt to define functions in mathematica – acl Aug 17 '11 at 23:36
  • 2
    In your first approach you seem to have cut and pasted a conditional result from the analytic integral into your numeric one. Bad idea, because the conditions rather explicitly preclude integrating y over a range from -pi to pi. – Daniel Lichtblau Aug 17 '11 at 23:52
  • possible duplicate of http://stackoverflow.com/questions/7078836/how-to-overcome-singularities-in-numerical-integration-in-matlab-or-mathematica ? – Verbeia Aug 18 '11 at 00:00
  • @Verbeia no, this is another integral (which doesn't vanish) – acl Aug 18 '11 at 00:02
  • @Daniel if I use PrincipalValue ->True, the condition is that $y$ is between $-\pi$ and $\pi$, is that appropriate to use? – Calvin Aug 18 '11 at 00:06
  • Is the numerator supposed to be (1-n(y)) n(y) or (1 - n(x)) n(y)? – rcollyer Aug 18 '11 at 01:50
  • @rcollyer it does look like a constraint for transitions with hard-core particles (in which case it would be like you say) does it not... – acl Aug 18 '11 at 02:18
  • Oh wow, all this time I saw n(x) as having a - sign instead of +. Amazing! Lucky it didn't affect my answer... So they're fermions then... – acl Aug 18 '11 at 02:38

1 Answers1

3

Unless I wrote down the integral wrong, this should do the job:

n[x_] := 1/(1 + Exp[eps[x]])
eps[x_] := 2(1 - Cos[x])
.25/(2*Pi)^2*NIntegrate[
    Cos[(x + y)/
     2]^2 ((1 - n[y]) n[y] - (1 - n[x]) n[x])/(Cos[y] - Cos[x]),
    {x, -Pi, Pi},
    {y, -Pi, Pi},
    Exclusions -> {Cos[x] == Cos[y]}
  ]

giving 0.0130098.

OK, I guess the quickest way to explain is like this:

enter image description here

going from the first to the second line of the first eq I've just renamed y to x (the region of integration is symmetric so it's OK). Then I have added the two (equivalent) expressions for I to obtain 2I, and what I numerically integrate is that. The point is that the numerator and denominator vanish at the same points, so in fact the Exclusions option isn't needed. Note that in my sketch of the method above I dropped the 1/(4*Pi^2) for brevity (or due to laziness, depending on the viewpoint).

acl
  • 6,490
  • 1
  • 27
  • 33
  • That's a brilliant idea @acl ... If I do the integral though, I get 0.0130098, not 0.0170457, though sleepiness could be a factor on my side too. – Calvin Aug 18 '11 at 00:43
  • @Calvin no I probably messed up in some trivial way. i'll take another look tomorrow – acl Aug 18 '11 at 00:44
  • I found it - I used my own code, so I missed that your eps and denominator are both a factor of 2 different to mine. – Calvin Aug 18 '11 at 00:55
  • @Calvin oops sorry, you're right! let me fix that. well, they agree now. so, off-topic, but, this is presumably something involving a square 2d lattice, bosons, and first order perturbation theory? – acl Aug 18 '11 at 00:58
  • +1 for integrating coolness. Not to spoil a nice party, but aren't we a bit drifting away from Mathematica as a programming language? This all seems to go in the direction of the Mathematics or Physics SE. – Sjoerd C. de Vries Aug 18 '11 at 07:12
  • @acl It's first order perturbation theory for a 1D system of fermions. Thanks for your help! – Calvin Aug 19 '11 at 15:39
  • @Calvin no problem. funny, I'm writing a paper about just this (1d fermions) right now... – acl Aug 19 '11 at 15:59