Data Interpolation, Curve Fitting, and Numerical Differentiation with MATLAB

1. Data interpolation

  • In engineering measurement and scientific experiments, the obtained data are usually discrete. If you want to get the values ​​of other points other than these discrete points, you need to interpolate based on these known data. For example, measuring nnThe data of n points are( x 1 , y 1 ), ( x 2 , y 2 ) , … , ( xn , yn ) (x_{1},y_{1}), (x_{2},y_{2 }), \dots, (x_{n},y_{n})(x1,y1)(x2,y2)(xn,yn) , these data points reflect a functional relationshipy = f ( x ) y=f(x)y=f ( x ) , however does not knowf ( x ) f(x)The analytical formula of f ( x ) .
  • The task of data interpolation is to construct a function y = g ( x ) y=g(x) according to the above conditionsy=g ( x ) , availablex ( i = 1 , 2 , . . . n ) x(i=1, 2, ... n)x(i=1,2,... n ) , there isg ( x ) = f ( x ) g(x)=f(x)g(x)=f ( x ) , and at two adjacent sampling points( xi , xi + 1 ) ( i = 1 , 2 , … , n − 1 ) (x_{i},x_{i+1})(i= 1,2,\dots ,n-1)(xi,xi+1)(i=1,2,,n1 ) ,g ( x ) g(x)g ( x ) smooth transition. If the interpolated functionf ( x ) f(x)f ( x ) is smooth, and the sampling points are dense enough, generally within the sampling interval,f ( x ) f(x)f ( x ) andg ( x ) g(x)g ( x ) is relatively close. Interpolation functiong ( x ) g(x)g ( x ) is generally acted by a linear function, a polynomial, a spline function, or a piecewise function of these functions.
  • According to the number of independent variables of the interpolated function, the interpolation problem is divided into one-dimensional interpolation, two-dimensional interpolation and multi-dimensional interpolation, etc.; according to the selected interpolation method, the interpolation problem is further divided into linear interpolation, polynomial interpolation and spline interpolation. MATLAB provides one-dimensional, two-dimensional, NNN- dimensional data interpolation functionsinterp1,interp2andinterpn, and 3 Hermite interpolation functions pchipandspline, etc.

1. One-dimensional data interpolation

  • If the interpolated function is a univariate function, the data interpolation problem is called one-dimensional interpolation. The methods used for one-dimensional interpolation include linear interpolation, closest point interpolation, 3rd Hermite interpolation and 3rd spline interpolation. In MATLAB, the function that implements these interpolations is interp1, and its calling format is as follows:
    Y1=interp1(X,Y,X1,method)
  • Function according to X , YX , YThe value of X and Y , the calculation function is inX 1 X1The value at X1 . Among them,X, YX, YX and Y are two known vectors of equal length, describing the sampling point and sampling value respectively. If the same sampling point has multiple sampling values, thenYYY can be a matrix,YYEach column of Y corresponds to a set of samples. X1 X1X 1 is a vector or scalar, describing the point to be interpolated,Y 1 Y1Y1 is the one with X1 and X1Interpolation result of equal length X 1 . methodIt is used to specify the interpolation method, and the allowed values ​​are as follows.
  • (1) 'linear': Linear interpolation. Linear interpolation is the default interpolation method, which connects two data points close to the interpolation point with a straight line, and then selects the data corresponding to the interpolation point on the straight line.
  • (2) 'nearest': Nearest point interpolation. Interpolate based on how far the interpolated point is from known data points. Interpolation points give priority to selecting closer data points for interpolation.
  • (3) 'pchip': Hermitian interpolation in three stages. The piecewise cubic Hermitian interpolation adopts a piecewise cubic polynomial. In addition to satisfying the interpolation conditions, the first-order derivatives at several nodes must also be equal, thereby improving the smoothness of the interpolation function. There is a special Hermite interpolation function of degree 3 in MATLAB pchip(X,Y,X1), its function and usage interpl(X,Y,X,'pchip')are the same .
  • (4) 'spline': Cubic spline interpolation. The so-called 3-degree spline interpolation refers to constructing a 3-degree polynomial in each segment (subinterval), so that its interpolation function not only meets the interpolation conditions, but also requires continuous first-order and second-order derivatives at each node , thus ensuring smoothness at the nodes. There is a special cubic spline interpolation function in MATLAB spline(X,Y,X1), its function and usage interp1(X,Y,Xl,'spline')are the same .
  • Note: The "linear" and "nearest" interpolation methods will give "NaN" errors for interpolation points outside the X range. For other methods, the extrapolation algorithm will be performed on out-of-range interpolation points.
  • For example, the data for the following probability integral is shown in the table below, we use different interpolation methods to calculate f ( 0.472 ) f(0.472)f(0.472) f ( x ) = 2 π ∫ 0 x e − x 2 d x f(x)=\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-x^{2}}\mathrm{d}x f(x)=Pi 20xex2dx
