Fibonnaci until a ratio is achieved between adjacent values.
Show older comments
Hello!
I am trying to write a code that accepts the first two numbers of a Fibonacci sequence as user input, then calculates additional values in the sequence until a ratio of adjacent values converges to within 0.001. I am able to write a Fibonacci sequence where the user inputs the first two numbers as well as to what Nth number it should calculate.
I am stuck on how it is supposed to keep calculation until said ratio is achieved.
I always get the error messeage "Index exceeds the number of array elements. Index must not exceed 2.". If I understand this correctly, the code stops before adding onto the vector x due to that it is not able to. I haven't been able to fix this.
clear,clc
format default
%Create a program that accepts the first two numbers of a Fibonacci
%sequence as user input, then calculates additonal values in the sequence
%until the ratio of adjacent values converges to within 0.001.
%Define the inputs.
fib_first=input('Please enter the first number: ');
fib_second=input('Please enter the second number: ');
%Define a vector which can be filled.
x=zeros();
x(1)=fib_first;
x(2)=fib_second;
i=3;
while abs(x(i)/x(i-1)-x(i-1)/x(i-2))>0.001
x(i)=x(i-2)+x(i-1);
i=i+1;
end
x(i)
1 Comment
akshatsood
on 3 Sep 2023
Edited: akshatsood
on 3 Sep 2023
Hi Milton,
I understand that you are facing an error while implementing the Fibonacci Sequence. I have investigated the attached code and found that the issue is due to the condition in the while loop. To be clear, let us focus on this section of your code:
x=zeros();
x(1)=fib_first;
x(2)=fib_second;
i=3;
while abs(x(i)/x(i-1)-x(i-1)/x(i-2))>0.001 % cause of error message
When the control reaches the while loop, and the condition is evaluated for the first iteration, it attempts to access x(3), whereas x is of size 2, meaning it only has two elements. To address this, you can tweak your code as per the following format:
while true
% statements
if condition
break
end
% update i
end
I hope this helps.
Accepted Answer
More Answers (1)
Ken Garrard
on 6 Sep 2023
The ratio of successive Fibonacci numbers converges very rapidly to 1-Phi, where Phi is the golden ratio, (1+sqrt(5)/2 = 1.618033988749... Convergence to less than .001 occurs with the 11 Fibonacci number.
>> fibseq = [0 1 1 2 3 5 8 13 21 34 55];
>> abs(diff(fibseq(1:end-1)./fibseq(2:end)))
ans =
1.0000 0.5000 0.1667 0.0667 0.0250 0.0096 0.0037 0.0014 0.0005
So you only need a vector of the first 11 Fibonacci numbers. You should validate that the inputs are Fibonacci numbers. And the second one isn't needed.
function seq = fibo
isperfect = @(x)(int64(sqrt(x)) .^ 2 == x);
isfib = @(x)(isperfect(5 * x.^2 + 4) | isperfect(5 * x.^2 - 4));
f(1) = input('1st Fibonacci Number:');
if ~isfib(f(1)), error('%d is not a Fibonacci number',f(1));
end
f(2) = input('2nd Fibonacci Number:');
if ~isfib(f(2)), error('%d is not a Fibonacci number',f(2));
end
fibseq = [0 1 1 2 3 5 8 13 21 34 55];
if f(2) >= fibseq(end), seq = f;
else seq = fibseq(fibseq >= f(1));
end
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!