Skip to main content
Engineering LibreTexts

7: The Fibonacci Sequence

  • Page ID
    85942
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The Fibonacci sequence, denoted \(F\), is a sequence of numbers where each number is the sum of the previous two. It’s defined by the equations \(F_1 = 1\), \(F_2 = 1\), and, for \(i > 2\), \(F_{i} = F_{i-1} + F_{i-2}\). The following expression computes the \(n\)th Fibonacci number: \[F_n = \frac{1}{\sqrt{5}} \left[ \left( \frac{1 + \sqrt{5}}{2} \right)^{n} - \left( \frac{1 - \sqrt{5}}{2} \right)^{n} \right] \notag\]

    We can translate this expression into MATLAB, like this:

    s5 = sqrt(5);
    t1 = (1 + s5) / 2;
    t2 = (1 - s5) / 2;
    diff = t1^n - t2^n;
    ans = diff / s5

    I use temporary variables like t1 and t2 to make the code readable and the order of operations explicit. The first four lines have a semicolon at the end, so they don’t display anything. The last line assigns the result to ans.

    If we save this script in a file named fibonacci1.m, we can run it like this:

    >> n = 10
    >> fibonacci1
    ans = 55.0000

    Before calling this script, you have to assign a value to n. If n is not defined, you get an error:

    >> clear n
    >> fibonacci1
    Undefined function or variable 'n'.
    
    Error in fibonacci1 (line 9)
    diff = t1^n - t2^n;

    This script only works if there is a variable named n in the workspace; otherwise, you should get an error. MATLAB will tell you what line of the script the error is in and display the line.

    Error messages can be helpful, but beware! In this example, the message says the error is in fibonacci, but the actual problem is that we have not assigned a value to n. And that brings us to the Fourth Theorem of Debugging:

    Error messages tell you where the problem was discovered, not where it was caused.

    Often you have to work backwards to find the line of code (or missing line) that caused the problem.


    This page titled 7: The Fibonacci Sequence 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.