I am using Python's sympy to create a Taylor series for sin x
Following is the Taylor series for sin(x). Reference here
Then following is how I should write the python code to create the Taylor series expression
Code:
import sympy as sym
import math
x = sym.symbols('x')
# Technique 1: Compute the 5 first terms in a Taylor series for sin(x) around x = 0.
T_sin5 = 1 - (x**3/math.factorial(3)) + (x**5/math.factorial(5)) - (x**7/math.factorial(7)) + (x**9/math.factorial(9))
print(T_sin5)
# Technique 2: Compute the 5 first terms in a Taylor series for sin(x) around x = 0 using sympy
T_sin5_2 = sym.sin(x).series(x, 0, 5).removeO()
print(T_sin5_2)
Question:
As you can see above, I have made T_sin5
using hand written way whereas T_sin5_2
is made using sympy
. So, my question is: Is Technique 1 same as Technique 2? Are T_sin5
and T_sin5_2
the same.
print(T_sin5)
and print(T_sin5_2)
both seem to print half of the series like below:
-x**3/6 + x
I was expecting both the prints to priint out the Talor series for first 5 terms which will be like below.
1 - x3/6 + x5/120 - x7/5040 + x9/362880
Why doesnt print(T_sin5_2)
print out 5 terms of the Taylor series like above?
Python version:
My python version is 3.8.6
The n
parameter of series
describes
The number of terms up to which the series is to be expanded.
This includes terms that are zero (as it happens for every second term for sin(x)
). I.e. n-1
is the greatest power in the series, so for series(x, 0, 5)
this happens to be 4
and for this power the coefficient is zero.
For your custom version though it should print the full series (this is the output I get):
>>> print(T_sin5)
x**9/362880 - x**7/5040 + x**5/120 - x**3/6 + 1