# Minimizing a conditional function with parameters

Is there a way to find the minimum of a piecewise functions with parameters?

Here is a simple example:

Minimize[{If[x > 0, 3 x, x^2 - x - 4]}, x]


works fine, but

Minimize[{If[x > 0, 3 x, x^2 - x - a], -2 < a < 2}, x]


doesn't work at all.

Is there some way to make it work? Or can anyone explain why is not working or suggest other methods to get the minimum?

### Edit 1

The answer I would like to get is something like:

If[a < 0, 0, -a]


### Edit 2

When I try

Minimize[{If[x > 0, 3 x, x^2 - x - a], -2 < a < 2}, x]


I get as output:

Minimize[{If[x > 0, 3 x, x^2 - x - a], -2 < a < 2}, x]


I'm using V10.1.0 for Linux x86 (64-bit) (March 24, 2015)"

• It's not clear to me what you aim at. I would suggest to use Minimize[{If[x > 0, 3 x, x^2 - x - a], -2 < a < 2}, {x, a}] instead. But since Mathematica tells me that there is no minimum, I am in doubt that this is what you are looking for... – Henrik Schumacher Feb 24 '18 at 14:43
• It works for me: i.stack.imgur.com/oMzoE.png -- So does the 2nd one: i.stack.imgur.com/BBeRX.png – Michael E2 Feb 24 '18 at 14:48
• Perhaps you're looking for something like Simplify[Minimize[{If[x > 0, 3 x, x^2 - x - a], 1 < a < 2}, x], 1 < a < 2]? – Michael E2 Feb 24 '18 at 14:51
• Thank you Michael, maybe the problem is that i have an older version? I got this : "10.1.0 for Linux x86 (64-bit) (March 24, 2015)" – arsik87 Feb 24 '18 at 14:52
• What do you get for output? (It might help to give the output and version details in the question.) I can't try V10.1, but maybe use PiecewiseExpand@If[x > 0, 3 x, x^2 - x - a] for your function. – Michael E2 Feb 24 '18 at 14:55

In versions before M11, maybe you can try something like the following:

minimize[{If[pred_, true_, false_], cond_}, x_] := Simplify[
Min[
First @ Minimize[{true, pred&&cond},x],
First @ Minimize[{false, !pred&&cond},x]
],
cond
]


The function needs error checking to be robust, but it works fine for your example (in M10.0):

minimize[{If[x > 0, 3 x, x^2 - x - a], -2 < a < 2}, x]


Min[0, -a]

A more general version that works for Piecewise expressions is also possible.

• thank you so much! is working !!! :) – arsik87 Feb 24 '18 at 16:22
\$Version

(* "10.0 for Mac OS X x86 (64-bit) (December 4, 2014)" *)

Clear[f]

f[x_, a_] = PiecewiseExpand@If[x > 0, 3 x, x^2 - x - a]

(* Piecewise[{{3*x, x > 0}}, -a - x + x^2] *)

Plot3D[f[x, a], {x, -3, 3}, {a, -2, 2}] Minimize[{f[x, a], -2 < a < 2}, {x, a}]

(* Minimize::wksol: Warning: there is no minimum in the region in which the
objective function is defined and the constraints are satisfied; returning a
result on the boundary. >>

{-2, {x -> 0, a -> 2}} *)

• Thank you so much for the answer! But this is not what I was looking for... I would like to find the minimum as a function of the parameter "a", while, as I understood, in your answer you consider "a" as another variable.. – arsik87 Feb 24 '18 at 15:27

Applying the minimization piecewise gives the expected result

Minimize[{3 x, x > 0}, x] (* {0, {x -> 0}}*)
Minimize[{x^2 - x - a, x <= 0}, x](*{-a, {x -> 0}} *)