polyfit — Fit least-squares polynomials in MATLAB and RunMat.

polyfit(x, y, n) returns coefficients of an n-th degree least-squares polynomial fit. Coefficient ordering, centering/scaling outputs, and diagnostics follow MATLAB semantics.

Syntax

p = polyfit(X, Y, n)
p = polyfit(X, Y, n, weights)
[p, S] = polyfit(X, Y, n)
[p, S] = polyfit(X, Y, n, weights)
[p, S, mu] = polyfit(X, Y, n)
[p, S, mu] = polyfit(X, Y, n, weights)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesSample x-values as a numeric vector.
YAnyYesSample y-values as a numeric vector.
nIntegerScalarYesPolynomial degree.
weightsAnyNoOptional nonnegative weight vector matching X and Y length.

Returns

NameTypeDescription
pAnyPolynomial coefficient vector ordered from highest power to constant term.
SAnyFit statistics structure with fields R, df, and normr.
muNumericArrayCentering and scaling vector [mean(x), std(x)].

Returned values from polyfit depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:polyfit:InvalidArgumentDegree/weight arguments are malformed or unsupported.polyfit: invalid argument
RunMat:polyfit:InvalidInputInput vectors or values cannot be processed for polynomial fitting.polyfit: invalid input
RunMat:polyfit:InternalRuntime fails while constructing fit outputs or provider payloads.polyfit: internal runtime failure

How polyfit works

  • 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.

Does RunMat run polyfit 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.

Using polyfit with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how polyfit changes the result.

Run a small polyfit example, explain the result, then change one input and compare the output.

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.

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref

Symbolic

digits · int · limit · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

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

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.