Numerical derivative of a vector

11,264

Solution 1

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

enter image description here

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.

Solution 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.

Share:
11,264
milad
Author by

milad

Updated on June 08, 2022

Comments

  • milad
    milad about 2 years

    I have a problem with numerical derivative of a vector that is x: Nx1 with respect to another vector t (time) that is the same size of x. I do the following (x is chosen to be sine function as an example):

    t=t0:ts:tf;
    x=sin(t);
    xd=diff(x)/ts;
    

    but the answer xd is (N-1)x1 and I figured out that it does not compute derivative corresponding to the first element of x.

    is there any other way to compute this derivative?

  • milad
    milad almost 10 years
    Dear gire I have the x values as a set of data and i dont have the analytic function. so how i can extend the domain? I think forward difference is the same as diff .
  • gire
    gire almost 10 years
    @milad by extending the domain I mean: if your t = [0, 1] and you are interested in the derivative dF(t = 0) and dF(t = 1), then you can extend t to t [-0.5, 1.5] (where F is your function and dF represents the numerical derivative)
  • milad
    milad almost 10 years
    thank you but how does it work mathematically for numerical data?
  • Yvon
    Yvon almost 10 years
    Yes. Central difference.
  • rayryeng
    rayryeng almost 10 years
    @thewaywewalk - Very nice!
  • Scott G
    Scott G over 7 years
    Thank you for your solution! Worked like a charm
  • ederag
    ederag over 6 years
    For octave users, the middle section can be replaced by dpp = ppder (pp). Here is the documentation of ppder.
  • Felipe G. Nievinski
    Felipe G. Nievinski over 2 years
    ppder is also available in the Matlab Spline toolbox.