6
$\begingroup$

I am trying to plot a function which has a discontinuity at one precise point. However, when I naïvely use Plot with this function the discontinuity point does not seem to be taken into account. For example:

Plot[If[x != 1, 1, 0], {x, 0, 2}]

gives:

enter image description here

One can add a vertical line manually to highlight the discontinuity:

Plot[If[x != 1, 1, 0], {x, 0, 2}, Epilog -> {Darker[Blue], Thickness[0.003], Line[{{1, 1}, {1, 0}}]}] 

enter image description here

However, I would like to find a way to obtain the same kind of plot without having to manually look for all the discontinuities of my function. Has anyone an idea on how to automatize this procedure?

$\endgroup$
6
$\begingroup$

Comment

Here is a workaround,

f[x_] := If[x != 1, 1, 0]

fdata = Table[{x, f[x]}, {x, 0, 2, 0.001}];

plot = ListPlot[fdata, Joined -> True]

enter image description here

$\endgroup$
  • $\begingroup$ I find it rather imitation. $\endgroup$ – user64494 Mar 1 '18 at 11:45
  • $\begingroup$ @user64494 I don’t follow $\endgroup$ – zhk Mar 1 '18 at 11:55
8
$\begingroup$

Without having to manually look:

 f[x_] := If[x != 1, 1, 0]
 g[x_] := Floor[x]/2
 Plot[{f[x], g[x]}, {x, 0, 5}, ExclusionsStyle -> {Dotted, Directive[Black, AbsolutePointSize[5]]}]

enter image description here

$\endgroup$
6
$\begingroup$

Use Exclusion as plot option:

Plot[1, {x, 0, 2}, Exclusions -> {1},ExclusionsStyle -> {PointSize -> 0.1,Red}, MaxRecursion -> 4,PlotPoints -> 100]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Thank you for your answer, however you still have to manually input your discontinuity points which may be a problem when the function you are dealing with is not as trivial as in the example I gave. $\endgroup$ – Sennin Mar 1 '18 at 12:29
  • $\begingroup$ No you don't need to know the singularities in advance: Plot[If[x != 1, 1, 0] , {x, 0, 2},ExclusionsStyle -> {PointSize -> 0.1, Red}] $\endgroup$ – Ulrich Neumann Mar 2 '18 at 8:09
3
$\begingroup$

You cannot expect Plot to find isolated singular points by itself. It samples the given function at a finite number of points. It stops sampling when the new points are close to an interpolation of the previous points. If it doesn't sample your special point during this process, you won't see it in the plot. You need a different mechanism to identify these. This will require more mathematical attention: no blind pre-programmed method will work.

$\endgroup$
  • $\begingroup$ Well, since this is Mathematica and not Matlab or something, I'd say one could expect it to figure this out at least in simple cases... though in general, deciding this for arbitrary If conditions would require solving the halting problem. $\endgroup$ – leftaroundabout Mar 1 '18 at 16:41
0
$\begingroup$

If you can create a list of values for your function and set the discontinuities to Missing, then ListLinePlot[] will skip the discontinuous points. In this case I took 0 to indicate a discontinuity and replaced it with Missing.

list = RandomInteger[{0,10},50]
list = list /. 0 -> Missing[]
ListLinePlot[list]

enter image description here

$\endgroup$

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.