% CP6p3c.m
% Golden Section method to find the minimum in [0,3] of
% f(x) = x^2 + 4 cos(x).
% Results: xmin = 1.89549426370732, fval = 2.31680841978821
% K. Ming Leung, 10/24/03

clear all; format compact; format long g;

fcn = @fcn6p3c;

% First plot the function in interval [0,3]
% to see that there is indeed a single minimum:
x = 0:0.002:3; y = feval(fcn,x);
plot(x,y,'b-','lineWidth',1.5);
xlabel('x','fontSize',16)
ylabel('f(x)','fontSize',16)
title('Function f(x) = x^2+4 cos(x)','fontSize',15);

% Set lower and upper limits, and tolerance
% to call the Golden Search function
a = 0;
b = 3;
tol = eps;
[xmin,fval] = GoldenSectionF(fcn,a,b,tol),

% To use the above script, you need to have a file
% containing the following function:
% %
% % fcn6p3c.m
% % Function to compute the function value for
% % Computer Problem 6.3c
% % K. Ming Leung, 10/24/03
%
% function y = fcn6p3c(x, varargin);
%
% y = x.^2+4*cos(x);