The sum of the value of $ e ^ x $ up to the nth term of the Taylor expansion
e^{x} = 1+\frac{x}{1!} +\frac{x^{2}}{2!}+ .... \frac{x^{n}}{n!}
Calculated by.
from math import pi,e, log, factorial
import matplotlib.pyplot as plt
"""
e^Taylor expansion of x
Example: x=Calculate e as 1
"""
def calc_e(n,x):
dum=0.0
for nn in range(n+1): # x^n/n!Calculation
dum+=x**nn/factorial(nn)
return dum
#main
sol=[]
for j in range(8): # n=Calculation up to 7th order
sol.append(calc_e(j,1)) #n=1 ~Store e in a list named sol when it is truncated at an order up to 8.
# for plot
plt.plot(sol)
plt.xlabel('x',fontsize=24)
plt.show()
The exact value is e = 2.718281828459045 ... The error is within 1% at n = 4.
n e 0 1.0 1 2.0 2 2.5 3 2.6666666666666665 4 2.708333333333333 5 2.7166666666666663 6 2.7180555555555554 7 2.7182539682539684