Preface


This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel.

Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in normal font. This means that you can copy and paste all commands into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts for your needs to learn the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.

Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the second course APMA0330
Return to Mathematica tutorial for the first course APMA0340
Return to the main page for the course APMA0340
Return to the main page for the course APMA0330
Return to Part I of the course APMA0330

Discontinuous Functions


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[Piecewise[{{x^2 - 1, x < 1}, {x^3 - 5, 1 < x < 2}, {5 - 2 x, x > 2}}], {x, -3, 5}]
Plot[Piecewise[{{x^2 - 1, x < 1}, {x^3 - 5, 1 < x < 2}, {5 - 2 x, x > 2}}], {x, -3, 5}, Exclusions -> {False}, PlotStyle -> Thick]

   
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.

Plot[1/(x - 1), {x, 0, 2}, PlotStyle->{Thick,Blue}]


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
line1 = Line[{{1, -11}, {1, 11}}];
lineStyle = {Thick, Red, Dashed};
Plot[1/(x - 1), {x, 0, 2}, PlotStyle -> {Thick, Blue}, Epilog -> {Directive[lineStyle], line1}]

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.

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

 

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


 

 

 

Return to Mathematica page

Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)
Return to the Part 7 (Boundary Value Problems)