I have a $200 \times 200$ matrix representing the values taken by a function over an equally spaced grid. I would like to perform derivatives on it.

I am interested in its gradient (i.e. its derivative in direction $x$ and its derivative in direction $y$) and in its Laplacian.

I work on the Matlab platform, where I use the built-in functions gradient and del2 which work very well but their accuracy is limited by the fact that they make use of a small number of points.

Reading this Wikipedia page about Finite difference coefficients, one can understand that there are finite-difference schemes to perform numerical differentiation in a more accurate way. The price to pay to have an increased accuracy is of course to use more complex formulas, which include more points.

I would like to know if there is a library where there are functions capable of doing this job which -I repeat- is: computing the first-order and the second-order derivatives of a 2D matrix (representing the values of a not-explicitly-known function) with a user-defined accuracy.

It would be great if someone could suggest me Matlab libraries, but also C/C++ libraries could work, I guess.

  • 3
    $\begingroup$ For equispaced data, I like to interpolate using a cubic b-spline and differentiate the interpolant. For 2D, you'll need a bicubic b-spline. $\endgroup$
    – user14717
    Apr 8 '19 at 13:44
  • $\begingroup$ Have you checked out the Documentation? (e.g. higher order diff) $\endgroup$
    – MPIchael
    Apr 8 '19 at 13:55
  • $\begingroup$ Thanks for your comment! As I am not an expert of numerical analysis, I propose the following algorithm. Please tell me if it is right: I start form my 200x200 matrix, and I compute a bicupic b-spline. In this way I obtain, for example, a 600x600 matrix (which is 9 times bigger). At this point I compute the derivatives that I need on THIS interpolating matrix, making use of the old built-in functions gradient and del2. Eventually, since my algorithm goes on using 200x200 matrices, I have to SUB-SAMPLE the result that I have obtained. Is it right? $\endgroup$
    – AndreaPaco
    Apr 8 '19 at 14:02
  • $\begingroup$ Thanks for your comment. Can you please explain me how can I set the "accuracy" i.e. the number of points involved in the numerical differentiation? I've guessed only how to change the order of the derivative but not its accuracy. $\endgroup$
    – AndreaPaco
    Apr 8 '19 at 14:04
  • 2
    $\begingroup$ You always (even with FD) differentiate an interpolant. Using (bicubic) splines you can first interpolate and obtain the coefficients, then differentiate the (analytically known) splines and use the coefficients computed before to obtain an approximation of the derivative which, much like the original interpolant, can be evaluated at any point of your choice. That's the use of interpolation: given a set of points find an approximate function which can be evaluated at other points not in the original set. $\endgroup$
    – Nox
    Apr 9 '19 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.