Question 1

Find the slope of the tangent line to the function \(f(x) = \frac{4}{x}\) at the point \((4,1)\).

# install.packages("Deriv")
library(Deriv)
f <- function(x) {
  4 / x
}
f_prime <- Deriv(f)
slope <- f_prime(x = 4)
cat("The slope at the point (4,1) is:",slope,"\n")
## The slope at the point (4,1) is: -0.25

Question 2

Solve the following system of equations.

\[12x - 6y = 7 \\ -2x - 5y = 3\]

q2_data <- data.frame(x = c(12,-2),
                      y = c(-6,-5),
                      constants = c(7,3))
q2_model <- lm(constants ~ . - 1,data = q2_data)
coef(q2_model)
##          x          y 
##  0.2361111 -0.6944444

Question 3

What is the area between the curve \(g(x) = \cos(x)\) and the x-axis from \(x = 0\) to \(x = \pi\)?

# 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
g <- function(x) {
  cos(x)
}
answer <- integrate(g,lower = 0,upper = pi)$value
x_values <- seq(0,pi,length.out = 500)
y_values <- g(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 = 1.25) +
  geom_ribbon(aes(ymin = 0,ymax = y),fill = "blue") +
  labs(title = "Graph of g(x) = cos(x)",
       caption = paste("Answer:",round(answer,2)),
       x = "x",
       y = "y") +
  theme_gray(base_size = 14)

Question 4

You are given the following data about animals and their masses. Find the heaviest animal.

  1. Define the data frame.
q4_data <- data.frame(Animal = c("Cat","Bird","Eagle","Lion","Caterpillar","St. Bernard Dog","Chihuahua","Elephant","Catfish","Polar Bear","Finch","Dachshund"),
                      Mass = c(4,2,6,190,.003,80,2.5,6000,15,500,.004,10))
q4_data
##             Animal    Mass
## 1              Cat 4.0e+00
## 2             Bird 2.0e+00
## 3            Eagle 6.0e+00
## 4             Lion 1.9e+02
## 5      Caterpillar 3.0e-03
## 6  St. Bernard Dog 8.0e+01
## 7        Chihuahua 2.5e+00
## 8         Elephant 6.0e+03
## 9          Catfish 1.5e+01
## 10      Polar Bear 5.0e+02
## 11           Finch 4.0e-03
## 12       Dachshund 1.0e+01
  1. Find the heaviest animal.
# install.packages("tidyverse")
library(tidyverse)
heaviest_animal <- q4_data %>%
  filter(Mass == max(Mass)) %>%
  pull(Animal)
cat("The heaviest animal is:",heaviest_animal,"\n")
## The heaviest animal is: Elephant

Question 5

There are two special dice in the shape of octagonal prisms. Each die has eight rectangular faces, each with a number 1 to 8, and two octagonal faces at the end. Abigail rlls the two dice on a flat surface until they settle with numbers on the top faces. Because the dice are rolled, they cannot land on the octagonal faces. Solve the following parts below.

  1. What is the probability Abigail scores two numbers on the top faces that add together to make 9?
die1 <- 1:8 # die 1
die2 <- 1:8 # die 2
counter1 <- 0 # counting the number of times the two rolls sum to 9
N <- 100000 # 100,000 trials
for (i in 1:N) {
  roll1 <- sample(x = die1,size = 1,replace = T)
  roll2 <- sample(x = die2,size = 1,replace = T)
  if (roll1 + roll2 == 9) {
    counter1 <- counter1 + 1
  }
}
probability_make9 <- counter1 / N
cat("The probability the two numbers add together to make 9 is:",probability_make9,"\n")
## The probability the two numbers add together to make 9 is: 0.12727
  1. What is the probability Abigail scores two numbers on the top faces that do not add together to make 9?
die3 <- 1:8 # die 1
die4 <- 1:8 # die 2
counter2 <- 0 # counting the number of times the two rolls do not sum to 9
N <- 100000 # 100,000 trials
for (j in 1:N) {
  roll3 <- sample(x = die3,size = 1,replace = T)
  roll4 <- sample(x = die4,size = 1,replace = T)
  if (roll3 + roll4 != 9) {
    counter2 <- counter2 + 1
  }
}
probability_notmake9 <- counter2 / N
cat("The probability the two numbers add together do not make 9 is:",probability_notmake9,"\n")
## The probability the two numbers add together do not make 9 is: 0.8745