fminsearch with output constraint

109 views (last 30 days)
gaia buratti
gaia buratti on 28 Jul 2011
Hi, I should minimize a function I created respect to two parameters, but I want one of the two to be positive. I don't think I have to use fmincon, i think fminsearch is enough but I don't know how to put this constraint.
May someone help me?
Thanks in advance Gaia

Answers (2)

Matt Tearle
Matt Tearle on 28 Jul 2011
But by definition fminsearch is an unconstrained method, so you can't provide a constraint. If there's some reason you can't use fmincon I guess you could try a hack like adding a penalty to your objective function -- ie newf(x) = f(x) + penalty(x) where penalty(x) is a huge number if x(1)<0 and zero otherwise (or some continuous version of that, if the discontinuity gives you problems).
As a separate issue, when you say "I want one of the two to be positive", do you mean a specific parameter has to be positive or that x(1) OR x(2) should be positive (ie it doesn't matter which one, as long as at least one is)?
  3 Comments
Walter Roberson
Walter Roberson on 28 Jul 2011
Why not use fmincon() with a lb parameter of minreal for that parameter (to ensure that it is positive but not rule out very small numbers) ?
Matt Tearle
Matt Tearle on 28 Jul 2011
fmincon can be a bit intimidating because it's so general, but it's not so bad...
ub = Inf(size(par1));
lb = -ub;
lb(1) = realmin;
x = fmincon(fun,x0,[],[],[],[],lb,ub)
fun is your objective function (@K_S2) and x0 is your initial guess (par01). The four empty arrays ([]) are placeholders for constraints you don't need to specify for your problem; then you specify lower and upper bounds, most of which are +/- infinity (ie unbounded). The lower bound on the first parameter is realmin, the smallest positive number MATLAB can represent. As Walter alludes to, this is because the bounds are technically interpreted as lb <= x, so if you use a lower bound of 0, you might get par(1) = 0.

Sign in to comment.


DrCommando
DrCommando on 21 Mar 2013
a simple trick is to make the search interval periodic around zero. so whenever fminsearch drops a negative parameter multiply by (-1).
a different but maybe more dangerous solution is to return huge errors if parameters leave the desired range - however, if you have problems that are non-analytic outside a desired parameter-range you may face problems with this work-around.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!