Derivatives are made in python.

I am trying to find higher order derivatives of a dataset (x, y). x and y are 1D arrays of length N.

Let's say I generate them as:

xder0=np.linspace(0,10,1000) yder0=np.sin(xder0) 

I define a derivative function that takes in 2 arrays (x, y) and returns (x1, y1), where y1 is the derivative calculated for each index as: (y [i + 1] -y [i]) / (x [g + 1] -x [I]). x1 is just the average of x [i + 1] and x [i]

Here is the function that does this:

 def deriv(x,y): delx =np.zeros((len(x)-1), dtype=np.longdouble) ydiff=np.zeros((len(x)-1), dtype=np.longdouble) for i in range(len(x)-1): delx[i] =(x[i+1]+x[i])/2.0 ydiff[i] =(y[i+1]-y[i])/(x[i+1]-x[i]) return delx, ydiff 

Now, to calculate the first derivative, I call this function as:

 xder1, yder1 = deriv(xder0, yder0) 

Similarly, for the second derivative, I call this function, giving the first derivatives as input:

 xder2, yder2 = deriv(xder1, yder1) 

And he continues:

 xder3, yder3 = deriv(xder2, yder2) xder4, yder4 = deriv(xder3, yder3) xder5, yder5 = deriv(xder4, yder4) xder6, yder6 = deriv(xder5, yder5) xder7, yder7 = deriv(xder6, yder6) xder8, yder8 = deriv(xder7, yder7) xder9, yder9 = deriv(xder8, yder8) 

Something special happens after I reach order 7. The 7th order becomes very noisy! Early derivatives are all either sinusoidal or related functions, as expected. However, the seventh order is a noisy sine. And, therefore, all derivatives after this explode.

The plot of derivatives up to 7th order

Any idea what is going on?

+5
source share
1 answer

This is a well-known stability problem with numerical interpolation using equidistant points . Read the answers at http://math.stackexchange.com .

To overcome this problem, you must use unevenly spaced points , such as the roots of the Lagranra polynomial . The instability arises due to the inaccessibility of information at the boundaries, therefore, a large concentration of points at the boundaries along the roots is required, for example, Lamendra polynomials or others with similar properties, such as the Chebyshev polynomial.

+2
source

Source: https://habr.com/ru/post/1237312/


All Articles