CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
calculuslab

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: calculuslab/Calculus_Lab
Path: blob/main/142-Labs/Lab 07 - Sequences and Series.ipynb
Views: 491
Kernel: SageMath 9.2

Lab 07 - Sequences and Series

Overview

In this lab, we will use SageMath to determine the convergence or divergence of a sequence of numbers and of infinite series.

Important SageMath Commands Introduced in this Lab

ParseError: KaTeX parse error: Undefined control sequence: \hfill at position 32: …|l|l|} \hline \̲h̲f̲i̲l̲l̲ ̲\textbf{Command…
Sections 10.1 and 10.2

Example 1

Consider the sequence {cos(nπ)arctan(n)}n=1\{\cos(n\pi)\arctan(n)\}_{n=1}^\infty. We start by determining the first 10 terms of this sequence. We can do this in SageMath by letting an=cos(nπ)arctan(n)a_n = \cos(n\pi)\arctan(n) and then using a for\textbf{for} loop and the range\textbf{range} command.

n=var('n') def a(n): return cos(n * pi) * arctan(n) for i in range(1,11): ## This will iterate i through the integers 1 through 10 print(a(i))

We can get a better idea of what these numbers are by using the round\textbf{round} command.

for i in range(1,11): print(round(a(i),5))

Note that the terms of the sequence do not appear to approach a specific number. We can better tell what is happening by plotting the first 100 or so terms of the sequence. We can plot a point in SageMath by using the point\textbf{point} command along with the show\textbf{show} command. To plot multiple points on the same plot, we will store the points in a list and then show the list. SageMath does not allow us to plug the list directly into the show\textbf{show} command. Instead, we must input the sum of the elements in the list.

points = [] ## Creates an empty list to hold our points for i in range(1,101): p = point((i,a(i)), color = "red") points.append(p) show(sum(points)) ## Plugging the sum of the points into the show command

From the graph, we see that the odd terms are approaching a specific value, namely π2-\frac{\pi}{2}, and the even terms are approaching a specfic value, namely π2.\frac{\pi}{2}. However, since these values are different, the sequence diverges.

We can check our answer in SageMath.

limit(a(n), n=infinity)

The output ind\textit{ind} for a limit means that the answer is indefinite but the terms are bounded.

Example 2

Consider the sequence {k=1n1k2}n=1\left\{ \displaystyle \sum_{k=1}^n \dfrac{1}{k^2} \right\}_{n=1}^\infty. Again, let us find the first 10 terms of this sequence. In order to define ana_n, we need to recall how to define a summation in SageMath. One way to do this is to place all of the summands into a list and then use the sum\textbf{sum} command. Let's practice this with n=3n = 3. We know that when n=3n=3, we should get k=131k2=1+14+19=4936.\sum_{k=1}^3 \dfrac{1}{k^2} = 1 + \dfrac{1}{4} + \dfrac{1}{9} = \dfrac{49}{36}.

summands = [] for k in range(1,4): ## Remember that our second input in range needs to be one higher than what we actually want summands.append(1/k^2) sum(summands)

Now that we know how to define the summation in SageMath, we can create the sequence ana_n.

def a(n): summands = [] for k in range(1, n+1): summands.append(1/k^2) return sum(summands) a(3)

Now, let's use our definition of ana_n to find the first 10 terms of the sequence.

for i in range(1,11): print(a(i))

To better see if these number are approaching a specific number, we can round.

It looks like the terms are getting closer together. Let's plot the first 100 terms and see.

According to the graph, these terms are converging to a specific number. In fact, they are converging to π26\dfrac{\pi^2}{6}.

line = plot(pi^2/6, xmin = 0, xmax = 100) show(sum(points) + line) ## You may have to change the name of the list points to whatever you called it above

SageMath has the ability to tell us that this is indeed what the sequence converges to. Unfortunately, it will not work with our current definition of ana_n.

limit(a(n),n=infinity)

To fix this, we can use the sum(expr, var, a, b)\textbf{sum(expr, var, a, b)} command to define our summation instead of a for\textbf{for} loop. Let's first test this command with n=3n = 3 and make sure that we get 4936\frac{49}{36} as expected.

k = var('k') sum(1/k^2, k, 1, 3)

Therefore, we will use the sum\textbf{sum} command and let b=b = \infty.

show(sum(1/k^2, k, 1, infinity))

Example 3

A typical format for a recursively defined sequence is an+1=f(an)a_{n+1} = f(a_n) for n=1,2,3,n = 1,2,3, \dots with a1a_1 given explicitly. Under the assumptions that {an}\{a_n\} converges to LL and ff is a continuous function, we have that L=limnan+1=limnf(an)=f(limnan)=f(L).L = \lim_{n \rightarrow \infty} a_{n+1} = \lim_{n\rightarrow \infty} f(a_n) = f\left(\lim_{n\rightarrow \infty} a_n\right) = f(L). Therefore, LL must be a solution to f(L)=L.f(L) = L. This equation is often difficult to solve by hand, but we can use SageMath to find a solution.

Consider the recursive sequence {an}\{a_n\} defined by a1=2a_1 = \sqrt{2} and an+1=2+ana_{n+1} = \sqrt{2 + a_n}. Let's determine the first 10 terms of this sequence.

a = [sqrt(2)] ## Defining the first term of our sequence for i in range(1,10): ## Using a for loop to find a_2 through a_10 newTerm = sqrt(2 + a[i-1]) ## We do a[i-1] since the index of a list starts at 0 and not 1 print(newTerm) ## Prints the term a_(i+1) a.append(newTerm) ## Adds the term to the list so that it can be used to define the next term in the sequence

We can round our outputs by either using the round\textbf{round} command or by starting with 2.0\sqrt{2.0} instead of 2\sqrt{2}.

a = [sqrt(2.0)] for i in range(1,10): newTerm = sqrt(2 + a[i-1]) print(newTerm) a.append(newTerm)

It appears that the terms are converging to 22. First, let's check this by plotting the first 100 terms and the line y=2y = 2.

a = [sqrt(2.0)] for i in range(1,100): newTerm = sqrt(2 + a[i-1]) a.append(newTerm) points = [] for i in range(1,101): p = point((i,a[i-1]), color = "red") ## Again we have to use a[i-1] because of the indexing of a list starting at 0 points.append(p) line = plot(2, xmin=0, xmax = 100) show(sum(points) + line)

A second way to verify that the sequence converges to 22 is to solve the equation f(L)=Lf(L) = L; that is, we need to solve the equation 2+L=L.\sqrt{2 + L} = L. We can solve this in SageMath by using the solve\textbf{solve} command.

L = var('L') solve(sqrt(2 + L) == L, L)

Unfortunately, SageMath offers no help with the equation in its current state. However, we can simplify the equation a bit by squaring both sides and obtaining 2+L=L22 + L = L^2. Now, we can ask SageMath to solve this equation.

solve (2 + L == L^2, L)

SageMath returns the two answers L=2L = 2 and L=1L = -1. We know that an+1=2+ana_{n+1} = \sqrt{2 + a_n} will always be positive. Thus, the solution L=1L = -1 is not possible. Therefore, we find that the sequence converges to L=2L = 2.

Example 4

For each of the following sequences generate the first 10 terms, plot the first 100 terms, and determine whether the sequence converges or diverges. If it converges, determine the exact value it converges to.

  1. {n2+nn}n=1\left\{\sqrt{n^2 + n} - n\right\}_{n=1}^\infty

  2. {nsin(πn)}n=1\left\{n\sin\left(\dfrac{\pi}{n}\right)\right\}_{n=1}^\infty

  3. n=11n4\displaystyle\sum_{n=1}^\infty \dfrac{1}{n^4}

  4. n=010nn!\displaystyle\sum_{n=0}^\infty\dfrac{10^n}{n!} You need to use factorial(n)\textbf{factorial(n)} for n!

Example 5

Consider the recursive sequence an+1=12(an+2an)a_{n+1} = \dfrac{1}{2}\left(a_n + \dfrac{2}{a_n}\right), where ai=1a_i = 1. Generate the first 10 terms, plot the first 100 terms, and verify that the sequence converges to 2\sqrt{2}.