1%% Optimization of a simple (Rosenbrock) function, with no constraints
2rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;
4% With no constraints, operation simply passes through
5% directly to fminsearch. The solution should be [1 1]
6xsol = fminsearchbnd(rosen,[3 3])
8%% Only lower bound constraints
9xsol = fminsearchbnd(rosen,[3 3],[2 2])
11%% Only upper bound constraints
12xsol = fminsearchbnd(rosen,[-5 -5],[],[0 0])
15xsol = fminsearchbnd(rosen,[2.5 2.5],[2 2],[3 3])
18xsol = fminsearchbnd(rosen,[0 0],[2 -inf],[inf 3])
20%% Provide your own fminsearch options
21opts = optimset(
'fminsearch');
24opts.MaxFunEvals = 100;
29Quadraticfun = @(x) x*H*x';
31% Global minimizer
is at [0 0 0 0 0].
32% Set all lower bound constraints, all of which will
33% be active in
this test.
35xsol = fminsearchbnd(Quadraticfun,[1 2 3 4 5],LB,[],opts)
37%% Exactly fix one variable, constrain some others, and set a tolerance
38opts = optimset(
'fminsearch');
43xsol = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB,opts)
45%% All the standard outputs from fminsearch are still returned
46[xsol,fval,exitflag,output] = fminsearchbnd(@(x) norm(x),[1 3 1 1],LB,UB)