Python and numerical calculation method

Source: Internet
Author: User

3 Interpolation and curve fitting

Interpolation and Curve Fitting
Given the n+1 number of points (xi,yi), i = 0,1,2,..., n, evaluate y (x).

3.1 Introduction (Introduction)

discrete datasets, or forms like the following, are often used in technical calculations, and data sources may come from experimental observations or numerical calculations.

3.2 Polynomial interpolation (polynomial interpolation)
There are differences between interpolation and curve fitting. For interpolation, we fit the data into a curve, in the fitting process, we have the potential to assume that the data is accurate and unique; for curve fitting, the data used usually has a measurement error and introduces noise, in a way, we want to find a smooth curve approximation data points, so that the curve does not have to pass through each data point. The differences between interpolation and curve fitting are as follows:

Lagrange ' s MethodLaGrand as Method
The simplest form of interpolation is a polynomial,n+1A clear data point to build a degree of freedomNThe specific polynomial is always achievable. The method that contains this polynomial is the Lagrange equation:

where the base functions (cardinal function)li (x)As follows:

  • Example 1:n=1,p1 (x) =y0l0 (x) +y1l1 (x )
  • Example 2:n=2,

By observing, the base function has the following properties

  • is a polynomial of degrees of freedom n

  • Note: Kronecker Delta (Δij), whenn=2,x0=0,x1=2,x2=3When the nature is as

The

Polynomial interpolation error is as follows:

& #x03BE; ">ξ is located in the interval ( x 0 " >x0 , x n " >xn )

Newton Method Newton ' s Method
The interpolation polynomial of Newton's method is as follows:

For n=3with four data points, the polynomial is as follows:

n=3, which facilitates programming, defines the following form:

n, defined as follows:

Denoting the x-coordinate array of the data points by XData and the degree of the polynomial by N, we have the following a Lgorithm for computing Pn (x):

p = a[n]for k in range(1, n+1):    p = a[n-k] + (x - xData[n-k])*p

CoefficientPnForcing a polynomial through each data point:YI=PN (xi),i=0,1,..., N。 The following equation occurs at the same time:

Introducing the concept of mean difference (divided differences)

Then there are:

Forn=4, the manual calculation factor can be quickly resolved by the following table:

is exactly the coefficient of the polynomial.

Machine computations can is carried out within a one-dimensional array a employing the following algorithm ation m = n + 1 = number of data points):
The Python calculation process is as follows:

a = yData.copy()for k in range(1,m):    for i in range(k, m):        a[i] = (a[i] - a[k-1])/(xData[i] - xData[k-1])

Initially, a contains the y-coordinate of the data, so it is the same as the second column in the previous table. Each time an external loop is passed, an entry is generated in the next column, which overrides the corresponding element of a. Therefore, the end contains the diagonal items in the table above (that is, the coefficients of the polynomial).

Python code for the

Newton polynomial interpolation method
Newton ' s method . Given the data point arrays XData and Ydata, the function coeffts returns the coefficient array A. After the coefficients is found, the interpolant P N ( x ) >PN (x) can be evaluated at any value of X with the function Evalpoly.

def evalPoly(a, xData, x):    n = len(xData) - 1    p = a[n]    for k in range(1, n+1):        p = a[n-k]  + (x - xData[n-k])*p    return pdef coeffts(xData, yData):    m = len(xData)    a = yData.copy()    for k in range(1,m):        a[k:m] = (a[k:m] - a[k-1]) / (xData[k:m] - xData[k-1])    return a

Reference translation "Numerical Methods in Engineering with Python 3"

Python and numerical calculation method

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.