% Example 6.1 function [y]=banana(x) global z1 z2 yg count y=100*(x(2)-x(1)^2)^2+(1-x(1))^2; z1(count)=x(1); z2(count)=x(2); yg(count)=y; count=count+1; end % global z1 z2 yg count count =1; options=optimset('MaxFunEvals',20) [b,fval] = fminsearch(@banana,[-1.2, 1],options) mat=[z1;z2;yg] %----------------------------------------------------------------------------- % % Example 6.2 function value = gp(x) x1 = x(1); x2 = x(2); value =(1+(x1+x2+1).^2.*(19-14.*x1+3.*x1.^2-14.*x2+6.*x1.*x2+3.*x2.^2))... .*(30+(2.*x1-3.*x2).^2.*(18-32.*x1+12.*x1.^2+48.*x2-36.*x1.*x2+27.*x2.^2)); end % clear all; bounds = [-2 2;-2 2]; % lower- and upper-bounds of design options.testflag = 1; % Global minimum is known options.globalmin = 3; % The value of known global min options.showits = 1; % Show iteration history options.tol = 0.01; % Stopping criterion Problem.f = 'gp'; % Objective function name [fmin,xmin,hist] = Direct(Problem,bounds,options); % Calling DIRECT algorithm plot(hist(:,2),hist(:,3)) % Plot iteration statistics xlabel('Fcn Evals'); ylabel('f_{min}'); %----------------------------------------------------------------------------- % % Example 6.6 function y = Rosenbrock(x) y = 100 * (x(1)^2 - x(2))^2 + (1 - x(1))^2; end % rng default % For reproducibility ndv = 2; lb = [-3,-3]; ub = [3,3]; [x,fval] = ga(@Rosenbrock,ndv,[],[],[],[],lb,ub) % options = optimoptions('ga','MaxGenerations',10000); [x,fval] = ga(@Rosenbrock,ndv,[],[],[],[],lb,ub,[],[],options) %----------------------------------------------------------------------------- % % Example 6.7 rng default % For reproducibility nvars = 2; lb = [-3,-3]; ub = [3,3]; options = optimoptions('particleswarm','SwarmSize',10); [x,fval,exitflag,output] = particleswarm(@Rosenbrock,nvars,lb,ub,options) %----------------------------------------------------------------------------- % % Example 6.8 function f = objfun(x) x1 = x(1); x2 = x(2); f = (4-2.1.*x1.^2+x1.^4./3).*x1.^2+x1.*x2+(-4+4.*x2.^2).*x2.^2; end % x0 = [0.5 0.5]; % Starting point lb = [-64 -64]; ub = [64 64]; rng default % For reproducibility [x,fval,exitFlag,output] = simulannealbnd(@objfun,x0,lb,ub) %-----------------------------------------------------------------------------