A rate function \(r(x) = \csc(2x + 3)\). What is \(r'(x)\)?
# install.packages("Deriv")
library(Deriv)
r <- function(x) {
1 / sin(2 * x + 3) # csc(x) = 1 / sin(x)
}
r_prime <- Deriv(r)
r_prime
## function (x)
## {
## .e2 <- 2 * x + 3
## -(2 * (cos(.e2)/sin(.e2)^2))
## }
Ben takes an ordinary deck of 52 playing cards and removes all the picture cards - all the Jacks, Queens, and Kings. Ben then shuffles the remaining cards and selects a card at random. Ben wins if the number on the card is a prime number. What is the probability of winning?
# install.packages("pracma")
library(pracma)
cards <- 1:10 # assuming the cards are filtered already
N <- 1e6 # 1 million trials
counter <- 0
for (i in 1:N) {
draw <- sample(x = cards,size = 1,replace = T)
if (isprime(draw)) {
counter <- counter + 1
}
}
probability <- counter / N
cat("The probability of winning is:",probability,"\n")
## The probability of winning is: 0.400682
What is the area between the curve \(y = e^x\) and the \(x\)-axis from \(x = -1\) 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() ──
## ✖ purrr::cross() masks pracma::cross()
## ✖ 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) {
exp(x)
}
area <- integrate(f = f,lower = -1,upper = 1)$value
x_values <- seq(-1.05,1.05,length.out = 500)
y_values <- f(x_values)
q3_data <- data.frame(x = x_values,y = y_values)
ggplot(q3_data,aes(x = x,y = y)) +
geom_line(col = "black",lwd = 2) +
geom_ribbon(data = subset(q3_data,x >= -1 & x <= 1),
aes(ymin = 0,ymax = y),
fill = "blue") +
labs(title = "Graph of f(x) = exp(x)",
caption = paste("Area:",round(area,4)),
x = "x",
y = "y") +
theme_gray()