pyoptools.misc.lsq.lsq module

pyoptools.misc.lsq.lsq.polyfit2d(x, y, z, order=2)

Perform a 2D polynomial fit using least squares.

This function fits a polynomial of a specified order to the provided two-dimensional data (x, y, z) using a least-squares approach. The fitting is done using a Vandermonde matrix, and the coefficients of the polynomial are solved to minimize the sum of squared residuals.

Parameters:
  • x (array-like) – 1D array representing the x-coordinates of the data points.

  • y (array-like) – 1D array representing the y-coordinates of the data points.

  • z (array-like) – 1D array representing the z-values (dependent variable) at the given (x, y) points.

  • order (int, optional) – The order of the polynomial to fit (default is 2).

Returns:

  • ret_poly (Poly2D) – A Poly2D object representing the fitted polynomial function.

  • e (float) – The root mean square error (RMSE) of the fit, calculated as the square root of the mean of the squared differences between the observed and fitted values.

Examples

>>> x = [1.0, 2.0, 3.0]
>>> y = [1.0, 2.0, 3.0]
>>> z = [1.0, 4.0, 9.0]
>>> ret_poly, error = polyfit2d(x, y, z, order=2)
>>> print(ret_poly)
Poly2D object with coefficients: [ ... ]
>>> print(error)
0.1234

Notes

The polynomial fit is performed using a Vandermonde matrix construction for two-dimensional data. The matrix is formed based on the powers of the input coordinates (x and y), and the least-squares solution is obtained by solving the corresponding normal equations.

pyoptools.misc.lsq.lsq.polyfito2(x, y, z)

Polyfit function optimized for polynomials of order 2

pyoptools.misc.lsq.lsq.vander_matrix(x, y, z, order=2)