Derivatives by hand

Find the first derivative of the following functions the long way

Use the definition of the derivative:

\[\frac{df}{dx}=\lim_{\Delta x\to 0}\frac{f(x+\Delta x)-f(x)}{\Delta x}\]

  1. \(f(x)=3x^2-x+1\)

  2. \(G(t)=4-3t\)

Find the first derivative of the following using rules

  1. \(f(x) = -3x^5+2x^3-12\)

  2. \(C(z)=4.2z-8.7z^3\)

Find an instantaneous slope

  1. A researcher finds that the increase in algal biomass (\(B\), in grams) in their aquarium over time (\(t\), in hours) follows the function:

\[B(t) = 0.4 + 0.035t^2\]

  • What is the mass of algae in tank at 4.5 hours?

  • At what rate is biomass increasing in the tank after 10 hours?

Derivatives in R with deriv() and D()

  • Create a new R project
  • To the project, add an R script (discuss how a script differs from R Markdown)
  • Add a header (comment out lines with # in a script)
  • Follow along to find the derivative of several functions using deriv() and D()

Example:

# Create an expression (right hand side of the equation...):
ex_1 <- expression(5 * x ^ 2)

# Find the derivative with deriv():
my_derivative <- deriv(ex_1, "x")

# Check it out:
my_derivative
## expression({
##     .value <- 5 * x^2
##     .grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
##     .grad[, "x"] <- 5 * (2 * x)
##     attr(.value, "gradient") <- .grad
##     .value
## })
# Evaluate it at x = 2.8
x <- 2.8

# Use it! (first returned is function value, second returned is slope)
eval(my_derivative)
## [1] 39.2
## attr(,"gradient")
##       x
## [1,] 28

Alternatively:

# Create an expression:
ex_2 <- expression(3.1 * x ^ 4 - 28 * x)

# Find the derivative with respect to x:
D(ex_2, "x")
## 3.1 * (4 * x^3) - 28

More examples with D()

Example:

# Create and store your function
fx <- expression(x^2)

# Find the derivative (with `D()` function):
df_dx <- D(fx, 'x')

# Return the derivative
df_dx
## 2 * x
# Find the slope at x = 10:
x <- 10
eval(df_dx)
## [1] 20

You try!

Using the D() function in R:

  1. Find \(\frac{dg}{dx}\) given: \(g(z) = 2z^3-10.5z^2+4.1\)

  2. Find \(\frac{dT}{dy}\) given: \(T(y) = (2y^3+1)^4-8y^3\)

Solutions:

\(g(z) = 2z^3-10.5z^2+4.1\)

  gz = expression(2*z^3 - 10.5*z^2 + 4.1)
  dg_dz = D(gz, 'z')
  
  # Return dg_dz
  dg_dz
## 2 * (3 * z^2) - 10.5 * (2 * z)

\(T(y) = (2y^3+1)^4-8y^3\)

  ty = expression((2*y^3+1)^4 - 8*y^3)
  dt_dy = D(ty, 'y')
  
  # Return dt_dy
  dt_dy
## 4 * (2 * (3 * y^2) * (2 * y^3 + 1)^3) - 8 * (3 * y^2)

To find the slope of T(y) at a range of y-values:

We found \(\frac{dT}{dy}\) above. What if we want to find the slope at a range of values, instead of just one?

# Create a vector of y values from -0.4 to 2.0, by increments of 0.1
y <- seq(-0.4, 2.0, by = 0.1)

# Evaluate the slope of T(y) at each of those values
eval(dt_dy)
##  [1] -1.293869e+00 -3.313644e-01 -4.534665e-02 -1.437122e-03  0.000000e+00
##  [6]  1.442882e-03  4.682121e-02  3.691558e-01  1.671357e+00  5.718750e+00
## [11]  1.673130e+01  4.460117e+01  1.119970e+02  2.692568e+02  6.240000e+02
## [16]  1.397065e+03  3.023241e+03  6.324914e+03  1.279990e+04  2.508216e+04
## [21]  4.765645e+04  8.793682e+04  1.578538e+05  2.761395e+05  4.715520e+05

Hello git & GitHub

Enter hand waving & storytelling: Why git & GitHub (Horst & Lowndes)