Learn R Programming

mosaic (version 0.3-13)

D: Derivative and Anti-derivative operators

Description

Operators for computing derivatives and anti-derivatives as functions.

Usage

D(expr, ..., ..h.. = NULL, symbolic = TRUE,
    numerical = !symbolic, method = c("center", "right"))

antiD(expr, from = 0, to = NULL, ...)

Arguments

expr
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
..h..
horizontal distance between points used for secant slope calculation in D(). This is used only if a symbolic derivative is not possible or if numerical=TRUE. The odd name, ..h.., is there to avoid conflicts
symbolic
a logical indicating whether symbolic differentiation should be attempted
numerical
opposite of symbolic available for convenience
method
For first-order numerical derivatives, whether to use a centered difference or a right-facing difference.
...
Default values to be given to unbound variables in the expression expr. See examples.
from
Default value for the lower bound of the interval of integration. This can be set at the time the integral function is invoked.
to
Default value for the upper bound of the interval of integration. This can be set at the time the integral function is invoked (and usually is).

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 used the named form of arguments, to make sure that you have the order right.

    For anti-derivatives, a decorated function (eventually this will be an object of a new class).

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 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. The numerical value of the integral or derivative can be found by evaluating that function against its inputs.

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(from=-Inf, to=0)
F(from=-Inf, to=Inf)
one = makeFun(1~x&y)
by.x = antiD( one(x=x,y=y)~x)
by.xy = antiD(by.x(from=-sqrt(1-y^2),to=sqrt(1-y^2),y=y)~y)
by.xy(from=-1,to=1)

Run the code above in your browser using DataLab