How Many Interations Does It Take Before Successive Iterations Do Not Change More Than 1E-6?

조회 수: 1 (최근 30일)
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;

답변 (1개)

Santhana Raj
Santhana Raj 2017년 4월 13일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by