CoCalc Public Files10 - Sequences Assignment / Sequences Notes.sagewsOpen with one click!
Author: Aaron Tresham
Views : 38
License: Other -- explicitly state in your code
Compute Environment: Ubuntu 20.04 (Default)
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


  • Intro to Sage


A sequence is simply an ordered list of objects. We will consider countably infinite lists of numbers. Such a sequence may be considered as a function with domain the set of positive integers and codomain the real numbers.


The elements (or terms) of a sequence are usually denoted using subscripts rather than function notation: an=f(n)a_n=f(n).

The sequence is then denoted {an}\{a_n\}, or more explicitly {an}n=1\displaystyle\{a_n\}_{n=1}^{\infty}. The integer nn is called the index of ana_n.

Example 1

Consider the sequence 12, 14, 16, 18, 12,\ 14,\ 16,\ 18,\ \ldots

This may be written in various ways:

  • an=10+2na_n=10+2n for n1n\ge 1

  • {10+2n}\{10+2n\}

  • {10+2n}n=1\{10+2n\}_{n=1}^{\infty}.

Of course, there is really no reason to start with n=1n=1 all the time.

  • an=2na_n=2n for n6n\ge 6

  • {2n}n=6\{2n\}_{n=6}^{\infty}

These give the same sequence.

Infinite sequences may be defined in various ways.

  • A list of numbers with some discernible pattern.

  • An explicit forumla for the nthn^{th} term.

  • A recursive formula that gives one term in terms of one or more previous terms.

Example 2

List of numbers:

2, 3, 5, 7, 11, 13, 2,\ 3,\ 5,\ 7,\ 11,\ 13,\ \ldots (prime numbers)

Example 3

Explicit formula:

an=3n5a_n=3n-5 (unless otherwise stated, we'll assume nn starts at 1)

Example 4

Recursive definition:

an=an1+an2a_n=a_{n-1}+a_{n-2} for n3n\ge3, a1=1a_1=1, a2=1a_2=1 (Fibonacci Sequence)

Graphing a Sequence

To graph a sequence, you put the index variable on the x-axis and the sequence values on the y-axis.

One way to graph a sequence in Sage is to use the "point" command with an imbedded for-loop.

Example 5

Graph the first 50 terms of an=3n5a_n=3n-5.

(Be very careful with the brackets and parentheses.)

point([(n,3*n-5) for n in [1..50]])

Example 6

Graph the first 50 terms of the sequence an=(1+1n)n\displaystyle a_n=\left(1+\frac{1}{n}\right)^n.

I'll set up the formulas so you can cut and paste for your assignment.
%var n a(n)=(1+1/n)^n #sequence definition n_start=1 #starting index n_end=50 #ending index point([(n,a(n)) for n in [n_start..n_end]])

Example 7

Graph the first 10 terms of the Fibonacci Sequence: an=an1+an2a_n=a_{n-1}+a_{n-2} for n3n\ge 3, a1=1a_1=1, a2=1a_2=1.

There are different ways you might deal with this recursive definition in Sage. I will use a list. One issue is that a list in Sage always begins with index 0, while our sequence begins at 1. I'm going to get around this by sticking an extra 0 in the zero position.

Writing a=[0,1,1] makes a list of three numbers.

The first item in the list has index 0. You can access this by typing a[0]. Similarly, a[2] is the element in the list with index 2 (the third element in the list).

a=[0, 1, 1] #add an extra 0 at the beginning, and define the first two terms to be 1 for n in [3..10]: #note: the next line needs to be indented a+=[a[n-1]+a[n-2]] #add the next term to the list (notice the square brackets around the thing you're adding) point([(n,a[n]) for n in [1..10]]) #once the list is done, plot the points

Example 8

Graph the first 20 terms of the sequence an=an12+2an1\displaystyle a_n=\frac{a_{n-1}}{2}+\frac{2}{a_{n-1}} for n2n\ge2, a1=15a_1=15.

a=[0, 15] #start with an extra 0, then the first term is 15 for n in [2..20]: #the recursive defintion starts with n=2; we stop at n=20 a+=[a[n-1]/2+2/a[n-1]] #type the recursive definition point([(n,a[n]) for n in [1..20]]) #plot the points

Limit of a Sequence

If the sequence approaches some fixed number as nn\to\infty, then we call this the limit of the sequence. We write L=limnan\displaystyle L=\lim_{n\to\infty}a_n.

Here is the formal definition:

If for every ϵ>0\epsilon>0 there exists N>0N>0 such that anL<ϵ|a_n-L|<\epsilon whenever n>Nn>N, then L=limnan\displaystyle L=\lim_{n\to\infty}a_n.

In other words, you can get the terms of the sequence arbitrarily close to LL by making nn big enough.

Example 9

Let's explore the definition of a limit graphically, using limn(1+1n)n=e2.71828\displaystyle\lim_{n\to\infty}\left(1+\frac{1}{n}\right)^n=e\approx 2.71828.

In the interactive box below, LL represents the potential limit, and ϵ\epsilon is the ϵ\epsilon from the definition. If LL really is the limit, then eventually the sequence will stay in the band between LϵL-\epsilon and L+ϵL+\epsilon.

Interact: please open in CoCalc

Is the sequence eventually inside the red band?

Now make epsilon smaller (you may need to increase the starting and ending indices).

Computing Limits with Sage

If we have an explicit formula, we can often compute the limit using Sage.

#Since we used the letter n in our for-loops above, we need to declare it as a variable now. %var n limit((1+1/n)^n,n=+Infinity)

The limit command treats the sequence as a function on the real numbers, rather than just on the whole numbers. That means the limit command can give us the wrong answer.

Example 10

Consider limnsin(πn)\displaystyle\lim_{n\to\infty}\sin(\pi n)

%var n limit(sin(pi*n),n=+Infinity)

Here Sage gives the answer "ind," which means "indefinite but bounded." If we look at a graph of the function sin(πx)\sin(\pi x), we can see why the limit does not exist: the function oscillates between 11 and 1-1 forever.


However, the limit of the sequence does exist. When nn is a whole number, sin(πn)=0\sin(\pi n)=0 for all nn, so the limit of the sequence is 00.

If we have a recursive definition, then our best bet is to look at the graph to see if it looks like the limit exists. Then calculate many, many values of the sequence and see where we end up.

Example 11

If {an}\{a_n\} is the Fibonacci Sequence, then consider the sequence bn=an+1an\displaystyle b_n=\frac{a_{n+1}}{a_n}.

First, let's look at a graph.

a=[0, 1, 1] #First, define the Fibonacci Sequence for n in [3..21]: a+=[a[n-1]+a[n-2]] b=[0] for n in [1..20]: #Now define the new sequence b+=[a[n+1]/a[n]] point([(n,b[n]) for n in [1..20]])

It looks like the sequence is approaching a limit, somewhere around 1.6.

Let's compute values of the sequence and approximate what the limit is.

terms=20 #how many terms we want a=[0, 1, 1] #First, define the Fibonacci Sequence for n in [3..terms+1]: a+=[a[n-1]+a[n-2]] b=[0] for n in [1..terms]: #Now define the new sequence b+=[a[n+1]/a[n]] print "The limit is approximately",N(b[terms],digits=30) #Get a decimal approximation for the last term computed.
The limit is approximately 1.61803399852180339985218033999

Try increasing "terms" and see what happens.

The actual limit of this sequence is the "Golden Ratio," denoted by the Greek letter ϕ\phi. The actual value is ϕ=1+52\displaystyle \phi=\frac{1+\sqrt{5}}{2}.


Example 12

Estimate the limit of the sequence an=an1a_n=\sqrt{a_{n-1}} for n2n\ge 2, a1=5a_1=5.

Solution: First, we'll graph the first 20 terms to see if the limit exists.

a=[0,5] for n in [2..20]: a+=[sqrt(a[n-1])] point([(n,a[n]) for n in [1..20]])

It looks like the limit exists. Graphically, we estimate that the limit is 1.

Let's confirm this numerically by calculating 50 terms.

a=[0,5] for n in [2..50]: a+=[sqrt(a[n-1])] N(a[50])

It looks like the limit is indeed 1.