# Piecewise-Defined Functions

In this course so far, we’ve mostly studied functions that are defined by a single formula. We’ll now look at some functions that are defined by different formulas at different inputs.

## Piecewise-defined functions

Let $f$ be a function which is defined as follows:

$$\cl"red"{f(x)=\{ {\table x - 1, \text"if"\;x<0; x^2 - 1, \text"if"\;x≥0}}$$

For example, $f(-3)=(-3) - 1=-4$, because $-3<0$. On the other hand, $f(2)=2^2-1=3$, because $2≥0$.

Find the value of $f(x)$ at each value of $x$ in the table below.

$x$$f(x) Click to see a graph of f(x). Notice that it looks like a line (the graph of y=x-1) to the left of the y-axis, and like a parabola (the graph of y=x^2-1) to the right of the y-axis. Functions like f, which have different formulas at different x-values, are called piecewise-defined functions. Let \cl"red"g be defined as follows:$$ \cl"red"{g(x)=\{ {\table -x, \text"if"\;x<0; x, \text"if"\;x≥0}} $$Find the value of g(x) at each value of x in the table below. x$$g(x)$

Click to see a graph of $g(x)$.

 What letter is this graph shaped like?

Notice that this is the same shape as the graph of $y={|x|}$. Click to see the graph of $g(x)$ along with the graph of $y={|x|}$.

 Are these two graphs the same or different?

That is, $g$ can be defined either piecewise, as above, or with the single formula $g(x)={|x|}$.

The function $h$ is defined by

$$\cl"red"{h(x)=\{ {\table 2x+4, \text"if"\;-3 ≤ x < 0; x-1, \text"if"\;0 ≤ x ≤ 3}}$$

This function $h$ is graphed on the grid to the left. The domain of $h$ does not include all real numbers. Instead, $\cl"red"h$ has the domain $\{x:-3≤x≤3\}$, because there is only a formula that tells you how to get $h(x)$ from $x$ when $-3 ≤ x ≤ 3$.

 What is $h(-1)$?
 What is $h(2)$?

Notice that $h(0)=0-1=-1$. This is because the formula $x-1$ is used when $0 ≤ x ≤ 3$, which includes $x=0$. The formula $2x+4$ is only used when $-3 ≤ x < 0$, which does not include $x=0$. This is shown graphically by plotting a dot at the point $(0,-1)$ on the $x-1$ piece of the graph.

Click to see the graph of the function $k$ defined by

$$\cl"blue"{k(x)=\{ {\table x+1, \text"if"\;-2 ≤ x < 1; -x^2, \text"if"\;1 ≤ x ≤ 2; -2x+3, \text"if"\: 2 < x ≤ 4}}$$
 What is $k(1)$?
 What is $k(2)$?

What is the domain of $k$?

## Step functions and the floor and ceiling functions

The function $f$ is defined by

$$\cl"red"{f(x)=\{ {\table 3, \text"if"\;-4 ≤ x ≤ -2; 1, \text"if"\;-2 < x < 1; 2, \text"if"\:1 ≤ x ≤ 3}}$$

This is a special kind of piecewise-defined function where each piece’s formula is a constant (doesn’t change with $x$). A function of this kind is called a step function, because its graph looks like stair steps.

 What is $f(-2)$?
 What is $f(3)$?

What is the domain of $f$?

Sometimes, you will need to take a number and turn it into a nearby integer (a whole number or the negative of a whole number). We’ll now look at two ways of doing this.

 The closest integer to 3.7 which is less than or equal to 3.7 is 3. What is the closest integer to 2.6 which is less than or equal to 2.6?
 The closest integer to $-3.7$ which is less than or equal to $-3.7$ is $-4$. What is the closest integer to $-2.6$ which is less than or equal to $-2.6$?

The function that gives the closest integer which is less than or equal to $x$ is called the floor function, written $\floor(x)$. For example, $\floor(2.78)=2$, and $\floor(-2.78)=-3$.

Find the value of $\floor(x)$ for each value of $x$ in the table below.

$x$$\floor(x) Click to see a graph of \floor(x).  Is each connected piece of this graph constant (horizontal)?  Is the floor function a step function?  The closest integer to 3.7 which is greater than or equal to 3.7 is 4. What is the closest integer to 2.6 which is greater than or equal to 2.6?  The closest integer to -3.7 which is greater than or equal to -3.7 is -3. What is the closest integer to -2.6 which is greater than or equal to -2.6? The function that gives the closest integer which is greater than or equal to x is called the ceiling function, written \ceiling(x). For example, \ceiling(2.78)=3, and \ceiling(-2.78)=-2. Click to see the graph of \ceiling(x). Find the value of \ceiling(x) for each value of x in the table below, and compare those values to the values of \floor(x) you found in the previous question. x$$\floor(x)$$\ceiling(x)$
 What is a number $x$ so that $\floor(x)=\ceiling(x)$? (You may find the above table helpful in answering this question and the next one.)
 What is a number $x$ so that $\floor(x)$ is not equal to $\ceiling(x)$?