pysdic.numeric.fit_polynomial#
- fit_polynomial(x, data, degree, *, axis=-1)[source]#
Fit a polynomial \(P(x) = c_0 + c_1 x + \dots + c_d x^d\) to sampled data, in the least squares sense, along an axis.
All the signals along the axis are fitted at once. To keep the problem well conditioned, the fit is computed with the centered and scaled variable \(x' = (x - \bar{x}) / s\) (\(\bar{x}\) the mean and \(s\) the half range of \(x\)), and the coefficients are converted back to the variable \(x\).
- Parameters:
x (ArrayLike) – The finite sampling points, of shape \((N,)\) with at least \(d + 1\) distinct values.
data (ArrayLike) – The finite sampled values, with \(N\) values along
axis.degree (Integral) – The degree \(d \geq 0\) of the polynomial.
axis (Integral, optional (default:
-1)) – The axis of the sampling indata.
- Returns:
The coefficients \((c_0, \dots, c_d)\) in increasing degree, with the shape of
datawhere the axis has the length \(d + 1\).- Return type:
- Raises:
ValueError – If an input has a wrong shape or non-finite values, or there are not enough distinct sampling points for the degree.
Examples
import numpy from pysdic.numeric import fit_polynomial x = numpy.linspace(0, 1, 10) fit_polynomial(x, 1 + 2 * x - x**2, 2) # [1.0, 2.0, -1.0]