Session 03
Session 03
3 Session 3
A fundamental principle in computer science is iteration. As the name suggests, the process is
repeated until an answer is achieved.
Iterative techniques are used to find roots of equations, solutions of linear and nonlinear systems
of equations and solutions of differential equations.
We will now begin the analysis of two methods to find numerical approximations for the roots
of an equation: fixed point iteration method and bisection method of Bolzano.
We must start with a real function f (x), of variable x ∈ R, we want to find the roots of f , that
is, to solve the equation f (x) = 0.
The bisection method, based on Bolzano’s theorem, consists of finding an interval [a, b] where
f (a) and f (b) have opposite signs. Since the graph y = f (x) of a continuous function is
unbroken, it will cross the x-axis at a zero z = r that lies somewhere in the interval [a, b]. The
bisection method systematically moves the end points of the interval closer and closer together
until we obtain an interval of arbitrarily small width that brackets the zero. The decision step
for this process of interval halving is first to choose the midpoint of the interval [a, b], x = a+b
2
and then to analyze the three possibilities that might arise:
1. if f (a) and f (x) have opposite signs, a zero lies in [a, x].
2. if f (x) and f (b) have opposite signs, a zero lies in [x, b].
If either case (1) or (2) occurs, we have found an interval half as wide as the original interval
that contains the root, and we are “squeezing down on it”. By repeating the process, we can
obtain an interval as small as desired and, ultimately, bring the solution to the desired accuracy.
Since both the zero r and the midpoint x lie in the interval [a, b], the distance between them
can’t be greater than half the width of this interval, that is,
b−a
|x − r| ≤
2
.
b−a
By applying the method N times, the error bound should be 2N
, that is,
b−a
|x − r| ≤
2N
so a way to make sure that the maximum error was a preset amount E, would be to find N
such that
b−a
=E
2N
Numerical Methods (Department of Mathematics - EPI Gijón) Page 3.2
and after rounding to the nearest integer greater than or equal to it, if the result is not exact,
apply the bisection method N times.
Thus, a way to program all of the above, as a function in file, is to create the file bisection.m
with the commands
function x=bisection(f,a,b,E)
% Function x=bisection(f,a,b,E)
% Bisection method for solving f(x) = 0
% INPUT ARGUMENTS:
% f ........ Numerical function whose roots we want to find
% a,b ...... Endpoints of the interval where the roots are
% E ........ Absolute error bound
% OUTPUT ARGUMENT:
% x ........ Numerical approximation for the root
fa=f(a); fb=f(b);
if fa*fb>0
disp(’IT IS NOT POSSIBLE TO APPLY THE METHOD:’)
disp(’The function has the same sign on the endpoints’)
x=[];
elseif fa==0
disp(’Exact solution’), x=a;
elseif fb==0
disp(’Exact solution’), x=b;
else
N=ceil(log2(abs(b-a)/E)); % Number of iterations such that |x-r| <= E
N=max([N 1]); % The bisection method is applied at least once.
for n=1:N
x=(a+b)/2; fx=f(x);
if fx==0
disp(’Exact solution’), return
elseif fa*fx<0
b=x;
else
a=x;
end
end
end
where we have used the statements ceil (ceil(x) rounds x to the nearest integer greater than
or equal to x), max (max(v) returns the largest element in v) and return (return terminates
the execution of the function and returns control to the invoking function or to the keyboard).
Example 3.1 Find an approximation of the roots of f (x) = ex−2 − ln(x + 2) − 2x that lie in
the interval [−1, 6], with a maximum absolute error of 10−12 :
Solution: First, we define the function and represent it graphically, by running the statements
f =
@(x)exp(x-2)-log(x+2)-2*x
>> fplot(f,[-1 6]);
>> hold on; fplot(@(x) 0,[-1 6]);
and we see that the function has two roots, the first one lies in the interval [−1, 0] and the
second one in the interval [4, 5], so, to calculate with the required precision, we simply do:
>> x1=bisection(f,-1,0,1e-12)
x1 =
-0.2314
>> x2=bisection(f,4,5,1e-12)
x2 =
4.3575
This method is used to solve equations in the form g(x) = x, that is, geometrically, the
intersection of the curve y = g(x) with the bisector of the first quadrant y = x.
The idea is, from a starting value x0 , we construct the sequence xn+1 = g(xn ), n ∈ N; if the
assumptions of the fixed point theorem are fulfilled, this sequence converges to the desired
solution, regardless of the choice made of the starting value x0 (sometimes known as “seed”).
So, we construct the sequence and we use the following as a stopping criterion
|g(xn ) − xn | |xn+1 − xn |
= ≤T
|xn | |xn |
being T the relative error we want to use (known as tolerance). To prevent possible divisions by
numbers close to zero, we will raise it as |xn+1 − xn | ≤ T · |xn | and, thus, a way of programming
this method is to create the file fixpt.m as follows
Numerical Methods (Department of Mathematics - EPI Gijón) Page 3.4
function [x n]=fixpt(g,x0,T,N)
% Function [x n]=fixpt(g,x0,T,N)
% Fixed point iteration method for solving g(x)=x
% INPUT ARGUMENTS:
% f ........ Numerical function whose fixed point we want to find
% x0 ....... Starting value of the method
% T ........ Relative tolerance allowed
% N ........ Maximum number of iterations
% OUTPUT ARGUMENTS:
% x ........ Numerical approximation for the fixed point
% n ........ Number of iterations done
stopping_criterion=0; n=0; % n is the counter of iterations
while stopping_criterion==0 && n<N
n=n+1;
x=g(x0);
stopping_criterion=abs(x-x0)<=T*abs(x0);
x0=x;
end
if stopping_criterion==0
disp(’It does not converge with the requested accuracy.’)
disp(’The result is not the fixed point but the last iteration.’)
end
x2 − 1
Example 3.2 Check graphically that the function g(x) = has an unique fixed point in
3
the interval [−1, 1] and find it using the starting value x0 = 0 and the relative tolerance 10−6 .
Solution: We define the function g(x). We produce a graph of the function y = g(x) and the
line y = x in the interval [−1, 1],
we see that the fixed point is the intersection of both functions and there is only one point. We
can find the fixed point by typing:
>> x=fixpt(g,0,1e-6,100)
x =
-0.3028
Example 3.3 Create a function in file fixpt2, by modifying the function fixpt, so that the
output argument is a vector whose elements are the sequence of the fixed point iteration method.
Then, apply fixpt and fixpt2 to the function
r
x + 3 − x4
g(x) =
2
with starting value x0 = 1, relative tolerance 10−6 and maximum number of iterations 100.
function x=fixpt2(g,x0,T,N)
% Function x=fixpt2(g,x0,T,N)
% Fixed point iteration method for solving g(x)=x
% INPUT ARGUMENTS:
% f ........ Numerical function whose fixed point we want to find
% x0 ....... Starting value of the method
% T ........ Relative tolerance allowed
% N ........ Maximum number of iterations
% OUTPUT ARGUMENT:
% x ........ vector whose elements are the sequence of the fixed
% point iteration method and the last one is the solution
and
stopping_criterion=0; n=0; % n is the counter of iterations
x(1)=x0;
while stopping_criterion==0 && n<N
n=n+1;
x(n+1)=g(x(n));
stopping_criterion=abs(x(n+1)-x(n))<=T*abs(x(n));
end
x(1)=[]; % we remove the starting value x0
if stopping_criterion==0
disp(’It does not converge with the requested accuracy.’)
end
we check it with the same function as in the previous example
>> x=fixpt2(g,0,1e-6,100)
x =
Columns 1 through 7
-0.3333 -0.2963 -0.3041 -0.3025 -0.3028 -0.3028 -0.3028
Columns 8 through 10
-0.3028 -0.3028 -0.3028
Numerical Methods (Department of Mathematics - EPI Gijón) Page 3.6
>> x=fixpt(g,1,1e-6,100)
It does not converge with the requested accuracy.
The result is not the fixed point but the last iteration.
x =
0.9306
>> x=fixpt2(g,1,1e-6,100);
It does not converge with the requested accuracy.
we see that with 100 iterations we don’t find the fixed point, so we wonder if we are considering
enough iterations. However, if we look at the last six elements of the sequence
>> x(95:end)
ans =
1.2611 0.9306 1.2611 0.9306 1.2611 0.9306
It is clear that it is not worth to increase the iterations, since the sequence is not convergent
(the subsequence of even elements has different limit than the subsequence of odd elements).