Page 305 - MATLAB an introduction with applications
P. 305

290 ———  MATLAB: An Introduction with Applications


                   >> [x0_u,f0_u]=fminunc(f,x0) % MATLAB built-in fminunc
                   Warning: Gradient must be provided for trust-region method;
                     using line-search method instead.
                   > In fminunc at 243
                   Optimization terminated: relative infinity-norm of gradient less than
                   options.TolFun.
                   x0_u =
                          0.4867    –0.1888
                   f0_u =
                          10.5382
                   >> [fc_u,f0_u,c0_u]=f322p(x0_u) % its results
                   fc_u =
                          10.5382
                   f0_u =
                          8.8627
                   c0_u =
                          –0.9119
                          0.5161

                   function [fc,f,c]=f322p(x)
                   f=(x(1)^2+x(2)^2–6*x(1)–8*x(2)+10);
                   c=[–4*x(1)^2+x(2)^2;  3*x(1)+5*x(2)]; % Constraint vector
                   v=[1 1];e=[1 1 ]’;% Weighting coefficient vector
                   fc=f+v*((c>0).*exp(e.*c)); %  New objective function

                   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); 78ikol
   300   301   302   303   304   305   306   307   308   309   310