Marking points of intersection between two curves
Update 3: Using Graphics`Mesh`FindIntersections
to get the intersection points (see also):
showIntersections = Show[#, Graphics @{Red, PointSize[Large],
Point @ Graphics`Mesh`FindIntersections @ #}] &;
Using the two examples in the original answer:
Row[showIntersections /@ {Plot[{Cos[x], x Sin[x]}, {x, -3 Pi, 3 Pi},
ImageSize -> 400],
Plot[{Tan[x], x Sin[x]}, {x, -3 Pi, 3 Pi}, ImageSize -> 400,
Exclusions -> Range[-5 Pi/2, 5 Pi/2, Pi]]}]
Original answer:
You can also use MeshFunctions
:
Plot[{Cos[x], x Sin[x]}, {x, -3 Pi, 3 Pi},
MeshFunctions -> {(Cos[#] - # Sin[#]) &}, Mesh -> {{0}},
MeshStyle -> Directive[Red, PointSize[Large]]]
Update: Dealing with Tan[x]
using Exclusions
Plot[{Tan[x], x Sin[x]}, {x, -3 Pi, 3 Pi},
MeshFunctions -> {(Tan[#] - # Sin[#]) &}, Mesh -> {{0}},
MeshStyle -> Directive[Red, PointSize[Large]],
Exclusions -> Range[-5 Pi/2, 5 Pi/2, Pi]]
(* or Exclusions -> (Cos[x] == 0) *)
Update 2: Using just Mesh
and MeshStyle
:
points = NSolve[Tan[x] == x Sin[x] && -3 Pi < x < 3 Pi, x][[All, 1, 2]];
Plot[{Tan[x], x Sin[x]}, {x, -3 Pi, 3 Pi},
Mesh -> {points},
MeshStyle -> {Directive[Red, PointSize[Large]]},
Exclusions -> Range[-5 Pi/2, 5 Pi/2, Pi]]
(* same picture as above *)
Edited to make it a function.
For the strange Exclusions
specification I use below, see my answer here. Thanks to @Oleksandr and @JM for their great comments.
plInters[{f1_, f2_}, {min_, max_}] :=
Module[{sol, x},
sol = x /. NSolve[f1[x] == f2[x] && min < x < max, x];
Framed@Show[
ListPlot[{#, f1[#]} & /@ sol, PlotStyle -> PointSize[Large]],
Plot[{f1[x], f2[x]}, {x, min, max}, Exclusions -> {True, f2[x] == 10, f1[x] == 10}]
]
]
GraphicsRow[plInters[#, {-10, 10}] & /@ {{# &, Tan}, {Tan, Coth}, {Sin, 1/# &}}]