function calculateRateOfChange() {
// 1. Get input values
var x1 = document.getElementById('x1_input').value;
var y1 = document.getElementById('y1_input').value;
var x2 = document.getElementById('x2_input').value;
var y2 = document.getElementById('y2_input').value;
var errorDiv = document.getElementById('aroc_error');
var resultsDiv = document.getElementById('aroc_results');
// 2. Validate inputs
if (x1 === "" || y1 === "" || x2 === "" || y2 === "") {
errorDiv.style.display = 'block';
errorDiv.innerHTML = "Please enter values for all X and Y coordinates.";
resultsDiv.style.display = 'none';
return;
}
// Convert to floats
x1 = parseFloat(x1);
y1 = parseFloat(y1);
x2 = parseFloat(x2);
y2 = parseFloat(y2);
// Check for Division by Zero
if (x1 === x2) {
errorDiv.style.display = 'block';
errorDiv.innerHTML = "Undefined Slope: x₁ and x₂ cannot be the same value (division by zero).";
resultsDiv.style.display = 'none';
return;
}
// 3. Perform Calculations
var deltaY = y2 – y1;
var deltaX = x2 – x1;
var rateOfChange = deltaY / deltaX;
// Calculate Y-intercept for Secant Line Equation: y = mx + b => b = y – mx
var b_intercept = y1 – (rateOfChange * x1);
var operator = b_intercept >= 0 ? "+ " : "- ";
var abs_intercept = Math.abs(b_intercept);
// Format equation string
// Round for display clarity
var displayRate = Number.isInteger(rateOfChange) ? rateOfChange : rateOfChange.toFixed(4);
var displayIntercept = Number.isInteger(abs_intercept) ? abs_intercept : abs_intercept.toFixed(4);
var equationString = "y = " + displayRate + "x " + operator + displayIntercept;
// 4. Update UI
errorDiv.style.display = 'none';
resultsDiv.style.display = 'block';
document.getElementById('aroc_final_value').innerText = displayRate;
document.getElementById('delta_y_value').innerText = Number.isInteger(deltaY) ? deltaY : deltaY.toFixed(4);
document.getElementById('delta_x_value').innerText = Number.isInteger(deltaX) ? deltaX : deltaX.toFixed(4);
document.getElementById('secant_equation').innerText = equationString;
}
Understanding the Average Rate of Change
The Average Rate of Change (AROC) is a fundamental concept in calculus, algebra, and physics. It measures how much a function or quantity changes on average over a specific interval. Geometrically, this rate represents the slope of the secant line connecting two points on a curve.
The Formula
To calculate the average rate of change between two points, $(x_1, y_1)$ and $(x_2, y_2)$, we use the following formula:
AROC = (y₂ – y₁) / (x₂ – x₁) = Δy / Δx
Where:
Δy (Delta Y): The change in the dependent variable (Output).
Δx (Delta X): The change in the independent variable (Input).
Real-World Example
Imagine a car traveling on a highway. We can calculate the average speed (which is a rate of change of distance over time) using two data points.
Point 1: At 2 hours (x₁), the car has traveled 120 miles (y₁).
Point 2: At 5 hours (x₂), the car has traveled 300 miles (y₂).
Step 1: Calculate the change in distance (Δy)
300 miles – 120 miles = 180 miles
Step 2: Calculate the change in time (Δx)
5 hours – 2 hours = 3 hours
Step 3: Divide Δy by Δx
180 / 3 = 60 miles per hour.
The average rate of change is 60 mph. This means that, on average, the car's position changed by 60 miles for every hour that passed during that interval.
Why is this important?
The average rate of change allows us to analyze trends in data. In economics, it helps determine the average profit growth over a year. In physics, it defines velocity. In biology, it can measure the growth rate of a population over time.
Interpreting the Results
Positive Rate: The function is increasing on average over the interval.
Negative Rate: The function is decreasing on average over the interval.
Zero Rate: There is no net change over the interval (the starting and ending y-values are the same).