import numpy as np
import matplotlib.pyplot as plt
Maclaurin Series
The Maclaurin series is a special case of a Taylor series expansion, centered at the origin (x = 0).
\[ e^x = 1 + x + \frac{{x^2}}{2!} + \frac{{x^3}}{3!} + \ldots \]
Import Libraries
Factorial
\[ x! = x \times (x-1) \times (x-2) \times \ldots \times 3 \times 2 \times 1 \]
def factorial(x):
if x<=1:
return 1
else:
=1
productfor i in range(1,x+1):
*=i
productreturn product
Let x = 0.5
=0.5 x
Exact Ans
=np.exp(x) exact
Estimated Answers
def estimate(terms,x):
sum = 0
for i in range(terms+1):
sum += i*(x**(i-1))/factorial(i)
return sum
Errors for each term ->
= []
terms = []
errors
print("Terms\tExact\t\tEstimate\tError (%)")
print("-------------------------------------------------------")
for i in range(1,11):
=estimate(i,x)
estimate_ans=(exact-estimate_ans)/exact*100
errorprint(f"{i}\t{exact:.9f}\t{estimate_ans:.9f}\t{error:.9f}%")
terms.append(i)
errors.append(error)
=(5, 3))
plt.figure(figsize='o')
plt.plot(terms, errors, marker"Number of Terms")
plt.xlabel("Error (%)")
plt.ylabel("Error vs. Number of Terms")
plt.title(True)
plt.grid( plt.show()
Terms Exact Estimate Error (%)
-------------------------------------------------------
1 1.648721271 1.000000000 39.346934029%
2 1.648721271 1.500000000 9.020401043%
3 1.648721271 1.625000000 1.438767797%
4 1.648721271 1.645833333 0.175162256%
5 1.648721271 1.648437500 0.017211563%
6 1.648721271 1.648697917 0.001416494%
7 1.648721271 1.648719618 0.000100238%
8 1.648721271 1.648721168 0.000006220%
9 1.648721271 1.648721265 0.000000344%
10 1.648721271 1.648721270 0.000000017%
Let x = 5
=5 x
Exact Ans
=np.exp(x) exact
Estimated Answers
def estimate(terms,x):
sum = 0
for i in range(terms+1):
sum += i*(x**(i-1))/factorial(i)
return sum
Errors for each term ->
= []
terms = []
errors
print("Terms\tExact\t\tEstimate\tError (%)")
print("-------------------------------------------------------")
for i in range(1,11):
=estimate(i,x)
estimate_ans=(exact-estimate_ans)/exact*100
errorprint(f"{i}\t{exact:.9f}\t{estimate_ans:.9f}\t{error:.9f}%")
terms.append(i)
errors.append(error)
=(5, 3))
plt.figure(figsize='o')
plt.plot(terms, errors, marker"Number of Terms")
plt.xlabel("Error (%)")
plt.ylabel("Error vs. Number of Terms")
plt.title(True)
plt.grid( plt.show()
Terms Exact Estimate Error (%)
-------------------------------------------------------
1 148.413159103 1.000000000 99.326205300%
2 148.413159103 6.000000000 95.957231801%
3 148.413159103 18.500000000 87.534798052%
4 148.413159103 39.333333333 73.497408470%
5 148.413159103 65.375000000 55.950671493%
6 148.413159103 91.416666667 38.403934517%
7 148.413159103 113.118055556 23.781653703%
8 148.413159103 128.619047619 13.337167407%
9 148.413159103 138.307167659 6.809363472%
10 148.413159103 143.689456570 3.182805731%
Plotting the graph
= np.linspace(-5, 5, 400)
x_vals = np.exp(x_vals)
exact_vals
for terms in range(2,8):
plt.figure()= [estimate(terms, x) for x in x_vals]
approx_y_values =f'Taylor Series (Terms = {terms})')
plt.plot(x_vals, approx_y_values, label='Actual Function')
plt.plot(x_vals, exact_vals, labelf"Maclaurin Series (Terms = {terms}) vs. Actual Function")
plt.title("x")
plt.xlabel("f(x)")
plt.ylabel(
plt.legend()True)
plt.grid( plt.show()