フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by