Piecewise Function Calculator

Define rules with conditions, evaluate values, validate gaps and overlaps, and graph each piece in its own color.

Results are calculated automatically as you enter data.

Rules and conditions
Rule f(x) Condition

Conditions can be x < 0, 0 <= x < 2, x >= 2, or one final otherwise rule.

Calculus checks
ValueDefine piecewise rules.

▼ See explanations and tips below ▼

What Is a Piecewise Function?

A piecewise function is a function that uses different formulas on different parts of its domain. Instead of one rule applying to every possible input, each rule applies only when its condition is true.

A simple piecewise function might look like this:

$$ f(x)= \begin{cases} -x, & x < 0 \\ x^2, & 0 \le x < 2 \\ 4, & x \ge 2 \end{cases} $$

This means:

  • Use \(-x\) when \(x\) is less than \(0\).
  • Use \(x^2\) when \(x\) is at least \(0\) but less than \(2\).
  • Use \(4\) when \(x\) is at least \(2\).

The main idea is that the input value decides which formula is active. Once you identify the correct interval or inequality, evaluating the function is usually just ordinary substitution.

Piecewise functions are useful when a relationship changes at specific breakpoints. They appear in algebra, precalculus, calculus, physics, economics, computer science, grading systems, step functions, absolute value functions, and many real-world rules with thresholds.


Why Piecewise Functions Matter

Piecewise functions help describe situations where one formula is not enough. Many real-world relationships behave differently in different ranges. For example, a rule might change after a deadline, a cost might change after a quantity threshold, or a graph might switch from a line to a curve at a boundary.

They also help students learn important function ideas:

  • how domain restrictions control a formula;
  • how inequalities define intervals;
  • how open and closed endpoints affect graphing;
  • how a function can have jumps, corners, gaps, or removable discontinuities;
  • how calculus ideas such as derivatives and integrals can behave differently on different pieces.

A piecewise function is still one function only when each allowed input has exactly one output. That is why the conditions matter as much as the formulas.


Key Terms to Know

  • Function: A relationship where each input has exactly one output.
  • Domain: The set of input values where a function is defined.
  • Piece: One formula together with the condition that tells where it applies.
  • Condition: An inequality or interval, such as \(x < 0\) or \(0 \le x < 2\), that decides when a piece is used.
  • Breakpoint: A boundary value where the rule may change, such as \(0\) or \(2\) in the example above.
  • Open endpoint: A boundary point not included in a piece, often shown with a hollow circle on a graph.
  • Closed endpoint: A boundary point included in a piece, often shown with a filled circle on a graph.
  • Gap: An input value or interval where no rule applies.
  • Overlap: An input value or interval where more than one rule applies.
  • Discontinuity: A break in the graph, which may happen at a gap, jump, or mismatched boundary value.

How Piecewise Functions Work

To evaluate a piecewise function, follow the conditions first and the formulas second.

For example, consider:

$$ f(x)= \begin{cases} -x, & x < 0 \\ x^2, & 0 \le x < 2 \\ 4, & x \ge 2 \end{cases} $$

To find \(f(-3)\), use the first piece because \(-3 < 0\):

$$ f(-3)=-(-3)=3 $$

To find \(f(1.5)\), use the second piece because \(0 \le 1.5 < 2\):

$$ f(1.5)=(1.5)^2=2.25 $$

To find \(f(2)\), use the third piece because \(2 \ge 2\):

$$ f(2)=4 $$

Notice that \(x=2\) is not part of the middle interval \(0 \le x < 2\), because the symbol \(<\) excludes \(2\). It is part of the last interval \(x \ge 2\), because the symbol \(\ge\) includes \(2\).

A well-defined piecewise function should avoid both gaps and overlaps. Ideally, each input in the intended domain should match one rule and only one rule.


Graphing Piecewise Functions

To graph a piecewise function, graph each formula only on the interval where it applies. Then mark boundary points carefully.

For the function

$$ f(x)= \begin{cases} -x, & x < 0 \\ x^2, & 0 \le x < 2 \\ 4, & x \ge 2 \end{cases} $$

