A linear equation is an algebraic equation in which each term is either a constant or the product of a constant and a single variable. In a two-dimensional coordinate system, a linear equation represents a straight line. The most common form is the Slope-Intercept Form, expressed as:
y = mx + b
Understanding the Components
m (Slope): This represents the steepness of the line. It is calculated as the "rise over run," or the change in Y divided by the change in X.
b (Y-Intercept): This is the point where the line crosses the Y-axis (where x = 0).
(x, y): Any coordinate point that lies on the line.
How to Calculate the Slope and Intercept
If you have two points, (x₁, y₁) and (x₂, y₂), you can find the characteristics of the line using these steps:
Find the Slope (m): m = (y₂ – y₁) / (x₂ – x₁)
Find the Y-Intercept (b): b = y₁ – (m * x₁)
Assemble the Equation: Plug m and b into the y = mx + b formula.
Practical Example
Suppose you have two points: (1, 2) and (3, 6).
Slope: (6 – 2) / (3 – 1) = 4 / 2 = 2
Intercept: 2 – (2 * 1) = 0
Equation: y = 2x + 0 (or simply y = 2x)
Using this equation, if you wanted to find the value of Y when X is 5, you would calculate: y = 2 * 5 = 10.
function calculateLinear() {
var x1 = parseFloat(document.getElementById('x1').value);
var y1 = parseFloat(document.getElementById('y1').value);
var x2 = parseFloat(document.getElementById('x2').value);
var y2 = parseFloat(document.getElementById('y2').value);
var targetX = document.getElementById('targetX').value;
var resultDiv = document.getElementById('linearResult');
var resSlope = document.getElementById('resSlope');
var resIntercept = document.getElementById('resIntercept');
var resEquation = document.getElementById('resEquation');
var interpRes = document.getElementById('interpolationResult');
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
alert('Please enter valid numerical coordinates for both points.');
return;
}
if (x1 === x2) {
resultDiv.style.display = 'block';
resSlope.innerHTML = 'Undefined (Vertical Line)';
resIntercept.innerHTML = 'None (or all points if x=0)';
resEquation.innerHTML = 'x = ' + x1;
interpRes.innerHTML = 'Note: This is a vertical line. Y can be any value at x = ' + x1;
return;
}
// Calculate Slope (m)
var m = (y2 – y1) / (x2 – x1);
// Calculate Y-Intercept (b)
var b = y1 – (m * x1);
// Format for display
var mDisp = +m.toFixed(4);
var bDisp = +b.toFixed(4);
var sign = bDisp >= 0 ? "+ " : "- ";
var bAbs = Math.abs(bDisp);
var equationString = "y = " + mDisp + "x " + sign + bAbs;
if (bDisp === 0) {
equationString = "y = " + mDisp + "x";
}
resSlope.innerHTML = mDisp;
resIntercept.innerHTML = bDisp;
resEquation.innerHTML = equationString;
// Handle Interpolation / Extrapolation
if (targetX !== "") {
var tx = parseFloat(targetX);
if (!isNaN(tx)) {
var ty = (m * tx) + b;
interpRes.innerHTML = "Result: When x = " + tx + ", y = " + (+ty.toFixed(4)) + "";
} else {
interpRes.innerHTML = "";
}
} else {
interpRes.innerHTML = "Enter a target X value above to solve for Y.";
}
resultDiv.style.display = 'block';
}