Wednesday, July 3, 2024
HomeLanguagesPythonGet the Least squares fit of Hermite series to data in Python

Get the Least squares fit of Hermite series to data in Python

In this article, we will discuss how to find the Least-squares fit of the Hermite series to data in Python and NumPy.

NumPy.polynomials.hermite.hermfit method

The Hermite series is an orthogonal polynomial sequence that has its applications in physics, wave theory, numerical analysis, and signal processing. Here, hermfit method is used to get the least-squares fit of the Hermite series to data, NumPy provides a function called Hermite.hermfit(). This function returns the coefficients of a Hermite series of degree deg that is the least-squares fit to the data values y given at points x. For the 1-D y- coordinate the least-squares coefficient returned is also 1-D. If y is more than 1-D multiple fits are done, one for each column of y, and the resulting coefficients are stored in the corresponding columns of a 2-D return.

Syntax: hermite.hermfit(x, y, deg, full)

Parameters:

  • x, y: array 
  • deg: degree of fitting polynomials
  • full: boolean nature of returning value
  • w:  weights

Returns: 

  • coef: array of Hermite series.

Example 1:

In the first example, let us take an X-coordinate array of size 30 in range (-1,1)  and let us define the Y-coordinate using NumPy random function as shown. Import the necessary package and pass the appropriate parameters as shown.

Python3




import numpy as np
from numpy.polynomial import hermite
  
# x - co ordinate
x = np.linspace(-2, 1, 30)
  
# defining the y - co ordinate using numpy random
y = x**3 - x + np.random.randn(len(x))
  
# finding the least squares
c, stats = hermite.hermfit(x, y, 3, full=True)
  
  
print(f'The x coordinate array is \n{x}\n')
print(f'The y coordinate array is \n{y}\n')
print(f'The hermit coefficient are \n{c}\n')
print(f'The resultant array is \n{stats}\n')


Output:

The x coordinate array is 

[-2.         -1.89655172 -1.79310345 -1.68965517 -1.5862069  -1.48275862

 -1.37931034 -1.27586207 -1.17241379 -1.06896552 -0.96551724 -0.86206897

 -0.75862069 -0.65517241 -0.55172414 -0.44827586 -0.34482759 -0.24137931

 -0.13793103 -0.03448276  0.06896552  0.17241379  0.27586207  0.37931034

  0.48275862  0.5862069   0.68965517  0.79310345  0.89655172  1.        ]

The y coordinate array is 

[-6.60789595 -5.02995614 -3.82266632 -4.29148936 -3.41689134 -2.73899431

 -2.80203248  0.42527851  1.56309224 -0.15421266  0.01694003 -0.27000318

  1.17370229  0.54996278  1.90737212  1.05195276  1.22928767 -0.24762963

  0.57362607 -0.17531795  0.9954843  -0.33641804 -0.14227936 -0.67820814

  1.36224629  0.31530983 -0.17916042 -0.41791427 -0.26638476  0.4571403 ]

The hermit coefficient are 

[ 0.32269386  0.23402692 -0.07321117  0.12818421]

The resultant array is 

[array([17.45846549]), 4, array([1.68066214, 0.84234764, 0.65197165, 0.2018866 ]), 6.661338147750939e-15]

Example 2:

In the second example, let us take an X-coordinate array of size 50 in range (-10,10)  and let us define a different Y-coordinate using the NumPy uniform function as shown. Import the necessary package and pass the appropriate parameters as shown.

Python3




import numpy as np
from numpy.polynomial import hermite
  
# x - co ordinate
x = np.linspace(-10, 10, 50)
  
# defining the y - co ordinate using numpy random
y = x**4 - x + np.random.uniform(len(x))
  
# finding the least squares
c, stats = hermite.hermfit(x, y, 3, full=True)
  
print(f'The x coordinate array is \n{x}\n')
print(f'The y coordinate array is \n{y}\n')
print(f'The hermit coefficient are \n{c}\n')
print(f'The resultant array is \n{stats}\n')


Output:

The x coordinate array is 

[-10.          -9.59183673  -9.18367347  -8.7755102   -8.36734694

  -7.95918367  -7.55102041  -7.14285714  -6.73469388  -6.32653061

  -5.91836735  -5.51020408  -5.10204082  -4.69387755  -4.28571429

  -3.87755102  -3.46938776  -3.06122449  -2.65306122  -2.24489796

  -1.83673469  -1.42857143  -1.02040816  -0.6122449   -0.20408163

   0.20408163   0.6122449    1.02040816   1.42857143   1.83673469

   2.24489796   2.65306122   3.06122449   3.46938776   3.87755102

   4.28571429   4.69387755   5.10204082   5.51020408   5.91836735

   6.32653061   6.73469388   7.14285714   7.55102041   7.95918367

   8.36734694   8.7755102    9.18367347   9.59183673  10.        ]

The y coordinate array is 

[1.00153754e+04 8.47958029e+03 7.12777075e+03 5.94462618e+03

 4.91549213e+03 4.02638027e+03 3.26396835e+03 2.61560027e+03

 2.06928601e+03 1.61370168e+03 1.23818950e+03 9.32757802e+02

 6.88081016e+02 4.95499697e+02 3.47020507e+02 2.35316221e+02

 1.53725724e+02 9.62540125e+01 5.75721951e+01 3.30174914e+01

 1.85932329e+01 1.09688622e+01 7.47993342e+00 6.12811226e+00

 5.58117577e+00 5.17301251e+00 4.90362246e+00 5.43911710e+00

 8.11171933e+00 1.49197635e+01 2.85276955e+01 5.22660726e+01

 9.01315635e+01 1.46786949e+02 2.27561119e+02 3.38449079e+02

 4.86111942e+02 6.77876934e+02 9.21737393e+02 1.22635277e+03

 1.60104862e+03 2.05581662e+03 2.60131455e+03 3.24886631e+03

 4.01046190e+03 4.89875744e+03 5.92707516e+03 7.10940340e+03

 8.46039661e+03 9.99537536e+03]

The hermit coefficient are 

[-8.75646882e+02 -5.00000000e-01  2.22734575e+01 -1.47220903e-16]

The resultant array is 

[array([33708458.779302]), 4, array([1.38316131, 1.31936531, 0.50919072, 0.29472834]), 1.1102230246251565e-14]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments