# Solving elliptic integrals in Mathematica

I have an integral

$$\int_{a2}^{a1}\frac{dx}{\sqrt{(a1 -x)(a2 - x)(a3 - x)}}$$

And I'm trying to integrate it with

F[u] = (u1 - u)*(u2 - u)*(u3 - u)

L = Integrate[1/Sqrt[F[u]], {u, u1, u2}]


But it won't run. Any ideas why?

• Does this have a closed-form formula?
– user49048
Mar 10, 2022 at 22:14
• I answered many similar questions and you should examine them all carefully, e.g. 1, 2, 3 this list can be continued. Mar 10, 2022 at 23:11
• Read also 4, 5, 6, 7. Mar 10, 2022 at 23:15
• And it can be expressed automatically if you prescribe appropriate numbers insted of symbolic (unknown) constants, e.g. Integrate[1/Sqrt[(1 - u) (2 - u) (3 - u)], {u, 1, 2}] yields -2 I EllipticK[-1] Mar 10, 2022 at 23:24
• final comment: thanks @Artes for providing the links. the answers are thoroughly detailed. very useful stuff. (+1) to all.
– user49048
Mar 10, 2022 at 23:46

Clear["Global*"]

$Version (* "13.0.1 for Mac OS X x86 (64-bit) (January 28, 2022)" *) F[u_] = (u1 - u)*(u2 - u)*(u3 - u); Assuming[{u3 > u2 > u1}, L = Integrate[1/Sqrt[F[u]], {u, u1, u2}] // Simplify] (* (2 (-9 I EllipticK[(u1 - u3)/(u1 - u2)] + EllipticK[(-u2 + u3)/(u1 - u2)]))/Sqrt[-u1 + u2] *)  EDIT: As pointed out in a comment by Akku14, there appears to be a problem with this result. Looking at the specific case of {u1 -> 2, u2 -> 3, u3 -> 4} L2 = L /. {u1 -> 2, u2 -> 3, u3 -> 4} (* 2 (EllipticK[-1] - 9 I EllipticK[2]) *)  Using arbitrary precision to avoid machine-precision calculations, N[L2, 15] (* -20.9764604343370 - 23.5985179886291 I *)  Comparing with integration after substitution, L3 = Integrate @@ ({1/Sqrt[F[u]], {u, u1, u2}} /. {u1 -> 2, u2 -> 3, u3 -> 4}) (* -2 I EllipticK[-1] *) N[L3, 15] (* -2.62205755429212 I *)  This result agrees with direct numeric integration L3 == NIntegrate[##, WorkingPrecision -> 15] & @@ ({1/Sqrt[F[u]], {u, u1, u2}} /. {u1 -> 2, u2 -> 3, u3 -> 4}) (* True *)  This indicates a problem with the general result, L. I will submit this to Wolfram Tech Support (CASE:4924364. Response: "It does appear that Integrate is returning an incorrect result in this case. I have forwarded an issue report to our developers with the information you provided"). • Just a comment that I think is noteworthy: you can get an answer for other ordering than$u_3 > u_2 > u_1\$ as well. At least in some cases. For instance Integrate[1/Sqrt[(2 - u) (3 - u) (1 - u)], {u, 2, 3}] evaluates to 2 EllipticK[-1]. I am on v12.
– user49048
Mar 10, 2022 at 23:48
• May be a typo or a bug in 13.0.1 @BobHanlon ? NIntegrate says the result is (2 (-I EllipticK[(u1 - u3)/(u1 - u2)] + EllipticK[(-u2 + u3)/(u1 - u2)]))/ Sqrt[-u1 + u2] /. {u1 -> 2, u2 -> 3, u3 -> 4} // N  without the factor 9  at the beginning. NIntegrate[1/Sqrt[F[u] /. {u1 -> 2, u2 -> 3, u3 -> 4}], Evaluate[{u, u1, u2} /. {u1 -> 2, u2 -> 3, u3 -> 4}]]  yield 0. - 2.62206 I  . Mar 11, 2022 at 8:01

If the variables can be ordered such that $$0 < \mathtt{u3} < \mathtt{u2} < \mathtt{u} < \mathtt{u1}$$ , then the integral gives :

Integrate[1/Sqrt[(u1 - u)*(u2 - u)*(u3 - u)], {u, u2, u1},
Assumptions -> {0 < u3 < u2 < u1}]

(* (2*EllipticK[-((u1 - u2)/(u2 - u3))])/Sqrt[u2 - u3] *)


If you don't like the negative argument of the elliptic integral, apply the imaginary modulus transformation to get:

(2/Sqrt[u1 - u3])*EllipticK[(u1 - u2)/(u1 - u3)]


which seems correct as seen in the plot:

u1 = 3; u2 = 2; Plot[{NIntegrate[1/Sqrt[(u1 - u)*(u2 - u)*(u3 - u)], {u, u2, u1}],
(2/Sqrt[u1 - u3])*EllipticK[(u1 - u2)/(u1 - u3)]}, {u3, 0, u2},
PlotStyle -> {Blue, Dashed}]


Edit: The case $$\mathtt{u2} < \mathtt{u1} < \mathtt{u3}$$ gives

Integrate[1/Sqrt[(u1 - u)*(u2 - u)*(u3 - u)], {u, u2, u1},
Assumptions -> {0 < u2 < u1 < u3}]
(* (2*(EllipticK[(u1 - u3)/(u1 - u2)] -
I*EllipticK[(-u2 + u3)/(u1 - u2)]))/Sqrt[u1 - u2] *)


a pure imaginary result and

the case $$\mathtt{u2} < \mathtt{u3} < \mathtt{u1}$$ contains a singularity within the integration range at $$\mathtt{u3}$$,that requires more attention...

u1 = 3; u2 = 2; Plot[{Abs[
NIntegrate[1/Sqrt[(u1 - u)*(u2 - u)*(u3 - u)], {u, u2, u1}]],
Abs[(2*(EllipticK[(u1 - u3)/(u1 - u2)] -
I*EllipticK[(-u2 + u3)/(u1 - u2)]))/Sqrt[u1 - u2]]},    {u3,u2, u1}],


because the numerical integration becomes unstable.

The machinery I presented in this answer can be used to obtain a result in terms of the Carlson symmetric integrals. To wit,

With[{cc = {{a1, -1}, {a2, -1}, {a3, -1}, {1, 0}},
pairs = {{1, 2}, {1, 3}, {2, 3}}, x = a2, y = a1},
-2 Apply[CarlsonRF,
Table[With[{g1 = cc[[id]],
g2 = cc[[Complement[Range[4], id]]]},
(Apply[Times, Sqrt[g1 . {1, x}] Sqrt[g2 . {1, y}]] +
Apply[Times, Sqrt[g2 . {1, x}] Sqrt[g1 . {1, y}]])/
(x - y)], {id, pairs}]^2]] // Simplify
-π CarlsonRK[a1 - a3, a2 - a3]


where we immediately obtain an answer in terms of the complete Carlson integral of the first kind, CarlsonRK[]. As a numerical example:

With[{a1 = 2, a2 = 3, a3 = -5},
{N[-π CarlsonRK[a1 - a3, a2 - a3], 25],
NIntegrate[1/Sqrt[(a1 - x) (a2 - x) (a3 - x)], {x, a2, a1}, WorkingPrecision -> 25]}]
{-1.148105728739062085568889, -1.148105728739062085568889}


This can of course be expressed in terms of the usual arithmetic-geometric mean (AGM):

-π CarlsonRK[a1 - a3, a2 - a3] /. CarlsonRK[x_, y_] :> 1/ArithmeticGeometricMean[Sqrt[x], Sqrt[y]]
-(π/ArithmeticGeometricMean[Sqrt[a1 - a3], Sqrt[a2 - a3]])


which can then be expressed in terms of EllipticK[]:

Assuming[a1 > a3 && a2 > a3, FullSimplify[FunctionExpand[%]]]
-((4 EllipticK[(Sqrt[a1 - a3] - Sqrt[a2 - a3])^2/
(Sqrt[a1 - a3] + Sqrt[a2 - a3])^2])/
(Sqrt[a1 - a3] + Sqrt[a2 - a3]))
`