How Many Interations Does It Take Before Successive Iterations Do Not Change More Than 1E-6?
1 view (last 30 days)
Show older comments
This is Fibonacci's sequence and f3 is each element in the sequence divided by the one before it. This is supposed to demonstrate the Golden Ratio. However, I need my code to tell me how many times elements there are before each successive element is no greater than 10^-6 before it. I'm pretty sure I need to use a while loop, but I still can't get it right....
%fib seq using Binet Eq
a= 1:10;
b= sqrt(5);
x = (1-b)/2;
y = (1+b)/2;
f = (y.^a - x.^a)./b;
fb=reshape(f,[],5)
%ratio
c=a+1;
f2 = (y.^c - x.^c)./b;
f3=f2./f;
0 Comments
Answers (1)
Santhana Raj
on 13 Apr 2017
The equation of b, x and y can be outside the loop and same as your definitions.
k=1;
while true
k=k+1;
f(k) = (y.^k - x.^k)./b;
c=k+1;
f2(k) = (y.^c - x.^c)./b;
f3(k)=f2(k)/f(k);
if(abs(f3(k)-f3(k-1))<10e-6)
break;
end
end
Hope it helps.
0 Comments
See Also
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!