Newton-Raphson Method is a well-celebrated Numerical Analysis method for finding the root of a function. It is an iterative method for solving equations.
\[ \text{Let } f(x) \text{ be a continuous function, and let } x_0 \text{ be an initial guess for a root of } f(x). \\ \text{The recursion relation is:} \\ x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} \]
The choice of the initial value \(x_0\) heavily affects convergence. Let’s visualize it below.
Let us take \[f(x)=x^3+5\]
library(animation)
library(magick)
f <- function(x) x^3 + 5
f1 <- function(x) 3 * x^2
epsi <- 0.001
x_start <- 4
x_old <- x_start
x_new <- x_old + epsi + 1
saveGIF({
while(abs(x_new - x_old) > epsi) {
curve(f(x), -12, 12, ylab = "f(x)", main = "Newton-Raphson Iteration")
abline(h = 0)
points(x_new, 0, col = "red", pch = 19)
text(x_new, -900, "sol")
arrows(x_new, -900, x_new, -0.1, col = "blue", length = 0.1)
x_old <- x_new
x_new <- x_old - f(x_old)/f1(x_old)
ani.pause()
}
# Final frame
curve(f(x), -12, 12, ylab = "f(x)", main = "Final Approximation")
abline(h = 0)
points(x_new, 0, col = "red", pch = 19)
text(x_new, -900, "sol")
arrows(x_new, -900, x_new, -0.1, col = "blue", length = 0.1)
}, movie.name = "newton.gif", interval = 0.5, ani.width = 600, ani.height = 400)
## [1] TRUE
# Now display the GIF
knitr::include_graphics("newton.gif")