fminsearchcon_demo

PURPOSE ^

% Optimization of a simple (Rosenbrock) function

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

% Optimization of a simple (Rosenbrock) function

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %% Optimization of a simple (Rosenbrock) function
0002 rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;
0003 
0004 % With no constraints, operation simply passes through
0005 % directly to fminsearch. The solution should be [1 1]
0006 xsol = fminsearchcon(rosen,[3 3])
0007 
0008 %% Only lower bound constraints
0009 xsol = fminsearchcon(rosen,[3 3],[2 2])
0010 
0011 %% Only upper bound constraints
0012 xsol = fminsearchcon(rosen,[-5 -5],[],[0 0])
0013 
0014 %% Dual constraints
0015 xsol = fminsearchcon(rosen,[2.5 2.5],[2 2],[3 3])
0016 
0017 %% Mixed constraints
0018 xsol = fminsearchcon(rosen,[0 0],[2 -inf],[inf 3])
0019 
0020 %% Fix a variable as constant, x(2) == 3
0021 fminsearchcon(rosen,[3 3],[-inf 3],[inf,3])
0022 
0023 %% Linear inequality, x(1) + x(2) <= 1
0024 fminsearchcon(rosen,[0 0],[],[],[1 1],1)
0025 
0026 %% Nonlinear inequality, norm(x) <= 1
0027 fminsearchcon(rosen,[0 0],[],[],[],[],@(x) norm(x) - 1)
0028 
0029 %% Minimize a linear objective, subject to a nonlinear constraints.
0030 fun = @(x) x*[-2;1];
0031 nonlcon = @(x) [norm(x) - 1;sin(sum(x))];
0032 fminsearchcon(fun,[0 0],[],[],[],[],nonlcon)
0033 
0034 %% Provide your own fminsearch options
0035 opts = optimset('fminsearch');
0036 opts.Display = 'iter';
0037 opts.TolX = 1.e-12;
0038 opts.MaxFunEvals = 100;
0039 
0040 n = [10,5];
0041 H = randn(n);
0042 H=H'*H;
0043 Quadraticfun = @(x) x*H*x';
0044 
0045 % Global minimizer is at [0 0 0 0 0].
0046 % Set all lower bound constraints, all of which will
0047 % be active in this test.
0048 LB = [.5 .5 .5 .5 .5];
0049 xsol = fminsearchcon(Quadraticfun,[1 2 3 4 5],LB,[],[],[],[],opts)
0050 
0051 %% Exactly fix one variable, constrain some others, and set a tolerance
0052 opts = optimset('fminsearch');
0053 opts.TolFun = 1.e-12;
0054 
0055 LB = [-inf 2 1 -10];
0056 UB = [ inf  inf 1  inf];
0057 xsol = fminsearchcon(@(x) norm(x),[1 3 1 1],LB,UB,[],[],[],opts)
0058 
0059 %% All the standard outputs from fminsearch are still returned
0060 [xsol,fval,exitflag,output] = fminsearchcon(@(x) norm(x),[1 3 1 1],LB,UB)
0061

Generated on Thu 10-Dec-2020 17:34:27 by m2html © 2005