# Plot a function after taking the supremum with respect to one variable

Assume given a continuous function $f$ with 2 variables, say $x$ and $y$. $x$ takes values on the interval $I$ and $y$ takes values on the interval $J$. I would like to know how to plot the function $$g(y):=\sup_{x\in I}f(x,y)$$ using Mathematica. For example, $f(x,y)=e^y\sin(x^2)$ with $x\in[0,1], y\in[0,1]$. Many thanks.

• Give us an example of an f. Give us an idea of where you are. Do you know how to plot functions in general? Do you know how to take the supremum of a function in Mathematica (if such a thing is possible)? Etc. Commented Mar 16, 2016 at 20:40
• @march So far I know that using Plot to plot the graph of a single variable function and MaxValue to find out the maximum value of a function on an interval. I am a beginner of using mathematica. Commented Mar 16, 2016 at 20:45

There are multiple ways. Here's one. (I chose this new function because it's a little more interesting).

f[x_, y_] = Exp[2 x] Exp[-2 y] Cos[15 x y]


Then, define

g[y_?NumericQ] := MaxValue[{f[x, y], 0 <= x <= 1}, x]


or

g[y_?NumericQ] := First@NMaximize[{f[x, y], 0 <= x <= 1}, x]


in which case

Plot[g[y], {y, 0, 1}]


yields

after a little bit of time. To get a better picture of this, define instead the function that gives us the 3D point, i.e.

g3D[y_?NumericQ] := NMaximize[{f[x, y], 0 <= x <= 1}, x]


make a Table of points via

vals = Riffle[{x /. #2, #1} & @@ g3D@#, #] & /@ Range[0, 1, 0.02];


and then plot:

Show[
Plot3D[f[x, y],
{x, 0, 1}, {y, 0, 1},
AxesLabel -> {"x", "y", "z"}, Mesh -> False, BoundaryStyle -> None
],
Graphics3D[Line@vals]
]


• You could've used NMaxValue[] for the third snippet. ;) Commented Mar 16, 2016 at 23:02
• @J.M.isslightlypensive, could you take a look at my recent question where I have two variables to optimize over: mathematica.stackexchange.com/questions/193867/… Commented Mar 24, 2019 at 10:55