Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Taylor Series
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sympy as sy | ||
import numpy as np | ||
from sympy.functions import sin,cos | ||
import matplotlib.pyplot as plt | ||
|
||
plt.style.use("ggplot") | ||
|
||
# Define the variable and the function to approximate | ||
x = sy.Symbol('x') | ||
f = sin(x) | ||
|
||
# Factorial function | ||
def factorial(n): | ||
if n <= 0: | ||
return 1 | ||
else: | ||
return n*factorial(n-1) | ||
|
||
# Taylor approximation at x0 of the function 'function' | ||
def taylor(function,x0,n): | ||
i = 0 | ||
p = 0 | ||
while i <= n: | ||
p = p + (function.diff(x,i).subs(x,x0))/(factorial(i))*(x-x0)**i | ||
i += 1 | ||
return p | ||
def plot(): | ||
x_lims = [-5,5] | ||
x1 = np.linspace(x_lims[0],x_lims[1],800) | ||
y1 = [] | ||
# Approximate up until 10 starting from 1 and using steps of 2 | ||
for j in range(1,10,2): | ||
func = taylor(f,0,j) | ||
print('Taylor expansion at n='+str(j),func) | ||
for k in x1: | ||
y1.append(func.subs(x,k)) | ||
plt.plot(x1,y1,label='order '+str(j)) | ||
y1 = [] | ||
# Plot the function to approximate (sine, in this case) | ||
plt.plot(x1,np.sin(x1),label='sin of x') | ||
plt.xlim(x_lims) | ||
plt.ylim([-5,5]) | ||
plt.xlabel('x') | ||
plt.ylabel('y') | ||
plt.legend() | ||
plt.grid(True) | ||
plt.title('Taylor series approximation') | ||
plt.show() | ||
|
||
plot() |