n=1..4
using
finite difference approximations.fderiv(f, x, n = 1, h = 0,
method = c("central", "forward", "backward"), ...)
n=0
function values will be returned.h=0
step size will be set automatically.f
.x
.Use the `forward' (right side) or `backward' (left side) method if the function can only be computed or is only defined on one side. Otherwise, always use the central difference formulas.
Optimal step sizes depend on the accuracy the function can be computed with.
Assuming internal functions with an accuracy 2.2e-16, appropriate step
sizes might be 5e-6, 1e-4, 5e-4, 2.5e-3
for n=1,...,4
and
precisions of about 10^-10, 10^-8, 5*10^-7, 5*10^-6
(at best).
For n>4
a recursion (or finite difference) formula will be applied,
cd. the Wikipedia article on ``finite difference''.
numderiv
, taylor
f <- sin
xs <- seq(-pi, pi, length.out = 100)
ys <- f(xs)
y1 <- fderiv(f, xs, n = 1, method = "backward")
y2 <- fderiv(f, xs, n = 2, method = "backward")
y3 <- fderiv(f, xs, n = 3, method = "backward")
y4 <- fderiv(f, xs, n = 4, method = "backward")
plot(xs, ys, type = "l", col = "gray", lwd = 2,
xlab = "", ylab = "", main = "Sinus and its Derivatives")
lines(xs, y1, col=1, lty=2)
lines(xs, y2, col=2, lty=3)
lines(xs, y3, col=3, lty=4)
lines(xs, y4, col=4, lty=5)
grid()
Run the code above in your browser using DataLab