library(tidyverse)
library(palmerpenguins)
\[f(a, z, t)=3at^2-4t+5.1az-z^3\]
\[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}\)).
\[G(t)=2.2 + 3.1t-5.6t^4 + \frac{4.3}{t^3}\]
Brain creativity break - let’s draw some higher order derivatives!
ggplot2
library(tidyverse)
eq = function(x){3*x^2 + 4} # Define function
# Plot it over a specific range using the `geom_function` mark:
ggplot(data.frame(x = c(1, 100)), aes(x = x)) +
geom_function(fun = eq)
eq_2 = function(x){2.4 - 5*x^3 + 2.1*x^2} # Define function
ggplot(data.frame(x = c(-50,50)), aes(x = x)) +
geom_function(fun = eq_2)
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}
# Plot it:
ggplot(data.frame(t=c(-5, 5)), aes(x=t)) +
geom_function(fun=ct)
# Find the derivative:
dc_dt <- D(expression(t^3), 't')
dc_dt
## 3 * t^2
# Store the derivative as a function:
dc_dt_fun <- function(t) {3 * t ^2}
# Then plot them together!
ggplot(data.frame(t = c(-5, 5)), 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 all partials, \(\frac{\partial f}{\partial x}\), \(\frac{\partial f}{\partial y}\) and \(\frac{\partial f}{\partial z}\)
f_xyz <- expression(2*x*y - 3*(x^2)*(z^3))
# Partial with respect to x:
df_dx <- D(f_xyz, 'x')
df_dx
## 2 * y - 3 * (2 * x) * (z^3)
# Partial with respect to y:
df_dy <- D(f_xyz, 'y')
df_dy
## 2 * x
# Partial with respect to z:
df_dz <- D(f_xyz, 'z')
df_dz
## -(3 * (x^2) * (3 * z^2))
Given the function: \[P(q)=2cos(3q+0.4)-5.6e^{1.4q}\]
Find the instantaneous slope at \(q = 0.8\).
# First, create the expression:
pq <- expression(2 * cos(3 * q + 0.4) - 5.6 * exp(1.4 * q))
# Find the first derivative with respect to q:
dp_dq <- D(pq, 'q')
# Return the first derivative:
dp_dq
## -(2 * (sin(3 * q + 0.4) * 3) + 5.6 * (exp(1.4 * q) * 1.4))
# Define value of q
q <- 0.8
# Evaluate dp_dq at that value:
eval(dp_dq)
## [1] -26.03839
Let’s customize a graph a little bit. Here, we’ll use the
penguins
dataset from palmerpenguins
.
Some extras we’ll learn here are:
ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species)) +
scale_color_manual(values = c("purple","cyan3","goldenrod")) +
labs(x = "Flipper length (mm)",
y = "Body mass (g)",
title = "Palmer penguin size measurements",
subtitle = "Palmer Archipelago, Antarctica (2007 - 2009)",
caption = "Collected by Dr. Kristen Gorman and colleages at Palmer Station LTER") +
coord_flip() +
facet_wrap(~island) +
theme_minimal()