# Plotting a function discontinuous at one point

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: 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}}]}] 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?

## 5 Answers

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] • I find it rather imitation. – user64494 Mar 1 '18 at 11:45
• @user64494 I don’t follow – zhk Mar 1 '18 at 11:55

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]}] Use Exclusion as plot option:

Plot[1, {x, 0, 2}, Exclusions -> {1},ExclusionsStyle -> {PointSize -> 0.1,Red}, MaxRecursion -> 4,PlotPoints -> 100] • 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. – Sennin Mar 1 '18 at 12:29
• 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}]  – Ulrich Neumann Mar 2 '18 at 8:09

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.

• 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. – leftaroundabout Mar 1 '18 at 16:41

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] 