function testglobmin
param=.08;% a function parameter
a=-8;b=8;%the search bounds
x0=4;%initial guess
y0=f2(x0,param);%for plotting purpose
m=100;% a very pessimistic bound
e=1e-14;%precision of the computed f(x,...) see test function f2 below
tolf=1e-8;%absolute tolerance on the global minimum value of f(x)
%some different calls about f(x)
[x,y,fcount]=globalmin(@f2,a,b,x0,m,e,tolf,param);
disp(['x = ',num2str(x,'%0.5e'),' y = ',num2str(y,'%0.5e'),' fcount = ',num2str(fcount)]);
%or
[x,y,fcount]=globalmin(@(x) f2(x,param),a,b,x0,m,e,tolf);
disp(['x = ',num2str(x,'%0.5e'),' y = ',num2str(y,'%0.5e'),' fcount = ',num2str(fcount)]);
%or
[x,y,fcount]=globalmin(@(t) param*t-sin(t),a,b,x0,m,e,tolf);
disp(['x = ',num2str(x,'%0.5e'),' y = ',num2str(y,'%0.5e'),' fcount = ',num2str(fcount)]);
%now plot some results
close all;maxifigure(figure);
t=(a:.1:b);z=f2(t,param);%f(x) plot
plot(t,z,'linew',3,'color','k');set(gca,'fontw','b','fonts',16);
title('\bf GLOBAL OPTIMIZATION','fonts',24)
line(x0,y0,'marker','+','markers',30,'linew',3,'color','r')
text(x0-0.3,y0,'\bfStarting point','fonts',24,'color','r','horiz','r')
m=[100 10 1];v=[1 .85 .7];%m values testing knowing that f''(x)<=1
for k=1:3
[x,y,fcount]=globalmin(@f2,a,b,x0,m(k),e,tolf,param);
line(x,y,'marker','+','markers',30,'linew',3,'color','b')
text(-7.5,v(k),['\bfm = ',num2str(m(k)),' f(x) Calls number : ',...
num2str(fcount)],'fonts',24,'color','b')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y=f2(t,param)
% here it is easy to verify that f2''=sin(t)<=1
y=param*t-sin(t);
% computation precision at least 1e-14
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%a graphical add on used here
function maxifigure(hf,offset)
% makes figure(hf) full screen size
if nargin==1,offset=0;end
size_screen=get(0,'screensize');
tmp=get(hf,'units');set(hf,'units','pixel');
size_screen(2)=size_screen(2)+offset;
size_screen(4)=size_screen(4)-offset;
set(hf,'outerposition',size_screen);
set(hf,'units',tmp);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%