Fibonnaci until a ratio is achieved between adjacent values.

4 次查看(过去 30 天)
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 个评论
akshatsood
akshatsood 2023-9-3
编辑:akshatsood 2023-9-3
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.

请先登录,再进行评论。

采纳的回答

MYBLOG
MYBLOG 2023-9-3
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(i-1)/x(i-2) - x(i-2)/x(i-3)) > 0.001
x(i) = x(i-1) + x(i-2);
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:i-1);
% Display the Fibonacci sequence.
disp('Fibonacci sequence:');
disp(x);
  1 个评论
Voss
Voss 2023-9-4
This answer gives an error, due to trying to access x(i-3) 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(i-1)/x(i-2) - x(i-2)/x(i-3)) > 0.001
x(i) = x(i-1) + x(i-2);
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:i-1);
% Display the Fibonacci sequence.
disp('Fibonacci sequence:');
disp(x);

请先登录,再进行评论。

更多回答(1 个)

Ken Garrard
Ken Garrard 2023-9-6
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

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by