1. Mathematical Principles
For a polynomial function with a known k+1 point, assuming that any of the two are different, the Lagrangian interpolation polynomial obtained by the Lagrange interpolation formula is:
Each of these LJ (x) is a lagrand-day basic polynomial (or interpolation-based function) whose expression is:
2. Lightweight implementation
Use
Directly write the program, you can interpolate directly, and get the corresponding function value. However, the coefficients cannot be obtained and the various operations cannot be performed on them.
def h (x,y,a): ans=0.0 for i in range (len (y)): T=y[i] for J in range (Len (y)): If I!=j: t*= (a-x[ J])/(X[i]-x[j]) ans +=t return Ansx=[1,0]y=[0,2]print (H (x,y,2))
In the code above, H (x,y,a) is an interpolation function, which is called directly on the line. The parameters are described as follows:
- The x, y, and y values are the corresponding points. Detailed explanations.
- A is the value of the function you want to get.
In fact, the simplest Lagrange interpolation is a straight line from the two-point formula.
For example:
P-Point (1,0) Q-point (0,2)
These two points determine a straight line, so when x=2, y should be-2
The code is to use these two point interpolation, then a as the x=2 call function validation.
3. Referencing the installation of Library 3.1 libraries
Mainly depends on the scipy. See the official website: https://www.scipy.org/install.html
The installation method is simple, which is to use PIP install scipy if it fails, download the WHL file to local and then use the command to install it.
Maybe if numpy is not installed
3.2 Use of libraries
From Scipy.interplotate Import Lagrange
Simply call the Lagrange (x, Y) function to return an object.
The parameters x, y, respectively, correspond to the respective points of each point.
For example: (3,5) (5,9) These three points, as function input should be written like this:
x=[1,3,5]
Y =[2, 5, 9]
A=lagrange (x, y)
You can see the interpolation function by outputting the object directly.
With this object, you can get a lot of features. See also: https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.poly1d.html
- A.ordeR Get Order
- a[] get coefficient
- A () gets the corresponding function value
- You can also perform a subtraction operation on it
3.3 Code Implementation
From scipy.interpolate import lagrangex=[1,2,3,4,7]y=[5,7,10,3,9]a=lagrange (x, y) print (a) print (a (1), A (2), A (3)) Print (A[0],a[2],a[3])
The result is:
<class ' numpy.lib.polynomial.poly1d ' > 4
4 3 2
0.5472 x-7.306 x + 30.65 x-47.03 x + 28.13
5.0 7.0 10.0
28.1333333333 30.6527777778-7.30555555556
Explain:
<class ' numpy.lib.polynomial.poly1d ' > 4
This line is the type of output a, and the highest power.
4 3 2
0.5472 x-7.306 x + 30.65 x-47.03 x + 28.13
The second and third lines are the results of the interpolation, and the function is displayed.
The number in the second row corresponds to the power of the X in the afternoon, and if the correspondence is not aligned, it is a typesetting problem.
5.0 7.0 10.0
Line four is the X value of the surrogate, and the result is obtained.
In other words, the result can be obtained directly by using this form of parentheses f (x).
28.1333333333 30.6527777778-7.30555555556
The last line is the extracted coefficients. That is, you can use F[a] this form to extract the coefficients corresponding to the power.
Lagrange Interpolation Python code implementation