Piecewise Function Grapher and Calculator
Define up to three different linear or quadratic segments of your piecewise function.
Function Segments
Segment 1
Segment 2
Segment 3
Function Definition:
Enter function details above.
Evaluate at X:
Understanding Piecewise Functions
A piecewise function is a function defined by multiple sub-functions, each applying to a certain interval of the main function's domain. In simpler terms, it's a function that behaves differently on different parts of the number line.
Mathematical Definition
A piecewise function, often denoted as f(x), can be represented as:
f(x) = {
g1(x), if x is in interval I1
g2(x), if x is in interval I2
g3(x), if x is in interval I3
...
}
Where:
f(x)is the overall piecewise function.g1(x),g2(x),g3(x), etc., are the individual functions (often linear or quadratic) that define the pieces.I1,I2,I3, etc., are the intervals (domains) over which each corresponding function applies.
Linear Segments
In this calculator, each segment g(x) is defined by a linear equation of the form y = mx + b, where:
mrepresents the slope of the line.brepresents the y-intercept (the point where the line crosses the y-axis).
The intervals are defined by a start and end X value, and can be inclusive or exclusive at the endpoints.
Interval Notation
The definition of intervals is crucial:
- Inclusive (
[a, b]): The interval includes both endpointsaandb. This is represented asa ≤ x ≤ b. - Left Exclusive (
(a, b]): The interval includes the endpointbbut nota. This is represented asa < x ≤ b. - Right Exclusive (
[a, b)): The interval includes the endpointabut notb. This is represented asa ≤ x < b. - Exclusive (
(a, b)): The interval includes neither endpointanorb. This is represented asa < x < b.
Special values like -infinity and infinity are used to denote unbounded intervals.
Use Cases
Piecewise functions are used in various fields:
- Economics: Tax brackets are a classic example, where the tax rate (function) changes based on income level (interval).
- Physics: Describing motion where velocity or acceleration changes abruptly at certain times.
- Engineering: Modeling systems with different operational modes or conditions.
- Computer Science: Implementing conditional logic or algorithms that behave differently based on input ranges.
- Mathematics Education: Understanding function behavior, graphing, continuity, and limits.
How This Calculator Works
This calculator allows you to define up to three linear segments of a piecewise function. For each segment, you provide:
- The slope (
m) and y-intercept (b) for the linear equationy = mx + b. - The start and end X values for the interval.
- The type of interval (inclusive/exclusive endpoints).
The calculator then constructs the mathematical definition of the piecewise function based on your inputs. You can also input a specific X value to find the corresponding Y value of the function.
y = ' + slope + 'x + ' + intercept + '';
var intervalPart = formatInterval(parsedStart, parsedEnd, type);
equationParts.push(funcPart + ', for ' + intervalPart);
}
if (errorMessage) {
document.getElementById('error-message').textContent = errorMessage;
document.getElementById('function-equation').innerHTML = 'Error in definition.';
document.getElementById('evaluation-result').textContent = ";
return;
}
if (segmentsData.length === 0) {
document.getElementById('error-message').textContent = "Please define at least one function segment.";
document.getElementById('function-equation').innerHTML = 'No function defined.';
document.getElementById('evaluation-result').textContent = ";
return;
}
// Check for overlapping intervals after validation
if (checkIntervalOverlap(segmentsData)) {
errorMessage += "Overlapping or invalid intervals detected between segments. ";
document.getElementById('error-message').textContent = errorMessage;
document.getElementById('function-equation').innerHTML = 'Error: Interval conflict.';
document.getElementById('evaluation-result').textContent = ";
return;
}
document.getElementById('error-message').textContent = "; // Clear previous errors
document.getElementById('function-equation').innerHTML = equationParts.join(");
document.getElementById('evaluation-result').textContent = "; // Clear previous evaluation
}
function evaluateFunction() {
var evaluateXInput = document.getElementById('evaluateX');
var evaluateX = evaluateXInput.value;
var xValue = parseXValue(evaluateX);
var resultText = ";
var errorMessage = ";
if (evaluateX === ") {
errorMessage = "Please enter a value for X to evaluate.";
} else if (isNaN(xValue)) {
errorMessage = "Invalid input for X value. Please enter a number or '-infinity'/'infinity'.";
} else {
var segments = [];
for (var i = 1; i <= 3; i++) {
var startInput = document.getElementById('start' + i);
var endInput = document.getElementById('end' + i);
var typeSelect = document.getElementById('type' + i);
var slopeInput = document.getElementById('slope' + i);
var interceptInput = document.getElementById('intercept' + i);
// Only consider segments that have valid numerical inputs for slope and intercept
if (isNumeric(slopeInput.value) && isNumeric(interceptInput.value)) {
segments.push({
slope: parseFloat(slopeInput.value),
intercept: parseFloat(interceptInput.value),
start: parseXValue(startInput.value),
end: parseXValue(endInput.value),
type: typeSelect.value
});
}
}
var foundSegment = false;
for (var j = 0; j = seg.start && xValue seg.start && xValue = seg.start && xValue seg.start && xValue < seg.end) inInterval = true;
break;
}
if (inInterval) {
var yValue = seg.slope * xValue + seg.intercept;
resultText = 'At x = ' + formatXValue(xValue) + ', y = ' + yValue.toFixed(4); // Format to 4 decimal places
foundSegment = true;
break; // Found the segment, stop searching
}
}
if (!foundSegment) {
resultText = 'The value x = ' + formatXValue(xValue) + ' is not within any defined interval.';
}
}
if (errorMessage) {
document.getElementById('error-message').textContent = errorMessage;
document.getElementById('evaluation-result').textContent = ";
} else {
document.getElementById('error-message').textContent = "; // Clear errors
document.getElementById('evaluation-result').innerHTML = resultText;
}
}
// Initial call to display the default function equation (or placeholder)
document.addEventListener('DOMContentLoaded', function() {
calculateAndDisplay();
});