derivative.RdThis computes the numerical derivative of y=y(x), using
centred derivatives for interior points, and first differences for
the endpoints. (If there are only 2 points, then first differences
are used, then repeated so the result has the same length as x.)
If y is not provided, then the derivative of x with respect to
index is computed.
derivative(x, y)a numerical vector.
optional numerical vector of the same length as x. See
‘Details’ for behaviour if y is not given.
derivative dy/dx
# Demonstrate with a known derivative
x <- seq(0, 2*pi, pi/16)
y <- sin(x)
dydx <- derivative(x, y)
layout(rbind(1,2), heights=c(0.6, 0.4))
par(mar=c(3,3,1,1), mgp=c(2,0.7,0))
plot(x, cos(x), ylab="True, Estimate")
points(x, dydx, col=2, pch=3)
legend("top", pch=c(1, 3), col=c(1, 2), legend=c("True", "Estimate"))
plot(x, cos(x) - dydx, ylab="True - Estimate")