2
$\begingroup$

Condsider

function = Tan[1 x] + Tan[2 x]
Plot[function, {x, 0, 1.5}]

enter image description here

It has a strong discontinuity around 0.8.

The problem is , if I want to find function==0, Mathematica will not only find the real root x~1.1 , but also a artifact root x~0.8 Because I think Mathematica created a line around the discontinuity. A single point the has its y value of all values, which I call "a point that is every point".

To make this obvious, let me make a Contour Plot:

ContourPlot[function + 2*y == 0, {x, 0, 1.5}, {y, 0, 1.5}, PlotPoints -> 100]

enter image description here

We know that only curved line represents real roots. The constant line is the artifact. In fact, if you plot contour for function + 2*y == 1 or function + 2*y == 2, this artifact line remains. Of course, since it is a point that is every point:

ContourPlot[function + 2*y == 1, {x, 0, 1.5}, {y, 0, 1.5}, PlotPoints -> 100]
ContourPlot[function + 2*y == 2, {x, 0, 1.5}, {y, 0, 1.5}, PlotPoints -> 100]

enter image description here enter image description here

So, how to solve the problem in Contour Plot if function to plot has a strong discontinuity that Mathematica tries wrongly (hard) to connect.

$\endgroup$
  • $\begingroup$ FYI FindRoot[function[x] == 0, {x, 1}] gives the right coordinate. $\endgroup$ – Öskå Jul 27 '14 at 9:28
  • 1
    $\begingroup$ A starting point is ContourPlot[function + 2*y == 1, {x, 0, 1.5}, {y, 0, 1.5}, PlotPoints -> 100, Exclusions -> {1/function == 0}]. $\endgroup$ – b.gates.you.know.what Jul 27 '14 at 9:32
2
$\begingroup$

If want solution without artefact, then use NSolve. And use Exclusions to avoid the divergence line

f[x_]:=Tan[1 x]+Tan[2 x]
sol=x/.NSolve[f[x]==0 && 0<x<4, x] (*finds all solutions for 0<x<4*)
Plot[f[x], {x, 0, 4}, GridLines -> {sol, {0}}, GridLinesStyle -> Red,  Exclusions -> 1/f[x] == 0]

The result you get is {1.0472,2.0944,3.14159}, free from a point that is every point.

enter image description here

You can omit the line that corresponds a divergence from ContourPlot also using Exclusions.

g[x_, y_] = f[x] + 2*y
ContourPlot[g[x, y] == 1, {x, 0, 1.5}, {y, 0, 1.5}, PlotPoints -> 100,  Exclusions -> 1/g[x, y] == 0]

enter image description here

| improve this answer | |
$\endgroup$
1
$\begingroup$

At least with V10 you get the desired results as follows (I can't show the images because my trial version doesn't allow that):

function = Tan[1 x] + Tan[2 x];

Plot[function, {x, 0, 1.5}, Exclusions -> Cot[2 x] == 0]

ContourPlot[function + 2*y == 1, {x, 0, 1.5}, {y, 0, 1.5},
 PlotPoints -> 100,
 Exclusions :> Cot[2 x] == 0]

sol = Table[FindRoot[function == 0, {x, i}], {i, 0, 1.5, 0.1}] /. 
 Rule[_, a_] :> Round[a, 0.0001] // Flatten // Chop // Union

Mathematica graphics Mathematica graphics

{0., 1.0472}

New in V10:

NumberLinePlot[function == 0,
 {x, 0, 1.5},
 AspectRatio -> 0.5,
 Frame -> True,
 FrameTicks -> {{None, None}, {sol, None}},
 GridLines -> {sol, None},
 GridLinesStyle -> Dashed,
 PlotTheme -> "Detailed"]

Mathematica graphics

PS - You can find the poles with

Reduce[function > 10^6, x] /. C[1] -> 0 // N // Flatten // Sort

-2.35619 < x < -2.35619 || -1.5708 < x < -1.5708 || -0.785399 < x < -0.785398 || 0.785398 < x < 0.785398 || 1.5708 < x < 1.5708 || 2.35619 < x < 2.35619

| improve this answer | |
$\endgroup$
  • $\begingroup$ You can get the exact positions of the poles (for this function) with Reduce[Simplify@TrigExpand[1/function] == 0, x]. $\endgroup$ – Michael E2 Jul 27 '14 at 12:27
  • $\begingroup$ @MichaelE2 Perfect - Thank you very much :) $\endgroup$ – eldo Jul 27 '14 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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