Python implements numerical computation-hermit interpolation, and python Interpolation

Source: Internet
Author: User

Python implements numerical computation-hermit interpolation, and python Interpolation

When the requirements for interpolation involve the requirements for the derivative of the interpolation function, the normal interpolation problem becomes the hermit interpolation problem. There is a low requirement for interpolation between the two functions. Only the value of the interpolation function must be equal to that of the inserted function, in this way, the value of the interpolation function on other non-interpolation nodes can be close to that of the inserted function. However, sometimes the requirements are higher. Not only must the interpolation function and the inserted function have the same value in the interpolation node function, but also must have the same derivative.

In fact, the situation is not complicated at this time. The idea of solving this problem is the same as that of the method of the Laplace interpolation method. The difference is that the constraint function of the Interpolation condition adds a derivative item. It turns out that ~ N interpolation nodes have n + 1 interpolation node, and the solution of n + 1 linear equation needs to be obtained (because it is difficult to calculate such a linear equations) that is to say, we need to construct a polynomial that does not exceed n + 1-1 = n times. Here we subtract 1 because n times of polynomials will solve n + 1 solutions, and there is a constant.

When each interpolation node has another constraint on the derivative, the linear equation becomes 2 * (n + 1) = 2n + 2, that is to say, we need to construct a polynomial of no more than 2n + 1 times.

The constructed hermit interpolation function should be in the following form:

It can be seen that the derivative constraint is added on the basis of the Laplace interpolation function.

Construct the Basis Function and when constructing the key of Hermit interpolation.

I don't know how to construct these two basic functions, but I can try to gather them online by meeting the following constraints:

  

Here I will post a matching rule.

  

It is the square of the Laplace Interpolation Basis Function.

The interpolation function is obtained by bringing the above two basis functions into the hermit interpolation polynomial.

 

Start to paste the Code:

The first post is the Laplace Interpolation Basis Function:

  

"@ Brief: Obtain the Basis Function of the Laplace interpolation @ param: xi is the abscissa of the I-th interpolation node @ param: x_set the whole interpolation node set @ return: the returned value is the interpolation base function "def get_li (xi, x_set = []): def li (Lx): W = 1; c = 1 for each_x in x_set: if each_x = xi: continue W = W * (Lx-each_x) for each_x in x_set: if each_x = xi: continue c = c * (xi-each_x) # Here it must be converted to the float type, otherwise it is prone to severe errors. the reason is not mentioned (truncation error) return W/float (c) return li

  

Then, based on the interpolation node, obtain the two basis functions required by the hermit interpolation polynomial:

  

"@ Brief: Obtain the hermit interpolation base function α (x) notice: do not evaluate partial basis function @ param: xi is the abscissa of the I-th interpolation node @ param: x_set the entire interpolation node set @ return: returns the base interpolation function "def get_basis_func_alpha (xi, x_set = []): def basis_func_alpha (x) corresponding to parameter xi ): tmp_sum = 0 for each_x in x_set: if each_x = xi: continue tmp_sum = tmp_sum + 1/float (xi-each_x) return (1 + 2 * (xi-x) * tmp_sum) * (get_li (xi, x_set) (x) ** 2 return basis_func_alpha "@ brief: Get the hermit interpolation base function β (x) notice: evaluate the basic function @ param: xi is the abscissa of the I-th interpolation node @ param: x_set the whole interpolation node set @ return: the return value is the interpolation base function "def get_basis_func_beta (xi, x_set = []): return lambda x: (x-xi) * (get_li (xi, x_set) (x) ** 2

Finally, we can construct the hermit interpolation function:

  

"@ Brief: Get the hermit interpolation function @ param: x the x coordinate set of the interpolation node @ param: fx the x coordinate set of the interpolation node @ param: deriv the derivative set of the interpolation node @ return: the interpolation function notice corresponding to the interpolation node set specified by the parameter: through the tests of the Laplace interpolation method, Newton Interpolation Method, and hermit interpolation method, it is found that the interpolation effect is very good, and the plug-in is basically unavailable, the error is very large (it cannot be called an error, it should be called an error ). "" def get_Hermite_interpolation (x = [], fx = [], deriv = []): set_of_func_alpha = [] # α (x) base function set set_of_func_beta = [] # β (x) base function set for each in x: # obtain the base function tmp_func = get_basis_func_alpha (each, x) set_of_func_alpha.append (tmp_func) # Save the interpolation base function corresponding to each element in set x to tmp_func = get_basis_func_beta (each, x) set_of_func_beta.append (tmp_func) # Save the interpolation base function corresponding to each element in set x def Hermite_interpolation (Hx): result = 0 for index in range (len (x )): result = result + fx [index] * set_of_func_alpha [index] (Hx) + deriv [index] * set_of_func_beta [index] (Hx) # Calculate the return result return Hermite_interpolation value of Lx Based on the Laplace Interpolation Method

Let's take a look at the effect:

  

After interpolation of the preceding interpolation nodes, the following results are obtained:

  

Great results.

The sample code is as follows:

  

"Demo:" if _ name _ = '_ main _': ''' interpolation node. Here, a quadratic function is used to generate an interpolation node, 10''' import math sr_x = [(I * math. pi) + (math. pi/2) for I in range (-3, 3)] sr_fx = [math. sin (I) for I in sr_x] deriv = [0 for I in sr_x] # The derivatives are 0 Hx = get_Hermite_interpolation (sr_x, sr_fx, deriv) # obtain the interpolation function tmp_x = [I * 0.1 * math. pi for I in range (-20, 20)] # Test Case tmp_y = [Hx (I) for I in tmp_x] # obtain the Y coordinate of the test case based on the interpolation function ''' drawing ''' import matplotlib. pyplot as plt. figure ("play") ax1 = plt. subplot (211) plt. sca (ax1) plt. plot (sr_x, sr_fx, linestyle = '', marker = 'O', color = 'B') plt. plot (tmp_x, tmp_y, linestyle = '--', color = 'R') plt. show ()

By observing the above principle, in fact, when we encounter more demanding interpolation problems in the future, for example, the interpolation function and the inserted function are required to be three times, and the four derivatives are equal, we can also use a similar method, but it may take some time to construct the corresponding base function, and the computing complexity will increase.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.