2
$\begingroup$

The following code does not display the Plot correctly. Specifically, the Plot axes are not displayed in the output.

Manipulate [max = NMaximize[{price x^0.5 - 0.2 x, x >= 0}, x];
 pt = Graphics[{PointSize[.02], Red, 
    Point[{(x /. max[[2]]), max[[1]]}]}];
 plt = Plot[price x^0.5 - 0.2 x, {x, 0, 20}, PlotRange -> All, 
   Axes -> True, AxesLabel -> "x"];
 Show[pt, plt],
 {price, 1, 10}]

Output

$\endgroup$
3
  • 2
    $\begingroup$ Welcome to Mathematica StackExchange! Swap the order: Show[plt, pt] $\endgroup$
    – Domen
    Feb 16, 2022 at 12:54
  • 2
    $\begingroup$ MMA uses the options that it encounters first. Therefore by putting pt before plt, the options of pt are used. Alternatively, you may use Axes->True in the Show command $\endgroup$ Feb 16, 2022 at 14:02
  • $\begingroup$ Show[pt, plt, Axes -> True] $\endgroup$
    – cvgmt
    Feb 17, 2022 at 3:43

3 Answers 3

3
$\begingroup$

Epilog can be used to include the red point and other Graphics primitives if required.

Clear[price, x];
Manipulate[max = NMaximize[{price x^0.5 - 0.2 x, x >= 0}, x];
 maxval = First@max;
 xmax = x /. Last@max;
 Plot[price x^0.5 - 0.2 x
  , {x, 0, 20}
  , PlotRange -> {0, 4}
  , Axes -> True
  , AxesLabel -> "x"
  , Epilog -> {AbsolutePointSize[8], Red
    , Point[{xmax, maxval}]
    , Text[
     Style["{" <> ToString@(xmax) <> ", " <> ToString@maxval <> "}", 
      Black, Bold], {Max[2.5, (xmax) + 0.2], maxval + 0.2}]
    , Text[
     Style["{Price = " <> ToString@price <> "}", Black, Bold], {3, 
      3.9}]
    , Dashed, Black
    , Line[{{xmax, maxval}, {xmax, 0}}]
    , Line[{{0, maxval}, {xmax, maxval}}]
    }
  ]
 ,
 {price, 0.2, 1.78}
 ]
$\endgroup$
1
  • $\begingroup$ This is a big improvement on my version, I was not aware of the use of Epilog in Plot. $\endgroup$
    – BenW
    Feb 17, 2022 at 6:14
2
$\begingroup$

Try this:

Manipulate[max = NMaximize[{price x^0.5 - 0.2 x, x >= 0}, x];
 pt = Graphics[{PointSize[.02], Red, 
    Point[{(x /. max[[2]]), max[[1]]}]}];
 plt = Plot[price x^0.5 - 0.2 x, {x, 0, 20}, PlotRange -> {0, 4}, 
   Axes -> True, AxesLabel -> "x"];
 Show[plt, pt], {price, 0.2, 1.78}]

Have fun!

$\endgroup$
1
$\begingroup$

The plot range should depend on the max value.

maxpts[price_?NumericQ] := 
  Block[{x}, x /. NMaximize[{price x^0.5 - 0.2 x, x >= 0}, x][[2]]];
Manipulate[
 Plot[price x^0.5 - 0.2 x, {x, 0, 2*maxpts@price}, 
  Mesh -> {{maxpts@price}}, 
  MeshStyle -> Directive[AbsolutePointSize[10], Red], 
  PlotRange -> All, Axes -> True, AxesLabel -> "x"], {price, 1, 10}]

enter image description here

$\endgroup$

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