Effacer les filtres
Effacer les filtres

Fibonacci Sequence and Golden Ratio issue

1 vue (au cours des 30 derniers jours)
Mitul Dattani
Mitul Dattani le 10 Mai 2018
Commenté : Guillaume le 10 Mai 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 commentaires
Mitul Dattani
Mitul Dattani le 10 Mai 2018
Modifié(e) : Mitul Dattani le 10 Mai 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 le 10 Mai 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.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by