gradient: Calculate the x and y gradients for a 2D matrix

Description Usage Arguments Details Value Author(s) Examples

Description

Calculate the x and y gradients for a 2D matrix

Usage

1
gradient(f, x, y, dx, dy)

Arguments

f

the 2D field for which to calculate the gradient

x,y

vectors of the x,y locations of the cells in f

dx,dy

scalar giving the dx,dy in each direction. If only dx is supplied it is assumed dy is the same and spits out a warning.

Details

Calculates the gradient in each of the two directions, x and y, corresponding to rows and columns of f. Uses centred differencing in the interior, and forward/backward differencing at the edges. Is more or less equivalent to Matlab's gradient function, except that the sense of x,y is different in that they correspond to rows/columns.

Value

A list containing the directional gradients corresponding to the x,y directions.

Author(s)

Clark Richards

Examples

1
2
3
4
5
6
7
8
9
library(oce) # for drawDirectionFields
x <- seq(-2, 2, 0.2)
y <- seq(-2, 2, 0.2)
X <- matrix(expand.grid(x,y)[,1], nrow=length(x))
Y <- matrix(expand.grid(x,y)[,2], nrow=length(x))
F <- X * exp(-X^2 - Y^2) 
g <- gradient(F, dx=0.2, dy=0.2)
contour(x, y, F)
drawDirectionField(as.vector(X), as.vector(Y), g$gx, g$gy, scalex=1, add=TRUE)

richardsc/crMisc documentation built on May 27, 2019, 7:59 a.m.