Evolutionary & Swarm Optimization
Use derivative-free search (fminsearch) to optimise noisy or non-smooth objective functions.
Step 1 — Define a multi-variable objective
Anonymous functions capture objective functions cleanly. The extended Rosenbrock is a classic benchmark: the global minimum is at (1, 1) with f = 0.
f = @(x) (1 - x(1))^2 + 100*(x(2) - x(1)^2)^2; disp(f([0, 0])); disp(f([1, 1]))▶ Run in SimLab
Expected output: f([0,0]) = 1, f([1,1]) = 0
Step 2 — Find the minimum with fminsearch
fminsearch implements the Nelder–Mead simplex method — a derivative-free strategy that mirrors the population-based exploration of evolutionary algorithms.
f = @(x) (1 - x(1))^2 + 100*(x(2) - x(1)^2)^2;
result = fminsearch(f, [0; 0]);
xopt = result.x;
fopt = result.fval;
printf('Optimum: x1=%.4f, x2=%.4f, fval=%.6f\n', xopt(1), xopt(2), fopt)▶ Run in SimLabExpected output: Optimum near x1=1.0000, x2=1.0000, fval≈0
Step 3 — Visualise the search landscape
Plot the objective surface to see why this function is hard: the narrow curved valley misleads gradient-based methods.
x = linspace(-1.5, 2, 200);
y = (1 - x).^2 + 100*x.^4;
plot(x, y);
xlabel('x1'); ylabel('f(x1, 0)');
title('Rosenbrock Valley Slice')▶ Run in SimLabExpected output: Contour plot with a narrow banana-shaped valley leading to (1,1)
Related Tutorials
Try SimLab — MATLAB®-compatible, free, in your browser
466 functions. Runs in your browser. No install.
Open SimLabMATLAB® is a registered trademark of The MathWorks, Inc. SimLab is an independent project by Simulations4All and is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.