DERIVN Function
Standard Library function that differentiates a function represented by an array.
Usage
result = DERIVN(a, n)
Input Parameters
a — An array of values of the dependent variable.
n — An integer (≥ 0) designating which dimension to differentiate.
Returned Value
result — An array of the same dimensions as a, representing the derivative with respect to the nth independent variable.
Keywords
x — A vector defining the independent variable of differentiation. x defaults to the indices into dimension n of a.
Discussion
DERIVN computes the partial derivatives of a function represented by an array. The array values represent the dependent variable which has been sampled on a grid of cartesian coordinates representing the independent variables. By default the grid is assumed to be square (and represented by array indices). Irregular rectangular grids can be accommodated with the X keyword. But there are often situations when the data is sampled on a non-rectangular grid. GRIDN can still be used in these situations if we regard the grid as points in some curvilinear coordinate system and employ the Chain Rule of differential calculus. Consider the following
2-dimensional example and suppose our data:
z = hanning(500,500)
shade_surf, z
has been sampled on a polar grid with coordinates r and t. For the purposes of this example, we can create such a grid like:
x = interpol([-1,1],500)
y = x
r = sqrt(tensor_add(x*x,y*y))
t = cprod(list(x,y))
t = reform(atan(t(*,1),t(*,0)),500,500)
contour, r, x, y, nlevels=9, position=[50,50,450,450], /device
contour, t, x, y, nlevels=9, position=[50,50,450,450], /device, $
/noerase
And using the chain rule:
dz/dr = (dz/dx)(dx/dr) + (dz/dy)(dy/dr)
dz/dt = (dz/dx)(dx/dt) + (dz/dy)(dy/dt)
we obtain the deriviatives of z with respect to polar coordinates r and t:
dzdr = derivn(z,0)/derivn(r,0) + derivn(z,1)/derivn(r,1)
dzdt = derivn(z,0)/derivn(t,0) + derivn(z,1)/derivn(t,1)
tvscl, dzdr
tvscl, dzdt
The artifacts in the final image are due to the jump discontinuity in t(x,y):
shade_surf, t
The key to this example is that even though z is sampled on a nonrectangular grid defined by r and t, all three variables are related to the same set of array indices. So it is these indices can be used as a basis for the x and y coordinates of the chain rule equations. For brevity, the example was limited to two dimensions, but the same ideas extend to higher dimensions.
Example
PM, DERIVN( [0,2,1,0,1], 0 )
; PV-WAVE prints:
; 2.00000
; 0.500000
; -1.00000
; 0.000000
; 1.00000
PM, DERIVN( [[0,2,1,0,1],[2,1,0,2,0],[1,0,2,1,2]], 0 )
; PV-WAVE prints:
; 2.00000 -1.00000 -1.00000
; 0.500000 -1.00000 0.500000
; -1.00000 0.500000 0.500000
; 0.000000 0.000000 0.000000
; 1.00000 -2.00000 1.00000
PM, DERIVN( [[0,2,1,0,1],[2,1,0,2,0],[1,0,2,1,2]], 1 )
; PV-WAVE prints:
; 2.00000 0.500000 -1.00000
; -1.00000 -1.00000 -1.00000
; -1.00000 0.500000 2.00000
; 2.00000 0.500000 -1.00000
; -1.00000 0.500000 2.00000
See Also