Chapter 7 Exercise 10

Find the area of the shaded region in thegiven graph.

curve(sin(x),xlim = c(0,6*pi/4),ylab = "y",xlab = "x")
curve(cos(x), add = TRUE )

The functions above are sin(x) and cos(x). Pretend that the area between the intersections is shaded. Let’s find the area of that using integration.

First, we gotta find where the intersections are. Set sin(x) to equal cos(x), and we’ll find that the two functions intersect at every \(pi/4 + pi * any\_integer\). In the above figure, that means they intersect at pi/4 and 5pi/4. Now we need to integrate each function between pi/4 and 5pi/4, and subtract the lower function (cos(x)) from the higher function (sin(x)).

#the integrate function wants defined functions, so:
fun1 <- function(x) {
  sin(x)
}

fun2 <- function(x) {
  cos(x)
}



sinval <- integrate(fun1, lower = pi/4, upper = 5*pi/4)[[1]] #It makes extra data, we just want the value

cosval <- integrate(fun2, lower = pi/4, upper = 5*pi/4)[[1]]

sinval - cosval
## [1] 2.828427