Page 309 - MATLAB an introduction with applications
P. 309

294 ———  MATLAB: An Introduction with Applications

                   Solution:
                   First we create an .m file to hold the function
                   function f=fx(x)
                   f=–(8*sin(x)–x^2/14)
                   The negative sign is for minimization.
                   >> x=fminbnd(‘fx’, 0,7)
                   The result from MATLAB program is
                   f =
                        –7.8268
                   x =
                         1.5432

                                                                                                        2
                                                                                                    2
                   Example E5.14: Use the MATLAB fminsearch function to find the maximum of f (x, y) = 3xy + 8x – x  – 9y ,
                   using initial guesses, x = –1 and y = 1.
                   Solution:
                   Create an m file to hold the function
                   function f=fxy(x)
                   f=–(3*x(1)*x(2)+8*x(1)–x(1)^2–9*x(2)^2)
                   Invoke the fminsearch function with

                   >> x=fminsearch(‘fxy’,[–1,1])
                   f =
                        –21.3333
                   x =
                          5.3334      0.8889

                   Examples E5.15 to E5.20 are based on Tutorials and Demos from MATLAB (The Mathworks, Inc.)
                   Example E5.15: This is an unconstrained minimization example. Find the set of values [x , x ] that solve
                                                                                               2
                                                                                            1
                                  min ( ) x =  e  1 x  (4x + 2x + 4x x + 2x + 0.9)
                                                     2
                                                2
                                     f
                                   x            1    2    1 2   2
                                 0
                   Starting guess: x  = [–1, 1].
                   Solution:
                   MATLAB Solution [Using built-in function]:
                   Write an m-file objfun.m
                   function f=objfun(x)
                   f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+0.9);

                   Write one of the unconstrained optimization routines:
                   >> x0=[–1,1];  % Starting guess at the solution
                   >> options=optimset(‘LargeScale’, ‘off’);
                   >> [x,fval,exitflag,output]=fminunc(@objfun,x0,options)
   304   305   306   307   308   309   310   311   312   313   314