# Finding maximum value of a function with parameter

How can I find the maximum value (and the corresponding $$x$$) of the following function with parameter $$t$$: $$f(x) = |x^2-tx + 2t|$$

on a closed interval $$x\in[0,1]$$?

I tried using Maximize, but it doesn't give a result:

f[x_] := Abs[x^2 - t x + 2 t]
Maximize[{f[x], 0 <= x <= 1}, x]
(* Maximize[{Abs[2 t - t x + x^2], 0 <= x <= 1}, x] *)

• how about {t->-(3/2)} ? Commented Aug 15, 2023 at 9:54
• @Nasser t==3/2||t==-3/2 Commented Aug 15, 2023 at 10:07

Indicating that t is real, one succeeds

Solve[Maximize[{RealAbs[2 t - t*x + x^2],
0 <= x <= 1 && t >= -Infinity}, x][[1]] == 3]


{{t -> -(3/2)}, {t -> 3/2}}

• If I change the t to look like thist \[Element] Reals why can't I find the value? Commented Aug 15, 2023 at 10:45
• @csn899: Sorry, don't understand. Please, present the whole code. Commented Aug 15, 2023 at 10:58
• I got your remark: Solve[Maximize[{RealAbs[2 t - t*x + x^2], 0 <= x <= 1 && t \[Element] Reals}, x][[1]] == 3, t] produces "Solve::nsmet: This system cannot be solved with the methods available to Solve". Don't know the reason. Commented Aug 15, 2023 at 11:05

Abs is a Complex Function. We use Sqrt[x^2] instead.

sol = Maximize[{Sqrt[(x^2 - t*x + 2 t)^2], 0 <= x <= 1}, x]
Plot[{sol[[1]], x /. sol[[2]]}, {t, -5, 5}]


• Sqrt[(x^2 - t*x + 2 t)^2] is nothing, but RealAbs[x^2 - t*x + 2 t]. FullSimplify[Sqrt[(x^2 - t*x + 2 t)^2] - RealAbs[x^2 - t*x + 2 t]] results in 0. Commented Aug 15, 2023 at 11:10

Numerical approach

max[t_?NumericQ] := {t, x /. #[[2]], #[[1]]} &[ NMaximize[{ Abs[x^2 - t x + 2 t], 0 <= x <= 1}, x]]

ParametricPlot[{max[t][[{1, 2}]], max[t][[{1, 3}]]}, {t, -3, 3},PlotLegends -> {"x[t]", "max[t]"}]


t-dependent maximal value max[t] is always at x==0or x==1!