# Why two the same integrals give different values?

Why two the same integrals give different values?

Psi[r_, n_] := (-1)^n *Exp[-1/2 *r^2] *Sqrt[2 *n!/Gamma[n + 3/2]]*
LaguerreL[n, 1/2, r^2];
KA[n1_, n2_] :=
0.0032433*Integrate[Psi[r, n2]*r^2*Psi[r, n1]*r^2, {r, 0, Infinity}]
KA[87, 12]

Out[1356]= 0.

Psi[r_, n_] := (-1)^n *Exp[-1/2 *r^2] *Sqrt[2 *n!/Gamma[n + 3/2]]*
LaguerreL[n, 1/2, r^2];
KA[n1_, n2_] :=
Integrate[Psi[r, n2]*0.0032433*r^2*Psi[r, n1]*r^2, {r, 0, Infinity}]
KA[87, 12]

Out[1352]= 6.14098*10^21
$$$$

• What happens if you use the exact rational 32433/10000000? Commented Mar 1, 2023 at 5:59
• @Ghoster, thanks! Commented Mar 1, 2023 at 6:08
• The first integral is evaluated symbolically. In the second integral, there are floating point numbers in the integrand. Then Mathematica switches silently to NIntegrate, which uses a numerical approximation. Not sure why it fails so drastically. It is very oscillatory, but even on intervals far to the right I get absurdly large integrals. You might want to consider to send this example to Wolfram Support. Commented Mar 1, 2023 at 6:08
• For such cases I'd highly recommend using FindSequenceFunction to discover exact expressions. In your case, ka[n_, n_] = 2 n + 3/2; ka[n1_, n2_] /; Abs[n1 - n2] == 1 = Sqrt[(n1 + n2 + 1) (n1 + n2 + 2)]/2; ka[_, _] = 0; (I've left out the numerical prefactor of $0.0032433$). I know this wasn't your question; but very generally, finding such regularities is more useful than actually integrating. Commented Mar 1, 2023 at 9:22

Evaluating high-order polynomials is numerically unstable.

Example: exact evaluation followed by numericalization is stable,

Psi[7, 87] // N
(*    -0.0271578    *)


Calling LaguerreL with numerical parameters is also stable,

Psi[7., 87]
(*    -0.0271578    *)


Expanding the Laguerre polynomial and inserting numbers, however, is unstable:

Psi[r, 87] /. r -> 7.
(*    5.64566*10^18    *)


The reason is that LaguerreL[87, 1/2, r^2] is a polynomial of order 174 in r, with (for $$r=7$$) contributions of order $$10^{45}$$ cancelling each other out to give a result of order $$10^9$$. This level of numerical cancellation is too much for 64-bit double-precision numbers to handle.

Illustrating this problem with a some graphics:

Plot[Psi[r, 87], {r, 0, 10}, PlotRange -> All]


Plot[Psi[r, 87] // Evaluate, {r, 0, 10}, PlotRange -> All]


Plot[Psi[r, 87] // Evaluate, {r, 0, 10}, PlotRange -> All,
WorkingPrecision -> 100]


• thank you very much for these useful comments! Commented Mar 3, 2023 at 14:56
• You’re welcome. If you think that this (or another) answer is good enough, please consider using the check mark button ✓` to guide future visitors of this question. Commented Mar 4, 2023 at 10:40