#!/usr/bin/octave -q

%-----------------------------------------------------------------------------
% Jonathan R. Senning <jonathan.senning@gordon.edu>
% Gordon College
% Written May 3, 1999
% Revised December 5, 2000
%
% Demonstrates the golden section search as described on pages 555-556 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;

% Maximum number of iterations we'll allow.

MAX_ITERATIONS = 60;

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

tol = 1e-6;

[x, k] = golden_section( f, a, b, tol, MAX_ITERATIONS );

fprintf( '%d iterations done\n', k );
fprintf( 'Solution is %f\n', x );
fprintf( 'tol = %e; Actual Error = %e\n', tol, x - x_exact );

% End of file.
