2

So I have this:

a=14;
max = FindMaximum[x^3 - a x^2 - x + 1, {x, -2, 15}]
 (* {1.01781, {x -> -0.0355787}}*)

And I plotted this:

Plot[f1[a, x], {x, -.5, .5}, Epilog -> 
      {PointSize[Medium], Red, Point[{-0.0355786613147075`, 1.0178118484082224`}]}]

But it's not neat enough. So I tried to put in max in the commands.

Plot[f1[a, x], {x, -.5, .5}, Epilog -> {PointSize[Medium], Red, Point[{max}]}]

But it failed since max

Coordinate {1.0178118484082224`, {$CellContext`x -> -0.0355786613147075}} should be a pair of numbers, or a Scaled or Offset form.
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
Onizuka
  • 349
  • 1
  • 2
  • 8

3 Answers3

3

It's easy when you apply slots and rules:

point = {x /. Last[#], First[#]} &@max;
a=14;
f1[a_, x_]:= x^3 - a x^2 - x + 1;
Plot[f1[a, x], {x, -.5, .5}, 
Epilog -> {PointSize[Medium], Red, Point[point]}, Frame -> True, 
FrameLabel -> {"x", "\!\(\*SubscriptBox[\(f\), \(1\)]\)(a,x)"}, 
BaseStyle -> Directive[FontSize -> Medium]]

enter image description here

Grzegorz Rut
  • 1,158
  • 8
  • 18
  • I don't understand: point = {x /. Last[#], First[#]} &@max;

    you're creating list, but the # &@max is what I don't understand.

    – Onizuka Sep 22 '14 at 09:42
  • This basically means {apply the rule from the last element of # to x, take the first element of #}, where # is an argument and & ends a definition of a function. @ works almost the same as [] so, for instance, Sin[x] is the same as Sin@x (but in general @ has different evaluation order, take a look on its documentation). Try to read sth about slots (#), maybe it will clear things out for you. http://reference.wolfram.com/language/ref/Slot.html – Grzegorz Rut Sep 22 '14 at 09:56
2
a = 14;
f[a_, x_] := x^3 - a x^2 - x + 1;
max = FindMaximum[f[a, x], {x, -2, 15}];

pnt = Reverse[Last @@@ max];
Plot[f1[a, x], {x, -.5, .5}, Epilog -> {PointSize[Large], Red, Point[pnt]}, Frame -> True]

enter image description here

Alternatively, you can use Mesh instead of Epilog:

Plot[f1[a, x], {x, -.5, .5}, Frame->True, Mesh->{{{pnt[[1]], Directive[PointSize[.03], Blue]}}}]
(* you can also use max[[2, 1, -1]] instead of pnt[[1]] *)

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
2

Yet another method is a destructuring pattern:

max /. {y_, {_ -> x_}} :> {x, y}
{-0.0355787, 1.01781}

For a more complete handling see: Replacing more than one element from a Sublist

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371