Page 306 - MATLAB an introduction with applications
P. 306
Optimization ——— 291
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
Example E5.11: Find the minimum point of the following objective function f(x) using quadratic approximation
method.
2
(x − 5)
() =
fx 2 − 1
8
Solution:
>> clear,clf
>> f323=inline(‘(x.*x–5).^2/8–1’, ‘x’);
>> a=0;b=3;TolX=1e–6;TolFun=1e–9;MaxIter=100;
>> [x0q,f0q]=opt_quad(f323,[a,b],TolX,TolFun,MaxIter)
x0q =
2.2361
f0q =
–1.0000
>> % x0q= minimum point and f0q = its function value in above
>> [x0q,f0q]=fminbnd(f323,a,b) % MATLAB built-in function
x0q =
2.2361
f0q =
–1.0000
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);