1 view (last 30 days)
Show older comments
Milton on 3 Sep 2023


Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/2016256fibonnaciuntilaratioisachievedbetweenadjacentvalues
Answered: Ken Garrard on 6 Sep 2023
Accepted Answer: MYBLOG
Open in MATLAB Online
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(i1)x(i1)/x(i2))>0.001
x(i)=x(i2)+x(i1);
i=i+1;
end
x(i)
1 Comment Show 1 older commentsHide 1 older comments
Show 1 older commentsHide 1 older comments
akshatsood on 3 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2016256fibonnaciuntilaratioisachievedbetweenadjacentvalues#comment_2871041
Edited: akshatsood on 3 Sep 2023
Open in MATLAB Online
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(i1)x(i1)/x(i2))>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.
Sign in to comment.
Sign in to answer this question.
Accepted Answer
MYBLOG on 3 Sep 2023
Open in MATLAB Online
The error you're encountering, "Index exceeds the number of array elements. Index must not exceed 2," is due to the fact that you're trying to access an element in the x vector that hasn't been allocated yet. In MATLAB, you cannot access an element of an array beyond its current size without first resizing it. In your code, you've initialized x with two elements, and then you're trying to access elements beyond the second element.
To fix this issue, you need to preallocate enough space for the x vector to hold all the Fibonacci numbers you want to calculate. You can do this by setting an upper limit for how many numbers you want to calculate and initializing the vector with zeros of that size. Here's a modified version of your code that addresses this issue:
clear, clc
format default
% Create a program that accepts the first two numbers of a Fibonacci
% sequence as user input, then calculates additional 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: ');
% Set an upper limit for the number of Fibonacci numbers to calculate.
max_iterations = 1000;
% Define a vector with enough space for the Fibonacci sequence.
x = zeros(1, max_iterations);
x(1) = fib_first;
x(2) = fib_second;
i = 3;
while abs(x(i1)/x(i2)  x(i2)/x(i3)) > 0.001
x(i) = x(i1) + x(i2);
i = i + 1;
if i > max_iterations
disp('Convergence not achieved within the specified limit.');
break;
end
end
% Trim the x vector to remove unused elements.
x = x(1:i1);
% Display the Fibonacci sequence.
disp('Fibonacci sequence:');
disp(x);
1 Comment Show 1 older commentsHide 1 older comments
Show 1 older commentsHide 1 older comments
Voss on 4 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2016256fibonnaciuntilaratioisachievedbetweenadjacentvalues#comment_2872241
Open in MATLAB Online
This answer gives an error, due to trying to access x(i3) when i is 3:
clear, clc
format default
% Create a program that accepts the first two numbers of a Fibonacci
% sequence as user input, then calculates additional 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: ');
fib_first = 1;
fib_second = 1;
% Set an upper limit for the number of Fibonacci numbers to calculate.
max_iterations = 1000;
% Define a vector with enough space for the Fibonacci sequence.
x = zeros(1, max_iterations);
x(1) = fib_first;
x(2) = fib_second;
i = 3;
while abs(x(i1)/x(i2)  x(i2)/x(i3)) > 0.001
x(i) = x(i1) + x(i2);
i = i + 1;
if i > max_iterations
disp('Convergence not achieved within the specified limit.');
break;
end
end
Array indices must be positive integers or logical values.
% Trim the x vector to remove unused elements.
x = x(1:i1);
% Display the Fibonacci sequence.
disp('Fibonacci sequence:');
disp(x);
Sign in to comment.
More Answers (1)
Ken Garrard on 6 Sep 2023
Open in MATLAB Online
The ratio of successive Fibonacci numbers converges very rapidly to 1Phi, 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:end1)./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
0 Comments Show 2 older commentsHide 2 older comments
Show 2 older commentsHide 2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationSymbolic Math ToolboxMathematicsNumber Theory
Find more on Number Theory 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
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
Americas
 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
 Deutsch
 English
 Français
 United Kingdom(English)
Asia Pacific
 Australia (English)
 India (English)
 New Zealand (English)
 中国
 日本Japanese (日本語)
 한국Korean (한국어)
Contact your local office