Notes 05
Notes 05
Flow chart:
no
INPUT OUTPUT
x n=Φ (x0 ) if |x n−x |>ε
x0 , ε 0 xn
yes
replace
x 0by x n
simple_iter:=proc(x,tol)
local xold,xnew,dif,phi;
# transcription of input
xold:=x;
# ensures that loop starts
dif:=tol+1;
# iterative map
phi:=z->cos(z);
# iteration loop
while dif>tol do
# iteration
xnew:=phi(xold);
# error estimate
dif:=abs(xnew-xold);
# transcription
xold:=xnew;
end do;
return xnew;
end proc:
b) Bisection algorithm
17
f
f(b)
Consider a (continuous) function f where
f (a) < 0 and f (b) > 0 (or vice versa). Then
f (x) = 0 for some x, a < x < b by the in- f(c)
a c b
0
termediate value theorem. How to compute
f(a)
such a value ?
Idea: Compute the midpoint c = (a + b)/2. If f (b) and f (c) have the same sign then x
is contained in (the smaller) interval [a, c]. If f (a) and f (c) have the same sign then x is
contained in [c, b]. Repeat the step with the smaller interval until its length is below the
required threshold. In each step the length of the intervals halves, i.e., after n steps the
length of the interval is |b − a|/2n . Thus the method always converges !
• If f (a) and f (c) have the same sign replace the lower limit a by c (to continue with
[c, b] !), else replace the upper limit b by c (to continue with [a, c] !)
• If the length of the interval |b − a| is larger than the threshold continue with step
3, else return the midpoint.
Flow chart:
INPUT
a,b,tol
yes a:=c
no b:=c yes
yes
OUTPUT
error
18
simple_bisection:=proc(a,b,tol)
local low,up,mid,f;
f:=x->cos(x)-x;
# transcription of input
low:=a;
up:=b;
# check for sign change
if f(up)*f(low)>0 then
error "f(a) f(b)>0";
end if;
# recursive bisection
while up-low>tol do
# midpoint
mid:=(up+low)/2;
# bisection update
if f(low)*f(mid)>0 then
low:=mid;
else
up:=mid;
end if;
end do;
# output
return (up+low)/2;
end proc:
Example 3.3: Suppose you start a bisection algorithm with initial interval [−2, 2] and
the algorithm returns 1/2. How many bisections have been performed?
The first bisection step computes the midpoint value 0. As our result is contained in [0, 2]
the algorithm continues with the interval [0, 2] and the second bisection step computes for
the midpoint the value 1. As our result is contained in [0, 1] the algorithm returns the
19
midpoint value 1/2, i.e., two bisection steps have been performed.
c) Newton-Raphson method
Graphically:
f
ϕ (x)=f(x0 )+f’(x )(x−x )
0 0
"first order approximation"
f(x )
0
x x
1 0
We want to compute x such that f (x) = 0. Suppose we know some initial guess x0 (close
to x). Replace f by the first order approximation ϕ and solve ϕ(x) = 0, i.e.,
f (x0 )
0 = f (x0 ) + f ′ (x0 )(x − x0 ) ⇒ x = x0 − = x1 .
f ′ (x0 )
Thus we obtain an improved value x1 . Repeat the procedure until the sequence converges,
i.e.,
f (xk )
xk+1 = Φ(xk ) = xk − .
f ′ (xk )
Iterative solution method with some “intelligent” iterative map Φ.
f (x∗ )
x∗ = Φ(x∗ ) = x∗ − ⇒ f (x∗ ) = 0
f ′ (x∗ )