Lecture 9 Root Finding:
Solution of Nonlinear Functions
Prof. M. Moness
Minia University
Department of Computers and Systems Engineering
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 1
Issues
– Definitions
– Classification of Methods
• Analytical Solutions
• Graphical Methods
• Numerical Methods
– Bracketing Methods
– Open Methods
§ 9.1 Introduction
• Definition :Root Finding Problems
• Many problems in Science and Engineering are expressed as:
These problems are called root finding problems.
A number r that satisfies an equation is called a root of the equation, e.g.,
This image cannot currently be displayed.
) 1 ( ) 3 )(
2 ( 18 15 7
3
. 1 ,
3 , 3 , 2
18 15
7 3
2 2
3 4
2 3 4
x x
x x
x x x
and
x x
x x
i.e.,
: roots four has
: equation The
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 3
4
Solution Methods
Several ways to solve nonlinear equations are possible:
– Analytical Solutions
• Possible for special equations only – Graphical Solutions
• Useful for providing initial guesses for other methods
– Numerical Solutions
• Bracketing methods
• Open methods
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
5
Analytical Methods
Analytical Solutions are available for special equations only.
if f(x) is a polynomial of degree 5 or higher, there is no such formula.
Also for nonlinear functions:
a ac b
roots b
c x b x a
2 4
0 2
2
:
of solution Analytical
0
e x x
: for available
is solution analytical
No
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
• Another example, consider the following function:
F(x) = 48x(1 + x) 60 ‐ (1 + x) 60 + 1 = 0
• What are the roots (solutions) of f(t), i.e., values x i , I = 1,, 2, …. , 60 such that f(x i )=0.
– How would you solve such an equation?
HIGH‐DEGREE POLYNOMIALS:
• As we mentioned before, for a quadratic equation : ax 2 + bx + c = 0, there is a well‐ known formula for the roots.
• For third‐ and fourth‐degree equations, there are also formulas for the roots.
– However, they are extremely complicated.
If f (x) is a polynomial of degree 5 or higher, there is
no such formula.
7
Graphical Methods
Graphical methods are useful to provide an initial guess to be used by other methods.
0.6
root root The
e x Solve
x
] 1 , 0 [
e x
x
Root
1 2 2
1
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
• Likewise, there is no formula that will enable us to find the exact roots of a transcendental equation such as :
cos x = x.
• APPROXIMATE SOLUTION
• We can find an approximate solution by plotting the left side of the equation.
• Using a graphing device, and after experimenting with viewing rectangles,
we produce the graph shown.
Another examples are the transcendental equations
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 8
• We see that, in addition to the solution x = 0, which
doesn’t interest us, there is a solution between 0.007 and 0.008
– Zooming in shows that the root is approximately 0.0076
• If we need more accuracy, we could zoom in repeatedly.
• That becomes tiresome, though.
Zooming in
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 9
Numerical Methods
A faster alternative is to use a numerical root finder on a calculator or computer algebra system.
Many methods are available to solve nonlinear equations:
Bisection Method
Newton’s Method
Secant Method
– False position Method – Muller’s Method – Bairstow’s Method – Fixed point iterations – ……….
Numerical methods are classified as either
bracketing method or open methods
11
Bracketing Methods
• In bracketing methods, the method starts with an interval that contains the root and a procedure is used to obtain a smaller interval containing the root.
• Examples of bracketing methods:
– Bisection method – False position method
Open Methods
• In the open methods, the method starts with one or more initial guess points. In each iteration, a new guess of the root is obtained.
• Open methods are usually more efficient than bracketing methods.
• They may not converge to a root.
In both cases, initial guesses are required
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
§ 9.2 Bracketing Methods
• Both bisection and false‐position methods require the root to be bracketed by the endpoints.
• How to find the interval (endpoints)?
* plotting the function
* incremental search
* trial and error
• We observed that f (x) Changed sign on
opposite sides of the root.
In general, if f(x) is real and continuous in the interval from x
lto x
uand f(x
l) and f(x
u) have opposite signs, that is f(x
l) f(x
u) < 0
Then there is at least one real root between f(x l ) and f(x u )
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 12
• Given some function, find location where f(x)=0
• Need:
Starting position x
0, hopefully close to a solution
Ideally, points that bracket the root
Well‐behaved function f(x
+) > 0
f ( x
–) < 0
Incremental Search:
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 13
Intermediate Value Theorem
• Let f(x) be defined on the interval [a,b].
• Intermediate value theorem:
if a function is continuous and f(a) and f(b) have different signs then the function has at least one zero in the interval [a,b].
a
b f(a)
f(b)
15
Examples
• If f(a) and f(b) have the same sign, the function may have an even number of real zeros or no real zeros in the interval [a, b].
• Bisection method can not be used in these cases.
a b
a b
The function has four real zeros
The function has no real zeros
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
Design a MATLAB function that implemental search (Steven Chapra, page 160)
function xb = incsearch(func,xmin,xmax,ns)
% incsearch: incremental search root locator
% xb = incsearch(func,xmin,xmax,ns):
% finds brackets of x that contain sign changes
% of a function on an interval
% inputs:
% func = name of function
% xmin, xmax = endpoints of interval
% ns = number of subintervals (default = 50)
% output:
% xb(k,1) is the lower bound of the kth sign change
% xb(k,2) is the upper bound of the kth sign change
% If no brackets found, xb = [].
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 16
% Check for input arguments if nargin < 3
error('at least 3 arguments required') end
if nargin < 4 ns = 50;
end %if ns blank set to 50
% Incremental search
x = linspace(xmin,xmax,ns);
f = func(x);
nb = 0; xb = []; %xb is null unless sign change detected
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 17
for k = 1:length(x)-1
if sign(f(k)) ~= sign(f(k+1)) %check for sign change nb = nb + 1;
xb(nb,1) = x(k);
xb(nb,2) = x(k+1);
end end
if isempty(xb) %display that no brackets were found disp('no brackets found')
disp('check interval or increase ns') else
disp('number of brackets:') %display number of brackets disp(nb)
end
Example :Use the previous M-file incsearch to identify brackets within the interval [3, 6] for the function:
f(x) = sin(10x) + cos(3x) Solution:
incsearch(@(x) sin(10*x)+cos(3*x), 3 , 6)
>> incsearch(@(x) sin(10*x)+cos(3*x), 3 , 6) number of brackets:
5 ans =
3.2449 3.3061 3.3061 3.3673 3.7347 3.7959 4.6531 4.7143 5.6327 5.6939
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 19
3 3.5 4 4.5 5 5.5 6
x
-2-1.5 -1 -0.5 0 0.5 1 1.5 2
f(x)
f(x)=sin(10x)+cos(3x)
missed missed
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 20
If we increase increase subintervals to 200
xb=incsearch(inline('sin(10*x)+cos(3*x)'),3,6,200) number of brackets:
9 xb =
3.2563 3.2714 3.3618 3.3769 3.7387 3.7538 4.2211 4.2362 4.2513 4.2663 4.7035 4.7186 5.1558 5.1709 5.1859 5.2010 5.6683 5.6834
Find all 9 roots!
3 3.5 4 4.5 5 5.5 6
x -2
-1.5 -1 -0.5 0 0.5 1 1.5 2
f(x)
f(x)=sin(10x)+cos(3x)
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 21
§ 9.1.1The Bisection Method
• It is the simplest root‐finding algorithm.
• requires previous knowledge of two initial guesses, a and b, so that f(a) and f(b) have opposite signs.
• Two estimates are chosen so that the solution is bracketed.
i.e., f(a) and f(b) have opposite signs.
• In the diagram this f(a) < 0 and f(b) > 0.
• The root d lies between a and b!
initial pt ‘a’
initial pt ‘b’
a
root ‘d’
d c
b
• The root d must always be bracketed (be
between a and b)!
• Iterative method.
23
MORE:
• The Bisection method is one of the simplest methods to find a zero of a nonlinear function.
• It is also called interval halving method.
• To use the Bisection method, one needs an initial
interval that is known to contain a zero of the function.
• The method systematically reduces the interval. It does this by dividing the interval into two equal parts,
performs a simple test and based on the result of the test, half of the interval is thrown away.
• The procedure is repeated until the desired interval size is obtained.
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
The Bisection Method (Cont.)
• The interval between a and b is halved by calculating the average of a and b.
• The new point c = (a+b)/2.
• This produces are two
possible intervals: a < x < c and
c < x < b .
initial pt ‘a’initial pt ‘b’
a
root ‘d’
d c
b
• If f(c ) > 0, let a new = a and b new = c and repeat process.
• If f(c ) < 0, let a new = c and b new = b and repeat process.
• This reassignment ensures the root is always bracketed!!
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 24
Bisection Method : Algorithm
Step 1 : Choose and such that and bracket the root, i.e. f( )*f( ) < 0.
Step 2 : Estimate the root (bisection),
= 0.5*( + )
Step 3: Determine the new bracket, If f( )*f( ) < 0
= and still the same else
= and still the same end
Step 4: Examine if is the root, i.e., ) = , where is the error (required accuracy), if not continue.
Step 5 : Repeat steps 2 and 3 until convergence
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 25
Flow Chart of Bisection Method
Start: Given a,b and ε u = f(a) ; v = f(b) c = (a+b) /2 ; w = f(c)
is u w <0
a=c; u= w b=c; v= w
is (b-a) /2<ε yes
yes
no Stop
no
Error
• Convergence rate:
– Error bounded by size of [x a … x b ] interval – Interval shrinks in half at each iteration – Therefore, error cut in half at each iteration:
| n+1 | = ½ | n | – This is called “linear convergence”
– One extra bit of accuracy in x at each iteration
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 27
Error Estimates for Bisection
At the iter1:
∗
length of the interval
12 16
‐34.8 17.6
16
‐12.6 17.6
12
14 15
‐12.6 1.5
‐34.8
Change of sign
Change of sign
17.6 Change
of sign
Iter1
Iter2
14
16
∗
True root live inside this interval
∗
∆ ∆
At the iter2:
∗
length of the interval
∗
∆ ∆
At the nth iteration:
∗
length of the interval
∗
∆ ∆
∗
the absolute error in the n-th iteration
Error Estimates for Bisection
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 28
Suppose is a continuous function defined on the interval , , with
and of opposite sign. The Intermediate Value Theorem implies that a number exists in , with
.
This technique is based on the Intermediate Value Theorem
Show that
has a root in [12, 16]
Example:
12 16
. .
Solution:
Since there is a change in sign, then f(x) has a root in the rang [12,16]
CSE 121, Prog. Lang.2, Prof. M. Moness,2020 29
Use Bisection method to find the root of the function
in [12, 16]
Example:
16
‐12.6 17.6
12
14 15
‐12.6 1.5
14.5 151.5
1 14.0000000000 2 15.0000000000 3 14.5000000000 4 14.7500000000 5 14.8750000000 6 14.9375000000 7 14.9062500000 8 14.8906250000 9 14.8984375000 10 14.9023437500 11 14.9003906250 12 14.8994140625 13 14.8999023438 14 14.9001464844 15 14.9000244141
‐34.8
Change of sign
Change of sign
17.6
Change of sign
Iter1
Iter2
Iter3
14
True root: ∗ 14.9
16
14
12 16
‐34.8 17.6
16
‐12.6 17.6
12
14 15
‐12.6 1.5
14.5 151.5
‐34.8
Change of sign
Change of sign
17.6
Change of sign
Change of sign
Iter1
Iter2
Iter3
14
16
14
∆
∆
∆
∆
At the n
thiteration:
endpoints of the inrteval
∆ ,
Length of the interval
∆
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 31
If
,is the desired error, this equation can be solved for
Error Estimates for Bisection
. n must be an integer !
1 14.0000000000 ‐9.0000000000e‐01 2 15.0000000000 1.0000000000e‐01 3 14.5000000000 ‐4.0000000000e‐01 4 14.7500000000 ‐1.5000000000e‐01 5 14.8750000000 ‐2.5000000000e‐02 6 14.9375000000 3.7500000000e‐02 7 14.9062500000 6.2500000000e‐03 8 14.8906250000 ‐9.3750000000e‐03 9 14.8984375000 ‐1.5625000000e‐03 10 14.9023437500 2.3437500000e‐03 11 14.9003906250 3.9062500000e‐04 12 14.8994140625 ‐5.8593750000e‐04 13 14.8999023438 ‐9.7656250000e‐05 14 14.9001464844 1.4648437500e‐04 15 14.9000244141 2.4414062500e‐05 16 14.8999633789 ‐3.6621093750e‐05
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 32
Bisection Method : function file
• What should do?
You pass the function required (func) along with lower (xl) and upper (xu) guesses.
an optional stopping criterion (es) and maximum iterations (maxit) can be entered.
The built‐in function first checks whether there are sufficient arguments and if the initial guesses bracket a sign change.
If not, an error message is displayed and the function is terminated.
It also assigns default values if maxit and es are not supplied.
Then a while...break loop is employed to implement the bisection algorithm until the approximate error falls below es or the iterations exceed maxit.
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 33
Bisection Method : function file (Chapra, page 151)
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
% bisect: root location zeroes
% [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% fx = function value at root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3
error('at least 3 input arguments required') end
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0
error('no sign change') end
if nargin<4||isempty(es) es=0.0001;
end
if nargin<5||isempty(maxit) maxit=50;
End
iter = 0; xr = xl; ea = 100;
CSE 121, Prog. Lang.2, Prof. M. Moness,2020 35
while (1)
xrold = xr; xr = (xl + xu)/2; iter = iter + 1;
if xr ~= 0
ea = abs((xr ‐ xrold)/xr) * 100;
end
test = func(xl,varargin{:})*func(xr,varargin{:});
if test < 0 xu = xr;
elseif test > 0 xl = xr;
else ea = 0;
end
if ea <= es || iter >= maxit break
end end
root = xr; fx = func(xr, varargin{:})
CSE 121, Prog. Lang.2, Prof. M. Moness,2020 36
The Bisection Method Stopping Criterion : version 1
function [xr,err,yc,iter,x]=bisect_ver1(f,a,b,es)
%Input: f is the function, a, b are endpts
% es is the tolerance, imax is max iter
%Output: c is the zero, yc= f(c) ya=f(a); yb=f(b); iter =0;
if ya*yb > 0,return,end for k=1:1000
iter = iter +1;
xr=(a+b)/2; yc=f(xr); x(k)=xr;
if yc==0
a=xr; b=xr;
elseif yb*yc>0 b=xr; yb=yc;
else
a=xr; ya=yc;
end
if b‐a < es, break,end end
xr=(a+b)/2; err=abs(b‐a); yc=f(xr);
CSE 121, Prog. Lang.2, Prof. M. Moness,2020 37
Example: Use Bisection method to find the root of the
function in the interval [1,16]
Using bisect_ver1 function :
>> f ‐@(x)(10*x^6‐149*x^5+10*x‐149)/((10*(x^4+1)))
>> xl=12;xu=16;es=0.001;
>> [xr,err,yc,iter,x]=bisect_ver1(f,xl,xu,es) xr =
14.8999 err =
9.7656e‐04 yc =
‐0.0015 iter =
12 x =
Columns 1 through 11
14.0000 15.0000 14.5000 14.7500 14.8750 14.9375 14.9063 14.8906 14.8984 14.9023 14.9004
Column 12
The Bisection Method Stopping Criterion : version 2 function
[xr,err,yc,iter,x]=bisect_ver2(f,a,b,es)
%Input: f is the function, a, b are endpts
% es is the tolerance, imax is max iter
%Output: c is the zero, yc= f(c) ya=f(a); yb=f(b); iter =0;
if ya*yb > 0,return,end
max1=1+round((log(b‐a)‐log(es))/log(2));
for k=1:max1 iter = iter +1;
xr=(a+b)/2; yc=f(xr); x(k)=xr;
if yc==0
a=xr; b=xr;
elseif yb*yc>0 b=xr; yb=yc;
else
a=xr; ya=yc;
end
% if b‐a < es, iter=k; break,end end
xr=(a+b)/2; err=abs(b‐a); yc=f(xr);
Stopping Criteria
>>
[xr,err,yc,iter,x]=bisect_ver2(f,xl,xu,es) xr =
14.9001 err =
4.8828e‐04 yc =
0.0022 iter =
13 x =
Columns 1 through 11
14.0000 15.0000 14.5000 14.7500 14.8750 14.9375 14.9063 14.8906 14.8984 14.9023 14.9004
Columns 12 through 13 14.8994 14.8999
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 39
40
Advantages and Disadvantages of Bisection Method
Advantages
• Simple and easy to implement
• One function evaluation per iteration
• The size of the interval containing the zero is reduced by 50% after each iteration
• The number of iterations can be determined a priori
• No knowledge of the derivative is needed
• The function does not have to be differentiable Disadvantage
• Slow to converge
• Good intermediate approximations may be discarded
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
§ 9.2 Open Methods
• There exist open methods which do not require bracketed intervals
• Newton‐Raphson method, Secant Method, Muller’s method, fixed‐point iterations
• First one to consider is the fixed‐point method
• Converges faster but not necessary converges
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 41
• Newton and Newton-Raphson are just different names for the same method.
• King of the root‐finding methods
• Newton‐Raphson method Based on Taylor series expansion
Newton's Method
Given an initial guess of the root x
0, Newton‐Raphson method uses information about the function and its derivative at that point to find a better guess of the root.
Assumptions:
– f(x) is continuous and the first derivative is known – An initial guess x
0such that f’(x
0)≠0 is given
2 i 1 i i
1 i i i
1
i
x x
! 2 x f
x x f x f x
f
Truncate the Taylor series to get
x i f x i f x i x i x i
f 1 1
At the root, f ( x i+1 ) = 0 , so
x i f x i x i x i
f
1
0
i i
i
i f x
x x f
x 1
Newton’s Method : Cont.
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 43
root
x*
0 4 3 )
( x x 4 x f
Newton’s Method : illustration
False position - secant line Newton’s method - tangent line
x i x i+1
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 44
Newton’s Method
• Note that an evaluation of the derivative (slope) is required
• You may have to do this numerically
• Open Method – Convergence depends on the initial guess (not guaranteed)
• However, Newton method can converge very quickly (quadratic convergence)
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 45
Newton’s Method : Algorithm
• Step 1 : Start at the point (x 1 , f(x 1 )).
• Step 2 : The intersection of the tangent of f(x) at this point and the x‐axis.
x 2 = x 1 ‐ f(x 1 )/f `(x 1 )
• Step 3: Examine if f(x 2 ) = 0
or abs(x 2 ‐ x 1 ) < tolerance,
• Step 4: If yes, solution x r = x 2
If not, x 1 x 2 , repeat the iteration.
47
Newton’s Method : Graphical Depiction ‐
• If the initial guess at the root is x i , then a tangent to the
function of x i that is f’(x i ) is extrapolated down to the x‐axis to provide an
estimate of the root at x i+1 .
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
• Consider the tangent line L to the curve y = f(x) at the point (x 1 ,f(x 1 )) and look at the x‐intercept of L, labeled x 2 .
• Here’s the idea behind the method.
– The tangent line is close to the curve.
– So, its x‐intercept, x
2, is close to the x‐intercept of the curve (namely, the root r that we are seeking).
– As the tangent is a line, – we can easily
find its x‐intercept
NEWTON’S METHOD
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 48
• To find a formula for x 2 in terms of x 1 , we use the fact that the slope of L is f’(x 1 ).
• So, its equation is:
y ‐ f(x 1 ) = f’(x 1 )(x ‐ x 1 )
• As the x‐intercept of L is x 2 , we set y = 0 and obtain:
• 0 ‐ f(x 1 ) = f’(x 1 )(x 2 ‐ x 1 )
• If f’(x 1 ) ≠ 0, we can solve this equation for x 2 :
• We use x 2 as a second approximation
• to r.
NEWTON’S METHOD
1
2 1
1
( ) '( ) x x f x
f x
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 49
• Next, we repeat this procedure with x 1 replaced by x 2 , using the tangent line at (x 2 ,f(x 2 )).
This gives a third approximation:
• If we keep repeating this process, we obtain a sequence of approximations
x 1 , x 2 , x 3 , x 4 , . . .
3 2
2 2
( )
'( ) x x f x
f x
THIRD APPROXIMATION
• In general, if the nth approximation is x n and f’(x n ) ≠ 0, then the next approximation is given by:
• If the numbers x n become closer and closer to r as n
becomes large, then we say that the sequence converges to r and we write:
• The sequence of successive approximations converges to the desired root for functions of the type illustrated in the previous figure. However, in certain circumstances, it may not converge.
1
( )
'( )
n
n n
n
x x f x
f x
SUBSEQUENT APPROXIMATION
lim
nn
x r
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 51
• Consider the situation shown here.
• You can see that x 2 is a worse approximation than x 1 . – This is likely to be the case when f’ (x
1) is close to 0.
It might even happen that an approximation falls outside the domain of f, such as x 3 .
– Then, Newton’s method fails.
– In that case, a better initial approximation x
1should be chosen.
NON‐CONVERGENCE
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 52
A Simple MATLAB Program
end
x f
x x f
x
n i for
x f n Assumputio
x x f x f Given
i i i
i
' ( )
) ( : 0
__
__________
__________
0 ) ( '
), ( ' ), (
1
0 0
end
X FP X F X X i for X
PROGRAM MATLAB
) ( / ) ( 5 : 1 4
%
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 53
• Example 1:
• find the third approximation x 3 to the root of the equation x 3 – 2x – 5 = 0
• Starting with x 1 = 2,
• We apply Newton’s method with
f(x) = x 3 – 2x – 5 and f’(x) = 3x 2 – 2
– Newton himself used this equation to illustrate his method.
– He chose x
1= 2 after some experimentation
because f(1) = ‐6, f(2) = ‐1 and f(3) = 16
55
Example (Cont.)
2130 . 0742 2 . 9
0369 . 4375 2 . ) 2 ( '
) (
4375 . 16 2 3 9 ) ( '
) (
33 3 4 33 ) ( '
) ( 1
4 3 '
2 2 2
3
1 1 1
2
0 0 0
1 2
x f
x x f
x
x f
x x f
x
x f
x x f
x x x (x) f
: 3 Iteration
: 2 Iteration
: 1 Iteration
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
56
Example
k(Iteration)
x
kf(x
k) f’(x
k) x
k+1|x
k+1–x
k|
0 4 33 33 3 1
1 3 9 16 2.4375 0.5625
2 2.4375 2.0369 9.0742 2.2130 0.2245
3 2.2130 0.2564 6.8404 2.1756 0.0384 4 2.1756 0.0065 6.4969 2.1746 0.0010
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
57
Disadvantages of Newton’s Method
• If the initial guess of the root is far from the root the method may not converge.
• Newton’s method converges linearly near multiple zeros { f(r) = f’(r) =0 }. In such a case, modified algorithms can be used to regain the quadratic convergence.
CSE 121, Prog. Lang.2, Prof. M. Moness, 2020
Example 2:
Use Newton’s method to find correct to eight decimal places.
– First, we observe that finding is equivalent to finding the positive root of the equation x
6– 2 = 0
– So, we take f(x) = x
6– 2 – Then, f’(x) = 6x
5So, Formula of Newton’s method becomes:
Choosing x
1= 1 as the initial approximation, we obtain:
As x
5and x
6agree to eight decimal places, we conclude that
to eight decimal places.
6
2
6
2
6
1 5
2 6
n
n n
n
x x x
x
2 3 4 5
1.16666667 1.12644368 1.12249707 1.12246205 x
x x x
6
2 1.12246205
Example 3:
Find, correct to six decimal places, the root of the equation cos x = x.
– We rewrite the equation in standard form: cos x – x = 0 – Therefore, we let f(x) = cos x – x
– Then, f’(x) = –sinx – 1
So, Newton’s formula becomes:
1
cos
sin 1
cos
sin 1
n n
n n
n
n n
n
n
x x
x x
x
x x
x x
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 59
• To guess a suitable value for x 1 , we sketch the graphs of y = cos x and y = x.
– It appears they intersect at a point whose x‐coordinate is somewhat less than 1.
Example 3 : Cont.
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 60
• So, let’s take x 1 = 1 as a convenient first approximation.
– Then, remembering to put our calculator in radian mode, we get:
– As x
4and x
5agree to six decimal places (eight, in fact), we conclude
that the root of the equation, correct to six decimal places, is 0.739085
2 3 4 5
0.75036387 0.73911289 0.73908513 0.73908513 x
x x x
Example 3 : Cont.
CSE 121, Prog. Lang.2, Prof. M. Moness,
2020 61
Flow Chart – Newton Start
Input: xo ,
s, maxi
a=1.1i=0
sStop
while
a>s&
i <maxi
i=1 or xn=0
x0=xn
n o100%
a n
x x
x
Print: xo, f(xo) ,
a , i 0
0 '
0
1
n
f x
x x
f x
i i
A MATLAB Program : Chapara, page 171
function [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations if nargin<3
error('at least 3 input arguments required')
end
CSE 121, Prog. Lang.2, Prof. M. Moness,2020 63
if nargin<4|isempty(es) es=0.0001;
end
if nargin<5|isempty(maxit) maxit=50;
end iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0
ea = abs((xr - xrold)/xr) * 100;
end
if ea <= es | iter >= maxit break
end end
root = xr ;
CSE 121, Prog. Lang.2, Prof. M. Moness,2020 64