In Exercises 9 – 12, use Newton’s Method to approximate all roots of the given funcƟons accurate to 3 places aŌer the decimal. If an interval is given, find only the roots that lie in that interval.Use technology to obtain good initial proximations.

  1. f(x) = x^3 + 5x^2 − x − 1
f <- function(x) {
  return(x^3 + 5*x^2 - x - 1)
}
f_prime <- function(x) {
  return(3*x^2 + 10*x - 1)
}
newton_method <- function(f, f_prime, initial_approximation, tolerance) {
  x <- initial_approximation
  while (abs(f(x)) > tolerance) {
    x <- x - f(x) / f_prime(x)
  }
  return(x)
}
tolerance <- 0.001
interval_start <- -10
interval_end <- 10
roots <- vector()
for (x in interval_start:interval_end) {
  root <- newton_method(f, f_prime, initial_approximation = x, tolerance)
  root <- round(root, 3)  
  roots <- c(roots, root)
}
roots <- unique(roots)  
print(roots)
## [1] -5.156  0.525 -0.369  0.526