Here is a second order approximation to du/dx which should be somewhat more accurate than a first order approximation, and if u is a quadratic function of x, then du/dx is exact (except of course for round-off errors.) Also note that there are as many points in the derivative array as in u and x. I assume in this code that u and x are row vectors of the same length. Intervals in x are not required to be equal. xd = diff([x(3),x,x(n-2)]); % <-- Corrected ud = diff([u(3),u,u(n-2)]); % <-- Corrected dudx = (ud(1:end-1)./xd(1:end-1).*xd(2:end) ... + ud(2:end)./xd(2:end).*xd(1:end-1)) ... ./ (xd(2:end)+xd(1:end-1)); Answer from Roger Stafford on mathworks.com
🌐
MathWorks
mathworks.com › matlabcentral › answers › 398306-how-to-find-a-differentiation-or-derivative-of-each-array-1000-2-in-cell-consists-7000-1
how to find a differentiation or derivative of each array(>1000*2) in cell consists 7000*1? - MATLAB Answers - MATLAB Central
April 30, 2018 - Input #2 expected to be a cell array, was double instead. and also i have written y2-y1/(x2-x1 formula to find the derivative of each point to point. if i write like that, then my code will very long for only finiding derivative!! ... https://www.mathworks.com/matlabcentral/answers/398306-how-to-find-a-differentiation-or-derivative-of-each-array-1000-2-in-cell-consists-7000-1#comment_563006
🌐
MathWorks
mathworks.com › matlabcentral › answers › 65503-approximate-derivative-of-array
approximate derivative of array - MATLAB Answers - MATLAB Central
March 1, 2013 - In the middle of it there has to be one peak in the derivative. I checked and it was not there, otherwise yes the noise is not higher than +-2. I tried to convert to double first and that works fine. Thanks! Sign in to comment. Sign in to answer this question. Find more on Graph and Network Algorithms in Help Center and File Exchange ... Find the treasures in MATLAB Central and discover how the community can help you!
🌐
MathWorks
mathworks.com › symbolic math toolbox › mathematics › calculus
Differentiation - MATLAB & Simulink
To find the derivative of g for a given value of x, substitute x for the value using subs and return a numerical value using vpa.
Top answer
1 of 2
21

These are just some quick-and-dirty suggestions. Hopefully somebody will find them helpful!

1. Do you have a symbolic function or a set of points?

  • If you have a symbolic function, you may be able to calculate the derivative analytically. (Chances are, you would have done this if it were that easy, and you would not be here looking for alternatives.)
  • If you have a symbolic function and cannot calculate the derivative analytically, you can always evaluate the function on a set of points, and use some other method listed on this page to evaluate the derivative.
  • In most cases, you have a set of points (xi,fi), and will have to use one of the following methods....

2. Is your grid evenly or unevenly spaced?

  • If your grid is evenly spaced, you probably will want to use a finite difference scheme (see either of the Wikipedia articles here or here), unless you are using periodic boundary conditions (see below). Here is a decent introduction to finite difference methods in the context of solving ordinary differential equations on a grid (see especially slides 9-14). These methods are generally computationally efficient, simple to implement, and the error of the method can be simply estimated as the truncation error of the Taylor expansions used to derive it.
  • If your grid is unevenly spaced, you can still use a finite difference scheme, but the expressions are more difficult and the accuracy varies very strongly with how uniform your grid is. If your grid is very non-uniform, you will probably need to use large stencil sizes (more neighboring points) to calculate the derivative at a given point. People often construct an interpolating polynomial (often the Lagrange polynomial) and differentiate that polynomial to compute the derivative. See for instance, this StackExchange question. It is often difficult to estimate the error using these methods (although some have attempted to do so: here and here). Fornberg's method is often very useful in these cases....
  • Care must be taken at the boundaries of your domain because the stencil often involves points that are outside the domain. Some people introduce "ghost points" or combine boundary conditions with derivatives of different orders to eliminate these "ghost points" and simplify the stencil. Another approach is to use right- or left-sided finite difference methods.
  • Here's an excellent "cheat sheet" of finite difference methods, including centered, right- and left-sided schemes of low orders. I keep a printout of this near my workstation because I find it so useful.

