Graph and find the roots of \(f(x) = x^2 + 7x + 12\).
# install.packages(c("rootSolve","tidyverse"))
library(rootSolve)
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) {
x^2 + 7 * x + 12
}
roots <- uniroot.all(f,c(-10,10))
for (x in seq_along(roots)) {
cat("Root:",x,":",roots[x],"\n")
}
## Root: 1 : -4
## Root: 2 : -3
x_values <- seq(-5,-2,length.out = 500)
y_values <- f(x_values)
q1_data <- data.frame(x = x_values,y = y_values)
ggplot(q1_data,aes(x = x,y = y)) +
geom_line(col = "black",lwd = 1.25) +
annotate("point",x = roots[1],y = f(roots[1]),col = "blue",size = 5) +
annotate("point",x = roots[2],y = f(roots[2]),col = "blue",size = 5) +
labs(title = "Graph of f(x) = x^2 + 7x + 12",
caption = paste("Roots:",roots[1],"and",roots[2]),
x = "x",
y = "y") +
theme_gray(base_size = 14)
If \(\vec{r}(t) = (5t^3,e^{11t},e^{5t})\), what is \(\frac{d}{dt} (\vec{r}(t))\)?
# install.packages("Deriv")
library(Deriv)
r <- function(t) {
c(5 * t^3,exp(11 * t),exp(5 * t))
}
r_prime <- Deriv(r)
r_prime
## function (t)
## c(15 * t^2, 11 * exp(11 * t), 5 * exp(5 * t))
Calculate the determinant of matrix \(A\) where \(A = \begin{bmatrix} 4 & 1 \\ 5 & 3 \end{bmatrix}\).
A <- matrix(data = c(4,1,5,3),nrow = 2,ncol = 2,byrow = T)
det_A <- det(A)
cat("det(A) =",det_A,"\n")
## det(A) = 7
Use differentiation from first principles to find the slope of the function \(g(x) = 3x - x^2\) at \(x = 2\).
# install.packages("Deriv")
library(Deriv)
g <- function(x) {
3 * x - x^2
}
g_prime <- Deriv(g)
slope <- g_prime(x = 2)
cat("The slope of the function at x = 2 is:",slope,"\n")
## The slope of the function at x = 2 is: -1
A coin was tossed 150 times. Perform a Monte Carlo simulation to determine the probability of landing on tails up.
coin <- c("Heads","Tails") # our coin
counter <- 0 # number of tails we get
N <- 150 # number of trails
for (i in 1:N) {
toss <- sample(coin,size = 1,replace = T)
if (toss == "Tails") {
counter <- counter + 1
}
}
probability <- counter / N
cat("The probability of landing tails up is:",probability,"\n")
## The probability of landing tails up is: 0.4066667
Bill counted up the number of words in the first 50 sentences of his book, with the results shown below. Record the results in a frequency distribution table, create a bar graph of the data, and determine what length of sentence had the highest frequency.
\[\text{Number of Words} = [5,10,10,10,9,9,10,7,8,8,8,11,11,7,8,5,7,8,8,9,11,11,11,6,10,10,7,12,10,11,9,10,10,12,12,10,11,9,9,9,10,10,11,11,9,9,10,10,9,9]\]
num_words <- c(5,10,10,10,9,9,10,7,8,8,8,11,11,7,8,5,7,8,8,9,11,11,11,6,10,10,7,12,10,11,9,10,10,12,12,10,11,9,9,9,10,10,11,11,9,9,10,10,9,9)
q6_table <- table(num_words)
q6_data <- as.data.frame(q6_table)
colnames(q6_data) <- c("Number of Words","Frequency")
q6_data
## Number of Words Frequency
## 1 5 2
## 2 6 1
## 3 7 4
## 4 8 6
## 5 9 11
## 6 10 14
## 7 11 9
## 8 12 3
# install.packages("tidyverse")
library(tidyverse)
ggplot(q6_data,aes(x = `Number of Words`,y = Frequency)) +
geom_col() +
labs(title = "Number of Words in a Book",
x = "Number of Words",
y = "Frequency") +
theme_gray(base_size = 14)
# install.packages("tidyverse")
library(tidyverse)
q6_data$`Number of Words` <- as.numeric(as.character(q6_data$`Number of Words`))
highest_frequency <- q6_data %>%
slice_max(Frequency) %>%
pull(`Number of Words`)
cat("The length of sentence with the highest frequency is:",highest_frequency,"\n")
## The length of sentence with the highest frequency is: 10