# fibonacci

Fibonacci numbers

## Syntax

``f = fibonacci(n)``

## Description

example

````f = fibonacci(n)` returns the nth Fibonacci Number.```

## Examples

collapse all

Find the sixth Fibonacci number by using `fibonacci`.

`f = fibonacci(6)`
```f = 8 ```

Find the first 10 Fibonacci numbers.

```n = 1:10; f = fibonacci(n)```
```f = 1×10 1 1 2 3 5 8 13 21 34 55 ```

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); plot(n,ratio,'--o') hold on yline(1.61803) hold off```

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

Represent the ${\mathit{n}}^{\mathrm{th}}$ Fibonacci number.

```syms n f = fibonacci(n)```
`f = $\mathrm{fibonacci}\left(n\right)$`

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); f300sym = fibonacci(num)```
`f300sym = $222232244629420445529739893461909967206666939096499764990979600$`

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

`f300double = double(f300sym)`
```f300double = 2.2223e+62 ```

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

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; end % 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') end```

## 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.

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 F0 = 0 and F1 = 1, the nth Fibonacci number is

Fn = Fn–1 + Fn–2.

Applying this formula repeatedly generates the Fibonacci numbers.

## Version History

Introduced in R2017a