3. Is your domain periodic? Can you assume periodic boundary conditions?

  • If your domain is periodic, you can compute derivatives to a very high order accuracy using Fourier spectral methods. This technique sacrifices performance somewhat to gain high accuracy. In fact, if you are using N points, your estimate of the derivative is approximately N^th order accurate. For more information, see (for example) this WikiBook.
  • Fourier methods often use the Fast Fourier Transform (FFT) algorithm to achieve roughly O(N log(N)) performance, rather than the O(N^2) algorithm that a naively-implemented discrete Fourier transform (DFT) might employ.
  • If your function and domain are not periodic, you should not use the Fourier spectral method. If you attempt to use it with a function that is not periodic, you will get large errors and undesirable "ringing" phenomena.
  • Computing derivatives of any order requires 1) a transform from grid-space to spectral space (O(N log(N))), 2) multiplication of the Fourier coefficients by their spectral wavenumbers (O(N)), and 2) an inverse transform from spectral space to grid space (again O(N log(N))).
  • Care must be taken when multiplying the Fourier coefficients by their spectral wavenumbers. Every implementation of the FFT algorithm seems to have its own ordering of the spectral modes and normalization parameters. See, for instance, the answer to this question on the Math StackExchange, for notes about doing this in MATLAB.

4. What level of accuracy are you looking for? Do you need to compute the derivatives within a given tolerance?

  • For many purposes, a 1st or 2nd order finite difference scheme may be sufficient. For higher precision, you can use higher order Taylor expansions, dropping higher-order terms.
  • If you need to compute the derivatives within a given tolerance, you may want to look around for a high-order scheme that has the error you need.
  • Often, the best way to reduce error is reducing the grid spacing in a finite difference scheme, but this is not always possible.
  • Be aware that higher-order finite difference schemes almost always require larger stencil sizes (more neighboring points). This can cause issues at the boundaries. (See the discussion above about ghost points.)

5. Does it matter to you that your derivative is evaluated on the same points as your function is defined?

  • MATLAB provides the diff function to compute differences between adjacent array elements. This can be used to calculate approximate derivatives via a first-order forward-differencing (or forward finite difference) scheme, but the estimates are low-order estimates. As described in MATLAB's documentation of diff (link), if you input an array of length N, it will return an array of length N-1. When you estimate derivatives using this method on N points, you will only have estimates of the derivative at N-1 points. (Note that this can be used on uneven grids, if they are sorted in ascending order.)
  • In most cases, we want the derivative evaluated at all points, which means we want to use something besides the diff method.

