Enter the coordinates for two points $(x_1, y_1)$ and $(x_2, y_2)$ to find the slope (m) and y-intercept (c).
Results
Gradient (m):
Y-Intercept (c):
Equation:
Understanding the Gradient-Intercept Form
The gradient-intercept form is one of the most common ways to express the equation of a straight line. Mathematically, it is written as y = mx + c (or y = mx + b in some regions). This formula allows us to understand the behavior of a line simply by looking at its constants.
Key Components
m (Gradient/Slope): This represents the steepness of the line. It is the "rise over run" – how much the y-value changes for every one unit change in the x-value.
c (Y-Intercept): This is the point where the line crosses the Y-axis. At this point, the value of x is always zero.
How to Calculate Gradient and Intercept Manually
To find the equation of a line passing through two points $(x_1, y_1)$ and $(x_2, y_2)$, follow these steps:
Find the Gradient (m): Use the formula $m = (y_2 – y_1) / (x_2 – x_1)$.
Find the Y-Intercept (c): Once you have $m$, substitute it and one point into the equation $y = mx + c$ and solve for $c$. The formula is $c = y_1 – (m \times x_1)$.
Real-World Example
Suppose you have two points: Point A (2, 5) and Point B (4, 11).
Gradient (m): $(11 – 5) / (4 – 2) = 6 / 2 = 3$.
Intercept (c): $5 = (3 \times 2) + c \rightarrow 5 = 6 + c \rightarrow c = -1$.
Result: The equation is y = 3x – 1.
Our calculator automates this process, providing instant results for any set of coordinates, including negative numbers and decimals.
function calculateLinearEquation() {
var x1 = parseFloat(document.getElementById('x1_val').value);
var y1 = parseFloat(document.getElementById('y1_val').value);
var x2 = parseFloat(document.getElementById('x2_val').value);
var y2 = parseFloat(document.getElementById('y2_val').value);
var resultsDiv = document.getElementById('results_area');
var errorDiv = document.getElementById('error_area');
// Reset displays
resultsDiv.style.display = 'none';
errorDiv.style.display = 'none';
// Validation
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
errorDiv.textContent = "Please enter valid numerical values for all coordinates.";
errorDiv.style.display = 'block';
return;
}
if (x1 === x2) {
if (y1 === y2) {
errorDiv.textContent = "The points are identical. A unique line cannot be determined.";
} else {
errorDiv.textContent = "This is a vertical line (x = " + x1 + "). It has an undefined gradient.";
}
errorDiv.style.display = 'block';
return;
}
// Calculation logic
var m = (y2 – y1) / (x2 – x1);
var c = y1 – (m * x1);
// Formatting for display
var mDisp = Number.isInteger(m) ? m : m.toFixed(4).replace(/\.?0+$/, "");
var cDisp = Number.isInteger(c) ? c : c.toFixed(4).replace(/\.?0+$/, "");
var equation = "y = " + mDisp + "x";
if (parseFloat(cDisp) > 0) {
equation += " + " + cDisp;
} else if (parseFloat(cDisp) < 0) {
equation += " – " + Math.abs(cDisp);
} else if (parseFloat(cDisp) === 0) {
// Don't add anything if c is 0
}
// Handle special case where m is 0
if (parseFloat(mDisp) === 0) {
equation = "y = " + cDisp;
}
// Handle special case where m is 1 or -1
if (parseFloat(mDisp) === 1) {
equation = equation.replace("1x", "x");
} else if (parseFloat(mDisp) === -1) {
equation = equation.replace("-1x", "-x");
}
document.getElementById('res_m').textContent = mDisp;
document.getElementById('res_c').textContent = cDisp;
document.getElementById('res_eq').textContent = equation;
resultsDiv.style.display = 'block';
}