newton_raphson.RdNewton-Raphson iteration to find roots of equations with the emphasis on complex functions
newton_raphson(initial, f, fdash, maxiter, give=TRUE, tol = .Machine$double.eps)Starting guess
Function for which \(f(z)=0\) is to be solved for \(z\)
Derivative of function (note: Cauchy-Riemann conditions assumed)
Maximum number of iterations attempted
Boolean, with default TRUE meaning to give
output based on that of uniroot() and FALSE meaning to
return only the estimated root
Tolerance: iteration stops if \(|f(z)|<tol\)
Bog-standard implementation of the Newton-Raphson algorithm
If argument give is FALSE, returns \(z\) with
\(|f(z)|<tol\); if TRUE, returns a list with elements
root (the estimated root), f.root (the function
evaluated at the estimated root; should have small modulus), and
iter, the number of iterations required.
Previous versions of this function used the misspelling “Rapheson”.
# Find the two square roots of 2+i:
f <- function(z){z^2-(2+1i)}
fdash <- function(z){2*z}
newton_raphson( 1.4+0.3i,f,fdash,maxiter=10)
#> $root
#> [1] 1.455347+0.3435607i
#>
#> $f.root
#> [1] 0+0i
#>
#> $iter
#> [1] 5
#>
newton_raphson(-1.4-0.3i,f,fdash,maxiter=10)
#> $root
#> [1] -1.455347-0.3435607i
#>
#> $f.root
#> [1] 0+0i
#>
#> $iter
#> [1] 5
#>
# Now find the three cube roots of unity:
g <- function(z){z^3-1}
gdash <- function(z){3*z^2}
newton_raphson(-0.5+1i, g, gdash, maxiter=10)
#> $root
#> [1] -0.5+0.8660254i
#>
#> $f.root
#> [1] 2.220446e-16-1.110223e-16i
#>
#> $iter
#> [1] 6
#>
newton_raphson(-0.5-1i, g, gdash, maxiter=10)
#> $root
#> [1] -0.5-0.8660254i
#>
#> $f.root
#> [1] 2.220446e-16+1.110223e-16i
#>
#> $iter
#> [1] 6
#>
newton_raphson(+0.5+0i, g, gdash, maxiter=10)
#> $root
#> [1] 1+0i
#>
#> $f.root
#> [1] 0+0i
#>
#> $iter
#> [1] 7
#>