A problem I often am faced with is applying a function to data from a simulation, for instance computing the Newton Cooling of a bunch of materials. I asked this question on Twitter https://twitter.com/JennyBryan/status/930475618210213888 and @JennyBryan provided two options for solving this problem using dplyr
and purrr
as comments on the newton-cooling-with-purrr.R
file below.
The material properties might be provided as follows:
material_details <- tribble(
~material, ~k.cooling.constant,
"copper", 0.1,
"graphite", 0.01
)
## A tibble: 2 x 2
# material k.cooling.constant
# <chr> <dbl>
#1 copper 0.10
#2 graphite 0.01
Initial temperatures as follows:
initial_temperatures <- tribble(
~material, ~t.zero, ~t.surroundings,
"copper", 350, 273,
"graphite", 300, 273
)
# A tibble: 2 x 3
# material t.zero t.surroundings
# <chr> <dbl> <dbl>
#1 copper 350 273
#2 graphite 300 273
Newton's Law of Cooling is as follows:
newton_cooling <- function(t,
t.zero = 300,
t.surroundings = 273,
k.cooling.constant = 0.001) {
t.surroundings + (t.zero - t.surroundings) * exp(-k.cooling.constant * t)
}
Now how can the cooling of these materials be simulated at time points seq(0, 100, 0.5)
most easily using the tidyverse?
Here are some ideas: