Page 298 - MATLAB an introduction with applications
P. 298
Optimization ——— 283
f1 = feval(func,x1);
end
end
if f1 < f2; fMin = f1; xMin = x1;
else; fMin = f2; xMin = x2;
end
Example E5.8: Use Fletcher-Reeves method to find the minimum of function in Problem EN3.18.
T
Start with [0 0.05] .
Solution:
Global X FUNC DFUNC V
X=[0,0];
>> FUNC=@fex3_20;DFUNC=@dfex3_20;X=[0,0];
>> [xMin,fMin,nCyc]=FletcherReeves
xMin =
–0.3000
–0.3000
fMin =
–0.4500
nCyc =
3
function [xMin,fMin,nCyc] = FletcherReeves(h,tol)
% Fletcher-Reeves method
% h = initial search increment = 0.1
% tol= error tolerance = 1.0e-6
% X = starting point
% FUNC = handle of function that returns f
% DFUNC = handle of function that returns grad(f)
% xMin = minimum point
% fMin = miminum value of f
% nCyc = # of cycles to convergence
global X FUNC DFUNC V
if nargin < 2; tol = 1.0e–6; end
if nargin < 1; h = 0.1; end
if size(X,2) > 1; X = X’; end
n = length(X);
g0 = –feval(DFUNC,X);
V = g0;
for i = 1:50