Hi Jose,

I understand that you would like to calculate Golden Ratio’s until a desired accuracy is reached. There are three problems I can notice from your code.

- You have a fixed number of Fibonacci numbers on which you are calculating golden ratios. The desired accuracy may or may not be present in the calculated golden ratios since it is a limited set. As David mentioned you need to calculate the Fibonacci numbers as you go.
- The code round (GR,DA) == G gives you a logical vector and if block only executes when all the values in logical vector are 1. Hence even when you reach a desired accuracy the if block may or may not execute depending on previous values of GR.
- Whenever you are testing for accuracy, it is recommended to check for closeness of value instead of them being equal as suggested by both Geoff and David.

You can try the following code.

F(1:2) = 1;

DA = input('How many decimals of accuracy would you like to calculate the Golden Ratio to: ');

G = ((1+sqrt(5))/2);

GR = 1;

index = 3;

while abs(G-GR) > 10^-DA

F(index) = F(index-1)+F(index-2);

GR = F(index)/F(index-1);

index = index + 1;

end

## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/512741-the-fibonacci-sequence-and-golden-ratio#comment_814892

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/512741-the-fibonacci-sequence-and-golden-ratio#comment_814892

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/512741-the-fibonacci-sequence-and-golden-ratio#comment_814893

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/512741-the-fibonacci-sequence-and-golden-ratio#comment_814893

Sign in to comment.