Average Rate of Change Between Two Points Calculator

Average Rate of Change Calculator

The average rate of change between two points on a function measures how much the function's output changes for each unit of change in its input. It essentially tells you the slope of the secant line connecting these two points. For a function f(x), given two points (x1, y1) and (x2, y2), where y1 = f(x1) and y2 = f(x2), the average rate of change is calculated as:

Average Rate of Change = (y2 – y1) / (x2 – x1)

function calculateAverageRateOfChange() { 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 resultDiv = document.getElementById("result"); if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) { resultDiv.innerHTML = "Please enter valid numbers for all inputs."; return; } if (x2 – x1 === 0) { resultDiv.innerHTML = "Cannot calculate: Division by zero (x2 cannot equal x1)."; return; } var averageRateOfChange = (y2 – y1) / (x2 – x1); resultDiv.innerHTML = "The average rate of change is: " + averageRateOfChange.toFixed(4); } .calculator-container { font-family: sans-serif; border: 1px solid #ccc; padding: 20px; border-radius: 8px; max-width: 500px; margin: 20px auto; background-color: #f9f9f9; } .calculator-container h2 { text-align: center; color: #333; margin-bottom: 15px; } .input-section { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin-bottom: 20px; } .input-section label { display: block; margin-bottom: 5px; font-weight: bold; color: #555; } .input-section input[type="number"] { width: calc(100% – 12px); padding: 8px; border: 1px solid #ccc; border-radius: 4px; font-size: 1em; } .calculator-container button { display: block; width: 100%; padding: 10px; background-color: #007bff; color: white; border: none; border-radius: 4px; font-size: 1.1em; cursor: pointer; transition: background-color 0.3s ease; } .calculator-container button:hover { background-color: #0056b3; } .result-section { margin-top: 25px; padding: 15px; background-color: #e9ecef; border: 1px solid #ced4da; border-radius: 4px; text-align: center; font-size: 1.2em; color: #333; }

Leave a Comment