there are three graph pieces:

  1. The line \(y=-x\) only to the left of \(0\).
  2. The parabola \(y=x^2\) from \(0\) up to, but not including, \(2\).
  3. The horizontal line \(y=4\) starting at \(2\) and continuing to the right.

At \(x=0\), the first piece is open because \(x<0\) does not include \(0\), while the second piece is closed because \(0 \le x\) includes \(0\). At \(x=2\), the second piece is open and the third piece is closed.

Open and closed endpoints are not just graphing details. They decide whether the function has a value at the boundary and which rule gives that value.


Examples of Piecewise Functions in Practice

Example 1: Evaluating the Correct Piece

Suppose

$$ g(x)= \begin{cases} 2x+1, & x < 3 \\ 10-x, & x \ge 3 \end{cases} $$

Find \(g(2)\) and \(g(3)\).

For \(x=2\), use the first piece because \(2<3\):

$$ g(2)=2(2)+1=5 $$

For \(x=3\), use the second piece because \(3 \ge 3\):

$$ g(3)=10-3=7 $$

The boundary value \(3\) belongs to the second piece, not the first one.


Example 2: A Gap

Suppose

$$ h(x)= \begin{cases} x+2, & x < 1 \\ x^2, & x > 1 \end{cases} $$

The input \(x=1\) is not covered by either condition. The first condition excludes \(1\), and the second condition also excludes \(1\).

So \(h(1)\) is undefined:

$$ h(1) \text{ is undefined} $$

This is a gap at \(x=1\).


Example 3: An Overlap

Suppose

$$ p(x)= \begin{cases} x+1, & x \le 2 \\ x^2, & x \ge 2 \end{cases} $$

At \(x=2\), both conditions are true. The first rule gives:

$$ 2+1=3 $$

The second rule gives:

$$ 2^2=4 $$

Because two different rules apply at the same input, the function is ambiguous at \(x=2\). A calculator may treat this as undefined or warn that more than one rule matches.


Example 4: Absolute Value as a Piecewise Function

The absolute value function can be written piecewise as:

$$ |x|= \begin{cases} -x, & x < 0 \\ x, & x \ge 0 \end{cases} $$

This works because negative inputs are made positive by multiplying by \(-1\), while zero and positive inputs stay the same.

For example:

$$ |-5|=-(-5)=5 $$

and

$$ |5|=5 $$

How to Interpret the Result

A piecewise function result tells you which rule applies at a specific input and what output that rule produces.

If the result is defined, it means exactly one condition matched the entered \(x\) value and the corresponding expression produced a finite output.

If no rule matches, the function is undefined at that input. This usually means there is a gap in the conditions.

If more than one rule matches, the function is ambiguous at that input. This usually means the conditions overlap. Even if two overlapping formulas happen to give the same number at one point, overlapping conditions can still make the function setup unclear and should be fixed.

When reviewing graph and status results, pay attention to these ideas:

  • Matching rule count of \(1\): The function is defined at the selected input.
  • Matching rule count of \(0\): No condition covers the selected input.
  • Matching rule count greater than \(1\): More than one condition covers the selected input.
  • Exact gap components: Interval analysis found uncovered points or intervals in the selected graph range.
  • Exact overlap components: Interval analysis found points or intervals covered by multiple ordinary rules.
  • Active rule: The condition used to compute the displayed value when exactly one rule matches.

A graph helps you see the pieces, endpoints, jumps, and gaps, but it is still a numerical drawing. Use the conditions to confirm exact boundary behavior.


Derivative and Integral Checks

Some piecewise function tools also estimate calculus information. These estimates are helpful for exploration, but they should not be confused with exact symbolic calculus.

A numerical derivative estimate often uses nearby points to approximate the local slope. A common central-difference estimate is:

$$ f'(x) \approx \frac{f(x+h)-f(x-h)}{2h} $$

where \(h\) is a small step size. This calculator now refines a scale-aware step and compares left- and right-hand slopes. At a boundary it reports a one-sided estimate, a converged two-sided estimate, or that the derivative does not exist instead of blindly averaging incompatible pieces.

