Page 335 - Applied Numerical Methods Using MATLAB
P. 335
324 OPTIMIZATION
We keep updating the three points this way until |x 2 − x 0 |≈ 0 and/or |f(x 2 ) −
f(x 0 )|≈ 0, when we stop the iteration and declare x 3 as the minimum point.
The rule for updating the three points is as follows.
1. In case x 0 <x 3 <x 1 ,we take {x 0 ,x 3 ,x 1 } or {x 3 ,x 1 ,x 2 } as the new set of
three points depending on whether f(x 3 )<f (x 1 ) or not.
2. In case x 1 <x 3 <x 2 ,we take {x 1 ,x 3 ,x 2 } or {x 0 ,x 1 ,x 3 } as the new set of
three points depending on whether f(x 3 ) ≤ f(x 1 ) or not.
This procedure, called the quadratic approximation method, is cast into the
MATLAB routine “opt_quad()”, which has the nested (recursive call) structure.
We made the MATLAB program “nm712.m”, which uses this routine to find the
minimum point of the objective function (7.1.1) and also uses the MATLAB
built-in routine “fminbnd()” to find it for cross-check. Figure 7.2 shows how
the routine “opt_quad()” proceeds toward the minimum point step by step.
(cf) The MATLAB built-in routine “fminbnd()” corresponds to “fmin()”inthe MAT-
LAB of version.5.x.
function [xo,fo] = opt_quad(f,x0,TolX,TolFun,MaxIter)
%search for the minimum of f(x) by quadratic approximation method
if length(x0) > 2, x012 = x0(1:3);
else
if length(x0) == 2, a = x0(1); b = x0(2);
elsea=x0 - 10;b=x0+10;
end
x012 = [a (a + b)/2 b];
end
f012 = f(x012);
[xo,fo] = opt_quad0(f,x012,f012,TolX,TolFun,MaxIter);
function [xo,fo] = opt_quad0(f,x012,f012,TolX,TolFun,k)
x0 = x012(1); x1 = x012(2); x2 = x012(3);
f0 = f012(1); f1 = f012(2); f2 = f012(3);
nd = [f0 - f2 f1 - f0 f2 - f1]*[x1*x1 x2*x2 x0*x0; x1 x2 x0]’;
x3 = nd(1)/2/nd(2); f3 = feval(f,x3); %Eq.(7.1.4)
ifk<=0 | abs(x3 - x1) < TolX | abs(f3 - f1) < TolFun
xo = x3; fo = f3;
if k == 0, fprintf(’Just the best in given # of iterations’), end
else
if x3 < x1
if f3 < f1, x012 = [x0 x3 x1]; f012 = [f0 f3 f1];
else x012 = [x3 x1 x2]; f012 = [f3 f1 f2];
end
else
if f3 <= f1, x012 = [x1 x3 x2]; f012 = [f1 f3 f2];
else x012 = [x0 x1 x3]; f012 = [f0 f1 f3];
end
end
[xo,fo] = opt_quad0(f,x012,f012,TolX,TolFun,k - 1);
end