#!/usr/bin/octave -q

%-----------------------------------------------------------------------------
% Jonathan R. Senning <jonathan.senning@gordon.edu>
% Gordon College
% Written May 3, 1999
% Revised December 5, 2000
%
% Demonstrates the Fibonacci search algorithm as described on pages 552-555
% of "Numerical Mathematics and Computing", 4th Edition, by Cheney & Kincaid,
% Brooks/Cole, 1999.
%-----------------------------------------------------------------------------

% Function to search for minimum, and the exact location of minimum (if
% known).  Of course, this will only be known for "textbook" problems with
% which we are testing this algorithm...

f = @(x) abs( sin( x ) );
x_exact = pi;

% Unimodal interval containing minimum

a = 2;
b = 4;

% Index of largest Fibonacci number we'll try.

MAX_FIB = 60;

% Tolerance -- we want our answer to be within this much of the true solution

tol = 1e-6;

[x, N] = fibonacci_search( f, a, b, tol, MAX_FIB );

fprintf( '%d Fibonacci numbers required\n', N );
fprintf( 'Solution is %f\n', x );
fprintf( 'tol = %e; Actual Error = %e\n', tol, x - x_exact );

% End of file.
