Optimization in the command window#
Every name on this page is typed at the command window (or passed to
ICoreBlocks --console "<line>") and answers what MATLAB's Optimization
Toolbox answers for the same arguments — same argument order, same defaults,
same empty-argument convention. MATLAB code that sets up and solves a
programme should paste in and run.
Two of these solvers are transcriptions rather than re-implementations —
fminunc and fsolve are MATLAB's own iterations written out, so they take
the same steps and stop at the same point — and the rest are compared on the
thing that is genuinely shared. Where a solver here is a different iteration
from MATLAB's, this page says so and says what is compared instead. That
distinction is the whole content of the section on surprises below, and it is
worth reading before you trust a fifth digit.
The families, and what to reach for#
| You want to | Use |
|---|---|
| Minimise a smooth function, no constraints | fminunc (quasi-Newton BFGS), or fminsearch (Nelder–Mead, derivative free) |
| Minimise one variable | fminbnd on a bracket |
| Minimise subject to constraints | fmincon — linear inequalities, linear equalities, bounds, and a nonlinear nonlcon |
| Solve F(x) = 0 | fsolve for a square system; fzero for one variable |
| Fit by least squares | lsqnonlin (you write the residual), lsqcurvefit (you write the model), lsqlin (linear with constraints), lsqnonneg (linear, x ≥ 0) |
| Solve a linear programme | linprog; intlinprog when some variables must be whole numbers |
| Solve a quadratic programme | quadprog |
| Minimise the WORST of several objectives | fminimax; fgoalattain to trade them off against goals |
| Set solver options | optimoptions(solver, …) for the modern solvers, optimset(…) for fminsearch/fminbnd/fzero/lsqnonneg; read one back with optimget |
| Set ODE options | odeset("Name", value, …), read back with odeget — the same option-set-as-a-value shape, for ode45 |
| Check a gradient you wrote | checkGradients(@f, x0) |
Every solver takes its arguments in MATLAB's order and spells an absent one
[], so a bounds-only problem is written with the empties in front of the
bounds — exactly as you would write it in MATLAB.
A worked line or two#
A linear programme, with its value and its stopping verdict:
>>> [x, fval, exitflag] = linprog([-1, -2], [1, 1; 1, -1; -1, 0; 0, -1], [4, 2, 0, 0])
x = [[0], [4]] # Matrix of Double
fval = -8 # Integer
exitflag = 1 # Integer
A quadratic programme, and an integer one:
>>> quadprog([2, 0; 0, 2], [-2, -5], [1, -2; -1, -2; -1, 2], [2, 6, 2])
[[1.4], [1.7]] # Matrix of Double
>>> intlinprog([-3, -2, -1], [3], [1, 1, 1; 0, 1, 0], [7, 1], [], [], [0, 0, 0], [10, 10, 10])
[[7], [0], [0]] # Matrix of Double
Rosenbrock's valley from MATLAB's own starting point, unconstrained, and then the same objective under a bound box:
>>> f = @(x) 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2; [x, fval, exitflag] = fminunc(f, [-1.2, 1])
x = [[0.999995, 0.999989]] # Matrix of Double
fval = 2.83204e-11 # Double
exitflag = 1 # Integer
>>> fmincon(@(x) (x(1) - 3)^2 + (x(2) - 2)^2, [0, 0], [], [], [], [], [0, 0], [1, 1])
[[1, 1]] # Matrix of Double
A square nonlinear system, and a curve fit written the two ways the toolbox offers:
>>> F = @(x) [2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))]; fsolve(F, [-5, -5])
[[0.567143, 0.567143]] # Matrix of Double
>>> F = @(a, xd) a(1)*exp(a(2)*xd); xd = 0:0.5:3; yd = 2.5*exp(-0.7*xd); [a, resnorm] = lsqcurvefit(F, [1, -1], xd, yd)
a = [[2.5, -0.7]] # Matrix of Double
resnorm = 5.92632e-16 # Double
>>> F = @(x) [x(1) - 1; x(2) - 2; x(1) + x(2) - 4]; [x, resnorm, residual] = lsqnonlin(F, [0, 0])
x = [[1.33333, 2.33333]] # Matrix of Double
resnorm = 0.333333 # Double
residual = [[0.333333], [0.333333], [-0.333333]] # Matrix of Double
An option set is a VALUE here, and it prints as the call that made it:
>>> optimoptions("fminunc", "OptimalityTolerance", 1e-8)
'optimoptions('fminunc','OptimalityTolerance',1e-08)' # String
Every transcript on this page is real output, captured with
ICoreBlocks --console "<line>" on 2026-09-05 at commit bd974745.
Nine things that surprise people#
lsqnonlin's F answers the RESIDUAL VECTOR, not its sum of squares. Writing@(x) sum(r(x).^2)gives a solver one number where it expected n, and it will minimise the square of that.resnormis the sum of squares;residualis the vector — both come back, so there is never a reason to form the sum yourself.lsqlin'sresnormis the SQUARED norm of the residual, not the quadratic the solver minimises. MATLAB's objective is half of it. A number that is out by exactly two everywhere is this.- A
nonlconmust answer TWO values and an anonymous handle cannot.fmincon's nonlinear constraint answers[c, ceq]— the inequalities and the equalities, either of them[]. An anonymous body answers one value, so it is refused by name rather than silently read asc:
>>> fmincon(@(x) x(1) + x(2), [0.5, 0.5], [], [], [], [], [], [], @(x) [x(1)^2 + x(2)^2 - 1, 0])
error: fmincon(..., nonlcon): nonlcon must answer TWO values, [c, ceq] -- the inequalities c(x) <= 0 and the equalities ceq(x) = 0, either of them [] -- and an anonymous handle answers one. Write it as a `function [c, ceq] = name(x)` and pass @name (C2.7)
- An LP's
xis not unique where the optimum is degenerate — and both answers are right. The VALUE of a linear programme belongs to the problem; the vertex you land on belongs to the method. Measured against MATLAB on a degenerate case, MATLAB answers[0, 2, 0]and this console[1, 1, 0], withfval−4 on both. Comparefval, and readxthrough it. fmincon'sexitflagis THIS solver's stopping test, not MATLAB's. Over a ten-problem corpus, three problems answer 3 (the objective stopped changing) where MATLAB's default answers 1 — at the same point, to 1e-7. The point is comparable; the integer is a report about an iteration, and the two iterations are not the same one.fminuncandfsolveare the exceptions: they are transcriptions, so their exit codes are MATLAB's.optimgeton a set that never mentioned an option answers[], not the solver's default.optimget(optimset(), "TolX")is[]and not 1e-6 — the default belongs to the solver that receives the set, not to the set. And anoptimoptionsset handed tooptimgetis refused on BOTH sides; MATLAB says "First argument must be an options structure created with OPTIMSET." A modern set is read by handing it to its solver.optimoptionsmaps the legacy option names rather than refusing them, and one of them sets two modern options.optimoptions(s, "TolFun", t)sets bothFunctionToleranceandOptimalityTolerance— measured — so a set built with the old name is not the same set as one built with either new name alone.- A ZERO weight in
fgoalattainis not "ignore this objective" — it makes that goal a HARD constraint. There is no gamma left to relax it with. It is the opposite of what the word "weight" suggests. fminimaxhere takes AFFINE objectives, and an unbounded problem is answered rather than wandered into. With F affine the epigraph programme is the problem itself and one linear programme solves it exactly, which is why these answers compare digit for digit. Measured on MATLAB, three affine objectives with no bounds givex = [-1.66e+17, -8.31e+16]and exitflag 0 — it ran out of budget where the LP can see there is no minimum, and this console says so instead. A NONLINEAR F is refused by name.
What is not here#
Refused, with the reason in the message:
fseminf— semi-infinite constraints.lsqnonlinon an UNDER-determined system (fewer equations than unknowns). MATLAB hands that case to Levenberg–Marquardt, a different algorithm from the trust-region-reflective one transcribed here, so it is refused rather than answered by an iteration nobody compared.fsolveon a non-square system, for the same reason.fminimaxandfgoalattainwith a nonlinear F.outputandlambda— every solver's fourth and fifth outputs.exitflagcrosses as a number;outputandlambdaare structs whose text fields this console has no kind for, and reading one names the rule it met rather than being quietly absent.
Not known to the console at all — typing one of these answers
unknown function '<name>':
| If you reach for | Use |
|---|---|
optimproblem, optimvar, optimexpr, solve(prob) | the solver-based forms above — linprog, quadprog, fmincon. There is no problem-based interface here |
optimtool | the same names typed directly; there is no interactive tool |
prob2struct | write the matrices yourself; that is what it converts to |
ga, particleswarm, patternsearch, simulannealbnd | Global Optimization Toolbox, which is outside this console's eight |
Where to look next#
- Command glossary — console commands, verbs, functions — every name the console answers, grouped by toolbox, with each one's arguments.
- The command window — the command engine for a user — the engine itself: variables, multiple outputs, function handles, scripts, and running a line headlessly.
- Curve fitting in the command window —
fitand its models, which are these solvers with the model library written for you. - Numerics — what the solver will and will not do — what a double can carry, and where these answers stop being exact.