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)

Schematic of basic git solo user workflow.
Schematic of basic git solo user workflow.

Getting started - our first shot

1. Make a new R Project

  • Create a new RProj

  • Add a new Quarto document

  • Render

2. Add version control w/ git

  • In the Console, run usethis::use_git() (press Enter, then enter the number for YES when prompted)

  • This makes your local git repo

3. Connect to remote repo

  • In the Console, run usethis::use_github() (press Enter, then enter the number for YES when prompted)

  • This has connected your local git repo to the remote repo, and pushed updates automatically

4. Make, stage, commit & push some changes

  • In your .qmd, make some changes (whatever you want). Save. Render (or not…as long as one file changes).

  • Notice that in the git tab in RStudio, the file(s) you updated show up as having been updated.

  • Stage (check box next to files), Commit (add commit message), Pull (for habit to check for remote changes), then Push to the remote repo. Go to your remote repo on GitHub. Let’s check out some changes!

End