consider \(f(x) = x^2 -2\) and \(x_{0} = 1.5\)
To approximate the value of the root of \(f(x)\) accurately to d decimal places using Newtons Method, we follow this plan:
Choose a value x0 as an initial approximation of the root.
Create successive approximations iteractevely. i.e. given an approximation \(x_{n}\) , compute the next approximation \(x_{n+1}\) as:
\[x_{n+1} = x_{n} - \frac{f(x_{n})}{f'(x_{n})}\] - Stop the Iteration when the successive approximations becomes almost the same at d places after the decimal point.
curve(x^2-2, -4, 4, col = "red", ylab = 'f(x) = x^2 − 2')
abline(v = 0, col = 'black')
abline(v = sqrt(2), col = 'blue')
abline(v = -sqrt(2), col = 'blue')
abline(h = 0, col = 'black')
abline(h = -2, col = 'green')
consider the first derivative of f(x):
\(f'(x) = 2x\)
let’s create our function that will perform the approximation:
library(kableExtra)
library(dplyr)
f <- function(x){return(x^2 -2)}
f1 <- function(x){return(2*x)}
approximTable <- list('iteration' = 0, 'result' = 1.5)
for (i in 2:6){
approximTable$iteration[i] <- i-1
approximTable$result[i] <- approximTable$result[i-1] - f(approximTable$result[i-1])/f1(approximTable$result[i-1])
}
approximTable <- data.frame(approximTable)
# kable(approximTable)
approximTable %>%
kable() %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left") %>%
row_spec(1:6, bold = T, color = "white", background = "#D7261E")
iteration | result |
---|---|
0 | 1.500000 |
1 | 1.416667 |
2 | 1.414216 |
3 | 1.414214 |
4 | 1.414214 |
5 | 1.414214 |
our real solution is \(x = \sqrt{2}\)
so let’s see how the difference is calculated between the real solution and the approximation and display everything at a glance.
#real_sol is our real solution
approximTable$real_sol <- sqrt(2)
#difference between the real solution and the calculated approximation
approximTable$diff <- round(approximTable$result - approximTable$real_sol,6)
approximTable %>%
kable() %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left") %>%
row_spec(1:6, bold = T, color = "white", background = "#D7261E")
iteration | result | real_sol | diff |
---|---|---|---|
0 | 1.500000 | 1.414214 | 0.085786 |
1 | 1.416667 | 1.414214 | 0.002453 |
2 | 1.414216 | 1.414214 | 0.000002 |
3 | 1.414214 | 1.414214 | 0.000000 |
4 | 1.414214 | 1.414214 | 0.000000 |
5 | 1.414214 | 1.414214 | 0.000000 |