Main Content


Fibonacci numbers



fibonacci(n) returns the nth Fibonacci Number.


Find Fibonacci Numbers

Find the sixth Fibonacci number by using fibonacci.

ans =

Find the first 10 Fibonacci numbers.

n = 1:10;
ans =
     1     1     2     3     5     8    13    21    34    55

Fibonacci Sequence Approximates Golden Ratio

The ratio of successive Fibonacci numbers converges to the golden ratio 1.61803.... Show this convergence by plotting this ratio against the golden ratio for the first 10 Fibonacci numbers.

n = 2:10;
ratio = fibonacci(n)./fibonacci(n-1);

hold on

line(xlim,[1.618 1.618])
hold off

Figure contains an axes. The axes contains 2 objects of type line.

Symbolically Represent Fibonacci Numbers

Use Fibonacci numbers in symbolic calculations by representing them with symbolic input. fibonacci returns the input.

Represent the nth Fibonacci number.

syms n
ans =

Find Large Fibonacci Numbers

Find large Fibonacci numbers by specifying the input symbolically using sym. Symbolic input returns exact symbolic output instead of double output. Convert symbolic numbers to double by using the double function.

Find the 300th Fibonacci number.

num = sym(300);
fib300 = fibonacci(num)
fib300 =

Convert fib300 to double. The result is a floating-point approximation.

ans =

For more information on symbolic and double arithmetic, see Choose Numeric or Symbolic Arithmetic.

Golden Spiral Using Fibonacci Numbers

The Fibonacci numbers are commonly visualized by plotting the Fibonacci spiral. The Fibonacci spiral approximates the golden spiral.

Approximate the golden spiral for the first 8 Fibonacci numbers. Define the four cases for the right, top, left, and bottom squares in the plot by using a switch statement. Form the spiral by defining the equations of arcs through the squares in eqnArc. Draw the squares and arcs by using rectangle and fimplicit respectively.

x = 0;
y = 1;
syms v u

axis off
hold on

for n = 1:8

    a = fibonacci(n);

    % Define squares and arcs
    switch mod(n,4)
        case 0
            y = y - fibonacci(n-2);
            x = x - a;
            eqnArc = (u-(x+a))^2 + (v-y)^2 == a^2;
        case 1
            y = y - a;
            eqnArc = (u-(x+a))^2 + (v-(y+a))^2 == a^2;
        case 2
            x = x + fibonacci(n-1);
            eqnArc = (u-x)^2 + (v-(y+a))^2 == a^2;
        case 3
            x = x - fibonacci(n-2);
            y = y + fibonacci(n-1);
            eqnArc = (u-x)^2 + (v-y)^2 == a^2;

    % Draw square
    pos = [x y a a];
    rectangle('Position', pos)

    % Add Fibonacci number
    xText = (x+x+a)/2;
    yText = (y+y+a)/2;
    text(xText, yText, num2str(a))

    % Draw arc
    interval = [x x+a y y+a];
    fimplicit(eqnArc, interval, 'b')


Input Arguments

collapse all

Input, specified as a number, vector, matrix or multidimensional array, or a symbolic number, variable, vector, matrix, multidimensional array, function, or expression.

More About

collapse all

Fibonacci Number

The Fibonacci numbers are the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21….

Given that the first two numbers are 0 and 1, the nth Fibonacci number is

Fn = Fn–1 + Fn–2.

Applying this formula repeatedly generates the Fibonacci numbers.

Introduced in R2017a