# 4.6: Vectors and Sequences


Vectors and sequences go together nicely. For example, another way to evaluate the Fibonacci sequence from Chapter 2 is to store successive values in a vector. Remember that the definition of the Fibonacci sequence is $$F_1 = 1$$, $$F_2 = 1$$, and $$F_{i} = F_{i-1} + F_{i-2}$$ for $$i > 2$$.

Listing 4.1 shows how we can compute the elements of this sequence and store them in a vector, using a capital letter for the vector F and lower-case letters for the integers i and n.

Listing 4.1: Calculating the Fibonacci sequence using a vector

F(1) = 1
F(2) = 1
for i=3:n
F(i) = F(i-1) + F(i-2)
end

If you had any trouble with , you have to appreciate the simplicity of this script. The MATLAB syntax is similar to the math notation, which makes it easier to check for correctness.

If you only want the $$n$$th Fibonacci number, storing the whole sequence wastes some space. But if wasting space makes your code easier to write and debug, that’s probably okay.

This page titled 4.6: Vectors and 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.