fibonacci series, while loop
Mostrar comentarios más antiguos
Remember the Fibonacci series:
a(k+1)=a(k)+a(k−1) ∀k>2, a(0)=1,a(1)=1 (2)
Interestingly the golden ratio can be calculated from the this series:
— Write a script that calculates the golden ratio from the Nth and (N − 1)th Fibonacci numbers.
my code look like this but it didnt work. Can someone help me? Thank you!
a(1) = 1;
a(2) = 1;
n=3
while n <= 16 %the value for this can be changed
a(n) = a(n-1)+a(n-2);
ratio = limit(a(n)/a(n-1),n,inf);
n=n+1;
end
2 comentarios
darova
el 6 de Oct. de 2019
Where did yu get this information?
John D'Errico
el 6 de Oct. de 2019
The limit will indeed approach the golden ratio. This is a well known fact, and pretty easily proven, based on the Binet formula for the Fibonacci sequence.
(sqrt(5) + 1)/2
ans =
1.61803398874989
Respuesta aceptada
Más respuestas (1)
David Hill
el 6 de Oct. de 2019
If you have the symbolic toolbox you can just use the built in fibonacci function.
estGR = fibonacci(n)/fibonacci(n-1);
Otherwise, the code to calculate the nth fibonacci number is not difficult.
Categorías
Más información sobre Mathematics en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!