4
$\begingroup$

I am new to Mathematica. I want to find the maximum of the functions:

FindMaximum[
 (1 + y)^2/((1 + y)^2 + (1 + x)^2)* 5 - y - (1/((1 + x)^2 + 1))*5 && 0 < y < 2 
, y]

So I want to find the maximum of above function, with y in the range 0 to 2, taking $x$ as a parameter. However it showed the error

FindMaximum::nrnum: 
The function value 1. +(5 (1+x)^2)/(1+(1+x)^2)-(5 (2. +x)^2)/(1+(2. +x)^2)
is not a real number at {y} = {1.}

How can I fix this?

It only works if I input a certain value for $x$ (such as x=1).

Thank you all so much!

$\endgroup$
  • $\begingroup$ Welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of your code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Jul 15 at 10:21
  • $\begingroup$ Did any of the answers fulfill your question? If the answers do give what you asked for, there are things to for you to do. If it didn't then you should edit your question and explain in more detail what you asked. For follow-up needs ask a new question and link to this one. Stay vigilant for a day or two, better approaches may come later and other users may point alternatives, caveats or limitations. New users should test answers before voting and a day or two before accepting** the best one $\endgroup$ – rhermans Jul 17 at 8:07
2
$\begingroup$

Numerical solution using NMaximize

maxi[x_?NumericQ] :=NMaximize[{(1 + y)^2/((1 + y)^2 + (1 + x)^2)*5 -y - (1/((1 + x)^2 + 1))*5, 0 < y < 2}, y][[1]]

Plot[maxi [x], {x, -1, 3}] (* takes some time... *)

enter image description here

$\endgroup$
2
$\begingroup$

If you are looking for an analytical solution then use Maximize

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

max = Simplify[
   Maximize[
    {expr, 0 < y < 2}
    , y
    ]
   ];

The solution is not compact.

sol = Assuming[
   0 < y < 2,
   Simplify[expr /. Last@max]
   ];

Plot[
 sol
 , {x, -5, 5}
 , PlotTheme -> "Scientific"
 ]

enter image description here


If you are looking for a numerical minimization, then you got your syntax wrong and have to define all symbols either as values or minimizing variables.

FindMaximum[
{
 (1+y)^2/((1+y)^2+(1+x)^2)*5-y-(1/((1+x)^2+1))*5
 , 0<y<2
}, {x,y}]
(* {0.675445,{x->0.470469,y->1.16228}} *)

or

With[
{x=1},
FindMaximum[
 {
  (1+y)^2/((1+y)^2+(1+x)^2)*5-y-(1/((1+x)^2+1))*5
  , 0<y<2
 }
, {y}
]]
(* {0.550873,{y->1.41368}} *)
$\endgroup$
  • $\begingroup$ Sorry, maybe you misunderstood my question. What I meant is that I want to take x as a parameter, not a variable. So I want to find maximum of y, keeping x constant as some number $\endgroup$ – user66418 Jul 15 at 10:26
  • $\begingroup$ @user66418 thanks for clarifying. I hope my answer now does address your need. If it does, probably we should delete the obsolete comments. $\endgroup$ – rhermans Jul 15 at 13:04
  • $\begingroup$ Yes, thank you, that's what I looked for. However the solution looks very messy, since I need to plot y ( as expression of x) into another function, and maximize again. Is there any way? $\endgroup$ – user66418 Jul 16 at 19:54
  • $\begingroup$ The sol that I offer does what you ask in your question. It's messy by nature, not as a consequence of the method. If you need to do now something different, like maximizing again, you should ask another question with the new requirements, and people will be happy to answer there. $\endgroup$ – rhermans Jul 17 at 7:56
1
$\begingroup$

It is possible to do this numerically in cases where Maximize is unable to work symbolically. Here is your function:

expr = (1 + y)^2/((1 + y)^2 + (1 + x)^2)* 5 - y - (1/((1 + x)^2 + 1))*5 /. y -> y[x];
expr //TeXForm

$\frac{5 (y(x)+1)^2}{(y(x)+1)^2+(x+1)^2}-y(x)-\frac{5}{(x+1)^2+1}$

where I introduced an x dependence in the variable y. Then, the following NDSolveValue call finds an InterpolatingFunction result for the maximum function:

m = NDSolveValue[
    {
    D[D[expr, y[x]] == 0, x],
    z[x] == expr,
    y[0] == NArgMax[{expr /. x->0, 0 < y[0] < 2}, y[0]]
    },
    z,
    {x, 0, 2}
];

Visualization:

Plot[m[t], {t, 0, 2}]

enter image description here

Note that this approach can break down when different branches cross, or when boundary conditions (0 < y < 2) interfere.

$\endgroup$
0
$\begingroup$
Assuming[Element[x, Reals], 
  Maximize[{(1 + y)^2/((1 + y)^2 + (1 + x)^2)*5 - y - (1/((1 + x)^2 + 1))*5, 0 < y < 2}, y]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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