Linear inequalities are fundamental in mathematics and have wide-ranging applications in fields like economics, operations research, and computer science. They represent a region on a coordinate plane where a set of points satisfy a given condition, rather than a single line or curve.
What is a Linear Inequality?
A linear inequality in two variables, say x and y, is an inequality that can be written in one of the following forms:
ax + by < c
ax + by ≤ c
ax + by > c
ax + by ≥ c
where a, b, and c are constants, and a and b are not both zero.
How to Graph a Linear Inequality: The Steps
Rewrite the inequality as an equation: Replace the inequality sign (<, ≤, >, ≥) with an equals sign (=) to get the equation of the boundary line. For example, if the inequality is 2x + 3y < 6, the boundary line is 2x + 3y = 6.
Graph the boundary line:
Find the x-intercept: Set y = 0 and solve for x.
Find the y-intercept: Set x = 0 and solve for y.
Plot these two points and draw a straight line through them.
Determine if the line is solid or dashed: If the inequality is ≤ or ≥ (includes "or equal to"), the line is solid, meaning points on the line are part of the solution. If the inequality is < or > (strict inequality), the line is dashed, meaning points on the line are not part of the solution.
Choose a test point: Select any point that is NOT on the boundary line. The easiest point to choose is usually the origin (0,0), unless the line passes through it.
Substitute the test point into the original inequality: Plug the coordinates of the test point into the inequality.
Shade the correct region:
If the test point makes the inequality true, shade the region that contains the test point.
If the test point makes the inequality false, shade the region on the opposite side of the boundary line.
Using the Calculator
This calculator helps visualize the solution region of a linear inequality.
Enter your inequality in the format ax + by < c (or ≤, >, ≥).
(Optional) Enter a test point (x, y). If provided, the calculator will evaluate if this point satisfies the inequality and indicate whether it falls into the shaded region.
Click "Graph Inequality" to get a description of the boundary line and the solution region.
Example Usage
Let's graph the inequality x - 2y ≥ 4:
Inequality: x - 2y ≥ 4
Test Point X: 0
Test Point Y: 0
Steps:
Boundary line equation: x - 2y = 4
Intercepts:
x-intercept: Set y=0, x = 4. Point: (4, 0)
y-intercept: Set x=0, -2y = 4, y = -2. Point: (0, -2)
Line type: Since it's ≥, the line is solid.
Test point: Choose (0,0).
Substitution: 0 - 2(0) ≥ 4 which simplifies to 0 ≥ 4. This is false.
Shading: Since (0,0) is false, shade the region that does not contain the origin.
The calculator will output a description based on these steps.
function calculateInequality() {
var equationInput = document.getElementById("equation").value.trim();
var testPointXInput = document.getElementById("testPointX").value;
var testPointYInput = document.getElementById("testPointY").value;
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (!equationInput) {
resultDiv.innerHTML = "Please enter an inequality.";
return;
}
// — Parsing the Inequality —
// Regular expression to capture 'a', 'op', 'b', 'c' from ax + by op c
// Handles variations like x, y, ax, by, constant terms, spaces, and inequality signs
var regex = /^\s*([+-]?\s*\d*\.?\d*)?\s*x\s*([+-]\s*\d*\.?\d*)?\s*y\s*([≤≥])\s*([+-]?\s*\d*\.?\d*)\s*$/i;
var match = equationInput.match(regex);
if (!match) {
// Try parsing standard form y = mx + c or x = c
// Standard form: y = mx + c or x = k
var standardFormRegex = /^\s*([+-]?\s*\d*\.?\d*)?\s*y\s*([≤≥])\s*([+-]?\s*\d*\.?\d*)?\s*x\s*([+-]\s*\d*\.?\d*)\s*$/i; // y op mx + c
var xFormRegex = /^\s*x\s*([≤≥])\s*([+-]?\s*\d*\.?\d*)\s*$/i; // x op k
var yFormRegex = /^\s*y\s*([≤≥])\s*([+-]?\s*\d*\.?\d*)\s*$/i; // y op k
var parsed = false;
// Case 1: y op mx + c
var standardMatch = equationInput.match(standardFormRegex);
if (standardMatch) {
var m = parseFloat(standardMatch[1] || (standardMatch[2] && standardMatch[2].includes('x') ? '1' : (standardMatch[2] && standardMatch[2].includes('-x') ? '-1' : 0))) || 0; // coefficient of x
var op = standardMatch[3]; // inequality sign
var c = parseFloat(standardMatch[4] || '0'); // constant term
var a = -m; // coefficient of x for ax + by form
var b = 1; // coefficient of y for ax + by form
var parsedEquation = (a === 1 ? " : (a === -1 ? '-' : a)) + 'x' + (b === 1 ? " : (b === -1 ? '-' : b)) + 'y' + op + c;
match = [null, a, b, op, c]; // Re-assign to generic structure for simpler processing later
parsed = true;
}
// Case 2: x op k
var xMatch = equationInput.match(xFormRegex);
if (!parsed && xMatch) {
var op = xMatch[1];
var k = parseFloat(xMatch[2] || '0');
match = [null, 1, 0, op, k]; // Treat as 1x + 0y op k
parsed = true;
}
// Case 3: y op k
var yMatch = equationInput.match(yFormRegex);
if (!parsed && yMatch) {
var op = yMatch[1];
var k = parseFloat(yMatch[2] || '0');
match = [null, 0, 1, op, k]; // Treat as 0x + 1y op k
parsed = true;
}
if (!parsed) {
resultDiv.innerHTML = "Invalid equation format. Please use formats like 'ax + by mx + b', 'x >= k', or 'y <= k'.";
return;
}
}
var a = parseFloat(match[1] ? match[1].replace(/\s/g, '') : (match[2] && (match[2].includes('x') || match[2].includes('y')) ? (match[2].includes('-') ? -1 : 1) : 0));
var b = parseFloat(match[2] ? match[2].replace(/\s/g, '') : (match[1] && (match[1].includes('x') || match[1].includes('y')) ? (match[1].includes('-') ? -1 : 1) : 0));
var operator = match[3];
var c = parseFloat(match[4] ? match[4].replace(/\s/g, '') : '0');
// Correctly parse coefficients if 'x' or 'y' are present without explicit numbers (e.g., 'x', '-y')
if (typeof match[1] === 'string') {
if (match[1].trim() === '' || match[1].trim() === '+') a = 1;
else if (match[1].trim() === '-') a = -1;
else if (match[1].toLowerCase().includes('x')) { // Check if it's just 'x' or '-x' etc.
if(match[1].toLowerCase() === 'x') a = 1;
else if(match[1].toLowerCase() === '-x') a = -1;
else a = parseFloat(match[1].replace(/\s/g, '').replace('x', '')) || 0;
} else a = parseFloat(match[1].replace(/\s/g, '')) || 0;
} else { // Handle cases where 'x' or 'y' are implicitly 0
if (equationInput.toLowerCase().includes('x') && equationInput.toLowerCase().indexOf('y') === -1) { // only x term
a = parseFloat(match[1] ? match[1].replace(/\s/g, '') : (match[2] ? match[2].replace(/\s/g, '') : 0));
if (isNaN(a)) { // check for x or -x without number
if (match[1] && match[1].trim() === '') a = 1;
else if (match[1] && match[1].trim() === '-') a = -1;
else a = 0;
}
b = 0;
} else if (equationInput.toLowerCase().includes('y') && equationInput.toLowerCase().indexOf('x') === -1) { // only y term
b = parseFloat(match[2] ? match[2].replace(/\s/g, '') : (match[1] ? match[1].replace(/\s/g, '') : 0));
if (isNaN(b)) { // check for y or -y without number
if (match[2] && match[2].trim() === '') b = 1;
else if (match[2] && match[2].trim() === '-') b = -1;
else b = 0;
}
a = 0;
} else { // both x and y are present
a = parseFloat(match[1] ? match[1].replace(/\s/g, '').replace('x', '') : (match[1] && match[1].trim() === '' ? 1 : (match[1] && match[1].trim() === '-' ? -1 : 0))) || 0;
b = parseFloat(match[2] ? match[2].replace(/\s/g, '').replace('y', '') : (match[2] && match[2].trim() === '' ? 1 : (match[2] && match[2].trim() === '-' ? -1 : 0))) || 0;
}
}
// Handle implicit coefficients like 'x' or '-y'
if (match[1] && typeof match[1] === 'string') {
if (match[1].trim() === '' || match[1].trim() === '+') a = 1;
else if (match[1].trim() === '-') a = -1;
else if (match[1].toLowerCase().includes('x')) {
if (match[1].trim().toLowerCase() === 'x') a = 1;
else if (match[1].trim().toLowerCase() === '-x') a = -1;
else a = parseFloat(match[1].replace(/\s/g, '').replace('x','')) || 0;
} else a = parseFloat(match[1].replace(/\s/g, '')) || 0;
}
if (match[2] && typeof match[2] === 'string') {
if (match[2].trim() === '' || match[2].trim() === '+') b = 1;
else if (match[2].trim() === '-') b = -1;
else if (match[2].toLowerCase().includes('y')) {
if (match[2].trim().toLowerCase() === 'y') b = 1;
else if (match[2].trim().toLowerCase() === '-y') b = -1;
else b = parseFloat(match[2].replace(/\s/g, '').replace('y','')) || 0;
} else b = parseFloat(match[2].replace(/\s/g, '')) || 0;
}
// Ensure 'c' is parsed correctly, especially if it's the only term after the operator
if (match[4] && typeof match[4] === 'string') {
c = parseFloat(match[4].replace(/\s/g, '')) || 0;
}
if (isNaN(a) || isNaN(b) || isNaN(c)) {
resultDiv.innerHTML = "Could not parse coefficients. Please ensure the format is correct.";
return;
}
var boundaryLine = "";
var solidOrDashed = "";
var inequalityType = "";
// Determine boundary line properties
if (b !== 0) { // Standard form: y = mx + c (or close to it)
var slope = -a / b;
var yIntercept = c / b;
boundaryLine = "" + (a === 0 ? "" : (a === 1 ? "x" : (a === -1 ? "-x" : a + "x"))) + (b === 0 ? "" : (b > 0 ? (a === 0 ? "" : "+") + (b === 1 ? "y" : (b === -1 ? "-y" : b + "y")) : (b === -1 ? "-y" : b + "y"))) + " = " + c + "";
if (operator === ") {
solidOrDashed = "dashed";
inequalityType = "strict";
} else { // =
solidOrDashed = "solid";
inequalityType = "inclusive";
}
} else if (a !== 0) { // Vertical line: x = k
var xIntercept = c / a;
boundaryLine = "" + (a === 1 ? "x" : (a === -1 ? "-x" : a + "x")) + " = " + c + "";
if (operator === ") {
solidOrDashed = "dashed";
inequalityType = "strict";
} else { // =
solidOrDashed = "solid";
inequalityType = "inclusive";
}
} else { // Should not happen if a and b are not both zero, but for safety
resultDiv.innerHTML = "Invalid equation: coefficients a and b cannot both be zero.";
return;
}
var resultText = "The boundary line is " + boundaryLine + " (a " + solidOrDashed + " line). ";
// — Test Point Evaluation —
var testPointValid = false;
var testPointTrue = false;
if (testPointXInput !== "" && testPointYInput !== "") {
var testX = parseFloat(testPointXInput);
var testY = parseFloat(testPointYInput);
if (!isNaN(testX) && !isNaN(testY)) {
testPointValid = true;
var testResult = a * testX + b * testY;
if (operator === '<') testPointTrue = testResult < c;
else if (operator === '<=') testPointTrue = testResult ') testPointTrue = testResult > c;
else if (operator === '>=') testPointTrue = testResult >= c;
resultText += "For the test point (" + testX + ", " + testY + "):";
resultText += "" + a + "(" + testX + ") + " + b + "(" + testY + ") " + operator + " " + c + " evaluates to " + testResult + ".";
if (testPointTrue) {
resultText += "This point satisfies the inequality. Shade the region containing this point.";
} else {
resultText += "This point does not satisfy the inequality. Shade the region on the opposite side of the line.";
}
} else {
resultText += "Invalid test point values entered.";
}
} else {
resultText += "To determine the shaded region, choose a test point not on the line and substitute its coordinates into the original inequality.";
}
resultDiv.innerHTML = resultText;
}