RunMat
GitHub

polyfit — Fit an n-th degree polynomial to data points using least squares with MATLAB-compatible outputs.

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 polyfit works in 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.

How polyfit runs on the GPU

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

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.

These functions work well alongside polyfit. Each page has runnable examples you can try in the browser.

polyval, roots, mldivide, gpuArray, gather, polyder, polyint

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how polyfit works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.