import numpy as np
import matplotlib.pyplot as plt
Taylor Series
A Taylor series is an infinite sum that approximates a function using its derivatives at a point, helping express complex functions as simpler polynomials for analysis and calculation.
\[ f(x) = f(a) + f'(a)(x - a) + \frac{f''(a)(x - a)^2}{2!} + \frac{f'''(a)(x - a)^3}{3!} + \cdots \]
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
Defining Function
def function(x):
return 1 + x + x**2 + x**3 + x**4
Setting up the variables
=1 # Point where we like to approximate the value of fxn
x=0 # Value of x where we know about the fxn a
Find nth derivative of a fxn at point a
def derivative(f, x, n):
if n == 0:
return f(x)
else:
= 1e-2
h = (derivative(f, x + h, n - 1) - derivative(f, x, n - 1)) / h
df return df
Exact Ans
=function(x) exact
Estimated Answers
def estimate(terms, a, x):
sum = 0
for i in range(terms):
sum += derivative(function, a, i) * (x - a)**(i) / np.math.factorial(i)
return sum
= []
order = []
errors
print("Order\tExact\t\tEstimate\tError (%)")
print("-------------------------------------------------------")
for i in range(1, 11):
= estimate(i, a, x)
estimate_ans = np.abs(exact-estimate_ans)/exact*100
error print(f"{i-1}\t{exact:.9f}\t{estimate_ans:.9f}\t{error:.9f}%")
-1)
order.append(i
errors.append(error)
=(5, 3))
plt.figure(figsize='o')
plt.plot(order, errors, marker"Order")
plt.xlabel("Error (%)")
plt.ylabel("Error vs. Order")
plt.title(True)
plt.grid( plt.show()
Order Exact Estimate Error (%)
-------------------------------------------------------
0 5.000000000 1.000000000 80.000000000%
1 5.000000000 2.010101000 59.797980000%
2 5.000000000 3.040801000 39.183980000%
3 5.000000000 4.100801000 17.983980001%
4 5.000000000 5.100801002 2.016020044%
5 5.000000000 5.100800873 2.016017454%
6 5.000000000 5.100806732 2.016134644%
7 5.000000000 5.100599667 2.011993336%
8 5.000000000 5.106547290 2.130945803%
9 5.000000000 4.963363765 0.732724697%
Plotting the Exact and Approx Functions
= np.linspace(-5, 5, 400)
x_values = 0
a
for terms in range(2,6):
plt.figure()= [estimate(terms, a, x) for x in x_values]
approx_y_values =f'Taylor Series (Terms = {terms})')
plt.plot(x_values, approx_y_values, label='Actual Function')
plt.plot(x_values, function(x_values), labelf"Taylor Series (Terms = {terms}) vs. Actual Function")
plt.title("x")
plt.xlabel("f(x)")
plt.ylabel(
plt.legend()True)
plt.grid( plt.show()
Plotting the graphs for Sin(x)
def function(x):
return np.sin(x)
= np.linspace(-5, 5, 400)
x_values = 0
a
for terms in range(2,10,2):
plt.figure()= [estimate(terms, a, x) for x in x_values]
approx_y_values =f'Taylor Series (Terms = {terms})')
plt.plot(x_values, approx_y_values, label='Actual Function')
plt.plot(x_values, function(x_values), labelf"Taylor Series (Terms = {terms}) vs. Actual Function")
plt.title("x")
plt.xlabel("f(x)")
plt.ylabel(
plt.legend()True)
plt.grid( plt.show()