Anonymous user
Roots of a function: Difference between revisions
made local variable declarations explicit, added explicit types
(Scala contribution maintained.) |
(made local variable declarations explicit, added explicit types) |
||
Line 1,311:
Without the Roots package, Newton's method may be defined in this manner:
<lang Julia>function newton(f, fp, x::Float64,tol=1e-14::Float64,maxsteps=100::Int64)
##f: the function of x
##fp: the derivative of f
local xnew, xold = x, Inf
local fn, fo = f(xnew), Inf
local counter = 1▼
▲ counter = 1
while (counter < maxsteps) && (abs(xnew - xold) > tol) && ( abs(fn - fo) > tol )
x = xnew - f(xnew)/fp(xnew) ## update
xnew, xold = x, xnew
fn, fo = f(xnew), fn
end
if counter
error("Did not converge in ", string(maxsteps), " steps")
else
|