Clear Filters
Clear Filters

Fibonacci Sequence and Golden Ratio issue

1 view (last 30 days)
Mitul Dattani
Mitul Dattani on 10 May 2018
Commented: Guillaume on 10 May 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 Comments
Mitul Dattani
Mitul Dattani on 10 May 2018
Edited: Mitul Dattani on 10 May 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 on 10 May 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.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!