Why This Matters
Finding the best possible outcome -- the maximum profit, the shortest path, the least material needed -- is one of the most practical applications of calculus. Optimization uses derivatives to find where a function reaches its highest or lowest value, turning real-world design problems into solvable math.
The key insight is that at a maximum or minimum, the derivative is zero (or undefined). These points are called critical points. The derivative being zero means the tangent line is horizontal -- the function has momentarily stopped increasing and started decreasing (or vice versa). By finding all critical points and evaluating the function there, you can identify the global maximum or minimum.
The second derivative test tells you which critical points are maxima and which are minima without checking every value. If the second derivative is negative at a critical point, the function curves downward there (concave down) -- it is a local maximum. If the second derivative is positive, the function curves upward (concave up) -- it is a local minimum. This efficient classification is used everywhere from machine learning to structural engineering.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Optimization: find critical points where the derivative is zero, classify them with the second derivative, check endpoints.
Code Example
// Find max/min of a function on an interval
function deriv(f, x, h = 0.00001) {
return (f(x + h) - f(x - h)) / (2 * h);
}
function secondDeriv(f, x, h = 0.0001) {
return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h);
}
// Find critical points by scanning for sign changes in f prime
function findCriticalPoints(f, a, b, steps = 1000) {
const criticals = [];
const dx = (b - a) / steps;
for (let i = 0; i < steps; i++) {
const x1 = a + i * dx;
const x2 = x1 + dx;
const d1 = deriv(f, x1);
const d2 = deriv(f, x2);
if (d1 * d2 <= 0) {
// Sign change: bisect to find zero
let lo = x1, hi = x2;
for (let j = 0; j < 50; j++) {
const mid = (lo + hi) / 2;
if (deriv(f, mid) * deriv(f, lo) <= 0) hi = mid;
else lo = mid;
}
criticals.push(parseFloat(((lo + hi) / 2).toFixed(6)));
}
}
return criticals;
}
// Optimization: f(x) = -x^2 + 4x + 1 on [0, 5]
const f = x => -x * x + 4 * x + 1;
const crits = findCriticalPoints(f, 0, 5);
console.log("f(x) = -x^2 + 4x + 1 on [0, 5]");
console.log("Critical points:", crits);
for (const c of crits) {
const sd = secondDeriv(f, c);
const type = sd < 0 ? "local max" : sd > 0 ? "local min" : "inconclusive";
console.log(`x = ${c}: f(x) = ${f(c).toFixed(4)}, f double-prime = ${sd.toFixed(2)}, ${type}`);
}
// Check endpoints
console.log(`\nEndpoints: f(0) = ${f(0)}, f(5) = ${f(5)}`);
console.log("Global max is at x = 2, f(2) = 5");
// Practical: maximize revenue R(x) = x * (100 - 2x)
console.log("\nRevenue: R(x) = x(100 - 2x)");
const R = x => x * (100 - 2 * x);
const revCrits = findCriticalPoints(R, 0, 50);
console.log("Optimal units:", revCrits[0]);
console.log("Max revenue:", R(revCrits[0]).toFixed(2));Interactive Experiment
Try these exercises:
- Find the maximum of f(x) = sin(x) on [0, 2*pi]. How many critical points are there?
- Minimize the surface area of a box with volume 1000 cubic cm. Hint: for a cube, each side has length 10.
- Find the point on the curve y = sqrt(x) closest to the point (3, 0). Minimize the distance function.
- Modify the code to handle the case where the second derivative is zero (the test is inconclusive). What should you do then?
- Optimize f(x) = x^3 - 6x^2 + 9x + 2 on [0, 5]. Are all critical points maxima or minima?
Quick Quiz
Coding Challenge
Write a function called `findExtremes` that takes a function f, and interval endpoints a and b. It should find the global maximum and minimum of f on [a, b] by: (1) scanning for critical points where the derivative changes sign, (2) evaluating f at all critical points and both endpoints, (3) returning an object with properties max and min, each rounded to 4 decimal places. Use 1000 scan steps and h = 0.00001 for numerical derivatives.
Real-World Usage
Optimization is one of the most commercially valuable applications of calculus:
- Machine learning: Training a model means minimizing a loss function. Gradient descent follows the derivative downhill to find the optimal parameters.
- Manufacturing: Minimizing material usage while meeting strength requirements (e.g., minimum surface area for a given volume).
- Economics: Firms maximize profit by finding where marginal revenue equals marginal cost (where the derivative of profit is zero).
- Logistics: Route optimization, inventory management, and scheduling all reduce to finding minima of cost functions.
- Architecture: Structural optimization finds shapes that minimize stress concentrations while supporting required loads.