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

1 回表示 (過去 30 日間)
cestcandice
cestcandice 2017 年 4 月 13 日
回答済み: Santhana Raj 2017 年 4 月 13 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by