Skip to content
Permalink
238fa7537c
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
37 lines (32 sloc) 940 Bytes
import sympy as sy
import numpy as np
from sympy.functions import sin,cos
import matplotlib.pyplot as plt
from sympy import E
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'
# Since I'm still having difficulting getting this to work, I can just create individual functions for each equation (since I don't need the whole thing).
#Make sure you create a function estimating the upper bound error
def taylor(function,x0,n,a):
i = 0
p = 0
while i <= n:
p = p + (function.diff(x,i).subs(x,x0))/(factorial(i))*(x-x0)**i
i += 1
# u = (M / factorial(n + 1)) * (x - x0)**(n+1)
return p
e = E.evalf()
print(taylor(e**x,0.5,5,1))
print("Break")
print(taylor(e**x, 0.05,5,0.1))
print("Break")
print(taylor(e**x,0.005,5,0.01))