4 min read

Sequences and Series in Python

Convergence of a Sequence

Definition. (Sequence Convergence) A sequence \((a_n)\) converges to a real number \(a\) if, for every positive number \(\epsilon\), there exists an \(N \in \mathbb N\) such that whenever \(n \geq N\) it follows that \(\left|a_n − a\right| \lt \epsilon\).

In the following, we will determine whether a given sequence converges to a certain limit, and then we’ll use Python to illustrate the sequence graphically. The method is to compute and plot the first \(20\) values of the given sequence, which can be done using the following Python libraries:

## import sympy
from sympy import limit_seq
from sympy.abc import n

1. Show that \(\lim\limits_{n\rightarrow\infty}\left(\frac{n+1}{n}\right) \equiv 1\).

We want to prove the inequality \(\left|a_n − a\right| \lt \epsilon\) for the sequence \(a_n \equiv \frac{n+1}{n}\ \) and \(a\equiv1\).

Proof. Let \(\epsilon \gt 0 \ \) be arbitrary. Choose \(N \in \mathbb N \ \) with \(N \gt \frac{1}{\epsilon}\). To verify that this choice of \(N\) is appropriate, let \(n \in \mathbb N\) satisfy \(n \geq N\). Then \(n \gt \frac{1}{\epsilon}\ \), which is the same as saying \(\frac{1}{n} \lt \epsilon\). Hence, this means

\[\begin{align} \left|\frac{n+1}{n}-1\right| \equiv \left|\frac{(n+1) - n}{n}\right| \equiv \frac{1}{n} \lt \epsilon .\end{align}\]

So by the definition of convergence, \(\lim\limits_{n\to \infty}{\frac{n+1}{n}} \equiv 1\).

Python Code.

## compute the sequence limit
a = (n + 1)/n
L = limit_seq(a, n)
print("The sequence converges to:", L)

## compute sequence terms
nvals = range(1, 21) 
a_20 = [a.subs({n:i}) for i in nvals]

## plot sequence
fig, ax = plt.subplots(figsize = (5, 4))
plt.plot(nvals, a_20, 'o') 
Observe that as $n$ tends to $\infty$, the sequence $a_n \equiv \frac{(n + 1)}{n}$ approaches $1$.

Figure 1: Observe that as \(n\) tends to \(\infty\), the sequence \(a_n \equiv \frac{(n + 1)}{n}\) approaches \(1\).


2. Verify that the following sequences converge to the proposed limit.

\(\;\) (a) \(\displaystyle \mathbf{\lim_{n \to \infty}{\frac{2n+1}{5n+4}} \equiv \frac{2}{5}}\)

We want to prove the inequality \(\left|a_n − a\right| \lt \epsilon\) for the sequence \(a_n \equiv \frac{2n+1}{5n+4}\ \) and \(a\equiv\frac{2}{5}\).

Proof. Let \(\epsilon \gt 0 \ \) be arbitrary. Choose \(N \in \mathbb N \ \) with \(N \gt \frac{3 - 20 \epsilon}{25 \epsilon}\). To verify that this choice of \(N\) is appropriate, let \(n \in \mathbb N\) satisfy \(n \geq N\). Then \(n \gt \frac{3 - 20 \epsilon}{25 \epsilon}\ \), which is the same as saying \(\frac{3}{5 \left(5 n + 4\right)} \lt \epsilon\). Hence, this means

\[\begin{align} \left|\frac{2n+1}{5n+4} - \frac{2}{5} \right| \equiv \frac{\left|5\left(2n+1\right)-2\left(5n+4\right)\right|}{5\left(5n+4\right)} \equiv \frac{3}{5\left(5n+4\right)}\ \lt \epsilon .\end{align}\]

So by the definition of convergence, \(\lim\limits_{n\to \infty}{\frac{2n+1}{5n+4}} \equiv \frac{2}{5}\).

Python Code.

## define sequence
a = (2*n + 1) / (5*n + 4)
L = limit(abs(a), n, oo) # alternating series test
print('The limit is', L)

## plot sequence
nvals = range(1, 81)
a_80 = [abs(a.subs({n:i})) for i in nvals]
fig, ax = plt.subplots(figsize = (6, 4))
plt.plot(nvals, a_80, linewidth = 1, color = '483d8b')
plt.plot(nvals, a_80, 'o', color = '8996d4', markeredgecolor = '483d8b')
Observe that as $n$ tends to $\infty$, the sequence $a_n \equiv \frac{2n+1}{5n+4}$ approaches $\frac{2}{5} \equiv 0.40$.

Figure 2: Observe that as \(n\) tends to \(\infty\), the sequence \(a_n \equiv \frac{2n+1}{5n+4}\) approaches \(\frac{2}{5} \equiv 0.40\).


\(\;\) (b) \(\displaystyle \mathbf{ \lim_{n \to \infty}{\frac{2n^2}{n^3+3}} \equiv 0}\)

We want to prove the inequality \(\left|a_n − a\right| \lt \epsilon\) for the sequence \(a_n \equiv \frac{2n^2}{n^3+3}\ \) and \(a\equiv0\).

Proof. Let \(\epsilon \gt 0 \ \) be arbitrary. Choose \(N \in \mathbb N \ \) with \(N \gt \frac{3}{\epsilon}\). To verify that this choice of \(N\) is appropriate, let \(n \in \mathbb N\) satisfy \(n \geq N\). Then, \(n \geq N\) implies \(n \gt \frac{3}{\epsilon}\ \), which is the same as saying \(\frac{3}{n} \lt \epsilon\). Hence, this means

\[\begin{align}\left|\frac{2n^2}{n^3+3} - 0 \right| \equiv \frac{2n^2}{n^3 + 3} \leq \frac{2}{n+\frac{3}{n^2}} \lt \frac{3}{n} \lt \epsilon .\end{align}\]

So by the definition of convergence, \(\lim\limits_{n\to \infty}{\frac{2n^2}{n^3 + 3}} \equiv 0\).

Python Code.

## define sequence
a = (2*n**2) / (n**3 + 3)
L = limit(abs(a), n, oo) # alternating series test
print('The limit is', L)

## plot sequence
nvals = range(1, 81)
a_80 = [abs(a.subs({n:i})) for i in nvals]
fig, ax = plt.subplots(figsize = (6, 4))
plt.plot(nvals, a_80, linewidth = 1, color = '483d8b')
plt.plot(nvals, a_80, 'o', color = '8996d4', markeredgecolor = '483d8b')
Observe that as $n$ tends to $\infty$, the sequence $a_n \equiv \frac{2n^2}{n^3+3}$ approaches $0$.

Figure 3: Observe that as \(n\) tends to \(\infty\), the sequence \(a_n \equiv \frac{2n^2}{n^3+3}\) approaches \(0\).


References

[1] Meurer, A., Smith, C. P., Paprocki, M., Čertík, O., Kirpichev, S. B., Rocklin, M., Kumar, A., Ivanov, S., Moore, J. K., Singh, S., Rathnayake, T., Vig, S., Granger, B. E., Muller, R. P., Bonazzi, F., Gupta, H., Vats, S., Johansson, F., Pedregosa, F., Curry, M. J., Terrel, A. R., Roučka, Š., Saboo, A., Fernando, I., Kulal, S., Cimrman, R. and Scopatz, A. (2017). SymPy: Symbolic computing in Python. PeerJ Computer Science 3 e103.

[2] Abbott, S. (2015). Understanding analysis. Springer, New York.