1
$\begingroup$

I need to calculate a coefficient as follows

coef[a_, j_] := 
 E^(-a^2/2)
   NSum[(a/Sqrt[2])^n 1/Sqrt[((n - j)!) (j!)], {n, j, Infinity}]

This coefficient is an infinite sum over n and depends on a real number a and an index j.

I choose a = 2, and j = 19, i.e. coef[2,19], I get the outcome

1.94412*10^-6

but when I calculate coef[2,20] (j=20) the program runs forever.

How can I resolve this problem?

$\endgroup$
3
  • 3
    $\begingroup$ Welcome to MSE! Change definition to coef[a_?NumericQ, j_?NumericQ] := E^(-a^2/2)*NSum[(a/Sqrt[2])^n 1/Sqrt[((n - j)!) (j!)], {n, j, Infinity}] and it will compute immediately. $\endgroup$
    – Alx
    Dec 18, 2019 at 12:00
  • $\begingroup$ @Alx: Thank you. So simple but definitely works. $\endgroup$ Dec 19, 2019 at 12:28
  • $\begingroup$ @Alx Please could you explain why it works? Or, to put in a different way the question I'm really interested in, "What problem prevented the equation being solved, and how does your solution overcome that problem?" $\endgroup$ Mar 30, 2023 at 10:20

0

Your Answer

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

Browse other questions tagged or ask your own question.