View all functions

CategoryMath: Poly
GPUYes

What does the polyfit function do in MATLAB / RunMat?

polyfit(x, y, n) returns the coefficients of an n-th degree polynomial that fits the samples (x, y) in a least-squares sense. The coefficients are ordered from the highest power of x (or t when centering is applied) down to the constant term, matching MATLAB's convention.

How does the polyfit function behave in MATLAB / RunMat?

  • x and y must contain the same number of finite numeric elements. They can be row vectors, column vectors, or N-D arrays; RunMat flattens them column-major (MATLAB order).
  • The optional fourth argument provides weights: polyfit(x, y, n, w) minimises ||sqrt(w) .* (y - polyval(p, x))||_2. All weights must be real, non-negative, and match the shape of x.
  • polyfit applies centring and scaling to the independent variable for numerical stability. It returns the vector mu = [mean(x), std(x)], and the polynomial fits (x - mu(1)) / mu(2). Use polyval(p, x, [], mu) to evaluate with the same scaling.
  • [p, S, mu] = polyfit(...) also returns S, a structure with fields R, df, and normr. These match MATLAB and are accepted directly by polyval for computing prediction intervals. S.R is the upper-triangular factor from the QR decomposition of the scaled Vandermonde matrix, S.df is the degrees of freedom (max(numel(x) - (n + 1), 0)), and S.normr is the 2-norm of the weighted residuals.
  • RunMat mirrors MATLAB error messages for inconsistent dimensions, non-integer degrees, singular scaling, and invalid weights.

polyfit Function GPU Execution Behavior

RunMat’s acceleration layer exposes a dedicated polyfit provider hook. The WGPU provider routes requests through this hook, gathers inputs to the host, executes the shared Householder QR solver, and returns MATLAB-compatible outputs while preserving residency metadata. Providers that have not implemented the hook yet fall back to the same host code path automatically, so results stay correct even when the inputs originated on the GPU.

Examples of using the polyfit function in MATLAB / RunMat

Fitting a straight line through noisy samples

x = 0:5;
y = 2.5 * x + 1 + 0.05 * randn(size(x));
p = polyfit(x, y, 1);

Expected output:

p ≈ [2.5 1.0];   % slope and intercept recovered from noisy data

Computing a quadratic fit and reusing mu

x = -3:3;
y = x.^2 - 2 * x + 4;
[p, S, mu] = polyfit(x, y, 2);
smoothed = polyval(p, x, [], mu);

Expected output:

smoothed matches y exactly because the quadratic model fits perfectly.

Retrieving the structure S for prediction intervals

t = linspace(0, 2*pi, 25);
y = sin(t) + 0.1 * randn(size(t));
[p, S, mu] = polyfit(t, y, 3);
[fitted, delta] = polyval(p, t, S, mu);

Expected output:

delta contains the one-standard-deviation prediction interval for each fitted sample.

Weighted polynomial fit

x = linspace(-1, 1, 7);
y = [1.1 0.4 0.2 0.0 0.1 0.5 1.4];
w = [1 2 2 4 2 2 1];
p = polyfit(x, y, 2, w);

Expected output:

Central samples influence the fit more heavily, matching MATLAB's weighting semantics.

Using polyfit with gpuArray inputs

x = gpuArray.linspace(-2, 2, 50);
y = gpuArray((x - 0.5).^3);
p = polyfit(x, y, 3);

Expected output:

p is returned on the host today; convert it back to gpuArray if desired.

Complex-valued data with polyfit

x = 0:4;
y = exp(1i * x);
p = polyfit(x, y, 2);

Expected output:

p is complex-valued and matches MATLAB's complex least-squares solution.

FAQ

What degree polynomial should I choose?

You must specify the degree n explicitly. A degree of numel(x) - 1 interpolates the data exactly, but higher degrees amplify noise dramatically. Use validation, cross-validation, or domain knowledge to select a sensible degree.

What do the outputs S and mu represent?

S packages the QR factors (R), degrees of freedom (df), and residual norm (normr) so you can call polyval(p, x, S, mu) to obtain prediction intervals. mu = [mean(x), std(x)] records the centering and scaling applied during the fit.

How are weights interpreted?

Weights act multiplicatively on the residual norm. RunMat minimises ||sqrt(w) .* (y - polyval(p, x))||_2. Zero weights ignore corresponding samples; negative weights are not allowed and trigger MATLAB-style errors.

Can I keep the outputs on the GPU?

Today the solver runs on the CPU even when inputs live on the GPU. The returned coefficients, S, and mu are CPU values. You can convert them back to gpuArray manually if needed. Future provider updates may keep everything on the device automatically.

Why does mu(2) need to be non-zero?

mu(2) is the scaling factor (standard deviation) applied to x. If all x values are identical, the polynomial is ill-conditioned. RunMat mirrors MATLAB by treating this as a singular scaling and raising an error unless the degree is zero.

See Also

polyval, poly, roots, mldivide, gpuArray, gather

Source & Feedback

  • Source: crates/runmat-runtime/src/builtins/math/poly/polyfit.rs
  • Found an issue? Open a RunMat issue with a minimal repro.