0
$\begingroup$

formally, what i want to do is to maximize f(x,y) with respect to y. so i can get another function $f^*(x)=max_y(f(x,y))$, which means the maximum value for all y at a fixed point x.

if it is not analyzable in mathematica, at least i want to plot this function. for example, i have $f(x,y)=x\cdot sin(y)-cos(y^x)$. so i want to plot $f^*(x)=max_y(f(x,y))$, let x in [0, 1] and y in [0,1]. so how can i plot it?

$\endgroup$
0

1 Answer 1

0
$\begingroup$

There are two possibilities that I can think of. The first one works for your example, but, while simple, is not particularly elegant. The second is nicer, but fails for slightly complicated functions (unless someone can suggest a fix). I've included the second one for completeness.

Method 1: Interpolation

You can just create a list of points of the data you want and then interpolate those points:

f[x_, y_] := x Sin[y] - Cos[y^x]
pts = Table[{x, MaxValue[{f[x, y], 0 <= y <= 1}, y]}, {x, 0, 1, 0.05}];
g = Interpolation[pts, InterpolationOrder -> 1];
Show[Plot[g[x], {x, 0, 1}], ListPlot[pts], 
  AxesLabel -> {"x", "\!\(\*SubscriptBox[\(Max\), \(y\)]\)[f[x,y]]"}]]]

enter image description here

You may need to play around with the InterpolationOrder for other functions (and the stepsize for x) that have more complicated maxima structures.

While you're at it, you can get the y value that maximized f[x, y] for given x, and plot that against x.

ypts = Table[{x, ArgMax[{f[x, y], 0 <= y <= 1}, y]}, {x, 0, 1, 0.05}]
ListPlot[ypts, AxesLabel -> {x, y}, Joined -> True]

enter image description here

Clearly, for your f the maximum value occurs at y = 1 for all x, except at x = 0 when f[0, y] = -Cos[1] is constant.

Method 2: MaxValue

It would be nice to be able to use MaxValue because it feels like it should be the right answer. It can do what you ask, but only for pretty straightforward functions. (I have no idea what the limit on functions it can handle is.)

Suppose we have a simpler function f2[x, y]. Then

ClearAll[g]    
f2[x_, y_] := Sqrt[y] - (x - y)^2
g[x_] = MaxValue[{f2[x, y], 0 <= x <= 1, 0 <= y <= 1}, y]
Plot[g[x], {x, 0, 1}, AxesLabel -> {"x", "\!\(\*SubscriptBox[\(Max\), \(y\)]\)[f[x,y]]"}]

enter image description here

enter image description here

In most cases, I suspect it will be impossible to get a nice function out at the end. But still, if it's possible it may well be preferable.

$\endgroup$

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