
Last chance! 50% off unlimited learning
Sale ends in
gradient(F, h1 = 1, h2 = 1)
F
is a vector, one gradient vector will be returned. If F
is a matrix, a list with two components will be returned:
F
. A single spacing value, h
, specifies the spacing between points in
every direction, where the points are assumed equally spaced.
fderiv
x <- seq(0, 1, by=0.2)
y <- c(1, 2, 3)
(M <- meshgrid(x, y))
gradient(M$X^2 + M$Y^2)
gradient(M$X^2 + M$Y^2, x, y)
# One-dimensional example
x <- seq(0, 2*pi, length.out = 100)
y <- sin(x)
f <- gradient(y, x)
max(f - cos(x)) #=> 0.00067086
plot(x, y, type = "l", col = "blue")
lines(x, cos(x), col = "gray", lwd = 3)
lines(x, f, col = "red")
grid()
# Two-dimensional example
v <- seq(-2, 2, by=0.2)
X <- meshgrid(v, v)$X
Y <- meshgrid(v, v)$Y
Z <- X * exp(-X^2 - Y^2)
image(v, v, t(Z))
contour(v, v, t(Z), col="black", add = TRUE)
grid(col="white")
grX <- gradient(Z, v, v)$X
grY <- gradient(Z, v, v)$Y
quiver(X, Y, grX, grY, scale = 0.2, col="blue")
Run the code above in your browser using DataLab