differentiateArray function

Array differentiateArray(
  1. Array y,
  2. Array x
)

Compute the numerical derivative of two arrays

  • y : an Array with y points
  • x : an Array with x points

References

  1. "Numerical Differentiation in Python/v3". https://plot.ly/python/v3/numerical-differentiation/. Retrieved 2019-08-01.
  2. "numpy.gradient". https://docs.scipy.org/doc/numpy/reference/generated/numpy.gradient.html. Retrieved 2019-08-01.
  3. "How to take the derivative of a Signal in Python?". https://stackoverflow.com/questions/40322215/how-to-take-the-derivative-of-a-signal-in-python. Retrieved 2019-08-01.

Examples

var x = linspace(0, 2*pi, num: 100);
var y = arraySin(x);
var dy = differentiateArray(y, x);

print(dy);

/* output:
Array([ 0.9993288 ,  0.99530486,  0.98727317,  0.97526609,  0.95933195,
    0.93953492,  0.91595471,  0.88868628,  0.85783942,  0.82353835,
    0.78592118,  0.74513938,  0.70135716,  0.65475083,  0.60550805,
    0.5538271 ,  0.49991609,  0.44399209,  0.38628029,  0.32701307,
    0.26642909,  0.2047723 ,  0.14229095,  0.07923666,  0.0158633 ,
    -0.04757393, -0.1108196 , -0.17361904, -0.23571937, -0.29687054,
    -0.35682633, -0.4153453 , -0.47219182, -0.52713699, -0.57995956,
    -0.63044684, -0.67839554, -0.72361258, -0.76591588, -0.80513511,
    -0.84111235, -0.87370272, -0.902775  , -0.92821213, -0.94991167,
    -0.96778626, -0.98176391, -0.99178834, -0.99781919, -0.99983218,
    -0.99781919, -0.99178834, -0.98176391, -0.96778626, -0.94991167,
    -0.92821213, -0.902775  , -0.87370272, -0.84111235, -0.80513511,
    -0.76591588, -0.72361258, -0.67839554, -0.63044684, -0.57995956,
    -0.52713699, -0.47219182, -0.4153453 , -0.35682633, -0.29687054,
    -0.23571937, -0.17361904, -0.1108196 , -0.04757393,  0.0158633 ,
    0.07923666,  0.14229095,  0.2047723 ,  0.26642909,  0.32701307,
    0.38628029,  0.44399209,  0.49991609,  0.5538271 ,  0.60550805,
    0.65475083,  0.70135716,  0.74513938,  0.78592118,  0.82353835,
    0.85783942,  0.88868628,  0.91595471,  0.93953492,  0.95933195,
    0.97526609,  0.98727317,  0.99530486,  0.9993288 ,  0.9993288 ]);
*/

Implementation

Array differentiateArray(Array y, Array x) {
  var dy = Array.empty();
  dy = arrayConcat([dy, arrayDiff(y) / arrayDiff(x)]);
  dy.add((y[y.length - 1] - y[y.length - 2]) /
      (x[x.length - 1] - x[x.length - 2]));
  return dy;
}