Use Newton’s Method to approximate all roots of the given functions accurate to 3 places after the decimal. If an interval is given, find only the roots that lie in that interval. Use technology to obtain good initial approximates.
\[f(x) = x^3 + 5x^2 - x - 1\]
Using Newton’s Method as described in Chapter 4 in the Apex Calculus Textbook.
\[x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}\]
eqn <- function(x){
return(x**3 + 5 * x**2 - x - 1)
}
# Reference: https://www.math.ucla.edu/~anderson/rw1001/library/base/html/curve.html
plot(eqn, from = -10, to = 10)
abline(0, 0, col = 'blue')
# Reference: https://www.youtube.com/watch?v=C4Zoc0Gh-es
f <- expression(x**3 + 5 * x**2 - x - 1)
newton_method <- function(eqn, x_function, initial_value){
# Gives the approximate roots after user gives an initial value
# Reference from page 161 from Apex Calculus textbook
x <- initial_value
f_prime <- function(x) {}
body(f_prime) <- D(x_function,'x')
while (TRUE) {
oldx <- x
x <- x - (eqn(x)/f_prime(x))
print(x)
if (abs(x-oldx) < .001){
break
}
}
}
print("Initial Guess: -5 ")
## [1] "Initial Guess: -5 "
newton_method(eqn, f, -5)
## [1] -5.166667
## [1] -5.156366
## [1] -5.156325
print("Initial Guess: 0 ")
## [1] "Initial Guess: 0 "
newton_method(eqn, f, 0)
## [1] -1
## [1] -0.5
## [1] -0.3809524
## [1] -0.3692266
## [1] -0.3691024
print("Initial Guess: 1 ")
## [1] "Initial Guess: 1 "
newton_method(eqn, f, 1)
## [1] 0.6666667
## [1] 0.5449735
## [1] 0.5259008
## [1] 0.5254279
The above function demonstrates the three roots using the Newton Method of Approximation. The roots to the above function are: -5.167, -0.369, and 0.525. Let’s confirm this with R.
if (!require(rootSolve)) install.packages("rootSolve")
## Loading required package: rootSolve
library(rootSolve)
# Function polyroot requires a vector of polynomial coefficients in increasing order
v_coefficient <- c(-1, -1, 5, 1)
ans <- polyroot(v_coefficient)
print(paste0("Roots of this function are: ", round(ans,3)))
## [1] "Roots of this function are: 0.525+0i"
## [2] "Roots of this function are: -0.369+0i"
## [3] "Roots of this function are: -5.156+0i"