Skip to content Skip to sidebar Skip to footer

Approximation Of E^x Using Maclaurin Series In Python

I'm trying to approximate e^x using the Maclaurin series in a function called my_exp(x), I believe everything I've done so far is right but I'm getting incorrect approximations for

Solution 1:

In order to accumulate the terms of the series, you need to replace the assignment to exp with a line such as:

exp = exp + ((x**i)/math.factorial(i))

Solution 2:

Your problem is that the e^x series is an infinite series, and so it makes no sense to only sum the first x terms of the series.

def myexp(x):
  e=0for i in range(0,100): #Sum the first 100 terms of the series
    e=e+(x**i)/math.factorial(i)
  return e

You can also define the precision of your result and get a better solution.

def myexp(x):
    e=0
    pres=0.0001
    s=1
    i=1while s>pres:
        e=e+s
        s=(x**i)/math.factorial(i)
        i=i+1return e

Post a Comment for "Approximation Of E^x Using Maclaurin Series In Python"