README.md

NewtonRaphson

The idea is to start with an initial guess which is reasonably close to the true root, then to approximate the function by its tangent line using calculus, and finally to compute the x-intercept of this tangent line by elementary algebra. This x-intercept will typically be a better approximation to the original function’s root than the first guess, and the method can be iterated.

Installation

You can install it from Github!

# install.packages("devtools")
devtools::install_github("wasabi1989/NewtonRaphson")

Example

This is a basic example which shows you how to compute root or zero of real-valued function:

library(NewtonRaphson)


target_func1 <- function(x) {x^3 + 3*x^2 + 8*x - 10}
Newton_Raphson(func = target_func1,
               x0 = 10)
#> $root
#> [1] 0.877134
#> 
#> $iter
#> [1] 10

target_func2 <- function(x) {x^4 + 3*x^3 - 6*x^2 + 8*x - 10}
Newton_Raphson(func = target_func2,
               x0 = 10)
#> $root
#> [1] 1.317561
#> 
#> $iter
#> [1] 12

func_list <- list(target_func1, target_func2)

# lapply(func_list, Newton_Raphson) is same
func_list %>% 
  purrr::map(.x = ., function(x){Newton_Raphson(x)})
#> [[1]]
#> [[1]]$root
#> [1] 0.877134
#> 
#> [[1]]$iter
#> [1] 5
#> 
#> 
#> [[2]]
#> [[2]]$root
#> [1] 1.317561
#> 
#> [[2]]$iter
#> [1] 6


wasabi1989/NewtonRaphson documentation built on Nov. 14, 2019, 12:18 a.m.