Maple Tutorials
Elmer K. Hayashi
Created Spring 2001
Working with Sequences


Working with Sequences

To generate the terms of a sequence that is defined by a simple defining formula, we use the command seq.

For example if a[n] = (2*n+1)/(3*n+4) then the first twenty terms of the sequence can be generated as follows:

> seq((2*n+1)/(3*n+4),n=1..20);

3/7, 1/2, 7/13, 9/16, 11/19, 13/22, 3/5, 17/28, 19/...

Note that the first parameter is the formula for the nth term of the sequence, and the second parameter is the range for n.

The above gives the exact value of each term, but if we wish to guess the limit of the sequence it is often helpful to look at

the decimal approximation for each term by using evalf.

> seq(evalf((2*n+1)/(3*n+4)),n=1..20);

.4285714286, .5000000000, .5384615385, .5625000000,...
.4285714286, .5000000000, .5384615385, .5625000000,...
.4285714286, .5000000000, .5384615385, .5625000000,...

It is often helpful to save an expression as a named expression to save typing and also to check that the expression has been typed correctly. For example, we could name the expression representing the formula for the nth term of the sequence as follows:

> a:=(2*n+1)/(3*n+4);

a := (2*n+1)/(3*n+4)

We can see from the last output that we have typed in the expression for the nth term correctly, or if there is an error, we should be able to see it is wrong and make the necessary corrections. Now to generate terms of the sequence, we need only type

> seq(a,n=1..30);

3/7, 1/2, 7/13, 9/16, 11/19, 13/22, 3/5, 17/28, 19/...

> seq(evalf(a),n=1..30);

.4285714286, .5000000000, .5384615385, .5625000000,...
.4285714286, .5000000000, .5384615385, .5625000000,...
.4285714286, .5000000000, .5384615385, .5625000000,...
.4285714286, .5000000000, .5384615385, .5625000000,...
.4285714286, .5000000000, .5384615385, .5625000000,...

The limit of a sequence can be found using the limit command with second parameter n=infinity.

> limit(a,n=infinity);

2/3

One example of a sequence that does not have a simple defining formula is one that involves a product with an increasing number of factors such as 1*3*5***(2n-1). A special command to deal with such products is called product. The first parameter is the formula for each factor, and the second parameter is the range of the variable. So we obtain

> product(2*k-1,k=1..2);

3

> product(2*k-1,k=1..3);

15

> product(2*k-1,k=1..4);

105

Now we can combine seq and product to obtain the terms of the sequence a[n]=1*3*5*(2n-1)

> seq(product(2*k-1,k=1..n),n=1..10);

1, 3, 15, 105, 945, 10395, 135135, 2027025, 3445942...

The limit of this sequence is evidently infinity.

> limit(product(2*k-1,k=1..n),n=infinity);

infinity

The product n! is defined in Maple as a function called factorial. Care should be taken with the use of this function since the value of factorial grows to an unmanageable size rapidly.

> seq(factorial(n),n=1..9);

1, 2, 6, 24, 120, 720, 5040, 40320, 362880

A second example of a sequence that does not have a simple defining formula is one that is defined recursively (terms are defined as a function of previous terms). For example a[1] = 1, a[n+1] = 1/(1+a[n]) is a recursively defined sequence. One can generate terms of this sequence by using a function with formula obtained by replacing a[n] in the formula of the sequence by the function variable x. Thus

> f:= x -> 1/(1+x);

f := proc (x) options operator, arrow; 1/(1+x) end ...

Now a2 = f(a1) = f(1), a3 = f(a2), etc.

> f(1);

1/2

> f(1/2);

2/3

> f(2/3);

3/5

> f(3/5);

5/8

A loop can be used to generate the terms of the sequence as follows

> s:=1; for k from 2 to 9 do s:= f(s); od;

s := 1

s := 1/2

s := 2/3

s := 3/5

s := 5/8

s := 8/13

s := 13/21

s := 21/34

s := 34/55