Question 1

Carl spins the spinner twice and adds the two scores together. Carl wins if he gets a total score of 9 or higher. Use a Monte Carlo simulation to estimate the probability of winning.

spinner <- c(1,5,1,5,2,1,5,3)
counter <- 0
N <- 100000
for (i in 1:N) {
  spin1 <- sample(x = spinner,size = 1,replace = T)
  spin2 <- sample(x = spinner,size = 1,replace = T)
  if (spin1 + spin2 >= 9) {
    counter <- counter + 1
  }
}
probability <- counter / N
cat("The probability of winning is:",probability,"\n")
## The probability of winning is: 0.14056

Question 2

What is the area between the curve \(f(x) = \tan(x)\) and the \(x\)-axis from \(x = 0.4\) to \(x = 1\)?

# install.packages("tidyverse")
library(tidyverse)
## Warning: package 'lubridate' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
f <- function(x) {
  tan(x)
}
area <- integrate(f,lower = 0.4,upper = 1)$value
x_values <- seq(0.3,1.1,length.out = 500)
y_values <- f(x_values)
q2_data <- data.frame(x = x_values,y = y_values)
ggplot(q2_data,aes(x = x,y = y)) +
  geom_line(col = "black",lwd = 1.25) +
  geom_ribbon(data = subset(q2_data,x >= 0.4 & x <= 1),
              aes(ymin = 0,ymax = y),
              fill = "blue") +
  labs(title = "Graph of f(x) = tan(x)",
       caption = paste("Area:",round(area,4)),
       x = "x",
       y = "y") +
  theme_gray(base_size = 14)