% hw029.m
%  Estimate phi to given accuracy using a sequence.
%  F(1) = 1, F(N) = 1 + 1/F(N-1).
%
acc = input ( '  Enter desired accuracy: ' );

i = 0;

while ( true )

  if ( i == 0 )
    b = 0;
    a = 1.0;
  else
    b = a;
    a = 1.0 + 1.0 / b;
  end

  err_est = abs ( b - a );

  if ( err_est <= acc )
    break;
  end

  i = i + 1;

end

phi = ( sqrt ( 5.0 ) + 1.0 ) / 2.0;
fprintf ( '\n' );
fprintf ( 'Number of steps was %d\n', i );
fprintf ( 'Estimated PHI = %20.16g\n', a );
fprintf ( 'Exact     PHI = %20.16g\n', phi );
fprintf ( 'Estimated Error = %8.4e\n', err_est );
fprintf ( 'Exact     Error = %8.4e\n', abs ( a - phi ) );