This estimate can work well on a smooth part of a piecewise function, but it can be misleading near:

  • jumps;
  • corners;
  • endpoints;
  • gaps;
  • overlaps;
  • vertical asymptotes or non-finite values.

For example, the absolute value function has a sharp corner at \(x=0\). The graph has a left-hand slope of \(-1\) and a right-hand slope of \(1\), so the ordinary derivative does not exist at that point even though nearby numerical estimates may produce a value.

A numerical integral estimate approximates signed area over an interval. Adaptive Gauss-Kronrod quadrature compares embedded formulas on each subinterval and subdivides where their estimates do not agree closely enough.

$$ \int_a^b f(t)\,dt \approx Q_K, \qquad \text{error estimate} \approx |Q_K-Q_G| $$

Here \(Q_K\) is the Kronrod estimate and \(Q_G\) is the embedded Gauss estimate. Their difference guides further subdivision.

In this calculator, the integral runs from the lower end of the graph range to the entered evaluation value. It is split at exact condition boundaries and evaluated with adaptive Gauss-Kronrod quadrature. The result includes an error estimate and evaluation count; it is withheld when an open interval is uncovered or ambiguous, the integrand is non-finite, or convergence limits are reached.


Common Mistakes and Misconceptions

Using the wrong piece. Always check the condition before substituting the input into a formula. The formula is only valid on its assigned interval.

Ignoring open and closed endpoints. The difference between \(x<2\) and \(x\le 2\) matters. One excludes \(2\); the other includes it.

Leaving a gap unintentionally. Conditions such as \(x<1\) and \(x>1\) leave \(x=1\) uncovered.

Creating an overlap unintentionally. Conditions such as \(x\le 1\) and \(x\ge 1\) both include \(x=1\).

Expecting rule order to fix overlaps. In some programming contexts, the first matching rule may be used. In this calculator, overlapping rules are treated as ambiguous rather than being resolved by order.

Using malformed or unsupported notation. The parser accepts implicit multiplication such as 2x and explicit multiplication such as 2*x. Use the variable \(x\), supported function names, and complete expressions.

Using equality-only point rules. A condition such as \(x=1\) is not supported by this calculator’s condition format. Use supported inequalities and intervals.

Treating numerical estimates as exact. Numerical derivative and integral results are approximations. They are especially sensitive near discontinuities and sharp changes.

Assuming the evaluation value must be inside the graph range. The graph range controls the displayed graph and the starting point for the integral estimate, but the entered \(x\) value is not automatically restricted to that range.


When to Use Piecewise Functions

Use piecewise functions when a relationship has different rules for different input ranges.

Common uses include:

  • modeling absolute value functions;
  • describing step functions;
  • defining rules with thresholds;
  • graphing functions with jumps or corners;
  • analyzing domain restrictions;
  • studying continuity and discontinuity;
  • introducing one-sided behavior in calculus;
  • approximating real-world systems whose behavior changes by interval.

Piecewise notation is especially useful when the formula changes at clear boundary values.


Limitations and Things to Keep in Mind

Piecewise functions are powerful, but they require careful setup. A small condition error can change the function’s value or make the function undefined.

For this calculator, keep these limitations in mind:

  • It supports one independent variable, \(x\).
  • Conditions must use supported one-sided inequalities, interval forms, or a single final otherwise / else fallback. Decimal and scientific-notation boundaries are supported.
  • Arbitrary Boolean logic, custom condition functions, and equality-only point rules are not supported.
  • Expressions must use supported operators, constants, and functions.
  • Multiplication may be implicit or explicit; both 2x and 2*x are accepted.
  • Rule order does not resolve overlaps.
  • Up to 12 rules are supported so live recalculation and graphing remain responsive.
  • Condition gaps and overlaps are checked exactly for the supported interval grammar. Expression-domain failures and sharp graph features still require numerical evaluation.
  • Derivative results use adaptive numerical one-sided checks, not exact symbolic differentiation.
  • Integral results use bounded adaptive quadrature, not exact antiderivatives; difficult or nonconvergent integrals are reported as unavailable.
  • Very small displayed values may be rounded to \(0\), and very large or very small values may be shown in scientific notation.

