필터 지우기
필터 지우기

Fibonacci Sequence and Golden Ratio issue

조회 수: 1 (최근 30일)
Mitul Dattani
Mitul Dattani 2018년 5월 10일
댓글: Guillaume 2018년 5월 10일
I was revising for an exam coming up and had to a fibonacci sequence and golden ratio
%delta = precision to 'm' sig figs
m = 3;
delta = 0.5*10^-m;
%Start from the calculation of n=2
n=2;
%only store two f numbers at a time
F1 = 1;
F2 = 2;
phi = F2/F1;
fprintf('At n = 2: F(n-1) = %d, F(n) = %d, phi = %.*f. \n',F1, F2, m, phi);
for n = 3:100
Ftemp = F2;
F2 = F1 + F2;
F1 = Ftemp;
phiold = phi;
phi = F2/F1;
fprintf('At n = %d: F(n-1) = %d, F(n) = %d, phi = %.*f.\n',n,F1,F2,m,phi);
if abs(phiold-phi) < delta
phi = round(phi,m);
fprintf('Phi = %.*f is now precide to %d d.p. \n',m,phi,m);
break
end
end
with the above code if I use F = ones(1,2) instead of declaring F1 and F2 as 1 and 1, I get 12 iterations where declaring f1 and f2 as i did in the code above I get 11 iterations.
The answer im supposed to get should give 12 iterations but I cant see why it wouldnt give the same answer.
  댓글 수: 3
Mitul Dattani
Mitul Dattani 2018년 5월 10일
편집: Mitul Dattani 2018년 5월 10일
Ah sorry typo using f = ones(1,2) gives 12 but declaring f1 and f2 seperately gives 11 ive edited the post
Guillaume
Guillaume 2018년 5월 10일
F or f does not appear in your code. If F is supposed to be the concatenation of [F1, F2], then
F1 = 1;
F2 = 2;
F = [F1, F2];
is not equivalent to
F = ones(1, 2);
The former results in [1, 2], the latter in [1, 1]
Note that matlab is case sensitive, f and F are different variables.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by