# problem with maximize function and envelope function

I Have the problem with the maximize of this problem:

ClearAll[expr, max, sol, x, y];
expr = (1 + x + y)/((1 + x + y) + 1)* 10 -
y - (((1 + x)/((1 + x) + 1))*10);

max = Simplify[Maximize[{expr, 0 <= y <= 1.5, 0 <= x <= 0.5}, y]]


What it returned is:

Maximize[{-((y (x^2 + 2 (-3 + y) + x (4 + y)))/((2 + x) (2 + x + y))),
0 <= y <= 1.5, 0 <= x <= 0.5}, y]


So it did not work.

I have another question with regard to extraction of value when use maximize function. After using Maximize, how do I extract the value of y and the value of the maximum function? y/. did not seem to work

My final question is regard to lower bound of a function. For example, my function is this

(1 + x + y)/((1 + x + y) + 1)* 10 - y - (((1 + x)/((1 + x) + 1))*10);


I want to create a code, such that for the above function, when x runs from 0 to 1, we find the maximum of that, expressed in terms of x. However, if the function is lower than 0, I want to return 0 and y is equal 0 in such case.

Thank you all so much!

## 2 Answers

Clear["Global*"]

expr = (1 + x + y)/((1 + x + y) + 1)*10 - y - (((1 + x)/((1 + x) + 1))*10);


Use exact numbers and use the constraints as Assumptions in Simplify

maxX = Simplify[
Maximize[{expr, 0 <= y <= 3/2, 0 <= x <= 1/2}, x], {0 <= y <= 3/2,
0 <= x <= 1/2}]

(* {-(((-3 + y)*y)/(2 + y)),
{x -> Piecewise[{{1/4, y == 0}}, 0]}} *)


EDIT: Use Part to verify that the max value (maxX[]) is produced by evaluating expr at the specified value of x (maxX[]),

Assuming[{0 <= y <= 3/2, 0 <= x <= 1/2},
maxX[] == expr /. maxX[] // Simplify]

(* True *)

maxXY = Maximize[{expr, 0 <= y <= 3/2, 0 <= x <= 1/2}, {x, y}] //
Simplify

(* {7 - 2 Sqrt, {x -> 0, y -> -2 + Sqrt}} *)

maxXY // N

(* {0.675445, {x -> 0., y -> 1.16228}} *)


EDIT: Again, using Part to verify,

maxXY[] == expr /. maxXY[]

(* True *)


Your expression has two variables, so

max = Simplify[Maximize[{expr, 0 <= y <= 1.5, 0 <= x <= 0.5}, {x, y}]]


{0.675445, {x -> 0., y -> 1.16228}}

The maximum value is then

max[]


0.675445

And the x and y values are

{x, y} /. max[]


{0., 1.16228}

Update

Maybe something like this:

Your expression looks well-behaved

Plot3D[expr, {x, 0, .5}, {y, 0, 1.5}, AxesLabel -> {x, y}] So we could see where the derivative with respect to y is 0

sol = Solve[D[expr, y] == 0, y]
(* {{y -> -2 - Sqrt - x}, {y -> -2 + Sqrt - x}} *)

Plot[y /. sol[], {x, 0, .5}, AxesLabel -> {"x", "maxy"}] Alternatively,

maxy[xx_] :=
Maximize[{expr, 0 <= y <= 1.5, 0 <= x <= 0.5} /. x -> xx, y]

Plot[y /. maxy[x][], {x, 0, .5}]
` • that's not what I meant, I need to find maximum of function with regard to x (in economics its called value function, if you do economics) – user66418 Sep 1 '19 at 21:39
• @user66418 See if the answer update is more helpful – MelaGo Sep 1 '19 at 22:08
• Hi, thank you for your help, I asked my colleagues and they say Nsolve may miss some maximum value. Do you have anyother suggestions? Thank you so much – user66418 Sep 8 '19 at 20:05