1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ If you know what $a$ and $b$ are beforehand, use NIntegrate instead of integrating and then finding the numeric value. $\endgroup$
    – eyorble
    Commented Feb 12, 2018 at 20:02

1 Answer 1

3
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Thank you! A question. Why do you use the Euler and Kinchin constants? Could it be any other constant? $\endgroup$ Commented Feb 13, 2018 at 13:23
  • $\begingroup$ @user3321 I use constants that I hope don't appear in the final answer, since my final replacement rule eliminates those constants from the answer. If one of those constants are supposed to be in the answer, then the result would be wrong. For example, if I had used $\pi$, then my final answer would not have included $\pi$, and it would have been wrong. $\endgroup$
    – Carl Woll
    Commented Feb 13, 2018 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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