The DERIV function uses three-point (quadratic) Lagrangian interpolation to compute the derivative of an evenly-spaced or unevenly-spaced array of data.

Given three neighboring points in your data with X coordinates [x_{0}, x_{1}, x_{2}] and Y coordinates [y_{0}, y_{1}, y_{2}], the three-point Lagrangian interpolation polynomial is defined as:

y = y_{0}(x â€“ x_{1})(x â€“ x_{2})/[(x_{0} â€“ x_{1})(x_{0} â€“ x_{2})] +

y_{1}(x â€“ x_{0})(x â€“ x_{2})/[(x_{1} â€“ x_{0})(x_{1} â€“ x_{2})] +

y_{2}(x â€“ x_{0})(x â€“ x_{1})/[(x_{2} â€“ x_{0})(x_{2} â€“ x_{1})]

This can be rewritten as:

y = y_{0}(x â€“ x_{1})(x â€“ x_{2})/(x_{01}x_{02}) â€“ y_{1}(x â€“ x_{0})(x â€“ x_{2})/(x_{01}x_{12}) + y_{2}(x â€“ x_{0})(x â€“ x_{1})/(x_{02}x_{12})

where x_{01} = x_{0} â€“ x_{1}, x_{02} = x_{0} â€“ x_{2}, and x_{12} = x_{1} â€“ x_{2}.

Taking the derivative with respect to x:

y' = y_{0}(2x â€“ x_{1} â€“ x_{2})/(x_{01}x_{02}) â€“ y_{1}(2x â€“ x_{0} â€“ x_{2})/(x_{01}x_{12}) + y_{2}(2x â€“ x_{0} â€“ x_{1})/(x_{02}x_{12})

Given a discrete set of *X* locations and *Y* values, the DERIV function then computes the derivative at all of the *X* locations. For example, for all of the *X* locations (except the first and last points), the derivative y' is computed by substituting in x = x_{1}:

y' = y_{0}x_{12}/(x_{01}x_{02}) + y_{1}(1/x_{12} â€“ 1/x_{01}) â€“ y_{2}x_{01}/(x_{02}x_{12})

The first point is computed at x = x_{0}:

y' = y_{0}(x_{01} + x_{02})/(x_{01}x_{02}) â€“ y_{1}x_{02}/(x_{01}x_{12}) + y_{2}x_{01}/(x_{02}x_{12})

The last point is computed at x = x_{2}:

y' = â€“y_{0}x_{12}/(x_{01}x_{02}) + y_{1}x_{02}/(x_{01}x_{12}) â€“ y_{2}(x_{02} + x_{12})/(x_{02}x_{12})

For an evenly-spaced array of values Y[0...Nâ€“1], the three-point Lagrangian interpolation reduces to:

Y'[0] = (â€“3*Y[0] + 4*Y[1] â€“ Y[2]) / 2

Y'[i] = (Y[i+1] â€“ Y[iâ€“1]) / 2 * ; i = 1...Nâ€“2*

Y'[Nâ€“1] = (3*Y[Nâ€“1] â€“ 4*Y[Nâ€“2] + Y[Nâ€“3]) / 2

This routine is written in the IDL language. You can find more details for all of the above equations in the source code in the file deriv.pro in the lib subdirectory of the IDL distribution.

## Example

Compute the derivative of the sin function:

x = [0:10:0.01]

`y = sin(x)`

`dy = DERIV(x, y)`

`help, dy`

print, max(abs(dy - cos(x)))

IDL prints:

`DY FLOAT = Array[1001]`

3.33786e-005

## Syntax

*Result* = DERIV([*X*,]* Y*)

## Return Value

Returns a one-dimensional array of values containing the derivative of *Y*. If either *X* or *Y* is double precision then the result will be double precision, otherwise the result will be single precision.

## Arguments

### X

A one-dimensional array of data containing the X locations. If *X* is omitted then the *Y* points are assumed to be evenly spaced.

### Y

A one-dimensional array of data containing the Y values.

## Version History

Pre 4.0 |
Introduced |

8.3 | Add theory and examples |

## Another Example

Here, we compute the derivative of the function x^{3} â€“ x^{2} + 1 with some added Gaussian random noise. We then use DERIVSIG to compute the error estimates. Finally, we make an errorbar plot of the derivative and overplot the theoretical derivative 3x^{2} â€“ 2x.

x = [-1:2:0.1]

y = x^3 - x^2 + 1 + 0.1*randomn(2,n_elements(x))

`dy = DERIV(x, y)`

sigd = DERIVSIG(x, y, 0, 0.1)

p = ERRORPLOT(x, dy, sigd, 'o', /SYM_FILLED, $

AXIS_STYLE=1, DIM=[300,300], $

XRANGE=[-1.1,2.1], $

XTITLE='x', YTITLE='dy/dx', $

` TITLE='Error bars are 1$\sigma$ uncertainty')`

p = PLOT('3*x^2 - 2*x', /OVERPLOT)

print, CORRELATE(dy, 3*x^2 - 2*x)

IDL prints:

0.940621

## Resources and References

F. B. Hildebrand, *Introduction to Numerical Analysis*, *2nd ed.*, Dover Books, 1987, p. 85.

P.R. Bevington and D. K. Robinson, *Data Analysis and Reduction for the Physical Sciences, 3rd ed.*, McGraw-Hill, 2002, p. 39.