function ShowGolden()
% Illustrates the method of Golden Section search that can be used
% to mininize a unimodal function.
% Display f and the search interval...
close all
t = linspace(0,2*pi);
plot(t,MyF(t))
hold on
L = 0;
R = 5;
plot([L R],[0 0],'r','linewidth',3)
% Find the global minimizer and display...
tmin = Golden(@MyF,L,R);
plot(tmin,0,'.c','Markersize',20)
hold off
shg

function tmin = Golden(f,a,b)
% Golden Section Search
% f a handle that references a function f(t) of a single variable.
% a,b define an interval [a,b] upon which f is unimodal.
%
% tmin approximate global minimizer of f on [a,b].

r = (3 - sqrt(5))/2;
c = a + r*(b-a); fc = f(c);
d = a + (1-r)*(b-a); fd = f(d);
while (d-c) > sqrt(eps)*max(abs(c),abs(d))
if fc >= fd
z = c + (1-r)*(b-c);
% [a c d b ] <--- [c d z b]
a = c;
c = d; fc = fd;
d = z; fd = f(z);
else
z = a + r*(d-a);
% [a c d b ] <--- [a z c d]
b = d;
d = c; fd = fc;
c = z; fc = f(z);
end
end
tmin = (c+d)/2;

function d = MyF(t)
% This is the 2-norm distance between (-4,3) and (3*cos(t),2*sin(t))
d = sqrt((-4 - 3*cos(t)).^2 + (3 - 2*sin(t)).^2);