# How can I find maximum of this function?

I am trying to find $$m$$ so that maximum of the function $$f(x) = |x^3 - 3 x + m |$$, $$x \in [0,3]$$ equal to 16. I work aroud by using the note $$\max\{a,b\} = \dfrac{a + b + |a-b|}{2}.$$ I tried

Clear[f, a, b, max, min]
f[x_] := x^3 - 3 x + m;
a = 1;
b = 3;
max = (Abs[f[a]] + Abs[f[b]] + Abs[Abs[f[a]] - Abs[f[b]]])/2;
m /. Solve[ max == 16, m, Reals]


How can I get the correct answer without working around?

• Have you seen the result of MaxValue[{x^3 - 3 x + m, 0 <= x <= 3}, x]? May 20, 2020 at 12:01
• @J.M. Please see edit. May 20, 2020 at 12:03
• Then, look at the result of MaxValue[{Sqrt[(x^3 - 3 x + m)^2], 0 <= x <= 3}, x]. May 20, 2020 at 12:06
• @J.M. I saw my wrong. May 20, 2020 at 13:20
• In that case, please feel free to write an answer to your own question, so it can be settled. May 20, 2020 at 13:23

## 2 Answers

I'm afraid part of the problem might be that you are focusing too much on how to reformulate the maximum in a way that can be used with Solve (and also that you have presupposed knowing that $$x=1$$ is relevant). To confirm, the two values of $$m$$ that you want are indeed $$m=-2$$ and $$m=-14$$, which is what your code produces.

Clear[f, a, b, max, min]
f[x_] := x^3 - 3 x + m;
a = 1;
b = 3;
max = (Abs[f[a]] + Abs[f[b]] + Abs[Abs[f[a]] - Abs[f[b]]])/2;
m /. Solve[max == 16, m, Reals]

(* Output: {-14,-2} *)


Let's talk a little bit about how this works in the code first. Your "work around" is not necessary, as noted by J.M. in the comments, but it is certainly one way to do it.

One way to use built-in functions more clearly is to consider that you have a very simple function -- the absolute value of a low degree polynomial. There absolute can only be two possible things, either $$|x^3-3x+m|=x^2-3x+m$$ or else $$|x^3-3x+m|=-(x^3-3x+m)$$.

That means either $$\max f(x) = x^3-3x+m$$ or else $$\max f(x) = -(x^3-3x+m)$$, for the particular value of $$x$$. The latter would be the same as $$-\min ( x^3-3x+m )$$ in the case where $$\min f(x)$$ is so low that in the absolute value, it is higher than other potential maxima of $$f(x)$$.

So the maximum value of $$|x^3-3x+m|$$ is either a maximum value of $$x^3-3x+m$$ or a minimum value of $$x^3-3x+m$$ (which we then negate to get a relatively large positive value). This provides us with the ability to cut right to the chase:

Maximize[{x^3 - 3 x + m, 0 <= x <= 3}, x]
(* Output: {18+m,{x->3}} *)
Minimize[{x^3 - 3 x + m, 0 <= x <= 3}, x]
(* Output: {-2+m,{x->1}} *)


This is, indeed, your "a" and "b" in the question, showing as x->3 and x->1. (Although, as written, you use $$f(a)$$ in place of $$a$$ at some point.)

Now, I always want tangible verification of what I'm seeing, so try this:

Manipulate[
Plot[{16, Abs[x^3 - 3 x + m]},
{x, 0, 3},
PlotRange -> {-25, 25}
],
{m, -20, 20}
]


Here is what that looks like for $$m=-2$$ and $$m=-14$$ as still images:

This verifies that these are the correct values of $$m$$.

We can also delve deeper into why these are the correct values of $$x$$ to be checking, for example if we were not able to use Maximize or a similar command. Because our function is $$|x^3-3x+m|$$, its derivative will the same as the derivative of $$x^3 -3x+m$$. We can look for critical points wherever the cubic has derivative zero:

Solve[D[x^3 - 3 x + m, x] == 0]
(* Output: {{x->-1},{x->1}} *)


Now, $$|x^3-3x+m|$$ also has critical points at its non-smooth points, but those are points where $$|x^3 - 3 x + m|=0$$, which will not be maxima (they are, indeed, the minima).

Now, you have one critical point within your interval $$x\in [0,3]$$, $$x=-1$$, that could be the maximum (along with wherever $$f(x)=0$$, critical points we know are the minima, so we can ignore them). From calculus, we know a continuous function obtains its extreme value(s) either at critical points (assuming the critical points are discrete) or else at the endpoints. Our candidates are $$x=0$$, $$x=1$$, and $$x=3$$.

But clearly $$f(0)=|m|$$, $$f(1)=|m-2|$$, and $$f(3)=|m+18|$$. That means one of these should be the maximum, depending on $$m$$. You can plot these:

Plot[{Abs[m], Abs[m - 2], Abs[m + 18]}, {m, -20, 20}]


You can see here (and verify) that the largest of these (which will be the value of $$f(x)$$ itself, at the $$x$$ value that gives the maximum) is $$|m+18|$$ for $$m>-8$$ and $$|m-2|$$ for $$m<-8$$.

Solve[Abs[m + 18] == Abs[m - 2]]
(* Output: {{m->-8}} *)


We can determine that $$|m+18|=16$$ if $$m=-2$$ and $$m=-34$$, while $$|m-2|=16$$ if $$m=-14$$ or $$m=18$$.

Note: The solutions $$m=18$$ and $$m=-34$$ are instances where $$f(x)$$ is entirely positive on $$[0,3]$$ and is minimum values (not maximum values) occur at $$x=1$$ or $$x=3$$, respectively.

So if you wanted to pack up your "work around" in a way that: (1) doesn't use this formula for maximum and (2) doesn't rely on knowing in advance the critical value of $$f(x)$$, you can try this, which relies on the (relatively new and extremely helpful) function RealAbs. Here I will use a and b for the endpoints of the interval, rather than the two key values of $$x$$ (so they will be 0 and 3, rather than 1 and 3 in your code).

Clear[f, x, m, a, b, critpts, points, mymax]
a = 0; b = 3;
f[x_] = RealAbs[x^3 - 3 x + m]
critpts = ReplaceAll[x, Solve[f'[x] == 0, x, Reals]]
points = Sort[Join[{0, 3}, Select[critpts, a < # < b &]]]
mymax[m_] = Max[f /@ points]
Solve[mymax[m] == 16, m]
(* output: {{m->-14},{m->-2}}


And that's a quick and easy distillation of the discussion above.

• That's a lovely analysis. :) May 20, 2020 at 16:19
• A few additional thoughts: If you want to see the extraneous cases $m=18$ and $m=-34$, you can do so with the Manipulate, but you'll need to change the range of m to include $m=-34$ and you might also want to increase the top end of the PlotRange. I would suggest Manipulate[ Plot[{16, Abs[x^3 - 3 x + m]}, {x, 0, 3}, PlotRange -> {-5, 50}], {m, -50, 50}]. Also, the plot with the three Abs functions could include a helpful option: PlotLegends -> "Expressions" (and this plot may also benefit from some style options and even the inclusion of the constant function 16 for reference). May 20, 2020 at 16:28

Thanks to J. M.'s technical difficulties' comment. My code is

Clear[f, a, b, max, min]
f[x_] = x^3 - 3 x + m;
a = MinValue[f[x], 0 <= x <= 3, x];
b = MaxValue[f[x], 0 <= x <= 3, x];
mymax = Simplify[(Abs[a] + Abs[b] + Abs[Abs[a] - Abs[b]])/2];
m /. Solve[ mymax == 16, m, Reals]