Home > Software design >  Calculating infinite sum using python
Calculating infinite sum using python

Time:06-30

enter image description here

I want to evaluate the following sum using python. I have tried using for loop but getting value error. Is there any way to approximate the sum? I dont want to put any random values like 1000 in place of infinity. Rather approximation using limits can work. Like for example we can approximate the sum(a^x/x!),when x runs from 0 to infinity, by e. Can similar thing be done here? Here a and b are greater than zero; any fixed value like 3, 4 etc. Kindly help.

CodePudding user response:

The inner summation is a truncated series for exp(a). Try writing the inner summation as exp(a) - (tail of series) and expanding. I think you'll see some stuff which is a function of y, and gets smaller as y gets larger. So the trick is to find a large enough y that you can approximate the inner summation accurately. What you want is to be able to do a finite summation up to y = (suitable number), and then approximate the rest.

Note that how this works in practice depends pretty strongly on a and b -- the smaller they are, the better this works. Working out error bounds as a function of a and b would be great, although possibly not easy. Good luck and have fun.

CodePudding user response:

We can code this in Python as follows

from math import factorial

a = int(input("Enter a : "))
b = int(input("Enter b : "))
x = int(input("Enter x : "))
y = int(input("Enter y : "))

#N in place of Infinity
#Since y varies from 1 to N, and we are calculation N!, avoid large N
N = int(input("Enter N in place of infinity : "))

outerSum = 0

for y in range(1,N):
    innerSum = 0
    for x in range(0, y):
        innerSum  = round((a**x)/factorial(x),2)
    outerSum  = round((innerSum*(b**y))/factorial(y),2)

print(outerSum)

We can see that Summation in Mathematics is closely related to loops in Programming!

  • Related