Fibonnaci until a ratio is achieved between adjacent values. (2024)

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/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values

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(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

Show -1 older commentsHide -1 older comments

akshatsood on 3 Sep 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#comment_2871041

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#comment_2871041

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.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

MYBLOG on 3 Sep 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#answer_1300066

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#answer_1300066

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(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 Comment

Show -1 older commentsHide -1 older comments

Voss on 4 Sep 2023

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#comment_2872241

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#comment_2872241

Open in MATLAB Online

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);

Sign in to comment.

More Answers (1)

Ken Garrard on 6 Sep 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#answer_1302466

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2016256-fibonnaci-until-a-ratio-is-achieved-between-adjacent-values#answer_1302466

Open in MATLAB Online

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

0 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

Tags

  • fibonacci

Products

  • MATLAB

Release

R2022b

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.


Fibonnaci until a ratio is achieved between adjacent values. (6)

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)

Europe

  • 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

Fibonnaci until a ratio is achieved between adjacent values. (2024)

FAQs

What is the ratio between Fibonacci numbers? ›

Fibonacci Sequence
Fibonacci numberdivided by the one beforeratio
11/1= 1.0000
22/1= 2.0000
33/2= 1.5000
55/3= 1.6667
9 more rows
Sep 12, 2020

What are the adjacent numbers in the Fibonacci sequence? ›

if (both(b,c) are == to 1) then the 2 initial numbers were adjacent in the Fibonacci sequence. (if they are 1 of these cases, then those 2 numbers are adjacent in the Fibonacci sequence). if one of the number is <=0, then those 2 numbers are not adjacent in the Fibonacci sequence.

What did Fibonacci say about the golden ratio *? ›

The Golden Ratio is a relationship between two numbers that are next to each other in the Fibonacci sequence. When you divide the larger one by the smaller one, the answer is something close to Phi. The further you go along the Fibonacci Sequence, the closer the answers get to Phi.

What is the limit of the ratio of the Fibonacci sequence? ›

The golden ratio is derived by dividing each number of the Fibonacci series by its immediate predecessor. Where F(n) is the nth Fibonacci number, the quotient F(n)/ F(n-1) will approach the limit 1.618, known as the golden ratio.

How to find Fibonacci ratio? ›

The key Fibonacci ratio of 61.8% is found by dividing one number in the series by the number that follows it. For example, 21 divided by 34 equals 0.6176, and 55 divided by 89 equals about 0.61798. The 38.2% ratio is discovered by dividing a number in the series by the number located two spots to the right.

What is the golden rule of the Fibonacci numbers? ›

The golden ratio, also known as the golden number, golden proportion, or the divine proportion, is a ratio between two numbers that equals approximately 1.618. Usually written as the Greek letter phi, it is strongly associated with the Fibonacci sequence, a series of numbers wherein each number is added to the last.

What is the God number in nature? ›

The golden ratio, represented by the Greek letter phi (Φ), is a special number approximately equal to 1.618033988749895. The golden ratio is also known as the divine proportion, the golden mean, or the golden section.

How is Fibonacci used in real life? ›

These numbers are used in various fields such as architecture, art, space exploration, engineering, technology, and computing. The Fibonacci sequence, also known as the golden ratio, is utilized in architectural designs, creating aesthetically pleasing structures 1.

What is the best Fibonacci golden ratio? ›

What is the Fibonacci sequence? The golden ratio of 1.618 – the magic number – gets translated into three percentages: 23.6%, 38.2% and 61.8%.

How to prove golden ratio? ›

You take a line and divide it into two parts – a long part (a) and a short part (b). The entire length (a + b) divided by (a) is equal to (a) divided by (b). And both of those numbers equal 1.618. So, (a + b) divided by (a) equals 1.618, and (a) divided by (b) also equals 1.618.

What is the golden pocket ratio of the Fibonacci numbers? ›

One key strategy you'll hear a lot of traders talk about in relation to Fibonacci retracement is trading the Golden Pocket for potential reversals. The Golden Pocket is typically considered to be between the 0.618 (or -61.8%) and 0.65 (or -65%) levels.

Is there a rule for the Fibonacci sequence? ›

The sequence follows the rule that each number is equal to the sum of the preceding two numbers. The Fibonacci sequence begins with the following 14 integers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ...

What is the nature's golden ratio? ›

The “golden ratio” is a 1.618:1 mathematical ratio, and the number 1.618 is known as “phi.” Golden ratios can be found in shells, plants, flowers, and animals, among other places. It is believed to be one of the strongest and oldest connections between math and creative arts.

What is the golden ratio of the nth Fibonacci number? ›

The numbers in a Fibonacci series are related to the golden ratio. Any Fibonacci number ((n + 1)th term) can be calculated using the Golden Ratio using the formula, Fn = (Φn - (1-Φ)n)/√5, Here φ is the golden ratio where φ ≈ 1.618034. For example: To find the 7th term, we apply F6 = (1.6180346 - (1-1.618034)6)/√5 ≈ 8.

Top Articles
Glen Mor Resident Services Office
Lolwiki Milio
Will Byers X Male Reader
Enrique Espinosa Melendez Obituary
Best Team In 2K23 Myteam
Soap2Day Autoplay
How to know if a financial advisor is good?
Polyhaven Hdri
Steamy Afternoon With Handsome Fernando
Nc Maxpreps
Jesse Mckinzie Auctioneer
2021 Tesla Model 3 Standard Range Pl electric for sale - Portland, OR - craigslist
Large storage units
ExploreLearning on LinkedIn: This month&#39;s featured product is our ExploreLearning Gizmos Pen Pack, the…
Walmart Windshield Wiper Blades
Cvs Appointment For Booster Shot
2016 Ford Fusion Belt Diagram
Teenleaks Discord
25Cc To Tbsp
Napa Autocare Locator
Kashchey Vodka
Pocono Recird Obits
Ficoforum
Znamy dalsze plany Magdaleny Fręch. Nie będzie nawet chwili przerwy
Malluvilla In Malayalam Movies Download
Smartfind Express Login Broward
Gunsmoke Tv Series Wiki
HP PARTSURFER - spare part search portal
Sony Wf-1000Xm4 Controls
Datingscout Wantmatures
Craigs List Tallahassee
Jeep Cherokee For Sale By Owner Craigslist
Tamilrockers Movies 2023 Download
Orangetheory Northville Michigan
The Mad Merchant Wow
2008 Chevrolet Corvette for sale - Houston, TX - craigslist
Aliciabibs
Delaware judge sets Twitter, Elon Musk trial for October
Jason Brewer Leaving Fox 25
Jasgotgass2
Clima De 10 Días Para 60120
Emily Tosta Butt
Craigslist Odessa Midland Texas
Dinar Detectives Cracking the Code of the Iraqi Dinar Market
Sdn Fertitta 2024
Kb Home The Overlook At Medio Creek
Flappy Bird Cool Math Games
Ucla Basketball Bruinzone
Meet Robert Oppenheimer, the destroyer of worlds
Jimmy John's Near Me Open
Diamond Desires Nyc
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6081

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.