6

enter image description here

Why is the Out[74] not {1 - a, {x -> -1}}?

 Maximize[{Sqrt[x^2] - a, -1 <= x <= 1}, x]
{1 - a, {x -> -1}}
Kuba
  • 136,707
  • 13
  • 279
  • 740
fptan
  • 61
  • 2
  • Please post germane code, not pictures... – ciao Jun 19 '14 at 04:17
  • 1
    Plot it. Think about it. Then try Maximize[{(Abs[x] - a), -1 <= x <= 1}, {x, a}] and see what you get. It is not a but. – ciao Jun 19 '14 at 04:25
  • 2
    @rasher I may be overlooking something obvious, but isn't the maximum always going to be 1 - a? It's not clear to me why MMA would return a symbolic maximum in the other instances and not here. – mfvonh Jun 19 '14 at 04:47
  • I think it is a bug but it works it you assign an exact number to a – mastrok Jun 19 '14 at 06:09
  • In[1]:= Maximize[{(Abs[x] - a), -1 <= x <= 1}, {x, a}]

    Out[1]= {[Infinity], {x -> Indeterminate, a -> Indeterminate}}

    – fptan Jun 19 '14 at 09:14
  • In[2]:= Maximize[{Abs[x] - a, -1 <= x <= 1}, x]

    Out[2]= Maximize[{-a + Abs[x], -1 <= x <= 1}, x]

    – fptan Jun 19 '14 at 09:15
  • In[3]:= Maximize[{Abs[x] - a, -1 <= x <= 1}, x, Reals]

    Out[3]= Maximize[{-a + Abs[x], -1 <= x <= 1}, x, Reals]

    – fptan Jun 19 '14 at 09:16
  • Everyone. Thank you for your help! – fptan Jun 19 '14 at 09:28
  • 2
    Sometimes Mathematica cannot solve a problem. I think only the programmers working on this function would be able to decide whether this is a bug (i.e. it doesn't give an answer because something goes wrong an an unintended way inside the function) or it's just something Mathematica can't do. – Szabolcs Jun 19 '14 at 15:00

1 Answers1

6

I suspect the reason this fails is because Mathematica does not know the derivative of Abs[x]. The reason behind this is Abs[x] is nowhere differentiable in the complex plane and Derivative and D take complex derivatives.

In Wolfram Alpha, we had to handle Abs ourselves assuming real variables since most people would want that. If you run WolframAlpha["derivative of Abs[x]"], you'll see the step-by-step solution specifies x is a real variable.

Since Derivative is not a protected symbol, we can add custom SubValues:

Derivative[1][Abs] = Sign;
Derivative[2][Abs] = Derivative[1][Sign] = DiracDelta;

Now Mathematica knows the (real) derivatives of these functions:

D[Abs[x], x]
(* Sign[x] *)

D[Abs[x], {x, 2}]
(* DiracDelta[x] *)

D[Sign[x], x]
(* DiracDelta[x] *)

Unfortunately this does not fix your Maximize issue:

Maximize[{Abs[x] - a, -1 <= x <= 1}, x]
(* Maximize[{-a + Abs[x], -1 <= x <= 1}, x] *)
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136