|
| 1 | +# Polynomial Least-Squares Fitting |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +Sensor calibration curves, ADC linearization, thermistor transfer functions, and drift trends |
| 6 | +all require fitting a smooth curve to a discrete set of measured points. A degree-$d$ polynomial |
| 7 | +captures these behaviors with only $d+1$ coefficients, making evaluation at runtime a handful of |
| 8 | +multiply-adds rather than a table lookup or expensive transcendental. |
| 9 | + |
| 10 | +The least-squares formulation finds the polynomial that minimizes the sum of squared residuals |
| 11 | +across all measurement samples. Unlike exact interpolation, it is robust to measurement noise: |
| 12 | +extra samples average out errors rather than being forced to pass through noisy points. |
| 13 | + |
| 14 | +## Mathematical Theory |
| 15 | + |
| 16 | +### The Model |
| 17 | + |
| 18 | +Given $n$ scalar observations $\{(x_i, y_i)\}_{i=0}^{n-1}$, the degree-$d$ polynomial model is |
| 19 | + |
| 20 | +$$p(x) = c_0 + c_1 x + c_2 x^2 + \cdots + c_d x^d$$ |
| 21 | + |
| 22 | +The goal is to find the coefficient vector $\mathbf{c} \in \mathbb{R}^{d+1}$ that minimizes |
| 23 | + |
| 24 | +$$\min_{\mathbf{c}} \sum_{i=0}^{n-1} \bigl(y_i - p(x_i)\bigr)^2$$ |
| 25 | + |
| 26 | +### Vandermonde Design Matrix |
| 27 | + |
| 28 | +Stacking the model evaluations at all sample abscissae gives the Vandermonde matrix |
| 29 | + |
| 30 | +$$\mathbf{V} \in \mathbb{R}^{n \times (d+1)}, \quad V_{i,j} = x_i^j$$ |
| 31 | + |
| 32 | +The least-squares problem then becomes $\min_{\mathbf{c}} \|\mathbf{V}\mathbf{c} - \mathbf{y}\|^2$. |
| 33 | + |
| 34 | +### Normal Equations |
| 35 | + |
| 36 | +Setting the gradient of the squared residual with respect to $\mathbf{c}$ to zero yields |
| 37 | + |
| 38 | +$$(\mathbf{V}^\top \mathbf{V})\,\mathbf{c} = \mathbf{V}^\top \mathbf{y}$$ |
| 39 | + |
| 40 | +The $(d+1)\times(d+1)$ matrix $\mathbf{V}^\top\mathbf{V}$ is symmetric and, when the abscissae are |
| 41 | +distinct and $n \geq d+1$, positive-definite. Its small size allows direct solution by Gaussian |
| 42 | +elimination or Cholesky factorization in bounded time on embedded hardware. |
| 43 | + |
| 44 | +### Horner Evaluation |
| 45 | + |
| 46 | +Once $\mathbf{c}$ is known, evaluating $p(x)$ at a new point uses Horner's method |
| 47 | + |
| 48 | +$$p(x) = c_0 + x\bigl(c_1 + x\bigl(c_2 + \cdots + x\,c_d\bigr)\cdots\bigr)$$ |
| 49 | + |
| 50 | +This requires exactly $d$ multiplications and $d$ additions — optimal for a degree-$d$ polynomial. |
| 51 | + |
| 52 | +## Complexity Analysis |
| 53 | + |
| 54 | +| Phase | Time | Space | Notes | |
| 55 | +|----------------------------------|-------------|-----------|-----------------------------------| |
| 56 | +| Build $\mathbf{V}$ | $O(n\,d)$ | $O(n\,d)$ | Incremental powers, no `pow()` | |
| 57 | +| Form $\mathbf{V}^\top\mathbf{V}$ | $O(n\,d^2)$ | $O(d^2)$ | Symmetric, only upper half needed | |
| 58 | +| Form $\mathbf{V}^\top\mathbf{y}$ | $O(n\,d)$ | $O(d)$ | Matrix-vector product | |
| 59 | +| Solve $(d+1)\times(d+1)$ system | $O(d^3)$ | $O(d^2)$ | Gaussian elimination | |
| 60 | +| Predict (Horner) | $O(d)$ | $O(1)$ | One MAC per coefficient | |
| 61 | + |
| 62 | +All dimensions are compile-time constants; no heap allocation is required. |
| 63 | + |
| 64 | +## Step-by-Step Walkthrough |
| 65 | + |
| 66 | +**Data:** $n = 4$ samples, $d = 2$ (quadratic fit). |
| 67 | + |
| 68 | +| $x_i$ | $y_i$ | |
| 69 | +|-------|-------| |
| 70 | +| 0 | 1 | |
| 71 | +| 1 | 0.75 | |
| 72 | +| 2 | 1 | |
| 73 | +| 3 | 1.75 | |
| 74 | + |
| 75 | +**Step 1 — Build $\mathbf{V}$:** |
| 76 | + |
| 77 | +$$\mathbf{V} = \begin{bmatrix} 1 & 0 & 0 \\ 1 & 1 & 1 \\ 1 & 2 & 4 \\ 1 & 3 & 9 \end{bmatrix}$$ |
| 78 | + |
| 79 | +**Step 2 — Normal equations:** |
| 80 | + |
| 81 | +$$\mathbf{V}^\top\mathbf{V} = \begin{bmatrix} 4 & 6 & 14 \\ 6 & 14 & 36 \\ 14 & 36 & 98 \end{bmatrix}, \qquad \mathbf{V}^\top\mathbf{y} = \begin{bmatrix} 4.5 \\ 7.25 \\ 19.75 \end{bmatrix}$$ |
| 82 | + |
| 83 | +**Step 3 — Solve:** Gaussian elimination → $\mathbf{c} \approx [1,\,-0.5,\,0.25]^\top$. |
| 84 | + |
| 85 | +**Result:** $p(x) = 1 - 0.5\,x + 0.25\,x^2$. |
| 86 | + |
| 87 | +**Prediction at $x = 1.5$:** |
| 88 | + |
| 89 | +$$p(1.5) = 0.25\cdot1.5^2 - 0.5\cdot1.5 + 1 = 0.5625 - 0.75 + 1 = 0.8125$$ |
| 90 | + |
| 91 | +## Pitfalls & Edge Cases |
| 92 | + |
| 93 | +- **Ill-conditioning of the Vandermonde system.** The condition number of $\mathbf{V}^\top\mathbf{V}$ |
| 94 | + grows exponentially with $d$ and with the spread of abscissae. Center and scale the abscissa |
| 95 | + $x \leftarrow (x - \bar{x})/\sigma_x$ before fitting to reduce condition numbers by orders of |
| 96 | + magnitude. Recommended for $d \geq 3$ or when abscissae are far from the origin. |
| 97 | + |
| 98 | +- **Degree selection.** Over-fitting occurs when $d$ is too large relative to $n$ or to the |
| 99 | + signal-to-noise ratio. Keep $d \leq 4$ for typical embedded calibration tasks. |
| 100 | + |
| 101 | +- **Exactly $n = d+1$ points.** The normal equation system has a unique solution equal to the |
| 102 | + interpolating polynomial; the residual is zero. The system is well-posed only if all abscissae |
| 103 | + are distinct. |
| 104 | + |
| 105 | +- **Repeated or nearly-coincident abscissae.** $\mathbf{V}^\top\mathbf{V}$ becomes singular or |
| 106 | + nearly so. Partial-pivoting in the Gaussian solver will flag this via `really_assert`; avoid |
| 107 | + duplicate $x$ values in practice. |
| 108 | + |
| 109 | +- **Large degree with `float` arithmetic.** Powers $x^d$ for $|x| \gg 1$ can exceed the `float` |
| 110 | + dynamic range. Centering/scaling eliminates this risk. |
| 111 | + |
| 112 | +## Variants & Generalizations |
| 113 | + |
| 114 | +| Variant | Key Difference | |
| 115 | +|-----------------------------|------------------------------------------------------------------------------| |
| 116 | +| Orthogonal polynomial basis | Uses Legendre/Chebyshev basis instead of monomials; much better conditioning | |
| 117 | +| Weighted least squares | Each sample weighted differently (e.g., by measurement precision) | |
| 118 | +| Regularized (Ridge) fitting | Adds $\lambda\|\mathbf{c}\|^2$ to damp large coefficients | |
| 119 | +| Constrained fitting | Enforces derivative constraints at endpoints | |
| 120 | +| Savitzky-Golay smoothing | Sliding-window polynomial fit for real-time derivative estimation | |
| 121 | + |
| 122 | +## Applications |
| 123 | + |
| 124 | +- **Sensor linearization** — converting thermistor resistance or pressure-sensor ADC counts to |
| 125 | + engineering units via a quadratic or cubic polynomial. |
| 126 | +- **Drift and aging compensation** — fitting a polynomial to sampled drift data and subtracting |
| 127 | + the trend from future measurements. |
| 128 | +- **Compact lookup-table replacement** — replacing a 256-entry table with a degree-3 polynomial |
| 129 | + evaluated in four MACs. |
| 130 | +- **Calibration curve storage** — a handful of coefficients in flash replace a bulky lookup table. |
| 131 | + |
| 132 | +## Connections to Other Algorithms |
| 133 | + |
| 134 | +```mermaid |
| 135 | +graph LR |
| 136 | + PF["Polynomial Fitting"] |
| 137 | + GE["Gaussian Elimination"] |
| 138 | + LR["Linear Regression"] |
| 139 | + SG["Savitzky-Golay (planned)"] |
| 140 | + RLS["Recursive Least Squares"] |
| 141 | +
|
| 142 | + PF --> GE |
| 143 | + PF -.->|"polynomial features = special case"| LR |
| 144 | + SG -.->|"local polynomial fit per window"| PF |
| 145 | + RLS -.->|"online counterpart"| PF |
| 146 | +``` |
| 147 | + |
| 148 | +| Algorithm | Relationship | |
| 149 | +|-----------------------------------------------------------|------------------------------------------------------------------| |
| 150 | +| [Gaussian Elimination](../solvers/GaussianElimination.md) | Solves the normal equations | |
| 151 | +| [Linear Regression](LinearRegression.md) | Polynomial fitting is linear regression with polynomial features | |
| 152 | +| [Recursive Least Squares](RecursiveLeastSquares.md) | Online / streaming counterpart for time-varying models | |
| 153 | + |
| 154 | +## References & Further Reading |
| 155 | + |
| 156 | +- Press, W. H., Teukolsky, S. A., Vetterling, W. T. and Flannery, B. P., *Numerical Recipes in C*, 3rd ed., Cambridge University Press, 2007 — Chapter 15 (Modeling of Data). |
| 157 | +- Golub, G. H. and Van Loan, C. F., *Matrix Computations*, 4th ed., Johns Hopkins University Press, 2013 — Chapter 5 (orthogonal factorizations and least squares). |
| 158 | +- Hildebrand, F. B., *Introduction to Numerical Analysis*, 2nd ed., Dover, 1987 — Chapter 7 (least-squares approximation). |
0 commit comments