An
INTRODUCTION to
NUMERICAL METHODS
A MATLAB
Approach
ABDELWAHAB KHARAB
King Fahd University of Petroleum and Minerals
RONALD B. GUENTHER
Oregon State University
CHAPMAN & HALL/CRC
Aci res Compa
Boca Raton London New%c Washi ington, D.C.Library of Congress Catuloging-in-Publication Data
Khana, Abdelwahab.
‘An introduction 1 oumerival methods : a MATLAB approach f Abdelwahab Kharub
and Ronald B. Guenther.
pem
Includes bibliographical references and i
ISBN 1-584N8-281-6 (alk, paper?
1. Numerical analysis—Datt processing. 2, MATLAB. Guenther, Ronald B11 Title
QA297 .K52 2001
539.4'0285 de
2091087074
‘This book contains information obtained from authentic an highly regarded sources, Reprinted material
is quoted with permission, and sources are indicated. A wide variety of references are listed, Reasonable
efforts have been made to publish reliable data and information, byt the author and the publisher cunnot
assume responsibility for the validity of all materials or for the consequences of their use
Neither this book mor any part may be reproduced or transmitted in any form or by ny means, electronic
‘or mechanical, including photocopying, microfilming. and recording, or by any information storage oF
roirieval system, without prior permission in waiting from the publisher.
The consent of CRC Press LLC does not extend to copying for general distribution, for promotion. fir
creating new works. or for resale, Specific permission must he obtined in writing from CRC Press LLC
for such copying.
Direct all inquiries to CRC Press LLC, 2000 N.W. Corporate Blvd., Bea Raton. Florida 33431,
Trademark Notice: Product or corporate names may be trademarks or registered rademmarks, and are
used only tor identification and explanation, without intent to infringe.
Visit the CRC Press Web site at [Link]
© 2002 by Chapman & Hal/CRC
No claim to original U.S. Government works
International Standard Book Number |-S8488.281-6
Library of Congress Card Number 2001047074
Printed in the United States of America 123.45 67890
Printed om avid-tree paperPreface
This is a textbook designed for an introductory course in numerical
analysis. It grew out of a set of lecture notes from a course on numer-
ical analysis that has been continuously offered at the departments of
mathematics of the King Fahd University of Petroleum and Minerals
and Oregon State University. It deals with the theory and application
of the most commonly used numerical methods for solving numerical
problems on microcomputers. It is intended for students in mathe-
matics, science, and engineering who have completed the introductory
calculus sequence. In addition, the reader is assumed to have taken
a structured programming course. The thrust of this text is to as-
sist the students to become familiar with the most common numerical
methods encountered in science and engineering. The content, material
of this book has been designed to be compatible with any introductory
numerical textbook that exists in the market. Students will be able to
examine and solve many numerical problems, using MATLAB™? in
a short period of time.
Due to the rapid advancement of computer technology and software
developments, we have used MATLAB as the computing environment
throughout all the chapters of the book. Each numerical method dis-
cussed in this book is demonstrated through the use of MATLAB which
is easy to use and has many features such as:
TMATLAB™* is a registered tradmark of the MathWorks, Inc.
For product information, please contact:
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098 USA
Tel: 508-647-7000 Fax: 508-647-7001
E-mail: info@[Link] Web: [Link]1. Powerful matrix straciure,
2. Powerful two- and three-dimensional graphing facilities,
3. A vast number of powerful built-in functions,
4, MATLAB's structured programming style that resembles FOR-
TRAN and BASIC,
This book introduces students to a wide range of useful and impor-
tant algorithms. Computer results are presented with full details so
that the main steps of the algorithm of cach numerical method are
visualized and interpreted. For this reason, a supplement CD-ROM
attached at the back of the book, has been developed for students’ use
with MATLAB. The CD-ROM contains simple MATLAB functions
that give a clear step-by-step explanation of the mechanism behind the
algorithm of each numerical method covered. Emphasis is placed on
understanding how the methods work. These functions guide the stu-
dent through the calculations ne ry to understand the algorithms).
The main feature of this book beside the use of MATLAB as its com-
puting environment, is that the style of the book is easy, direct and
simple. T! likely to boost students’ confidence in their ability to
master the elements of the subject.
The book is organized in a fairly standard manner. Topics that are
sinpler, both theoretically and computationally, come first; for exam
ple root of equations is covered in Chapter 3. Chapter 2 contains an
introduction to computer floating point arithmetic. errors, and interval
arithmetic.
Both direct. and iterative methods are presented in Chapter 4 for
solving systems of linear equations.
Interpolation, spline functions, and concepts of least squares data
fitting are the subjects of Chapters , 6, and 7. Interpolation forms the
theoretical basis for much of numerical analy:
Chapter 8 is devoted to mmerical differentiation and integration.
Several efficient integration techniques are presented.
In Chapter 9, a wide variety of numerical techniques is presented
jor solving ordinary diffcrential equations. An introduction for solving
boundary value problems is presented in Chapter 10. In Chapter 11, the
book presents a new numerical method (Quadratic method) developed
by Dr. Elgindi and Dr. Kharab that approximates the eigenpairs of a
matrix.The last chapter provides a basic introduction to numerical tech-
niques for solving partial differential equations.
It is the purpose of this book to implement various important numer-
ical methods on a microcomputer and to provide not only a source of
theoretical information on the methods covered, but also to allow the
student to easily interact with the microcomputer and the algorithm
for each method using MATLAB.
This text should provide an excellent tool suitable for teaching nu-
merical method courses at colleges and universities. It is also suitable
for self-study purposes.
Finally, we wish to thank the many persons who have helped us in the
creation of this book. They are: R. Baker Kearfoot of the University
of Southwestern Louisiana, Rachid Kharab of the University of Rouen,
A. Laradji of the King Fahd University of Petroleum & Minerals, and
L. Cheded of the King Fahd University of Petroleum & Minerals, who
encouraged us to submit a book proposal.
Recommendations that were provided to us by the publisher were
used to improve the book. We acknowledge the reviewers for these
recommendations.
The authors remain very grateful to the editorial and production
staff of Chapman & Hall/CRC who have been helpful and available
at all stages. Among them are Samar Haddad, project Editor, who
was in constant communication with me during the preparation of the
final version of the book and Sunil Nair, mathematics and statistics
publisher, for his assistance and encouragement.
Finally, the authors are grateful for the financial and facilities support
provided by King Fahd University of Petroleum & Minerals, Dhahran,
Saudi Arabia.
Suggestions for improvements to the book are always welcome and
can be made by e-mail at: awkharab@[Link].
Abdelwahab Kharab
Ronald B. GuentherContents
1 Introduction
Ll
12
13
21
2.2
23
24
ABOUT THE SOFTWARE MATLAB... . .
AN INTRODUCTION TO MATLAB
1.2.1 Matrices and matrix computation
1.2.2 Output format . .
1.2.3. Planar plots. .
124 3-D mesh plots .......
1.2.5 Function files... 2... .
1.2.6 Defining functions
1.2.7 Relations and loops
‘TAYLOR SERIES
Number System and Errors
FLOATING-POINT ARITHMETIC ...........
ROUND-OFF ERRORS ..... .
TRUNCATION ERROR..... -
INTERVAL ARITHMETIC ... .
Roots of Equations
THE BISECTION METHOD ..........
THE METHOD OF FALSE POSITION .. .
THE SECANT METHOD........... was
NEWTON’S METHOD ............-..-..
CONVERGENCE OF THE NEWTON AND SECANT
METHODS
FIXED POINT ITERATION . :
MULTIPLE ROOTS AND THE MODIFIED NEWTON
METHOD .3.8 NEWTON’S METHOD FOR. NONLINEAR SY‘ 72
System of Linear Equations 79
4.1 MATRICES AND MATRIX OPERATIONS... .... 79
4.2 NAIVE GAUSSIAN ELIMINATION . . . 83,
4.3 GAUSSIAN ELIMINATION WITH SCALED PARTIAL
PIVOTING ... ee 92
44 LU DECOMPOSITION . se eee ee 102
4.4.1 Crout’s and Choleski’s methods... 0... + . 102
4.4.2 Gaussian climination method 2.0... 108
4.5 ITERATIVE METHODS .......- 2. 15
1 Jacobi iterative method 2.2.0... 0 eee ee 5
4.5.2 Gauss-Seidel iterative method... -.. 9
4.5.3 Convergence... 0000s bee 123
Interpolation 131
5.1 POLYNOMIAL INTERPOLATION THEORY .... - 132
5.2. NEWTON’S DIVIDED DIFFERENCE
INTERPOLATING POLYNOMIAL ......0.-.5- 134
5.3. THE ERROR OF THE INTERPOLATING
POLYNOMIAL © 6 ee d44
54 LAGRANGE INTERPOLATING POLYNOMIAL... id?
Interpolation with Spline Functions 153
6.1 PIECEWISE LINEAR INTERPOLATION | -.. 153
6.2. QUADRATIC SPLINE 158
6.3 NATURAL CUBIC SPLINES... [Link]... . 161
The Method of Least Squares 175
7.1 LINEAR LEAST SQUARES 176
7.2 LEAST SQUARES POLYNOMIAL 182
7.3 NONLINEAR LEAST SQUARES. eee eee. 190
7.3.1 Exponential form. . . bees 191
7.3.2 Hyperbolic form ....... - 2 . 193
7.4 TRIGONOMETRIC LEAST SQUARES
POLYNOMIAL . 197
Numerical Differentiation and Integration 203
8.1 NUMERICAL DIFFERENTIATION . os 204
8.2 RICHARDSON’S FORMULA... 20.0 .-2.-.. 5. 2088.3 TRAPEZOIDAL RULE .
8.4 SIMPSON'S RULE... .
8.5 ROMBERG ALGORITHM .. .
8.6 GAUSSIAN QUADRATURE ..
9 Numerical Methods for Ordinary Differential Equa-
tions
9.1 EULER’S METHOD .
9.2 ERROR ANALYSIS . .
9.3 HIGHER ORDER TAYLOR ‘SERIES METHODS
9.4 RUNGE-KUTTA METHODS . .
9.5 MULTISTEP METHODS ... - see
9.6 ADAMS-BASHFORTH METHOD ......
9.7 PREDICTOR-CORRECTOR METHODS . .
9.8 ADAMS-MOULTON METHOD
9.9 NUMERICAL STABILITY ................
9.10 HIGHER ORDER EQUAT!
9.11 IMPLICIT METHODS AN.
9.12 PHASE PLANE ANALYSI
10 Boundary-Value Problems
10.1 FINITE-DIFFERENCE METHODS
10.2 SHOOTING METHODS
10.2.1 The nonlinear case
10.2.2 The linear case . .
11 Eigenvalues and Eigenvector
11.1 BASIC THEORY .
11.2 THE POWER METHOD vee
11.3 THE QUADRATIC METHOD .
IONS AND SYSTEMS .. .
'D STIFF SYSTEMS .. .
S$: CHAOTIC EQUATIONS
8
11.4 EIGENVALUES FOR BOUNDARY- VALUE.
PROBLEMS .......
11.5 BIFURCATIONS IN DIFFERENTIAL EQUATIONS
12 Partial Differential Equations
12.1 PARABOLIC EQUATIONS
12.1.1 Explicit methods
12.1.2 Implicit methods .. . . .
12.2 HYPERBOLIC EQUATIONS . .
12.3 ELLIPTIC EQUATIONS
- 259
- 299
301
311
.. Bll
.. 318
. 318
321
329
359Bibliography and References
Appendix A Calculus Review
A.V Limits and continuity. 2... 0.0...
A.0.2 Differentiation... . . . ee
A038 Integration. [Link] ee eee
Appendix B MATLAB Built-In Functions
Appendix © Text MATLAB Functions
Answers to Selected Exercises
Index
389
393
393
394
304
397
401
403
427Chapter 1
Introduction
The Taylor Series is one of the most important tools in numerical anal-
ysis. It constitutes the foundation of numerical methods and will be
used in most of the chapters of this text. From the Taylor Series, we
can derive the formulas and error estimates of the many numerical tech-
niques used. This chapter contains a review of the Taylor Series, and
a brief introduction to MATLAB:
=<
1.1 ABOUT THE SOFTWARE MATLAB
The widespread availability of personal computers, their low cost,
utility, and convenience, is rapidly becoming preferable to the large,
mainframe computers. It gives an opportunity to students to more
effectively integrate computing in the classroom. To exploit this op-
portunity, students in science and engineering should make use of the
software available for implementing numerical methods on microcom-
puters. An Example of these tools is the symbolic algebra programs
such as MATLAB, MAPLE, DERIVE, MathCad, and Mathematica.
Fortunately, the power and computing capabilities of these software
programs have not been fully used in classrooms by teachers in science
and engineering. For this reason, part of this text is intended to in-
troduce the computational power of MATLAB to modern numerical
methods.
MATLAB has several advantages. There are three major elements
that have contributed to its immense popularity. First, it is extremely
easy to use since data can be easily entered, especially for algorithms
that are adaptable to a table format. This is an important feature be-2 INTRODUCTION
cause it allows students to experiment with many numerical problems
in a short period of time. Second, it includes high-level commands for
two-dimensional and three-dimensional data visualization, aud presen
tation graphics. Plots are easily obtained from within a script or in
command mode. Third, the most evident power of a MATLAB is its
speed of calculation. The program gives instantaneous feedback. Be-
ause of their popularity, MATLAB and other software such as MAPLE
and Mathematica are now available in most. university microcomputer
laboratories.
One of the primary objectives of thi
step-by-step explanation of the algorithm corresponding to cach nu-
merical method used. To accomplish this objective, we have developed
MATLAB m-functions contained in a supplementary CD-ROM at. the
back of the book. These M-files can be used for the application or
illustration of all numerical methods discussed in this text. Each m-
function will enable students to solve numerical problems simply by
entering data and executing the m-functions.
Tt is well known that the best. way to learn computer programming
is to write computer programs. Therefore. I believe that by undor-
standing the basic theory underlying cach numerical method and the
algorithm behind it, students will have the ne ry tools to write
their own programs in a high-level computer language such as Cor
FORTRAN.
< text is to give students a clear
=
1.2 AN INTRODUCTION TO MATLAB
Tn this section we give to the reader a brief tutorial introduction to
MATLAB. For additional information we urge the reader to use the
reference and user's guides of MATLAB
4.2.1 Matrices and matrix computation
MATLAB treats all variables as matrices.
pressions by using an equal sign and their names are cas
example
>> A= [4 -25;617;-10 6]
A=
They are assigned to ex-
sensitive. ForAN INTRODUCTION TO MATLAB 3
4 -2 5
6 1 7
-1 0 6
New rows may be indicated by a new line or by a semicolon. A cok
umn vector may be given as
>> x= [2 8; Jorx = [2 8 9
x=
2
8
9
Elements of the matrix can be a number or an expression, like
>>x=[2 142 12/4 273]
x=
23 3 8
One can define an array with a particular structure by using the
command
x=a: step: b
As example
>>y=[Link]
0 0.2000 0.4000 0.6000 0.8000 1.0000
>> y = (0: pi/3: pi]
y=
O 1.0472 2.0944 3.1416
>> y = (20: -5: 2]
y=
20 15 10 5
MATLAB has a number of special matrices that can be generated by
built-in functions
>> ones(2)4 INTRODUCTION
ans =
I 1
1 I
a matrix of all Ls.
>> zeros(2,4)
ans =
0 0 0 0
0 0 0 0
a 2x 4 matrix of zeros.
>> rand(2,4)
0.9501 0.6068 0.8913 0.4565
0.2311 0.4860 0.7621 0.0185
a2» 4 random matrix with uniformly distributed random clements.
>> eyes(3)
ans =
1 0 0
o 1 0
0 0 1
the 3.x 3 identity matrix.
The most. useful matrix functions are
eig values wid eigenvectors
inv inverse
Iu LU decomposition
gr QR factorization
rank rank
A component-by-component addition of two vectors of the same di-
mensions is indicated as
>>x=(15 8;
>>y=(2 4 3);
Dox+y
ans =
3 9 uAN INTRODUCTION TO MATLAB 5
‘The component-by-component multiplication of the vectors x and y
is indicated as
>> x Fy
ans =
2 20 24
The transpose of a matrix or a vector is denoted by a prime symbol.
For example
>> x’
ans =
1
5
8
‘The matrix operations of multiplication, power, and division are in-
dicated as
>> B= [1 3,4 2];
>> A= [2 5:0 6;
>> AB
ans =
22 16
24 (12
>> AZ
ans =
4 40
0 36
>> A/B
1.6000 0.1000
2.4000 0.6000
>> A*B
ans =
2 156 INTRODUCTION
Note that the three operations of nnultiplication, division, and power
can operate elementwise by preceding them with a period.
1.2.2 Output format
While all computations in MATLAB are performed in donble pre-
cision, the format of the displayed outpnt can he controlled by the
following, commands:
format short fixed point with 4 dechnal places (the default)
format long fixed point. with 14 decimal places
format short
format long
forme
cientific notation with 4 decimal places
tific notation with 15 decimal p
itogers
ation by ratio of small
vat appre:
Once invoked, the chosen format remains in effect until changed.
It is possible to make the printed output from a MATLAB function
look good by using the disp and fprintf functions.
disp(X) displays the array X, without printing the array name. If X
is a string, the text is displayed. For example
disp(‘The input matrix A is’)
will display the text placed in single quotes. The function disp also
allows us to print numerical values placed in text strings. For example
disp(['Newton's method converges after '.num2str(iter),’ iterations’])
will write on the screen
Newton's method converges after 9 iterations.
Here the function num2str(iter) converts a number to a string.
The other function fprintf is more flexible and has the form
fprintf( ‘filename’, format’ list)
where,
filename is optional: if it, is omitted, output goes to the sereen,AN INTRODUCTION TO MATLAB 7
format is a format, control string containing conversion specifications or
any optional text. The conversion specifications control the output of
array elements.
Common conversion specifications include:
%[Link] _ for exponential
%[Link] fixed point
%[Link] to automatically select the shorter of %[Link] or %[Link]
where P and Q are integers that set the field width and the number of
decimal places, respectively.
list is a list of variable names separated by commas, and the special
format \n, produces a new line.
For example, the statements
>> x = pis y = 4.679; 2=9;
>> fprintf("\n x = %8.7A\n y = %4.3A\n 2 = %L.0F\n' x,y,z)
prints on the screen
x = 3.1415927
y = 4.679
z=9
1.2.3 Planar plots
Plotting functions is very easy in MATLAB. Graphs of two-dimensional
functions can be generated by using the MATLAB command plot. You
can, for example, plot the function y = 2? + 1 over the interval (—1, 2]
with the following commands:
>> x= [-L: 0.02: 2];
>> y=x2 41;
>> plot(xy)
The plot is shown in Figure 1.1. To plot a function f1 defined by
an M-file (see Subsection 1.2.6) in an interval (a, 6), we use the command8 INTRODUCTION
FIGURE 1.1 ,
Plot of the function f(a) = 2? +
>> fplot(f1’.[a.b)).
Other features can be added to a given graph using commands such
as grid, xlabel, ylabel, title, etc... For example, the command grid on
will create grid lines on the graph.
Plots of parametrically defined curves can also be made. Try, for
example,
t=(0: 0.01: 4*pi]: x=cos(2*t); y=sin(3*t); plot(x.y).
Multiple plots on a single graph are illustrated by
>> x=(0:0.1:2*pi];
>> yl=cos(x); y2=sin(x); y3=cos(2*x);
>> plot(xyl,x,y2,x,y3)
1.2.4 3-D mesh plots
Three-dimensional or 3-D mesh surface plots are drawn with the
fiction mesh. The command mesh(z) creates a three-dimensional per-
spective plot of the elements of the matrix =AN INTRODUCTION TO MATLAB 9
The following example will generate the plot shown in Figure 1.2, of
the function z = e~**-¥” over the square [—4, 4] x [—4, 4].
>> [x y]=meshgrid(-4.0:0.2:4.0,-4.0:0.2:4.0);
>> z=exp(-x.72-y.72);
>> mesh(xy.z)
FIGURE 1.2
Three-dimensional surface.
Other commands related to 3-D graphics are plot3 and surf.
1.2.5 Function files
MATLAB contains a vast collection of built-in functions from ele-
mentary functions like sine and cosine, to more sophisticated functions
like matrix inversions, matrix eigenvalues, and fast Fourier transforms.
Table 1.1 contains a small selection of elementary mathematical MAT-
LAB functions and Appendix B contains additional lists of functions
grouped by subject area.
Note that all these function names must be in lower cases. MATLAB
functions may have single or multiple arguments. For example,Ww INTRODUCTION
cos{x) cosine oft
sin(x) sine of &
tan(x) tangent of J
sqrt{x) square root of a
abs(x} absolute valne of ut
exp(x) exponential of 2
log(x) log to the base ¢ of
logl0(x) log to the base 10 of a
cosh(x) hyperbolic cosine of
tanh(x) hyperbolic tangent of &
asin(x) inverse sine function of we
expm(A) exponential of matrix A
Table 1.1) Elementary mathematical MATLAB functions.
[U,D]=eig(A)
produces a matrix U whose columns are the eigenvectors of A and
a diagonal matrix D with the eigenvalucs of A on its diagonal.
1.2.6 Defining functions
MATLAB allows users to define their own functions by constructing,
an M-file in the M-file Editor/Debugger. The first line of a function
has the form
function y = function_name( input arguments)
As an example, let us define the function f1 (a) = 2a ~ 30-41 as an
mfile, Using the Editor/Debugger we enter
function y=f1(x)
ya2hx.°3-3*x+1;
Once this function is saved as an M-file named flan, we can use the
MATLAB command window to compute, for example.
>> FL(2)
ans =
4AN INTRODUCTION TO MATLAB soy
User defined functions can themselves have functions as input pa-
rameters. They can be evaluated internally by using the function feval.
For example,
>> x1=fzero((f1',0)
xl=
0.3949
gives the zero of f1 closest to 0.
1.2.7. Relations and loops
Like most computer languages, MATLAB offers a variety of flow con-
trol statements like for, while, and if. The statements that we use to
control the flow are called relations.
The Relational operators in MATLAB are
equal
less than or equal
greater than or equal
not equal
< less than
> greater than
Note that is used in an assignment statement while “==” is
used in a relation.
Logical operators
& and
if, for, while statements
The general form of an if loop is12 INTRODUCTION
if
elseif
else
end
Hero is a simple example. If i,j = 1...., 10, then
will result in a diagonal matrix A with L's in its diagonal.
The general form of a for loop is
for <[Link]= [Link]>
end
For example,
:100
S=S41:
end
Ss
results in the ontput
S=
100,
You can also nest for statements. ‘The following statements create an
m xn Hilbert matrix, H.TAYLOR SERIES 13
J=1/(i4i-)s
Finally, MATLAB also has its own version of the while loop, which
repeatedly executes a group of statements while a condition remains
true. Its form is
while < while expression >
< condition statements>
end
Given a positive number n the following statements compute and
display the even powers of 3 less than n.
k=0;
while 37k 0 and a positive integer n such that
|F(A)| 2 and 0 0.
4. A partial ordering of the elements of I(R) is defined by
A 2=b/aE B/A.
The starting point for the application of interval analysis was, in ret-
rospect, the desire in numerical mathematics to be able to execute
algorithms on digital computers capturing all the round-off errors au-
tomatically and therefore to calculate strict error bounds automatically.
Interval arithmetic, when practical, allows rigor in scientific computa-
tions and can provide tests of correctness of hardware, and function
libraries for floating-point computations. A complete study of interval
arithmetic can be found in [2]. In that reference, more topics such as
interval matrix operations and interval systems of equations are pre-
sented.NUMBER SYSTEM AND ERRORS
Le
EXERCISE SET 2.4
1.
. Compute a solution set for [2,4|.X =
. By testing the sign of the end points of the interva
Carry out the following interval arithmetic:
. Using interval arithmetic, compute hounds on the range of the
following real rational functions:
(a) f(w)=2?4+2e+1, OSash
(b) fy =ryt2 -lersl,-ltol)&(iter<=n)
w=teval(f,c);
fprintf('%2.0f %10.4f %10.4f %12.6f %10.6F %10.6fin’, iter,
a, b,c, w, err)
if (w*u<0)
b=c;v=w;
end;
if (w*u>0)
a=CiU=W;
end;
itersiter+1;
¢=(a+b)"0.5;
err=abs(b-a)*0.5;
end;
if (iter>n)
disp(' Method failed to converge')
end;
else
disp(’ The method cannot be applied f(a)f(b)>0')
end;
% Plot f(x) in the interval [a,b].
fplot(t, [a0 bo)
xlabel('x’); ylabel('f(x)'); grid40 ROOTS OF EQUATIONS
EXAMPLE 3.1
The function f(z) =
bisection algorithm to approximate the zero of f to within 1074.
— 2? — 1 has exactly one zero in [1,2]. Use the
Since f(1) = —L < Oand f(2) = 3 > 0, then (3.2) is satisfied.
Starting with ap = 1 and by = 2; we compute
2
y — tbo VA is and F(eo) = 0.125.
2 2
Since f(1.5)f(2) > 0. the function changes sign on [ap. 0] = [1.1.5].
To continue, we set ay = ag and by = cg; so
by Le LS
oy = FET 95 and flex) = —0.609375.
2 2
Again f(1.25)f(1.5) < 0 so the function changes sign on [e).1] =
(1.25, 1.5]. Next we set az = c and bg = 6). Continuing in this manner
leads to the values in Table 3.1 which converge to r = 1.465454.
Note that, before calling the MATLAB function bisect, we must de-
fine the MATLAB function f1 as follows
function f=f1(x)
fex,73-x.°21;
Now call the the function bisect to get the results shown in Table 3.1.
Some of the features of MATLAB can be used to view, for example,
step-by-step calculations involved in the function bisect. ‘This can be
done by using the pause function of the form pause(s). Tf we add the
line
pause(1);
just before the end statement of the while loop, execution is held up for
approximately 1 second at the end of cach output line. MATLAB also
provides a wide range of graphic facilities that may be used to graphi-
cally visualize the results. For example if the folowing commands
fplot(’f1',[20,b0])
gridTHE BISECTION METHOD 41
}» bisect(‘f1',1,2,10%(-4),40)
iter a b ¢ fio) fo-af2
0 1.0000 2,0000 1.500000 0.125000 0.500000
1 1.0000 1.5000 1.250000 -0.609375 0.250000
2 1.2500 1.5000 1.375000 -0.291016 0.125000
3 1.3750 1.5000 1.437500 0.095947 0.062500
4 1.4375 1.5000 1.468750 0.011200 0.031250
5 1.4375 1.4688 1.453125 0.043194 0.015625
6 1.453 1.4688 1.460938 -0.016203 0.007813
7 1.4609 1.4688 1.464844 0.002554 0.003906
8 1.4648 1.4688 1.466797 0.004310 (0001953
9 1.4648 1.4668 1.465820 0.000875 0.000977
10 1.4648 1.4658 1.465332 0.000840 0.000488
u 1.4653 14658 1.465576 0.000017 0.000244
12 1.4653 1.4656 1.465454 __-0.000411__0.000122
Table 3.1 Solution of <3 — x? —1 = 0 in [1,2] using the bisection method.
are added at the end of the function bisect.m, then a plot of the func-
tion f(#) = «8 —2?~1 in [a,}], shown in Figure 3.2, will be given with
the output.
Although the Bisection method always converges and is easy to pro-
gram, compared to other methods, its rate of convergence is rather
slow. We state the following convergence result.
THEOREM 3.1
Let f be a@ continuous function on an interval [a,b] where f(a) f(b) <0. At
the nth step, the bisection procedure approximates a root with an error of at
most (b — a)/2°*!,
Proof: Let |ag, 69] be the original interval and a a zero of f in this
interval. Define
bo +.a0
2
co and a € [ao, by]
then
Ja cal < (64 ~ a1) = BEAQ ROOTS OF EQUATIONS
FIGURE 3.2
A plot of f(x)
Lin [1,2].
with a; and b; being the endpoints of the new subinterval containing
a.
Tf ¢, denotes the nth value of ¢, then we have
a
fa al < Cnt Ae) =p POL
EXAMPLE 3.2
Determine the number of iterations required to find the zero of f(a), defined
in Exaniple 3.1, with an absolute error of no more than 10°.
We want to find such that.
boa
lo en] S Sar = Gear < 10
This implies that
—(n+1jlog2) <6 orn > 1 19.
lox?
So one needs at most 19 iterations to obtain an approximation accurate
to Lo’.THE BISECTION METHOD 43
EXERCISE SET 3.1
1.
©
oe
Find a root of f(z) = 23+ 2x —3 in the range 0 < 2 < 7/5 using
the bisection method.
. The function f(x) = x? — 2.67 — 2.31 has one root in the interval
[3,4]. How many steps of the bisection method are needed to
locate the root with an error of at most 1075.
. Find a root of the equation e2* — 7a = 0.
. Verify that the function f(x) = 2? sing + 2x — 3 has exactly one
root in (0,2). Find this root by using the bisection method with
an error of no more than 10-5. Count the number of iterations
needed to find the root and compare your answer with the result
obtained by using the error formula given by Theorem 1 of section
3.1.
. The function f(x) = 25 — 3x3 + 2.52 — 0.6 has exactly one root
in (1,2). Demonstrate it graphically and determine the root with
an error of at most 1075,
. Consider the function
22 16 8
aot — bed 4 “eg? 2
fajax baht ye atts
(a) Check that f(x) has a root a; between 0 and J and another
root @ between 1 and 4.
(b) Compute both roots using the bisection method.
. Sketch the graph of f(z) = tanz+tanhz = 0 and find an interval
in which its smallest positive root lies . Determine the root correct,
to two decimal digits using the bisection method.
. Computer assignment:
Write a computer program in FORTRAN or C that finds the zeros
of a function f(z) on an interval (a, b) using the bisection method.
Assume that there is only one simple root in this interval. Input
data to the program should be a function f(z), a,b and the error
tolerance €.4d ROOTS OF EQUATIONS
your program do if the given f is f(«) = cosa and
a=0,6= x? What does it do ifa = 0b = 27?
‘Test your program to find the root of the equation given in Ex-
ercise 2.
nn
3.2. THE METHOD OF FALSE POSITION
The inethod of false position, also known as regula falsi, is similar
to the Bisection method but has the advantage of being slightly faster
than the latter. As in the bisection method, the function is assimed
to be continuous over the interval [27,2] with
fli) f(a) < 0.
In this procedure, the first point ay is selected as point of intersection
of the a-axis, and the straight Hine joining the points (1. f(y) and
(a9, f(a) as shown in Figure 3.3. From the equation of the secant
line, it follows that
Py 02 flely (3.6)
FIGURE 3.3
The method of false posit
to its zero a.
ion and the first two approximations
With 23 now known, we then compute f(#3) and repeat the pre
dure between the values at which the function changes sign, that is, iTHE METHOD OF FALSE POSITION 45
F(21)f(w3) < 0 set 22 = a3, otherwise set z1 = 23. At each step the
method of false position produces a new interval that contains a root of
f and the sequence of points generated by the method will eventually
converge to the root. An algorithm of the method of false position is
given below,
Given a continuous function f(x) on [ao, bo] with f(ag)f (bo) < 0.
for n = 0,1,...,.ITMAX
flbn Jan — Flan dou
¥
OS Fon ile
if flan) f(c) <0, set ansi = en, basi =e
| otherwise, set ana1 =¢, busi = bn»
Because in some cases the method of false position fails to give a small
interval in which the zero is known to lie, we terminate the iterations
if
\flra)l Se
or
|@n — &n-i| Se
where ¢ is a specified tolerance value.
One of the main disadvantages of this method is that if the sequence
of points generated by its algorithm is one-sided, it will tremendously
slow the convergence of the method. This applies to a function whose
graph is concave up or concave down on (ao, 6] (see Figure 3.4).
EXAMPLE 3.3
The function f(x) = a — x? — 1 has exactly one zero in [1,2]. Use the
method of false position to approximate the zero of f to within 1074.
A root lies in the interval [1,2] since f(1) = —1 and f(2) = 3.
Starting with ag = 1 and bo = 2, we get using Eqn. (3.6)
= = 1.25 and (eo) = ~0.609375.
Here, f (co) has the same sign as f(ag) and so the root must lie on the
interval [¢o, bo] = [1.25, 2]. Next we set ay = a9 and 6; = by to get theAGB ROOTS OF EQUATIONS
FIGURE 3.4
The method of false position and approximation lines on
{o, bo).
next approximation:
2— 1,25)
.609375)
= 1.37662337 and f(y) = --0.2862640.
Now f(r) changes sign on {¢, , by) = [1.37662337, 2]. Thus we set a2 =e
and by = 6. Continuing in this manner leads to the values in Table
3.2 which converge to r = 1.465558.
een nnn
EXERCISE SET 3.2
1. Find a root of f(z) = 23 + 2x —3 in the range 0 tol)&(abs(b-a)>tol)&(iter<=n)&((v-u)~=0)
we=feval(f,c);
fprintf('%2.0f %12.4f %12.4f %12.6f %10.6f %10.6fin’, iter, a,
b, c, w, abs(b-a))
if (w'u<0)
Ww;
)
end;
if (w*u>0)}
a=C;U=W;
end;
itersiter+1;
c=(v*a-u*b)(v-u);
end;
if (iter>n)
disp(' Method failed to converge’)
end;
if (v-u==0)
disp(’ Division by zero')
end;
else
disp(' The method cannot be applied f(a)f(b)>0')
end;
fplot(f,[aO bO])
xlabel(’x’);ylabel(‘((x)); grid48 ROOTS OF EQUATIONS
10%(-4),40)
iter a b © fey iba}
0 1 2 1.250000 -0,609375 ‘1.000000
1 1.25 2 1.376623 0286264 0.750000
2 1.3766 2 1.430925 0.117660, 0.623377
3 1.4309 2 1.452402 0.045671 0.569075,
4 1.4524 2 1.460613 0.017331 0.547598
3 1.4606 2 1.63712 0.006520 0.539387
6 1.4637 2 1.464875 -0,002445 0.536288
1 1.4649 2 1.465310 -0,000916 125
8 1.4653 2 1.465474 -0,000343 469
9 1.4655 2 1.465535 0.000128 34526
lo Lass; 2 1465558 -0.000048 0.534465
Table 3.2 Solntion of
position.
- 1 = O in [1,2] using the method of false
5, Computer assignment:
Write a computer program in FORTRAN or C that finds the ze
ros of a function f(r) on an interval a,b] using the method of
false position. Input data to the program should be a function
F(x), ab, the ertor tolerance ¢, and the maximum number of it-
crates.
T
ercise 1,
your program to find the root of the equation given in Ex-
—
3.3. THE SECANT METHOD
Becanse the bisection and the false position methods converge at a
very slow speed, our next approach is an attempt to produce a method
that is faster. One such method is the secant method. Similar to the
false position method, it is based on approximating the function by a
straight. line connecting two points on the graph of the function ff. but
we do not require f to have opposite signs at the initial points. FigureTHE SECANT METHOD 49
FIGURE 3.5
The secant method and the first two approximations to its
zero a.
3.5 illustrates the method.
In this method, the first point, x2, of the iteration is taken to be
the point of intersection of the 2-axis and the secant line connecting
two starting points (2, f(zo)) and (x1, f(1)). The next point, x3, is
generated by the intersection of the new secant line, joining (21, f(z1))
and (22, f(z2)) with the z-axis. The new point, 23, together with xo,
is used to generate the next point, 24, and so on.
A formula for +1 is obtained by setting « = rn41 and y = 0 in the
equation of the secant line from (2p—1, f(an—1)) to (2p, f(tn))
Ln ~ Fai
f(tn—1))°
Ten 7)
Bnet = Iq — f(atn)
This is of course, the same formula as for the method of false position.
An algorithmic statement of this method is shown below.
Let zo and z1 be two initial approximations,
for n = 1,2,..., ITMAX
Lane = tn Sen) [pS] -
A suitable stopping criterion is
lont1 =
If(@n)l Se, |tntr~ nl Se or
[en]
where € is a specified tolerance value.50 ROOTS OF EQUATIONS
m-function 3.3
The following MATLAB function secant.m finds the solution of an
equation using the secant method. INPUTS are a function £ initial
approximations xo, xi; @ tolerance fof, the maximum number of
iterations n. The input function f(x) should be defined as an M-file.
function secant(f,x0,x1 ,tol,n)
% The secant method for solving the nonlinear equation f(x)=0.
iter=0;
u=feval(f,x0);
v=teval(f,x1);
err=abs(x1-x0);
disp( iter xn (xn) fn t)toxn)— fxnet-xn|')
gisp( ‘)
fprintf('%2.0f %12.6f %12.6f\n’ iter,x0,u)
fprintf('%2.0f %12.6f %12.6f %12.6f %12.6f\n' iter,x1,v,v-u,err)
while (err>tol)&(iter<=n)&((v-u)~=0)
x=x1-v" (x1 -XO)/(v-u);
usv;
x1=x;
veteval(.x1);
err=abs(x1-x0);
itersiter+1:
fprintf(‘%2.0f %12.6f %12.6f %12.6f %12.6f\n’iter,x1.v,v-u,
err)
end
if ((v-u)}==0)
disp(' Division by zero’)
end
if (iter>n)
disp(' Method failed to converge’)
end
EXAMPLE 3.4
Use the secant method with ay = 1 and a
within 1074.
= 2 to solve a?
With iq = 1. f(a) = 1 and wy = 2. flor) = 3, we have
= DB) _
lp 1.25THE SECANT METHOD 51
from which f(:2) = f(1.25) = —0.609375. The next iterate is
(1.25 — 2)(—0.609375) _
0.600375 3 = 1.3766234.
Continuing in this manner leads to the values in Table 3.3 which con-
verge to r = 1.4655713.
@3 = 1.25 —
D> secant(FT’,1,2,10%-4),40)
xn fixn) foxmti}fixn) [ant l-xn |
1.000000 -1,000000
2.000000 3.000000 4.000000 1.000000
1.250000 -0.609375 -3.609375 0.750000
1.376623, -0.286264 0.323111 0.126623
1.488807 0.083463 0.369727 0.112184
1.463482 0.007322 -0.090786 0.025325
1.465525, -0.000163 0.007160 0.002043
1.465571 3.20E-07 0.000163 0.000046
Table 3.3 Solution of 2° — 2?
to = 1, 21 = 2.
1 = 0 using the secant method with
a
EXERCISE SET 3.3
1. Repeat Exercise 1 of Section 3.1 using the secant method
2. Solve the equation x3 — 42? +2¢—8 = 0 with an accuracy of 10-4
by using the secant method with ao = 3,2) = 1.
3. Approximate to within 10~® the root of the equation e~?*—72 = 0
in (1/9, 2/3] by the secant method.
4, Use the secant method to approximate the solution of the equa-
tion 2 = z* — e~* to within 10-° with zp = —1 and x, =1.
5. The function f(x) = 2° — 3a +2.5a —0.6 has two zeros in the in-
terval (0, 1]. Approximate these zeros by using the secant method
with:52 ROOTS OF EQUATIONS
(a) xp = 0,4; = 1/2.
(b) ap = 3/401 = 1b
6. Ifthe secant method is used to find the zeros of f(x) = 2 —32?+
2a — 6 with ry = Land a = 2, what is 2?
7. Use the secant method to approximate the solution of the cqua-
tion 2 = —e” sin — 5 to within 107!° in the interval [3,3.5].
8. Computer assignment:
Write a computer program in FORTRAN or C that finds the
zeros of a function f (ic) using the secant method. Input data to
the program should be a function f(x), two initial guesses for the
root and the error tolerance €.
Test your program to solve Exercis
En
3.4 NEWTON’S METHOD
Newton's method is one of the most widely used of all iterative tech-
niques for solving equations. Rather than using a secant line, the
method uses a tangent line to the curve. Figure 3.6 gives a graphi-
cal interpretation of the method. To use the method we begin with an
initial guess 2, sufficiently close to the root a. The next approximation
wr; is given by the point at which the tangent line to f at f(vo, f(r0))
crosses the z-axis. It is clear that the value a is much closer to a
than the original guess wo. If vn41 denotes the value obtained by the
succeeding iterations, that is the x-intercept of the tangent line to f at
(va, f(rp)), then a formula relating sry and z_+1, known as Newton’s
method, is given by
_ fan}
fen)
fat = tn n>0 (3.8)
provided f/(,) is uot zero.
To derive Eqn. (3.8), notice that the equation of the tangent line at
(x0, f(wo)) is
y— f(x) = f'(r0)(a ~ 20)NEWTON'S METHOD 53
FIGURE 3.6
Newton’s method and the first two approximations to its zero
a
If x1 denotes the point where this line intersects the z-axis, then y = 0
at x = a), that is
~F (#0) = f'(x0)e1 — x0),
and solving for a gives
_ fo)
F(zo)
By repeating the process using the tangent line at (21, f(71)), we obtain
for x2
f(z)
f(z)"
By writing the preceding equation in more general terms, one gets Eqn.
(3.8). A suitable way of terminating the iterations is by using one of
the stopping criteria suggested for the secant method.
An algorithmic statement of Newton’s method is given below.
Given a continuously differentiable function f and an initial value x9
21 = 29
22 =a, —
for n = 0,1,..., ITMAX
Flea,
Tn
Lo tng © tn
Sometimes Newton’s method diverges. If xq is not sufficiently close to
the root, the method may not converge. It is evident that one should
not choose zr such that f’(29) is close to zero, for then the tangent line
is almost horizontal, as illustrated in Figure 3.7.54 ROOTS OF EQUATIONS:
m-function 3.4
The following MATLAB function newton.m approximates the
solution of an equation using Newton's method. INPUTS are a
function f and its derivative f; an initial guess xo; a tolerance tof, the
maximum number of iterations 7. The input functions f(x) and F(x)
should be defined as M-files.
function newton(f,df,x0,tol,n)
% Newton's method for solving the nonlinear equation f(x)=0.
feval(f,x0);
feval(df,x0);
err=abs(u/v);
disp(’
disp(’ iter x a) f(x) xn+t-xn]
disp(\_ — —
fprintf((%2.0F %12.6F %12.6f %12.6fin’, iter, x0, u, v)
while (err>tol)&{iter<=n)&(v~=0)
xt=x0-u/v;
ert;
fprintf('%2.0f %12.6f %12.6f %12.6f %12.6An',iter,x0,u,v,err)
end
if (ve=0)
disp(' division by zero')
end
if (iter>n)
disp(' Method failed to converge’)
end
EXAMPLE 3.5
Use Newton's method to compute a root of
to an accuracy of 1074. Use 20
The derivative of f is f’(x) =
Qu. Using, x9 = 1 gives [() =NEWTON'S METHOD 55
FIGURE 3.7
Failure of Newton’s method due to a bad starting point.
and f‘(1) = 1 and so the first Newton iterate is
and f(2)=3, f"(2)=8
The next iterate is 3
tp =2— 5 = 1.605.
Continuing in this manner leads to the values in Table 3.4 which con-
verge to r = 1.465571. In Table 3.4 the MATLAB function dfl is
defined as follows:
function f=df1(x)
F=3%x.*2-2*x;
fs newton('f1',df1',1,10%(-4),40)
iter xn fxn) F(xa) |antl-xnf
0 1.000000 =1,000000 1,000
1 2.000000 3.000000 8.000000 1.000000
2 1.625000 0.650391 4.671875 0.375000
3 1485786 0.072402 3.651108 0.139214
4 1.465956 0.001352 3.515168 0.019830
3 1.465571 0.000001 3.512556 0.000385
6 1.465571 6.93E-14 3.512555 1.43E-07
Table 3.4 Solution of 23 — 2? — 1 = 0 using Newton's method with
fo =1.56 ROOTS OF EQUATIONS
ern
EXERCISE SET 3.4
1. Find the roots of the following equations in the interval indicated
with an accuracy of 10% by using Newton's method.
(a) eF 430% +a ~4=0. in [0,1
(b) rt 4 20? » 3a? — dir — 2= 0. in [1,2].
(c) 2 — 32? +3 =0, in [1,2]
2. Use Newton's method te compute a zero of the funetion
3x8 — Sa $d
to an accuracy of 10°8. Use to = 1
3. Find an approximate root of e-?