x x x f ( x ) f(x) f(x)
0.46 0.4846555
0.47 0.4937542
0.48 0.5027498
0.49 0.5116683
  • This is a one-dimensional interpolation problem, the procedure is as follows:
>> x=0.46:0.01:0.49;  %给出x
>> f=[0.4846555,0.4937542,0.5027498,0.5116683];  %给出f(x)
>> format long  %显示小数点后16>> interp1(x,f,0.472)  %用默认方法,即线性插值计算f(0.472)

ans =

   0.495553320000000

>>  interp1(x,f,0.472,'nearest')  %用最近点插值计算f(0.472)

ans =

   0.493754200000000

>> interp1(x,f,0.472,'pchip')  %3次埃尔米特计算f(0.472)

ans =

   0.495561119712056

>> interp1(x,f,0.472,'spline')  %3次样条插值计算f(0.472)

ans =

   0.495560736000000

>> format short  %小数点后显示4
  • In the example, the interpolation results of 3 Hermite and 3 spline interpolation are better than the closest point interpolation method and linear interpolation method, but the quality of the interpolation method also depends on the interpolated function, and no one is the same for all functions The best interpolation method.
  • For example, a detection parameter fff over timettThe sampling results of t are shown in the table below. We use the data interpolation method to calculatet = 2 , 12 , 22 , 32 , 42 , 52 t=2,12,22,32,42,52t=2,12,22,32,42,ffat 52f value.
t t t f f f t tt f ff
0 3.1025 5 2.256
10 879.5 15 2968.8
20 2968.8 25 4136.2
30 5237.9 35 6152.7
40 6725.3 45 6848.3
50 6403.5 55 6824.7
60 7328.5 65 7857.6
  • This is a one-dimensional data interpolation problem, the procedure is as follows:
>> T=0:5:65;  %给出T
>> X=2:10:52;  %给出X
>> F=[3.2015,2.2560,879.5,1835.9,2968.8,4136.2,5237.9,6152.7,...
    6725.3,6848.3,6403.5,6824.7,7328.5,7857.6];  %给出F(x)
>> F1=interp1(T,F,X)  %用线性插值方法插值

F1 =

   1.0e+03 *
15

   0.002823300000000   1.262060000000000   3.435760000000000   5.603820000000000   6.774500000000000
6

   6.571980000000000

>> F2=interp1(T,F,X,'nearest')  %用最近点插值法插值


F2 =

   1.0e+03 *
15

   0.003201500000000   0.879500000000000   2.968800000000000   5.237900000000000   6.725300000000000
6

   6.403500000000000

>> F3=interp1(T,F,X,'pchip')  %3次埃尔米特插值方法插值

F3 =

   1.0e+03 *
15

   0.002460228000000   1.248358437730487   3.436483654858926   5.636234122067274   6.797756124209316
6

   6.507716445924324

>> F4=interp1(T,F,X,'spline')  %3次样条插值方法插值

F4 =

   1.0e+03 *
15

  -0.170219570210629   1.256002190473428   3.439603968838626   5.637043773267335   6.859291256904060
6

   6.481654623389509

  • Here, 16 digits are displayed after the decimal point because the format long code was used last time, but the format short code was not executed.

2. Two-dimensional data interpolation

  • When the function depends on the change of two independent variables, its sampling point should be a plane area composed of these two parameters, and the interpolation function is also a two-dimensional function. The problem of interpolating a function that depends on two parameters is called a two-dimensional interpolation problem.
  • Similarly, in MATLAB, a function to solve the two-dimensional interpolation problem is provided interp2, and its calling format is as follows:
    Z1=interp2(X,Y,Z,X1,Y1,method)
  • Among them, X , YX, YX and Y are two vectors, respectively describing the sampling points of the two parameters,ZZZ is the function value corresponding to the parameter sampling point,X 1 , Y 1 X1, Y1X 1 and Y 1 are two vectors or scalars, describing the points to be interpolated. Z 1 Z1Z 1 is the interpolation result obtained according to the corresponding interpolation method.
  • The value of method is the same as the one-dimensional interpolation function, but two-dimensional interpolation does not support 'pchip'the method . X, Y, ZX, Y, ZX , Y , Z can also be in matrix form. Similarly, for beyondX, YX, YInterpolation points 'linear'in X , Y'nearest' range, using and interpolation methods, will give NaN error. For 'spline'the method
  • For example, let z = x 2 + y 2 z=x^{2}+y^{2}z=x2+y2 , we havezzz function in[ 0 , 1 ] × [ 0 , 2 ] [0, 1] × [0, 2][01]×[ 0 , 2 ] area for interpolation.
  • The procedure is as follows:
>> x=0:0.1:1;
>> y=0:0.2:2;
>> [X,Y]=meshgrid(x,y);  %产生自变量网络坐标
>> Z=X.^2+Y.^2;  %求对应的函数值
>> interp2(x,y,Z,0.5,0.5)  %(0.5.0.5)点插值

ans =

    0.5100

>> interp2(x,y,Z,[0.5,0.6],0.4)  %(0.5.0.4)点和(0.6,0.4)点插值

ans =

    0.4100    0.5200

>> interp2(x,y,Z,[0.5,0.6],[0.4,0.5])  %(0.5.0.4)点和(0.6,0.5)点插值

ans =

    0.4100    0.6200

%(0.5.0.4),(0.6.0.4),(0.5.0.5)(0.6,0.5)各点插值
>> interp2(x,y,Z,[0.5 0.6]',[0.4 0.5])  

ans =

    0.4100    0.5200
    0.5100    0.6200

  • If you want to improve the interpolation accuracy, you can choose the interpolation method as 'spline', for this example, the accuracy can be improved. Enter the following program:
>> interp2(x,y,Z,[0.5 0.6]',[0.4 0.5],'spline')

ans =

    0.4100    0.5200
    0.5000    0.6100

  • For example, an experiment tests the temperature propagation of a heat source on a steel rail with a length of 10 m. use ddd represents the distance of the measuring point (m), usettt represents the measurement time (s), useccc represents the measured temperature (°C) of each point, and the measurement results are shown in the table below. We use linear interpolation to obtain the temperature at every 20 s and every 2 m of the rail within one minute.
d d d=0 d dd=2.5 d dd=5 d dd=7.5 d dd=10
t t t=0 95 14 0 0 0
t tt=30 88 48 32 12 6
t tt=60 67 64 54 48 41
  • The procedure is as follows:
>> d=0:2.5:10;
>> t=(0:30:60)';
>> c=[95,14,0,0,0;88,48,32,12,6;67,64,54,48,41];
>> di=0:2:10;
>> ti=(0:20:60)';
>> ci=interp2(d,t,c,di,ti)
  • The result of the program running is as follows:
ci =

   95.0000   30.2000    5.6000         0         0         0
   90.3333   47.4000   27.4667   16.0000    7.2000    4.0000
   81.0000   58.8667   44.9333   33.2000   22.7333   17.6667
   67.0000   64.6000   58.0000   51.6000   46.6000   41.0000

  • The figure below is based on the interpolation results [ di , ti , ci ] [d_{i},t_{i},c_{i}][di,ti,ci] , the rail temperature distribution map surf(di,ti,ci)drawnThe distribution plotted is more ideal if the interpolation points are densified.

insert image description here

2. Curve Fitting

1. Curve Fitting Principle

  • Similar to data interpolation, the purpose of curve fitting is to use a simpler function to approximate a complex or unknown function, based on the condition of function values ​​of a limited number of sampling points in an interval or an area.
  • Data interpolation requires that the approximating function is equal to the approximated function at the sampling point, but due to errors in experiments or measurements, the obtained data may not be accurate. In this case, it is obviously unreasonable to force the approximation function to pass through each sampling interpolation point.
  • For this, the constructor y = g ( x ) y=g(x)y=g ( x ) to approximatef ( x ) f(x)f ( x ) , the curve g ( x ) g(x)is not required hereg ( x ) strictly passes through the sampling points, but it is expected thatg ( x ) g(x)g ( x ) can be as close as possible to these points, that is to make the errorδ i = g ( x ) − f ( x ) ( i = 1 , 2 , … , n ) δ_{i}=g(x)-f(x ) (i=1, 2,...,n)di=g(x)f(x)(i=1,2, n ) reach the minimum in a certain sense.
  • The optimal standard of MATLAB curve fitting is to adopt the common least squares principle, and the constructed g ( x ) g(x)g ( x ) is a polynomial whose degree is less than the number of interpolation nodes.
  • We assume that the measured nn discrete points( xi , yi ) ( i = 1 , 2 , . . . n ) (x_{i}, y_{i})(i=1, 2,... n)(xi,yi)(i=1,2 , ... n ) , want to construct am ( m ≤ n ) m(m≤n)m(mn ) degree polynomialp ( x ) p(x)p(x) p ( x ) = a m x m + a m − 1 x m − 1 + … + a 1 x + a 0 p(x)=a_{m}x^{m}+a_{m-1}x^{m-1}+…+a_{1}x+a_{0} p(x)=amxm+am1xm1++a1x+a0
  • The so-called least square principle of curve fitting is to make the deviation p ( xi ) − yip(x_{i})-y_{i} of the above fitting polynomial at each nodep(xi)yisum of squares ∑ i = 1 n ( p ( xi ) − yi ) 2 \sum_{i=1}^{n}(p(x_{i})-y_{i})^{2}i=1n(p(xi)yi)2 to a minimum. It has been proved mathematically that the solution to the above least squares approximation problem is always deterministic.

2. Realization of curve fitting

  • When using the least squares method for curve fitting, it is actually seeking a coefficient vector, which is a coefficient of a polynomial.
  • In MATLAB, use polyfitthe function to find the coefficients of the least square fitting polynomial, and then polyvaluse the function to calculate the approximate value of the function on the given point according to the obtained polynomial.
  • polyfitThe function call format is as follows:
    p=polyfit(X,Y,m)
    [P,S]=polyfit(X,Y,m)
    [P,S,mu]=polyfit(X,Y,m)
  • The function is based on the sampling point XXX and sample point function valueYYY , yielding ammm degree polynomialPPP and its error vector SSat the sampling pointS
  • Among them , X , YX, YX and Y are two vectors of equal length,PPP is a lengthm + 1 m+1m+A vector of 1 ,PPThe elements of P are polynomial coefficients. mu is a binary vector, mu(1) is a function mean(X)FindXXThe average value of X , and mu(2) is a function std(X)to findXXThe variance of X.
  • You can use polyvalthe function to calculate XX from the resulting polynomialThe value of the polynomial at each point of X.
  • For example, we use a polynomial of degree 3 in the interval [ 0 , 2 π ] [0,2\pi][0,2 π ] internal approximation functionsin ⁡ x \sin xsinx
  • On a given interval, we uniformly select 50 sampling points, and calculate the function value of the sampling points, and then use 3-degree polynomial approximation.
  • The procedure is as follows:
>> X=linspace(0,2*pi,50);  %02Π之间等差的选取50个点
>> Y=sin(X);
>> P=polyfit(X,Y,3)  %得到3次多项式的系数和误差

P =

    0.0912   -0.8596    1.8527   -0.1649

  • The 3-degree fitting polynomial p ( x ) p(x) is obtained aboveCoefficient of p ( x ) , get p ( x ) = 0.0912 x 3 − 0.8596 x 2 + 1.8527 x − 0.1649 p(x)=0.0912x^{3}-0.8596x^{2}+1.8527x-0.1649p(x)=0.0912x _30.8596 x2+1.8527 x0.1649
  • Next, we use the method of plotting function to convert the polynomial p ( x ) p(x)p ( x ) andsin ⁡ x \sin xsinx for comparison, the procedure is as follows:
>> X=linspace(0,2*pi,50);
>> Y=sin(X);
>> P=polyfit(X,Y,3)

P =

    0.0912   -0.8596    1.8527   -0.1649

>> Y1=polyval(P,X);
>> plot(X,Y,':o',X,Y1,'-*')

  • plot sin ⁡ x \sin xsinx and the polynomialp ( x ) p(x)The function curve of p ( x ) in a given interval is shown in the figure below. where the dashed line issin ⁡ x \sin xsinx , the solid line isp ( x ) p(x)p ( x ) . function polyval(P,X)returnsp ( x ) p(x)p ( x ) gets the value.

insert image description here

3. Numerical differentiation

  • In general, the derivative of a function is still a function. Let the function f ( x ) f(x)The derivative function of f ( x ) f ′ ( x ) = g ( x ) f'(x)=g(x)f(x)=g ( x ) , advanced mathematics is concerned withg ( x ) g(x)The form and properties of g ( x ) , and the problem of numerical analysis is how to calculateg ( x ) g(x)g ( x ) in a series of discrete pointsX = ( x 1 , x 2 , … , xn ) X=(x_{1}, x_{2}, …, x_{n})X=(x1,x2xn) approximationG = ( g 1 , g 2 , … , gn ) G=(g_{1}, g_{2}, …, g_{n})G=(g1g2gn) and how much error the calculated approximation has.

1. Numerical difference and difference quotient

  • Any function f ( x ) f(x)f ( x ) atxxThe derivative at point x is defined by the limit: f ′ ( x ) = lim ⁡ h → 0 f ( x + h ) − f ( x ) h f'(x)=\lim_{h \to 0}\frac{ f(x+h)-f(x)}{h}f(x)=h0limhf(x+h)f(x) f ′ ( x ) = lim ⁡ h → 0 f ( x ) − f ( x − h ) h f'(x)=\lim_{h \to 0}\frac{f(x)-f(x-h)}{h} f(x)=h0limhf(x)f(xh) f ′ ( x ) = lim ⁡ h → 0 f ( x + h / 2 ) − f ( x − h / 2 ) h f'(x)=\lim_{h \to 0}\frac{f(x+h/2)-f(x-h/2)}{h} f(x)=h0limhf(x+h/2)f(xh/2)
  • In the above formulas, it is assumed that h > 0 h>0h>0 , if the h ⟶ 0 h\longrightarrow 0on the right side of the above equation is removedh0 , and introduce the notation: △ f ( x ) = f ( x + h ) − f ( x ) \bigtriangleup f(x)=f(x+h)-f(x)f(x)=f(x+h)f(x) ▽ f ( x ) = f ( x ) − f ( x − h ) \bigtriangledown f(x)=f(x)-f(x-h) f(x)=f(x)f(xh) δ f ( x ) = f ( x + h / 2 ) − f ( x − h / 2 ) \delta f(x)=f(x+h/2)-f(x-h/2) δf(x)=f(x+h/2)f(xh/2)
  • △ f ( x ) 、 ▽ f ( x ) \bigtriangleup f(x)、\bigtriangledown f(x) f(x)f(x) δ f ( x ) \delta f(x) δ f ( x ) respectively in the functionxxAt point x with h ( h > 0 ) h(h>0)h(h>0 ) is the forward difference, backward difference and central difference of the step size. When the step size ishhWhen h is sufficiently small, f ′ ( x ) ≈ △ f ( x ) h f'(x)\approx \frac{\bigtriangleup f(x)}{h}f(x)hf(x) f ′ ( x ) ≈ ▽ f ( x ) h f'(x)\approx \frac{\bigtriangledown f(x)}{h} f(x)hf(x) f ′ ( x ) ≈ δ f ( x ) h f'(x)\approx \frac{\delta f(x)}{h} f(x)hδf(x)
  • Like the difference, say △ f ( x ) / h , ▽ f ( x ) / h \bigtriangleup f(x)/h, \bigtriangledown f(x)/hf(x)/hf(x)/h δ f ( x ) / h \delta f(x)/h δ f ( x ) / h are respectively the functions atxxAt point x with h ( h > 0 ) h(h>0)h(h>0 ) is the forward difference quotient, backward difference quotient and center difference quotient of the step length.
  • When the step size h ( h > 0 ) h(h>0)h(h>0 ) sufficiently small, the functionfff at pointxxThe differential of x is close to any kind of differential of the function at that point, andfff at pointxxThe derivative of x is close to any kind of difference quotient of the function at that point.

2. Realization of numerical differentiation

  • There are two ways to compute an arbitrary function f ( x ) f(x)f ( x ) at a given pointxxThe numerical derivative of x , the first way is to use polynomial or spline functiong ( x ) g(x)g ( x ) versusf ( x ) f(x)f ( x ) for approximation (interpolation or fitting), and then use the approximation functiong ( x ) g(x)g ( x ) at pointxxDerivative at x as f ( x ) f(x)f ( x ) at pointxxDerivative at x , the second way is to use f ( x ) f(x)f ( x ) at pointxxSome kind of difference quotient at x as its derivative.
  • In MATLAB, there is no function to calculate the numerical derivative directly, only the function to calculate the forward difference diff, and the calling format is as follows.
  • (1) DX=dif(X): Calculate the vector XXX forward difference,DX ( i ) = X ( i + 1 ) − X ( i ) , i = 1 , 2 , … , n − 1 DX(i)=X(i+1)-X(i) , i=1, 2, …, n-1DX(i)=X(i+1)X(i)i=12n1
  • (2) DX= diff(X,n): Calculate XXX 'snnNth order forward difference. For example,diff(X,2)=diff(diff(X)).
  • (3) DX-dif(A,n,dim): Calculation matrix AAA 'snnnth- order difference, when dim=1 (default state), the difference is calculated by column; when dim=2, the difference is calculated by row.
  • For example, we set xxby x [ 0 , 2 π ] [0, 2\pi][0,2 π ] composed of 6 uniformly distributed points, findsin ⁡ x \sin xsinThe 1~3 order difference of x .
  • The procedure is as follows:
>> X=linspace(0,2*pi,6);
>> Y=sin(X)

Y =

         0    0.9511    0.5878   -0.5878   -0.9511   -0.0000

>> DY=diff(Y)  %计算Y的一阶差分

DY =

    0.9511   -0.3633   -1.1756   -0.3633    0.9511

>> D2Y=diff(Y,2)  %计算Y的二阶差分,也可以用命令diff(DY)计算

D2Y =

   -1.3143   -0.8123    0.8123    1.3143

>> D3Y=diff(Y,3)  %计算Y的三阶差分,也可以用命令diff(D2Y)diff(DY,2)计算

D3Y =

    0.5020    1.6246    0.5020

  • For example, let f ( x ) = x 3 + 2 x 2 − x + 12 + x + 5 6 + 5 x + 2 f(x)=\sqrt{x^{3}+2x^{2}-x+ 12}+\sqrt[6]{x+5}+5x+2f(x)=x3+2x _2x+12 +6x+5 +5 x+2 We use different methods to find the functionf ( x ) f(x)The numerical derivative of f ( x ) and plot f ′ ( x ) f'(x)in a coordinate systemf (x)image.
  • To determine the point at which to calculate the numerical derivative, assume that in [ − 3 , 3 ] [-3, 3][3,3 ] Calculate the numerical derivative with a step size of 0.01 in the interval. The following three methods are used to findf ( x ) f(x)The derivative of f ( x ) at these points.
  • First use a polynomial of degree 5 p ( x ) p(x)p ( x ) fitting functionf ( x ) f(x)f ( x ) , and forp ( x ) p(x)p ( x ) seeks the derivative in the general sensedp ( x ) dp(x)d p ​​( x ) , finddp ( x ) dp(x)The value of d p ( x ) at the hypothetical point.
  • The second method directly finds f ( x ) f(x)Numerical derivative of f ( x ) at hypothetical points.
  • The third method to find f ′ ( x ) f'(x)f(x) f ′ ( x ) = 3 x 2 + 4 x − 1 2 x 3 + 2 x 2 − x + 12 + 1 6 x + 5 6 + 5 f'(x)=\frac{3x^{2}+4x-1}{2\sqrt{x^{3}+2x^{2}-x+12}}+\frac{1}{6\sqrt[6]{x+5}}+5 f(x)=2x3+2x _2x+12 3x _2+4x _1+66x+5 1+5
  • Then directly find f ′ ( x ) f'(x)f (x)at the hypothetical point, and finally display the 3 curves on the same axis.
  • The procedure is as follows:
>> f=@(x) sqrt(x.^3+2*x.^2-x+12)+(x+5).^(1/6)+5*x+2;
>> g=@(x) (3*x.^2+4*x-1)./sqrt(x.^3+2*x.^2-x+12)/2+1/6./(x+5).^(5/6)+5;
>> x=-3:0.01:3;
>> p=polyfit(x,f(x),5);				%5次多项式p拟合f(x)
>> dp=polyder(p);					%对拟合多项式p求导数dp
>> dpx=polyval(dp,x);				%求dp在假设点的函数值
>> dx=diff(f([x,3.01]))/0.01;		%直接对f(x)求函数导数
>> gx=g(x);							%求函数f的导函数g在假设点的导数
>> plot(x,dpx,x,dx,'.',x,gx,'-');	%作图
  • The program running result is shown in the figure below. The results show that the numerical derivatives obtained by the three methods are relatively close.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45891612/article/details/131032590