Find the roots and graph \(f(x) = x^2 - 13x + 40\).
# 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 - 13 * x + 40
}
roots <- uniroot.all(f,c(-10,10))
for (x in seq_along(roots)) {
cat("Root",x,":",roots[x],"\n")
}
## Root 1 : 5
## Root 2 : 8
x_values <- seq(4,9,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 - 13x + 40",
caption = paste("Roots:",roots[1],"and",roots[2]),
x = "x",
y = "y") +
theme_gray(base_size = 14)
Find the slope of the tangent line at \((4,1)\) on the graph \(g(x) = (x - 2)(x + 4)\).
# install.packages("Deriv")
library(Deriv)
g <- function(x) {
(x - 2) * (x + 4)
}
g_prime <- Deriv(g)
slope <- g_prime(x = 4)
cat("The slope of the tangent line at (4,1) is:",slope,"\n")
## The slope of the tangent line at (4,1) is: 10
Find the value(s) of \(x\) where the function \(h(x) = -544x - 152x^2 - 480\) crosses the \(x\)-axis.
# install.packages(c("rootSolve","tidyverse"))
library(rootSolve)
library(tidyverse)
h <- function(x) {
-544 * x - 152 * x^2 - 480
}
q3_roots <- uniroot.all(h,c(-10,10))
for (x in seq_along(q3_roots)) {
cat("Root",x,":",q3_roots[x],"\n")
}
## Root 1 : -2
## Root 2 : -1.578767
x_vals <- seq(-3,0,length.out = 500)
y_vals <- h(x_vals)
q3_data <- data.frame(x = x_vals,y = y_vals)
ggplot(q3_data,aes(x = x,y = y)) +
geom_line(col = "black",lwd = 1.25) +
annotate("point",x = q3_roots[1],y = h(q3_roots[1]),col = "red",size = 5) +
annotate("point",x = q3_roots[2],y = h(q3_roots[2]),col = "red",size = 5) +
labs(title = "Graph of h(x) = -544x - 152x^2 - 480",
caption = paste("Roots:",q3_roots[1],"and",round(q3_roots[2],2)),
x = "x",
y = "y") +
theme_gray(base_size = 14)
Determine whether or not the equation below has a unique solution.
\[\begin{bmatrix} 6 & -8 \\ 3 & 12 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} -188 \\ -101 \end{bmatrix}\]
This translates to the following system of equations…
\[6x - 8y = -188 \\ 3x + 12y = -101\]
q4_data <- data.frame(x = c(6,3),
y = c(-8,12),
constants = c(-188,-101))
q4_model <- lm(constants ~ . - 1,data = q4_data)
coef(q4_model)
## x y
## -31.91667 -0.43750
Since we received two unique values, this means our matrix equation has an unique solution.
Michael counted the number of letters in each word of a sentence and recorded his results below. Construct a bar graph for the data.
q5_data <- data.frame(`Number of Letters` = 1:8,
Frequency = c(3,4,7,5,4,1,0,2))
q5_data
## Number.of.Letters Frequency
## 1 1 3
## 2 2 4
## 3 3 7
## 4 4 5
## 5 5 4
## 6 6 1
## 7 7 0
## 8 8 2
# install.packages("tidyverse")
library(tidyverse)
ggplot(q5_data,aes(x = factor(Number.of.Letters),y = Frequency)) +
geom_col() +
labs(title = "Number of Letters in Each Sentence",
x = "Number of Letters",
y = "Frequency") +
theme_gray(base_size = 14)
For the sentence below, record the letters in a tally and frequency table. Then, determine which letter occurs the most frequently.
“PLEASE EXCUSE MY DEAR AUNT SALLY”
# install.packages("tidyverse")
library(tidyverse)
sentence <- "PLEASE EXCUSE MY DEAR AUNT SALLY"
q6_data <- sentence %>%
str_replace_all(" ","") %>% # remove spaces
str_split("",simplify = T) %>% # split into letters
table() %>% # counting each letter
as.data.frame() # convert into a data frame
colnames(q6_data) <- c("Letter","Frequency")
q6_data
## Letter Frequency
## 1 A 4
## 2 C 1
## 3 D 1
## 4 E 5
## 5 L 3
## 6 M 1
## 7 N 1
## 8 P 1
## 9 R 1
## 10 S 3
## 11 T 1
## 12 U 2
## 13 X 1
## 14 Y 2
# install.packages("tidyverse")
library(tidyverse)
q6_data$Letter <- as.character(q6_data$Letter) # converting from a categorical variable to a character variable
answer <- q6_data %>%
filter(Frequency == max(Frequency)) %>%
pull(Letter)
cat("The letter that occurs the most frequently is:",answer,"\n")
## The letter that occurs the most frequently is: E
A sample of young people were asked what they most liked to watch on TV on each day of the week, and they obtained the following results. Perform a \(\chi^2\) test to determine the value of \(\chi^2\), the number of degrees of freedom, and the value of p.
q7_data <- data.frame(Weekdays = c("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"),
Movies = c(2,3,4,5,3,1,8),
Reality = c(6,7,8,4,3,7,4),
Sport = c(5,6,7,3,7,3,7),
Documentaries = c(7,4,1,8,7,9,1))
q7_data
## Weekdays Movies Reality Sport Documentaries
## 1 Sunday 2 6 5 7
## 2 Monday 3 7 6 4
## 3 Tuesday 4 8 7 1
## 4 Wednesday 5 4 3 8
## 5 Thursday 3 3 7 7
## 6 Friday 1 7 3 9
## 7 Saturday 8 4 7 1
# install.packages("tidyverse")
library(tidyverse)
chi_square_test_result <- q7_data %>%
select(Movies,Reality,Sport,Documentaries) %>%
chisq.test()
## Warning in chisq.test(.): Chi-squared approximation may be incorrect
cat("The value of chi squared is:",chi_square_test_result$statistic[['X-squared']],"\n")
## The value of chi squared is: 28.36893
cat("The number of degrees of freedom is:",chi_square_test_result$parameter,"\n")
## The number of degrees of freedom is: 18
cat("The value of p is:",chi_square_test_result$p.value,"\n")
## The value of p is: 0.05665829
Use differentiation from first principles to find the slope of the function \(y = \frac{1}{x^2}\) at \(x = -3\).
# install.packages("Deriv")
library(Deriv)
y <- function(x) {
1 / (x^2)
}
y_prime <- Deriv(y)
result <- y_prime(x = -3)
cat("The slope of the tangent line at x = -3 is:",result,"\n")
## The slope of the tangent line at x = -3 is: 0.07407407