fibonacci
Fibonacci numbers
Syntax
Description
returns the nth
Fibonacci Number.f = fibonacci(n)
Examples
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 . 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 Fibonacci number.
syms n
f = fibonacci(n)f =
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.
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
Input, specified as a number, vector, matrix or multidimensional array, or a symbolic number, variable, vector, matrix, multidimensional array, function, or expression.
More About
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)