# The maximum value under the condition of complex constraint

Let a<0, (3x^2+a)(2x+b)>=0 be permanent established on (a, b), and what is the maximum value of b-a?

Code 1

    MaxValue[{b - a,
a < 0 && (3 x^2 + a) (2 x + b) >= 0 && a < x < b}, {x, a, b}]


The result of the return is infinity, but the answer should be 1/3

Code2

    MaxValue[{b - a,
ForAll[{x}, a < 0 && a < x < b, (3 x^2 + a) (2 x + b) >= 0]}, {a,
b}]


But no return to the result.I want to know how to solve this problem by Mathematica.

Start by minimizing the expression you want to be non-negative:

min = Minimize[{(3 x^2 + a) (2 x + b), a <= x <= b, a < 0}, x]


Which returns a piecewise function for the minimum value. One of the entries are
1/192 (-1 - 48 a) $\quad$ if $\quad$ b == -(1/12) && -(1/8) < a < -(1/12)

Within that case the optimal value for you problem can be found by constraining the minimum to be positive:

Maximize[{b - a,
1/192 (-1 - 48 a) >= 0,
b == -(1/12) && -(1/8) < a < -(1/12),
a < 0}, {a, b}]

{1/24, {a -> -(1/8), b -> -(1/12)}}


The code below does the same thing for all entries of the piecewise minimum:

opts = Map[
Maximize[{b - a, #[[1]] >= 0, #[[2]], a < 0}, {a, b}] &,
FullSimplify[min[[1, 1]], {a < 0}]];


The last case (0 if True) of the piecewise minimum remains, but True is only correct given that all the previous conditions were False. Combining all the conditions we get:

lastCond = FullSimplify[Not[Or @@ min[[1, 1, All, 2]]], a < 0] && a < 0 // FullSimplify

b == 0 && -(1/3) <= a < 0


Using that we can find the optimum within the last case and add it to the other:

opts = Append[opts,
Maximize[{b - a, min[[1, 2]] >= 0, lastCond, a < 0}, {a, b}]]


There have been 2 kinds of error messages: Some contraints correspond to the empty set, which makes Maximize return -∞. Also sometimes the closure was used in order to return a maximum.

Below the emptyset optimizations are removed and the remaining are sorted:

SortBy[Select[opts, First[#] =!= -∞ &], N@*Minus@*First] // DeleteDuplicates

{{1/3, {a -> -(1/3), b -> 0}}, {1/4, {a -> -(1/3), b -> -(1/12)}},
{1/24, {a -> -(1/8), b -> -(1/12)}}, {0, {a -> -(1/12), b -> -(1/12)}}}


and despite the closures that were taken, the largest optimum gives non-negativity:

Plot[(3 x^2 + a) (2 x + b) /. {a -> -(1/3), b -> 0}, {x, -1/3, 0}]