Numerical Chapter2
Numerical Chapter2
Numerical Chapter2
1
Chapter 2
Non-linear Equation
Locating roots
consider an equation of the form f (x) = 0, assuming the function f (x) is
continuously differentiable function of sufficiently high order,we want to find
solution (roots of the equation) f (x) = 0. That is, we find numbers a such
that f (a) = 0. a is the point at which the graph of the function intersects
the x−axis.
The function can be algebraic equation such as polynomials, rationals or
transcendental equations: trigonometric, exponential, logarithmic, nth root,
etc.
This nonlinear function for example have infinitely many solutions but it is
difficult to find them
In most cases we have to use approximate solutions i.e. find a such that
|f (a)| < ε where ε is given tolerance which gives an interval where the root
is located but not the exact root. in this case the equation f (x) = 0 and
M.f (x) = 0 (where M is a constant) do not have the same roots
2
2.1 Bisection method
This is a simple but slowly convergent method for determining the roots of a
continuous function f (x). it is based on the intermediate value theorem for
f (x) in an interval [a0 , b0 ] for which if f has opposite signs at the end points
say f (a0 ) < 0 and f (b0 ) > 0, then f has a root in [a0 , b0 ]
a0 +b0
compute the mid point x0 = 2
Advantage:-
• it works if the equation has odd number of roots in the given interval.
Disadvantage:-
• The need for two initial guesses, one on each side of the root, is difficult
if we have no idea of the root, or if we have roots close to each other.
• Round off error can cause problems as xn approached the actual root
a.
Manalebish D: 3 3
f (0) = 1 > 0 f (1) = e − 3 < 0 and f (2) = e2 − 6 ≃ 1.39 > 0
Then there are roots on [0, 1] and [1, 2]
0+1
1. Let us take the first root between [a0 , b0 ] = [0, 1] with x1 = 2 = 0.5
Manalebish D: 4 4
a=c ;
end
f p r i n t f ( ’ \ n T h e r e f o r e , c=%.5 f \n Here , f ( c )=%.5 f ’ , c , f ( c ) )
if iter > 1
i f a b s ( c )>0
Ea=a b s ( ( c−c o l d )/ c ) ;
i f a b s ( Ea)<Es
break
end
end
end
c o l d=c ;
end
f p r i n t f ( ’ \ n Root o f g i v e n e q u a t i o n i s %f ’ , c )
Manalebish D: 5 5
Example 3 find the roots of f (x) = x2 −3 on the interval [1, 2] with absolute
error δ = 0.01
Manalebish D: 6 6
interval. i.e.
1. Set a0 = a, b0 = b
Example 4 Solve 2x3 − 25 x − 5 = 0 for the interval [1, 2] using false position
method with absolute error ε < 10−3
5
f (x) = 2x3 − x − 5
2
5 11
f (1) = 2 − − 5 = − < 0 f (1) < 0
2 2
f (2) = 16 − 5 − 5 = 6 > 0 f (2) > 0
11
f (1)f (2) = − 6 = −33 < 0
2
Manalebish D: 7 7
Then we look for the roots in [1, 2]
Let a0 = 1 b0 = 2
f (2)(1.47826) − f (1.47826)2
w2 = = 1.6198574
f (2) − f (1.47826)
Then f (w2 ) = f (1.6198574) = −0.548832
f (w1 )f (w2 ) = f (1.47826)f (1.6198574) = (−2.23489761)(−0.548832) > 0
Next set a2 = w2 and b2 = b1 = b0 = 2
f (2)(w2 ) − f (w2 )2
w3 = = 1.65171574
f (2) − f (w2 )
Then f (w3 ) = f (1.65171574) = −0.116983
f (w2 )f (w3 ) = (−0.548832)(−0.116983) > 0
Then set a3 = w3 and b3 = 2
f (2)(w3 ) − f (w3 )2
w4 = = 1.658376
f (2) − f (w3 )
f (w4 ) = −0.0241659
f (w3 )f (w4 ) > 0 continuing in this manner
w7 = 1.660085
f (w7 ) = −0.0002089
|f (1.660085)| < 0.001
Hence one of the root is 1.660085
Manalebish D: 8 8
% Find t h e r o o t s o f x ˆ3−2∗x−5=0 by u s i n g F a l s e P o s i t i o n Method .
f=@( x ) 2∗ x ˆ3 −(5/2)∗ x −5;
x0 =1; x1 =2; Es = 0 . 0 1 ;
i t e r =0;
w h i l e i t e r <1000
f p r i n t f ( ’ \ n Hence r o o t l i e s b e t w e e n (%.5 f ,%.5 f ) ’ , x0 , x1 )
x r =( f ( x1 ) ∗ x0−f ( x0 ) ∗ x1 ) / ( f ( x1)− f ( x0 ) ) ;
i f f ( x0 ) ∗ f ( x r )>0
x0=x r ;
e l s e x1=x r ;
end
i t e r = i t e r +1;
f p r i n t f ( ’ \ n T h e r e f o r e , c=%.5 f \n Here , f ( c )=%.5 f ’ , xr , f ( x r ) )
if iter > 1
i f a b s ( x r )>0
Ea=a b s ( ( xr−x r o l d )/ x r ) ;
i f a b s ( Ea)<Es
break
end
end
end
x r o l d=x r ;
end
Answer=x r
√
Exercise 1 Approximate 2 using f (x) = x2 − 2 = 0 on [0, 2] with error
δ < 0.01
Solution:-
Manalebish D: 9 9
[0, 2] f (0) = −2 < 0 and f (2) = 2 > 0
0(2) − 2(−2)
x1 = =1 f (1) = −1 < 0
2 − (−2)
1(2) − 2(−1) 4 4 2
[1, 2] x2 = = f =− <0
2 − (−1) 3 3 9
4 2
(2) − 2(− 9 ) 28
4
,2 x3 = 3 = = 1.4 f (1.4) = −0.04 < 0
3 2 − (− 29 ) 20
1.4(2) − 2(−0.04)
[1.4, 2] x4 = = 1.412 f (1.4) = −0.006256 < 0
2 − (−0.04)
Manalebish D: 10 10
x1 − x0
This chord intersects the x-axis at x2 where x2 = x1 −f (x1 )
f (x1 ) − f (x0 )
xn−1 − xn−2
In general, for xn = xn−1 − f (xn−1 )
f (xn−1 ) − f (xn−2 )
If f (xn ) = 0 xn is a root
If f (xn ) < 0 take the interval [an+1 , bn+1 ] = [xn , bn ]
If f (xn ) > 0 take the interval [an+1 , bn+1 ] = [an , xn ]
continue the process until f (xn ) = 0 or until |f (xn )| < δ
Algorithm for secant method
To determine a root f (x) = 0 given the values x0 and x1 that are near
the root,
if |f (x0 )| < |f (x1 )| Then swap x0 with x1
Repeat
x1 − x0
set x2 = x1 − f (x1 )
f (x1 ) − f (x0 )
set x0 = x1
set x1 = x2
until |f (x2 )| <tolerance value
Manalebish D: 11 11
Remark 2.3.1 Possible matlab algorith for this question
%f i n d r o o t s o f xˆ3+x−1 u s i n g s e c a n t method
f=@( x ) 2∗ xˆ3+x−8 ;% d e f i n e t h e f u n c t i o n
a (1)=0;% a s s i g n l o w e r i n i t i a l p o i n t
a (2)=2;% a s s i g n u p p e r i n i t i a l p o i n t
n=0.001; % r e l a t i v e error al lo we d
f o r i =3:10;
f p r i n t f ( ’ \ n Hence r o o t l i e s b e t w e e n (%.6 f ,%.6 f ) ’ , a ( i −2) , a ( i −1))
a ( i )=a ( i −1)−( f ( a ( i − 1 ) ) ) ∗ ( ( a ( i −1)−a ( i −2))/( f ( a ( i −1))− f ( a ( i − 2 ) ) ) ) ;
i f a b s ( ( a ( i )−a ( i −1))/ a ( i ) ) ∗ 1 0 0 0 < n
r o o t=a ( i )
break
end
f p r i n t f ( ’ \ n T h e r e f o r e , a ( i )=%.6 f \n Here , f ( a ( i ))=%.6 f ’ , a ( i ) , f ( a ( i ) ) )
end
√
Exercise 2 Approximate 2 using f (x) = x2 − 2 = 0 on [0, 2] with error
δ < 0.01
Solution:-
and so on
Manalebish D: 12 12
2.4 Iteration method:-
we choose an arbitrary initial guess x0 (usually with rough graphical methods
) and try to improve the guess by repeating (iterating) a few steps again and
again so that we approach the root a.
we write the equation f (x) = 0 in the form x = g(x) where g is defined
in some interval I containing a and the range of g lies in I for x ∈ I.
Compute successively
x1 = g(x0 ) (2.1)
x2 = g(x1 ) (2.2)
x3 = g(x2 ) (2.3)
..
. (2.4)
xn+1 = g(xn ) (2.5)
Manalebish D: 13 13
x2 + 4
Let’s take x0 = 2 and x = g(x) = . Then
5
22 + 4 8
x1 = g(x0 ) = = = 1.6
5 5
2
(1.6) + 4
x2 = g(1.6) = = 1.312
5
(1.312)2 + 4
x3 = g(1.312) = = 1.144
5
(1.144)2 + 4
x4 = g(1.144) = = 1.062
5
x5 = 1.025 x6 = 1.010 x7 = 1.004
12
y=(x2+4)/5
10
6
y=x
−2
−4
−3 −2 −1 0 1 2 3 4 5 6 7
Note 1 From the graph convergence depends on the fact that in a neighbor-
hood I of a solution, the slope of the curve y = g(x) is less than the slope of
y = x(i.e. |g ′ (x)| < 1 for x ∈ I).
Manalebish D: 14 14
Remark 2.4.1 Possible algorithm for this particular question is
%F i x e d p o i n t i t e r a t i o n Method
%f i n d r o o t s o f xˆ2−5x+4 u s i n g F i x e d p o i n t i t e r a t i o n method and p l o t e r r o r
f = @( x ) ( x ˆ2+4)/5;% d e f i n i n g a f u n c t i o n
% starting value
x c u r r e n t = 2;%
i t e r = 0;% c o u n t t h e i t e r a t i o n s , s e t t i n g a maximum i n m a x i t e r
maxiter = 100;
xArray = NaN( 1 , m a x i t e r );% i n i t i a l i z e t h e a r r a y t o s t o r e our i t e r a t i o n s
% convergence tolerance
xtol = 0.001;
% b e f o r e we s t a r t , t h e e r r o r i s s e t t o be BIG . t h i s
% j u s t l e t s our w h i l e l o o p g e t t h r o u g h t h a t f i r s t i t e r a t i o n
xerr = inf ;
% the while w i l l stop i f either criterion f a i l s
w h i l e ( i t e r < m a x i t e r ) && ( x e r r > x t o l )
i t e r = i t e r + 1;
xnew = f ( x c u r r e n t ) ;
% s a v e e a ch i t e r a t i o n
xArray ( i t e r ) = xnew ;
% compute t h e d i f f e r e n c e b e t w e e n s u c c e s s i v e i t e r a t i o n s
x e r r = a b s ( xnew − x c u r r e n t ) ;
x c u r r e n t = xnew ;
end
% r e t a i n o n l y t h e e l e m e n t s o f xArray t h a t we a c t u a l l y g e n e r a t e d
xArray = xArray ( 1 : i t e r ) ;
f p r i n t f ( ’ % . 5 f \n ’ , xArray ) ;
x2 + 1
Exercise 3 Find solutions of f (x) = x2 −3x+1 = 0 using x = g(x) =
3
and x0 = 1, 2, 3
Manalebish D: 15 15
solution of x = g(x) and suppose that g has a continuous derivative in some
interval I, containing a. Then if |g ′ (x)| ≤ α < 1 in I, then the iteration
process xn+1 = g(xn ) converges for any x0 ∈ I.
Manalebish D: 16 16
2x −5 5 −5 5
|g ′ (x)| < 1 ⇔ −1 < <1⇔ < x < i.e. I = ,
5 2 2 2 2
4
Thus x0 = 2 ∈ I with g ′ (2) = close to 1 Thus xn → a1 = 1 slowly where
5
as x0 = 5 ̸∈ I need not converge to a2 = 4.
Manalebish D: 17 17
Example 8 Find the roots of 4x − 4 sin x = 1
write f (x) = 0 as x = sin x + 0.25 = g(x) with |g ′ (x)| = | cos x| < 1 say for
1 < x < 1.3 choosing x0 = 1.2 we have
x1 = 0.3̇
x2 = 0.465
x3 = 0.53
.
= ..
x7 = 0.610
But a2 ̸∈ I suppose x0 = 2
x1 = 2.46
x2 = 3.91
x3 = 16.7
diverges
Manalebish D: 18 18
So to obtain a2 , take x = g(x) = 4x − ex with g ′ (x) = 4 − ex
Hence convergent
Manalebish D: 19 19
f at the point (x0 , f (x0 )).
Let x1 be the point of intersection of the tangent line with the x-axis.
′ ′′ (xn+1 − xn )2
f (xn+1 ) = f (xn ) + f (xn )(xn+1 − xn ) + f (xn ) + ···
2!
If xn+1 is close to the actual root a, f (xn+1 ) = 0 and xn ≈ xn+1 so that
(xn+1 − xn )2 and all higher powers can be neglected which gives
Manalebish D: 20 20
Solution:-
f ′ (x) = ex − 3
f (xn )
xn+1 = xn − ′
f (x )
xnn
e − 3xn xn (xn − 1)
= xn − = e
exn − 3 exn −3
e0 (0 − 1) −1
for x0 = 0 x1 = = = 0.5
e0 − 3 −2
e0.5 (0.5 − 1)
x2 = = 0.61
e0.5 − 3
x3 = 0.619
e2 (2 − 1)
for x0 = 2 x1 = = 1.6835
e2 − 3
x2 = 1.5435
x3 = 1.5134
x4 = 1.512
i t e r = i t e r +1;
Manalebish D: 21 21
if iter > 1
i f a b s ( x )>0
Ea=a b s ( ( x−x o l d )/ x ) ;
i f a b s ( Ea)<Es
break
end
end
end
x o l d=x ;
end
Solution:-
Manalebish D: 22 22