# Numerically minimize function with limited domain

I would like to minimize a simple function that is defined only for a certain range of the independent variable. The function has only one minimum. To illustrate, let's take a parabola defined for $x\in [17,22]$. In mathematica, I tried

NMinimize[
Piecewise[{{0.01 (x - 19.5)^2 + 16, x > 17 && x < 22}, {Exp[10000],
True}}], x, Method -> "DifferentialEvolution"]
(* {8.80681822566*10^4342, {x -> -6.96782}} *)


The result is apparently nonsense. It doesn't seem to matter which Method I use. I guess the issue is the way how I define the forbidden points (by setting their values to Exp[10000]). Is there a robust way to exclude points or generally speaking a more robust way to find the minimum of a function with limited domain, knowing that the function has definitely just one minimum?

• You have to add a constraint: NMinimize[{func, 17 < x < 22}, x]. Oct 5, 2016 at 19:33
• To see why giving NMinimize some notion of where to look is needed, examine the x's that are tested in your code: ListPlot@Last@Reap@NMinimize[Piecewise[{{0.01 (x - 19.5)^2 + 16, x > 17 && x < 22}, {Exp[10000], True}}], x, Method -> "DifferentialEvolution", EvaluationMonitor :> Sow[x]] -- the x's appear as second (vertical) coordinate). The EvaluationMonitor option can be helpful in situations like these. Oct 5, 2016 at 20:03
• I used to work with constraints, but that had it's own issues, see mathematica.stackexchange.com/questions/127554/… . Due to the suggestion in that question, I switched to defining constraints via the piecewise function. Probably what I need is a way to use the "NonlinearInteriorPoint" function as suggested in mathematica.stackexchange.com/questions/69622/… where, unfortunately, no syntax is provided to use the function. Oct 6, 2016 at 0:57