function calculateSlope() {
var x1Input = document.getElementById('x1_val');
var y1Input = document.getElementById('y1_val');
var x2Input = document.getElementById('x2_val');
var y2Input = document.getElementById('y2_val');
var x1 = parseFloat(x1Input.value);
var y1 = parseFloat(y1Input.value);
var x2 = parseFloat(x2Input.value);
var y2 = parseFloat(y2Input.value);
var resultArea = document.getElementById('result-area');
var slopeDisplay = document.getElementById('final-slope');
var equationDisplay = document.getElementById('line-equation');
var stepsDisplay = document.getElementById('calc-steps');
var unitRateText = document.getElementById('unit-rate-text');
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
alert("Please enter valid numbers for all X and Y coordinates.");
return;
}
resultArea.style.display = "block";
var deltaY = y2 – y1;
var deltaX = x2 – x1;
// Handle vertical lines (undefined slope)
if (deltaX === 0) {
slopeDisplay.innerHTML = "Undefined";
unitRateText.innerHTML = "Vertical Line";
equationDisplay.innerHTML = "x = " + x1;
stepsDisplay.innerHTML = "1. Formula: m = (y₂ – y₁) / (x₂ – x₁)" +
"2. Substitute: (" + y2 + " – " + y1 + ") / (" + x2 + " – " + x1 + ")" +
"3. Result: " + deltaY + " / 0″ +
"4. Division by zero is undefined.";
return;
}
var slope = deltaY / deltaX;
// Calculate Y-intercept (b) -> y = mx + b -> b = y – mx
var intercept = y1 – (slope * x1);
// Formatting Output
var slopeFormatted = Number.isInteger(slope) ? slope : slope.toFixed(4);
var interceptFormatted = Number.isInteger(intercept) ? Math.abs(intercept) : Math.abs(intercept).toFixed(4);
var interceptSign = intercept >= 0 ? "+ " : "- ";
// Build Equation String
var eqString = "y = " + slopeFormatted + "x";
if (intercept !== 0) {
eqString += " " + interceptSign + interceptFormatted;
} else if (slope === 0) {
eqString = "y = " + y1;
}
// Display Results
slopeDisplay.innerHTML = slopeFormatted;
unitRateText.innerHTML = "Rate of change: " + slopeFormatted + " units of Y per unit of X";
equationDisplay.innerHTML = eqString;
// Step-by-Step Logic
var stepHTML = "Step 1: Find the change in variables";
stepHTML += "Change in Y (Rise) = y₂ – y₁ = " + y2 + " – " + y1 + " = " + deltaY + "";
stepHTML += "Change in X (Run) = x₂ – x₁ = " + x2 + " – " + x1 + " = " + deltaX + "";
stepHTML += "Step 2: Calculate Slope (Unit Rate)";
stepHTML += "m = Δy / Δx = " + deltaY + " / " + deltaX + " = " + slopeFormatted + "";
stepHTML += "Step 3: Find Y-Intercept (b)";
stepHTML += "Formula: b = y₁ – (m * x₁)";
stepHTML += "b = " + y1 + " – (" + slopeFormatted + " * " + x1 + ")";
stepHTML += "b = " + y1 + " – (" + (slope * x1).toFixed(4) + ")";
stepHTML += "b = " + (intercept).toFixed(4) + "";
stepHTML += "Step 4: Write Equation";
stepHTML += "y = mx + b → " + eqString;
stepsDisplay.innerHTML = stepHTML;
}
function resetCalculator() {
document.getElementById('x1_val').value = ";
document.getElementById('y1_val').value = ";
document.getElementById('x2_val').value = ";
document.getElementById('y2_val').value = ";
document.getElementById('result-area').style.display = "none";
}
Understanding Unit Rate and Slope
Whether you are solving algebra problems or analyzing real-world data like speed or cost, understanding the relationship between two variables is essential. This Unit Rate and Slope Calculator helps you determine the rate of change between two coordinate points and defines the linear equation that connects them.
What is Slope?
In mathematics, the slope (often denoted as m) represents the steepness and direction of a line. It defines how much the dependent variable (Y) changes for every single unit increase in the independent variable (X). This is commonly referred to as "Rise over Run."
Slope (m) = (y₂ – y₁) / (x₂ – x₁)
Relationship to Unit Rate
The term Unit Rate is often used in real-world applications of slope. While slope is a geometric property of a line, unit rate puts it into context.
Example 1: If Y is distance (miles) and X is time (hours), the slope represents the speed (miles per hour).
Example 2: If Y is total cost ($) and X is the number of items purchased, the slope represents the price per item (unit price).
How to Use This Calculator
Calculating the unit rate and slope is straightforward if you have two known data points. Follow these steps to use the tool above:
Identify Point 1: Enter the first X and Y coordinate values in the first section. For example, if you drove 100 miles in 2 hours, X₁=2 and Y₁=100.
Identify Point 2: Enter the second set of coordinates. If you later drove 200 miles in 4 hours, X₂=4 and Y₂=200.
Calculate: Click the "Calculate Unit Rate" button.
Analyze: The tool will provide the slope (unit rate), the Y-intercept (starting value), and the full linear equation ($y = mx + b$).
Interpreting the Results
Positive Slope: Indicates an upward trend (as X increases, Y increases).
Negative Slope: Indicates a downward trend (as X increases, Y decreases).
Zero Slope: Indicates a horizontal line (Y remains constant regardless of X).
Undefined Slope: Indicates a vertical line (X remains constant, suggesting infinite change in Y).
Real-World Calculation Example
Let's say you are analyzing the cost of a subscription service. After 1 month (x₁), you have paid $20 (y₁). After 5 months (x₂), you have paid $100 (y₂).