The average rate of change of a function \(f(x)\) over an interval \([a, b]\) is the slope of the secant line connecting the points \((a, f(a))\) and \((b, f(b))\) on the graph of the function. It tells us the average how much the function's output changes for each unit change in its input over that interval.
Enter your function here (e.g., x^2, sin(x), 2x + 3)
.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;
}
.calculator-container p {
color: #555;
line-height: 1.6;
margin-bottom: 20px;
font-size: 0.95em;
}
.inputs-section {
margin-bottom: 15px;
}
.input-group {
margin-bottom: 15px;
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.input-group input[type="text"],
.input-group input[type="number"] {
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1em;
box-sizing: border-box; /* Ensures padding doesn't affect width */
}
.input-group small {
font-size: 0.8em;
color: #777;
margin-top: 4px;
}
button {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1em;
width: 100%;
box-sizing: border-box;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #45a049;
}
.result-section {
margin-top: 25px;
padding: 15px;
background-color: #e7f3e7;
border: 1px solid #c5e1c5;
border-radius: 4px;
font-size: 1.1em;
color: #2e7d32;
text-align: center;
font-weight: bold;
}
function calculateAverageRateOfChange() {
var functionString = document.getElementById("functionInput").value;
var a = parseFloat(document.getElementById("startInterval").value);
var b = parseFloat(document.getElementById("endInterval").value);
var resultDiv = document.getElementById("result");
if (isNaN(a) || isNaN(b)) {
resultDiv.innerHTML = "Please enter valid numbers for the interval endpoints.";
return;
}
if (a === b) {
resultDiv.innerHTML = "The interval endpoints cannot be the same.";
return;
}
try {
// Function to evaluate the user-provided function string
// This uses a simple approach and might need more robust parsing for complex functions or security.
// For a Desmos-like experience, a more sophisticated math engine would be required.
// Here, we'll use eval for demonstration, but acknowledge its risks.
var f_a = eval(functionString.replace(/x/g, '(' + a + ')'));
var f_b = eval(functionString.replace(/x/g, '(' + b + ')'));
if (isNaN(f_a) || isNaN(f_b)) {
resultDiv.innerHTML = "Could not evaluate the function at the given interval points. Check your function input.";
return;
}
var rateOfChange = (f_b – f_a) / (b – a);
resultDiv.innerHTML = "The average rate of change is: " + rateOfChange.toFixed(4);
} catch (error) {
resultDiv.innerHTML = "Error evaluating function. Please check the function format. Error: " + error.message;
}
}