End interactive session 2B
3 * x^2 + 4Higher order derivatives, partial derivatives, plotting functions in ggplot
\[B(h, T)=0.6Th+2.1h^2\] For water at 28 degrees, find an expression for how biomass is changing with respect to time (\(\frac{\partial B}{\partial h}\)).
\(\frac{\partial B}{\partial h}=0.6T + (2)(2.1)h\)
\(\frac{\partial B}{\partial h}=(0.6)(28) + 4.2h\)
\(\frac{\partial B}{\partial h}=16.8 + 4.2h\)
{ggplot2}day2b-exercises.qmd) to your existing eds212-day2-GHpractice repository and follow along with the exercises, below.In the code chunk below, we create an R function to plot the function, \(f(x) = 3x^2 + 4\). We can use a nifty keyboard shortcut to create a R function around this expression following these steps:
Highlight the expression, then use the keyboard shortcut control + option + X (Mac) or Ctrl + Alt + X (Windows) to pop open a dialog box, which will prompt you to provide a name for your function (you can call it eq1).
Watch your function appear! The shortcut will automatically add any variables from the body of your function (here, just x) as arguments inside function():
# load packages ----
library(tidyverse)
# Define function ----
eq1 <- function(x) {
3 * x^2 + 4
}
# create data frame with range of values to evaluate function over ----
my_data_range <- data.frame(x = c(1, 100))
# Plot it as a continuous curve over a specific range using `geom_function()` ----
# `geom_function()` automatically evaluates the function over the range of x-values specified in the df and adds the resulting y-values to the plot
ggplot(data = my_data_range, aes(x = x)) +
geom_function(fun = eq1)
Let’s look at the function: \(C(t)=t^3\)
Let’s plot the function along with the first derivative:
# Store the function C(t) ----
ct <- function(t) {
t^3
}
# create data frame with range of values to evaluate function over ----
my_data_range <- data.frame(t = c(-5, 5))
# Plot it ----
ggplot(data = my_data_range, aes(x = t)) +
geom_function(fun = ct)
3 * t^2
# Store the derivative as a function ----
dc_dt_fun <- function(t) {
3 * t ^2
}
# Then plot them together ----
ggplot(data = my_data_range, aes(x = t)) +
geom_function(fun = ct, color = "red") +
geom_function(fun = dc_dt_fun, color = "blue")
Specify the variable you want to take the derivative with respect to.
\[f(x,y,z)=2xy-3x^2z^3\]
Find partial, \(\frac{\partial f}{\partial x}\):
Follow the same pattern that we practiced in today’s earlier interactive session (2A), and verify that it worked by checking out your GitHub repo.
End interactive session 2B