Page 307 - MATLAB an introduction with applications
P. 307

292 ———  MATLAB: An Introduction with Applications


                   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

                   Example E5.12: Find the minimum point of the following objective function f (x) using quadratic approxi-
                   mation method.
                   Solution:
                   >> clear,clf
                   >> f324=inline(‘(x.*x+3).^2/10+exp(x)–7’,’x’);
                   >> a=0;b=3;TolX=1e–6;TolFun=1e–9;MaxIter=100;
                   >> [x0q,f0q]=opt_quad(f324,[a,b],TolX,TolFun,MaxIter)
                   x0q =
                          –0.4793
                   f0q =
                          –5.3377
                   >> % x0q= minimum point and f0q = its function value
                   >> [x0q,f0q]=fminbnd(f324,a,b) % MATLAB built-in function
   302   303   304   305   306   307   308   309   310   311   312