0% found this document useful (0 votes)
23 views

Notes 05

Uploaded by

Ahsan Ramzan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Notes 05

Uploaded by

Ahsan Ramzan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

16

Flow chart:
no
INPUT OUTPUT
x n=Φ (x0 ) if |x n−x |>ε
x0 , ε 0 xn

yes
replace
x 0by x n

Loop (without counter)

Maple code (for f (x) = cos(x) − x = 0, Φ(x) = cos(x)):

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:

Problem: loops without a counter may run forever.

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 !

Structure of the code:

• Given: lower limit a, upper limit b, and threshold ε.

• Check whether f (a) and f (b) have opposite sign.

• Compute the midpoint c = (a + b)/2.

• 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 f(a) and f(c) no


f(a) and f(b) OUTPUT
have the c=(a+b)/2 have the |b−a|>tol
(a+b)/2
same sign same sign

no b:=c yes
yes

OUTPUT
error
18

Maple code (for f (x) = cos(x) − x = 0):

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

Remark: on Taylor series expansions



X f (k) (x0 ) f ′′ (x0 )
f (x) = (x − x0 )k = f (x0 ) + f ′ (x0 )(x − x0 ) + (x − x0 )2 + . . .
k=0
k! 2
f (x) = f (x0 ) + f ′ (x0 )(x − x0 ) + R(x, x0 )
| {z } | {z }
ϕ(x), linear in x remainder (“small”)

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 Φ.

Φ(x) = x − f (x)/f ′ (x) is called Newton-Raphson map. Properties of Φ (see §3a)

• “Fixed point condition”: if (x0 , x1 , x2 , . . .) converges, xn → x∗ then

f (x∗ )
x∗ = Φ(x∗ ) = x∗ − ⇒ f (x∗ ) = 0
f ′ (x∗ )

i.e., the limit is the desired solution.

You might also like