Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


While answering this question, I defined (symbolic and numerical) path integrations as follows

 ContourIntegrate[f_, par : (z_ -> g_), {t_, a_, b_}] := 
       Integrate[Evaluate[(f /. par) D[g, t]], {t, a, b}]

 NContourIntegrate[f_, par : (z_ -> g_), {t_, a_, b_}] := 
       NIntegrate[Evaluate[D[g, t] (f /. par) /. t -> t1], {t1, a, b}]

I also defined a piecewise contour

pw[t_, a_: 1] = Piecewise[{{a Exp[I t], t < Pi}, {-a + 2 a (t - Pi)/Pi, t >= Pi}}]
ParametricPlot[pw[t] // {Re[#], Im[#]} &, {t, 0, 2 Pi}]

Mathematica graphics

While checking these routines on wikipedia examples, I tried numerically

 Table[NContourIntegrate[Exp[I i x]/(x^2 + 1), x -> pw[t, 2], {t, 0, 2 Pi}], {i, 2, 5}] // Chop

(* {0.425168,0.156411,0.0575403,0.0211679} *)

which corresponds accurately to (see example II for Cauchy distributions)

Table[Exp[-i] Pi, {i, 2, 5}] // N

On the other hand, the symbolic integration

ContourIntegrate[Exp[I x]/(x^2 + 1), x -> pw[t, 2], {t, 0, 2 Pi}] // FullSimplify

returns 0.


What am I doing wrong?


Example I and III work ;-)

ContourIntegrate[1/(x^2 + 1)^2, x -> pw[ t, 2], {t, 0, 2 Pi}] // FullSimplify
NContourIntegrate[1/(x^2 + 1)^2, x -> pw[ t, 2], {t, 0, 2 Pi}] // Chop

(* Pi/2  1.5708 *)


NContourIntegrate[1/I/x/(1 + 3 ((x + 1/x)/2)^2), x -> Exp[I t], {t, 0, 2 Pi}]//Chop
ContourIntegrate[1/I/x/(1 + 3 ((x + 1/x)/2)^2), x -> Exp[I t], {t, 0, 2 Pi}]

(* 3.14159  Pi *)
share|improve this question
For Example II, with exactly the same code as you have (except that I changed your x more suggestively to z), Mathematica 8.0.4 is giving me result Pi/E. This agrees with the answer provided by the Residue Theorem. – murray Oct 27 '12 at 15:39
@murray uhm... it seems you are right.. – chris Oct 27 '12 at 15:43
The answer you got from ContourIntegrate in Example IIIa is correct -- and is the same, except for form, as the answer $\pi \sqrt{2}/4$ obtained from substitution and the Residue Theorem as in the wikipedia page you cite. – murray Oct 27 '12 at 15:43
@murray right again... ghosh I guess I am out of my mind.. – chris Oct 27 '12 at 15:48
But you have a nicely defined contour integration routine. Note that it's certainly not necessary to pack parameterization of the entire contour into a single Piecewise function; that just forces one do some contortions to get the t-value at the end of one piece to match up with the t-value at the start of the second piece. It seems it would be simpler to separately parameterize the semicircle and the $x$-axis and evaluate the contour integrals of both; then just add. – murray Oct 27 '12 at 15:54
up vote 7 down vote accepted

The problem lies with the fact that my init.m file has

 SetOptions[Integrate, GenerateConditions -> False]

If I use

 SetOptions[Integrate, GenerateConditions -> True]

or define

 ContourIntegrate[f_, par : (z_ -> g_), {t_, a_, b_}] := 
   Integrate[Evaluate[(f /. par) D[g, t]], {t, a, b}, GenerateConditions -> True]

the discrepency vanishes.

I guess this makes this question too narrow to be of general interest!

In any case, the bring home message is don't do complex integration without paying attention to branch-cuts !

share|improve this answer
No, I don't think this is too narrow an issue: the post might help somebody else with a mysterious result! – murray Oct 27 '12 at 19:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.