Last time¶
Forward and backward error
Computing volume of a polygon
Rootfinding examples
Use Roots.jl to solve
Introduce Bisection
Today¶
Limitations of bisection
Convergence classes
Intro to Newton methods
using Plots
default(linewidth=4)Recap From last time:¶
Stability demo: Volume of a polygon
X = ([1 0; 2 1; 1 3; 0 1; -1 1.5; -2 -1; .5 -2; 1 0])
R(θ) = [cos(θ) -sin(θ); sin(θ) cos(θ)]
Y = X * R(deg2rad(10))' .+ [1e4 1e4] # rotate it and translate it far away
plot(Y[:,1], Y[:,2], seriestype=:shape, aspect_ratio=:equal)using LinearAlgebra
function pvolume(X)
n = size(X, 1)
vol = sum(det(X[i:i+1, :]) / 2 for i in 1:n-1)
end
@show pvolume(Y)
[det(Y[i:i+1, :]) for i in 1:size(Y, 1)-1]
A = Y[2:3, :]
sum([A[1,1] * A[2,2], - A[1,2] * A[2,1]])
pvolume(Y) = 9.249999989226126
31285.71436703205Why don’t we even get the second digit correct? These numbers are only 108 and so shouldn’t we get about 8 digits correct?
Rootfinding¶
Given , find such that .
We’ll work with scalars ( and are just numbers) for now, and revisit later when they vector-valued.
Solve for
We aren’t given , but rather an algorithm f(x) that approximates it.
Sometimes we get extra information, like
fp(x)that approximatesIf we have source code for
f(x), maybe it can be transformed “automatically”
We saw:
Iterative bisection algorithm¶
f(x) = cos(x) - x
hasroot(f, a, b) = f(a) * f(b) < 0
function bisect_iter(f, a, b, tol)
hist = Float64[]
while abs(b - a) > tol
mid = (a + b) / 2
push!(hist, mid)
if hasroot(f, a, mid)
b = mid
else
a = mid
end
end
hist
endbisect_iter (generic function with 1 method)length(bisect_iter(f, -1, 3, 1e-20))56hist = bisect_iter(f, -1, 3, 1e-10)
r = hist[end] # What are we trusting?
hist = hist[1:end-1]
scatter( abs.(hist .- r), yscale=:log10)
ks = 1:length(hist)
plot!(ks, 4 * (.5 .^ ks))We saw that the error after bisections satisfies the bound
2. Convergence classes¶
A convergent rootfinding algorithm produces a sequence of approximations such that
where . For analysis, it is convenient to define the errors . We say that an iterative algorithm is -linearly convergent if
(The is for “quotient”.) A smaller convergence factor represents faster convergence.
A slightly weaker condition (-linear convergence or just linear convergence - the “r” here is for “root”) is that
for all sufficiently large when the sequence converges -linearly to 0.
# an example of q-convergence
ρ = 0.8
errors = [1.]
for i in 1:30
next_e = errors[end] * ρ
push!(errors, next_e)
end
plot(errors, yscale=:log10, ylims=(1e-10, 1))e = hist .- r
scatter(abs.(errors[2:end] ./ errors[1:end-1]), ylims=(0,1)) # clear q-convergence of errors, but what do you see with e?Poll 12.1: Convergence class of the bisection method¶
Is the Bisection Method:
A = q-linearly convergent
B = r-linearly convergent
C = neither
Remarks on bisection¶
Specifying an interval is often inconvenient
An interval in which the function changes sign guarantees convergence (robustness)
No derivative information is required
If bisection works for , then it works and gives the same accuracy for where .
Roots of even degree are problematic
A bound on the solution error is directly available
The convergence rate is modest - one iteration per bit of accuracy
Newton-Raphson Method¶
Much of numerical analysis reduces to Taylor series, the approximation
centered on some reference point .
In numerical computation, it is exceedingly rare to look beyond the first-order approximation
Since is a linear function, we can explicitly compute the unique solution of as
This is Newton’s Method (aka Newton-Raphson or Newton-Raphson-Simpson) for finding the roots of differentiable functions.
An implementation¶
function newton(f, fp, x0; tol=1e-8, verbose=false)
x = x0
for k in 1:100 # max number of iterations
fx = f(x)
fpx = fp(x)
if verbose
println("[$k] x=$x f(x)=$fx f'(x)=$fpx")
end
if abs(fx) < tol
return x, fx, k
end
x = x - fx / fpx
end
end
f(x) = cos(x) - x
fp(x) = -sin(x) - 1
newton(f, fp, 1; tol=1e-15, verbose=true)[1] x=1 f(x)=-0.45969769413186023 f'(x)=-1.8414709848078965
[2] x=0.7503638678402439 f(x)=-0.018923073822117442 f'(x)=-1.6819049529414878
[3] x=0.7391128909113617 f(x)=-4.6455898990771516e-5 f'(x)=-1.6736325442243012
[4] x=0.739085133385284 f(x)=-2.847205804457076e-10 f'(x)=-1.6736120293089505
[5] x=0.7390851332151607 f(x)=0.0 f'(x)=-1.6736120291832148
(0.7390851332151607, 0.0, 5)