Average Rate of Change of a Function Calculator

Average Rate of Change Calculator

The average rate of change of a function measures how much the output of a function changes, on average, over a given interval. It's essentially the slope of the secant line connecting two points on the function's graph. The formula for the average rate of change of a function $f(x)$ over the interval $[a, b]$ is:

Average Rate of Change = $\frac{f(b) – f(a)}{b – a}$

function evaluateFunction(funcStr, xVal) { try { // Replace ^ with ** for exponentiation, handle common math functions funcStr = funcStr.replace(/\^/g, '**'); funcStr = funcStr.replace(/sin\(/g, 'Math.sin('); funcStr = funcStr.replace(/cos\(/g, 'Math.cos('); funcStr = funcStr.replace(/tan\(/g, 'Math.tan('); funcStr = funcStr.replace(/sqrt\(/g, 'Math.sqrt('); funcStr = funcStr.replace(/log\(/g, 'Math.log('); funcStr = funcStr.replace(/exp\(/g, 'Math.exp('); // Create a safe evaluation context var scope = { x: xVal, Math: Math }; var variableNames = Object.keys(scope); var variableValues = Object.values(scope); // Use a function constructor for safer evaluation than eval() var func = new Function(…variableNames, `return ${funcStr}`); return func(…variableValues); } catch (error) { console.error("Error evaluating function:", error); return NaN; // Return NaN if there's an error } } function calculateAverageRateOfChange() { var functionInput = document.getElementById("functionInput").value; var aValueInput = document.getElementById("aValue").value; var bValueInput = document.getElementById("bValue").value; var resultDiv = document.getElementById("result"); resultDiv.innerHTML = ""; // Clear previous results if (!functionInput || !aValueInput || !bValueInput) { resultDiv.innerHTML = "Please fill in all fields."; return; } var a = parseFloat(aValueInput); var b = parseFloat(bValueInput); if (isNaN(a) || isNaN(b)) { resultDiv.innerHTML = "Interval values (a and b) must be valid numbers."; return; } if (a === b) { resultDiv.innerHTML = "The interval cannot have the same start and end points (a cannot equal b) for a meaningful rate of change calculation."; return; } var f_a = evaluateFunction(functionInput, a); var f_b = evaluateFunction(functionInput, b); if (isNaN(f_a) || isNaN(f_b)) { resultDiv.innerHTML = "Could not evaluate the function at the given interval points. Please check your function syntax."; return; } var rateOfChange = (f_b – f_a) / (b – a); if (isNaN(rateOfChange)) { resultDiv.innerHTML = "Calculation resulted in an invalid number. Please check your inputs and function."; } else { resultDiv.innerHTML = "The average rate of change of the function over the interval [" + a + ", " + b + "] is: " + rateOfChange.toFixed(6); } } #arc-calculator { font-family: sans-serif; border: 1px solid #ccc; padding: 20px; border-radius: 8px; max-width: 600px; margin: 20px auto; background-color: #f9f9f9; } #arc-calculator h2 { text-align: center; color: #333; margin-bottom: 15px; } #arc-calculator p { margin-bottom: 15px; line-height: 1.6; } .input-section { margin-bottom: 15px; } .input-section label { display: block; margin-bottom: 5px; font-weight: bold; color: #555; } .input-section input[type="text"] { width: calc(100% – 22px); padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 1em; } #arc-calculator button { display: block; width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 5px; font-size: 1.1em; cursor: pointer; transition: background-color 0.3s ease; } #arc-calculator button:hover { background-color: #0056b3; } #result { background-color: #e9ecef; padding: 15px; border-radius: 5px; text-align: center; font-size: 1.1em; color: #333; }

Leave a Comment