# Chapter 6 Derivatives and differentiation

As with all computations, the operator for taking derivatives, D() takes inputs and produces an output. In fact, compared to many operators, D() is quite simple: it takes just one input.

• Input: an expression using the ~ notation. Examples: x^2~x or sin(x^2)~x or y*cos(x)~y

On the left of the ~ is a mathematical expression, written in correct R notation, that will evaluate to a number when numerical values are available for all of the quantities referenced. On the right of the ~ is the variable with respect to which the derivative is to be taken. By no means need this be called x or y; any valid variable name is allowed.

The output produced by D() is a function. The function will list as arguments all of the variables contained in the input expression. You can then evaluate the output function for particular numerical values of the arguments in order to find the value of the derivative function.

For example:

g <- D(x^2 ~ x)
g(1)
## [1] 2
g(3.5)
## [1] 7

## 6.1 Formulas and Numerical Difference

When the expression is relatively simple and composed of basic mathematical functions, D() will often return a function that contains a mathematical formula. For instance, in the above example

g
## function (x)
## 2 * x
## <bytecode: 0x7fbab18b28f0>

For other input expressions, D() will return a function that is based on a numerical approximation to the derivative — you can’t see" the derivative, but it is there inside the numerical approximation method:

h <- D(sin(abs(x - 3) ) ~ x)
h
## function (x)
## numerical.first.partial(.function, .wrt, .hstep, match.call())
## <environment: 0x7fbab12ad238>

## 6.2 Symbolic Parameters

You can include symbolic parameters in an expression being input to D(), for example:

s2 <- D(A  * sin(2 * pi * t / P) + C ~ t)

The parameters, in this case A, P, and C, will be turned into arguments to the s2() function. Note that pi is understood to be the number $$\pi$$, not a parameter.

s2
## function (t, A, P, C)
## A * (cos(2 * pi * t/P) * (2 * pi/P))

The s2() function thus created will work like any other mathematical function, but you will need to specify numerical values for the symbolic parameters when you evaluate the function:

s2
## function (t, A, P, C)
## A * (cos(2 * pi * t/P) * (2 * pi/P))
s2( t=3, A=2, P=10, C=4 )
## [1] -0.3883222
graphFun( s2(t, A=2, P=10, C=4) ~ t, tlim=range(0,20))

## 6.3 Partial Derivatives

The derivatives computed by D( ) are partial derivatives. That is, they are derivatives where the variable on the right-hand side of ~ is changed and all other variables are held constant.

### 6.3.1 Second derivatives

A second derivative is merely the derivative of a derivative. You can use the D( ) operator twice to find a second derivative, like this.

df <- D(sin(x) ~ x)
ddf <- D(df(x) ~ x)

To save typing, particularly when there is more than one variable involved in the expression, you can put multiple variables to the right of the ~ sign, as in this second derivative with respect to $$x$$:

another.ddf <- D(sin(x) ~ x & x)

This form for second and higher-order derivatives also delivers more accurate computations.

### 6.3.2 Exercises

#### 6.3.2.1 Exercise 1

Using D(), find the derivative of 3 * x ^ 2 - 2*x + 4 ~ x.

What is the value of the derivative at $$x=0$$? {-6,-4,-3,-2,0,2,3,4,6}

What does a graph of the derivative function look like? a. A negative sloping line #. A positive sloping line #. An upward-facing parabola #. A downward-facing parabola

#### 6.3.2.2 Exercise 2

Using D(), find the derivative of 5 * exp(0.2 * x) ~ x.

1. What is the value of the derivative at $$x=0$$? {-5,-2,-1,0,1,2,5}.

2. Plot out both the original exponential expression and its derivative. How are they related to each other?
1. They are the same function
2. Same exponential shape, but different initial values
3. The derivative has a faster exponential increase
4. The derivative shows an exponential decay

#### 6.3.2.3 Exercise 3

Use D() to find the derivative of $$e^{-x^2}$$ with respect to $$x$$ (that is, exp(-(x^2) ~ x). Graph the derivative from $$x=-2$$ to 2. What does the graph look like?

1. A bell-shaped mountain
2. Exponential growth
3. A positive wave followed by a negative wave
4. A negative wave followed by a positive wave

#### 6.3.2.4 Exercise 4

What will be the value of this derivative?

D(fred^2 ~ ginger)
1. 0 everywhere
2. 1 everywhere
3. A positive sloping line
4. A negative sloping line

#### 6.3.2.5 Exercise 5

Use D() to find the 3rd derivative of cos(2 * t). If you do this by using the ~t&t&t notation, you will be able to read off a formula for the 3rd derivative.

1. What is it?
1. $$\sin(t)$$
2. $$\sin(2 t)$$
3. $$4 \sin(2 t)$$
4. $$8 \sin(2 t)$$
5. $$16 \sin(2 t)$$
2. What’s the 4th derivative?
1. $$\cos(t)$$
2. $$\cos(2 t)$$
3. $$4 \cos(2 t)$$
4. $$8 \cos(2 t)$$
5. $$16 \cos(2 t)$$

#### 6.3.2.6 Exercise 6

Compute and graph the 4th derivative of cos(2 * t ^ 2) ~ t from $$t=0$$ to 5.

1. What does the graph look like?
1. A constant
2. A cosine whose period decreases as $$t$$ gets bigger
3. A cosine whose amplitude increases and whose period decreases as $$t$$ gets bigger
4. A cosine whose amplitude decreases and whose period increases as $$t$$ gets bigger
2. For cos(2 * t ^ 2) ~ t the fourth derivate is a complicated-looking expression made up of simpler expressions. What functions appear in the complicated expression?
1. sin and cos functions
2. cos, squaring, multiplication and addition
3. cos, sin, squaring, multiplication and addition
4. log, cos, sin, squaring, multiplication and addition

#### 6.3.2.7 Exercise 7

Consider the expression x * sin(y) involving variables $$x$$ and $$y$$. Use D( ) to compute several derivative functions: the partial with respect to $$x$$, the partial with respect to $$y$$, the second partial derivative with respect to $$x$$, the second partial derivative with respect to $$y$$, and these two mixed partials:

pxy = D(x * sin(y) ~ x & y)
pyx = D(x * sin(y) ~ y & x)

Pick several $$(x,y)$$ pairs and evaluate each of the derivative functions at them. Use the results to answer the following:

1. The partials with respect to $$x$$ and to $$y$$ are identical. T or F
2. The second partials with respect to $$x$$ and to $$y$$ are identical. T or F
3. The two mixed partials are identical. That is, it doesn’t matter whether you differentiate first with respect to $$x$$ and then $$y$$, or vice versa. T or F