0% found this document useful (0 votes)
11 views7 pages

Ex-2 Taylor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Ex-2 Taylor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

EX NO 02

DATE TAYLOR SERIES

26/7/24

AIM:
To implement Python code to execute Taylor's Sales and to find error
approximation.

ALGORITHM:

1. TAYLOR SERIES 1/(1-x) = x^0 + x^1 + x^2 + x^3 +...+ x^n


 Get the inputs x as float and n as integer.
 Assign initial values of s, i as 0,0.
 Using while condition to do Taylor series calculation upto n values.
 Compute x power i and store in term.
 Add the term valve to s and increment s.
 Increment value i by 1 and do the looping until i=n.
 Print the Sum of series value.

2. E(X) e^x = x^0/0! + x^1/1! + x^2/2! + x^3/3! +...+ x^n/n!


 Define a function to find factorial of number.
 Using if loop calculate n* fact (n-1) upto n value.
 Get the inputs x as float and n as integer.
 import math function and matplotlib function.
 Assign initial valves of 5, i as 0,0
 Using while condition to do Taylor series calculation for ex upto n
valves.
 Compute xi and call factorial function and store in term.
 Add the team to s value and Increment s.
 Increment & value by and do the looping until i=n.
 Print the sum of series.
 Do the error calculation by subtracting s from actual exp(x).
 Print the error approximation value.
 Compute error approximation for different valves of n and plot it.

3. E(-X) e^-x = x^0/0! - x^1/1! + x^2/2! – x^3/3! +...+ x^n/n!


 Define a function to find factorial of number.
 Using if loop calculate n* fact (n-1) upto n value.
 Get the inputs x as float and n as integer.
 import math function and matplotlib function.
 Assign initial valves of 5, i as 0,0
 Using while condition to do Taylor series calculation for ex upto n
valves.
 Compute (-1)i (xi) /i! and call factorial function and store in term.
 Add the team to s value and Increment s.
 Increment & value by and do the looping until i=n.
 Print the sum of series.
 Do the error calculation by subtracting s from actual exp(-x).
 Print the error approximation value.
 Compute error approximation for different valves of n and plot it.

4. Sin(x) = x^1/1! - x^3/3! + x^5/5! - x^7/7! +...+ x^2i-1/2i-1!


 Define a function to find factorial of number.
 Using if loop calculate n* fact (n-1) upto n value.
 Get the inputs x as float and n as integer.
 import math function and matplotlib function.
 Assign initial valves of 5, i as 0,0
 Using while condition to do Taylor series calculation for ex upto n
valves.
 Compute (-1)i (x2i+1) /(2i+1)! and call factorial function and store in
term.
 Add the team to s value and Increment s.
 Increment & value by and do the looping until i=n.
 Print the sum of series.
 Do the error calculation by subtracting s from actual sin(x).
 Print the error approximation value.
 Compute error approximation for different valves of n and plot it.

5. Cos(x) = 1-x^2/2! + x^4/4! - x^6/6! + x^8/8! +...+ x^2i/2i!


 Define a function to find factorial of number.
 Using if loop calculate n* fact (n-1) upto n value.
 Get the inputs x as float and n as integer.
 import math function and matplotlib function.
 Assign initial valves of 5, i as 0,0
 Using while condition to do Taylor series calculation for ex upto n
valves.
 Compute (-1)i (x2i) /2i! and call factorial function and store in term.
 Add the team to s value and Increment s.
 Increment & value by and do the looping until i=n.
 Print the sum of series.
 Do the error calculation by subtracting s from actual cos(x).
 Print the error approximation value.
 Compute error approximation for different valves of n and plot it.

CODE:

#METHOD1 :1/(1-x)=x^0+x^1+x^2+x^3+...+x^n

x=float(input("Enter X:"))
n=int(input("Terms n:"))
import math
s,i=0,0
while i<=n:
Term=x**i
s+=Term
i+=1
print("The sum of the series:",s)

OUTPUT:
Enter X:0.5
Terms n:5
The sum of the series: 1.96875

#METHOD2: e^x=x^0/0!+x^1/11+x^2/2!+x^3/3!+...+x^n/n!

import math
import matplotlib.pyplot as plt

def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
x = float(input("Enter X: "))

n_values = range(0, 20)


errors = []

for n in n_values:
s=0
for i in range(n + 1):
Term = (x ** i) / fact(i)
s += Term
error = math.exp(x) - s
errors.append(error)

plt.figure(figsize=(8, 5))
plt.plot(n_values, errors, marker='o')
plt.title(f'Error Approximation for e^{x} Series Expansion')
plt.xlabel('Number of Terms (n)')
plt.ylabel('Error Approximation')
plt.show()

OUTPUT:
#METHOD3 e^-x=x^0/0!-x^1/1!+x^2/2!-x^3/3!+...+x^n/n!
import math
import matplotlib.pyplot as plt

def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
x = float(input("Enter X: "))

n_values = range(0, 20)


errors = []
for n in n_values:
s=0
for i in range(n + 1):
Term = ((-1)**i)*((x**i)/fact(i))
s += Term
error = math.exp(-x) - s
errors.append(error)

plt.figure(figsize=(8, 5))
plt.plot(n_values, errors, marker='o')
plt.title(f'Error Approximation for e^{-x} Series Expansion')
plt.xlabel('Number of Terms (n)')
plt.ylabel('Error Approximation')
plt.show()
OUTPUT:

#METHOD4 sinx=x^1/1!-x^3/3!+x^5/5!-x^7/7!+...+x^2i-1/2i-1!

import math
import matplotlib.pyplot as plt

def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
x = float(input("Enter X: "))

n_values = range(0, 20)


errors = []

for n in n_values:
s=0
for i in range(n + 1):
Term = ((-1)**i)*((x**((2*i)+1))/fact((2*i)+1))
s += Term
error = (math.sin(x))-s
errors.append(error)

plt.figure(figsize=(8, 5))
plt.plot(n_values, errors, marker='o')
plt.title(f'Error Approximation for sin{x} Series Expansion')
plt.xlabel('Number of Terms (n)')
plt.ylabel('Error Approximation')
plt.show()

OUTPUT:

#METHOD5 cosx=1-x^2/2!+x^4/4!-x^6/6!+x^8/8!+...+x^2i/2i!
import math
import matplotlib.pyplot as plt

def fact(n):
if n == 0:
return 1
return n * fact(n - 1)
x = float(input("Enter X: "))

n_values = range(0, 20)


errors = []

for n in n_values:
s=0
for i in range(n + 1):
Term = ((-1)**i)*((x**((2*i)))/fact((2*i)))
s += Term
error = (math.cos(x))-s
errors.append(error)

plt.figure(figsize=(8, 5))
plt.plot(n_values, errors, marker='o')
plt.title(f'Error Approximation for cos{x} Series Expansion')
plt.xlabel('Number of Terms (n)')
plt.ylabel('Error Approximation')
plt.show()

OUTPUT:

RESULT:
Taylor series of 1/(1-x) , exp(x), exp(-x), Sin(x) and Cos(x) has been executed
using python code and error approximations has been plotted.

You might also like