1. The point of this unit is to introduce derivatives and partial derivatives in a general setting, and to introduce numerical differentiation with grad.
    • Previously, we noted the simplest example of a derivative is the slope of a line.
    • We also noted that the closer you look at a smooth curve, the more it looks like a line. This allows us to assign a slope at any point on a smooth curve by taking a limit.
  2. Numerical derivatives with grad
    • Derivatives of many functions can be found analytically. The figure shows how to find the derivative of \(x^2\) using only the definition and elementary algebra.
    • Here, however, we will focus on estimating derivatives numerically, using the function, grad, in package numDiff. (The name is a short form of “gradient,” a term which will be explained later.)
    • grad takes two arguments, a function and a point at which the derivative of the function is to be estimated.
    • For instance grad(function(x)x^2, 1) will estmate the derivative of x^2 at x=1. You know from the analytic derivation that this should be 2.
    • In the example, grad’s first argument, function(x)x^2 is an R function which takes one numerical argument and returns the value x^2 at the point x represented by that argument.
    • Using common functions such as cos, sin, exp, log, 1/x, have students compare their numerical and analytic derivatives. The follow code illustrates how that might be done. Note that grad is vectorized for functions of 1 variable but has nothing analogous for functions of several. I think that is confusing, but vectorization comes in handy here.
    # Show that the derivative of sin is cos.
    x <- seq(0, 2*pi, by=.2)
    # swirl does this
    plot(x, cos(x), type='l', lwd=2, xlab="x", ylab="sin(x)", main=expression(over(d, d*x)~~sin(x) == cos(x)))
    abline(h=0)
    legend('bottomleft', "cos(x)", lwd=2, cex=1.5)
    legend('bottomright', expression(over(d, d*x)~~sin(x)), pch=19, col="blue", cex=1.5)
    # student does this
    deriv <- grad(sin, x)
    points(x, deriv, pch=19, col="blue", cex=1.5)

  3. Gradients on Maunga Whau
    • In general, we may have functions of several variables, such as this topographic map of Maunga Whau volcano. Here f(x, y) is altitude, x is northward distance from a reference point, and y is westward distance from the reference point.
    • For this case there are partial derivatives in which estimate the slope with respect to one variable while holding the others constant.
    • At a point on Maunga Whau, the partial derivatives represent slopes in the x and y directions. In the case pictured, the partial derivatives are indicated by the slopes of the lines labeled x and y.
    • The gradient of a function of more than one variable is a vector of its partial deriviatives. It is usually symbolized by a nabla (upside down triangle,) as illustrated in the figure.
    • It can be shown that the gradient indicates the magnitude and direction of steepest ascent. The gradient at a point on Maunga Whau is shown here.
    • Like derivatives, gradients can be estimated using the function, grad, in package numDiff.
    • As before, grad takes two arguments, a function and a point at which the gradient of the function is to be estimated. But in this case, the “point” is a vector.
    • If the function depends on two variables, the vector has length two, if three length three, and so on.
    • We’ve written mw(point) which gives the altitude of Maunga Whau as a function of point coordinates. (Actually it gives twice the altitude, since that’s the way z is calibrated.)
    • Illustrate the use of mw by finding the altitude at the pictured points.
    mw(c(670, 310))
    ## [1] 258
    mw(c(580, 390))
    ## [1] 280
    mw(c(360, 460))
    ## [1] 322
    • Illustrate grad by finding the gradient at those same points
    grad(mw,c(670, 310))
    ## [1] -73.04468 -78.64506
    grad(mw,c(580, 390))
    ## [1] 0 0
    grad(mw,c(360, 460))
    ## [1]  66.55686 105.26291
    • The gradient at c(670, 310) is zero because that location is a local maximum, i.e., the top of a hill. Note that it is not the highest point on the map, hence is called local.
  4. Gradient ascent
    • Ascend Maunga Whau by gradient ascent. The climber goes 50 meters in the gradient, are steepest ascent, direction.
    • This could be a demo or an interactive question where the user gets to guess which of 3 starting points will ascend to the rim of the volcano.