6. Do you need to calculate multiple orders of derivatives?

  • One can set up a system of equations in which the grid point function values and the 1st and 2nd order derivatives at these points all depend on each other. This can be found by combining Taylor expansions at neighboring points as usual, but keeping the derivative terms rather than cancelling them out, and linking them together with those of neighboring points. These equations can be solved via linear algebra to give not just the first derivative, but the second as well (or higher orders, if set up properly). I believe these are called combined finite difference schemes, and they are often used in conjunction with compact finite difference schemes, which will be discussed next.
  • Compact finite difference schemes (link). In these schemes, one sets up a design matrix and calculates the derivatives at all points simultaneously via a matrix solve. They are called "compact" because they are usually designed to require fewer stencil points than ordinary finite difference schemes of comparable accuracy. Because they involve a matrix equation that links all points together, certain compact finite difference schemes are said to have "spectral-like resolution" (e.g. Lele's 1992 paper--excellent!), meaning that they mimic spectral schemes by depending on all nodal values and, because of this, they maintain accuracy at all length scales. In contrast, typical finite difference methods are only locally accurate (the derivative at point #13, for example, ordinarily doesn't depend on the function value at point #200).
  • A current area of research is how best to solve for multiple derivatives in a compact stencil. The results of such research, combined, compact finite difference methods, are powerful and widely applicable, though many researchers tend to tune them for particular needs (performance, accuracy, stability, or a particular field of research such as fluid dynamics).

Ready-to-Go Routines

  • As described above, one can use the diff function (link to documentation) to compute rough derivatives between adjacent array elements.
  • MATLAB's gradient routine (link to documentation) is a great option for many purposes. It implements a second-order, central difference scheme. It has the advantages of computing derivatives in multiple dimensions and supporting arbitrary grid spacing. (Thanks to @thewaywewalk for pointing out this glaring omission!)

  • I used Fornberg's method (see above) to develop a small routine (nderiv_fornberg) to calculate finite differences in one dimension for arbitrary grid spacings. I find it easy to use. It uses sided stencils of 6 points at the boundaries and a centered, 5-point stencil in the interior. It is available at the MATLAB File Exchange here.

Conclusion

The field of numerical differentiation is very diverse. For each method listed above, there are many variants with their own set of advantages and disadvantages. This post is hardly a complete treatment of numerical differentiation.

Every application is different. Hopefully this post gives the interested reader an organized list of considerations and resources for choosing a method that suits their own needs.

This community wiki could be improved with code snippets and examples particular to MATLAB.

2 of 2
2

I believe there is more in to these particular questions. So I have elaborated on the subject further as follows:

(4) Q: What level of accuracy are you looking for? Do you need to compute the derivatives within a given tolerance?

A: The accuracy of numerical differentiation is subjective to the application of interest. Usually the way it works is, if you are using the ND in forward problem to approximate the derivatives to estimate features from signal of interest, then you should be aware of noise perturbations. Usually such artifacts contain high frequency components and by the definition of the differentiator, the noise effect will be amplified in the magnitude order of . So, increasing the accuracy of differentiator (increasing the polynomial accuracy) will no help at all. In this case you should be able to cancelt the effect of noise for differentiation. This can be done in casecade order: first smooth the signal, and then differentiate. But a better way of doing this is to use "Lowpass Differentiator". A good example of MATLAB library can be found here.

However, if this is not the case and you're using ND in inverse problems, such as solvign PDEs, then the global accuracy of differentiator is very important. Depending on what kind of bounady condition (BC) suits your problem, the design will be adapted accordingly. The rule of thump is to increase the numerical accuracy known is the fullband differentiator. You need to design a derivative matrix that takes care of suitable BC. You can find comprehensive solutions to such designs using the above link.

(5) Does it matter to you that your derivative is evaluated on the same points as your function is defined? A: Yes absolutely. The evaluation of the ND on the same grid points is called "centralized" and off the points "staggered" schemes. Note that using odd order of derivatives, centralized ND will deviate the accuracy of frequency response of the differentiator. Therefore, if you're using such design in inverse problems, this will perturb your approximation. Also, the opposite applies to the case of even order of differentiation utilized by staggered schemes. You can find comprehensive explanation on this subject using the link above.

(6) Do you need to calculate multiple orders of derivatives? This totally depends on your application at hand. You can refer to the same link I have provided and take care of multiple derivative designs.

🌐
MathWorks
mathworks.com › matlab › mathematics › numerical integration and differential equations › numerical integration and differentiation
diff - Differences and approximate derivatives - MATLAB
Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension whose size does not equal 1. ... diff(A,1,1) works on successive elements in the columns of A and returns a (p-1)-by-m difference matrix.
Find elsewhere
🌐
MathWorks
mathworks.com › matlabcentral › answers › 505999-grabbing-number-from-array-to-calculate-the-numerical-derivative
Grabbing number from array to calculate the numerical derivative - MATLAB Answers - MATLAB Central
February 18, 2020 - Hello, I ma looking for solution that will allow me to calculate a derative (in numerical way) from data from array. I have an array caled DataOutput it's defined as: DataOutput = zeros(length(p...
Top answer
1 of 1
1

diff is simply the difference between two adjacent elements in arr, which is exactly why you lose 1 element for using diff once. For example, 10 elements in an array only have 9 differences.

gradient and del2 are for derivatives. Of course, you can use diff to approximate derivative by dividing the difference by the steps. Usually the step is equally-spaced, but it does not have to be. This answers your question why x is not used in the calculation. I mean, it's okay that your x is not uniform-spaced.

So, why gradient gives us an array with the same length of the original array? It is clearly explained in the manual how the boundary is handled,

The gradient values along the edges of the matrix are calculated with single->sided differences, so that

G(:,1) = A(:,2) - A(:,1);

G(:,N) = A(:,N) - A(:,N-1);

Double-gradient and del2 are not necessarily the same, although they are highly correlated. It's all because how you calculate/approximate the 2nd-oder derivatives. The difference is, the former approximates the 2nd derivative by doing 1st derivative twice and the latter directly approximates the 2nd derivative. Please refer to the help manual, the formula are documented.

Okay, do you really want curvature or the 2nd derivative for each point on arr? They are very different. https://en.wikipedia.org/wiki/Curvature#Precise_definition

You can use diff to get the 2nd derivative from the left. Since diff takes the difference from right to left, e.g. x(2)-x(1), you can first flip x from left to right, then use diff. Some codes like,

x=fliplr(x)
first=x./h
second=diff(first)./h

where h is the space between x. Notice I use ./, which idicates that h can be an array (i.e. non-uniform spaced).

🌐
MathWorks
mathworks.com › matlabcentral › answers › 427016-how-do-i-find-derivative-if-given-a-matrix-of-values
how do i find derivative if given a matrix of values? - MATLAB Answers - MATLAB Central
October 30, 2018 - If I am given a vector of velocities stored in a vector (say V= [v(0) v(1) ... v(n)], is there a way for me to find the acceleration (derivative of velocity) without having a function at just those points (0,1,.... n)? Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/427016-how-do-i-find-derivative-if-given-a-matrix-of-values#answer_344274
🌐
TutorialsPoint
tutorialspoint.com › differential-or-derivatives-in-matlab
Differential or Derivatives in MATLAB
September 6, 2023 - This MATLAB code calculates the difference between two consecutive elements of the input vector starting from left. The following syntax of the 'diff' function is utilized to calculate the derivative of a multidimensional array along a specified dimension ? ... Here, A is the multidimensional ...
🌐
GeeksforGeeks
geeksforgeeks.org › differential-or-derivatives-in-matlab
Differential or Derivatives in MATLAB | GeeksforGeeks
August 23, 2021 - These derivatives help us grasp how a function changes considering its input variables. In machine learning, where we commonly deal with complicated models and high-dimension ... Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,.
🌐
MathWorks
mathworks.com › matlab › mathematics › numerical integration and differential equations
Numerical Integration and Differentiation - MATLAB & Simulink
For differentiation, you can differentiate an array of data using gradient, which uses a finite difference formula to calculate numerical derivatives.
Top answer
1 of 3
9

You are looking for the numerical gradient I assume.

t0 = 0;
ts = pi/10;
tf = 2*pi;

t  = t0:ts:tf;
x  = sin(t);
dx = gradient(x)/ts

The purpose of this function is a different one (vector fields), but it offers what diff doesn't: input and output vector of equal length.


gradient calculates the central difference between data points. For an array, matrix, or vector with N values in each row, the ith value is defined by

The gradient at the end points, where i=1 and i=N, is calculated with a single-sided difference between the endpoint value and the next adjacent value within the row. If two or more outputs are specified, gradient also calculates central differences along other dimensions. Unlike the diff function, gradient returns an array with the same number of elements as the input.

2 of 3
2

I know I'm a little late to the game here, but you can also get an approximation of the numerical derivative by taking the derivatives of the polynomial (cubic) splines that runs through your data:

function dy = splineDerivative(x,y)

% the spline has continuous first and second derivatives
pp = spline(x,y); % could also use pp = pchip(x,y);

[breaks,coefs,K,r,d] = unmkpp(pp);
% pre-allocate the coefficient vector
dCoeff = zeroes(K,r-1);

% Columns are ordered from highest to lowest power. Both spline and pchip
% return 4xn matrices, ordered from 3rd to zeroth power. (Thanks to the
% anonymous person who suggested this edit).
dCoeff(:, 1) = 3 * coefs(:, 1); % d(ax^3)/dx = 3ax^2;
dCoeff(:, 2) = 2 * coefs(:, 2); % d(ax^2)/dx = 2ax;
dCoeff(:, 3) = 1 * coefs(:, 3); % d(ax^1)/dx = a;

dpp = mkpp(breaks,dCoeff,d);

dy = ppval(dpp,x);

The spline polynomial is always guaranteed to have continuous first and second derivatives at each point. I haven not tested and compared this against using pchip instead of spline, but that might be another option as it too has continuous first derivatives (but not second derivatives) at every point.

The advantage of this is that there is no requirement that the step size be even.

🌐
MathWorks
mathworks.com › matlabcentral › answers › 406064-sorting-a-complex-array-and-its-derivatives
Sorting a complex array and its derivatives - MATLAB Answers - MATLAB Central
June 18, 2018 - I was expecting that array of indices in every row (variable idx) would do the same job, but without for loop, however, angle3 just contains 4 values (1st,2nd, 3rd,4th of the original array), which is though logical because idx has only integer values 1..4. ... Thank you in advance! Sign in to comment. Sign in to answer this question. ... https://www.mathworks.com/matlabcentral/answers/406064-sorting-a-complex-array-and-its-derivatives#answer_325079
Top answer
1 of 1
2

This code for the most part does what it intends to do: compute the numerical approximation to the derivative. However, the way you're computing the derivative is slightly incorrect. Basically, for each point in your array, you want to subtract the point to the left with the point to the right and divide by 2*h. If you want to do that, you need to post-multiply the vector y and transform it into a column vector.

Transpose y then take D and multiply this with y:

>> d = D*y.'

d =

     4
     8
    12
    -9

However, I'd like to point out that the first and last entry don't make any sense because of your D matrix:

D =

         0    0.2500         0         0
   -0.2500         0    0.2500         0
         0   -0.2500         0    0.2500
         0         0   -0.2500         0

What you are computing here is the central difference for the numerical derivative, which requires a point to the left and a point to the right of the point you are evaluating in your array. Basically, for the first point, you only have the point to the right where the left point is undefined as you are going out of bounds. Similarly for the last point, you only have a point to the left and the point to the right is also out of bounds as it doesn't exist.

The numerical derivative matrix here assumes that the points going out of bounds are 0. It just turns out that you get the derivative correct at this point which is a fluke (i.e. 2*x at x = 1 is 2). The reason why is because the point at x = 0 does give the output of x.^2 being equal to 0, so the omission of this point in the array acts as if it was in your point array to begin with.

The same thing is required for the second derivative:

>> d2 = D*d

d2 =

    2.0000
    2.0000
   -4.2500
   -3.0000

However, bear in mind that for your previous first derivative result d, the last entry is garbage and so if you computed the derivative of this result, you would be using the incorrect right most term when you look at the third entry of d, and so the last two and also the first two entries of d2 are garbage. It just so happens that the second entry of d2 is correct. This is attributed to x = 0 not actually existing in the previous result, but with the assumption that values out of bounds in your point array are 0, we do get the right result.

You should try this on a longer sequence. For example, try doing it on this:

h = 2;
x = 2:h:20;
y = x.^2;

This defines a sequence going from 2 to 20 in steps of h = 2. This is what we get for x, and d:

x =

    2     4     6     8    10    12    14    16    18    20

d =

     4
     8
    12
    16
    20
    24
    28
    32
    36
   -81

As you can see the first and last entry are meaningless as we don't have a left most point for the first entry and a right most point for the second entry to consider. However, the first point by fluke gives us the correct result as we talked about earlier. In general, the first and last entry should not give you the correct results as we don't have a left or right point to account for the derivative. For the rest of the points, you can see that we are correctly computing the derivative, or 2*x.

Let's take a look at the second derivative, d2:

d2 =

    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
    2.0000
  -28.2500
   -9.0000

It so happens that the first two elements are correct, even though they theoretically shouldn't, but we got it by fluke. However, the last two elements aren't correct.


What you need to take away from this is to remember one thing. Remember that when taking numerical derivatives with the central difference, you have to trim off the first n elements and the last n elements, where n is the order of the derivative you are considering.