Scroll to Top

Virtual Math Learning Center

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

Generating and Plotting Sequences in Python

Author: David Manuel

In this video, we use Python to numerically, graphically, and analytically find the limit of a sequence. We will use Python's list comprehension tool to easily generate the first 20 terms and plot the first 50 terms. Lastly, we use Python's built-in limit command to find the exact limit of the sequence.

Transcript 25

Problem: Suppose that
\[\lim_{n\rightarrow\infty} a_n = n\sin\left(\frac{\pi}{n}\right)\]

  1. List the first 20 terms (numerical estimate).
  2. Plot the first 50 terms (graphical estimate).
  3. Find the exact limit.

Python Code:
Limit of a_n = n * sin(pi/n)
a) List the first 20 terms (numerical estimate)
b) Plot the first 50 terms (graphical estimate)
c) Find the exact limit

from sympy import *

n=symbols('n',positive=True,integer=True)
a=n*sin(pi/n)
a1to20=[a.subs(n,i).evalf() for i in range(1,21)]
print(a1to20)

matplotlib notebook

import matplotlib.pyplot as plt
n1to50=range(1,51)
a1to50=[a.subs(n,i) for i in n1to50]
plt.plot(n1to50,a1to50,'o')

L=limit(a,n,oo)
print('The limit of the sequence is',L)

Open Python Notebook File

See more videos from this section