How Many Interations Does It Take Before Successive Iterations Do Not Change More Than 1E-6?
    4 次查看(过去 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;
0 个评论
回答(1 个)
  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.
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

