Exercise 6 p173

the roots of f(x) are known or are easily found. Use 5 iterations of Newton’s Method with the given initial approximation to approximate the root. Compare it to the known value of the root. \(f(x) = x^2 - 2, x_o = 1:5\)

Newton’s Method define as : \(x_n + 1 = x_n - \frac {f(x_n)}{f'(x_n)}\)

where \(f'(x)\) is the derivative of \(f(x)\)

\(f(x) = x^2 - 2\) , the derivative is \(f'(2x)\)

See solution in R

# Define the function f(x) and its derivative f'(x)
f <- function(x) {
  return(x^2 - 2)
}

f_prime <- function(x) {
  return(2 * x)
}

# Implement Newton's Method function
newtons_method <- function(f, f_prime, x0, num_iterations) {
  x <- x0
  
  for (i in 1:num_iterations) {
    fx <- f(x)
    fx_prime <- f_prime(x)
    
    x <- x - fx / fx_prime
  }
  
  return(x)
}

# Set initial approximation and number of iterations
x0 <- 1.5
num_iterations <- 5

# Apply Newton's Method
root_approx <- newtons_method(f, f_prime, x0, num_iterations)

# Print the approximation result
print(root_approx)
## [1] 1.414214