Message Boards Message Boards

0
|
2171 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Plotting points on a graph.

Posted 11 years ago

I'm having to create lots of plots for my calculus class (I'm a tutor for the class), and I'm wanting to reduce typing by creating a function of another function and a numeric argument. To make this more concrete: I want to plot a function and plot points at extrema of the function and at points of inflection. To do this currently, I use the Plot function to get the trace of whatever function, saving it to a variable, like so:

p1=Plot[f[x],{x,0,1}]:

Where the function f[x_] was previously defined. If the function has a maximum at a value A, I then do something like:

p2=Graphics[{{PointSize[Large], Point[{A,f[A]}]}}];

Then I use the Show command to show the final plot:

Show[p1, p2]

This works beautifully, but it's a lot of typing. It occurred to me that I could write a function like this for the point to be plotted (sometimes several points are required):

mypoint[f, x] := Graphics[{{PointSize[Large], Point[{x, f[x]}]}}];

So, when I execute the Show command:

Show[Plot[f[x], {x, 0, 2}], mypoint[f, 1]]

it seems to work, unless f[x] is something like x Exp[-3x]. When that is the case, the my point function returns symbolic results. Changing the function to be:

mypoint[f, x] := Graphics[{{PointSize[Large], Point[{N[x], N[f[x]]}]}}]

does not seem to help, because I get the following:

Show::gcomb: Could not combine the graphics objects in Show[<plot>, Graphics[{{PointSize[Large],Point[{0.666667,0.0902235}]},GrayLevel[1],\ PointSize[Medium],Point[{0.666667,0.0902235}]}]]. >>

Which is pretty much the error I get when the values are symbolic.

Any suggestions would be most welcome. Thanks.

POSTED BY: Kim Helliwell
2 Replies
Posted 11 years ago

Thanks, Bill. This probably gives me what I need.

POSTED BY: Kim Helliwell
Posted 11 years ago

Can you find reasonable examples that still break this and you can't see a way to patch it?

In[1]:= f[x_] := x Exp[-3 x];
p1 = Plot[f[x], {x, 0, 1}];
v = x /. {ToRules[Reduce[D[f[x], x] == 0, x]], ToRules[Reduce[D[f[x], {x, 2}] == 0, x]]};
If[VectorQ[v], p2 = Graphics[{{PointSize[Large], Map[Point[{#, f[#]}] &, v]}}]; Show[p1, p2], Show[p1]]

Out[4]= ...PlotSnipped...

In[5]:= f[x_] := (x - 3) (x - 2) (x - 1);
p1 = Plot[f[x], {x, 0, 4}];
v = x /. {ToRules[Reduce[D[f[x], x] == 0, x]], ToRules[Reduce[D[f[x], {x, 2}] == 0, x]]};
If[VectorQ[v], p2 = Graphics[{{PointSize[Large], Map[Point[{#, f[#]}] &, v]}}]; Show[p1, p2], Show[p1]]

Out[8]= ...PlotSnipped...

In[9]:= f[x_] := Exp[x];
p1 = Plot[f[x], {x, 0, 1}];
v = x /. {ToRules[Reduce[D[f[x], x] == 0, x]], ToRules[Reduce[D[f[x], {x, 2}] == 0, x]]};
If[VectorQ[v], p2 = Graphics[{{PointSize[Large], Map[Point[{#, f[#]}] &, v]}}]; Show[p1, p2], Show[p1]]

Out[12]= ...PlotSnipped...

If everything seems to work then can you package it into your function without it somehow breaking?

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract