Generated reference › Constrained Linear Least Squares — Control Systems/Optimization
kind: generated#block#control-systems-optimization

Constrained Linear Least Squares — Control Systems/Optimization

Control_Systems/Optimization/Constrained_Linear_Least_Squares · 2 input / 3 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text

Description#

The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.

Constrained Linear Least Squares

Control Systems / Optimization

Solves minx ½·||C·x − d||² subject to Aeq·x = beq, A·x ≤ b and lb ≤ x ≤ ub on every step – MATLAB's lsqlin. C and the constraint matrices are settings; the data d and the inequality right-hand side b arrive on ports. It is the quadratic program with H = C'C and f = −C'd, and it runs the same Goldfarb-Idnani dual active set as Quadratic Program: it starts at the unconstrained least-squares answer, adds the most violated constraint, and drops whichever active constraint's multiplier would turn negative first.

Ports

  • d – the data, an [m,1] column with one entry per ROW of C.
  • b – the right-hand side of A·x ≤ b, a [p,1] column with one entry per row of A. When A is empty this port is unused and must be connected to any [1,1] signal.
  • x – the minimizer, [n,1], one entry per COLUMN of C.
  • resnorm – ||C·x − d||², [1,1], as lsqlin's second output. It is computed from the x that comes out, on every exit flag.
  • exitflag – [1,1], with MATLAB's values: 1 optimal, 0 the iteration limit was reached, −2 the constraints are infeasible. Read it before using x.

Parameters

  • Options – empty (default), or the path of a Solver Options block (Home/Solver Options), MATLAB’s options argument: for the run, every option it sets replaces this block’s parameter of the same name; one it leaves at default, or one this block does not have, changes nothing.
  • Coefficient Matrix C – the matrix, [m,n] with m ≥ n and at most 48 rows and 12 columns. It must have full column rank, so that the unconstrained problem – and with it the constrained one – has a unique answer; a C whose columns are numerically dependent is refused with that reason when the model is built. Its Householder QR is taken once, at config load.
  • Inequality Matrix A – the inequality matrix, [p,n], or [] for none. Its right-hand side is the b port.
  • Equality Matrix Aeq, Equality Vector beq – the equality constraints, [q,n] and [q,1], or [] for none. The rows of Aeq must be linearly independent. beq is a SETTING, not a port: only the inequality right-hand side moves per step.
  • Lower Bounds lb, Upper Bounds ub – bounds, [n,1] each or [] for none; -Inf/Inf entries leave a variable unbounded on that side. Each finite bound becomes one more constraint row, and Aeq, A and the finite bounds together may not exceed 40 rows.
  • Constraint Tolerance – a constraint counts as violated when it is breached by more than this. Default 1e-8, lsqlin's own.
  • Maximum Iterations – the limit on solver passes, each one a small linear solve. 0 selects the active-set default, 10·(variables + constraints). When the limit is reached the exit flag is 0 and x is the last iterate, which satisfies the constraints the solver had made active but may violate others.
  • Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.

Code export

