# Most Adaptable Method for Simply Graphing Piecewise-Defined Functions

Consider the piecewise defined function:

$f(x) \, = \, \left\{ \begin{array}{cc} 1/x \, + 2, & \mbox{if \, 0 \, < \, x \, < \, 1} \\ x^{2} \, + \, 1, & \mbox{if \, 1 \, \leq \, x \, < \, 2} \\ 5, & \mbox{if \, x \, = \, 2} \\ 2 \, x \, + \, 1, & \mbox{if \, 2 \, < \, x \, \leq \, 4} \\ - \, x \, + \, 5, & \mbox{if \, x \, > \, 4} \end{array} \right.$


This is but one of many similar functions that I want to be able to graph accurately to accompany various sets of limit and continuity questions that students will be given to answer based on the information provided in the graph.

I am seen some examples on this site of how to graph such functions but with some fairly complicated coding.

Is there a simplest way to graph, say, this function, so that points of discontinuity are easily identifiable and limits (left and right) are also easily evaluated; and moreover, with the coding such that it can be modified to accommodate other piecewise functions without too much trouble?

I appreciate your help. Thank you.

• Hey mlchristians, I can't find you in chat, but if you want, I have a solution for your other code about the rope around Earth. You can ping me in chat when/if you do so. Oct 19, 2020 at 16:49
• @Alenanno Many thanks. I just reposted the question. I could not find the deleted one to undelete it, so I posted it as a new question. Thank you again. Oct 19, 2020 at 17:51
• Ah sorry I saw your comment now, I can undelete it for you by the way. :) Oct 19, 2020 at 18:15

I define a function \piecewise to be used inside a tikz picture that takes as input a comma-separated list, with each entry having the following form:

{function} / left-endpoint / right-endpoint / {open-points} / {closed-points}

The code

\begin{tikzpicture}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -1) -- (0, 3) node[above] {$y$};
\begin{scope}[line width=1pt, blue]
\piecewise{{\x+3}/-3/-1/{-1}/{},{\x*\x}/-1/1/{}/{-1},{.5*\x+.5}/1/3/{}/{}}
\end{scope}
\end{tikzpicture}


produces the following:

The piecewise function is x+3 on the interval [-3,-1), x^2 on the interval [-1,1] and (x+1)/2 on the interval (1,3]. Note that functions must be entered to be parsed by \tikz, so the variable x must have a backslash in the formula.

{open-points} is a comma separated list of x-values where you want an open circle. Similarly, {closed-points} produces filled-in circles. These can be empty lists.

If you want the axes visible inside the open circles, plot them after the function:

\begin{tikzpicture}
\begin{scope}[line width=1pt]
\piecewise{{-1}/-3/0/{0}/{},{0}/0/0/{}/{0},{1}/0/3/{0}/{}}
\end{scope}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -2) -- (0, 2) node[above] {$y$};
\end{tikzpicture}


Here is the complete code. Of course you can adjust the size of the circles (or any other aspect of the plot) to your liking.

\documentclass{article}

\usepackage{tikz}

\newcommand{\piecewise}[1]{
\foreach \f/\a/\b/\open/\closed in {#1}{%
\draw[domain=\a:\b, smooth, variable=\x] plot ({\x}, \f);
\foreach \x[evaluate={\y=\f;}] in \open{%
\draw[fill=white] (\x,\y) circle (.8mm);
}
\foreach \x[evaluate={\y=\f;}] in \closed{%
\draw[fill] (\x,\y) circle (.8mm);
}
}
}

\begin{document}

\begin{tikzpicture}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -1) -- (0, 3) node[above] {$y$};
\begin{scope}[line width=1pt, blue]
\piecewise{{\x+3}/-3/-1/{-1}/{},{\x*\x}/-1/1/{}/{-1},{.5*\x+.5}/1/3/{}/{}}
\end{scope}
\end{tikzpicture}

\vspace{2cm}
\begin{tikzpicture}
\begin{scope}[line width=1pt]
\piecewise{{-1}/-3/0/{0}/{},{0}/0/0/{}/{0},{1}/0/3/{0}/{}}
\end{scope}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -2) -- (0, 2) node[above] {$y$};
\end{tikzpicture}

\end{document}


Your example has an asymptote, which needs a little care:

I just picked .13 for the left endpoint in the first piece of the function since it looked good to me.

\begin{tikzpicture}[scale=.7]
\begin{scope}[line width=1pt]
\piecewise{{1/\x+2}/.13/1/{1}/{},{\x*\x+1}/1/2/{}/{1},{5}/2/2/{}/{2},{2*\x+1}/2/4/{}/{4},{-\x+5}/4/6.2/{4}/{}}
\end{scope}
\draw[thick,->] (-1, 0) -- (7, 0) node[right] {$x$};
\draw[thick,->] (0, -1.2) -- (0, 10) node[above] {$y$};
\node[below left] at (0,0) {0};
\draw[ultra thin] (-.4,-1.1) grid (6.2,9.8);
\end{tikzpicture}


One could also use the command to create graphs of functions with removable singularities:

\begin{tikzpicture}
\begin{scope}[line width=1pt]
\piecewise{{1}/-3/3/{0}/{}}
\end{scope}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -1) -- (0, 3) node[above] {$y$};
\node[above] at (1.5,1) {$f(x)=\frac{x}{x}$};
\node[below left] at (0,0) {0};
\node[below left] at (0,1) {1};
\end{tikzpicture}


As a side note, I strongly recommend using cases instead of array for formatting the function in your document.

• Very nice your complete explanation and the elegance. Sep 29, 2020 at 20:20
• Thanks @Sebastiano! It seems not everyone agrees with you since there was a downvote! Oh, well. I guess there's no way to please everyone. Sep 29, 2020 at 21:47
• This is the life...also I would guess to please everyone...but not all the people have our same sensiblity. Sep 30, 2020 at 11:35