For homework, exams, engineering work, scientific work, or any decision where accuracy matters, use the calculator as a checking and visualization tool. Verify important boundary points and exact symbolic results separately.


How to Use This Calculator

  1. Enter the \(x\) value where you want to evaluate \(f(x)\).
  2. Enter the graph range as two numbers, such as -5, 5.
  3. Enter each rule’s expression in terms of \(x\).
  4. Enter the condition where each rule applies, such as \(x < 0\), \(0 \le x < 2\), or \(x \ge 2\).
  5. Add or delete rule rows as needed.
  6. Review the displayed function value, matching rule count, active rule, graph, and any gap or overlap warnings.
  7. Treat derivative and integral cards as numerical estimates, especially near endpoints, jumps, corners, gaps, or overlaps.
  8. Download the graph as a PNG if you need to save the visual.

Frequently Asked Questions

What makes a function piecewise?

A function is piecewise when it is defined by different formulas on different parts of its domain. Each formula has a condition that tells when it applies. The input value determines which formula is used.


How do I evaluate a piecewise function?

First, find the condition that contains the given input value. Then substitute the input into that rule’s formula. Do not substitute into every formula unless you are checking for gaps or overlaps.


What happens if no condition matches my x-value?

If no condition matches, the function is undefined at that input. This usually means there is a gap in the intervals or inequalities. Check boundary symbols such as \(<\), \(\le\), \(>\), and \(\ge\) carefully.


What happens if two conditions match the same x-value?

If two conditions match the same input, the setup has an overlap. A well-defined function should give only one output for each input. This calculator treats overlapping rules as ambiguous instead of choosing the first rule automatically.


Why are open and closed endpoints important?

Open and closed endpoints decide whether a boundary value is included in a piece. For example, \(x<2\) excludes \(2\), while \(x\le 2\) includes it. This can change the value of the function and the appearance of the graph.


Can a piecewise function be continuous?

Yes. A piecewise function can be continuous if its pieces connect correctly at their boundary points and the function is defined there. To check a boundary, compare the left-hand behavior, right-hand behavior, and the actual function value at that point.


Are the derivative and integral results exact?

No. The derivative and integral cards are numerical estimates. They can be useful for checking local slope or approximate area, but they are not symbolic derivatives, exact antiderivatives, or formal continuity proofs.


Sources and References

Books

  1. Jay Abramson. College Algebra 2e. OpenStax, 2021. Chapter 3, “Introduction to Functions,” especially function evaluation, domain and range, intervals, and piecewise function key concepts. https://openstax.org/books/college-algebra-2e/pages/3-introduction-to-functions
  2. Gilbert Strang and Edwin “Jed” Herman. Calculus Volume 1. OpenStax, 2016. Sections 1.2 “Basic Classes of Functions,” 2.4 “Continuity,” and 3.1 “Defining the Derivative.” https://openstax.org/books/calculus-volume-1/pages/1-introduction
  3. Gilbert Strang and Edwin “Jed” Herman. Calculus Volume 2. OpenStax, 2016. Section 3.6 “Numerical Integration.” https://openstax.org/books/calculus-volume-2/pages/1-introduction
  4. Qingkai Kong, Timmy Siauw, and Alexandre Bayen. Python Programming and Numerical Methods: A Guide for Engineers and Scientists. 1st ed., Elsevier, 2020. Chapter 20.2, “Finite Difference Approximating Derivatives.” https://shop.elsevier.com/books/python-programming-and-numerical-methods/kong/978-0-12-819549-9

Online and University Sources

  1. Baylor University. “Section 1.2: Piecewise Defined Functions.” Co-requisite Supplement for Calculus I, accessed July 4, 2026. https://openbooks.library.baylor.edu/mth1121/chapter/section-1-2-piecewise-defined-functions/
  2. Berkeley Python Numerical Methods. “Finite Difference Approximating Derivatives,” accessed July 4, 2026. https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter20.02-Finite-Difference-Approximating-Derivatives.html