Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to illustrate the solutions numerically and graphically for an equation such as Tan[x] == x. I think I did everything ok except I wanted to mark each intersection between Tan[x] and x.

Does anyone know how such a thing can be done?

share|improve this question
Comment by the OP (migrated from the question) -> ** I'm sorry if I'm not 'commenting' properly. I'll figure it out when I get more time here. I wanted to say THANKS to EVERYONE that posted. This is exactly what I was looking for. I will use all the input and make sure I learn from what was given. What a goldmine this site is for learning something like this. Thanks again, it's greatly appreciated!! – belisarius Sep 12 '12 at 19:23

2 Answers

up vote 29 down vote accepted
f[x_] := Tan[x]
g[x_] := x

sol = x /. NSolve[g[x] == f[x] && -10 < x < 10, x]
Show[Plot[    {f[x], g[x]}, {x, -10, 10}], 
     ListPlot[{#, g[#]} & /@ sol, PlotStyle -> PointSize[Large]]]

Mathematica graphics

Edit

A little bit more sophisticated. Using the comments by @Oleksandr and @JM below. For the strange Exclusions specification I use below, see my answer here

f[x_] := Tan[x]
g[x_] := x

sol = x /. NSolve[g[x] == f[x] && -10 < x < 10, x]
Framed@Show[
  ListPlot[{#, g[#]} & /@ sol, PlotStyle -> PointSize[Large], Ticks -> {Range[-5, 5] Pi}],
      Plot[{f[x], g[x]}, {x, -10, 10}, Exclusions -> {True, f[x] == 1}]]

Mathematica graphics

share|improve this answer
1  
Damn, yours is better. – kale Sep 12 '12 at 1:00
1  
@kale That is one of the good things in this site. You can always find other ways. – belisarius Sep 12 '12 at 1:01
1  
Anyway definite +1 from me. – kale Sep 12 '12 at 1:06
1  
I don't really know why Plot can sometimes determine exclusions properly and sometimes not. Still, this'll look a bit better if you set Exclusions -> Pi/2 Range[-5, 5, 2] for Plot (otherwise, it may be harder for students to see that these aren't really solutions). – Oleksandr R. Sep 12 '12 at 1:45
1  
@Oleksandr, for exclusions for the tangent function, it's slightly neater to use Exclusions -> {Cos[x] == 0}... – J. M. Sep 12 '12 at 1:53
show 7 more comments

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]]]

plot of Cos[x] and x Sin[x]

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) *)

plot of Tan[x] and x Sin[x]

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 *)
share|improve this answer
very nice! :) $ $ – rm -rf Sep 12 '12 at 1:45
3  
This has some problems with the exclusions :( – belisarius Sep 12 '12 at 5:03
1  
@R.M thank you. Belisarius, right... without excluding the vertical segments of the Tan function (using Exclusions or RegionFunction) MeshFunctions do not work. – kguler Sep 12 '12 at 5:43
But that is not always easy > mathematica.stackexchange.com/q/10501/193 – belisarius Sep 12 '12 at 13:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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