Skip to main content

How to Graph Piecewise Functions in Desmos

Graph pieces, restrictions and endpoints

By JasonPublished 2 min read

A student hikes a trail that changes from boardwalk to stone steps to a steep slope.

To graph a piecewise function in Desmos, use curly braces with a condition, a colon and the rule for each piece, separated by commas: y={x<0: -x, x>=0: x^2}. To graph only part of one function, put a condition in braces after it: y=2x+1{0<x<5}. Try the examples in our free Desmos calculator.

A piecewise function uses different rules for different inputs. A domain restriction keeps only the part of a graph whose x-values satisfy a condition. Both use braces, but their syntax has a useful difference: a restriction goes after a rule; a piecewise definition puts each condition and its rule inside the braces.

Restrict the domain of a graph

Start with y=2x+1, and keep only the part where 0<x<5.

  1. Type y=2x+1.
  2. Immediately after the rule, type {.
  3. Type 0<x<5 inside the braces.
  4. Finish with }. The completed line is y=2x+1{0<x<5}.

The graph is the part of the line between x=0 and x=5. Neither boundary is included: x must be greater than 0 and less than 5. For example, x=2 is allowed, giving y=5, but x=0 is not allowed.

To include both boundaries, use y=2x+1{0<=x<=5}. Type <= for the less-than-or-equal-to sign and >= for greater-than-or-equal-to. You can include just one boundary: 0<=x<5 includes 0 and excludes 5. The direction of each inequality matters, so read the condition from left to right before graphing.

A domain restriction tests x-values. You can also restrict y, which keeps only the part of a graph in a chosen range of outputs. Type y=2x+1{1<y<7} to keep the part where y is between 1 and 7, excluding both boundaries. For this line, that is the same segment as 0<x<3. The condition still follows the equation; it just uses y instead of x.

Check the condition

Pick a point you expect to keep. For y=2x+1 at x=2, the point is (2,5). Both 0<2<5 and 1<5<7 are true, so it belongs to both restricted graphs above.

Write a piecewise function

Now give Desmos a different rule on each side of x=0: use −x when x<0 and x2 when x≥0.

  1. Type y= and then {.
  2. Type the first condition, x<0.
  3. Type a colon, :, then the first rule, -x.
  4. Type a comma, ,, to separate the pieces.
  5. Type the second condition, x>=0, a colon, and the second rule, x^2.
  6. Finish with }. The line is y={x<0: -x, x>=0: x^2}.

Read each piece as “if this condition is true, use this rule.” At x=−2, the first condition applies, so y=−(−2)=2. At x=3, the second applies, so y=32=9. At x=0, the second rule applies because its inequality includes equality.

For three pieces, follow the same pattern and add another comma. Use x+2 for x<−1, use 1 for −1≤x<2, and use x−1 for x≥2.

  1. Type y={ and then x<-1:x+2.
  2. Type , and then -1<=x<2:1.
  3. Type another , and then x>=2:x-1.
  4. Finish with }. The whole line is y={x<-1:x+2, -1<=x<2:1, x>=2:x-1}.

The middle piece is a horizontal segment at y=1. Test the boundary x=2: it is excluded from the middle condition and included in the last one, so the last rule gives y=1. Check every boundary this way. If none of the conditions includes an input, the function has no value there. Avoid overlapping conditions that assign different outputs to the same input.

Common mistake

Do not reverse the condition and the rule. Inside a piecewise definition, write x<0:-x, not -x:x<0. The colon separates the condition from the rule; the comma separates one piece from the next.

Add open and closed endpoints

A strict inequality such as x<0 excludes its boundary. An inequality such as x≥0 includes it. On a graph drawn with endpoint markers, use an open circle for an excluded endpoint and a filled circle for an included endpoint. Desmos does not automatically draw those open and closed circles on restricted curves, so add the points yourself.

For example, graph y={x<0:x+1,x>=0:x}. The left piece approaches (0,1) but excludes it; the right piece includes (0,0).

  1. On a new line, type (0,0) to add the closed point.
  2. On another line, type (0,1) for the excluded endpoint.
  3. Press and hold the colored icon beside the (0,1) expression. In the point style options, select the open circle.

The button starts with both points filled. Change the style of (0,1) to open as described above. The graph has an output of 0 at x=0, not 1. If two pieces meet at the same coordinate and one includes it, that coordinate belongs to the graph; do not label it open just because the other piece excludes it.

Name the function and evaluate it

Replace y= with f(x)= to give the first two-piece example a name.

  1. Type f(x)={x<0:-x,x>=0:x^2}.
  2. On the next line, type f(-2). Desmos shows 2.
  3. On another line, type f(3). Desmos shows 9.

The number in parentheses is the input. Decide which condition it satisfies, then substitute into that piece. The calculator does that selection for you. See SAT functions for more practice with inputs, outputs and graphs.

Absolute value is a piecewise function

The graph of y=|x| is a V. For negative inputs, its rule is −x; for zero and positive inputs, its rule is x. In Desmos, type y=|x| on one line and y={x<0:-x,x>=0:x} on the next. The two graphs lie on top of each other.

For instance, |−3|=3 because the negative-input rule gives −(−3)=3. At x=3, the other rule gives 3. This is why both sides of the V sit on or above the x-axis. Our SAT absolute value guide shows how to use the graph to solve equations.

Where this helps on the SAT

The SAT rarely writes piecewise functions. Domain restrictions are useful when a model only makes sense over certain values, such as time from 0 onward. The restriction makes the relevant part of a graph easier to see; it does not change the rule within that interval.

Practice question · Functions

Show the Detailed Explanation

Put 9 into the rule: W(9)=95−4(9)=59 gallons, choice A.

In Desmos, type W(t)=95-4t, then W(9) on the next line. Desmos shows 59. To see the model from the start until the tank is empty, graph y=95-4x{0<=x<=23.75}: 95−4x=0 at x=23.75, and 9 is inside the interval.

For more calculator techniques, see key Desmos features, then practice in Functions topic training or Desmos Skills training.

Piecewise graphs in Desmos

  • Put a restriction after a rule: y=2x+1{0<x<5}. Use y in the condition to restrict outputs instead.
  • Inside a piecewise definition, type each condition, a colon and its rule; separate pieces with commas.
  • Check each boundary for inclusion, and add open or filled endpoint points yourself.
  • Name the rule f(x) and type a value such as f(-2) to evaluate it.

Tutor? Use Resolve Prep with your students