Skip to main content

Piecewise Function Grapher

Plot piecewise functions using a JavaScript-style condition ? a : b expression.

Reviewed for accuracy by the Math Ora X team Last updated

Result

About the Piecewise Function Grapher

Plot piecewise functions using a JavaScript-style condition ? a : b expression.

How to use this calculator

  1. Type your piecewise rule using a JavaScript-style conditional expression, such as x < 0 ? -x : x.
  2. Use comparisons like <, <=, >, and >= to split the domain into intervals.
  3. Enter each formula carefully so the graph changes at the right boundary point.
  4. Check the plotted graph to confirm each piece appears on the correct interval.

The formula explained

A piecewise expression has the form condition ? value_if_true : value_if_false. It computes one expression when the condition is true, and a different expression when the condition is false.

  • x = the input value on the horizontal axis
  • condition = a test such as x < 2 that decides which formula to use
  • a = the expression returned when the condition is true
  • b = the expression returned when the condition is false

Step by step method

  1. Identify the interval where each rule should apply.
  2. Write a condition that is true on that interval, for example x < 3.
  3. Put the matching formula after the ?, and the other formula after the :.
  4. Graph the expression and check that the break point is in the right place.

Worked example

Problem. Graph the piecewise function defined by f(x) = x + 1 for x < 2, and f(x) = 5 for x >= 2.

  1. Write the rule as x < 2 ? x + 1 : 5.
  2. For x = 1, the condition x < 2 is true, so f(1) = 1 + 1 = 2.
  3. For x = 2, the condition x < 2 is false, so f(2) = 5.

Answer. The graph follows the line y = x + 1 to the left of x = 2, then switches to the horizontal line y = 5 at and to the right of x = 2.

Tips and common mistakes

  • Be careful with boundary points, because < and <= change whether the endpoint is included.
  • If the graph looks wrong, check that your condition matches the interval exactly, especially near the switch point.

Frequently asked questions

How do I enter a piece?+

Use condition ? valueIfTrue : valueIfFalse, e.g. x<1 ? 1 : x.

Can I nest conditions?+

Yes, chain them: x<0 ? a : (x<2 ? b : c).

More Mathematics Tools

Explore related calculators in this category

You Might Also Like

Popular tools from other categories

Can't Find the Right Calculator?

Try our AI Math Solver, type any problem in plain English and get instant step-by-step solutions.

Try AI Solver

Browse All Categories

Home Mathematics Current Tool
Facebook Twitter WhatsApp