# 3.6: Sequences


Now that we have the ability to write loops, we can use them to explore sequences and series, which are useful for describing and analyzing systems that change over time.

In mathematics, a sequence is a set of numbers that corresponds to the positive integers. The numbers in the sequence are called elements. In math notation, the elements are denoted with subscripts, so the first element of the series $$A$$ is $$A_1$$, followed by $$A_2$$, and so on.

A for loop is a natural way to compute the elements of a sequence. As an example, in a geometric sequence, each element is a constant multiple of the previous element. As a more specific example, let’s look at the sequence with $$A_1 = 1$$ and the relationship $$A_{i+1} = A_i/2$$, for all $$i$$. In other words, each element is half as big as the one before it.

The following loop computes the first 10 elements of $$A$$:

a = 1
for i=2:10
a = a/2
end

The first line initializes the variable a with the first element of the sequence, $$A_1$$. Each time through the loop, we find the next value of a by dividing the previous value by 2, and assign the result back to a. At the end, a contains the 10th element.

The other elements are displayed on the screen, but they are not saved in a variable. Later, we’ll see how to save the elements of a sequence in a vector.

This loop computes the sequence recurrently, which means that each element depends on the previous one. For this sequence, it’s also possible to compute the $$i$$th element directly, as a function of $$i$$, without using the previous element.

In math notation, $$A_i = A_1 r^{i-1}$$, where $$r$$ is the ratio of successive elements. In the previous example, $$A_{i+1} = A_i/2$$, so $$r = 1/2$$.

##### Exercise $$3.3$$

Write a script named sequence.m that uses a loop to compute elements of $$A$$ .

This page titled 3.6: Sequences is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.