Out of \([57,58,59,60]\), which number is prime?
# install.packages(c("pracma","comprehenr"))
library(pracma)
library(comprehenr)
## Warning: package 'comprehenr' was built under R version 4.5.2
prime_number <- to_vec(for (x in 57:60) if (isprime(x) == TRUE) x)
cat("Tne number",prime_number,"is prime.","\n")
## Tne number 59 is prime.
\((x^3 + 1)(x^2 - 5) + 3 = 0\) has three real roots. Estimate, correct to one decimal place, the middle one of the three roots.
# 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() ──
## ✖ purrr::cross() masks pracma::cross()
## ✖ 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^3 + 1) * (x^2 - 5) + 3
}
x_values <- seq(-3,3,length.out = 500)
y_values <- f(x_values)
q2_data <- data.frame(x = x_values,y = y_values)
ggplot(q2_data,aes(x = x,y = y)) +
geom_line(col = "black",lwd = 1.25) +
geom_hline(aes(yintercept = 0),col = "red",linetype = "dashed") +
labs(title = "Graph of f(x) = (x^3 + 1)(x^2 - 5) + 3",
x = "x",
y = "y") +
theme_gray(base_size = 14)
# install.packages("rootSolve")
library(rootSolve)
##
## Attaching package: 'rootSolve'
## The following objects are masked from 'package:pracma':
##
## gradient, hessian
roots <- uniroot.all(f,c(-10,10)) # f(x) from part (a)
for (x in seq_along(roots)) {
cat("Root",x,":",roots[x],"\n")
}
## Root 1 : -2.295697
## Root 2 : -0.6949581
## Root 3 : 2.175985
cat("The middle root is:",round(roots[2],1),"\n")
## The middle root is: -0.7
If \(g(x) = 3 \tan(x) - \cos(x)\), what is \(\frac{dy}{dx}\)?
# install.packages("Deriv")
library(Deriv)
g <- function(x) {
3 * tan(x) - cos(x)
}
g_prime <- Deriv(g)
g_prime
## function (x)
## 3/cos(x)^2 + sin(x)