Question 1

A rate function is \(r(x) = \cos(2x + 7)\). What is \(r'(x)\)?

# install.packages("Deriv")
library(Deriv)
r <- function(x) {
  cos(2 * x + 7)
}
r_prime <- Deriv(r)
r_prime
## function (x) 
## -(2 * sin(2 * x + 7))

Question 2

There are nineteen sweets in a jar - 4 blue, 4 red, 4 green, 5 yellow, and 2 lilac. You shake the jar and choose one sweet from the jar without looking and note its color. You shake the jar again, and then choose a second sweet without looking. Answer the following questions.

A. What is the probability both sweets chosen are yellow?

jar1 <- c(rep("Blue",4),rep("Red",4),rep("Green",4),rep("Yellow",5),rep("Lilac",2))
N1 <- 1e5
counter1 <- 0
for (i in 1:N1) {
  pick1 <- sample(x = jar1,size = 2,replace = T)
  if (all(pick1 == "Yellow")) {
    counter1 <- counter1 + 1
  }
}
probability1 <- counter1 / N1
cat("The probability both sweets chosen are yellow is:",probability1,"\n")
## The probability both sweets chosen are yellow is: 0.06945

B. What is the probability both sweets chosen are not yellow?

jar2 <- c(rep("Blue",4),rep("Red",4),rep("Green",4),rep("Yellow",5),rep("Lilac",2))
N2 <- 1e5
counter2 <- 0
for (j in 1:N2) {
  pick2 <- sample(x = jar2,size = 2,replace = T)
  if (all(pick2 != "Yellow")) {
    counter2 <- counter2 + 1
  }
}
probability2 <- counter2 / N2
cat("The probability both sweets chosen are not yellow is:",probability2,"\n")
## The probability both sweets chosen are not yellow is: 0.54025

Question 3

Quentin went to a local take-out. How much did he pay for one fries and one nugget?

  1. Define the data frame.
q3_data <- data.frame(Item = c("Burger","Hot Dog","Fries","Soda","Drumstick","Onion Ring","Coffee","Nugget"),
                      Price = c(0.89,0.72,0.65,0.58,0.95,0.24,0.99,0.20))
q3_data
##         Item Price
## 1     Burger  0.89
## 2    Hot Dog  0.72
## 3      Fries  0.65
## 4       Soda  0.58
## 5  Drumstick  0.95
## 6 Onion Ring  0.24
## 7     Coffee  0.99
## 8     Nugget  0.20
  1. Find the solution.
# 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
solution <- q3_data %>%
  filter(Item %in% c("Fries","Nugget")) %>%
  summarise(Total = sum(Price)) %>%
  pull(Total)
cat("Quentin paid $",solution,"for one fries and one nugget.","\n")
## Quentin paid $ 0.85 for one fries and one nugget.