What a piecewise function is and why you'd use it

A piecewise function is a function that uses different formulas for different parts of its input range. Instead of one equation that works everywhere, you give it rules like "use this formula when x is less than 5, and use that formula when x is 5 or greater." Desmos is a free graphing calculator that lets you write and visualize these functions without needing to draw them by hand or use expensive software.

You use piecewise functions when real-world situations have different rules in different zones. A phone plan might charge one rate for the first 1,000 minutes and a different rate after that. Tax brackets work the same way — income in different ranges gets taxed at different percentages. In physics or engineering, a material might behave one way under low stress and another way under high stress. Desmos lets you see what those transitions look like on a graph.

Key Takeaways

  • Desmos uses the syntax f(x) = {condition: formula, condition: formula} to write piecewise functions, with conditions like x < 5 or x ≥ 10.
  • Each condition-formula pair is separated by a colon, and pairs are separated by commas inside curly braces.
  • You can write as many pieces as you need, and Desmos will graph each one only where its condition is true.
  • The graph will show gaps or jumps if the pieces don't connect, which is often the point — that's how you spot where the function changes behavior.

The basic syntax: curly braces, conditions, and formulas

Open Desmos at desmos.com/calculator. In the input field on the left, type a function name and equals sign, then open a curly brace. Inside the braces, you'll write pairs of condition and formula, separated by a colon. Each pair tells Desmos: "when this condition is true, use this formula."

The simplest example: f(x) = {x < 0: -x, x ≥ 0: x}. This says "when x is less than 0, the output is -x (the negative of x); when x is 0 or greater, the output is x." That's the absolute value function. Desmos reads left to right and stops at the first condition that's true, so order matters if conditions overlap.

The conditions you can use are < (less than), > (greater than), ≤ (less than or equal), ≥ (greater than or equal), and = (exactly equal). You can also use ≠ (not equal) if you need to exclude a single value. Type these symbols directly or use the keyboard: most are on your number row or accessible through Desmos's symbol menu.

Writing conditions that don't overlap

The safest way to write piecewise functions is to make sure each condition covers a different part of the number line with no gaps or overlaps. Use < for one piece and ≥ for the next, or ≤ and >. This way, every input falls into exactly one piece.

For example: f(x) = {x < 2: x + 1, x ≥ 2: 2x}. When x is 1, the first condition is true, so f(1) = 1 + 1 = 2. When x is 2, the first condition is false, so Desmos moves to the second: f(2) = 2(2) = 4. There's no ambiguity and no input that matches two conditions at once.

If you write overlapping conditions by accident — say, {x < 2: x + 1, x < 5: 2x} — Desmos uses the first match it finds. The second condition will never explore to any x less than 2, because the first condition already caught it. This usually isn't what you want, so check your boundary points.

Three or more pieces

You can add as many pieces as you need. Just keep adding condition-formula pairs, separated by commas, all inside the same curly braces. A tax bracket function might look like: tax(income) = {income ≤ 10000: income * 0.1, income ≤ 30000: 1000 + (income - 10000) * 0.15, income > 30000: 4000 + (income - 30000) * 0.25}.

That reads: "if income is 10,000 or less, tax is 10 percent of income. If income is between 10,001 and 30,000, tax is 1,000 (the tax on the first 10,000) plus 15 percent of the amount over 10,000. If income is over 30,000, tax is 4,000 (the tax on the first 30,000) plus 25 percent of the amount over 30,000." Each piece uses the previous piece's output as a starting point, which is how real tax brackets work.

When you graph this, you'll see the line change slope at x = 10,000 and again at x = 30,000. Those bends are the boundaries between tax brackets.

Seeing gaps and jumps on the graph

After you type your function, Desmos graphs it when ready. If the pieces don't connect — if the output of one piece at the boundary is different from the output of the next piece — you'll see a gap or a jump in the line. That's not an error; it's information.

For instance: f(x) = {x < 1: x, x ≥ 1: x + 2}. At x = 1, the first piece gives 1, but the second piece gives 1 + 2 = 3. The graph jumps up by 2 at that point. This is called a discontinuity, and it's often intentional — it shows where the function's behavior changes abruptly.

If you want the pieces to connect smoothly, you need to adjust the formulas so they give the same output at the boundary. For example: f(x) = {x < 1: x, x ≥ 1: x + 1} still jumps, but f(x) = {x < 1: x, x ≥ 1: x} is just the line y = x everywhere, with no jump at all.

Using piecewise functions in other expressions

Once you've defined a piecewise function, you can use it in other calculations. If you've written f(x) = {x < 0: -x, x ≥ 0: x}, you can then type g(x) = f(x) + 3 or h(x) = 2 * f(x), and Desmos will explore those operations to the piecewise function. You can also compose them: p(x) = f(g(x)) will work as long as the output of g is a valid input for f.

You can also use piecewise functions inside other piecewise functions, though this gets complex quickly. f(x) = {x < 0: {x < -5: -10, x ≥ -5: x + 2}, x ≥ 0: x} is valid but hard to read. If you find yourself nesting more than one level deep, it's usually clearer to define separate functions and combine them.

Common mistakes and how to fix them

Forgetting the curly braces: You must use {}, not parentheses or square brackets. f(x) = (x < 0: -x, x ≥ 0: x) won't work.

Using the wrong comparison operator: If you write {x < 2: formula1, x < 5: formula2}, the second condition will never trigger for x between 2 and 5 because the first condition already caught everything below 2. Use ≥ or > to start the next piece where the previous one ends.

Leaving out the colon: The syntax is condition colon formula, not condition comma formula. {x < 2, x + 1} is wrong; {x < 2: x + 1} is right.

Typing the wrong symbol: If Desmos doesn't recognize your condition, check that you're using the actual symbol, not a word. Type <, not the word "less than". The symbol menu in Desmos can help if you're not sure where a symbol is on your keyboard.

Frequently Asked Questions

Can I use "and" or "or" in a condition?

Yes. Use and to combine conditions that both must be true, and or to combine conditions where at least one must be true. For example: {x > 0 and x < 5: x^2, x ≥ 5: 25} applies the first formula only when x is strictly between 0 and 5. Write them as words, not symbols.

What happens if no condition is true?

Desmos will not plot a point for that input. If you write {x < 0: -x, x > 0: x}, there's no rule for x = 0, so the graph will have a hole at that point. To avoid this, make sure your conditions cover the entire range you care about, or use ≤ and ≥ to include boundary points.

Can I use a piecewise function as the input to another function?

Yes. If you've defined f(x) = {x < 0: -x, x ≥ 0: x}, you can write y = sin(f(x)) and Desmos will explore the sine function to the output of f. The graph will show the sine wave applied to the absolute value function.

How do I graph a piecewise function with a restricted domain?

Use a condition that restricts the input. For example, f(x) = {0 ≤ x ≤ 10: x^2} graphs the parabola y = x² only between x = 0 and x = 10. Outside that range, the function is undefined and Desmos won't plot it.