Azzera filtri
Azzera filtri

Fibonacci Sequence and Golden Ratio issue

1 visualizzazione (ultimi 30 giorni)
Mitul Dattani
Mitul Dattani il 10 Mag 2018
Commentato: Guillaume il 10 Mag 2018
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 Commenti
Mitul Dattani
Mitul Dattani il 10 Mag 2018
Modificato: Mitul Dattani il 10 Mag 2018
Ah sorry typo using f = ones(1,2) gives 12 but declaring f1 and f2 seperately gives 11 ive edited the post
Guillaume
Guillaume il 10 Mag 2018
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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by