using Plots
f = x -> (x.^2 - 2)
fp = x -> 2x
plot(f, linspace(-2,2,50))
"""
`newton`
=======
Find a point where f(x) = 0 via Newton's method.
-`x, hist = newton(f,fp,x0,delta,maxit)`
-`x = newton(f,fp,x0)` uses `delta = sqrt(eps(1.0)), maxit=100`
Runs newton's method starting from x0 where f evaluates
the function f and fp evaluates the derivative of the function
f. This runs for at most maxit iterations until `abs(f(x)) <= delta`
"""
function newton(f, fp, x, delta, maxit)
hist = zeros(maxit)
for i=1:maxit
fpx = fp(x)
step = f(x)/fpx
x = x - step
hist[i] = x
if abs(f(x)) < delta
hist = hist[1:i]
break
end
end
return x, hist
end
# default options
newton(f, fp, x) = newton(f, fp, x, sqrt(eps(1.0)), 100)
# show example
newton(f, fp, 1.0)
"""
`secant`
=======
Find a point where f(x) = 0 via the secant's method.
-`x, hist = secant(f,x0,delta,maxit)`
-`x = secant(f,x0)` uses `delta = sqrt(eps(1.0)), maxit=100`
Runs the secant method starting from x0 where f evaluates
the function f and x0 and x1 are two starting vectors that produce
an initial approximation of the derivative. This runs for at
most maxit iterations until `abs(f(x)) <= delta`
"""
function secant(f, x, delta, maxit)
hist = zeros(maxit)
# evaluate initial derivative
fpx = (f(x) - f(x - sqrt(eps(1.0))))/(sqrt(eps(1.0)))
for i=1:maxit
xold = x
step = f(x)/fpx
x = x - step
fpx = (f(x) - f(xold))/(-step) # could improve
hist[i] = x
if abs(f(x)) < delta
hist = hist[1:i]
break
end
end
return x, hist
end
# default options
secant(f, x) = secant(f, x, sqrt(eps(1.0)), 100)
# show example
secant(f, 1.0)
f1 = x -> x^2
f1p = x -> 2*x
xn,nhist = newton(f1, f1p, 1.0)
xs,shist = secant(f1, 1.0)
display(nhist)
display(shist)
f1 = x -> x^3 - x - 3
f1p = x -> 3*x^2 - 1.0
xn,nhist = newton(f1, f1p, 0.0)
xs,shist = secant(f1, 0.0)
display(nhist)
display(shist)