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.

  • $\begingroup$ 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. $\endgroup$
    – march
    Commented Mar 16, 2016 at 20:40
  • $\begingroup$ @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. $\endgroup$
    – Frank Lu
    Commented Mar 16, 2016 at 20:45

1 Answer 1


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]


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

in which case

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


enter image description here

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:

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

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.