Newton Raphson Root Calculating Function

ftn4 <- function(x) {
     # returns function value and its derivative at x
     fx <- (x^2-9)
     dfx <- 2*x
     return(c(fx, dfx))
}

Executing It to Show the Number of Iterations Needed

newtonraphson(ftn4,2)
## At iteration 1 value of x is: 3.25 
## At iteration 2 value of x is: 3.009615 
## At iteration 3 value of x is: 3.000015 
## At iteration 4 value of x is: 3 
## Algorithm converged
## [1] 3

Showing Graphically After the First Iteration

Initial Value 2

newtonraphson_show(ftn4,2,xmax = 3.5)

## last x value 3.25  continue (y or n)?
## [1] 3.25

Showing Graphically after 2nd Iteration

Value becomes 3.25

newtonraphson_show(ftn4,3.25,xmax=3.5)

## last x value 3.009615  continue (y or n)?
## [1] 3.009615

Showing Graphically after 3rd Iteration

Value becomes 3.009615 and the triangle becomes too small as

the value comes closer to 3.00

newtonraphson_show(ftn4,3.009615,xmax=3.5)

## last x value 3.000015  continue (y or n)?
## [1] 3.000015

Showing Graphically after last Iteration

Final value becomes 3

newtonraphson_show(ftn4,3,xmax = 3.5)

## last x value 3  continue (y or n)?
## [1] 3