Page 308 - MATLAB an introduction with applications
P. 308
Optimization ——— 293
x0q =
5.6302e–005
f0q =
–5.0999
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);
else a=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)
if k<=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
x 2
Example E5.13: Use the MATLAB fminbnd function to find the maximum of f (x) = 8 sin x – with the
14
interval x = 0 and x = 7.
u