Question 1

Find the roots and graph \(f(x) = 3x^2 - x - 2\).

# 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) {
  3 * x^2 - x - 2
}
roots <- uniroot.all(f,c(-10,10))
for (x in seq_along(roots)) {
  cat("Root",x,":",roots[x],"\n")
}
## Root 1 : 1 
## Root 2 : -0.6666979
x_values <- seq(-2,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) = 3x^2 - x - 2",
       caption = paste("Roots:",roots[1],"and",round(roots[2],2)),
       x = "x",
       y = "y") +
  theme_gray(base_size = 14)

Question 2

Compute \(A \cdot B\), where \(A\) and \(B\) are defined below.

\[A = \begin{bmatrix} 8 & 4 \\ 2 & 4 \\ 6 & 1 \end{bmatrix} B = \begin{bmatrix} 4 & 2 \\ 4 & 9 \\ 2 & 1 \end{bmatrix}\]

A <- matrix(data = c(8,4,2,4,6,1),nrow = 3,ncol = 2,byrow = T)
B <- matrix(data = c(4,2,4,9,2,1),nrow = 3,ncol = 2,byrow = T)
if (nrow(B) == ncol(A)) {
  AB <- A %*% B
  AB
} else {
  cat("AB does not exist due to insufficient dimensions.")
}
## AB does not exist due to insufficient dimensions.

Question 3

The above shows part of the graph of the function \(g(x) = 0.1x^2 - 5\). Use the graph to estimate the slope (gradient) at the point on the curve with x-coordinate 10.

# install.packages("Deriv")
library(Deriv)
g <- function(x) {
  0.1 * x^2 - 5
}
g_prime <- Deriv(g)
slope <- g_prime(x = 10)
cat("The slope at the point x = 10 is:",slope,"\n")
## The slope at the point x = 10 is: 2