Code
"""
newtonsys(f,jac,x1[;maxiter,ftol,xtol])
Use Newton's method to find a root of a system of equations, starting from 'x1'.
The functions 'f' and 'jac' should return the residual vector and the Jacobian
matrix, respectively. Returns the history of root estimates as a vector of
vectors.
The optional keyword parameters set the maximum number of iterations and the
stopping tolerance for values of 'f' and changes in 'x'.
"""
function newtonsys(f,jac,x1;maxiter=40,ftol=100*eps(),xtol=1000*eps())
x = [float(x1)]
y, J = f(x1), jac(x1)
delx = Inf # for initial pass below
k = 1
while (norm(delx) > xtol) && (norm(y) > ftol)
delx = -(J\y) # Newton step
push!(x, x[k] + delx) # append to history
k += 1
y, J = f(x[k]), jac(x[k])
if k == maxiter
@warn "Maximum number of iterations reached."
break
end
end
return x
endnewtonsys