Scroll to Top

Virtual Math Learning Center

Virtual Math Learning Center Texas A&M University Virtual Math Learning Center

Plotting a Function and Partial Sums of its Power Series in Python

Author: David Manuel

In this video, we plot the partial sums of a power series along with the function which is represented by that power series. We use Python's list comprehension tool to easily generate the third, fifth, and tenth partials sums of the power series, which are Taylor Polynomials.

Transcript 29

Problem: Plot \(f(x)=\arctan(x)\) and the 3rd, 5th, and 10th partial sum of its powers series
\[\sum_{n=0}^\infty \frac{(-1)^n x^{2n+1}}{ 2n+1}\]

Python Code: 
Plot f(x)=arctan(x) and the 3rd, 5th, and 10th partial sum of its powers series

sum from 0 to infinity (-1)^n x^(2n+1) / (2n+1)

from sympy import *

n=symbols('n',positive=True,integer=True)
x=symbols('x')
a=(-1)**n*x**(2*n+1)/(2*n+1)
f=atan(x)
a1to3=[a.subs(n,i) for i in range(4)]
s3=sum(a1to3)
print(s3)
a1to5=[a.subs(n,i) for i in range(6)]
s5=sum(a1to5)
a1to10=[a.subs(n,i) for i in range(11)]
s10=sum(a1to10)

matplotlib notebook

plotf=plot((f,(x,-1.5,1.5)),(s3,(x,-1.5,1.5)),(s5,(x,-1.5,1.5)),(s10,(x,-1.5,1.5)),ylim=[-1,1])
plotf[1].line_color='r'
plotf[2].line_color='y'
plotf[3].line_color='k'
plotf.show()

Open Python Notebook File

See more videos from this section