Calculate the constant rate of change (slope) from two points on a graph.
Coordinate Point 1 (x₁, y₁)
Coordinate Point 2 (x₂, y₂)
Please enter valid numeric coordinates.
Calculation Results
Unit Rate (Slope m)
–
Change in Y (Rise)
–
Change in X (Run)
–
Equation of Line
–
Step-by-Step Logic:
Understanding Unit Rate on a Graph
In algebra and physics, finding the unit rate on a graph is equivalent to finding the slope of the line. The unit rate describes how much the dependent variable (usually represented on the Y-axis) changes for every single unit increase in the independent variable (represented on the X-axis).
How to Calculate Unit Rate from a Graph
To calculate the unit rate, you need to identify two distinct points on the line shown on your graph. Let's call these points $(x_1, y_1)$ and $(x_2, y_2)$. The formula used is often referred to as "Rise over Run":
Unit Rate (m) = (y₂ – y₁) / (x₂ – x₁)
This formula calculates the change in the vertical direction divided by the change in the horizontal direction.
Key Concepts
Rise (Δy): The vertical change between two points ($y_2 – y_1$).
Run (Δx): The horizontal change between two points ($x_2 – x_1$).
Proportional Relationships: If the line passes through the origin $(0,0)$, the unit rate is simply $y / x$ for any point on the line.
Real-World Examples of Unit Rate
Unit rates are used constantly in daily life to compare performance and cost:
Speed: If you graph distance (y) vs. time (x), the unit rate is speed (e.g., miles per hour).
Pricing: If you graph total cost (y) vs. quantity purchased (x), the unit rate is the price per item.
Wages: Graphing total earnings (y) vs. hours worked (x) reveals the hourly wage.
Interpreting the Results
If your calculation results in a positive number, the line on the graph is going up from left to right, indicating growth or increase. If the result is negative, the line is going down, indicating a decrease or decay. A steeper line indicates a higher unit rate of change.
function calculateUnitRate() {
// Retrieve inputs using var
var x1 = document.getElementById('x1_val').value;
var y1 = document.getElementById('y1_val').value;
var x2 = document.getElementById('x2_val').value;
var y2 = document.getElementById('y2_val').value;
var unitLabel = document.getElementById('units_label').value;
var errorDiv = document.getElementById('error-message');
var resultsDiv = document.getElementById('results');
// Parse inputs to floats
var x1Num = parseFloat(x1);
var y1Num = parseFloat(y1);
var x2Num = parseFloat(x2);
var y2Num = parseFloat(y2);
// Validation: Check for empty strings or NaN
if (isNaN(x1Num) || isNaN(y1Num) || isNaN(x2Num) || isNaN(y2Num)) {
errorDiv.style.display = 'block';
resultsDiv.style.display = 'none';
return;
}
// Hide error if validation passes
errorDiv.style.display = 'none';
resultsDiv.style.display = 'block';
// Core Calculation Logic
var rise = y2Num – y1Num;
var run = x2Num – x1Num;
var slope = 0;
var isUndefined = false;
// Check for vertical line (division by zero)
if (run === 0) {
isUndefined = true;
} else {
slope = rise / run;
}
// Calculate Y-intercept (b = y – mx)
var yIntercept = 0;
if (!isUndefined) {
yIntercept = y1Num – (slope * x1Num);
}
// Update UI
if (isUndefined) {
document.getElementById('res-slope').innerHTML = "Undefined";
document.getElementById('res-rise').innerHTML = rise.toFixed(2);
document.getElementById('res-run').innerHTML = "0";
document.getElementById('res-equation').innerHTML = "x = " + x1Num;
document.getElementById('step-1').innerHTML = "Δy (Rise) = " + y2Num + " – " + y1Num + " = " + rise;
document.getElementById('step-2').innerHTML = "Δx (Run) = " + x2Num + " – " + x1Num + " = 0″;
document.getElementById('step-3').innerHTML = "Slope = " + rise + " / 0 = Undefined (Vertical Line)";
} else {
// Format numbers to remove trailing zeros if integer, else fixed to 2 decimals
var slopeDisplay = Number.isInteger(slope) ? slope : slope.toFixed(2);
var riseDisplay = Number.isInteger(rise) ? rise : rise.toFixed(2);
var runDisplay = Number.isInteger(run) ? run : run.toFixed(2);
var interceptDisplay = Number.isInteger(yIntercept) ? yIntercept : yIntercept.toFixed(2);
// Construct Equation String (y = mx + b)
var operator = yIntercept >= 0 ? "+ " : "- ";
var absIntercept = Math.abs(yIntercept);
var equationStr = "y = " + slopeDisplay + "x " + operator + (Number.isInteger(absIntercept) ? absIntercept : absIntercept.toFixed(2));
// Append optional unit label
var finalSlopeDisplay = slopeDisplay;
if (unitLabel && unitLabel.trim() !== "") {
finalSlopeDisplay += " (" + unitLabel + ")";
}
document.getElementById('res-slope').innerHTML = finalSlopeDisplay;
document.getElementById('res-rise').innerHTML = riseDisplay;
document.getElementById('res-run').innerHTML = runDisplay;
document.getElementById('res-equation').innerHTML = equationStr;
// Step Logic Display
document.getElementById('step-1').innerHTML = "Δy (Rise) = " + y2Num + " – " + y1Num + " = " + riseDisplay;
document.getElementById('step-2').innerHTML = "Δx (Run) = " + x2Num + " – " + x1Num + " = " + runDisplay;
document.getElementById('step-3').innerHTML = "Unit Rate = " + riseDisplay + " / " + runDisplay + " = " + slopeDisplay;
}
}