4.6: Vectors and Sequences

Matlab and Octave Programming for STEM Applications · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    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 was last modified on Wed, 13 Jul 2022 23:38:28 GMT and 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.