B.1 Operation
R commands for taking derivatives of a defined function involve the expression,
D,
and eval
functions. You wrap the function you want to differentiate in expression()
, apply D()
to take the derivative, and use eval()
to compute the result.
simple example
#define a function
f=expression(sqrt(x))
#take the first derivative
df.dx=D(f,'x')
df.dx
#> 0.5 * x^-0.5
#take the second derivative
d2f.dx2=D(D(f,'x'),'x')
d2f.dx2
#> 0.5 * (-0.5 * x^-1.5)
Evaluate
- The first argument passed to eval is the expression you want to evaluate
- the second is a list containing the values of all quantities that are not defined elsewhere.