Class activity solutions

Author

Ciaran Evans

x1 <- list(c(2, 7, 9))
x1[[1]]
[1] 2 7 9
x2 <- list(list(c(2, 7, 9)))
x2[[1]][[1]]
[1] 2 7 9
x3 <- list(c(2, 7, 9), list("a", "b"))
x3[[1]]
[1] 2 7 9
x4 <- list(c("a", "b"), list(list(c(2, 7, 9))))
x4[[2]][[1]][[1]]
[1] 2 7 9
  1. There are many possible solutions to this question. Here is one possible solution:
x5 <- list("a", "b", list("c", list(c(2, 7, 9))))
x5[[3]][[2]][[1]]
[1] 2 7 9
fun_list <- list(function(x) {return(x^2 - x)}, 
                 function(x) {return(sin(x) + cos(x))}, 
                 function(x) {return(exp(x)/x)})
  1. The output of the integrate function is actually a list! So it makes sense to store each result in a list:
integral <- list()

for(i in 1:length(fun_list)){
  integral[[i]] <- integrate(fun_list[[i]], 1, 2)
}

integral
[[1]]
0.8333333 with absolute error < 9.3e-15

[[2]]
1.024276 with absolute error < 1.1e-14

[[3]]
3.059117 with absolute error < 3.4e-14