필터 지우기
필터 지우기

The Fibonacci Sequence and Golden Ratio.

조회 수: 3 (최근 30일)
Jose Grimaldo
Jose Grimaldo 2020년 3월 24일
답변: Rashed Mohammed 2020년 3월 27일
Im having trouble calculating the Golden Ratios until the desired accuracy is reached
% Code
Fibonacci Sequence
F=[1 1 2 3 5 8 13 21 34 55]
DA=input('How many decimals of accuracy would you like to calculate the Golden Ratio to: ');
G=round(((1+sqrt(5))/2),DA);
GR(1)=0;
for index=2:N % N, is equal to 10
GR(index)=F(index)/F(index-1);
index=index+1;
if round(GR,DA)==G
end
end
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2020년 3월 24일
Jose - careful with code like
round(GR,DA)==G
because GR is an array whereas DA and G are scalars. Do you really mean to compare (or whatever) the array GR with DA and G? Or do you just want to consider the last element of GR? Also, you use == (generally) when comparing integers and not when using doubles (due to floating point precision, etc.) and so instead would use some sort of tolerance test to see if two doubles (or floats) are "close enough". This might be something like
abs(x - y) < eps
to check to see if the difference between the two numbers is small enough (less than eps) so that x and y can be considered equal. You will probably need to do something here with the last element calculated in GR and G.
David Hill
David Hill 2020년 3월 24일
I believe that you need to calculate the Fibonacci number as you go and should not use a lookup table. I suggest you use a while loop looking at the difference of the last two Golden ratios calculated to determine if the accuracy requirement is met. If accuracy greater than floating point is required, then that is another problem. Try something like:
F(1:2)=1;
GR(1:2)=[0,1]
c=3;
while abs(GR(end)-GR(end-1))>10^-DA
F(c)=F(c-1)+F(c-2);
GR(c)=F(c)/F(c-1);
c=c+1;
end

댓글을 달려면 로그인하십시오.

채택된 답변

Rashed Mohammed
Rashed Mohammed 2020년 3월 27일
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.
  1. 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.
  2. 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.
  3. 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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by