  # Sequences and Series - some ideas with Matlab

 Sequences and series are very related: a sequence of numbers is a function defined on the set of positive integers (the numbers in the sequence are called terms). In other words, a sequence is a list of numbers generated by some mathematical rule and typically expressed in terms of n. In order to construct the sequence, you group consecutive integer values into n.

For example, the sequence of odd integers is generated by the sequence
2n – 1. The sequence produce 2(1)-1, 2(2)-1, 2(3)–1, 2(4)-1... which produces 1, 3, 5, 7...

A series is the sum of the terms of a sequence. Series are based on sums, whereas sequences are not.

In Matlab, let’s generate the sequence above (for odd numbers), in two ways:

a) Using a for-loop in a sequence:

% We initialize our sum
s = 0;
% We are going to get the first 5 terms,
% one-by-one
in our sequence
for n = 1 : 5

% This is the shown formula for odd numbers
tn = 2*n-1

% We add-up every term in the series
s = s + tn;
end

% We display the sum of the series
s

The code produces these results:

tn = 1
tn = 3
tn = 5
tn = 7
tn = 9
s =  25

b) Using a vectorized way, which is simpler and faster:

% We define our first 5 ns
n = 1 : 5
% We calculate the terms
tn = 2*n - 1
% We add-up all the terms in the series
s = sum(tn)

The code produces these results:

n =   1     2     3     4     5
tn =  1     3     5     7     9
s =   25

### Arithmetic Sequences

This is a sequence of numbers each of which (after the first) is obtained by adding to the preceding number a constant called the common difference. Then, 3, 6, 9, 12, 15... is an arithmetic sequence because each term is obtained by adding 3 to the preceding number. In the arithmetic sequence 500, 450, 400... the common difference is -50.

Formulas for arithmetic sequences:

- The nth term, or last term: L = a + (n - 1)d
- The sum of the first n terms: S = n/2 (a + L) = n/2 [2a + (n - 1)d]

where
a = first term of the sequence
d = common difference
n = number of terms
L = nth term, or last term
S = sum of first n terms

Example:

Consider the arithmetic sequence 3, 7, 11, 15...
where a = 3 and d = 4.

The sixth term is:

L = a + (n - 1)d = 3 + (6 - 1)4 = 23.

The sum of the first six terms is:

S = n/2(a + L) = 6/2(3 + 23) = 78 or
S = 6/2[2a + (n - 1)d] = 6/2[2(3) + (6 - 1)4] = 78.

Let’s do it effortlessly with Matlab, without for-loops (vectorized way):

% Let's define our sequence, starting with 3,
% using steps of four units, until we reach 999, for example

seq = 3 : 4 : 999;

% Find the 6th element
sn = seq(6)

% Find the sum of the first 6 elements
s1_6 = sum(seq(1 : 6))

The results are:

sn = 23
s1_6 =  78

### Geometric Sequences

A geometric sequence is a sequence of numbers each of which is obtained by multiplying the preceding number by a constant number called the common ratio. So 4, 8, 16, 32... is a geometric sequence because each number is obtained by multiplying the preceding number by 2. In the geometric sequence 64, 16, 4, l, 1/4... the common ratio is 4.

Formulas for geometric sequences:

- The nth term, or last term: L = arn-1
- The sum of the first n terms:

S = a(rn – 1)/(r - 1) = (rL-a)/(r - 1)  (r and 1 are different)

where
a = first term
r = common ratio
n = number of terms
L = nth term, or last term
S = sum of first n terms

Example:

Consider the geometric sequence 5, 10, 20, 40...
where a = 5 and r = 2

The seventh term is:
L = arn-1 = 5(27-1) = 5(26) = 320

The sum of the first seven terms is:
S = a(rn-1)/(r-1) = 5(27-1)/(2-1) = 635

Let’s calculate it with Matlab:

a = 5; r = 2;

n = 1 : 7;
L = a * r.^(n-1)

% Find the 7th term
L(7)

% Find sum of first 7 terms
sum(L(1:7))

Matlab response is:

L =      5    10    20    40    80   160   320
ans =    320
ans =    635

Another way to approach it is:

n = 0 : 6

gs = 2.^n * 5

% Find the 7th term
gs(7)

% Find the sum of the first 7 terms
sum(gs(1 : 7))

n =      0     1     2     3     4     5     6
gs =     5    10    20    40    80   160   320
ans =    320
ans =    635

From 'Sequences and Series ' to home

From 'Sequences and Series ' to Calculus Problems  