Piecewise Functions

In math it is common to have a function that behaves differently for different intervals of the input value. For example, suppose \(f\) is a function such that \(y=f(x)\). Suppose that \(f(x) = -x\) if \(x\) is negative but \(f(x)=x\) if \(x\) is not negative. The graph of this function is shown below.

no alt text

To write a function formula for this function \(f\), we have to separate the function into two parts: when \( x \lt 0\) and when \(x \ge 0\). To do this, we write \(f\) as a piecewise function. We would write the function formula as follows: \[ f(x) = \begin{cases} -x & \text{if } x \lt 0 \\ x & \text{if } x \ge 0 \end{cases} \] Each "line" of this formula tells us the value of \(f(x)\) for the given condition. For example, the formula tells us that \(f(x) = -x\) if \(x \lt 0\). The second line then tells us that \(f(x) = x\) if \(x \ge 0\). If \(x\) is not less than 0, then it must be greater than or equal to 0 (\(\ge 0\)) and therefore specifying this in the function formula can be redundant. Therefore, it is common to see this formula written as \[ f(x) = \begin{cases} -x & \text{if } x \lt 0 \\ x & \text{else}\end{cases} \] This kind of thinking should look familiar! We use this kind of thinking in programming all the time!

Let's use programming to represent the function \(f\) from above.

function f(x) {
  if (x < 0) {
    return -x;
  } else {
    return x;
  }
}

print( f(-15) );
print( f(7) );

In the code above, we define the function f using lines (1) - (7). On line (2) we check to see if x is negative, and if it is we return -x (line (3)). Otherwise, we return x (line (5)). This is the same logic that we used when thinking mathematically about the function, but now we're just representing it with code.

Example: Tipping your waitress

When eating at a restaurant, it is courtious to tip your waitress. Generally, people tip between 10% and 25% of their bill. Suppose you decide to tip your waitress 15% if your bill was less than $100, but tip her 20% if the bill was $100 or more. Let's define a function "tipAmount" that takes a bill amount (in dollars) as an input, and then outputs the amount that you will tip the waitress. Mathematically, this function will look like the following: \[ \text{tipAmount}(x) = \begin{cases} 0.15\cdot x & \text{if } x \lt 100 \\ 0.2\cdot x & \text{else} \end{cases} \] We could represent this math function with code using the following:

function tipAmount(x) {
  if (x < 100) {
    return 0.15 * x;
  } else {
    return 0.2 * x;
  }
}

print(`Tip on $35: ${tipAmount(35)}`);
print(`Tip on $147.35: ${tipAmount(147.35)}`);

Similar to our previous code, lines (2) - (4) take care of the case when the bill is less than $100, and then lines (4) - (6) handle the case when the bill is $100 or more.

Multiple "Pieces"

Piecewise functions can have more than two "pieces"! When we write the formulas for such functions, we just add more "lines" or "cases" to our formula. Consider the piecewise function shown below. \[ g(x) = \begin{cases} 0 & \text{if } x \lt 0 \\ 2x & \text{if } 0 \le x \lt 2 \\ x^2 & \text{if } x \ge 2 \end{cases} \] For this function, the output \(g(x)\) is different for \(x \lt 0\), \(0 \le x \lt 2\) and \(x \ge 2\). We could represent this function with code as below.

function g(x) {
  if (x < 0) {
    return 0;
  } else if (x < 2) {
    return 2*x;
  } else {
    return x*x;
  }
}

print( g(-15) );
print( g(1.3) );
print( g(4) );

This function g looks sort of interesting. In line (4) we use the else if statement, which allows us to add more cases to our if-else statement. On line (2) we check to see if x is negative, and if it is we return 0. Otherwise, if x is ≥0, we will enter the else if part of the function (lines (4) - (6)). So, if x is ≥0 and <2 line (5) will run and return 2*x. If x is not <2 then line (7) will run and we return x*x which represents \(x^2\).

Activities

Activity: Defining a piecewise functions

Consider the function \[ f(x) = \begin{cases} -x & \text{if } x \lt 0 \\ x^2 & \text{else} \end{cases} \] Use the editor below to define a coding function to represent \(f\).

function f(x){
  // YOUR CODE HERE
  return 0; // <-- delete this
}

print(`${f(-2)} should equal 2.`);
print(`${f(3.5)} should equal 12.25.`);

Activity: Computing a tip

Suppose a fancy restaurant mandates a certain tip based on the bill. The restaurant mandates that you tip 15% on all bills less than $50, 20% on all bills between $50 and 100$, and 25% on all bills that are $100 or more. Define a math function \(\text{tipAmount}\) that inputs a bill amount (in dollars) \(x\) and outputs the tip amount (in dollars). Once you have done this, represent this function using the editor below.

function tipAmount(x) {
  // YOUR CODE HERE
  return 0; // <-- Delete this line
}

print(`${tipAmount(20)} should be equal to 3.`);
print(`${tipAmount(80)} should be equal 16.`);
print(`${tipAmount(150)} should be equal 37.5.`);