Learn R Programming

mosaic (version 0.4-1)

D: Derivative and Anti-derivative operators

Description

Operators for computing derivatives and anti-derivatives as functions.

Usage

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

antiD(formula, ..., Const = 0)

makeAntiDfun(.function, .wrt, from, to, .tol, Const)

numerical.integration(f, wrt, av, args)

Arguments

formula
A formula. The right side specifies the variable(s) with which to carry out the integration or differentiation. On the left side should be an expression or a function that returns a numerical vector of the same length as its argument. The expres
...
Default values to be given to unbound variables in the expression expr. See examples.#' Note that in creating anti-derivative functions, default values of "from" and "to" can be assigned. They are to be written with the name of the v
Const
Numerical value for the constant of integration.
.hstep
horizontal distance between points used for secant slope calculation in numerical derivatives.
add.h.control
logical indicating whether the returned derivative function should have an additional parameter for setting .hstep. Meaningful only for numerical derivatives.
.function
function to be integrated
.wrt
character string naming the variable of integration
from
default value for the lower bound of the integral region
to
default value for the upper bound of the integral region
.tol
tolerance of the numerical integrator (not yet implemented)
f
a function
wrt
character string naming a variable: the var. of integration
av
a list of the arguments passed to the function calling this
args
default values (if any) for parameterss

Value

  • For derivatives, the return value is a function of the variable(s) of differentiation, as well as any other symbols used in the expression. Thus, D(A*x^2 + B*y ~ x + y) will compute the mixed partial with respect to x then y (that is, $\frac{d^2 f}{dy\;dx}$). The returned value will be a function of x and y, as well as A and B. In evaluating the returned function, it's best to use the named form of arguments, to ensure the order is correct.

    a function of the same arguments as the original expression, but with the integration variable split into "from" and "to" prefaced by the name of the variable, e.g. y.from and y.to.

Details

D attempts to find a symbolic derivative for simple expressions, but will provide a function that is a numerical derivative if the attempt at symbolic differentiation is unsuccessful. The symbolic derivative can be of any order (although the expression may become unmanageably complex). The numerical derivative is limited to first or second-order partial derivatives (including mixed partials). antiD always does numerical integration.

antiD returns a function with arguments to and from=0, the upper and lower bounds of the interval of integration w.r.t. the variable of integration. There is also an argument, initVal, that plays the role of the constant of integration. The numerical value of the integral or derivative can be found by evaluating that function.

Examples

Run this code
D(sin(t) ~ t)
D(A*sin(t) ~ t )
D(A*sin(2*pi*t/P) ~ t, A=2, P=10) # default values for parameters.
f <- D(A*x^3 ~ x + x, A=1) # 2nd order partial -- note, it's a function of x
f(x=2)
f(x=2,A=10) # override default value of parameter A
g <- D(f(x=t, A=1)^2 ~ t)  # note: it's a function of t
g(t=1)
gg <- D(f(x=t, A=B)^2 ~ t, B=10)  # note: it's a function of t and B
gg(t=1)
gg(t=1, B=100)
F <- antiD( A*exp(-k*t^2 ) ~ t, A=1, k=0.1)
F(t.from=-Inf, t.to=0)
F(t.from=-Inf, t.to=Inf)
one = makeFun(1~x&y)
by.x = antiD( one(x=x, y=y) ~x )
by.xy = antiD(by.x(x.from=-sqrt(1-y^2), x.to=sqrt(1-y^2), y=y)~y)
by.xy(y.from=-1, y.to=1)
vel <- antiD( -9.8 ~ t  )
pos <- antiD( vel( t.to=t, initVal=v0)~t, Const=50)
pos(0:5, v0=10)
pos(0:5, v0=10, initVal=100)

Run the code above in your browser using DataLab