Learn R Programming

mosaic (version 0.4-1)

numD: Numerical Derivatives

Description

Constructs the numerical derivatives of mathematical expressions

Usage

numD(formula, ..., .hstep = NULL, add.h.control = FALSE)

setInterval(C, wrt, h)

setCorners(C, var1, var2, h)

dfdx(.function, .wrt, .hstep)

d2fdxdy(.function, .var1, .var2, .hstep)

d2fdx2(.function, .wrt, .hstep)

numerical.first.partial(f, wrt, h, av)

numerical.second.partial(f, wrt, h, av)

numerical.mixed.partial(f, var1, var2, h, av)

Arguments

formula
a mathematical expression (see examples and plotFun)
...
additional parameters, typically default values for mathematical parameters
.hstep
numerical finite-difference step (default is 1e-6 or 1e-4 for first and second-order derivatives, respectively)
add.h.control
arranges the returned function to have a .hstep argument that cann be used to demonstrate convergence and error
C
list of arguments for evaluating the function at the "center" point
wrt
character string naming the variable with respect to which differentiation is to be done
h
the finite-difference step size
var1
character string naming the first variable with respect to which differentiation is to be done
var2
character string naming the second variable with respect to which differentiation is to be done
.function
function to be differentiated
.wrt
character string naming the variable with respect to which differentiation is to be done
.step
the finite-difference step size
.var1
character string naming the first variable with respect to which differentiation is to be done
.var2
character string naming the second variable with respect to which differentiation is to be done
f
function to differentiate
av
arguments to the function calling this

Value

  • a function implementing the derivative as a finite-difference approximation

Numerical partials

These functions are not indended for direct use. They just package up the numerical differentiation process to make functions returned by numD and D easier to read.

Details

Uses a simple finite-difference scheme to evaluate the derivative. The function created will not contain a formula for the derivative. Instead, the original function is stored at the time the derivative is constructed and that original function is re-evaluated at the finitely-spaced points of an interval. If you redefine the original function, that won't affect any derivatives that were already defined from it. Numerical derivatives, particularly high-order ones, are unstable. The finite-difference parameter .hstep is set, by default, to give reasonable results for first- and second-order derivatives. It's tweaked a bit so that taking a second derivative by differentiating a first derivative will give reasonably accurate results. But, if taking a second derivative, much better to do it in one step to preserve numerical accuracy.

See Also

D, symbolicD, makeFun, antiD, plotFun

Examples

Run this code
g = numD( a*x^2 + x*y ~ x, a=1)
g(x=2,y=10)
gg = numD( a*x^2 + x*y ~ x&x, a=1)
gg(x=2,y=10)
ggg = numD( a*x^2 + x*y ~ x&y, a=1)
ggg(x=2,y=10)
h = numD( g(x=x,y=y,a=a) ~ y, a=1)
h(x=2,y=10)
f = numD( sin(x)~x, add.h.control=TRUE)
plotFun( f(3,.hstep=h)~h, hlim=range(.00000001,.000001))
ladd( panel.abline(cos(3),0))

Run the code above in your browser using DataLab