Surface Fit — Control Systems/Curve Fitting
Control_Systems/Curve_Fitting/Surface_Fit · 3 input / 2 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.
Surface Fit
Control Systems / Curve Fitting
Fits a least-squares polynomial surface ẑ = f(x, y) to the
last W samples of three signals and emits its coefficients and the
fitted value at the newest point. The model is MATLAB's polyIJ: every
term pab·xa·yb with
a ≤ I, b ≤ J and a + b ≤ max(I, J), so
poly23 is
p00 + p10·x + p01·y + p20·x2 + p11·x·y
+ p02·y2 + p21·x2·y
+ p12·x·y2 + p03·y3.
This is fit([x y], z, 'polyIJ') on a stream: at every sample, once
the window is full, the output equals what that call returns for the last W
samples.
Ports
- x – the first predictor, scalar [1,1].
- y – the second predictor, scalar [1,1].
- z – the response being fitted, scalar [1,1]. The three are sampled together: sample k is the point (xk, yk, zk).
- c – the coefficient column, [n, 1], in MATLAB's
order – by total degree, and within one total degree by descending power
of x: p00, p10, p01, p20, p11, p02, p30, p21, p12, p03, … with the terms the
model leaves out skipped. n follows X Degree and Y Degree alone
(n = 9 for poly23, 7 for poly31, 21 for poly55), exactly
numel(coeffnames(fittype('polyIJ'))). - zf – the fitted surface evaluated at the newest (x, y), [1,1]: the value ẑ the fit predicts for the sample that just arrived, so z − zf is its residual.
Parameters
- X Degree – I, the highest power of x. A whole number from 0
to 5, MATLAB's own range (there is no
poly60). - Y Degree – J, the highest power of y. A whole number from 0 to 5.
- Window Length – W, how many samples the fit sees. A whole number from 2 to 64, and at least n, the number of coefficients: MATLAB refuses fewer points than coefficients ("Insufficient data") and so does this block. Leave it well above n – at W = n the surface interpolates the window exactly and rejects no noise at all.
- 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.
Unlike the other fits in this family, the least-squares problem is solved every sample in every generated core: both predictors arrive on ports, so the design matrix is data and nothing can be solved ahead of time. Each core keeps the three windows, rebuilds the W×n design matrix, factorizes it by modified Gram–Schmidt and back-substitutes – about W·n2 multiply-adds and n square roots per sample. The degrees and the window are structural and are inlined at export; re-export after changing them.
The three HDL targets carry the solve in real arithmetic and
are simulation-only, not synthesizable: a per-sample factorization with
square roots and divisions of run-time quantities does not belong in a Q16.16
datapath. The inputs and outputs are quantized to Q16.16 at the ports, so their
answers differ from the software targets' by what that quantization moves the fit;
each coefficient must also stay inside the format's ±32768.
Simulink bridge
None (Support::None). The Curve Fitting Toolbox ships no
Simulink library at all – fit and prepareSurfaceData
are MATLAB functions – and no Simulink block fits a surface to a running
window, so there is no path a diagram could name. The bridge reports this block
rather than dropping it silently, and it therefore has no parity
testbench; code export verification still covers it across all ten languages.
No configuration of it crosses either, including "Sampling Time (s)", which has no
counterpart to be written to.
Notes
- Stateful, and discrete by nature
(
setDiscreteOnlyBlock(true)): the window advances once per sample. The output depends on the sample that just arrived, so a feedback loop through the block is an algebraic loop. - Zero until the window is full. For the first W−1 samples of a run both outputs are 0: the block fits real samples only, and never the fictitious zeros a prefilled window would hold. From sample W on, the answer is the fit of exactly the last W samples.
- A window that cannot determine the model gives zeros. If the points do not pin the surface down – a predictor that does not move, points all on one line when the model needs more – both outputs are 0 until the window recovers. MATLAB answers such data with a warning and coefficients of order 1031; a stream cannot stop to warn, and zero is an answer a downstream block can test for. The test is on each pivot of the factorization against its own column's norm, at 10−10.
- No normalization. Like
fitby default (Normalizeoff), the coefficients are in the units of x and y themselves. Data far from the origin – x around 1000, say – makes the powers of x nearly parallel and the fit ill-conditioned, exactly as it does in MATLAB; subtract an offset upstream first. - Verified against MATLAB. Over 50 sliding 16-sample windows of a
verification run, the coefficients this block emits agree with
fit([x y], z, 'poly23')in R2026a to 2.5e−14 absolute (3.2e−15 of the largest coefficient) and the fitted value to 3.1e−15; at poly31, to 3.0e−14 and 1.2e−15. MATLAB solves by Householder QR and this block by modified Gram–Schmidt; both are orthogonal factorizations, so the error grows with the conditioning of the data rather than with its square. prepareSurfaceDatahas no counterpart. It reshapes gridded data into columns and drops non-finite points; samples here already arrive one point at a time, and a non-finite sample makes the outputs non-finite until it has left the window.- Evaluate Fit cannot read c. That block evaluates a polynomial in ONE variable; this output is a surface's coefficients in MATLAB's two-variable order. Use zf for the fitted value, or evaluate the terms yourself.
- No state space. The output is a nonlinear function of the window (the predictors enter the design matrix), so the block carries none and model reduction correctly declines to merge it.
Code facts#
| Fact | Value |
|---|---|
| registered type | Control_Systems/Curve_Fitting/Surface_Fit |
| family | Control_Systems/Curve_Fitting |
| solver environment class | ICoreBlock_0_Control_Systems_1_Curve_Fitting_2_Surface_Fit |
| source | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Curve_Fitting/Surface_Fit/ICoreBlock_0_Control_Systems_1_Curve_Fitting_2_Surface_Fit.cpp |
| header | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Curve_Fitting/Surface_Fit/ICoreBlock_0_Control_Systems_1_Curve_Fitting_2_Surface_Fit.h |
| default size on canvas | 130 × 84 px |
| ports at insert | 3 in, 2 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | x |
| 2 | in | ICoreDouble | y |
| 3 | in | ICoreDouble | z |
| 4 | out | ICoreDouble | c |
| 5 | out | ICoreDouble | zf |
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 variable | Default | Simulink parameter |
|---|---|---|
X Degree | 2 | — |
Y Degree | 2 | — |
Window Length | 12 | — |
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.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): fitting a polynomial surface to a running window is a MATLAB function (fit with prepareSurfaceData), not a Simulink library block -- the Curve Fitting Toolbox ships no Simulink library at all -- 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).
Surface Fit -- fit([x y], z, 'polyIJ') over the last W samples, as COEFFICIENTS Three scalar signals arrive per sample; the block keeps the last W of each and fits MATLAB's polyIJ surface to them every sample. The header carries the argument for the shape (a solve per sample, because both predictors are data) and for the two decisions MATLAB does not make (the warm-up and the rank-deficient window).
The per-sample program is written ONCE, below, as a short list of assignments, loops and conditions, and rendered into all ten targets by a small private renderer; compute_h() is the same program hand-written in C++, statement for statement, so the live run and every export do the same arithmetic in the same order.
Measured against R2026a rather than assumed, and measured on THIS BLOCK'S OWN OUTPUT rather than on a prototype: over 50 sliding 16-sample windows of the verification run's recorded stimulus, the coefficients agree with fit([x y], z, 'poly23') to 2.5e-14 absolute (3.2e-15 relative to the largest coefficient) and the fitted value with the same object evaluated at the newest point to 3.1e-15; at poly31, to 3.0e-14 and 1.2e-15. The term list and its order were read off coeffnames(fittype('polyIJ')) for all 36 models.
Sample results#
| t | in ICoreDouble-Out-0 | in ICoreDouble-Out-0 | in ICoreDouble-Out-0 | out ICoreDouble-Out-0 [6x1] entry 0 | out ICoreDouble-Out-1 |
|---|---|---|---|---|---|
| 0 | -2 | -2 | -2 | [0, 0, 0, 0]… | 0 |
| 0.4 | 0.5 | 0.5 | 0.5 | [0, 0, 0, 0]… | 0 |
| 0.8 | -2 | -2 | -2 | [0, 0, 0, 0]… | 0 |
| 1.2 | 0.5 | 0.5 | 0.5 | [0, 0, 0, 0]… | 0 |
| 1.6 | -2 | -2 | -2 | [0, 0, 0, 0]… | 0 |
| 2 | 0.5 | 0.5 | 0.5 | [0, 0, 0, 0]… | 0 |
| 2.4 | -2 | -2 | -2 | [0, 0, 0, 0]… | 0 |
| 2.8 | 0.5 | 0.5 | 0.5 | [0, 0, 0, 0]… | 0 |
| 3.2 | -2 | -2 | -2 | [0, 0, 0, 0]… | 0 |
| 3.6 | 0.5 | 0.5 | 0.5 | [0, 0, 0, 0]… | 0 |
| 4 | -2 | -2 | -2 | [0, 0, 0, 0]… | 0 |
| 4.4 | 0.5 | 0.5 | 0.5 | [0, 0, 0, 0]… | 0 |
| 4.8 | -2 | -2 | -2 | [0, 0, 0, 0]… | 0 |
| 5.2 | 0.5 | 0.5 | 0.5 | [0, 0, 0, 0]… | 0 |
Every 4th of 60 samples, from the table stimulus.
The same rig also ran:
| Stimulus | What it is | Output range |
|---|---|---|
impulse | Impulse: one sample of 1 at k = 5, 0 elsewhere (Repeating Sequence Stair) | 0 … 0 |
ramp | Ramp: slope 1 from t = 0 | 0 … 0 |
sine | Sine Wave: amplitude 1, 2 rad/s, no phase, no bias | 0 … 0 |
step | Step: 0 -> 1 at t = 1 s | 0 … 0 |
Plotted: table — Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample
Category static · sample time 0.1 · 60 steps · commit ae1a5a4f23bf9080195613e8ad1f6128ae5da42d · produced by docsSample --out <folder> --blocks Turbofan_Engine_System EOM_6DOF_Wind_Angles EOM_6DOF_Custom_Variable_Mass_Wind_Angles EOM_6DOF_Simple_Variable_Mass_Wind_Angles Surface_Fit Smoothing_Spline Thin_Plate_Spline LPC_To_LSF_LSP --steps 60 · data docs/generated/samples/Control_Systems__Curve_Fitting__Surface_Fit.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).