Question
What is the difference between fminsearch and fmincon in MATLAB, and how do I choose the right solver for optimization problems?
Expert Answer
Neeta Dsouza
PhD Expert
Answered Aug 30, 2026
The fundamental difference between fminsearch and fmincon in MATLAB comes down to constraints and derivative requirements.
- fminsearch is an unconstrained, derivative-free solver based on the Nelder-Mead simplex algorithm. It only evaluates function values and is best for noisy, non-smooth, or discontinuous functions without any constraints.
- fmincon is a constrained, gradient-based solver (using Interior-Point, SQP, or Active-Set algorithms). It solves continuous, smooth objective functions subject to bounds, linear constraints, and nonlinear constraints.
Key Differences at a Glance
| Feature | fminsearch |
fmincon |
|---|---|---|
| Optimization Type | Unconstrained local minimization | Constrained local minimization |
| Algorithm | Nelder-Mead Direct Search Simplex | Interior-Point (default), SQP, Active-Set, Trust-Region Reflective |
| Gradients / Derivatives | Not required (evaluates only function values) | Required (computed via finite differences or user-supplied analytic gradients) |
| Constraint Handling | None (cannot handle bounds or inequalities natively) | Handles lower/upper bounds (lb, ub), linear constraints (A*x ≤ b), and nonlinear constraints (nonlcon) |
| Toolbox Required | Base MATLAB | Optimization Toolbox |
| Speed & Scalability | Slow for high dimensions (> 5–10 variables) | Fast and efficient for large-scale, high-dimensional problems |
1. When to Use fminsearch (Example)
Use fminsearch when your objective function is unconstrained, black-box, or contains noise/discontinuities where derivatives cannot be computed reliably:
% Minimize Rosenbrock Banana Function (Unconstrained)
objective = @(x) 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
x0 = [-1.2, 1.0]; % Initial guess
[x_opt, fval] = fminsearch(objective, x0);
fprintf('Optimal x: [%.4f, %.4f]\n', x_opt(1), x_opt(2));
fprintf('Minimum value: %.6f\n', fval);
2. When to Use fmincon (Example)
Use fmincon when you have physical boundaries (e.g., temperatures > 0, voltages < 240V) or mathematical equality/inequality constraints:
% Minimize Rosenbrock function subject to bound constraints: 0 <= x1 <= 0.8, 0 <= x2 <= 2.0
objective = @(x) 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
x0 = [0.2, 0.2];
% Bounds: lb <= x <= ub
lb = [0.0, 0.0];
ub = [0.8, 2.0];
% Optimization options
options = optimoptions('fmincon', 'Algorithm', 'interior-point', 'Display', 'off');
[x_opt, fval] = fmincon(objective, x0, [], [], [], [], lb, ub, [], options);
fprintf('Constrained Optimal x: [%.4f, %.4f]\n', x_opt(1), x_opt(2));
fprintf('Minimum value: %.6f\n', fval);
Decision Rule: Which One Should You Pick?
- If your problem has any bounds or constraints (e.g., \(x_1 + x_2 \le 5\)), always use
fmincon. - If your problem is smooth and unconstrained, use
fminunc(gradient-based) rather thanfminsearchfor much faster convergence. - If your problem is unconstrained but noisy, discontinuous, or has non-differentiable step functions, use
fminsearch. - If your function has multiple local minima (multimodal), use global optimization solvers like
ga(Genetic Algorithm) orparticleswarm.
Have a different question? Ask here
Related minimization Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →