2
$\begingroup$

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.
$\endgroup$
0

3 Answers 3

3
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ I don't understand: point = {x /. Last[#], First[#]} &@max; you're creating list, but the # &@max is what I don't understand. $\endgroup$
    – Onizuka
    Commented Sep 22, 2014 at 9:42
  • $\begingroup$ 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. reference.wolfram.com/language/ref/Slot.html $\endgroup$ Commented Sep 22, 2014 at 9:56
2
$\begingroup$
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

$\endgroup$
2
  • $\begingroup$ Nice use of Last @@@ max. I don't see other people using that property often enough. $\endgroup$
    – Mr.Wizard
    Commented Sep 20, 2014 at 7:41
  • $\begingroup$ @Mr.W, recently used in this answer to a question marked as duplicate. $\endgroup$
    – kglr
    Commented Sep 20, 2014 at 11:43
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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