User manual › Optimization in the command window
kind: manual#console#optimization#solvers#linear-programming#least-squares#manual

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 toUse
Minimise a smooth function, no constraintsfminunc (quasi-Newton BFGS), or fminsearch (Nelder–Mead, derivative free)
Minimise one variablefminbnd on a bracket
Minimise subject to constraintsfmincon — linear inequalities, linear equalities, bounds, and a nonlinear nonlcon
Solve F(x) = 0fsolve for a square system; fzero for one variable
Fit by least squareslsqnonlin (you write the residual), lsqcurvefit (you write the model), lsqlin (linear with constraints), lsqnonneg (linear, x ≥ 0)
Solve a linear programmelinprog; intlinprog when some variables must be whole numbers
Solve a quadratic programmequadprog
Minimise the WORST of several objectivesfminimax; fgoalattain to trade them off against goals
Set solver optionsoptimoptions(solver, …) for the modern solvers, optimset(…) for fminsearch/fminbnd/fzero/lsqnonneg; read one back with optimget
Set ODE optionsodeset("Name", value, …), read back with odeget — the same option-set-as-a-value shape, for ode45
Check a gradient you wrotecheckGradients(@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#

  1. 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. resnorm is the sum of squares; residual is the vector — both come back, so there is never a reason to form the sum yourself.
  2. lsqlin's resnorm is 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.
  3. A nonlcon must 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 as c:
>>> 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)
  1. An LP's x is 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], with fval −4 on both. Compare fval, and read x through it.
  2. fmincon's exitflag is 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. fminunc and fsolve are the exceptions: they are transcriptions, so their exit codes are MATLAB's.
  3. optimget on 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 an optimoptions set handed to optimget is 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.
  4. optimoptions maps the legacy option names rather than refusing them, and one of them sets two modern options. optimoptions(s, "TolFun", t) sets both FunctionTolerance and OptimalityTolerance — measured — so a set built with the old name is not the same set as one built with either new name alone.
  5. A ZERO weight in fgoalattain is 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.
  6. fminimax here 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 give x = [-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.
  • lsqnonlin on 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.
  • fsolve on a non-square system, for the same reason.
  • fminimax and fgoalattain with a nonlinear F.
  • output and lambda — every solver's fourth and fifth outputs. exitflag crosses as a number; output and lambda are 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 forUse
optimproblem, optimvar, optimexpr, solve(prob)the solver-based forms above — linprog, quadprog, fmincon. There is no problem-based interface here
optimtoolthe same names typed directly; there is no interactive tool
prob2structwrite the matrices yourself; that is what it converts to
ga, particleswarm, patternsearch, simulannealbndGlobal Optimization Toolbox, which is outside this console's eight

Where to look next#