Mathematical Modeling - Interpolation and Fitting

1. Interpolation and fitting

1. What is interpolation and fitting?

 Interpolation and fitting is to construct a function through sample data. If the constructed function image is required to pass through all sample points, it is interpolation, otherwise it is fitting.

2. Commonly used one-dimensional interpolation methods

(1) piecewise linear interpolation

 Piecewise linear interpolation is to first divide the sample into n segments, and find a segmental polynomial P(x) to satisfy it on each segment: P i ( x ) = y i , i = 0 , 1... , n P_{i}(x)=y_{i} ,i=0,1...,n Pi​(x)=yi​,i=0,1...,n

(2) Spline interpolation

 Spline interpolation is to divide the sample into n segments, that is, to set (n+1) points. set point to x i , i = 0 , 1 , . . . , n x_{i},i=0,1,...,n xi​,i=0,1,...,n. have functions S ( x ) S(x) S(x) satisfies S i ( x ) S_{i}(x) Si​(x):
(1) in the interval [ x i , x i + 1 ] [x_{i},x_{i+1}] [xi​,xi+1​] is m m m degree polynomial
(2) in the interval [ x 0 , x n ] [x_{0},x_{n}] [x0​,xn​] on m − 1 m-1 m−1 order continuous derivative
then called S ( x ) S(x) S(x) is about this division m m spline function of degree m, m m The curve of the spline function of degree m is called a spline curve.
  It can be seen that the piecewise linear interpolation is actually a spline interpolation.
  One-dimensional interpolation in Python can be implemented with the interp1d function of the scipy.interpolate module.

3. Commonly used two-dimensional interpolation methods

(1) The interpolation node is grid data

 When the interpolation node is grid data, bicubic spline interpolation is generally used. In Python, it can be implemented with the interp2d function of the scipy.interpolate module.

This is the grid interpolation node

This is a non-grid interpolation node (if it is written in the form of the above table, there will only be z data on the diagonal of the table)

(2) The interpolation node is non-grid data

  Solved using the griddata function of the scipy.interpolate module.

4. Fitting

  Least squares fitting is a commonly used method for fitting curves in mathematics. There are fitting functions designed according to this principle in Python. For example, the curve_fit function of the scipy.optimize module uses nonlinear least squares fitting. and numpy's polyfit polynomial least squares fitting.
 Different from obtaining the interpolation function, the fitting function must be guessed before fitting. Guessing is best done on the basis of scatterplots of existing data. There are the following principles to choose the appropriate fitting function:

data trendsoptional function
tends to a straight line f ( x ) = a 1 x + a 2 f(x)=a_{1}x+a_{2} f(x)=a1​x+a2​
tends to parabola f ( x ) = a 1 x 2 + a 2 x + a 3 f(x)=a_{1}x^{2}+a_{2}x+a_{3} f(x)=a1​x2+a2​x+a3​
start rising fast then rise slowly f ( x ) = a 1 e − a 2 x f(x)=a_{1}e^{\frac{-a_{2}}{x}} f(x)=a1​ex−a2​or f ( x ) = x a 1 x + a 2 f(x)=\frac{x}{a_{1}x+a_{2}} f(x)=a1​x+a2​x​
start falling fast then fall slowly f ( x ) = a 1 e − a 2 x f(x)=a_{1}e^{-a_{2}x} f(x)=a1​e−a2​x or f ( x ) = 1 a 1 x + a 2 f(x)=\frac{1}{a_{1}x+a_{2}} f(x)=a1​x+a2​1​or f ( x ) = 1 a 1 x 2 + a 2 f(x)=\frac{1}{a_{1}x^{2}+a_{2}} f(x)=a1​x2+a2​1​

2. Interpolation function

1.interp1d function

1.grammar: scipy.interpolate.interp1d(x, y, kind='linear', axis=- 1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)
2.Function: interpolate one-dimensional function
3.Common parameters:
(1)x: real-valued vector representing the interpolation node
(2)y: N Dimensional real-valued array, the length of the interpolation direction is equal to x length
(3)kind: Interpolation method selection. linear(piecewise linear interpolation), nearest(nearest neighbor interpolation), slinear(single spline), quadratic(quadratic splines), cubic(cubic spline)
(4)axis: Specifies the interpolation direction, the default is the last axis. That is if y is a two-dimensional array, then the interpolation direction is horizontal ( axis==1)
4.Return value: returns the interpolation function f(x)

Get the interpolation function based on the following sample data and draw it

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
# interpolation node
# The value corresponding to the interpolation node
# interpolation point
# Interpolation function, the first uses piecewise linear interpolation, the second uses cubic spline interpolation
# draw

2.interp2d function

1.grammar: scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=None)
2.Function: interpolate on a two-dimensional grid
3.Common parameters:
(1)x with y: Sample point coordinates x direction and y direction vector if x with y are multidimensional, then they are automatically flatten use later
(2)z: An array of interpolated point values, which will be compressed to one-dimensional before use
(3)kind: Interpolation method selection. The default is piecewise linear interpolation l(inear),Optional cubic splines ( cubic)or quintic spline interpolation ( quintic)
4.Return value: returns the interpolation function f(x,y)

Calculate the approximate value of the regional surface area based on the following elevation data, obtain the interpolation function and draw the regional contour line and three-dimensional surface map.

import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg import norm #The role of the norm function is to find the matrix norm
from scipy.interpolate import interp2d
import warnings
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\Dengb.ttf")
# The value corresponding to the interpolation node
# interpolation node
#Use bicubic spline interpolation to get the interpolation function f

# Set the subpoint xi=10i (i=0,1...140), yi=10j (j=0,1,...,120) the plane is divided into 140*120 small rectangles by this subpoint
# Interpolation of small rectangle vertices
# Calculation of area; the surface area corresponding to a small rectangle can be approximated as a quadrilateral in space. The area of ​​this quadrilateral can be regarded as the sum of the areas of the two triangles
# The triangle area is considered to be solved by Heron's formula: p=(a+b+c)/2 s=sqrt(p*(p-a)*(p-b)*(p-c))
for i in range(len(xi)-1):
    for j in range(len(yj)-1):
print('area{}cubic meter'.format(s[0]))

# Use plt.contour to draw contour line (contour line)
# Use the plt.clabel function to add labels to contour lines
plt.title('contour line')
# For a 3D surface plot, the subplot projection type is set to 3d. default 2d
# full image title

Regional area: 4880683.019677971 cubic meters

3.griddata function

1.grammar: scipy.interpolate.griddata(points, values, xi, method='linear', fill_value=nan, rescale=False)
2.Function: Do interpolation of non-grid interpolation node data
3.Common parameters:
(1)points: It is generally convenient to pass a tuple of data point coordinate vectors. Such as x is the interpolation node x a vector of values ​​for the direction, y is the interpolation node y vector of orientation values. So pass (x,y)
(2)values: is a vector of floating-point or complex numbers corresponding to the interpolation points
(3)xi: This parameter is the interpolation point. pass np.meshgrid function processing x,y The grid matrix is ​​obtained after interpolating the point vector in the direction and then passed as a tuple to xi More convenient
(4)kind: interpolation method. optional nearest,linear,cubic
4.Return value: return the interpolation point directly xi Corresponding interpolation array

Draw seabed topographic maps and contour maps based on water depth data

import  numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
# Interpolate node vector data
# Interpolation point x-direction and y-direction vector
# Construct grid nodes using vectors xn and yn
# Use the griddata function to interpolate values ​​at grid nodes based on unstructured data
zn1=griddata((x,y),z,(xng,yng),method='nearest') # nearest neighbor interpolation
# draw 3D image

3. Fitting function

1.polyfit function

1.grammar: numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)
2.Function: Least squares polynomial fitting, it seems that it can only do one-dimensional fitting
3.Common parameters:
(1)x: sample point x coordinate vector
(2)y: sample point y Coordinate vector or a two-dimensional array, each column represents the sample y coordinate vector, the columns share x coordinate vector
(3)deg: polynomial degree
4.Return Value: A vector of coefficients of the polynomial or a 2D array of the vectors of coefficients of the polynomial as columns

According to the sample data, a polynomial fitting curve is obtained and x = 0.25 with 0.35 x=0.25 and 0.35 Values ​​at x=0.25 and 0.35

import numpy as np
import matplotlib.pyplot as plt
# sample
# Use the polyfit function to fit the coefficients of the quadratic polynomial
# Use the polyval function to bring specified values ​​into polynomial calculations
print('x=0.25 predicted value of{:.2}  x=0.35 predicted value of{:.2}'.format(yhat[0],yhat[1]))
# plot the curve of the fitted function

2.curve_fit function

1.grammar: scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(- inf, inf), method=None, jac=None, *, full_output=False, **kwargs)
2.Function: Use nonlinear least square method to fit a function f
3.Common parameters:
(1)f: callable function f(x,...). He must treat the independent variables as f The first parameter of , the parameters in the function model are respectively as f the rest of the parameters. x can be viewed as a scalar or a vector, so we can define multidimensional functions
(2)xdata: sample point, if f is a one-dimensional function then xdata is a vector; if f yes k dimension function, x that is shape[0]==k array of . For example, my sample point is ( x,y,z),then my xdata can be written as np.vstack((x,y))
(3)ydata: A vector of function values ​​corresponding to the sample points
4.return value:
(1)popt: Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized.
(2)pcov: The estimated covariance of popt.

Fit function to sample points z = a e b x + c y 2 z=ae^{bx}+cy^{2} z=aebx+cy2

from scipy.optimize import curve_fit
import numpy as np
# Sample point data
# Fit function model 
def fun(var,a,b,c):
    return a*np.exp(b*var[0])+c*var[1]**2
# Curve fitting
print("The parameters of the fitted function model are:{}".format(popt))

4. Drawing function

1.contour function

1.grammar: matplotlib.pyplot.contour(*args, data=None, **kwargs)
2.Function: draw contour line(contour line)
3.Common parameters:
(1)x: x vector of orientation values, satisfying len(x)==z.shape[0]
(2)y: y direction-worthy vector, satisfying len(y)==z.shape[1]
(3)z: The height value of the contour line, which is a two-dimensional array
(4)cmap:colormap can have cmap control, optional
(5)alpha: opacity, 0~1 between

2.plot_surface function

1.grammar: plot_surface(X, Y, Z, *, norm=None, vmin=None, vmax=None, lightsource=None, **kwargs)
2.Function: draw three-dimensional surface
3.Common parameters:
(1)X,Y,Z: A 2D array of sample points. pass np.meshgrid function can put x vector sum y Vectorize to 2D array X,Y

Tags: Python Mathematical Modeling numpy

Posted by russy on Fri, 30 Dec 2022 07:58:35 +0300