fibonacci
Fibonacci numbers
Syntax
Description
returns the nth
Fibonacci Number.f
= fibonacci(n
)
Examples
Find Fibonacci Numbers
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
Fibonacci Sequence Approximates Golden Ratio
The ratio of successive Fibonacci numbers converges to the golden ratio . 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
Symbolically Represent Fibonacci Numbers
Use Fibonacci numbers in symbolic calculations by representing them with symbolic input. fibonacci
returns the input.
Represent the Fibonacci number.
syms n
f = fibonacci(n)
f =
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); f300sym = fibonacci(num)
f300sym =
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.
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; 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
n
— Input
number | vector | matrix | multidimensional array | symbolic number | symbolic variable | symbolic vector | symbolic matrix | symbolic multidimensional array | symbolic function | symbolic expression
Input, specified as a number, vector, matrix or multidimensional array, or a symbolic number, variable, vector, matrix, multidimensional array, function, or expression.
More About
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)