0
|
1452 Views
|
2 Replies
|
2 Total Likes
View groups...
Share

# Plotting points on a graph.

Posted 10 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[, Graphics[{{PointSize[Large],Point[{0.666667,0.0902235}]},GrayLevel,\ 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.
2 Replies
Sort By:
Posted 10 years ago
 Can you find reasonable examples that still break this and you can't see a way to patch it? In:= 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= ...PlotSnipped... In:= 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= ...PlotSnipped... In:= 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= ...PlotSnipped... If everything seems to work then can you package it into your function without it somehow breaking?
Posted 10 years ago
 Thanks, Bill. This probably gives me what I need.