R TUTORIAL. Part 1.1: Plotting Discontinuous Functions

Prof. Vladimir A. Dobrushkin

This tutorial contains many R scripts. You, as the user, are free to use all codes for your needs, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. Any comments and/or contributions for this tutorial are welcome; you can send your remarks to

Email Vladimir Dobrushkin

Explicit Protting

There are two types of discontinuous functions. There are piecewise functions and functions that are discontinuous at a point. A piecewise function is a function defined by different functions for each part of the range of the entire function. A discontinuous function is a function that has a discontinuity at one or more values mainly because of the denominator of a function is being zero at that points. For example, if the denominator is (x-1), the function will have a discontinuity at x=1.

Using Mathematica, it is easy to plot a piecewise discontinuous function.

An example of a Piecewise function is given below. There are three different functions that have been generated in a single graph. Here are two options: either exclude discontinuities (which is a default option) or connect them (with option Exclusions)

   
Plot with exclusions (left) and without exclusions (right).

This code is very similar to the Plot command. The only difference is that you add the Piecewise Command and after this command you enter the different components of the piecewise equation and the range for each of these components.
This code creates a graph that shows all three of the different piecewise components over the range of -3 to 5. Piecewise graphing does not create vertical lines at the boundary lines of each of the piecewise components.

Discontinuous functions can be plotted in Mathematica using the following command.


The latest version of Mathematica does not generate the vertical line of discontinuity (at x = 1 in our case). It can be added with Epilog

Another option is to use GridLines, which is an option for two-dimensional graphics functions that specifies grid lines:
Plot[1/(x - 1), {x, 0, 2}, PlotStyle -> {Thick, Blue}, GridLines -> {{{1, Red}}, None}]

Let’s plot a piecewise function: \( f(t) = \begin{cases} t^2 , & \ 0 < t < 2 , \\ 4 - t, & \ 2 < t < 4, \\ 2, & t > 4. \end{cases} \) The function is undefined at the points of discontinuity x = 1 and x = 4.

func <- function(x){(x < 1)*(x^2 - 1)+(1 < x & x < 2)*(x ^ 3 - 5)+(x>2)*(5 - 2 * x)} vectFunc <- Vectorize(func) plot(vectFunc,-3,5, type = "l", col = "red", ylab = "", main = "Piecewise Function") abline(v = 0, h = 0)

 

The same function with the value of 1 at t=2:

f[t_] := Piecewise[{{Piecewise[{{t^2, 0 < t < 2}, {1, t == 2}}], 0 < t <= 2}, {Piecewise[{{4 - t, 2 < t < 4}, {2, t > 4}}], t > 2}}]
Plot[f[t], {t, 0, 10}, PlotRange -> Full]

To show the discrete value at t=2, we have two options:

pts := ListPlot[{{2, 1}}]
a := Plot[f[t], {t, 0, 10}, PlotRange -> Full]
Show[a, pts]

dp := DiscretePlot[1, {k, 2, 2}]
Show[a, dp]

When you are graphing discontinuous functions, often times, it can be useful to generate a vertical or horizontal asymptote. To do this, you can use the following commands:

Plot[c,{x,c1,c2}] for horizontal asymptotes. In this command, c is the value of the horizontal asymptote and c1 and c2 are the range of the graph.

It is more difficult to graph a vertical line using Mathematica than a horizontal line. One way to do this is:

Plot[0, {x, 1, 3}, GridLines -> {{2}, None}]

Mathematica can easily add the vertical line. The range of this function is 1 to 3. Then the command calls for Mathematica to create a straight vertical gridline at x=2. None is part of the command that tells Mathematica to just make it a straight dark, non dashed line.

If you're actually using Plot (or ListPlot, etc.), the easiest solution is to use the GridLines option,
which lets you specify the x- and y-values where you want the lines drawn.

Plot[Cos[x], {x, 0, 2 \[Pi]}, GridLines -> {{0, \[Pi]/2, \[Pi], 3 \[Pi]/2, 2 \[Pi]}, {-1, -Sqrt[3]/2, -1/2, 0, 1/2, Sqrt[3]/2, 1}}]

For the case of horizontal lines when using Plot the easiest trick is to just include additional constant functions

Plot[{Cos[x], .75}, {x, 0, 2 Pi}]

For vertical lines, there's the Epilog option for Plot and ListPlot:

Plot[Sin[x], {x, 0, 2 Pi}, Epilog -> Line[{{Pi/2, -100}, {Pi/2, 100}}]]


Plot[Sin[x], {x, 0, 2 Pi}, Epilog -> {Dashed, Line[{{Pi/2, -100}, {Pi/2, 100}}]}]               (*dashed vertical line *)

Another, perhaps even easier, option would be using GridLines:

Plot[{Cos[x]}, {x, 0, Pi}, GridLines -> {{0.5}, {Pi/2}}, PlotRange -> All]

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> \[Pi]/15, z -> 1, q -> \[Pi]/600}
Plot[{f[x], f[\[Pi]/15],f[\[Pi]/15]/Sqrt[2]}, {x, \[Pi]/15 - .01, \[Pi]/15 + .01}]