All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text, printed from the same program the block runs, so every target does the same arithmetic in the same order as the simulation. C, the constraint rows, (C'C)−1, the map R−1Q1' that gives the unconstrained answer and (C'C)−1 times the constraint normals are baked in as constants at export time; re-export after changing them.

The whole solve runs in every generated body, and its cost is bounded: at most Maximum Iterations passes, each a Cholesky of a K×K matrix in the active constraints, where K is the number of constraint rows. That loop is that length in the emitted code and exits early through a flag.

The three HDL targets carry the arithmetic in real and quantize only at the ports: simulation-only, not offered as synthesizable. A Cholesky has a division by a pivot and a square root, and the active set is a data-dependent branch, none of which belongs in a Q16.16 datapath.

Simulink bridge

None (Support::None). lsqlin is an Optimization Toolbox function and that toolbox ships no Simulink library at all, so there is no path a diagram could name. The bridge reports this block rather than dropping it silently, and it has no parity testbench; code export verification covers all ten languages.

Notes

  • Algebraic: the answer depends on this step's d and b alone, and nothing is carried between steps – there is no warm start. It is piecewise affine in (d, b) rather than linear, so it carries no state space.
  • ⚠ x is continuous in d and b while the exit flag is 1, which is why quantizing the two ports (as the HDL targets do) moves x smoothly rather than jumping. The flag itself is a discrete output: a problem sitting on the feasibility boundary, or on the iteration limit, can be flagged differently by a target that sees a quantized right-hand side.
  • ⚠ An exit flag of −2 or 0 leaves x defined but not optimal: it is the iterate the solver had reached, which satisfies the constraints it had made active and may violate the rest. MATLAB's own x is different there, because a different method walks a different path.
  • ⚠ Unconstrained, this is not the plain least-squares block: with A, Aeq, lb and ub all empty the answer is R−1Q1'd and the solver does nothing, which is the same number a QR solve would give – but the block still carries the whole active-set program into every export.
  • The measured agreement with R2026a, the problem sets behind it and the emitted MATLAB body's own check are on the source banner.

Code facts#

FactValue
registered typeControl_Systems/Optimization/Constrained_Linear_Least_Squares
familyControl_Systems/Optimization
solver environment classICoreBlock_0_Control_Systems_1_Optimization_2_Constrained_Linear_Least_Squares
sourcesrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Optimization/Constrained_Linear_Least_Squares/ICoreBlock_0_Control_Systems_1_Optimization_2_Constrained_Linear_Least_Squares.cpp
headersrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Optimization/Constrained_Linear_Least_Squares/ICoreBlock_0_Control_Systems_1_Optimization_2_Constrained_Linear_Least_Squares.h
default size on canvas170 × 100 px
ports at insert2 in, 3 out
code generators implementedPython, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text

Ports#

#DirectionSignal typeDescription label
1inICoreDoubled
2inICoreDoubleb
3outICoreDoublex
4outICoreDoubleresnorm
5outICoreDoubleexitflag

Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.

Configuration variables#

Config variableDefaultSimulink parameter
Coefficient Matrix C[1 0.4; 0.3 1; 0.5 0.8]—
Inequality Matrix A[1 0.5; 0.3 1; 0.8 0.9]—
Equality Matrix Aeq[]—
Equality Vector beq[]—
Lower Bounds lb[]—
Upper Bounds ub[]—
Constraint Tolerance1e-8—
Maximum Iterations0—
Options——

Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.

supportSupport::None
Simulink path—
port-count rulePortsParam::None
SampleTime parameteryes

Caveat (shown to the user): quadprog is an Optimization Toolbox function and that toolbox ships no Simulink library, so there is no path a diagram could name; the block is reported rather than dropped when a model crosses

Catalog contract: src/ICoreBlocks/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h

Description vs code#

The lists agree. check_block_descriptions.py finds no disagreement between the description's Ports, Parameters, Code export and Simulink bridge lists and the code's.

The verdict above is tools/docs/check_block_descriptions.py (P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.

File banner (developer view)#

The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).

Constrained Linear Least Squares -- MATLAB's lsqlin, on every step minimize 1/2 ||C x - d||^2 subject to Aeq x = beq, A x <= b, lb <= x <= ub

That is the quadratic program with H = C'C and f = -C'd, and it is solved by the same Goldfarb-Idnani dual active set as Constrained Linear Least Squares (ICoreOptimizationSupport). C and the constraint matrices are SETTINGS; the data d and the inequality right-hand side b arrive on PORTS, so the per-step cost is the active-set passes and nothing else: H^-1 comes from a Householder QR of C at config load, as does the map x0 = R^-1 Q1' d that starts the solve at the unconstrained least-squares answer, and both are printed into every export as literals.

Measured against R2026a over 2200 problems (n <= 12, up to 40 constraint rows, equalities, bounds with infinite entries, duplicated rows, constraints through the unconstrained optimum, degenerate vertices and 596 infeasible ones): every exit flag equal, and where both converged worst |dx|/(1+|x|) = 1.6e-11 against lsqlin's own 'active-set' algorithm (2.0e-5 against the default interior point on the deliberately degenerate sets, which is that solver's own accuracy there). The emitted MATLAB body, run in R2026a, is bit-identical to this C++ and agrees with lsqlin to 4.2e-14.

Support::None: lsqlin is an Optimization Toolbox function and that toolbox ships no Simulink library, so there is no path a diagram could name.

Sample results#

Constrained Linear Least Squares — Sine Wave, [3,1]: amplitudes 1/2/3 at 2 rad/s (tried only because every scalar stimulus was refused)Constrained Linear Least Squares — Sine Wave, [3,1]: amplitudes 1/2/3 at 2 rad/s (tried only because every scalar stimulus was refused)-1012012345t (s)in ICoreDouble-Out-0 [3x1] entry 0in ICoreDouble-Out-0 [3x1] entry 0out ICoreDouble-Out-0 [2x1] entry 0out ICoreDouble-Out-1out ICoreDouble-Out-2

Plotted: vector — Sine Wave, [3,1]: amplitudes 1/2/3 at 2 rad/s (tried only because every scalar stimulus was refused)

Category dynamic · sample time 0.1 · 60 steps · commit 6e86c1ef7f9cbef66d471271096a3bb1dc78e0c1 · produced by docsSample --out <folder> --blocks Nonnegative_Least_Squares Quadratic_Program Constrained_Linear_Least_Squares --steps 60 · data docs/generated/samples/Control_Systems__Optimization__Constrained_Linear_Least_Squares.json · the SVG is generated from those numbers by tools/docs/plot_svg.py, so it is a run and not a drawing (R-D10).