I'm new to numerical integration. Why is this code wrong?
N[Integrate[(b^2 Sin[θ]^3 Cos[ϕ]^2)/(b^2 Cos[ϕ]^2 Sin[θ]^2 + a^2 Sin[ϕ]^2 Sin[θ]^2 +
a^2 b^2 Cos[θ]^2), {θ, 0, Pi}, {ϕ, 0, 2 Pi}]]
$a$ and $b$ are real values.
You can give Integrate
assumptions, e.g.:
integrand = (b^2 Sin[θ]^3 Cos[ϕ]^2)/(b^2 Cos[ϕ]^2 Sin[θ]^2+a^2 Sin[ϕ]^2 Sin[θ]^2+a^2 b^2 Cos[θ]^2);
Integrate[
integrand,
{θ,0,π},
{ϕ,0,2 π},
Assumptions->(a|b) ∈ Reals
]
$Aborted
However, I wasn't patient enough to let the above finish. An alternate method (e.g., as mentioned here) is to substitute real constants for the parameters, and then substitute back. For example:
f[a_, b_] = Integrate[
integrand /. {a->EulerGamma, b->Khinchin},
{θ, 0, π},
{ϕ, 0, 2 π}
] /. {EulerGamma->a, Khinchin->b}; //AbsoluteTiming
f[a, b] //TeXForm
{17.7305, Null}
$\frac{4 \pi b \left(\frac{a \sqrt{b^2-a^2} \left(E\left(\frac{1-b^2}{a^2-b^2}\right)-E\left(\sin ^{-1}(a)|\frac{1-b^2}{a^2-b^2}\right)\right)}{a^2-1}+b\right)}{b^2-a^2}$
When using this approach it is best to compare the numerical answer with the proposed answer:
f[1/4, 1/3] //N
f[11/4, 2/3] //N
6.58407 + 0. I
1.16785 + 0. I
Versus:
g = With[
{i = integrand /. {a->#1, b->#2}},
Function[NIntegrate[i, {θ, 0, π}, {ϕ, 0, 2 π}]]
];
g[1/4, 1/3]
g[11/4, 2/3]
6.58407
1.16785
NIntegrate
instead of integrating and then finding the numeric value. $\endgroup$