library(rootSolve)
Code: pg 161 Book: Calculus Exercises Section 4.1 8-11
Problem: 8
equation <- function(x){
return(x**3 + 5 * x**2 - x - 1)
}
plot(equation, from = -10, to = 10)
abline(0, 0, col = 'blue')
g <- function(x) {
x^3 + 5*x^2 - x - 1
}
gPrime <- function(x) {
3*x^2 + 10*x -1
}
tolerance <- .00001
root <- function(g, gPrime, guess, tolerance) {
x = guess
while (abs(g(x)) > tolerance) {
x = x - g(x)/gPrime(x)
}
x
}
root(g, gPrime, 5, tolerance)
## [1] 0.525429
root(g, gPrime, 0, tolerance)
## [1] -0.3691024
root(g, gPrime, -5, tolerance)
## [1] -5.156325
Verify with R:
coefficient <- c(-1, -1, 5, 1)
result <- polyroot(coefficient)
result
## [1] 0.5254276-0i -0.3691024+0i -5.1563252-0i
Problem: 9
equation <- function(x){
return(x**4 + 2*x**3 - 7*x**2 - x - 1)
}
plot(equation, from = -4, to = 3, n = 1000, ylim=c(-40,40))
abline(0, 0, col = 'blue')
g <- function(x) {
x^4 + 2*x^3 - 7*x^2 - x - 1
}
gPrime <- function(x) {
4*x^3 + 6*x^2 -14*x -1
}
tolerance <- .00001
root <- function(g, gPrime, guess, tolerance) {
x = guess
while (abs(g(x)) > tolerance) {
x = x - g(x)/gPrime(x)
}
x
}
root(g, gPrime, 3, tolerance)
## [1] 1.961383
#root(g, gPrime, 1, tolerance)
root(g, gPrime, -3, tolerance)
## [1] -3.793903
Verify with R:
coefficient <- c(-1, -1, -7, 2, 1)
result <- polyroot(coefficient)
result
## [1] -0.0837397+0.3568934i -0.0837397-0.3568934i 1.9613824-0.0000000i
## [4] -3.7939030-0.0000000i
Problem: 10
equation <- function(x){
return(x**17 - 2*x**13 - 10*x**8 + 10)
}
plot(equation, from = -4, to = 3, n = 1000, ylim=c(-40,40))
abline(0, 0, col = 'blue')
g <- function(x) {
x**17 - 2*x**13 - 10*x**8 + 10
}
gPrime <- function(x) {
17*x^16 - 26*x**12 -80*x**7 + 10
}
tolerance <- .00001
root <- function(g, gPrime, guess, tolerance) {
x = guess
while (abs(g(x)) > tolerance) {
x = x - g(x)/gPrime(x)
}
x
}
root(g, gPrime, 2, tolerance)
## [1] 1.39341
root(g, gPrime, 1, tolerance)
## [1] 0.9883117
root(g, gPrime, -2, tolerance)
## [1] -1.013403
Verify with R:
coefficient <- c(10,0,0,0,0,0,0,0,-10,0,0,0,0,-2,0,0,0,1)
result <- polyroot(coefficient)
result
## [1] 0.6879488+0.7471924i -0.7018552+0.6786703i -0.7018552-0.6786703i
## [4] 0.6879488-0.7471924i 0.0124521+0.9991391i -1.0134032-0.0000000i
## [7] 0.0124521-0.9991391i 0.9883117+0.0000000i 0.1724880+1.3570560i
## [10] -1.2615042+0.3619573i -0.5516665-1.1339576i 0.9579780+0.7115178i
## [13] -0.5516665+1.1339576i -1.2615042-0.3619573i 0.9579780-0.7115178i
## [16] 1.3934096-0.0000000i 0.1724880-1.3570560i
Problem: 11
equation <- function(x){
return(x**2 *cos(x) + (x-1)*sin(x))
}
plot(equation, from = -3, to = 3, n = 1000)
abline(0, 0, col = 'blue')
g <- function(x) {
x**2 *cos(x) + (x-1)*sin(x)
}
gPrime <- function(x) {
-x**2*sin(x) + 3*x*cos(x) + sin(x) - cos(x)
}
tolerance <- .00001
root <- function(g, gPrime, guess, tolerance) {
x = guess
while (abs(g(x)) > tolerance) {
x = x - g(x)/gPrime(x)
}
x
}
root(g, gPrime, 5, tolerance)
## [1] 4.87404
root(g, gPrime, 4, tolerance)
## [1] 7.96335
root(g, gPrime, 3, tolerance)
## [1] 1.813278
#root(g, gPrime, 2, tolerance)
root(g, gPrime, 1, tolerance)
## [1] 0.5245027
root(g, gPrime, 0, tolerance)
## [1] 0
#root(g, gPrime, -1, tolerance)
#root(g, gPrime, -2, tolerance)
root(g, gPrime, -3, tolerance)
## [1] -2.164774
root(g, gPrime, -4, tolerance)
## [1] -7.993809
root(g, gPrime, -5, tolerance)
## [1] -4.950579
Verified using the following website:
http://www.wolframalpha.com/widgets/view.jsp?id=a7d8ae4569120b5bec12e7b6e9648b86