1%% Optimization of a simple (Rosenbrock) function, with no constraints
2% The unconstrained solution
is at [1,1]
3rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;
5% With no constraints, operation simply passes through
6% directly to fminsearch. The solution should be [1 1]
7xsol = fminsearchbnd(rosen,[3 3])
9%% Full lower and upper bound constraints which will all be inactive
10xsol = fminsearchbnd(rosen,[3 3],[-1 -1],[4 4])
12%% Only lower bound constraints
13xsol = fminsearchbnd(rosen,[3 3],[2 2])
15%% Only upper bound constraints
16xsol = fminsearchbnd(rosen,[-5 -5],[],[0 0])
19xsol = fminsearchbnd(rosen,[2.5 2.5],[2 2],[3 3])
21%% Dual constraints, with an infeasible starting guess
22xsol = fminsearchbnd(rosen,[0 0],[2 2],[3 3])
25xsol = fminsearchbnd(rosen,[0 0],[2 -inf],[inf 3])
27%% Provide your own fminsearch options
28opts = optimset(
'fminsearch');
35Quadraticfun = @(x) x*H*x';
37% Global minimizer
is at [0 0 0 0 0].
38% Set all lower bound constraints, all of which will
39% be active in
this test.
41xsol = fminsearchbnd(Quadraticfun,[1 2 3 4 5],LB,[],opts)
43%% Exactly fix one variable, constrain some others, and set a tolerance
44opts = optimset(
'fminsearch');
49xsol = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB,opts)
51%% All the standard outputs from fminsearch are still returned
52[xsol,fval,exitflag,output] = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB)