Roots & Minimization
Two questions come up constantly in science and engineering: where is this function zero? and where is it smallest? For polynomials, roots answers the first. For any other function, MATLAB has fzero (one variable), fminbnd (minimum on an interval) and fminsearch (minimum of several variables).
fzero: solving f(x) = 0
fzero(f, x0) searches for a root near the starting point x0. Given an interval [a b] where f changes sign, it's guaranteed to find a root inside it:
f = @(x) cos(x) - x; % where does cos(x) equal x?
r1 = fzero(f, 1);
fprintf('root from x0 = 1: %.8f\n', r1);
r2 = fzero(f, [0 2]);
fprintf('root in the interval: %.8f\n', r2);
fprintf('|f(root)| below 1e-12: %d\n', abs(f(r2)) < 1e-12);root from x0 = 1: 0.73908513
root in the interval: 0.73908513
|f(root)| below 1e-12: 1
The root is approximate: fzero stops when it can't improve the answer in double precision, so check the result against a tolerance rather than for exact zero.
Functions can have several roots; the starting point decides which one you get:
g = @(x) x.^2 - 2*x - 3; % roots at -1 and 3
fprintf('starting near 0: %g\n', fzero(g, 0));
fprintf('starting near 5: %g\n', fzero(g, 5));
fprintf('roots() for comparison: %s\n', mat2str(roots([1 -2 -3])'));starting near 0: -1
starting near 5: 3
roots() for comparison: [3 -1]
Solving an equation with parameters
Rewrite "solve A = B" as "find the zero of A − B". Parameters are captured by the anonymous function:
% What yearly interest rate turns 1000 into 1500 in 8 years?
P = 1000; target = 1500; years = 8;
h = @(r) P * (1 + r)^years - target;
rate = fzero(h, [0 1]);
fprintf('required rate: %.4f%%\n', 100 * rate);
fprintf('check: %.2f\n', P * (1 + rate)^years);required rate: 5.1990%
check: 1500.00
fminbnd: minimum on an interval
[x, fval] = fminbnd(f, a, b) finds the minimum of a one-variable function between a and b. To find a maximum, minimise -f:
% Box from a 20 x 20 sheet: cut squares of side x from the corners and fold.
volume = @(x) x .* (20 - 2*x).^2;
[xBest, negV] = fminbnd(@(x) -volume(x), 0, 10);
fprintf('best cut: %.4f, volume %.4f\n', xBest, -negV);
fprintf('exact answer: x = 10/3 = %.4f\n', 10/3);best cut: 3.3333, volume 592.5926
exact answer: x = 10/3 = 3.3333
fminsearch: several variables
fminsearch(f, x0) minimises a function of a vector using the Nelder–Mead simplex method, which needs no derivatives. A classic test is the Rosenbrock "banana" function, whose minimum is at (1, 1):
banana = @(p) (1 - p(1))^2 + 100 * (p(2) - p(1)^2)^2;
opts = optimset('TolX', 1e-8, 'TolFun', 1e-10);
[pBest, fBest] = fminsearch(banana, [-1.2 1], opts);
fprintf('minimum at (%.4f, %.4f), value below 1e-8: %d\n', pBest, fBest < 1e-8);minimum at (1.0000, 1.0000), value below 1e-8: 1
fminsearch is also a quick way to fit models to data: minimise the sum of squared errors over the model parameters.
t = [0 1 2 3 4 5];
y = [5.1 3.0 1.9 1.1 0.7 0.4]; % decaying measurements
sse = @(p) sum((p(1) * exp(-p(2) * t) - y).^2);
opts = optimset('TolX', 1e-8, 'TolFun', 1e-10);
p = fminsearch(sse, [1 1], opts);
fprintf('model: y = %.3f * exp(-%.3f t)\n', p(1), p(2));model: y = 5.079 * exp(-0.505 t)
Note: These solvers find local solutions. A bad starting point can lead to a different root or a local minimum that isn't the global one. Plot the function first when you can, and try several starting points. MATLAB's Optimization Toolbox (
fminunc,fmincon,lsqcurvefit) and Octave'soptimpackage offer more powerful methods.
Test yourself
What does fzero(f, [a b]) require of the interval?
- Nothing
- f(a) and f(b) must have opposite signs
- a must be 0
- f must be a polynomial
How do you find the maximum of f with fminbnd?
fmaxbnd(f, a, b)- Minimise
@(x) -f(x) - Use
max(f) - Pass 'max' as an option
Which function minimises a function of several variables without needing derivatives?
fzerofminbndfminsearchroots