Reputation: 3
I'm using R to integrate a function of single variables, but with varying limits. How do I get a vector solution?
Given the following variables: variable inputs
I need to integrate the following function: t∝ = time till conversion, Ta = Ta upper limit, Ta-∆∝ = Ta lower, E∝ = activation energy, β=5, T0=1173
Here is the code I used that failed:
> library(readr)
_test_file <- read_csv("~/R test file.csv")
#Parsed with column specification:
cols(
Conversion = col_double(),
`Activation Energy (kJ/mol)` = col_double(),
`Ta upper (Kelvin)` = col_double(),
`Ta lower (Kelvin)` = col_double()
)
View(R_test_file)
Ea = R_test_file$`Activation Energy (kJ/mol)`
TaU = R_test_file$`Ta upper (Kelvin)`
TaL = R_test_file$`Ta lower (Kelvin)`
int.fun=function(Ea,T) (exp(-Ea/(8.314462*T)))/(5(exp(-Ea/(8.314462*1173))))
integrate(int.fun, TaL, TaU)
Upvotes: 0
Views: 132
Reputation: 1131
There are some errors in your function definition. Corrected version:
int.fun <- function(T, Ea) (exp(-Ea/(8.314462*T)))/(5*(exp(-Ea/(8.314462*1173))))
You can integrate the function as:
integrate(int.fun, Ea=-33.54, lower=296.1008, upper=553.4211)
If you want to integrate over a vectors of values for limits, you can do something like this:-
Ea <- c(-33.54, -33.51, 7.24, 159.18)
TaU <- c(553.4211, 637.3555, 681.7970, 709.6234)
TaL <- c(296.1008, 553.4211, 637.3555, 681.7970)
lapply(seq_along(Ea), function(i) integrate(int.fun, Ea = Ea[i], lower = TaL[i], upper = TaU[i]))
Upvotes: 1