0% found this document useful (0 votes)
408 views139 pages

Numerical Analysis and Computational Applications Farkaleet Series

Uploaded by

Zeeshan Ejaz
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)
408 views139 pages

Numerical Analysis and Computational Applications Farkaleet Series

Uploaded by

Zeeshan Ejaz
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/ 139

FARKALEET SERIES

NUMERICAL ANALYSIS AND


COMPUTATIONAL APPLICATIONS

AUTHORS:
Prof. (R) Muhammad Urs Shaikh
Prof. Dr. Asif Ali Shaikh
Prof. Dr. Feroz Shah

CO-AUTHORS:
Muhammad Tahir Wahocho (M.Sc)
Dr. Wajid Ai Shaikh

EDITED BY:
Dr. Sania Niazamani

JAHANGIR PUBLICATIONS, LARKANA


Cell: 0333-756-5595, 0304-453-2100
PREFACE TO FIRST EDITION
About thirty years ago I wrote a series of books on different subjects of Engineering
Mathematics. The series was named “FARKALEET SERIES”. FARKELEET means
MUHAMMAD (S.A.W.S). This name of our Prophet (S.A.W.S) is mentioned in the BIBLE.
The series though was hand written but became very much popular among my students at
Mehran University of Engineering and Technology, Jamshoro, Pakistan. The series also
gained popularity in the students of other universities and colleges of Sindh as well. Many
teachers of different universities and colleges also appreciated such efforts of mine. The
reason is that each topic was clearly explained and that every student as well as teacher
enjoyed reading without any difficulty.
The book “NUMERICAL ANALYSIS AND COMPUTER APPPLICATIONS” is in your hands.
Each topic is explained in well manner and in detail as well.
We shall be looking forward to hear from the readers any critic that will improve the
standard of the book.
AUTHORS: CO-AUTHORS:
Prof. (R) Muhammad Urs Shaikh Muhammad Tahir Wahocho (M.Sc)
Prof. Dr. Asif Ali Shaikh Dr. Wajid Ai Shaikh
Prof. Dr. Feroz Shah

EDITED BY:
Dr. Sania Niazamani

PREFACE TO SECOND EDITION


In the first edition, the book was containing some typo-graphical as well as some
mathematical calculation mistakes. These mistakes have been now removed from the first
edition. We hope that, students will be much comfortable with the new revised edition.
However, if any mistake is found, we will appreciate if the same may be conveyed to the
authors.

Prof. (R) Muhammad Urs Shaikh


CONTENTS
CHAPTER ONE
ERROR ANALYSIS
Introduction 1
What is an Error 2
Sources of Errors 3
Significant Figures 5
Accuracy and Precision 6
Absolute , Relative and Percentage Errors 7
Effects Of Rounding Errors In Arithmetic Operations 8
Error Accumulation in Addition and Subtraction 8
Error Accumulation in Multiplication 9
Error Accumulation in Division 10
Errors in Powers, Roots and Functions 11
WORKSHEET 01 12

CHAPTER TWO
NON-LINEAR EQUATIONS
Introduction 13
Root Location Methods 13
Bisection Method 14
False Position Method 17
Newton-Raphson Method 21
Simple Iteration Method/Fixed Point Iteration Method 23
WORKSHEET 02 26

CHAPTER THREE
SOLUTION OF LINEAR SYSTEMS
Introduction 28
Non-Iterative (Direct) Methods 28
Triangular Decomposition (Factorization) Method 28
LU-Decomposition Method 28
Inverse of Matrix by LU-Decomposition Method 33
Cholesky’s Method 34
Inverse of Matrix by Cholesky’s Method 34
Iterative (Indirect) Methods 36
Jacobi’s Method 36
Gauss-Seidel Method 39
Eigen values and Eigen Vectors 42
Power Method 43
WORKSHEET 03 47

CHAPTER FOUR
INTERPOLATION AND EXTRAPOLATION
Introduction 49
Finite Differences 49
Forward Difference Operator 50
Backward Operator, Shift Operator, Central Difference Operator and 51
Mean/Average Difference Operator
Symbolic Relations between the Operators 52
Estimating the Missing Term 57
Interpolation With Equally Spaced Data 58
Newton Forward Interpolation Formula 58
Newton Backward Interpolation Formula 59
Sterling’s Interpolation Formula 60
Interpolation With Unequally Spaced Data 63
Lagrange’s Interpolation Formula 63
Divided Difference Interpolation Formula 65
WORKSHEET 04 66

CHAPTER FIVE
NUMERICAL DIFFERENTIATION AND INTEGRATION
Introduction 70
Numerical Differentiation 70
Numerical Integration 73
Newton’s Cotes Formula 73
Trapezoidal Rule 74
Simpson’s 1/3 Rule 76
Combination of Trapezoidal Rule and Simpson’s 1/3 Rule 78
Simpson’s 3/8 Rule 78
Boole’s and Weddle’s Rules 80
WORKSHEET 05 80
CHAPTER SIX
SOLUTION OF ORDINARY DIFFERENTIAL EQUATIONS
INTRODUCTION 83
Semi-Analytic And Semi Numerical Methods 84
Picard’s Method 84
Taylor’s Series Method 85
Numerical Methods 86
Euler’s Method 86
Modified Euler’s Method 89
Improved Euler’s Method 90
Runge-Kutta Methods 91
Predictor Corrector Methods 95
Milne’s Method 95
Adams-Bash forth Method 98
Second Order Differential Equations 99
WORKSHHET 06 100

CHAPTER SEVEN: NUMERICAL SOLUTION OF PARTIAL DIFFERENTIAL EQUATIONS


Introduction 102
Classification Of 2nd Order Partial Differential Equations 103
Finite Differences 104
Geometrical Representation Of Finite Difference Quotients 105
Elliptical Equation 106
Laplace Equation 107
Poisson Equation 110
Parabolic Equation 112
Bender Schmidt Method 112
Hyperbolic Equation 114
Solution of Wave Equation 114

WORKSHEET 07 116
APPENDIX “PROGRAMMING WITH C ++” 117
CHAPTER ONE ERROR ANALYSIS

CHAPTER
ONE ERROR
ANALYSIS

INTRODUCTION
The projectile theory is a subject of interest to mathematicians, engineers and scientists. The
height H(x) reached by a projectile at distance x from the point of projection is given by:
H(x) = x tan α – gx2 /2(v0 cos α)2 (1)
where vo is the initial velocity, α is angle of projection and g is acceleration due to gravity. See
the figure for projectile motion.
Now when α = 60o (1.04 Rad), v0 = 100 m/s (say), with g = 9.8 m/s2,
the height H when x = 500 m is determined by (1) is H
(500) = 376 m. It has remained an important assignment of scientists
and engineers to see how small changes in the parameters α and v0
affect the value of height H. This phenomenon is frequently called
sensitivity and perturbation analysis. For example, if v0 is decreased by 2% in its value then
v0 = 98 m/s and H (500) = 356 m which shows that H is decreased by about 5.32%.
Now let us take v0 = 100 but decrease α by 2%, that is, let α = 0.98 (Radians), we obtain
H = 369 m. This shows that there is only 1.86% change in the actual value of H. This shows that
small change in v0 affects the result much more than a small change in α.
When there are many parameters involved in any model, it is practically difficult to see what
changes occur in the dependent variable if one or some of these parameters are changed slightly.
Moreover, the changes in many parameters occur simultaneously as the computation continues. In
handling such problems, analytical methods do not help much, but numerical methods play a vital
role to determine the effects on the results that are made when certain parameters are changed. At
the same time they provide an adequate solution of these problems as well.
Let us consider some other problems that will make the readers to understand the limitations of
“Analytical Solutions”.
1. Suppose we are given a data set of n values (T i, Vi), i = 1, 2, 3, …, n where T and V
represent respectively the temperature and volume of a particular gas. If it is required to
find the volume at some value of T that lie in the interval (Ti, Vi) or out side it, can we
find any analytical method that helps to find the value of V?
2. Consider an equation x2 – 6x + 5 = 0. We may use quadratic formula or otherwise to see
that x = 2 and x = 3 both satisfy this equation. In other words, 2 and 3 are the roots of
quadratic equation x2 – 6x + 5 = 0. Is there any analytical method to find the value of x

-:1:-
CHAPTER ONE ERROR ANALYSIS
x
that satisfies the equation e – 4x = 0? At a moment, you may have no answer of this as
well.
3. Suppose that a body covers a distance `s` at any time t where s is given by s = t3– t2 + 5.
The velocity at time t = 2 is given by v = ds/dt = 8 m/s. Now suppose one observes that a
body has covered different distances for different time intervals as shown under:
t (sec): 0 2 4 6 8 10 12
s(m) : 0 10 25 50 80 120 150
Can you tell what is the velocity of body when t = 3 seconds? We think you don’t have answer of
this question at this stage. In other words there is no analytical method that gives answer of this
question.
2 2
8
 
4. Consider a definite integral x 2 dx  . Can you evaluate cos x 2 dx by any analytical
3
0 0
method? Surely the answer is no.
dy x
5. One can solve the initial value problem  , y(1)  2 analytically. How about solving
dx y

dy x y
2 2
 , y(1)  2 analytically? You may have no answer to this as well.
dx xy
We have addressed a very few problems that possess no analytical solutions. There are many
problems like these whose analytical solution are definitely not available. Thus we are required to
use some other methods that provide the solution to these problems.
It may be noted that analytical solutions always provide exact solutions of the problem but when
such solutions are not possible, the only way of obtaining solution is the numerical or
approximate solution. The process of obtaining an approximate solution is possible by using
repetition of same steps so that the computations become automatic. Such a process is called a
Numerical Method and the derivation and analysis of such methods lie within the discipline of
Numerical Analysis. Thus the subject of Numerical Analysis is concerned with the derivation,
analysis and implementation of methods for obtaining reliable numeric answers to complex
mathematical problems. Numerical methods provide estimates that are very close to the exact
analytical solution. Obviously an error is introduced into the computation.
In the following section we discuss sources of errors and how to minimize the occurrence of
errors.
What is an Error
It is important to understand that an error here dose not mean a human error, such as a blunder or
mistake or oversight but rather a discrepancy between the exact and approximate or computed
values. All measurements, however, carefully and scientifically performed are subject to errors.
Errors once committed contaminate or pollute subsequent results. Error Analysis is the study and
evaluation of the errors. The main function of error analysis is to estimate the errors and suggest
ways to eliminate or minimize them. Investigations of error propagation are, of course,
particularly important in connection with iterative processes and computations where each value
depends on its predecessors.
An error in a numerical computation is the difference between the true or actual value of a
quantity and its computed or approximate value.
-:2:-
CHAPTER ONE ERROR ANALYSIS

If x represents the computed value of a quantity, and the true value is x*, then the difference,
x  x*  x is called the error of approximation.
Sources of Errors
A numerical method for solving a given problem will, in general, involve an error of several
types. Although different sources initiate the error, they all cause the same effect; that is,
diversion from the exact answer. Some errors are small and may be neglected, while others may
be distressing or upsetting if overlooked. In all cases, error analysis must accompany the
computational scheme, whenever possible.
The main sources of error are:
 Gross Errors
 Rounding Errors
 Truncation Errors
Gross Errors
Although gross errors are not directly concerned with most of numerical methods, they can have
great impact on the success of modeling efforts. These errors are either caused by human
mistakes or by the computing. A few examples of these errors are as follows:
a. Misreading or misquoting the figures, particularly in the interchanges of adjacent digits.
b. Use of inaccurate mathematical formula (algorithm) to solve a particular problem.
c. Use of inaccurate data.
Rounding Errors
When a numerical method is actually run on a digital computer, it forms a kind of error called
Round-off Error. The error introduced by rounding-off numbers to a limited number of decimal
places is called rounding error. Rounding-off errors play an important role in numerical analysis.
In order to obtain a smaller error as a result of rounding-off, we apply the following rules when
performing manual calculations. We discard/remove all digits after the first decimal place and
proceed as follows:
a. If the first discarded digit is less than 5, the previous digit is unchanged. For example,
the number 56.44, when rounded to the first decimal place it becomes 56.4.
b. If the discarded digit is greater than 5, the previous digit is increased by 1. For example,
the number 56.46 when rounded to the first decimal, it becomes 56.5.
c. If the discarded digit is exactly 5, the previous digit is unchanged, if it is even and is
increased by 1, if it is odd. For example, 56.45 becomes 56.4 and 56.75 becomes 56.8.
The number thus rounded-off is said to be correct to `n` significant figures. A table being
provided to grasp it easily:
Round-off to
Number
Three digits Four digits Five digits
3.142857143 3.14 3.143 3.1429
00.563241 00.563 00.5632 00.56324
89.5355 89.5 89.53 89.536
00.12556 00.126 00.1256 00.12556
00.887878 00.888 00.8879 00.88788

-:3:-
CHAPTER ONE ERROR ANALYSIS

Truncation Errors
An error arising by replacing one series by another with fewer terms is called truncation error.
x 2 x3
For example, ex = 1 + x + + + ...
2! 3!
x 2 x3
If we take 4 terms of the series, then we have e x = 1 + x + + + R 4 where R4 is remainder
2! 3!
after 4 terms of above series. In analyzing errors arising from truncation of series, several types of
series expansion can be considered, for example;
 Binomial expansion
 Infinite geometric progression
 Taylor series expansion
 Mac Lauren’s series expansion
In order to understand the truncation error, we turn to a mathematical formulation that is used
commonly in numerical analysis for expressing functions in an approximate fashion-the Taylor
series.
Statement of Taylor’s Series: Suppose that f(x) has n + 1 continuous derivatives on [a, b] and x0
is some point in [a, b] then, for all x  [a, b], there exists ζ(x) in the interval containing x0 and x
satisfying:
(x - x 0 )2 '' (x - x 0 )3 ''' (x - x 0 )n n
f (x) = f (x 0 ) + (x - x 0 )f ' (x 0 ) + f (x 0 ) + f (x 0 ) + ... + f (x 0 ) + R n+ 1
2! 3! n!
Here Rn+1 is the remainder after (n + 1) term given by
(x  x 0 )n 1 n 1
R n 1  f (x ) (x 0  x  x)
(n  1)!
If x0 = 0, then above series becomes:
x 2 '' x 3 ''' xn n
f (x) = f (0) + x f ' (0) + f (0) + f (0) + ... + f (0) + R n + 1
2! 3! n!
x n+ 1 n+ 1
This is Mac Lauren’s Series with remainder: R n + 1 = f (x)
(n + 1)!
Example 01: Use Taylor’s theorem to find the series for the function f(x) = 1n x at x = 1. Also
find the remainder.

Solution: f(x) = ln x f (1) = 0, f `(x) = 1/x, f `(1) = 1,


f ``(x) = -1/x2, f ``(1) = -1, f ```(x) = 2/x3, f ```(x) = 2!
iv iv
f (x) = 3!/x4, f (1) = -3!, etc.
In general, f n(1) = (–1)n-1 (n – 1)!. Hence,
 x – 12  x – 13  x – 1n
ln x  ln1+  x – 1 –  +... +( 1) n 1
2 3 n
n 1
(x  1) 1
where, R n+1  (1) n , for some x between 1 and x.
n  1 x 1
n

Now suppose we wish to find approximate value of ln 1.1 by using Taylor’s series. Taking say
n = 4 and x = 1.1, we obtain using the above series:

ln 1.1  0.1 –
 0.12   0.13   0.14  0.095308333
2 3 4
-:4:-
CHAPTER ONE ERROR ANALYSIS

(0.1)5 1
where, R 5  (1)5 , for some x between 1 and 1.1 NOTE : ln1  0
5 x5
1 (0.1)5 1
Now,  5   (0.1)5  2  106 , because x lies between 1 and 1.1.
5 x 5
Thus we may consider 0.095308333 as an approximation to ln 1.1 with an error less than
2 106 . The exact value of ln 1.1 = 0.095310179, showing that the actual difference between the
exact and approximate value is [0.095310179 – 0.095308333]  1.85 106 which is consistent
with the bound obtained above.
-3
Example 02: Determine the degree `n` that will assure an accuracy of 10 when ln 1.5 is
approximated using the result of Example 01.
(x  1)n 1 1
Solution: Since, R n+1  (1) n , for some x between 1 and x , we wish to achieve:
n  1 xn 1
(0.5) n 1
|R n+1 (1.5) |  103.
n 1
(0.5) n 1
Now, by “Hit & Trial” method, for  103  0.001 :
n 1
(0.5)11
If n = 1, then = 0.125 ≤ 0.001 (This is not true)
11
(0.5) 21
If n = 2, then = 0.0147 ≤ 0.001 (This is not true)
2 1
(0.5)31
If n = 3, then = 0.0156 ≤ 0.001 (This is not true)
3 1
(0.5) 41
If n = 4, then = 0.0063 ≤ 0.001 (This is not true)
4 1
(0.5)51
If n = 5, then = 0.0026≤ 0.001 (This is not true)
5 1
(0.5)61
If n = 6, then = 0.0011 ≤ 0.001 (This is not true)
6 1
(0.5)7 1
If n = 7, then = 0.0004 ≤ 0.001 (This is true)
7 1
This shows that for n ≥ 7, Taylor’s series provides a better approximation of ln 1.5 with a possible
error of 10-3.
Significant Figures
The digits used to express a number are called significant digits. A significant digit in an
approximate number is a digit which gives, reliable information about the size of the number. The
following rules are generally used:
i. Leading zeros are not significant. For example, the number 0.0002025. has four
significant digits.
ii. Following zeros that appear after the decimal point are significant. For example the
number 0.00202570 has six significant digits.
iii. Following zeros that appear before the decimal point may or may not be significant. For
example the number 202500 may have 4, 5, 6, or 7 significant digits depending upon the
situation.
-:5:-
CHAPTER ONE ERROR ANALYSIS

iv. The significant digit in a number does not depend on the position of decimal point in the
number. For example, 12345 and 0.12345 both contain 5 significant digits.
The significant figure in a number written in Scientific Notation (N  10k ) consists of all the
digits explicitly in N.
The following table is being provided how to find significant figure in a given number.
Number Significant digits Significant figure
6578 6,5,7,8 04
2010 2,0,1 03
9800 9,8 02
78.39 7,8,3,9 04
00.65987 6,5,9,8,7 05
59.00 5,9,0,0 04
0.000645 6,4,5 03
00.00480 4,8,0 03
7.0087 7,0,0,8,7 05
9.6 108 9,6 02
2.808 105 2,8,0,8 04
74  106 7,4 02

Accuracy and Precision


Accuracy is the number of digits to which an answer is correct. For example, the number 6.345
has 4 significant figures but it is correct to 3 decimal places accuracy.
Precision refers to the number of decimal positions or order of magnitude of the last digit in the
value. For example the number 53.968 has precision 10-3.
Example 01: Which of the following numbers have greatest precision?
4.3201, 4.32 and 4.3201069
Solution: Precision of number 4.3201 is 10-4
Precision of number 4.32 is 10-2
Precision of number 4.3201069 is 10-7
Hence the number 4.3201069 has the greatest precision.
Absolute Error
If x* is the actual value and x is an approximate value of a number, we define absolute error as
*
A.E  x  x*  x . For example, If x = 4.83 and x = 4.832, then

A.E  x  x*  x  4.83  4.832  0.002  0.002


Generally, if a number is correct to `n` decimal places, it has a rounding error
A.E  0.5 10n .
In the above example, x = 4.832 has 3 decimal places hence, A.E  0.5 103 = 0.0005 which is
less than 0.002.
Relative Error
| A.E | | x*  x | *
The relative error is defined as R.E  *  ,x  0
|x | | x* |
| A.E | 1
If x * (actual value is not known) then R.E  where A.E   10 n (x  0)
|x| 2

-:6:-
CHAPTER ONE ERROR ANALYSIS

0.002
From the above example, R.E = = 0.00041 .
| 4.83 |
Example 02: Suppose that 1.414 is used as an approximation to 2 . Find the absolute and
relative errors.
Solution: The true value of 2 =1.41421356. The approximate value is 1.414. Now

Error = True value – Approximate value = 1.41421356 - 1.414 = 0.00021356.

A.E = |Error| = |0.00021356| = 0.00021356 = 0.21356  10 .


-3

R.E = A.E/True value = (0.21356  10-3)/ 2 =0.000151 = 0.151  10-3.


Example 03: Three approximate values of 1/3 are: 0.30, 0.33 and 0.34. Which of these three is
the best approximation?
Solution: True value = 1/3 = 0.33333.
(i) If approximate value is 0.30 then A.E = |0.33333 – 0.30| = 0.03333
(ii) If approximate value is 0.33 then A.E = |0.33333 – 0.33| = 0.00333
(iii) If approximate value is 0.30 then A.E = |0.33333 – 0.34| = 0.00667
We observe that absolute error is least in case (ii), hence 0.33 is the best approximation.
Percentage Error
Relative error expressed in percentage is called the percentage error (P.E) and is defined by,
P.E  100  R.E . From the above example P.E  100  0.00041  0.041%
COMPUTER PROGRAM
The following programming code in C++ evaluates the absolute error, relative error, and
percentage error. The input values are to be supplied by the user.
#include<constream.h>
#include<math.h>
void main()
{ float x,x1,AE,RE,PE;
c o u t < < ” Enter the actual value: “ ;
cin>>x;
c o u t < < ” Enter the approximate value: “ ;
cin>>x1;
AE=fabs(x-x1);
RE=AE/fabs(x);
PE=RE*100;
cout<<"\nAbsolute Error (A.E) = “<<AE;
cout<<"\nRelative Error (R.E) = “<<RE;
cout<<"\nPercentage Error (A.E) = “<<PE;
getch();
}
Input
Enter the actual value: 4.83
Enter the approximate value: 4.832
-:7:-
CHAPTER ONE ERROR ANALYSIS

Out Put
Absolute Error (A.E) = 0.002
Relative Error (R.E) = 0.000414
Percentage Error (A.E) = 0.041405
Effects Of Rounding Errors In Arithmetic Operations
In this section we shall show that how the rounding errors in numbers affect their sum, difference,
multiplication and division. We shall also derive formulas for A.E and R.E, for each of these
fundamental operations.
Error Accumulation in Addition
Let x1 and x2 be two approximate numbers and z be their sum. That is,
z = x1 + x2 (1)
Let e1, e2 and ez be the errors in x1, x2 and z respectively. Therefore,
z – ez = (x1 – e1) + (x2 – e2) = (x1 + x2) – (e1 + e2) = z – ( e1 + e2)
or ez = (e1 + e2) giving | ez | = |e1 + e2| ≤ |e1| + |e2|
Thus A.E | ez |  | e1 |  | e2 |
The above proof can be extended to the sum of any number of factors, i.e.
A.E  ez  e1  e2  ...  en
Thus, absolute error of sum of n approximate numbers does not exceed the sum of the absolute
errors of the numbers. The relative error is calculated using the following relation;
Absolute Error | A.E |
R.E = =
sum of given numbers |z|
Error Accumulation in Subtraction
Let x1 and x2 be two approximate numbers and z be their sum. That is,
z  x1  x 2 , x1  x 2 . (2)
Let e1, e2 and ez be the errors in x1, x2 and z respectively. Therefore,
z - ez = (x1 - e1 ) - (x 2 - e2 ) = (x1 - x 2 ) - (e1 - e 2 )
From equation (2), we have e z  e1  e 2 .
Thus the absolute error of two approximate numbers is:
A.E  ez  e1  e2  e1  e2
The relative error is calculated using the following relation;
R.E = A.E / | z |
Remark: The range in which the true answer lies is given by: z ± A.E
Example 01: If the numbers 0.3062 – 0.25026 + 2.51392 are rounded, estimate the maximum
absolute and relative errors. Find also the range in which the true answer lies?
Solution: Let x1 = 0.3062, x2 = 0.25026 and x3 = 2.51392. Then,
z = x1 – x2 + x3 = 0.3062 - 0.25026 + 2.51392 = 2.56986
Let e1, e2, and e3 be the errors in x1, x2, and x3 respectively. Thus, the respective absolute errors
1 1 1
are as follows: e1  104 , e2  105 , e3  105
2 2 2
-:8:-
CHAPTER ONE ERROR ANALYSIS

1 1 1
A.E  e1  e2  e3   104   105   105  0.6  104
2 2 2
A.E. 0.6  104
R.E    0.2335  104
z 2.56986
The result lies in the range z  A.E  2.56986  0.6  104 or2.56980  z  2.56992
The answer may be rounded meaningfully to 2.57, which is correct to 3 s.f or 2 d.p.
Example 02: If the numbers 0.4162 – 0.36026 + 2.62392 are rounded, estimate the maximum
absolute and relative errors. Find also the range in which the true answer lies? Also write the
Computer program in C++ code.
Solution: Let x1 = 0.4162, x2 = 0.36026 and x3 = 2.62392.
Thus z = x1 – x2 + x3 = 0.4162 – 0.36026 + 2.62392 = 2.67986
Let e1, e2, and e3 be the errors in x1, x2, and x3 respectively. Thus, the respective absolute errors
1 1 1
are as follows: e1  104 , e2  105 , and e3  105  AE  e1  e2  e3
2 2 2
1 1 1 AE 6  105
Now, A.E   104   105   105  6  105  R.E    2.23892  105
2 2 2 z 2.67986
The result lies in the range z  A.E  2.67986  0.6 104
The answer may be rounded meaningfully to 2.68, which is correct to 2 decimal place (dp).
COMPUTER PROGRAM
#include<constream.h>
#include<math.h>
void main()
{ clrscr();
float x1= 0.4162, x2 = 0.36026, x3 = 2.62392, z, ez, e1, e2, e3, AE,RE;
z = x1- x2 + x3;
e1=0.5* pow(10,-4);
e2=0.5* pow(10,-5);
e3=0.5* pow(10,-5);
AE=fabs(e1) + fabs(e2) + fabs(e3);
RE=AE/z;
cout<<"z = " <<z <<endl;
cout<<",A.E = "<<AE<<endl;
cout<<",R.E = "<<RE;
getch();
}
Output
z = 2.67986 AE = 6e-05 RE = 2.238923e-05
Error Accumulation in Multiplication
Suppose, we want to multiply two approximate number, x1 and x2. Let z = x1 . x 2
As before, z  ez  (x1  e1 )(x 2  e2 )  x1x 2  x1e2  x 2e1  e1e2
Since e1 and e2 are small quantities, their product is still smaller and hence may be neglected.

-:9:-
CHAPTER ONE ERROR ANALYSIS

Thus, z  ez  x1x 2  x1e2  x 2e1  z   x1e2  x 2e1 


Therefore, ez  x1e1  x 2e2 (since z  x1 . x 2 )
ez e e
Now dividing by z, then we have: R.E   1  2
z x1 x2
Hence, the relative error of product of two numbers does not exceed the sum of the relative error
of given numbers.
Example 03: If the given numbers are rounded, estimate the relative and absolute errors of the
product, 4.0643  0.37487 . Find also range in which the product lies.
Solution: Let x1 = 4.0643 and x2 = 0.37487
As z  x1.x 2  4.0643  0.37487  1.5236 . Now, e1  0.5 104 ; e2  0.5 105
e1 e 0.5 104 0.5 105
Thus, relative error, R.E   2    0.2564  104
x1 x2 4.0643 0.37487
Absolute error, . A.E  R.E  z  0.2564 104 1.5236  0.39 104
Thus, the product lies in the range;
z  A.E  1.5236  0.39 104 1.523561  z  1.523639 . This is the error bound. If these
figures are rounded meaningfully, we get z = 1.5236, which is correct to 4 d.p.
Error Accumulation in Division
Suppose z  x1 / x 2 , where x 2  0 . Thus we can define,
 e 
x1 1  1  1
x e x1  x  e  e 
z  ez  1 1   or z  ez  1 1  1 1  2 
x 2  e2  e  x 2  x1  x 2 
x 2 1  2 
 x2 
Now expanding with help of binomial expansion, we have
x  e  e   x e  e  x e xe e xe
z  ez  1 1  1 1  2    1  1 1  2   1  1  1 22  z  1  1 22
x 2  x1  x 2   x 2 x1  x 2  x 2 x 2 x2 x2 x2
 e1 x1e2 
  2 
e1 x1e2 ez  x 2 x 2  x 2  e1 x1e2  e1 e2
Thus, ez   2 or     2  
x2 x2 z  x1  x1  x 2 x 2  x1 x 2
 x2 
 
e e e
This gives, RE  z  1  2
z x1 x 2
Hence, the Relative Error of a quotient of two terms is equivalent to the sum of the Relative
Errors.
3.0532
Example 04: For the number , estimate the following:
26.376
 a  The Relative Error  b  The maximum Absolute Error  c  The range in which the quotient lies
Solution: Let x1  3.0532 and x 2  26.376 . Then z  x1 / x 2  0.1158
Also, e1  0.5 104 , e2  0.5 103
e1 e 0.5 104 0.5 103 0.5  104 0.5  104
a  R.E   2      0.3533  104
x1 x 2 3.0532 26.376 3.0532 2.6376

-:10:-
CHAPTER ONE ERROR ANALYSIS

 b  A.E  R.E  z   0.3533 104    0.1158  0.0409  104


 c  The quotient lies in range: z  A.E  0.1158  0.0409 104
Errors in Powers and Roots
Let z = xn where n denotes an integer or a fractional quantity.
n
 e 
As before, z  ez   x  e1   x n 1  1 
n

 x
Expanding by the binomial theorem, and neglecting higher powers of e1/x, we have
 e 
z  ez  x n 1  n 1   x n  ne1x n 1 or ez  ne1x n 1
 x
ez ne1x n 1 ne1 e e
Now dividing by z, we get:  n
 or R.E  z  n  1
z x x z x
Hence, the “Relative Error” is modulus of a factor, raised to a power n is the product of the
modulus of power and the relative error of the factor.
0.82137
Example 05: Evaluate 6.2342  and find the minimum transmitted error if the given
2.7268
numbers are rounded.
x1x 2
Solution: Let x1  6.2342, x 2  0.82137, and x 3  2.7268 or z   1.37035 .
x3
Now, n  1/ 2; e1  e3  0.5 104 and e2  0.5 105 . Hence,
1  0.5  104 0.5  105 0.5  104  1  0.5  104 0.5  104 0.5  104  4
R.E           0.16222  10
2  6.2342 0.82137 2.7268  2  6.2342 8.2137 2.7268 
Also, A.E  R.E  z  0.16222  104  1.37035  0.2223  104  2.223  105
Thus the answer lies in the range z  A.E  1.37035  2.223  105 .
Error in Function Evaluation
Let z = f(x). As before, z + ez = f(x + e1). Applying the Taylor series expansion and neglecting
higher powers of e1 we have
z  ez  f  x   e1f   x   ez  e1f   x  [Note: z = f(x)]
Therefore, A.E  ez  e1f   x  . Now dividing by z, we have
ez e f (x)
R.E  1
z z
Example 06: Estimate the Absolute and Relative Errors if f(x) = cos x for x = 0.2345, where x is
rounded. Write a computer program in C++.
Solution: z  f  x   cos x  z  f .2345  cos  0.2345  0.9999
Also, f   x    sin x  f  .2345   sin  0.2345  0.0041
As x is rounded to 4 d.p, we have: e1  0.5 104
0.0041
 R.E  0.5 104   0.00205  104  0.205  106
0.9999
A.E  R.E  z  0.205  106  0.9999  0.2049  106
Remark: Angle x must be defined in radians.

-:11:-
CHAPTER ONE ERROR ANALYSIS

COMPUTER PROGAM
#include<constream.h>
#include<math.h>
#define f(x) (cos(x))
#define df(x) (-sin(x))
void main()
{ clrscr();
float AE, RE, x=0.2345, z, e = (0.5*pow(10,-4));
z = f(x);
RE = fabs(e*(df(x)/f(x)));
AE = RE*z;
cout<<"\n z = f(x) = "<<f(x);
cout<<"\n dz = df(x) = "<<df(x);
cout<<"\n RE = "<<RE;
cout<<"\n AE = "<<AE;
getch();
}
Output
z = f(x) = 0.999992, dz = df(x) = -0.004094, RE = 2.047196e-07, AE = 2.047179e-07

WORKSHEET 01
1. Find the Absolute and Relative Errors in each of the following cases (all numbers are rounded).
2.567  3.029
i  67.4  145.5 ii  0.561 5.7418 iii 
5.467
8.3425  0.87695
 iv   v  3.4(4.18  2.34)
29.876
2. If x = 3.25 and y = 4.75 are roundup numbers. Find the maximum absolute error involved in
evaluating:
i. x + y ii. x – y iii. x/y iv. x2 + xy + y2
v. x2 – xy + y2 vi. x2 y + xy2
3. The length and breath of a rectangle are given by 3.25 and 2.87 cm respectively, find the range
in which its area lies, giving the answer to as any dp as are meaningful.
4. Suppose 1.414 is considered to be an approximation of 2 . Find the absolute and relative
errors due to this choice.
5. If a = 0.1 and b = 0.01 are rounded numbers, calculate the maximum absolute error in a/b.
3 2
6. Evaluate the function f(x) = 0.51x – 3.18x – 2.4x + 17.1 for x = 0.14, where x is rounded.
Compute the absolute and relative Errors.
7. Compute the absolute and relative errors in computation of the following:
i. if f(x) = x – 1/x when x = 0.24
ii. if f(x) = x2 – 1/x2 when x = 0.76
iii. if f(x) = ex when x = 0.2345
iv. if f(x) = cos x when x = 0.2345. [Note: Here x is rounded]
-:12:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

CHAPTER
TWO SOLUTIONS OF
NON-LINEAR EQUATIONS

INTRODUCTION
Consider an equation f(x) = 0. If f(x) is quadratic or cubic polynomial such as x2 – 5x + 6 = 0 or
3 2
x – x + x – 1 = 0 respectively then, algebraic methods are available to find the x for which these
equations are satisfied. The value of x that satisfies the equation f(x) = 0 is known as “root” or
“Zero” of function f(x).
Geometrically, it is that value of x where the graph of f(x) touches or crosses the x-axis. In the
figure shown here, x = x1 and x = x2 are the roots of an equation
f(x) = 0 or zeros of the function f(x).
The algebraic methods are also called analytical methods as they
provide the exact solutions of such equations. In case when f(x) is
a polynomial of higher degree or an expression involving
transcendental functions e.g. 1 + cos x –5x, tan x – cosh x, e-x – sin x etc, it is impossible or
infeasible to find an analytical solution. In such cases only numerical methods are used to find the
solution x to required degree of accuracy. It may be noted that numerical methods are also known
as approximate or iterative methods. They are called approximate methods because the solutions
are not exact but approximate. Moreover, these methods are known as iterative methods because
process of finding the roots are iterative or recursive in nature.
The following sections are devoted to locate the root(s) of an equation f(x) = 0 where f(x) may be
algebraic or transcendental function of x.
ROOT LOCATION METHODS
Root location method can be divided in two types.
a. Bracketing Methods / Closed Methods
b. Open Methods
Bracketing Methods are of two types:
i. Bisection Method
ii. Regula Falsi / False Position Method
Open Methods are of three types:
i. Newton Raphson Method
ii. Fixed Point Iterative Method
iii. Secant Method

-:13:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

Bisection Method
Perhaps, this is the simplest and most robust method for finding a root of an equation f(x) = 0.
The Bisection method, which is alternatively called Binary Chopping, Interval Halving, or
Bolzano’s method, is one type of incremental search method in which the interval say [a, b] is
always divided in two equal sub-intervals (see below figure). This method is probably the most
primitive procedure for finding a real root of f(x) = 0. It is very simple but is very slow to
converge.

However, the convergence is guaranteed. For this reason, this method is often used for solving
non-linear equations. This method is particularly useful to find the root as it can easily be coded
using any programming language.
Algorithm of Bisection Method
Given an equation f(x) = 0 and an interval [a, b]:
I. If f(a).f(b) < 0, then root lies between a and b else try to find an other interval where the
above inequality holds.
II. Find m1, using midterm formula m1 = (a + b)/2
III. Evaluate f(m1)
i. If f(m1) = 0, then m1 is the root of f(x)
ii. If f(m1) ≠ 0, then two approaches are possible:
a) If f(a).f(m1) < 0, the root lies in the sub-interval [a, m1].
b) If f(a).f(m1) > 0, the root lies in the sub-interval [m1, b].
IV. Rename the sub-interval which contains the root as [a, b] and find m2= (a + b)/2. Then
evaluate f(m2).
V. Repeat the above process until the root is found with required accuracy.
The heart of Bisection method is the assumption that an interval [a, b] has been found, such that
f(a) f(b) < 0 and the method undertakes to decrease the size of the interval.
Example 01: Use the Bisection method to find the root of an equation x 3 – 9x + 1 = 0 correct to
10% accuracy. Also write the computer program in C++ code.
Solution: Given that f(x) = x3 – 9x + 1 (1)
Let a = 2 and b = 3 (Given initial guess values). Putting the initial guess values in given
polynomial (1), we have: f(a) = f(2) = 8 – 18 + 1 = – 9, f(b) = f(3) = 27 – 27 + 1 = 1.
Now, f(a). f(b) = f(2).f(3) = (–9)(1) = – 9 < 0. This shows that root lies in the interval [a, b] = [2, 3]
Now, m1 = (a + b)/2 = (3 + 2)/2 = 2.5. Putting m1 = 2.5 in given function, we get
-:14:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

f(m1) = f(2.5) = 15.625 – 22.5 + 1 = – 5.875


From the table shown below, we observe that root lies in the interval [m1, b] = [2.5, 3] = [a, b].
a=2
.
.m1 = 2.5 b=3
.

.f(a) = -9 .f(m1) = –5.875 .f(b) = 1

Now f(a).f(b) = f(2.5).f(3) = – 5.875 (1) = – 5.875 < 0. This shows that the root lies in the interval
[2.5, 3] = [a, b]. Now, m2 = (a + b)/2 = (2.5 + 3)/2 = 2.75, then
f(m2) = f(2.75) = 20.7969 – 24.75 + 1 = – 2.953
Observe that root lies in the interval [m2, b] = [2.75, 3] = [a, b]. This is shown below.
. a = 2.5 m2 = 2.75
.
b=3
.

.f(a) = –5.875 .f(m2) = –2.953 .f(b) = 1

The percent error x  m2  m1 100%  2.75  2.5  100%  25%


Now, m3 = (a + b)/2 = (2.75 + 3)/2 = 2.875, then
f(m3) = f(2.875) = 237637 – 25.875 + 1 = – 1.113
Observe that root lies in the interval [m2, b] = [2.875, 3] = [a, b]. This is shown below.

a = 2.75
.
.m3 = 2.875 b=3
.

.f(a) = –2.953 f(m3) = –1.13


.
.f(b) = 1

x  m3  m2 100%  2.875  2.75 100%  12.5% .


Now, f(m3) = f(2.875) = 23.7637 – 25.875 + 1 = – 1.113
Observe that f(m3).f(b) = f(2.875).f(3) = – 1.113 < 0.This shows that the root lies in the interval
[2.875, 3] = [a, b]. Now, m4 = (a + b)/2 = (2.875 + 3)/2 = 2.9375, then
f(m4) = f(2.9375) = 23.7637 – 25.875 + 1 = – 1.113
Observe that root lies in the interval [m4, b] = [2.9375, 3] = [a, b]. This is shown below.

.a = 2.875 .m4 = 2.9375 b=3


.

.f(a) = –1.113 f(m3) = –0. 09


.
.f(b) = 1

Observe that f(m4).f(b) = f(2.9375).f(3) = – 0. 09 < 0.This shows that the root lies in the interval
[m4, b] = [2.9375, 3] = [a, b].
The percent error is: x  m4  m3 100%  2.9375  2.875 100%  6.25% .
This shows that approximate root to 6.25% accuracy is 2.9375.
Remark: If more accuracy is required we continue the above process of halving the interval.
Some authors use “Relative Error” to compute the root to required accuracy. The following
computer program will allow you to enter as much accuracy you like to have.
In many engineering and technical problems we always require high class of accuracy. For
example, the results corresponding to 0.0001% accuracy are shown in the table given below after
the programming code is executed. We find that this accuracy is achieved at 20th iteration. The
results also show that after each iteration, the interval [a, b] is halved from one iteration to the

-:15:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

next. The following theorem reveals the same. For proof, refer to “Numerical Analysis: Theory
& Practice by N. S. Asaithambi, Saunders College Publishing.
Theorem: Let f(x) be a continuous function on [a, b], and let f(a) f(b) < 0. Let α be the exact
solution of f(x) = 0. Then the approximations xn produced by the bisection method
satisfy: |   x n | (b  a) / 2n , n  1 .
Remark: Always denote the sub-interval that contains the root by [a, b]. This will help in
program coding.
COMPUTER PROGRAM
#include<constream.h>
#include<math.h>
#include<stdio.h>
#define f(x) (pow(x,3) -9*x+1)
void main()
{
clrscr();
float a = 2, b=3, Rac, Ac, xn;
int count = 1;
float xo = a;
cout<<"Enter the required accuracy in % :";
cin>>Rac;
clrscr();
cout<<"Computation of a Root by Bisection Method\n";
cout<<"\n"<<"It#"<<setw(8)<<"a"<<setw(17)<<"b" ;
cout<<setw(20)<<"Root(x)"<<setw(18)<<"Accuracy";
cout<<"\n==================================\n";
do
{ xn=(a+b)/2;
Ac=fabs(xn-xo)*100;
cout<<endl<<count;
printf("\t%.6f\t%.6f",a,b);
printf("\t%.6f",xn);
printf("\t%.6f",Ac);
if(f(a)*f(xn)<0)
{ b=xn;
xo=xn;
}
else
{ a=xn;
xo=xn;
}
count++;

-:16:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

while(Ac>Rac);
cout<<"\n====================================";
cout<<"\n\nThe Approximate Root of Given Equation to ";
printf("%.4f",Ac);
cout<<"% Accuracy is ";
printf("%.6f",xn);
getch();
}
PROGRAM OUTPUT:
Computation of a Root by Bisection Method
It# a b Root(x) Accuracy
==============================================
1 2.000000 3.000000 2.500000 50.000000
2 2.500000 3.000000 2.750000 25.000000
3 2.750000 3.000000 2.875000 12.500000
4 2.875000 3.000000 2.937500 6.250000
5 2.937500 3.000000 2.968750 3.125000
6 2.937500 2.968750 2.953125 1.562500
7 2.937500 2.953125 2.945312 0.781250
8 2.937500 2.945312 2.941406 0.390625
9 2.941406 2.945312 2.943359 0.195312
10 2.941406 2.943359 2.942383 0.097656
11 2.942383 2.943359 2.942871 0.048828
12 2.942383 2.942871 2.942627 0.024414
13 2.942627 2.942871 2.942749 0.012207
14 2.942749 2.942871 2.942810 0.006104
15 2.942810 2.942871 2.942841 0.003052
16 2.942810 2.942841 2.942825 0.001526
17 2.942810 2.942825 2.942818 0.000763
18 2.942818 2.942825 2.942822 0.000381
19 2.942818 2.942822 2.942820 0.000191
20 2.942820 2.942822 2.942821 0.000095

“The Approximate Root of Given Equation to 0.0001% Accuracy is


2.942821”
While using this program, make changes in 4th line where we have used a function and the
interval values [a, b] in 8th line.
False Position Method
Although Bisection method is perfectly valid technique for determining roots, its convergence
approach is relatively slow. It is evident that the bisection method makes no use of the value of
-:17:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

f(x) at any point of interest; it uses only the sign of f(x) in the selection of an appropriate interval
containing the root. It may be helpful to take into account the actual value of f(x) at any point
under inspection. For instance, instead of taking the average (mid-point) of interval containing the
root, it may be useful to consider a “weighted average” of the end point.
The false position method is an alternative method and is more efficient than Bisection method. It
is the oldest method for finding the real root of an equation f(x) = 0 and is somewhat similar to
the bisection method.
Consider the equation f(x) = 0. Let [x0, x2] be two different values of x such that
f(x0).f(x1) < 0. The graph as shown below crosses the x-axis at point P between x0 and x1.
The equation of the chord joining the points [x0, f(x0)] and
[x1, f(x1)] is found by using the formula:
y 2  y1
y  y1  (x  x1 )
x 2  x1
f (x1 )  f (x 0 )
 y  f (x 0 )  (x  x 0 )
x1  x 0
Now on the x-axis, y = 0. Thus above equation becomes:
f (x1 )  f (x 0 ) f (x 0 )(x1  x 0 )
0  f (x 0 )  (x  x 0 ) or   (x  x 0 )
x1  x 0 f (x1 )  f (x 0 )
f (x 0 )(x1  x 0 ) f (x n 1 )(x n  x n 1 )
 x  x0  or x n 1  x n 1 
f (x1 )  f (x 0 ) f (x n )  f (x n 1 )
x f (x )  x n f (x n 1 )
Simplifying further, we get: x n 1  n 1 n
f (x n )  f (x n 1 )
This is known as Regula Falsi iterative formula.
If we consider an interval [a, b] where the root of f(x) = 0 lies then above formula may be
af (b)  bf (a)
expressed as: x
f (b)  f (a)
Algorithm of Regula Falsi Method
Given an equation f(x) = 0 and an interval [a, b]:
I. If f(a) f(b) < 0 then root lies in the given interval else find other interval where this
condition is satisfies.
af (b)  bf (a)
II. Find x1, by using formula x1 
f (b)  f (a)
III. Evaluate f(x1)
i. If f(x1) = 0, then x1 is the root of f(x).
ii. If f(x1) ≠ 0, then two possibilities occur:
a. If f(a).f(x1) < 0 then root lies in the sub-interval [a, x1]. We call
this new sub-interval as [a, b] and compute the iterate x2 from:
af (b)  bf (a)
x2 
f (b)  f (a)

-:18:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

b. If f(x1).f(b) < 0 then root lies in the sub-interval [x1, b]. We denote this
af (b)  bf (a)
sub-interval by [a, b] and compute x2, from: x 2 
f (b)  f (a)
The process is repeated until the root is estimated adequately.
Example 03: Use False Position method find, an approximate root of an equation x 3 – 9x + 1 = 0
lies in the interval [2, 3] correct to 10% accuracy. Also write the computer program in C++ code.
Solution: Given f(x) = x3 – 9x + 1 and the interval [a, b] = [2, 3].
First putting the given initial guess values in the given polynomial, we have
f(a) = f(2) = 8 – 18 + 1 = – 9, f(b) = f(3) = 27 – 27 + 1 = 1.
Now f(a)f(b) = f(2)f(3) = – 9 < 0, hence the root lies in the interval [2, 3]. Thus,
af (b)  bf (a) 2 1  3  (9) 2  27
x1     2.9  f (x1 )  f (2.9)  0.711
f (b)  f (a) 1  (9) 10
We see that f(x1) f(b)=f(2.9)f(3)= -0.711 < 0.
This shows that root lies in the subinterval [2.9, 3] = [a, b].
af (b)  bf (a)
Now we compute x 2   2.94  f (x 2 )  f (2.74)  0.048
f (b)  f (a)
The percent error, x  x 2  x1  100  4% . We see that f(x2).f(b) = –0.048 < 0, hence the root
af (b)  bf (a)
lies in the sub-interval [x2, b] = [2.94, 3] = [a, b]. Now we compute x3   2.943
f (b)  f (a)
The percent error is x  x3  x 2 100  2.943  2.94  100  0.3% .
Hence the approximate root to 0.3% accuracy is x3 = 2.943.
Remark: In bisection method we found that the approximate root was 2.9375 with 6.25%
accuracy and it was achieved after 5th iteration whereas a better accuracy is achieved by false
position method with less number of iterations. Moreover, the accuracy of 0.0001% is achieved in
6th iteration (as shown in the output of the program) whereas the same accuracy in bisection
method is achieved in 20th iteration. This indicates that “False Position” method converges to the
root faster than bisection method.
COMPUTER PROGRAM
#include<constream.h>
#include<math.h>
#include<stdio.h>
#define f(x) (pow(x,3)-9*x+1)
void main()
{ clrscr();
float a = 2, b = 3, Rac, Ac, xn;
int count=1;
float xo=a;
cout<<"Enter the required accuracy in % :";
cin>>Rac;
clrscr();
cout<<"Computation of a Root by False Position Method\n";

-:19:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

cout<<"\n"<<"It#"<<setw(8)<<"a"<<setw(17)<<"b"<<setw(20)<<
"Root(x)"<<setw(18)<<"Accuracy";
cout<<"\n==========================================\n";

do
{ xn=(a*f(b)-b*f(a))/(f(b)-f(a));
Ac=fabs(xn-xo)*100;
cout<<endl<<count;
printf("\t%.6f\t%.6f",a,b);
printf("\t%.6f ",xn);
printf("\t%.6f",Ac);
if(f(a)*f(xn)<0)
{ b=xn;
xo=xn;
}
else
{ a=xn;
xo=xn;
}
count++;
}
while(Ac>=Rac);
cout<<"\n============================================";
cout<<"\n\nThe Approximate Root of Given Equation to ";
printf("%.6f",Ac);
cout<<"% Accuracy is ";
printf("%.6f",xn);
getch();
}
PROGRAM OUTPUT
Computation of a Root by False Position Method
It# a b Root(x) Accuracy
===========================================
1 2.000000 3.000000 2.900000 90.000008
2 2.900000 3.000000 2.941555 4.155445
3 2.941555 3.000000 2.942783 0.122881
4 2.942783 3.000000 2.942819 0.003552
5 2.942819 3.000000 2.942820 0.000119
6 2.942819 2.942820 2.942820 0.000000
===========================================
“The Approximate Root of Given Equation to 0.000000% Accuracy is 2.942820”

-:20:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

Pitfalls of False Position Method


Although false position method is better than bisection method as it converges to the root faster
than the bisection method, there are cases where its performance is very poor. In such cases,
bisection method works better than it.
Example 04: Use both bisection and false position method to compute the approximate solution
of x10 – 1 = 0.
Solution: We are not providing the step-by-step solution of this problem. On the other hand we
have produced here the computer output using both methods. The interval where the root lies is
[0, 1.3] and the exact root is x = 1. You may compare the results and realize that bisection method
provides the approximate root x = 1.015625 accurate to 4% at the 5th iteration where as false
position method gives an accuracy of 17.1% at the 5th iteration where the approximate root is
0.40788. The reason for the slow convergence of false position method is clear from the graph of
the given function f(x) = x10 – 1 given below.
“Out put using Bisection Method”
It# a b Root(x) Accuracy (%)
============================================
1 0.000000 1.300000 0.650000 100.0000
2 0.650000 1.300000 0.975000 33.30000
3 0.975000 1.300000 1.137500 14.30000
4 0.975000 1.137500 1.056250 7.700000
5 0.975000 1.056250 1.015625 4.000000
============================================

“Output using False Position Method”


It# a b Root(x) Accuracy (%)
============================================
1 0.000000 1.300000 0.0.09430 100.0000
2 0.094300 1.300000 0.1817600 48.1000
3 0.181760 1.300000 0.2628700 30.9000
4 0.262870 1.300000 0.3381150 22.3000
5 0.338110 1.300000 0.4078800 17.1000
============================================
Newton-Raphson Method
Instead of working with the chord joining two points on the graph of y = f(x) as in bisection and
false position methods, Newton’s method uses the tangent at one point on the graph of y = f(x).
Therefore, it requires only one instead of two initial guesses.
Suppose x0 is an initial guess. Draw the tangent line at x = x0 and
. produce it to meet the x-axis at x = x1.
This is shown in the figure below. Now from the right triangle
f (x 0 )
tan   (1)
x 0  x1

-:21:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

But tan θ is the slope of tangent line at x = x0. Hence, tan θ = f `(x0).
f (x 0 ) f (x 0 )
Thus equation (1) becomes: f (x 0 )  or (x 0  x1 ) 
x 0  x1 f (x 0 )
f (x 0 ) f (x n )
 x1  x 0  . Subsequent iterations are defined similarly as x n 1  x n 
f (x 0 ) f (x n )
. provided f `(xn) ≠ 0. This is Newton Raphson formula.
Drawback/failure of NR method
The drawbacks or pitfalls of this method are:
(i) At any point of computation if f `(xn) = 0, that is,
the tangent is parallel to the x-axis then this method will
not work at all.
.

(ii) If the initial guess lies away from the real root, the convergence will be too slow.
Example 05: Use Newton Raphson Method to obtain a root of f(x) = xex-2 correct to 3dp.
Solution: As an initial guess is not given hence it is to be evaluated first as it is the requirement
of NR-method.
x 0 1 2
f(x) –2 0.718 12.778

From the table we see that function changes the sign in the interval [0, 1]. This means that the
root lies in the interval [0, 1]. Thus we let an initial guess to be x0 = 1. It may be noted that if an
interval [a, b] is founded such that f(a) . f(b) < 0 and if |f(a)| < |f(b)| then for the fast convergence
it would be better if x0 = b is taken as an initial guess. Now, f `(x) = xex + ex. Using N-R
algorithm, we get:
f (x 0 ) 0.71828
n  0, x1  x 0  1  0.70521
f (x 0 ) 2.43656
f (x1 ) 0.57246
n  1, x 2  x1   0.71828   0.87105  x  x 2  x1  0.87105  0.70521  0.16584

f (x1 ) 3.45181
f (x 2 ) 0.08130
n  2, x 3  x 2  '
 0.87105   0.85287  x  x 3  x 2  0.85287  0.87105  0.01818
f (x 2 ) 4.47072
f (x 3 ) 0.00115
n  3, x 4  x 3   0.85287   0.85261  x  x 4  x 3  0.85261  0.85287  0.00026
f (x 3 ) 4.34752
Hence at 4th iteration approximate root is 0.85261 which is correct to 3 dp.
COMPUTER PROGRAM
#include<constream.h>
#include<math.h>
#include<stdio.h>
#define f(x) (x*exp(x) -2)
#define fd(x) (x*exp(x)+exp(x))
void main()
{ clrscr();
float xo,xn,Rac,Ac;
cout<<"Enter the initial guess ";
-:22:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

cin>>xo;
cout<<"\nEnter the required accuracy in dp ";
cin>>Rac;
clrscr();

int count=1;
cout<<"Computation of the Root by Newton Raphson Method";
cout<<"\n\n"<<"It#"<<setw(15)<<"ROOT"<<setw(25 )<<"Accuracy";
cout<<"\n===================================";
do
{ xn= xo-f(xo)/fd(xo);
Ac=fabs(xn-xo);
cout<<"\n"<<count<<setw(20)<<xn<<setw(20)<<Ac;
count++;
xo=xn;
}
while(Ac>=Rac);
cout<<"\n===================================";
cout<<"\n\n”The Approximate Root of given equation with accuracy of
6dp is";
printf("%.6f",xn);
getch();
}
Computation of the Root by Newton Raphson Method Required Accuracy is 0.0001
It# Root Accuracy
==============================
1 0.867879 0.132121
2 0.852783 0.015096
3 0.852606 0.000178
4 0.852606 0
==============================
The Approximate Root of given e quation with accuracy of 6dp is
0.852606.
Simple Iteration Method / Fixed Point Iteration Method
Simple iteration which is usually known as fixed point iteration method is useful method. It
involves rearranging or transforming the equation f(x) = 0 in the form x = g(x). The only
restriction on this transformation is that there must be one to one relationship with solutions to
x = g(x) for the roots being sorted. The efficiency and convergence of this method depends on the
final form of g(x). The above transformation is then recursively used as follows:
x1 = g(x0) as a 1st approximation
x2 = g(x1) as a 2nd approximation
………..
xn+1 = g(xn) as the (n + 1)st approximation
-:23:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

Remark: While rearranging the equation f(x) = 0 in the form x = g(x), g(x) should be chosen so
that |g`(x0)| < 1.
The following theorem will help the readers to understand the above idea of imposing this
condition on g(x).
Theorem: If α  [a, b] be a root of f(x) = 0 which is rearranged into x = g(x) and |g`(x)| < 1 for all
x  [a, b], then the sequence of approximations x1, x2, …, xn will converge to the root `α`
provided the initial approximation x0  [a, b].
Proof: By mean value theorem if g(x) is derivable in some interval [a, b] and that c is some point
g(b)  g(a)
in [a, b] then g(c) 
ba
Now xn+1 = g(xn) and xr+1 = g(xr) or xn+1 – xr+1 = g(xn) – g(xr). Now for convergence, we
must have |xn+1 – xr+1| < 1. In other words,
g(x n )  g(x r )
1
xn  xr
Then using mean value theorem, it fallows that |g`(c)| < 1.
3
Example 06: Find an approximate root of an equation x + x – 1 = 0 by fixed point iteration
method taking an initial guess x0 = 1 correct to 2 d.p.
Solution: Let us rearrange the given equation in the form: x = 1 – x3.
Here g(x) = 1 – x3 giving g`(x) = – 3x2. Now, |g`(1)| = |–3| = 3 > 1. Hence the condition
|g`(1)| < 1 fails. This means with the above choice of g(x) the iteration scheme will not work, that
is, the sequence x1, x2, … will diverge. Now if the above equation is rearranged in the form:
1 1
x(x 2  1)  1 or x  g(x) 
x 1 x 1
2 2
2 2
Now, g`(x) = –2x/(1 + x ) then |g`(1)| = 0.5 < 1. Thus the condition |g`(1)| < 1 is satisfied. This
shows that the above scheme will converge as is worked out below.
1
Define an iterative scheme as: x n 1  2
xn  1
1 1 1
Put n  0, x1  2  2   0.5  x | x1  x 0 |  | 0.5  1| 0.5
x0  1 1  1 2
1 1
Put n  1, x 2    0.8  x | x 2  x1 || 0.8  0.5 | 0.3
x 21 1 0.52  1
1 1
Put n  2, x 3    0.60976  x  x 3  x 2  0.60976  0.8  0.19024
x2  1
2
(0.8) 2  1
1 1
Put n  3, x 4    0.72896  x  x 4  x 3  0.72896  0.60976  0.1192
x 3  1 (0.60976) 2  1
2

If above process is continued we find that the approximate root to 2 d.p is obtained at 10th
iteration as is shown in the table shown below. The values in the table are obtained by running a
computer program. However, an accuracy of 4 d.p is achieved at 20th iteration. This is also
depicted in the results in the table.
COMPUTER PROGRAM
#include<constream.h>
#include<math.h>
-:24:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

#include<stdio.h>
#define g(x) (1/((x * x)+1))
void main()
{ clrscr();
float xo, xn, Rac, Ac;
cout<<"Enter the initial guess ";
cin>>xo;
cout<<"\nEnter the required accuracy in dp ";
cin>>Rac;
clrscr();
int count=1;
cout<<"Computation of the Root by Fixed Point Iteration Method";
cout<<"\n Required Accuracy is "<<Rac;
cout<<"\n\n"<<"It#"<<setw(15)<<"Root"<<setw(25)<<"Accuracy";
cout<<"\n===========================================";
do
{ xn=g(xo);
Ac=fabs(xn-xo);
printf("\n%d\t\t%.6f\t\t%.6f",count,xn,Ac);
count++;
xo=xn; }
while(Ac>=Rac);
cout<<"\n===========================================";
cout<<"\n The Approximate Root of Given Equation with Required
Accuracy of is ";
printf("%.6f",xn);
getch(); }
PROGRAME OUT PUT:
“Computation of the Root by Fixed Point Iteration Method. Required Accuracy is 10-4”
It# Root Accuracy It# Root Accuracy
1 0.500000 0.500000 11 0.680394 0.004980
2 0.800000 0.300000 12 0.683557 0.003163
3 0.609756 0.190244 13 0.681547 0.002010
4 0.728968 0.119212 14 0.682824 0.001277
5 0.653000 0.075968 15 0.682013 0.000811
6 0.701061 0.048062 16 0.682528 0.000515
7 0.670472 0.030590 17 0.682201 0.000328
8 0.689878 0.019406 18 0.682409 0.000208
9 0.677538 0.012339 19 0.682276 0.000132
10 0.685374 0.007835 20 0.682360 0.000084

The Approximate Root of Given Equation with Required Accuracy of 4 d.p is 0.682360.

-:25:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

Comparison of methods
Below is the comparison of alternative methods for finding root of f(x) = 0.
Method Initial guess convergence Stability Accuracy Comments
Bisection 2 Slow Always Good
False 2 Slow/medium Always Good Requires
positional Derivative of
Function f(x)
Newton- 1 Fast Not always/ Good
Raphson May diverge

Fixed point 1 Slow Not always/ Good Time consuming in


May diverge choosing g(x)

WORKSHEET 02
1. Find an approximate root by using bisection method correct to 5% accuracy.
a. x3 – x2 – 2x + 1 = 0 b. 2x – ln x – 7 = 0 c. 2ex – sin x = 0

2. Using “False Position” method, find an approximate value of the root of the following
equations correct to 2 d.p.
a. x2 – 5x + 6 = 0 correct to 3 dp. b. x3 + x2 – 3x – 3 = 0 correct to 3dp
x
c. 1 + cos t – 4t = 0 in (0, 1) correct to 4 dp d. x e = 1
3. Determine the approximate root of the following equations up to 4 decimal places using
Newton Raphson method.
a. 4sinx = ex in (0, 0.5) b. ex sin x = x/2, taking x0 = π
4 3 2
c. x + 0.5x + 0.8x – 1.66x – 1.32 = 0 d. 2sinhx = cosh x in (0, 1)
4. Find the diameter D of the pipe which satisfies the flow equation
5
882D – 2.31D – 0.6446 = 0 with D0 = 1, correct to 3 decimal places.
5. The volume of a gas depends on the temperature. Volumes V1 and V2 of two gases under
T
ideal situations are given by V1 = e and V2 = 4T, T being the temperature. For what value of T
are the volumes of the gases equal? (Hint: You may use N-R or False Position method).
6. Find the root of f(x) = 0. The pollutant bacteria concentration C in a lake varies
as 1  8e2t  2e0.1t , then calculate the time required for the bacteria concentration by using
Newton Raphson method.
7. A plastic ball is floating in a bucket filled with water the immersed height (measured from the
bottom of the partly sub merged ball) is hand the radius of ball is r. The following equation can be
derived using Archimedes’s principle: r 3  0.25l h 2 (3r - h) / s where l and s are the
densities of water and plastic, respectively. Apparently, s  l , if the radius of the ball r = 10 cm,

s  0.5 gcm-3 and l  1 gcm -3 , find the immersed height h by solving the above equation using
the Bisection method. What values of the lower and upper limits of the bracket will you use?

 
8. The diode equation i  Is e v   1 , determines current i (in amperes) through a diode, where

Is is the saturation current (in amperes),  is the diode variable and v is the voltage across the
diode. A junction diode for which Is  20 A and   0.052 must also satisfy the equations:

-:26:-
CHAPTER TWO SOLUTIONS OF NON-LINEAR EQUATIONS

v  104 i  4 and i  Is v  . Eliminating I and find v. Then use the values of Is and θ to compute
the value of v. Now use the values of v, Is and θ find i using any numerical method to 1%


accuracy. Note : A  106 ampere 
9. In the analysis of the anti-symmetric buckling of beams, a factor  satisfies 0     / 2 and
6cos     sin , where  depends on the geometry and the critical stress on the beams.
Determine  given   0.1,0.5,1, and 5 by using Newton’s method. Use your own initial guesses,
with required accuracy   103 .
10. The resistance R in the electrical network varies with the current I according to R = A + BI3/2
by Ohm’s law, V = I.R. Determine I, given that V = 5, A = 100, and B = 10.
11. According to Archimedes’ law, when a solid of density  is placed in a liquid of density , it
will sink to a depth h that displaces an amount of liquid whose weight equals the weight of the

solid. For a sphere of radius r, Archimedes’ law translates to


1
3
  4
 3rh 2  h3   r3, given
3
r  5,   1, and   0.6, determine h, by using Bisection method.
12. Show that the equation x = 1 + tan-1x has a solution  .Determine an interval [a, b] containing
 such that for every the iteration x (n 1)  1  tan 1 x (n) , will converge to  . Determine  .
13. Consider solving the equation by using fixed point iteration for a solution in the interval
[2, 3]. Without performing any iteration, examine each of the following g(x) for convergence:
1 5
  5
13
a. g  x    x 2   b. g  x   2x 2  5 c. g  x   2  2
2 x x
14. Solve each of the following equations by fixed point iteration. Start with the indicated initial
guess x (0) . Use the termination criterion with   104 .
a. x 3  4x 2  10  0, x (0)  1.5 b. x 2  x  1  0, x (0)  1
c. x 2  17  0, x (0)  4
15. Solve each the following equation by fixed point iteration, using the given g(x) and starting
with the indicated x (0) . Use the termination criterion with   104 .
ex
i. ex  3x 2  0, g(x)  , x (0)  1
3
ii. 0.2sin x  x  0.5  0, g(x)  0.2sin x  0.5 x (0)  0.5
iii. sin x  x  6.28  0 g(x)  6.28  sin x
16. The volume of gas depends on the temperature. Volumes V1 and V2 of two gases under ideal
situations are given by V1 = eP and V2 = 4P, P being the pressure. Using Regula Falsi method find
for what value of P are the volumes of the gases equal.

-:27:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

CHAPTER
THREE SOLUTIONS OF
LINEAR EQUATIONS

INTRODUCTION
Solving equations of the form Ax = b is central to many numerical algorithms. There are number of
methods which may be used, some provide analytical solution, while the others are iterative in
nature and providing only approximate solutions. Which is the best? It depends on the structure of
A, the context in which it is to be solved and the size compared with the available computer
resources. Readers are familiar with few of these methods namely “Inverse Matrix Method”,
“Cramer’s Rule Method, “Gauss Elimination and Gauss Jordan Methods”. In this chapter we shall
present methods which are new to the readers and very much efficient when their algorithms are
transferred to any programming language. We shall present two types of methods, “Non-Iterative
Methods” and “Iterative Methods”.
Non-Iterative (Direct) Methods
Non iterative methods are those that are programmed easily but their programming contains no
iterative process.
Triangular Decomposition (Factorization) Method
This method is based on the fact, that a non-singular square matrix A of the system A x = b is
decomposed into two factors such as A = L U, where L is the Lower Triangular Matrix and U is the
Upper Triangular Matrix.

l11 0 0   u11 u12 u13 


   
i.e. L  l21 l22 0  and U  0 u 22 u 23 
l31 l32 l33  0 0 u 33 

Such a factorization is unique if the diagonal elements of either of L or U are unity (1s). If the
diagonal elements of L are taken to be unity, the resulting scheme has the three methods:
(i) If A is not Symmetric and positive definite then this scheme is called LU- Decomposition
Method.
(ii) If A is a Symmetric Positive Definite Matrix that is, A = AT, det(A) > 0 and determinant of
minor matrix is also positive, then this scheme is called Cholesky’s Method. In this case the
decomposition of matrix A is defined as: A = L.LT.
(iii) If we choose the diagonal elements of U as unity, the resulting scheme is called Crouts
Method / Crouts Reduction.
LU-Decomposition Method
A frequently used form of Gauss Elimination is LU Factorization which is also known as LU
Decomposition method. Notice that this decomposition is underspecified in that we may choose the
-:28:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

relative scale of the two matrices arbitrarily. By convention, the L matrix is scaled to have a leading
diagonal of unit values. Once we have computed L and U we need to solve only Ly = b then
Ux = y. To understand the method, let us consider a system of three variables in three unknowns
(variables). For example, consider
a11x1  a12 x 2  a13 x 3  b1
Ax = b, that is; a 21x1  a 22 x 2  a 23 x 3  b2
a 31x1  a 32 x 2  a 33 x 3  b3
Now let us assume that matrix A = L U (1)
l11 0 0   u11 u12 u13 
   
where L  l21 l22 0  and U  0 u 22 u 23 
l31 l32 l33  0 0 u 33 

Then (1) can be written as L. Ux=b (2)


If we set: Ux=y (3)
then (2) becomes: Ly=b (4)
1 0 0   y1   b1 
    
Which is equivalent to: l21 1 0  y2   b2 
l31 l32 1   y3   b3 
After simplification, we have
y1  b1
l21y1  y2  b2 or y2  b2  l21b1
l31y1  l32 y2  y3  b3 or y3  b3  l31b1  l32 (b 2  l21b1 )
Solve the above for y1 , y 2 , and y3 , using forward substitution. Now, putting y values in (3) we
 u11 u12 u13   x1   y1 
    
compute the values of x from: 0 u 22 u 23   x 2    y2 
0 0 u 33   x 3   y3 
Simplifying, we get: u11x1  u12 x 2  u13 x 3  y1 , u 22 x 2  u 23x 3  y 2 , u 33x 3  y3
This can be simplifying for x1 , x 2 , and x 3 by using backward substitution.
Now there remains to find the relationship between the elements of A, L and U. These are given as
under: As we know that LU = A.
l11 0 0   u11 u12 u13 
   
where L  l21 l22 0  and U  0 u 22 u 23 
l31 l32 l33  0 0 u 33 
1 0 0   u11 u12 u13  a11 a12 a13 
    
Therefore, LU  l21 1 0 0 u 22 u 23   a 21 a 22 a 23 
l31 l32 1  0 0 u 33  a 31 a 32 a 33 
 u11 u12 u13   a11 a12 a13 
l u   a a 23 
Thus,  21 11 l21u12  u 22 l21u13  u 23   21 a 22
 l31u11 l31u12  l32 u 22 l31u13  l32u 23  u 33  a 31 a 22 a 33 

-:29:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

u11  a11 , u12  a12 , u13  a13 



a 21 a 31 
l21u11  a 21 or l21  , l31u11  a 31 or l31 
a11 a11 

a11a 22  a 21a12 
l21u12  u 22  a or u 22   (5)
22 a11 
a11a 23  a 21a13 a 32  l31u12 
, l21u13  u 23  a 23 or u 23  ,l31u13  l32 u 22  a 32 or l32  
a11 a11 
l31u13  l32 u 23  u 33  a 33 or u 33  a 33  l31u13  l32 u 23 

The method of computing elements of matrices L and U is straight forward and can easily be coded
using any kind of high level programming language. We now present an example that will help the
readers to understand the use and advantage of LU-decomposition method over other non-iterative
methods described above.
Example 01: Solve the system of equations by LU-Decomposition Method
2x1  0x 2  x 3  4,  3x1  4x 2  2x 3  3, x1  7x 2  5x 3  6
Solution: Taking LU = A.
l11 0 0   u11 u12 u13 
   
Here L  l21 l22 0  and U  0 u 22 u 23 
l31 l32 l33  0 0 u 33 
1 0 0   u11 u12 u13   2 0 1
  
Therefore, LU  l21 1 0  0 u 22 u 23    3 4  2 
l31 l32 1  0 0 u 33  1 7  5
Using the equations given in (1), we get:
3
u11  2, u12  0, u13  1.Now, l21u11  3  l21   , l21u12  u 22  4  u 22  4
2
1 1
l21u13  u 23  u 23   , l31u11  1  l31 
2 2
7 37
l31u12  l32 u 22  7  l32  , l31u13  l32 u 23  u 33  5  u 33  
4 8
1 0 0  1 0 0 
  
L  l21 1 0   3 2 1 0  and
l31 l32 1   1 2 76 1
 u11 u12 u13   2 0 1 
  
U  0 u 22 u 23    0 4  1 2 
0 0 u 33  0 0  37 8
Now to find values of y by using the equation Ly = b, we have
 1 0 0   y1   4 
  
  3 2 1 0   y2    3
 1 2 7 4 1  y3   6 
After simplification, using forward substitution, we obtain
3 1 7 5
y1  4,  y1  y 2  3  y 2  3, y1  y 2  y3  6  y3  
2 2 4 4
 (y1 , y 2 , y3 )  (4, 3, 5 / 4)

-:30:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

Now to find the values of x’s by using the equation U x = y, we have


2 0 1   x1   4 
0  
 4  1 2   x 2    3 
0 0  37 8  x 3   5 / 4 
After simplification, using backward substitution, we have
37 5 10 1 29 69
 x3    x3  , 4x 2  x3  3  x 2  , 2x1  x3  4  x1 
8 4 37 2 37 37
 69 29 10 
Hence the solution vector is  x1 , x 2 , x 3    , , .
 37 37 37 
COMPUTER PROGRAM
#include<constream.h>
#include<math.h>
#include<iomanip.h>
void main()
{ clrscr();
float a[5][5],L[5][5],u[5][5],n,b[5],y[5],x[5];
int i,j;
cout<<" \nEnter the total number of linear equations (5): ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
c o u t <<"\n\nEntre the co-efficents of a["<<i<<"]["<<j<<"] : ";
cin>>a[i][j];
}
cout<<"\nEnter the constant of b["<<i<<"] : ";
cin>>b[i];
clrscr();
}
gotoxy(20,1);
cout<<"LU-Decomposition Method";
gotoxy(19,2);
cout<<"************************"<<endl;
c o u t <<"\nLower and Upper Matrices Component\n\n";

for(i=2;i<=n;i++)
for(j=1;j<=n-1;j++)
u[i][j]=0;
for(i=1;i<=n-1;i++)
for(j=2;j<=3;j++)
L[i][j]=0;
-:31:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

for(i=1;i<=n;i++)
{
L[i][i]=1;
u[1][i]=a[1][i];
}
L[2][1]=a[2][1]/u[1][1];
u[2][2]=a[2][2]-L[2][1]*u[1][2];
u[2][3]=a[2][3] -L[2][1]*u[1][3];
L[3][1]=a[3][1]/u[1][1];
L[3][2]=(a[3][2] -L[3][1]*u[1][2])/u[2][2];
u[3][3]=a[3][3] -L[3][1]*u[1][3] -L[3][2]*u[2][3];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
cout<<"\tu"<<i<<j<<" = "<<u[i][j];
c o u t <<setw(10)<<"l"<<i<<j<<" = "<<L[i][j]<<endl<<endl;
}
y[1]=b[1];
y[2]=b[2]-L[2][1]*y[1];
y[3]=b[3]-L[3][1]*y[1] -L[3][2]*y[2];
x[3]=y[3]/u[3][3];
x[2]=(y[2]-u[2][3]*x[3])/u[2][2];
x[1]=(y[1]-u[1][2]*x[2]-u[1][3]*x[3])/u[1][1];
cout<<"\n\n\t*********LY = b **********\n\n";
for(i=1;i<=3;i++)
cout<<"\ty["<<i<<"] = "<<y[i];
c o u t <<"\n\n\n\t********* ** UX = Y **********\n\n";
for(i=1;i<=3;i++)
cout<<"\tx["<<i<<"] = "<<x[i];
cout<<"\n\n\t This is the solution vector";
getch();
}
Output
LU-Decomposition Method
************************
Lower and Upper Matrices Components
u11 = 2 l11 = 1– 2 u23 = –0.5 l23 = 0
u12 = 0 l12 = 0 u31 = 0 l31 = 0.5
u13 = 1 l13 = 0 u32 = 0 l32 = 1.75
u21 = 0 l21 = –1.5 u33 = –4.625 l33 = 1
u22 = 4 l22 = 1

-:32:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

********* LY = b ***********
y[1] = 4 y[2] = 3 y[3] = -1.25
********* UX = Y **********
x[1] = 1.864865 x[2] = 0.783784 x[3] = 0.27027. This is the solution vector.
Inverse of Matrix By LU-Decomposition Method
We know that if two square matrices A and B are such that AB = BA = I then A and B are said to
be multiplicative inverse of each other. We have learnt two methods to compute the inverse of
matrix A. (i) By adjoint matrix method (ii) By elementary row operation method. Although these
methods are straight forward but they become laborious and cumbersome if A is of order 4 or more.
LU-Decomposition method is well applied to compute the inverse of a square matrix as its
algorithm is available and can be coded easily on any high level programming language. Following
steps are used for computing the Inverse of matrix A.
-1 -1 -1 -1
Step-I: Let A = L U Step-II: Compute A = (L U) = U L

Step-III: Pre-multiplying both sides by U, we get

UA-1 = UU-1 A-1 or UA-1 = L-1 (since UU-1 = I)

Step-IV: L1 can be compute by using LL1  I

Example 02: Evaluate the inverse matrix of the following matrix by LU-decomposition method.
2 0 1
A   3 4  2
1 7  5

Solution: Readers may observe that the given matrix is the system matrix that has been presented in
“Example 01” above whose LU-decomposition has already been found.
1 0 0   u11 u12 u13   2 0 1
  
 LU  l21 1 0  0 u 22 u 23    3 4  2  ( A = LU)
l31 l32 1  0 0 u 33  1 7  5
The matrices L and U have already been computed in example 1. Now,
 1 0 0  1 0 0  1 0 0
 
LL  I    3 2
1
1 0  l21 1 0   0 1 0 
 1 2 76 1 l31 l32 1  0 0 1
1 0 0
 1 
Here we have assumed that L  l21 1 0
l31 l32 1 
Now multiplying left side and equating the terms, weget
3 3 1 7 9 7 7
  l21  0  l21  ,  l21  l31  0  l31   ,  l32  0  l32  
2 2 2 6 4 6 6
 1 0 0
 L   3 2
1
1 0 . Now UA 1  L1 .
  9 4 7 6 1 

-:33:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

2 0 1  a11 
a12    1
a13 0 0
    
This gives, 0 4  1 2  a 21 a 22 a 23    3 2 1 0
0 0  37 8 a 31 a 32 a 33    9 4 7 6 1 
37 9 18 37 7 28 37 8
 a 31    a 31  ,  a 32    a 32  ,  a 33  1  a 33  
8 4 37 8 6 111 8 37
1 1 1 125
4a 23  a 33  0  a 23   , 4a 22  a 32  1  a 22 
2 37 2 444
1 3 129 4
4a 21  a 31   a 21    a 33  0  a13
, 2a13  
2 2 296 37
  a 32  0  a12
2a12   14 / 111,   a 31  1  a11
2a11   19 / 74

19 74 14 111 4 37 


Hence A 1
 129 296 125 444 1 37 
18 37 28 111 8 37 
Cholesky Method
We have mentioned above that if A is a Symmetric Positive Definite Matrix, then the decomposition
of matrix A is defined as A = L.LT. This scheme of decomposition is called Cholesky Method.
Consider an equation A x = b. In matrix form, it is
a11x1  a12 x 2  a13 x 3  b1
a 21x1  a 22 x 2  a 23 x 3  b2
2a 31x1  a 32 x 2  a 33 x 3  b3

Now by Cholesky method, A = L.LT, where L is a lower triangular matrix and LT is its transpose.
This implies that:
a11 a12 a13  l11 0 0  l11 l21 l31 
    
a 21 a 22 a 23   l21 l22 0  0 l22 l32 
a 31 a 32 a 33  l31 l32 l33  0 0 l33 
T
Now A x = b or L.(L x) = b (1)
T
Assuming that: L x=y (2)
We get, Ly = b (3)
Thus to compute the solution, we first use equation (3) to find y and then equation (2) to compute
the solution x.
Remark: If the matrix A is not “Positive Definite”, then it may happen that at any point of
computation, either lii  0 or lii2  0 which results in an imaginary number. If any of these two
conditions occur, the Cholesky method fails. This is the major disadvantages of this method.
Inverse of a Matrix by Cholesky Method
Since A is symmetric matrix hence its inverse exists and that:

   L   L -1   L-1  L 
-1 -1 T
A-1  LLT T -1

This shows that it is sufficient to compute the inverse of L only. Multiplication of it with its own
transpose will produce the inverse of A.

-:34:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

Example 03: Solve the following system of equations by Cholesky’s method. Also find the inverse
of system matrix A.
9x1  6x 2  3x 3  30
6x1  5x 2  x 3  19
3x1  x 2  4x 3  17
9 6 3  x1  30 
 
Solution: Here, A  6 5 
1 , x  x 2 , b  19 
3 1 4   x 3  17 
We see that:
(i) Matrix A is symmetric (ii) det (A) = 18 > 0 and (iii) det[Mij] = 886 > 0. Students may verify (ii)
and (iii). Thus conditions of Cholesky method are satisfied. Now we can apply this method to
compute the solution of the given system. Since,
9 6 3  l11 0 0  l11 l21 l31 
  
A = LL giving 6 1  l21
T
5 l22 0  0 l22 l32 
3 1 4  l31 l32 l33  0 0 l33 
2
Therefore, l11 9  l11  3, l11l21  6  l21  2, l21
2
 l22
2
 5  l22  1
l31l11  3  l31  1, l31l21  l32 l22  1  l32  1
2
l31  l32
2
 l33
2
4  l33  4  12  (1) 2  2

3 0 0   y1  30 
  
Now taking L y = b: 2 1 0   y 2   19 
1 2   y3  17 
 -1
After simplification, we have
3y1  30  y1  10
2y1  y2  19  y 2  19  20  1
y1  y2  2y3  17  y3  3 2
3 2 1   x1  10 
    
Now consider L x = y: 0T
1 -1   x 2    1 
0 2   x 3  3 2 
 0

3x1  2x 2  x3  10, x 2  x3  1 and 2x3  3 2


Using backward substitutions, we obtain: x1  1, x 2  2, and x 3  3
This is the solution of given system of linear equations.

 
T
Now to find A-1 we proceed as follows: Since, A 1  L1 L1

Now, let

l11 0 0  3 0 0   l11 0 0  1 0 0
    
L1  l21 l22 0   LL1  I   2 1 0   l21 l22 0   0 1 0
l31 l32 l33  1 2  l31 l32 l33  0 0 1
 -1

-:35:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

  1  l11
3l11   13 ,   l21  0  l21  2l11
2l11    23 , l22  1
  l21  2 l31  0  2 l31  l11
l11   l21   13  23  1  l31   1 ,
2

l22  2 l32  0  l32  1 , '


2 l33  1  l33
'
 1
2 2

 1 0 0
 3 
 
T
1  2
Thus, L   3 1 0  . Now, A 1  L1 L1 ; thus
 
 1 1 1 
 2 2 2 

1  1
 23  0   19
1
3  3 0 - 76 - 12 
2   18 
A 1   0 1 1   2 1 0   - 76 3 1
 2  3   2 2

 1   1 1 
- 1 2 
1 1 1
0 0
2 2 2 2   2 2 
Iterative Methods (Indirect Methods)
So far we have studied some of direct methods which yield, after a certain amount of fixed
computation, the solution to simultaneous linear equations. Now we shall discuss the indirect or
iterative methods. In these methods we start from an approximation to the true solution and, if
convergent, derive a sequence of closer approximations. We repeat the cycle of computations till
the required accuracy is obtained.
It may be noted that iterative methods although are simple and can be programmed easily, they are
not applicable to all systems of linear equations. For this, each equation of the system must contain
diagonal element which is large (in its absolute value) than the sum of absolute values of the other
coefficient. The matrix A arising from the system A x = b is then known as diagonally dominant.
Thus the convergence of the solution depends on the condition:
j n
| a ii |   | a ij | , i  1, 2,..., n and j  i.
j1

We shall now discuss two of such iterative methods.


Jacobi’s Method
For sake of simplicity, we assume that the system is of order 3, that is;
a1 x + b1 y + c1 z = d1
a2 x + b2 y + c2 z = d2
a3 x + b3 y + c3 z = d3
Now rearrange the above equations and assuming that the coefficients a1, b2 and c3 are the largest in
absolute value than the sum of absolute values of the other coefficient in their respective equations,
the above system of equations is then put in the form:
d1  b1y  c1z d  a 2 x  c2 z d  a x  b3 y
x ,y 2 , z 3 3
a1 b2 c3
Generating the iterative scheme as:
d1  b1yn  c1zn d  a 2 x n  c2 z n d  a x  b3 yn
x n 1  , yn 1  2 , zn 1  3 3 n
a1 b2 c3

-:36:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

This is the generalized form of “Jacobi Iterative Scheme” for 3 by 3 system.


Let x0 = y0 = z0 = 0 be an initial guess (You may assume any values of x0, y0, z0). Now after
repeatedly applying the above iterative scheme, we obtain the approximate solution of the given
system to the required accuracy.
Example 01: Solve the following system of linear equations by using Jacobi Method
20x + y – 2z = 17, 3x + 20y – z = –18, 2x – 3y + 20z = 25

Solution: Here we see that absolute values of coefficients of x, y and z in 1 st, 2nd, and 3rd equations
are greater that the sum of the absolute values of other coefficients in their respective equations.
Now rearrange the given system of equations in the form

17  y0  2z0 18  3x 0  z0 25  2x 0  3y0


x1  (i) y1  (ii) z1  (iii)
20 20 20
Let x 0  y0  z 0  0 be the initial guess. Then, u sin g (i), (ii) and (iii), we get
17 18 25
x1   0.85, y1   0.9, z1   1.25
20 20 20
Thus (x1 , y1 , z1 )  (0.85,0.9,1.25). Now, x  x1  x 0  0.85  0  0.85,
y  y1  y0  0.9  0  0.9, z  z1  z 0  1.25  0  1.25
For the second iteration, putting the 1st iterative values in the above equations, we obtain
17  (0.9)  2(1.25) 18  3(0.85)  1.25
x2   1.02, y 2   0.965,
20 20
25  2(0.85)  3(0.9)
z2   1.03. Thus (x 2 , y 2 , z 2 )  (1.02, 0.965,1.03)
20
Now, x  x 2  x1  1.02  0.85  0.17, y  y2  y1  0.965  (0.9)  0.065
and z  z 2  z1  1.03  1.25  0.22
Repeat the above process for 3rd iteration, we have
17  (0.965)  2(1.03) 18  3(1.02)  1.03
x3   1.00125, y3   1.0015, and
20 20
25  2(1.02)  3(0.965)
z3   1.00325.Also
20
x  x 3  x 2  1.00125  1.02  0.01875, y  y3  y 2  1.0015  (0.965)  0.0365
and z  z3  z 2  1.00325  1.03  0.02675.
Thus (x 3 , y3 , z3 )  (1.00125, 1.0015,1.00325)
Now, for the 4th iteration, we have
17  (1.0015)  2(1.00325) 18  3(1.00125)  1.00325
x4   1.0004, y 4   1.000025,
20 20
25  2(1.00125)  3(1.0015) 19.993
z4    0.99965
20 20
x  x 4  x 3  1.0004  1.00125  0.00085
y  y 4  y3  1.000025  (1.0015)  0.001475
z  z 4  z3  0.99965  1.00325  0.0036
Thus (x 4 , y 4 , z 4 )  (1.0004, 1.000025,0.99965)

-:37:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

You may observe that an accuracy of 2 d.p has been achieved, hence the approximate solution of
given system of linear equations to 2 d.p is:
(x, y, z) = (1.0004, –1.000025, 0.99965)

COMPUTER PROGRAM
#include<constream.h>
#include<stdio.h>
#define size 10
static float temp, xtemp[size], A[size][size],b[size],x[size];
void main()
{ int i,j,k,neq,nitr;
clrscr();
cout<<"\nEnter the number of equations ";cin>>neq;
for(i=0;i<neq;i++)
{ for(j=0;j<neq;j++)
{ cout<<"\nEnter the co-efficent values A"<<i<<j<<" : ";
cin>>A[i][j];
}
cout<<"Enter the constant value b"<<i<<" : ";cin>>b[i];
clrscr(); }
for(i=0;i<neq;i++)
x[i]=0;
cout<<"\nEnter the total number of iterations : ";cin>>nitr;
for(i=0;i<=neq-1;i++)
{ temp=A[i][i];
for(j=0;j<=neq;j++)
A[i][j]/=temp;
b[i]/=temp; }
cout<<"\n\n";
for(i=0;i<neq;i++)
cout<<" x"<<i+1<<setw(16);
cout<<"\n\n";
while(nitr>=1)
{ for(i=0;i<=neq-1;i++)
{ xtemp[i]=0.0;
xtemp[i]+=A[i][neq];
for(j=neq-1;j>=0;j--)
{ if(i==j)
j--; xtemp[i]+=A[i][j]*x[j]; }}
cout<<endl;
for(k=0;k<neq;k++)
{ x[k]=b[k]-xtemp[k];

-:38:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

printf("%.6f\t",x[k]); }
cout<<"\n";nitr--; }
getch(); }
Program Output
Enter the total number of iterations: 10

x1 x2 x3
0.850000 -0.900000 1.250000
1.020000 -0.965000 1.030000
1.001250 -1.001500 1.003250
1.000400 -1.000025 0.999650
0.999966 -1.000077 0.999956
1.000000 -0.999997 0.999992
0.999999 -1.000000 1.000000
1.000000 -1.000000 1.000000

It may be noted that instead of x, y and z we have programmed with x1, x2, and x3 variables to make
the program generalized one. You also observe that the results obtained are exactly the same as are
obtained in the above example. It may be noted that above computer program has been designed to
ask for the number of iterations. This we have entered as 10 and we observe that from the 8th
iteration the results are converging to the true/exact solution of the system.
Gauss-Seidel Method
Gauss-Seidel Method is modified form of the Jacobi Method. The process is similar to that of
Jacobi method. The only difference between them is that GS method uses the most recent values of
unknowns obtained from the preceding iteration. Now given that:
a1 x + b1 y + c1 z = d1 , a2 x + b2 y + c2 z = d2 , a3 x + b3 y + c3 z = d3
Rearrange the above equations, and assuming that the coefficients a1, a2 and a3 are the largest in
absolute value than the sum of absolute values of the other coefficient in their respective equations;
the above system of equations is then put in the form:
d1  b1y  c1z d  a 2 x  c2 z d  a x  b3 y
x ,y 2 , z 3 3
a1 b2 c3
Generating the iterative scheme as:
d1  b1yn  c1zn d  a 2 x n 1  c2 zn d a x b y
x n 1  , yn 1  2 , zn 1  3 3 n 1 3 n 1
a1 b2 c3
This is the general form of the “Gauss-Seidel Scheme” for 3 by 3 system.
Taking an initial guess for x 0 , y0 and z 0 , we proceed to compute the approximate solution of the
given system using the general form of the GS method as defined above.
Example 02: Use the Gauss-Seidel method to solve the system of linear equations
3x1  0.1x 2  0.2x 3  7.85, 0.1x1  7x 2  0.3x 3  19.3, 0.3x1  0.2x 2  10x 3  71.4
correct to 3dp accuracy.
Solution: It may be noted that instead of x, y and z we have taken the variables x1, x2 and x3.
Rearrange the given equations, we get

-:39:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

7.85  0.1x 2  0.2x 3 19.3  0.1x1  0.3x 3 71.4  0.3x1  0.2x 2


x1  , x2  , x3 
3 7 10
1st Iteration: Now taking the initial guess as x1  x 2  x 3  0 , we obtain:
7.85  0.1(0)  0.2(0)
x1   2.616667, Ex1  2.616667  0  2,616667
3
This value of x1 along with the assumed value x 3  0 , may be substituted in (2), we obtain
19.3  0.1(2.616667)  0.3(0)
x2   2.794524, Ex 2  2.794524  0  2.794524
7
Now substituting the new values of x1 and x2 into (3), we get
71.4  0.3(2.616667)  0.2(2.794524)
x3   7.005610, Ex3  7.005610  0  7.005610
10
Here Ex1, Ex2, Ex3 are the errors that occur during the first iteration. Thus:
(x1, x2, x3)(1) = (2.616667, –2.794524, 7.005610).
2nd Iteration:
7.85  0.1(2.794524)  0.2(7.005610)
x1   2.990557 : Ex1  2.990557  2.616667  0.37389
3
19.3  0.1(2.990557)  0.3(7.005610)
x2   2.499625 : Ex 2  2.499625  2.794524  0.294899
7
71.4  0.3(2.990557)  0.2( 2.499625)
x3   7.000291: Ex 3  7.000291  7.005610  0.005319
10
Thus, (x1, x2, x3)(2) = (2.990557, –2.499625, 7.000291).
3rd Iteration:
7.85  0.1(2.499625)  0.2(7.000291)
x1   3.000032 : Ex1  3.000032  2.990557  0.009475
3
19.3  0.1(3.000032)  0.3(7.000291)
x2   2.499988 : Ex 2  2.499988  2.499625  0.000363
7
71.4  0.3(3.000032)  0.2( 2.499988)
x3   7: Ex 3  7  7.000291  0.000291
10
(3)
(x1, x2, x3) = (3.000032, –2.499988, 7).
We may observe that the error in computation of x1, x2 and x3 after the third iteration is
approximately reduced to 3 d.p. If more accuracy is needed, we may continue the above process till
the required accuracy is obtained.
Remark: Gauss-Seidel method will always converge if Jacobi method converges and will do so
more rapidly. If Jacobi method diverges, so does Gauss-Seidel method. Moreover, GS method is
faster that Jacobi method as it uses the most recent values that are available in the current iteration.
COMPUTER PROGRAM
#include<constream.h>
#include<stdio.h>
#define size 10
static float temp,xtemp[size], A[size][size],b[size],x[size];
void main()

-:40:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

{ int i,j,k,neq,nitr;
clrscr();
cout<<"\nEnter the number of equations ";cin>>neq;
for(i=0;i<neq;i++)
{ for(j=0;j<neq;j++)
{ cout<<"\nEnter the co-efficent values A"<<i<<j<<" : ";
cin>>A[i][j];
}
cout<<"Enter the constant value b"<<i<<" : ";cin>>b[i];
clrscr();
}
for(i=0;i<neq;i++)
x[i]=0;
cout<<"\nEnter the total number of iterations : ";cin>>nitr;
for(i=0;i<=neq-1;i++)
{ temp=A[i][i];
for(j=0;j<=neq;j++)
A[i][j]/=temp;
b[i]/=temp;
}
cout<<"\n\n";
for(i=0;i<neq;i++)
cout<<" x"<<i+1<<setw(16);
cout<<"\n\n";
while(nitr>=1)
{ for(i=0;i<=neq-1;i++)
{ xtemp[i]=0.0;
xtemp[i]+=A[i][neq];
for(j=neq-1;j>=0;j--)
{ if(i==j)
j--;
xtemp[i]+=A[i][j]*x[j]; }
x[i]=b[i]-xtemp[i]; }
cout<<endl;
for(k=0;k<neq;k++)
{ x[k]=b[k]-xtemp[k];
printf("%.6f\t",x[k]);
}
cout<<"\n";nitr--; }
getch(); }

-:41:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

Program Output: Enter the total number of iterations: 10


x1 x2 x3
2.616667 –2.794524 7.005610
2.990556 –2.499624 7.000291
3.000032 –2.499988 7.000000
3.000000 –2.500000 7.000000
3.000000 –2.500000 7.000000

Eigenvalues and Eigenvectors


Let A be square matrix of order n, λ be a scalar and x be a column vector of order n. If
A x = λ x then λ is called an eigenvalue and x is called the corresponding eigenvector of matrix A.
It may be noted that:
(i) The matrix [A – λI] is called the characteristic matrix (ii) The determinant equations of
characteristic matrix i.e. | A – λI| = 0 is called the characteristic equation which is a polynomial of
degree n. (iii) The roots of characteristic equation | A – λI| = 0 are called latent roots or eigenvalues
of matrix A.
Some Important Properties of Eigenvalues and Eigenvectors
The following properties of eigenvalues and eigenvectors of square matrix may be noted:
 The sum of all eigenvalues of a matrix is equal to the trace of the matrix, where the
“Trace” is the sum of the elements on the principle diagonal of a matrix.
 Any square matrix A and its transpose AT have the same eigenvalues.
 The product of the all eigenvalues of a matrix A is equal to the determinant of A.
 The eigenvector x corresponding to eigenvalue λ is not unique.
 If λ1, λ2, …, λn be distinct eigenvalues of square matrix then corresponding eigenvectors
x1, x2, …, xn are linearly independent.
 If two or more eigenvalues are equal it may or may not be possible to get a set of linearly
independent eigenvectors corresponding to the equal eigenvalues.
 Two eigenvectors xi and xj are called orthogonal if their dot product is zero.
 Eigenvalues of any square matrix may be real, repeated or complex.
 Eigenvectors of a symmetric matrix are always real and eigenvectors corresponding to
different eigenvalues are always orthogonal.
Theoretical Method to Compute an Eigenvalues and Eigenvecors
In this section we present a theoretical method to compute the eigenvalues and corresponding
eigenvectors by providing two examples. These examples help you to understand how the
eigenvalues and corresponding eigenvectors of a square matrix are obtained.
2 4 
Example 01: Find the eigenvalues of the matrix A   
 1 1
Solution: The characteristic matrix is:
2 4 1 0  2   4 
 A  I    1   
1
 
1   
 0 1  1
The corresponding characteristic equation is:

-:42:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

2 4
A  I   (2  )(1  )  4   2    6  0
1 1  
Solving the above polynomial equation (Quadratic equation), we get:
  3 and   2 .
These are the latent roots / eigenvalues of given matrix A.
Example 02: Find the eigenvalues and eigenvectors of the matrix
1 0 1
 
A  1 2 1
2 2 3 

Solution: The characteristic equation of given matrix is |A – λ I| = 0
or  3– 6  2 + 11  – 6 = 0 or (  – 1) (  – 2) (  – 3) = 0
or   1,   2, and   3
Now to find the eigenvectors for the corresponding eigenvalues, consider the matrix equation:
1   0 1   x  0
 
(A  I)x  0 or  1 2 1   y   0 (1)
 2 2 3     z  0

Eigenvector corresponding to eigenvalue   1 is obtained by putting   1 in matrix equation (1),
we get
0 0 1  x  0
 
1 1 1   y   0
2 2 2   z  0

  z  0 or z  0 (i) x  y  z  0 (ii) & 2x  2y  2z  0  x  y  z  0 (iii)
We see that 2nd and 3rd equations are same, hence putting z = 0 in one of these equations, we obtain
x = –y. Now let x = k where k is a free parameter. Hence the eigenvector corresponding to
x1 =  k k k 
t
eigenvalue  = 1is:
We see that for any value of k there are infinitely many eigenvectors corresponding to the
eigenvalue  = 1.
Note: Readers are advised to find the eigenvectors corresponding to the remaining eigenvalues.
Power Method
Sometimes it is desired to compute only the eigenvalue which is absolutely largest in magnitude.
Power method is the most effective, well-organized and robust iterative method for determining the
largest eigenvalue. With slight modification, it can also be used to determine the smallest and an
intermediate eigenvalues. (This may be found in any advance book on “Theory of Eigenvalue
Problems”). It has the additional benefit that the corresponding eigenvector is obtained as a by-
product of the method.
Let A be a square matrix of order n whose eigenvalues satisfy:
|λ1| ≥ |λ2| ≥ |λ3| ≥…|λn| (1)
The eigenvalue λ1 will be referred to as the dominant eigenvalue. By assumption, there exist n
linearly independent eigenvectors x1, x2, …, xn and any arbitrary vector z0 can be expressed as
linear combination in the form:

-:43:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

n
z0   a i x i (2)
i 1

where ai are scalars not all zero. Let us define an iterative scheme
zk = A zk-1, k = 1, 2, 3, … (3)
2 k
where z0 is arbitrary. Then: zk = A zk-1 = A zk-2 = … = A z0
n
Using (2), we get zk =  ai ki xi (4)
i 1

Here we have used the equation Ax = λx and (2).


Now provided that a1 is not zero, the right side of equation (4) is ultimately dominated by the term
a1λ1k x1. In practice, the elements of zk are scaled at each step, and equation (3) is replaced by the
pair of equations:
yk = Azk-1
zk = yk/max|| y ||
In this case, zk → x1/max|| x1 ||
and max|| yk || → λ1 as k → ∞
Algorithm: Given square matrix A, let y0 be an initial eigenvector. Define:
(p) (p- 1)
(i) z =Ay , p = 1, 2, 3, …
(p)
= z /max || z(p)||, where the symbol max|| || is known as norm which
(p)
(ii) y
is the largest value of a matrix or vector in absolute value.
(iii) Assuming that λ(p) = max ||z (p)||, compute A.E = | λ(p + 1) – λ(p)|.
(iv) If A.E is reached to its required value, we have found the largest
eigenvalue λ(p + 1)
and the corresponding eigenvector z(p + 1)
. Else go to
step (i).
The following example will help to understand how a dominant eigenvalue and corresponding
eigenvector as a by-product of the power method are found.
Note: As mentioned above that power method is an iterative method, hence it requires an initial
guess xo. The best choice of taking the guessed eigenvector is
y0  1 0 0 or y0  1 1 1
t t

Example 03: Determine the dominant/largest eigenvalue and the corresponding eigenvector of the
matrix using power method.

2 -1 1 
-1 2 -1 

1 -1 2 
Solution: Let x (0)  1 0 0 :
t

2 -1 1  1   2  1 
st
1 Iteration: Ax (0)
 -1 2 -1   0   1  2  0.5
    
1 -1 2   0 1   0.5 

-:44:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS
(1)
Thus after the first iteration, we find that the largest eigenvalue λ = 2 and the corresponding
eigenvector is:
1 
x (1)
=  0.5
0.5 

2 1 1  1  3  1 
nd
2 Iteration: Ax (1)
  1 2  1   0.5   2.5  3  0.833
     
1 1 2  0.5   2.5   0.833 

Thus after the second iteration, the largest eigenvalue λ(2) = 3 and the corresponding eigenvector is
1 

=  0.833 . Also ε = | λ2 – λ1| = |3 – 2| = 1
(2)
x
 0.833 
The above process is repeated until the largest eigenvalue and the corresponding eigenvector are
found to required accuracy.

COMPUTER PROGRAM
#include<constream.h>
#include<math.h>
#include<stdio.h>
void main()
{ float a[10][10],xn[10],xo[10],Ac,Rac,Lo=1,Ln,sum;
int i,j,count =1,n;
clrscr();
cout<<"Enter the order of matrix not greater than 10 : ";cin>>n;
cout<<"\nEnter the required accuracy in %age : ";cin>>Rac;
for(i = 1; i <= n; i++)
for(j = 1; j <=n; j++)
{
cout<<"\nEnter the value of a"<<i<<j<<" : ";
cin>>a[i][j];
clrscr();
}
for(i = 1; i <= n; i++)
{
cout<<"\nEnter the initial vector x"<<i<<" : ";
cin>> xo[i];
}
clrscr();
cout<<"Power Method to Compute the Largest Eigen-Value ";
cout<<"\n\n=============================================";
cout<<"\n"<<"It.#"<<setw(10)<<"E-Value"<<setw(17)<<"E. Vector"
<<setw(17)<<"Accuracy"<<endl;
cout<<"==========================================="<<endl;

-:45:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

do
{ for(i = 1; i <= n; i++)
{ sum = 0;
for(j = 1; j <= n; j++)
{
sum = sum + a[i][j]*xo[j];
xn[i]=sum;
}
}
Ln=xn[1];
for(i = 2; i <= n; i++)
{
if(Ln < xn[i])
Ln = xn[i];
}
Ac=fabs((Ln-Lo)/Lo)*100;
cout<<"\n"<<count<<setw(10);
printf("\t%.2f",Ln);
for(i = 1; i <= n; i++)
printf("\t%.2f",xn[i]/Ln);
printf("\t%.2f",Ac);
Lo=Ln;
for(i = 1; i <= n; i++)
xo[i] = xn[i]/Ln;
count ++;
}
while(Ac > Rac);
cout<<"\n==============================================";
getch();
}
Program Output
Power Method to Compute the Largest Eigen-Value
=============================================
It.# E-Value E-Vector Accuracy (%)
=============================================
1 2.00 1.00 -0.50 0.50 100.00
2 3.00 1.00 -0.83 0.83 50.00
3 3.67 1.00 -0.95 0.95 22.22
4 3.91 1.00 -0.99 0.99 6.61
5 3.98 1.00 -1.00 1.00 1.73
6 3.99 1.00 -1.00 1.00 0.44
=============================================
We observe that at the 6th iteration the approximate value of largest eigenvalue is 3.99 to 0.44%
accuracy.

-:46:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

WORKSHEET 03
1. Solve the following system of linear equation by using LU-Decomposition method.

i. 4x1  5x 2  2x 3  5.6 ii. 4x1  3x 2  x 3  2 iii. 0.5x1  0.3x 2  0.2x 3  0.7


2x1  3x 2  x 3  1.3 - 2x1  4x 2  5x 3  20 0.2x1  0.1x 2  0.4x 3  0.3
2x1  4x 2  4x 3  9.5 x1  2x 2  6x 3  7 0.4x1  0.3x 2  0.6x 3  0.2
2. Solve the following systems of equations by Cholesky’s method. Also find A-1.

i. x1  2x 2  6x 3  13 ii. 8x1  4x 2  2x 3  24
2x1  5x 2  15x 3  15 4x1  10x 2  5x 3  4x 4  32
6x1  15x 2  46x 3  19 2x1  5x 2  6.5x 3  4x 4  26
4x 2  4x 3  9x 4  21
3. Solve the system of linear equations using Gauss-Seidel method with 5% accuracy.
4x  y  z  13, 3x  5y  2z  21, 2x  y  6z  14
4. Compute the load on a slab in all directions represented by the system of following linear
equations up to six iterations by Jacobi’s method.

30x  y  2z  21, 3x  30y  z  17, 2x  3y  30z  19


5. Discuss the convergence and divergence criteria in Gauss-Seidel method and find the solution of
system of linear equations obtained from an electrical net-work by using Gauss-Seidel method up to
5% accuracy.
10I1 – 2I2 – 3I3 = 205, 2I1 + 10I2 – 2I3 = 154, –2I1 – I2 + 10I3 = 120
6. Three reactors are connected with each other in a chemical system satisfying the system of
equations: 17q1  8q 2  3q3  480,  2q1  6q 2  3q3  0,  q1  4q 2  13q3  0
Find the flow rate q in each pipe by using Gauss-Seidel method with 5%accuracy.
7. Solve by Jacobi’s method the following set of equations arising from Mechanical network
system: 3.6x1  2x 2  1.1x 3  13.7, 2x1  6.9x 2  2.2x 3  54.2, 1.1x1  2.2x 2  7.6x3  17.6
8. Find the Eigenvalues and corresponding Eigenvectors of the following matrices.
2 2 1 1 2 2 
 2 1  
i.   ii. 1 3 1 iii. 1 1 1 

1 -1  1 2 2  1 3 1

9. Determine the dominant eigenvalues of the following matrices by using Power method.
1 1 2 10 7 8  10 7 8 
a. 0 1 3  b. 7 6 6  c. 7 6 6 
1 1 1  8 6 19  8 6 19 
4 1 1 4 3  7 7 3  1
d. 1 1 1  e.  2 3 2  f. 3 4  1 
 2 0  6   4 2 7   2 1 3

-:47:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS

10. The pressures applied on different materials to test their breaking strengths are given by
10P1–P2–P3=3, –P1+10P2–P3=1, and – P 1 – P 2 + 1 0 P 3 = 3 . Find the unknown
pressures by using Gauss-Seidel iteration method. Results should be correct to 1% accuracy.
11. A yarn merchant sells brands A, B, C, yarn each of which is blend of Pakistani, Egyptian,
American cotton in ratio 2 : 2 : 1 , 2 : 1 : 2 , 2 : 1 : 2 , respectively. If one kilogram of A, B, C, cost
40, 50, 60, rupees, respectively, find the cost of a kilogram of cotton of each country.
12. Solve the following linear system by Gauss-Jacobi iteration. Start with x(0)  0 for i = 1, 2 ,3,
and 4. Iterate x k  x (k 1)  0  1 . The exact solution is x i   1 i. Observe the reduction in error
i

in each step. 4x1  x 2  x 3  x 4  5, x1  5x 2  x 3  x 4  2, 2x1  x 2  6x 3  2x 4  14,


 x1  x 2  2x 3  4x 4  25 .

13. Solve the following linear system by Gauss-Seidel iteration. Start with x(0)  0 for i = 1, 2, 3,
and 4. Iterate x k  x (k 1)  0  01 .

The exact solution is x i  i. Observe the reduction in error in each step.


3x1  x 2  x 3  5, 2x1  5x 2  x 4  2,  x 2  6x 3  3x 4  4,  x1  2x 3  4x 4  25

-:48:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

CHAPTER
FOUR INTERPOLATION
AND EXTRAPOLATION

INTRODUCTION
Interpolation is a technique of obtaining the values of some function for any intermediate value(s)
of an independent variable. Suppose we are given the following values of y = f(x) for a set of x
values.

x x0 x1 x2 … xn

y = f(x) y0 y1 y2 … yn

Then the process of finding the value of y corresponding to any value of x = x i between x0 and xn
is called interpolation. Thiele (a mathematician) defines interpolation as “the art of reading
between the lines of table”. The process of finding the value of y for any value of x that lies
outside the interval [x0, xn] is called extrapolation. However, many authors use the term
interpolation for both processes.
If the form of the function f(x) is known, one can find f(x) for any value of x by simple
substitution. But in most practical problems which occur frequently in engineering and science,
the form of function f(x) is not known. Instead, the values of f(x) are given in tabulated form as
shown above. Hence, it is difficult to find out the exact value of the function f(x) for any known
value of x when it is given in tabular form. In such cases we find out a interpolating function say
g(x) satisfying the property that f(xi) = g(xi). It may be noted that g(x) is also known as smoothing
function. Once the function g(x) is established, one can find the approximate value of f(x) for any
value of x from g(x) to the required degree of accuracy.
If g(x) is a polynomial then it is called interpolating polynomial and the process is called
polynomial interpolation. If g(x) is a finite trigonometric series the process is called trigonometric
interpolation. Usually, polynomial interpolation is preferred due to the reason that they do not
contain singularities, and are differentiable and integrable, and easy to manipulate. The study of
the interpolation is based on the concept of differences.
Finite Differences
Let y = f(x) be the function with certain values y0, y1, y2, … yn corresponding to equally spaced
values x0, x0 + h, x0 + 2h, …, x0 + nh. The value of the independent variable x is usually called
the argument and the corresponding functional value is known as entry. The argument and entry
are shown in a tabular form as follows.

-:49:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

Argument: x x0 x1 = x 0 + h x2 = x0 + 2h … xn = x0 + nh

Entry: y = f(x) y0 = f(x0) y1 = f(x0 + h) y2 = f(x0 + 2h) … yn = f(x0 + nh)

Finite differences are useful in determining the values of f(x) and its derivatives f `(x), f ``(x) for
some intermediate value of argument x. Although there are many types of finite differences but
we shall discuss only the following. They are:
i. Forward Difference Operator
ii. Backward Difference Operator
iii. Shift Operator
iv. Central Difference Operator
v. Mean Difference Operator
Forward Difference Operator
Forward difference operator of first order is usually denoted by Δ and is defined as follows:
y0  y1  y0 , y1  y 2  y1 ,..., y n  y n 1  y n
Similarly, second order differences are defined as:

 2 y0  y1  y0 ,  2 y1  y 2  y1 ,...,  2 y n  y n 1  y n

In general, the mth order difference is defined as  m y n   m 1y n 1   m 1y n .


It may be noted that:

i.  2 y0  (y0 )  (y1  y0 )  y1  y0  (y 2  y1 )  (y1  y0 )  y 2  2y1  y0


ii. 3 y0   2 (y0 )  (y 2  2y1  y0 )  y 2  2y1  y0
 (y3  y2 )  2(y1  y0 )  (y1  y0 )  y3  3y 2  3y1  y0
The coefficients on RHS are same as those of binomial coefficients. Moreover, the terms appear
with alternating signs. Thus,

iii.  n y0  y n  C 1n y n 1  C 2n y n 2  C 3n yn 3  ...  (1) n y0


Example 01: Find the Forward difference table of the following data.
x: 0 1 2 3 4
y: 7 9 11 15 17
Solution: The difference table is shown as under.
x y y 2y 3y 4y
1 7
2
1 9 0
2 2
2 11 2 -6
4 -4
3 15 -2
2
4 17
-:50:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

This table is known as forward difference table. The following code produces this table.
Backward Difference Operator
Backward difference operator of first order is usually denoted by  (known as Nebla) and is
defined as: y1  y1  y0 , y 2  y 2  y1 ,...., y n  y n  y n 1
Similarly, second order differences are defined as:

 2 y2  y2  y1  (y2  y1 )  (y1  y0 )  y2  2y1  y0 . Similarly,


 2 y3  y3  y2  y3  2y2  y1 etc.

In general, the m order difference is defined as  m y n   m 1y n   m 1y n 1 .


th

An Important Note: If f(x) is a polynomial of degree n then nth finite difference of it is always
constant and the difference higher than n are always zero. This is shown in the table of example 8
below.
Shift Operator
The shift or displacement operator is usually denoted by E and is defined as:
Eyr = yr+1 or E[f(xr)] = f(xr + h)
The effect of E is to shift or increase the functional value of yr or f(xr) by one interval to the next
higher value yr+1. The higher order operation of E may be similarly defined by:
E2yr = E[Eyr] = Eyr+1 = yr+2 or E3yr = yr+3 or Enyr = yr+n. This is equivalent to:
E2f(xr) = E[E f(xr)] = Ef(xr + h) = f(xr + 2h), E3f(xr) = f(xr + 3h) or Enf(xr) = f(xr + nh).
Some Important Facts about Shift Operator E
i. Verify that Enf(xr) = Erf(xn)
Proof: We know that Enf(xr) = f(xr + nh) = yr + n = yn + r = f(x + rh) = Erf(xn)
This gives, Enf(xr) = Erf(xn)
ii. In particular, E1/2yr = yr+1/2, E-1/2yr = yr-1/2.
iii. It may also be noted that: Ey0 = y1, E2y0 = E[Ey0] = Ey1 = y2 or Ery0= yr
iv. E[yr+ zr] = yr+1 + zr+1 = Eyr + Ezr
v. E[ayr] = ayr+1 = aEyr or Ea = aE
vi. EmEn[yr] = Em[En yr] = Em(yr+n) = yr + n + m = yr + (n + m) = Em + nyr or EmEn = Em + n
Central Difference Operator
Central Difference operator is usually denoted by δ and is defined as:
δ y1/2 = y1 – y0 or δyr + 1/2 = yr + 1 – yr. Similarly,
δ2 y1 = δ(δy1) = δ (y3/2 – δy1/2) = δy3/2 – δ1/2 = (y2 – y1) – (y1 – y0) = y2 – 2y1 + y0
Similarly, δ2 y2 = y3 – 2y2 + y1.
Average/Mean Difference Operator
The average or mean difference operator is denoted by μ and is defined as:
μyr = (1/2) [yr+1/2 + yr-1/2] or μ [f(xr)] = (1/2) [f(xr + h/2) + f(xr – h/2)]
-:51:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

You may observe that averaging operator μ denotes the average or mean value at r of two
adjacent values at r + 1/2 and r – 1/2.
Symbolic Relation Between the Operators
We know that: Ey0 = y1, y0 = E-1y1, ∆y0 = y1 – y0,  y1 = y1 – y0, δ y1/2 = y1 – y0
Using these relations, we see that:
i. ∆y0 = y1 – y0 = Ey0 – y0 = (E – 1)y0 or ∆ = E – 1 or E = 1 + ∆
ii.  y1 = y1 – y0 = y1 – E-1y1 = (1 – E-1)y1 or  = 1 – E-1 or E = (1 –  )-1
iii. We know that differential operator D is defined as Dy = y`. Now if y = f(x) then by Taylor’s
series:
h2 h3 h2 h3
f (x  h)  f (x)  hf (x)  f (x)  f (x)  ...  f (x)  hDf (x)  D 2f (x)  D3f (x)  ...
2! 3! 2! 3!
 h 2
h 3 
 1  hD  D2  D3  ...  f (x)  e hD f (x)
 2! 3! 
 
But f(x + h) = y1 = Ey0 = Ef(x). Thus, Ef(x) = ehDf(x) or E = ehD = (1 + ∆) = (1 –  )-1
iv. The central difference operator is defined as:
δ yr = yr+1/2 – yr-1/2 = E1/2yr – E-1/2yr = (E1/2 –E-1/2)yr or δ = E1/2 – E-1/2.
Using (iii), we get: δ = ehD/2 – e-hD/2 = 2sinh (hD/2) NOTE: sinh z = (ez – e-z)/2
v. The mean operator μ is defined as: μ yr = (1/2)[yr+1/2 + yr-1/2]
or μyr = (1/2) [E1/2yr + E-1/2yr] = (1/2)[E1/2+ E-1/2] yr or μ = (1/2)[E1/2+ E-1/2]
Using (iii), we see that: μ = (1/2)[ehD/2 + e-hD/2] = cosh (hD/2) NOTE: cosh z = (ez + e-z)/2
vi. We know that μ = (1/2)[E1/2+ E-1/2]
or μ2 = (1/4)[E1/2 + E-1/2]2 = (1/4)[(E1/2)2 + 2 + (E1/2)2]= (1/4){ [(E1/2)2 –2 + (E1/2)2] + 4}
= (1/4){ [E1/2 – E-1/2]2 + 4} = (1/4)[δ2 + 4] = δ2/4 + 1 [Using (iv)]

2
Taking square root, we obtain:   1 
4
vii. Now consider, μ δyr = (1/2) [δyr+1/2 + δyr-1/2] = (1/4) [yr+1 – yr + yr – yr-1]
= (1/2) [yr+1 – yr-1] = (1/2) [Eyr – E-1yr] = (/2) [E – E-1] yr
or μ δ = (1/2) [E – E-1] = (1/2) [ehD/2 – e-hD/2 ]= sinh (hD/2) [Using (iii)]
Thus, μ δ = sinh(hD/2)
Example 02: Prove that (i) (∆ /E) x3 = 6xh2 (ii) (∆2 x3/E x3) = 6h2/(x + h)2
2

Solution: (i) By definition


  2  3   E  1  3  E 2  2E  1  3
2
  x  
E  E
x  
  E
1 3 3 3
1 3
 x  E  2  E x  Ex  2x  E x 
     
 2 
  x  h   2x 3   x  h   x 3  3x 2 h  3xh 2  h 3  2x 3  x 3  3x 2 h  3xh 2  h 3    x 3  6xh 2
3 3
 E 
 

(ii)
2 x3

 E  12 x 3

E 2

 2E  1 x 3

E x
2 3
 2Ex 3  x 3  NOTE : E 2 x  (x  2h)
3 3 3 3
Ex Ex Ex Ex
-:52:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

 (x  2h)3  2(x  h)3  x 3  x 3  6x 2 h  12xh 2  8h 3  2x 3  6x 2 h  6xh 2  h 3  x 3


  
 (x  h)3 (x  h)3
 
6xh 2  6h 3 6h 2 (x  h) 6h 2  2 x3  6h 2
     
(x  h)3 (x  h)3 (x  h) 2  Ex 3  (x  h) 2
 
2
    2 x
Example 03: Evaluate the following:  i   x   ii 
 Ex  E 2 x
If μx = x find the value of above expressions.
3

2 2
       x  2x 1  2 x 1 x  2x
Solution: (i)  x    x 1   (1)
 Ex   x 1  2 x 1

 ii 
 2 x

 E  12  x

E 2
 2E  1  x  
E 2 x  2E x   x

 x  2  2 x 1   x
(2)
E x
2
E x
2
E x
2
E x
2 x 2
Now,  x  x 3   x 1   x  1 and  x  2   x  2 
3 3

 
2
  x 
2
 x  1  2  x  1 x 3  x 3 6 3
9x 4  18x 3  15x 2  6x  1
Thus, equation 1 becomes :    
 E x   x  16  x  16
And Equation (2) becomes:
 2 x  x  2 3  2  x  13  x 3 6  x  1
  [This we get after simplification]
E 2 x  x  2 3  x  2 3
Example 04: Prove that (i) ∆ = E – 2E + 1 (ii)  = E-1 ∆ and E  = E – 1 = ∆
2 2

(iii) ∆n = δn En/2 (iv) ∆ = δE1/2. Also evaluate (2∆ + 3)(E + 2) (3x2 + 2).
Solution: (i) We know that ∆ = E – 1 or ∆2 = (E – 1)2 = E2 – 2E + 1
(ii)  = E E  = E (E  ) = E ∆ (Proved)
-1 -1 -1

 = E-1 ∆ = E-1 (E – 1) = (E – 1)/E or E  = E – 1 = ∆ (Proved)


(iii) δE1/2 = [E1/2 – E-1/2] E1/2 = E – 1 = ∆ or ∆n = δn En/2
(iv) ∆yr = δE yr = δ[yr+1/2] = yr+1 – yr = ∆yr or ∆ = δE
1/2 1/2

Now, (2∆ + 3)(E + 2) (3x2 + 2) = [2(E – 1)](E + 2) (3x2 + 2)


= (2E2 + 5E + 2) (3x2 + 2) = 2E2(3x2 + 2) + 5E(3x2 + 2) + 2(3x2 + 2)
= 6E2x2 + 4E2 + 15 Ex2 + 10E + 6x2 + 4 = 6(x+ 2)2 + 4 + 15(x + 1)2 + 10 + 6x2 + 4
= 6(x2 + 4x + 4) + 4 + 15(x2 + 2x + 1) + 10 + 6x2 + 4 = 27x2 + 54x + 57
Example 05: If y = x3 + x2 – 2x + 1, calculate the values of y for x = 0, 1, 2,3, 4, 5 and form the
difference table. Find the value of y at x = 6 extending the table and verify that that the same
value is obtained by substituting.
Solution: The table on the next page shows the tabular values of x, y and forward differences.
We see that at x = 6, y = 241. Let us prove it with the help of difference table.
We know that: ∆y5 = y6 – y5 or 100 = y6 – 141 or y6 = 241.

-:53:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

∆y ∆y ∆y ∆y
2 3 4
x y

0 1
0 = ∆y0
1 1 8 = ∆2y0
8 = ∆y1 6 = ∆3y0
2 9 14 = ∆2y1 0
22 = ∆y2 6 = ∆ y1
3

20 = ∆ y2
2
3 31 0
42 = ∆y3 6 = ∆ y2
3

4 73 26 = ∆2y3 0
68 = ∆y4 6 = ∆ y3
3

32 = ∆ y4
2
5 141
100 = ∆y5
6 241

Example 06: Construct a backward difference table for y = log10 x for x = 10, 20, 30, 40, 50 and
find the values of  3 log10 40 and  4 log10 50.
Solution: The difference table is shown below.
2 3 4
x y y  y  y  y

10 1
0.3010
20 1.3010 -0.1249
0.1761 0.0738
30 1.4771 -0.0511 -0.0508
0.1250 0.0230
40 1.6021 -0.0281
0.0969
50 1.6990

From the difference table we observe that  log10(40) = 0.0738 and  log10(50) = –0.0508.
3 4

Example 07: Construct a backward difference table from the data:


sin 30o = 0.5000 sin 35o = 0.5736
sin 40o = 0.6428 sin 45o = 0.7071
Assuming third differences to be constant, find the value of sin 25o.
Solution: Refer the difference table shown below, we have the following calculations.
Here  3y40 = -0.0005 or  2y40 -  2y35 = -0.0005 or -0.0044 -  2y35 = -0.0005
Or  2y35 = - 0.0039 or  2y35 =  y35 -  y30 = -0.0039 or 0.0736 -  y30 = -0.0039
Or  y30 = 0.0755 or  y30 = y30 – y25 = 0.0755 or 0.5000 – y25 = 0.0755
o
or y25 = 0.4225 or sin 25 = 0.4225
-:54:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION
2 3
x y y  y  y
25 0.4225
0.0755
30 0.5000 (– 0.0039)
0.0736 –0.0005
35 0.5736 –0.0044 3rddiff: (const)
0.0692 –0.0005
40 0.6428 –0.0049
0.0643
45 0.7071
 5x  12 
Example 08: Evaluate (i) ∆ tan x (ii) ∆ cos 2x (iii) 2  2
-1 2

 x  5x  6 
Solution: (i) By definition,
 xhx  1  h 
∆tan-1x = tan 1 (x  h)  tan 1 x  tan 1    tan  
 1  (x  h)x   1  hx  x 
2

(ii) ∆2 cos 2x = ∆ [∆(cos 2x)] = ∆[cos(2x + 2h) – cos 2x] = ∆cos(2x + 2h) – ∆cos 2x
= [cos (2x + 2h + 2h) – cos(2x + 2h)] – [cos(2x + 2h) – cos 2x]
= [cos(2x + 4h) – cos (2x +2h)] - [cos(2x + 2h) – cos 2x]
= [–2 sin(2x + 3h).sin h] [–2sin(2x + h).sinh]
= 4 sin2 h sin(2x + 3h) sin(2x + h)
 5x  12  2 5x  12  2 A B  2 2 3 
(iii) 2  2     (x  2)(x  3)     x  2  x  3     x  2  x  3 
 x  5x  6       
 1 1    1 1    1 1 
   2  3     2  x  3  x  2     3  x  4  x  3  
 x  2 x  3       
 1 1   1 1 
 2       3    
 x  3 x  2   x  4 x  3  
 1 1 1 1   1 1 1 1 
 2        3     
 x  4 x  3 x  3 x  2    x  5 x  4 x  4 x  3  
 1 1 1 1   1 1 1 1 
 2        3     
 x  4 x  3 x  3 x  2    x  5 x  4 x  4 x  3  
 1 2 1   1 2 1 
 2       3    
 x  4 x  3 x  2    x  5 x  4 x  3  
 4 6  2(5x  16)
    
 (x  2)(x  3)(x  4) (x  3)(x  4)(x  5)   (x  2)(x  3)(x  4)(x  5)
Example 08: Assuming that the following values of y belong to a polynomial of degree 4,
compute the next three values:
x: 0 1 2 3 4 5 6 7
y: 1 -1 1 -1 1 -- -- --

-:55:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

Solution: The difference table is shown as under:


∆y ∆y ∆y ∆y
2 3 4
x y
0 1
-2
1 -1 4
2 -8
2 1 -4 16
-2 8
3 -1 4 16
∆ y2
3
2
4 1 ∆2y3 16
∆y4 ∆3y3
∆ y4
2
5 y5 16
∆y5 ∆3y4
6 y6 ∆y5
∆y6
7 y7
Since y belongs to a polynomial of degree 4 and the first fourth difference is 16 hence all
differences are also 16.
Now, ∆4y0 = ∆4y1 = ∆4y2 = ∆4y3 = 16.
Also, ∆4y1 = 16 or ∆3y2 – ∆3y1 = 16 or ∆3y2 – 8 = 16 or ∆3y2 = 24
Now, ∆3y2 = ∆2y3 – ∆2y2 = 24 or ∆2y3 – 4 = 24 or ∆2y3 = 28
Also, ∆2y3 = ∆y4 – ∆y3 = 28 or ∆y4 – 2 = 28 or ∆y4 = 30
Finally, ∆y4 = y5 – y4 = 30 or y5 – 1 = 30 or y5 = 31
Now start with ∆ y2 = 16 and solving as above, we get y6 = 129
4

Finally start with ∆ y3 = 16 and solving as above, we get y7 = 351


4

Example 09: Obtain the first term of the series whose 2nd and subsequent terms are 8, 3, 0, -1, 0.
Solution: We know that E[f(1)] = f(2) or f(1) = E-1[f(2)] = (1 + ∆)-1 f(2)
or f(1) = (1 – ∆ + ∆ – ∆ + …) f(2)
2 3
(1)
x y ∆y ∆y2
∆y3
∆4y
2 8
-5
3 3 2
-3 0
4 0 2 0
-1 0
5 -1 2
1
6 0
-:56:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

From the difference table we see that 3 , 4 , 5 ,… differences are zero. Hence, equation (1)
rd th th

becomes: f(1) = f(2) – ∆f(2) + ∆ f(2) = 8 – (–5) + (2) = 15.


2

 
Example 10: Prove that (i)      (ii)        / 2
 
Solution: (i) Consider
  E 1  
 E  1 1  E 1   E 1  E  
 
     y x   1
  yx  

E 1    E 1 
   y   E  1  y  E  E 1 y
E 1 
x 
E 
x x  
  1 E  

 E  
 
 1     1     y x       y x        /      /  

     
(ii) y x    E1/2  E 1/2  y x    E1/2 y x  E 1/2 y x     y h  y h     y h     y h 
     x  2 x   x   x 
2  2  2

2

1 1/2
 1
2
 x
1
 E  E 1/2 y h  E1/2  E 1/2 y h    y x  h  y x    y x  y x h  
x 2
2 2
1 1
  y x  h  y x    y x  y x h         y x         / 2
2 2
Estimating the Missing Term
Suppose some of the values of y = f(x) are given in tabular form but some of them are not known
or missing. The following example illustrates how to find this value.
Example 10: The following table shows a production from 2001 to 2007. Estimate the missing
production.
x: 2001 2002 2003 2004 2005 2006 2007
y: 200 220 260 ----- 350 ----- 430
th
Solution: Since five figures are known, we assume that the 5 differences are zero.
(This is in fact the way we start with). Now,
∆5 y0 = 0 and ∆5y1 = 0
or (E – 1) y0 = 0 (E – 1) y1 = 0
5 5
and
or (E5 – 5E4 + 10E3 – 10E2 + 5E – 1)y0 = 0
and (E5 – 5E4 + 10E3 – 10E2 + 5E – 1)y1 = 0
or E5y0 –5E4 y0 + 10E3 y0 –10E2 y0 + 5E y0 – y0 = 0
and E5y1 –5E4 y1 + 10E3 y1 –10E2 y1 + 5E y1 – y1 = 0
or y5 – 5y4 + 10y3 –10y2 + 5y1 – y0 = 0
and y6 – 5y5 + 10y4 –10y3 + 5y2 – y1 = 0
Substituting the known values
or y5 –5(350) + 10y3 – 10(260) + 5(220) – 200 = 0 or y5 + 10y3 = 3450
and 430 – 5y5 + 10(350) – 10y3 + 5(260) – 220 = 0 or –5y5 – 10y3 = –5010
Solving the two equations, we get: y3 = 306 and y5 = 390
-:57:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

Interpolation with Equally Spaced Data Values


Newton Forward Interpolation Formula
Let y = f(x) be the function with certain values y0, y1, y2, … yn corresponding to equally spaced
values of x = x0, x0 + h, x0 + 2h, … x0 + nh (where x = x0 + nh). Now by definition:
y0  y1  y0 or y1  y0  y0  (1   )y0
y1  y 2  y1 or y 2  y1  y1  (1   )y1  (1   ) 2 y0
y n 1  y n  y n 1 or y n  y n 1  yn 1  (1   )y n 1  (1   ) n y0
By binomial theorem,
n(n  1) 2 n(n  1)(n  2) 3 n(n  1)(n  2)(n  3) 4
(1  )n  1  n        ...
2! 3! 4!
n(n  1) 2 n(n  1)(n  2) 3 n(n  1)(n  2)(n  3) 4
 y n  (1  n        ...)y0
2! 3! 4!
n(n  1) 2 n(n  1)(n  2) 3 n(n  1)(n  2)(n  3) 4
or yn  y0  ny0   y0   y0   y0  ...
2! 3! 4!
where n  (x  x 0 ) / h, where y n  y(x n ).
This is known as Newton Forward Interpolation Formula.
Example 01: The following data shows the volume V of a gas for various temperatures T.
Estimate V when T = 2.5 from the given data by using Newton Forward Interpolation Formula.
Also compute the interpolating polynomial.
T 0 1 2 3 4 5
V 1 2 5 10 17 26
Solution: Let x = T and y = V. Here we see that h = 1 hence; n = (x – x0)/h = (2.5 – 0)/1 = 2.5
Now the Newton forward difference table is:
. x y y  2y  3y
0 1
1
1 2 2
3 0
2 5 2
5 0
3 10 2
7 0
4 17 2
9
5 26
Using Newton Forward Interpolation Formula up to third difference, that is,

n(n  1)2 y0 n(n  1)(n  2)3 y0


y  y0  ny0   (1)
2! 3!
-:58:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

h = 1, n = (x – x0)/h = (2.5 – 2)/1 = 0.5


0.5  (0.5  1)  2 0.5  (0.5  1)(0.5  2)  0
y(2.5)  y  5  0.5 1    7.475
2 3 2
Thus when the temperature T = 2.5 the volume of gas is 7.475. You may observe that T = 2.5 lies
in between 2 and 3 and hence expect that the value of V would lie in between 5 and 10. This is
true in our case because value of V = 7.475.
[Now if an interpolating polynomial is required, we proceed as under:
x  x0 x  0
Putting n    x . Then by using equation (1), we obtain:
h 1
x(x  1).2.1 x(x  1)(x  2).0.1
y 1 x  
2 3 2
 1  x  x2  x  0  y  x2  1 (2)
Readers may observe that the values given in the above table are obtained from the equation
y = x2 + 1 which is exactly the same as found above in (2). This shows that Newton’s
interpolation formula gives exact results for the data obtained from the polynomials. However,
there will be some deviations if the data values are taken from any transcendental function.
Newton Backward Interpolation Formula
Let y = f(x) be the function that assumes values y0 , y1 , y 2 , y n corresponding to equally spaced
values of x, that is, x0, x0 + h, x0 + 2h, …, x0 + nh. Then by definition of backward differences,
y n  y n  y n 1  y1  y1  y0 (when n  1)
 y0  y 1 y1  (1  )y1  y1  (1  ) 1 y0
y 2  y 2  y1 (when n  2)  y1  y 2 y 2  (1  )y 2 or y 2  (1  ) 1 y1
or y2  (1  )2 y0 .Continuing this way, we get y n  (1  )  n y 0
Now expand by using Binomial Expansion, the we have
 ( n)( n  1) 2 ( n)( n  1)( n  2) 3 
y n  1  ( n)     y 0
 2! 3!
 
n(n  1) 2 y 0 n(n  1)(n  2)3 y 0
or y n  y0  ny0   
2! 3!
This is known as Newton Backward Interpolation Formula. It may be noted that: n = (x – xn)/h
and yn = y(xn).
Example 02: Using Newton’s Backward Difference Interpolation formula, compute f(11.8) from
the given tabulated data.
.x 2 4 6 8 10 12 14
.f(x) 23 93 259 569 1071 1813 2843

Solution: The Newton Backward difference table is under.


Now using Newton Backward Interpolation Formula up to 4th differences, we get

-:59:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION
2 3 4
. x y y  y  y  y
2 23
70
4 93 96
166 48
6 259 144 0
310 48
8 569 192 0
502 48
10 1071 240 0
742 48
12 1813 288
1030
14 2843

n(n  1)2 y0 n(n  1)(n  2)3 y0 n(n  1)(n  2)(n  3)4 y0


yn  y0  ny0   
2! 3! 4!
x  x n 11.8  12
h  2, n    0.1 . Now putting the values in (1) then we have
h 2
(0.1)(0.1  1)(248) (0.1)(0.1  1)(0.1  2)(48)
y  1813  (0.1)(742)  
2 6
(0.1)(0.1  1)(0.1  2)(0.1  3)(0)
  1736.832
24
Thus when x = 11.8 the approximate value of y is 1736.832. You may observe that x = 11.8 lies
in 10 and 12 and hence it is expect that the value of y would lie in between 1071 and 1813. This
is true in our case because value of y = 1736.832.
Now if x is unknown and we wish to find the interpolating polynomial of degree say 4, we may
use the above table and determine such polynomial.
Now as x is unknown then n = (x – x0)/h = (x – 14)/2. Then equation (1) becomes:
x  14  x  14  x  14  x  14  x  14 
  1 (288)   1  2  (48)
x  14 2  2  2  2  2 
y  2843  (1030)  
2 2 6
x  14  x  14  x  14  x  14 
  1  2   3  (0)
2  2  2  2 

24
 2843  (x  14)515  (x  14)(x  12)36  (x  14)(x  12)(x  10)
 y  x 3  7x  1. This is the required polynomial.
Sterling’s Interpolation Formula
Newton’s forward interpolation formula is:
n(n  1) 2 n(n  1)(n  2) 3 n(n  1)(n  2)(n  3) 4
yn  y0  ny0   y0   y0   y0  ...
2! 3! 4!
-:60:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

We know that: ∆ y-1 = ∆ y0 – ∆ y-1 ∆ y0 = ∆ y-1 + ∆ y-1


3 2 2 2 2 3
giving
∆ y0 = ∆ y-1 + ∆ y-1 ∆ y0 = ∆ y-1 + ∆ y-1
3 3 4 4 4 5
Similarly, giving
Similarly, ∆4y-1 = ∆4y-2 + ∆5y-2; etc.
Substituting these in the above formula, we get:

yn  y0  ny0 
n(n  1) 2
2!

 y 1  3 y 1  
n(n  1)(n  2) 3
3!

 y 1   4 y 1 
(1)

n(n  1)(n  2)(n  3) 4
4!

 y1  5 y1  ... 
Combining the li ke ter ms, we get:
n(n  1) 2 (n  1)n(n  1) 3 (n  1)n(n  1)(n  2) 4
y n  y0  ny0   y 1   y 1   y 2  ...
2! 3! 4!
This is another for m of Gauss Forward Inte r polation For mula.
[Students are advised to make the above substitution and get the final result]
Remark: In the central differences notations, this formula may be expressed as:
n(n  1) 2 (n  1)n(n  1) 3 (n  1)n(n  1)(n  2) 4
yn  y0  ny1/2   y0   y1/2   y0  ...
2! 3! 4!
This is known as “Central Difference Formula ”
Now reconsider Newton’s forward interpolation formula:
n(n  1) 2 n(n  1)(n  2) 3 n(n  1)(n  2)(n  3) 4
yn  y0  ny0   y0   y0   y0  ...
2! 3! 4!
We know that: ∆2y-1 = ∆y0 - ∆y-1 giving ∆y0 = ∆y-1 + ∆2y-1
∆ y0 = ∆ y-1 + ∆ y-1 ∆ y0 = ∆ y-1 + ∆ y-1
2 2 3 3 3 4
Similarly, giving
Similarly, ∆4y-1 = ∆4y-2 + ∆5y-2; etc. Substituting these in the above formula, we get:


yn  y0  n y1   2 y1   
n(n  1) 2
2!
 y1  3 y1  
n(n  1)(n  2) 3
3!

 y1   4 y1 

n(n  1)(n  2)(n  3) 4
4!

 y1  5 y1  ... 
Combining the li ke ter ms, we get:
(n  1)n 2 (n  1)n(n  1) 3 (n  2)(n  1)n(n  1) 4
yn  y0  ny1   y1   y2   y2  ... (2)
2! 3! 4!
This is known as Gauss Interpolation For mula.
Taking average of for mulae (1) and (2), we get:

 y  y1  n 2 2 n(n 2  1)  3 y1  3 y2  n 2 (n 2  1) 4


y n  y0  n  0 
 2!  y 1      y2  ... (3)
 2  3!  2  4!

This is known as Sterling’s Interpolation Formula.


Remark: Students are advised to take the average of two equations and check the final result (3).
Remark: Before we use the interpolation formula, we must check which formula is suitable to use
under given conditions. We describe this as under:
-:61:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

(i) If a tabulated value is required at the beginning of the table, we use Newton’s
Forward Interpolation Formula (NFIF).
(ii) If a tabulated value is required at the end of the table, we use Newton’s Backward
Interpolation Formula (NBIF).
(iii) If a tabulated value is required at the mid of the table, we use Stirling’s Interpolation
Formula.
Example 03: Use Sterling’s Interpolation formula for the following data and compute the value
of y at 12.2 where y = 1 + log10 sin x. Here x is measured in degree.
x: 10 11 12 13 14
y: 0.23967 0.28060 0.31788 0.35209 0.38368
Solution: The finite difference table is given as under.

--- x y ∆y ∆2y ∆3y ∆4y


–2 10 0.23967
0.04093
–1 11 0.28060 -0.00365
0.03728 0.00058
0 12 0.31788 –0.00307 –0.00013
0.03412 –0.00045
1 13 0.35209 –0.00062
0.03159
2 14 0.38368

Remark: Since we are required to find y at x = 12.2 which is near to 12, hence we consider 12 as
a base value (0–value) and the values above it are marked –1 and –2 and the values below it are
marked 1 and 2 respectively.
Now, n = (x – x0)/h = (12.2 – 12)/1 = 0.2. Employing the Stirling’s Formula, we get:

 y  y 1  n 2 2 n(n 2  1)  3 y 1  3 y 2  n 2 (n 2  1) 4
y n  y0  n  0    y 1      y 2  ...
 2  2! 3!  2  4!

 y0.2
 0.03728  0.03421  0.2
 0.31788  0.2 
2
 
 (0.00307)
 2  2
0.2 (0.2) 2  1  0.00058  0.00045   0.2  (0.2) 2  1
2
    0.00013
    
6  2  24
 0.31788  0.00715  0.00006  0.000002  0.0000002  0.32497

Now actual value of y = 1 + log10 (sin 12.2) = 0.32495. We may observe that this exact value is
very near to Stirling’s Interpolation value.

-:62:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

Interpolation with Unequally Spaced Data Values


Newton forward and backward interpolation formulae possess the disadvantages that they only
are applicable when the data points are equally spaced. However, in practice, the data values may
not be equally spaced. We therefore develop new interpolation formulae for unequally spaced
values of independent variable.
Lagrange’s Interpolation Formula
Let y = f(x) be the function with values y0, y1, …, yn corresponding to x0, x1, … xn, where f(x) be
a polynomial of degree n. Then f(x) may be expressed as:
y = f(x) = a0 (x – x1) (x – x2) …(x – xn) + a1 (x – x0) (x – x2) …(x – xn) + … +
+ an (x – x0) (x – x1) …(x – xn-1) . (1)
Here a0, a1, …,an are some constant to be determined. Putting x = x0, x = x1, …, x = xn in turn in
equation (1), we obtain:
y0
y0 = f(x0) = a0(x0 – x1) (x0 – x2) … (x0 – xn) or a 0 
(x 0  x1 )(x 0  x 2 )...(x 0  x n )
y1
y1 = f(x1) = a1 (x0 – x1) (x0 – x2) … (x0 – xn) or a1 
(x1  x 0 )(x1  x 2 )...(x1  x n )
………….. …………. …………… ………..…. ………… ………
yn
yn = f(xn) = an (xn – x0) (xn – x1) … (xn – xn-1) or a n 
(x n  x 0 )(x n  x1 )...(x n  x n 1 )
Substituting these values of a0, a1, …, an in equation (1), we obtain:
n n
(x  x i ) n
y  f (x)    f (x j )   L j (x)f (x j )  L 0 (x)f (x 0 )  L1(x)f (x1)  ...L n (x)f (x n )
j0 i 0 (x j  x i ) j0
i j
n
(x  x i )
Here, L j (x)   , j = 0, 1, 2, …, n is known as Lagrange polynomial of degree n and is
i 0 (x j  x i )
i j
(x  x 0 )(x  x1 )...(x  x j1) )(x  x j1) )...(x  x n )
defined as: L j (x) 
(x j  x 0 )(x j  x1 )...(x j  x j1 )(x j  x j1 )...(x j  x n )

1 if j  i
Lagrange polynomial has the property that: L j (x i )  
0 if j  i
Example 04: The following table shows breaking strength (BS) against the force applied (F). Use
Lagrange’s Interpolation formula to find BS at F = 7 from the following data given below:
F 1 3 4
BS 4 12 19

Solution: Let F = x and BS = y. As we see that there are three points of interpolation, hence
Lagrange polynomial of degree two is employed.
2
f (x)   Li yi  L0 y0  L1y1  L2 y2 (1)
i 0

-:63:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

 x  x1  x  x 2   7  3  7  4   4  3 
Here, L0            2
 x 0  x1  x 0  x 2   1  3  1  4   2  3 
 x  x0  x  x 2   7  1  7  4   6  3 
L1            9
 x1  x 0   x1  x 2   3  1  3  4   2  1 
 x  x 0  x  x1   7  1  7  3   6  4 
L2            8
 x 2  x 0   x 2  x1   4  1  4  3   3  1 
Now putting the values in (1), we have: y  f (7)  2  4  (9) 12  8 19  52
If we wish to find the interpolating polynomial, we have:
 x  x1  x  x 2   x  3  x  4  x 2  7x  12
L0      
 x 0  x1  x 0  x 2   1  3  1  4  6
 x  x 0  x  x 2   x  1  x  4  x 2  5x  4
L1      
 x1  x 0   x1  x 2   3  1  3  4  2
 x  x 0  x  x1   x  1  x  3  x 2  4x  3
L2      
 x 2  x 0   x 2  x1   4  1  4  3  3
Now putting in (1), we get
x 2  7x  12 x 2  5x  4 x 2  4x  3
y  f (x)  4  12   19
6 2 3
2x 2  14x  24  18x 2  90x  72  19x 2  76x  57

3
2
Simplifying, we get y = f(x) = x + 3. This is the required polynomial of degree two.
COMPUTER PROGRAM
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ clrscr();
float x[10],y[10],p[10],xn,yn,Nu_prod,Den_prod;
int i,j,TD;
clrscr();
cout<<"\tLAGRANGE'S INTERPOLATION";
cout<<"\n\t---------------------------------------\n";
cout<<"\nEnter the total number of tabulated data : ";cin>>TD;
for(i=0;i<TD;i++)
{ cout<<"\nEnter the tabulated value of x"<<i<<" and y"<<i<<" : ";
cin>>x[i]>>y[i];
}
cout<<"\n\tEnter the value of x at which y is required : "; cin>>xn;
for(i=0;i<TD;i++)

-:64:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

{ Nu_prod=1.0;Den_prod=1.0;
for(j=0;j<TD;j++)
{ if(i!=j)
{ Nu_prod*=(xn-x[j]);
Den_prod*=(x[i]-x[j]);
}
}
p[i]=(Nu_prod/Den_prod)*y[i];
}
yn=0.0;
for(i=0; I < TD ; i++)
yn+=p[i];
cout<<"\n\tApproximate value of y at "<<xn<<" is "<<yn;
getch(); }
OUTPUT
LAGRANGE'S INTERPOLATION
Enter the total number of tabulated data: 3 Enter the tabulated value of x0 and y0: 14
Enter the tabulated value of x1 and y1: 3 12 Enter the tabulated value of x2 and y2: 4 19
Enter the value of x at which y is required: 7 Approximate value of y at 7 is 52
Divided Difference Interpolation Formula
Let y = f(x) be the function with values y0 , y1 , y 2 , y n corresponding to x0, x1, …, xn.
We define:
y  y0
The first divided difference as: [x, x 0 ]   y  y0  (x  x 0 )[x, x 0 ]
x  x0
The second divided difference as:
[x  x 0 ]  [x 0 , x1 ]
[x, x 0 , x1 ]  [x  x 0 ]  [x 0 , x1 ]  (x  x1 )[x, x 0 , x1 ]
x  x1
Substituting this value of [x, x 0 ] in (1), we obtain
y  y0  (x  x 0 )[x, x 0 ]  (x  x 0 )(x  x1 )[x, x 0 , x1]
Continuing this way n time, we obtain:
y  y0  (x  x 0 )[x, x 0 ]  (x  x 0 )(x  x1 )[x, x 0 , x1]
 (x  x 0 )(x  x1 )(x  x 2 )[x, x 0 , x1, x 2 ]  ...
 (x  x 0 )(x  x1 )(x  x 2 )...(x  x n )[x, x 0 , x1, x 2 ,..., x n ]
This is known as “Newton Divided Difference Interpolation Formula”.
Remark: It may be noted that both Lagrange and Newton divided difference formula can be
applied for an equally space data values as well.
The following table shows how to evaluate divided differences of first, second, and third orders.

-:65:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

Using the notation given in the table below, Newton Divided Difference Interpolation Polynomial
as defined above can be expressed as:
y = f(x) = y0 + (x – x0) y0 + (x – x0)(x – x1)
2
y0 + (x – x0)(x – x1) (x – x2)
3
y0 …

2 3
. x y

.x0 y0
.
y1  y0
= y0
x1  x 0 2
y0 3
.x1 y1
.
y0
y 2  y1
= y1
x 2  x1
2
.x2 y2
. y1
y3  y 2
= y2
x3  x 2
3
.x3 y3
. y1
2
y 4  y3 y2
= y3
[[ x4 y4
.
x 4  x3

Example 02: Using Newton divided difference interpolation formula, find y at x = 2, and x = 5
using the following tabulating values.
x . 1 3 4
y . 4 12 29
Solution: The difference table is shown as under:

2
. x .y

1 4 12  4
4
3 1
74
3 12 1
19  12 4 1
7
4 19 43

Now using the Newton formula given above, we get:


y = y0 + (x – x0) y0 + (x – x0)(x – x1) 2y0
.

Putting x = 2, we get: y = 4 + (2 – 1) (4) + (2 – 1) (2 – 3)(1) = 6


Putting x = 5, we get: y = 4 + (5 – 1) (4) + (5 – 1) (5 – 3)(1) = 26
Now let us suppose that x is unknown. Then we can find the polynomial of degree two from the
given tabulated data as follows: y = 4 + (x – 1) (4) + (x – 1) (x – 3)(1) = x2 + 3.
WORKSHEET 04
1. The data below represents the bacterial growth in a liquid culture over the number of days.
Day 0 2 4 6 8 10 12 14 16 18 20
Amount
67.38 70.93 74.67 78.6 82.74 87.10 91.69 92.51 101.6 106.95 112.58
(in) 106

-:66:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

2. Kinematics viscosity of water v, related to temperature T is in the following manner.


T(0F) 40 50 60 70 80
V 1.66 1.41 1.22 1.06 0.93

Use a suitable interpolation formula to predict v at T = 62 0F.


3. Under what conditions Lagrange’s interpolation formula is valid to find y at any x. The values
for x and y are given as below:
x
. 05 06 09 11
y
. 12 13 14 16

Interpolate the value of y at x = 10.


4. The pressure p of a wind corresponding to velocity v is given below:
V 10 23 30 40
P 1 2 3 5

Find the pressure when v = 25 using any method and write its computer program.
5. The deflection of a beam f(x) measured at various distances x from one end of a cantilever
beam is given in the following table:
x
. 0.0 0.2 0.4 0.6 0.8 1.0
f(x)
. 0.0000 0.0456 0.1278 0.3494 0.4027 0.4825

Find the deflection at x = 0.85.


6. An experiment is performed to find the relationship between applied stress and the time to
fracture for a stainless steel. Eight different values of stress are applied and the resulting data is:

Applied stress
5 10 15 20 25 30 35 40
X kg/ mm 2

Fracture time, y (sec) 40 30 25 40 18 20 22 15

Predict the fracture time for an applied stress of 12 kg/ mm 2 and interpret your result using
Newton’s forward interpolation formula.
7. The distance covered by a particle at a time t is given below;
T 2 4 6 8 10 12
D 3.6 4.8 6.3 8.0 9.5 10.1

Find distance of particle at t = 3 and t = 9 by using suitable interpolation method.


8. The pressure p of a wind corresponding to velocity v is given below;
V 10 23 30 40
P 1 2 3 5

Find the pressure when v = 25.

-:67:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

9. The pressure drop occurs when water flows through an orifice meter is measured using a
differential pressure transmitter. The output current is converted to voltage drop by a resistor. The
objective is to correlate the flow rate with the voltage drop. The following data were collected
from an experiment.

F (g min-1) 3 5 6 9

P (mV) 293 508 585 764

Estimate the pressure at 8g min-1 flow rate with the voltage drop by a resister, using suitable
interpolation method.
10. Define an Interpolation and drive the Newton’s backward difference interpolation formula.
Surface tension of water is measured at various temperatures. It decreases with temperature as
follows:
T(0C) 20 30 40 50 60

 (mNm-1) 72.8 71.2 69.6 67.9 66.2

Derive the interpolating polynomial, using the forward difference interpolation formula. Use this
polynomial to estimate surface tension at 250C and 530C.
11. The population statistics pertaining to a town for 4 years is recorded as under.
Year 1950 1960 1970 1980
Population (mil) 7 10 15 20

Apply Lagrange’s formula to predict the population for the year 1975.
12. The following data was taken from an experiment that measured the production (P) of
automobile components fabricated by lath machine against the time in hours.
Time 50 80 90 110
Production 725 905 1190 1423
Determine P for t = 30 hrs and t = 120 hrs using Lagrange or Newton-Divided difference formula.
13. The data below represents the twist which impart by twist gear in simplex machine (m/c).

Twist gear 40 42 44 46 48

Twist 7.25 6.91 6.59 6.31 6.04

Estimate the amount of twist when we use 45 twist gears?


14. The following data shows mass variation against length.

Length 30 32 35 37
Mass variation 99 105 1065 107
Interpolate the value of mass variation at length equal to 34?

-:68:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION

15. The data given below shows the draft and speed of front roller of draw frame
Draft 5 6 7 8 9 10
Speed 26 29 31 34 37 40
Find the speed, when draft is 6.5?
16. The following data shows pix versus length.
Length 1 inch 2 inch 3 inch 4 inch
Pix 20 36 54 69

Find Pix when length is 0.5 inch and 5 inch.


17. Prove that (i) μ2 = 1 + δ2 /4 (ii)  = δ E-1/2 = 1 – E-1 = 1 – (1 + ∆)-1
(iii) E1/2 = μ + δ/2 (iv) E-1/2 = μ – δ/2 (iv) μ.δ = [∆E]/2 + ∆/2
2 2
(v) 1  22  1  2 / 2 (vi)    1 (vii)   E1  E1  1  
2 4
(viii) E 1/2     / 2 (ix)   E1/2  E1/2  
(x)  E1/2  E 1/2  E 1  
1/2 1/2
(xi)     2 (xii)    1      1   
18. Evaluate the following:
(i) (∆ – 1)(2∆ + 1) (x2 + x + 2) (ii) (  + ∆)2 (x2 + x + 1) (iii) ∆2E3 x3 (iv) (E-1∆)x3
(iv) (E + 2) (E – 1) (x2 + 1) (v) (xE2 – 2xE – 1)x3 (vi) (E-1∆) x3
19. Given the table below, find (i) ∆ y10 (ii) ∆y20 (iii) ∆ y15 and ∆ y10.
2 3 5

. x: 10 20 30 40
. y: 1.1 2.0 4.4 7.9
20. Construct a difference table for the following data and find ∆3f(2).
. x: 0 1 2 3 4
. y: 1.0 1.5 2.2 3.1 4.6

21. Prove that: (i) y 4  y3  y 2   2 y1  3 y1 (ii) y 4  y0  4y0  6 2 y 1  103 y 1


22. Find the missing values in the following table.
x: 2 2.1 2.2 2.3 2.4 2.5
y: 0.135 --- 0.111 0.100 --- 0.082
24. Use Striling’s Interpolation Formula to compute log 337.5 using the following data drawn by
function log x.
x: 310 320 330 340 350 360
y: 2.4914 2.5052 2.5185 2.5315 2.5441 2.5563

-:69:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

CHAPTER
FIVE NUMERICAL
DIFFERENTIATION
AND INTEGRATION

INTRODUCTION
Consider a function f(x) = x3 + 2 x – 5. Then f `(2) = 14 and f ``(2) = 12. Thus we can find the
derivatives of any function at any point provided they exist. We know that physical meaning of
the first derivative of any time dependent variable function s = f(t) represents the velocity and the
second derivative the acceleration. Now suppose that a particle moves in such a way that it covers
the following distances at different time intervals as shown below:
Time (t) in Minutes: 0 2 4 6 8 10 12 14
Distance (s) in Meters: 0 100 500 1000 1700 2600 4000 5500
Now if we ask what are the magnitudes of velocity and acceleration at time t = 1 minute and at t =
14 minutes, then we do not have a formula in the form of function that gives the answers to the
above questions.
Let us consider a situation where we are given values of current flow in a circuit at different time
intervals as shown in the following table.
t (sec): 0 3 6 9 12 15 18 21 24 27
I (A): 0 2 5 10 19 24 39 50 66 86
If we are asked to find the total charge Q in the circuit when t = 27 seconds we do not have an
answer of this question at a moment. Now we know that current I is the rate of change of charge
w.r.t time t, that is, I = dQ/dt, hence
27 27

 I dt   (dQ / dt) dt  Q
0 0
It means that if we integrate I w.r.t t from 0 to 27, we get the total charge in the circuit.
In the situations as described above there are no analytical methods that provide the solutions of
problems described above. Thus the only way to get solutions of such problems is to use
numerical methods. In the following sections we shall derive formulae for numerical
differentiation and integration which will help us to get the solution of the above problems.
Remark: It may be noted that numerical differentiation and integration are the processes to find
the derivatives of f(x) and integration of f(x) when values of f(x) are given in tabular form.
NUMERICAL DIFFERENTIATION
Let y = f(x) be function with certain values y0, y1, …, yn corresponding to x0, x1, …, xn where
x0, x1, …, xn are equally spaced so that xn = x0 + nh. Now by Newton Forward Interpolation
n(n  1) 2 n(n  1)(n  2) 3
Formula, y(x n )  y0  ny0   y0   y0 
2! 3!

-:70:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

n(n  1) 2 n(n  1)(n  2) 3


 y(x 0  nh)  y0  ny0   y0   y0 
2! 3!
Putting n  r, we obtain:
(r 2  r) 2 (r 3  3r 2  2r) 3
y(x 0  rh)  y0  ry0   y0   y0 
2! 3!
Now differentiate w. r. t. r, we get
1 (2r  1) 2 (3r 2  6r  2) 3 
y(x 0  rh)  y(x r )   y0   y0   y0  ... 1
h  2 6 
This is known as first derivative Newton Forward Interpolation formula.
Differentiate equation (1) w. r. t. r once again, we get
1 (6r  6) 3 
y(x 0  rh)h   0   2 y0   y0  
h 6 
1
or y(x 0  rh)  y(x r )  2   2 y0  (r  1)3 y0    2
h  
nd
This is known as 2 derivative Newton Forward Interpolation formula.
In particular, if x = x0 then r = 0, in which case equations (1) and (2) in simplified form will be:

1  2 y0 3 y0 3  1
y  x 0   y0    y0  ... and y  x 0   2  2 y0  3 y0  ...
h  2 3  h  

Example 01: The table below shows the tabulated values of an unknown function y = f(x). Using
Newton’s formula of numerical differentiation, find the first and second derivatives of f(x) at
x = 2. Also develop these formulae in terms of independent variable x.
x 0 1 2 3 4
y 1 2 5 10 17

Solution: Here, h = x1 – x0 = 1 – 0 = 1 and r = (x – x0)/h = (2 – 0)/1 = 2


The difference for above tabulated values is:

x y  2 3
0 1
3–1 = 2
4–2 = 2
1 3 2–2 = 0
7–3 = 4
2 7 6–4 = 2
13–7 = 6
3 13
2–2 = 0
8–6 = 2
21–13 = 8
4 21

Taking 1st derivative Newton Forward Formula up to third differences:


1 (2r  1) 2 (3r 2  6r  2) 3 
y(x 0  rh)   y0   y0   y0  
h  2 6 
-:71:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

Now substituting the values of finite differences and those of r and h, we get
1 (2.2  1) (3.4  6.2  2) 
y(0  2)  2  .2  .0  ...  y(2)  5
1 2 6 
This shows that numerical value of first derivative of f(x) at x = 2 is 5, i.e., f `(2) = 5.
The second derivative Newton Forward Formula is:
1  2
y(x 0  rh) 
2 
 y0  (r  1)3 y0  ...
h 
Substituting the values of finite differences and those of r and h, we get
y``(2) = f ``(2) = 2.
Now let us find the first and second derivatives of a function in terms of x. Taking h = 1 and
r = (x – x0)/h = (x – 0)/1 = x, we get
1 (2r  1) 2 (3r 2  6r  2) 3 
y`(x 0  rh)h   y0   y0   y0  ...
h  2 6 

1 (2x  1) (3x 2  6x  2) 
y(0  x)   2  .2  .0  giving y` = f `(x) = 2 x + 1
1  2 6 
Taking the 2nd derivative Newton Forward Interpolation Formula up to third difference and
substituting the given values and simplifying, we get: y`` = f ``(x) = 2.
Example 02: Find the magnitudes of velocity and acceleration at t = 2.5 minutes, from the given
tabulated data when a body covers different distances at various time intervals.
.t (m) 2 2.5 3 3.5 4 4.5
[[ s (km) 10 12 15 18 22 27

Solution: Here h = t1 – t0 = 2.5 – 2 = 0.5 and r = (t1 – t0)/h = 1


The difference table is:
t
. s ∆ ∆2 ∆3 ∆4 ∆5
2 10
2
2.5 12 1
3 –1
3 15 0 2
3 1 –3
3.5 18 1 –1
4 0
4 22 1
5
4.5 27
Taking 1st derivative Newton Forward Interpolation Formula up to fifth difference:

 (2r  1) 2 (3r 2  6r  2) 3 4r 3  18r 2  22r  6 4 


 s0   s0   s0   s0  
1
s(t 0  rh)   
2 6 24
h  5r 4  40r 3  105r 2  100r  24 5 
  s0  ... 
 120 

-:72:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

Now putting the given values and simplifying, we get

s`(2.5) = 5.966 which means that body’s speed at time t = 2.5 minutes is approximately 5.97
.

km/min. Now taking 2nd derivative forward formula:

1  2 6r 2  18r  11 4 2r 3  12r 2  21r  10 5 


s(t 0  rh)h 
2 
 s 0  (r  1)  3
s 0   s 0   s 0  ... Using
h  12 12 
the values of finite differences and those of r and h and simplifying, we get
s``(2.5) = 1.333
This shows that the approximate acceleration of a body is 1.333 km/min2 at time t = 2.5 minutes.
NUMERICAL INTEGRATION
There are two main reasons for us to need to do numerical integration:
(i) analytical integration may be impossible or infeasible (ii) we may wish to integrate tabulated
data rather than known functions. Y y = f(x)

In this section we outline the main approaches


to numerical integration. Which is preferable
depends in part on the results required, and in part
on the function or data to be integrated.
It may be noted that in this section we are interested X
in finding the definite integration of f(x) between a b
two limits say a and b. Further to note the definite integration provides the area under the curve
y = f(x) bounded between the x-axis and the ordinates at x = a and x = b. This is shown in the
above figure.
Manual Method
If we were to perform the integration by hand, one approach is to superimpose a grid on a graph
of the function to be integrated, and simply count the squares, counting only those covered by
50% or more of the function. Provided the grid is sufficiently fine, a reasonably accurate estimate
may be obtained.
If the grids are not taken sufficiently fine, the error Y y = f(x)
in the computation of area under the curve y = f(x)
between the point x = a and x = b is significantly
large. Hence manual method is not preferred specially
when the interval [a, b] is too large.
Newton’s Cotes Formula a b X
Computation of integral when f(x) is given in tabular form is called numerical integration where
b
geometrically  f (x)dx represents the area under the curve y = f(x), x-axis and the ordinates at
a
x = a, and x = b.
Let the interval [a, b] = [x0, xn] be subdivided into n equal sub-intervals of width h. Then
x = x0 + nh, giving n = (x – x0)/h and dx = h dn.
b n
  f (x)dx  h  f (x 0  nh)dn
a 0

-:73:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

We know that the Newton’s forward interpolation formula is given by:


y = f(x)

y0 y1 y2 y3 y4 yn-1 yn

x0 x1 x2 x3 x4 xn-1 xn
n(n  1) 2 n(n  1)(n  2) 3 n(n  1)(n  2)(n  3) 4
f (x)  y0  ny0   y0   y0   y0  ...
2! 3! 4!
b n
Putting in (1), we have  f (x)dx  h  f (x 0  nh)dn
a 0

n n
 n(n  1) 2 n(n  1)(n  2) 3 n(n  1)(n  2)(n  3) 4 
 f (x0  nh)dn  h   y0  ny0  2!  y0  3!
 y0 
4!
 y0  ... dn

0 0

Integrating and simplifying, we get


n
 n n(2n  3) 2 n(n  2)2 3 
 f (x 0  nh)dn  nh 

y 0 
2
y 0 
12
 y 0 
24
 y 0  ...

(1)
0

This formula is known as Newton’s Cotes Quadrature Formula. Particular cases of


Newton-Cotes formula for n =1, n = 2, n = 3, n = 4, and n = 6 are called Trapezoidal Rule,
Simpson’s 1/3 Rule, Simpson’s 3/8 Rule, Booles Rule, and Weddle’s Rule respectively.
(I) Trapezoidal Rule
Putting n = 1 in equation (1) and taking curve through (x0, y0) and (x1, y1) as a straight line i.e. a
polynomial of first degree so that the differences of order higher than first become zero, we get
x x h
1 0
 1  h
 f (x)dx  h  y0  y0    y0  y1  .Similarly,
x  2  2
0
x2 x3 x n
h h h
 f (x)dx   y1  y2 ,  f (x)dx   y 2  y 3  , ... ,  f (x)dx   y n 1  y n ,
2 2 2
x1 x2 n 1
x
Now adding above integrals, we have
b xn
h
I   f (x) dx   f (x) dx  2  y0  2(y1  y 2  ...y n 1 )  y n 
a x0
This is known as general Trapezoidal Rule or Trapezoidal Rule.
Local and Global Errors for Trapezoidal Rule
The error that occurs at each step of computation is known as “Local Error” and sum of all the
local errors is termed as “Global Error”.
For Trapezoidal Rule the Local and Global Errors are respectively given by:
1 3
L.E   h f (), where a    b
12

-:74:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

1 2
and G.E   h (b  a)f (), where a    b.
12
1
Example 01: Evaluate  dx / (1  x 2 ) by
0
(i) analytical method and (ii) trapezoidal rule taking n = 10 and discuss the results.
Solution: We know that
1
1
 dx / (1  x
2
)   tan 1 x   tan 1 1  tan 1 0   / 4  0   / 4  0.7854
 0
0
Now h = (b – a)/n = (1 – 0)/10 = 0.1. The following table shows the values of y for x = 0, (0.1), 1.
x: 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.00
y: 1 0.99 0.96 0.92 0.86 0.80 0.74 0.67 0.61 0.55 0.50
. y0 y1 y2 y3 y4 y5 y6 y7 y8 y9 y10
Now, by trapezoidal rule,
I = h/2 [y0 + 2(y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9) + y10]
1
Taking h = 0.1 and the tabulated values of y, we get: I =  dx / (1  x 2 )
0
= 0.05 [1+2(0.99 + 0.96 + 0.92 + 0. 86 + 0. 80 + 0.74 + 0.67 + 0.61 + 0.55) + 0.50] = 0.785
This result agrees with the exact solution of given integral up to 3 d.p.
To find the local and global errors for trapezoidal rule, we proceed as under:
1 3
L.E   h f (), where a    b.
12
Here f (x)  1 / (1  x 2 )  f (x)  2(3x 2  1) / (1  x 2 )3 , a  0, b  1 and h  0.1
Now, f (0)  2 and f (1)  0.5, therefore
Minimum L.E  (0.1)3 ( 2) / 12  0.000167
Maximum L.E  (0.1)3 (0.5) / 12  0.00004167 (neglecting the  ve sign)
1 2
G.E   h (b  a)f (), where a    b.
12
Minimum G.E  (0.1) 2 (1  0)( 2) / 12  0.00167
Maximum G.E  (0.1) 2 (1  0)(0.5) / 12  0.0004167 (neglecting the  ve sign)
COMPUTER PROGRAM FOR TRAPEZOIDAL RULE
#include<constream.h>
#include<math.h>
#define f(x)(1/(1+(x*x)))
void main()
{ clrscr();
float a,b,h,sum=0.0,TR;
int n,i;
gotoxy(30, 2);
cout<<"TRAPEZOIDAL RULE";
gotoxy(30, 3);

-:75:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

cout<<"==================\n";
cout<<"\nEnter the lower and upper limits of a function : ";
cin>>a>>b;
cout<<"\nEnter the total no. of strips : "; OUTPUT
Enter the lower & upper
limits of a function: 0, 1
cin>>n; Enter the total no. of
h=(b-a)/n; strips: 10
The strip length is 0.1
cout<<"\nThe strip length is "<< h; x f(x)
0 1
cout<<"\n\n\tx"<<"\t\tf(x)\n"; 0. 1 0.909091
cout<<"\t=================\n"; 0.2 0.833333
0.3 0.769231
cout<<"\t"<<a<<"\t\t"<<f(a)<<endl; 0.4 0.714286
0.5 0.666667
for(i=1;i<n;i++)
0.6 0.626
{ 0.7 0.588235
0.8 0.555556
sum+=f(a+i*h); 0.9 0.526316
cout<<"\t"<<a+i*h<<"\t\t"<<f(a+i*h)<<endl; 1.0 0.5
===============
} Result: 0.693771
cout<<"\t"<<b<<"\t\t"<<f(b);
TR=(h/2.0)*(f(a)+f(b)+2*sum) ;
cout<<"\n\n=============\n”;
cout<<”Result: "<<TR;cout<<\n==============”;
getch();
}
Remark: Students have to make necessary changes in line # 3 of this program while putting the
function whose integration is to be evaluated.
(II) Simpson’s 1/3 Rule
Substituting n = 2 in equation (1) and taking the curve through (x0, y0), (x1, y1) and (x2, y2) as a
polynomial of degree two (parabola) so that the differences of order higher than second vanish,
x 2  x 0  2h
  2 y0  h
we get:  f (x)dx  2h  y0  y0     y0  4y1  y 2  .
x0  6  3

Note: y0  y1  y0 ,  2 y0  y 2  2y1  y0


x4 x6
h h
Similarly,  f (x)dx 
3
 y 2  4y3  y 4 ,  f (x)dx  3  y 4  4y5  y 6 ...
x2 x4
xn
h
and  f (x)dx   y n 2  4y n 1  y n 
x n 2
3

Summing all these results, we obtain


b xn
h
I   f (x)dx   f (x)dx 
3
 y0  4(y1  y3  ...  y n 1 )  2(y 2  y 4  ...  y n 2 )  y n 
a x0

This is known as general formula of Simpson’s 1/3 Rule.


Remark: As we observe from the derivation of Simpson’s 1/3 rule that number of division of the
interval i.e., n must be an even positive integer.
-:76:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

1
Example 02: Evaluate  dx / (1  x 2 ) using Simpson’s 1/3 rule taking n = 10. Comparing the
0
result obtained with that of trapezoidal method.
Solution: The tabulated values are:
x: 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.00
y: 1 0.99 0.96 0.92 0.86 0.80 0.74 0.67 0.61 0.55 0.50
y0 y1 y2 y3 y4 y5 y6 y7 y8 y9 y10
1
Using the Simpson’s 1/3 formula: I =  dx / (1  x 2 ) dx
0

= h/3 [y0 + 4(y1 + y3 + y5 + y7 + y9) + 2(y2 + y4 + y6 + y8) + y10]


= 0.1/3[1 + 4(0.99 + 0.92 + 0.80 + 0.67 + 0.55) + 2(0.96 + 0.86 + 0.74 + 0.61) + 0.5]
= 0.7854
Observe that Simpson’s 1/3 rule gives better accuracy than trapezoidal rule because the results are
correct to 4 d.p.
Local and Global Errors for Simpson’s 1/3 Rule
For Simpson’s 1/3 Rule the Local Error and Global Error are given by:
1 5 iv 1 4
L.E   h f (), where a    b and G.E   h (b  a) f iv (), where a    b
90 180

Now, f (x) 
iv 
2 30x 4  8x 3  16x 2  36x  12 
1  x 
5
2

 f iv (0)  24 and f iv (1)  3.875, b  1,a  0, h  0.1.


1 5 iv
The Local Error is : L.E   h f (), where a    b. Thus,
90
Minimum L.E  (0.1)5 (24) / 90  0.00000267 (Neglecting  ve sign)
Maximum L.E  (0.1)5 (3.875) / 90  0.00000043 (Neglecting  ve sign)
1 4
Now G.E   h (b  a) f iv (), where a    b.Now (b  a)  1.Thus,
180 OUTPUT
SIMPSON’S 1/3 RULE
Minimum G.E  (0.1) 4 (24) / 180  0.000013 (Neglecting  ve sign) Enter the lower & upper limits
(a, b): 0, 1
Maximum G.E  (0.1) 4 (3.875) / 180  0.00000215 (Neglecting  ve sign) Enter the total no. of
strips: 10
COMPUTER PROGRAM FOR 1/3 RULE The strip length is 0.1
x f(x)
#include<constream.h> 0 1
#include<math.h> 0. 1 0.909091
0.2 0.833333
#define f(x)(sin(x)) 0.3 0.769231
0.4 0.714286
void main() 0.5 0.666667
{ clrscr(); 0.6 0.626
0.7 0.588235
float a,b,h,S13R,sum1=0.0,sum2=0.0; 0.8 0.555556
int i,n; 0.9 0.526316
1.0 0.5
gotoxy(30,2); ===============
Result: 0.69315
cout<<"SIMPSON'S 1/3 RULE";

-:77:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

gotoxy(30,3);
cout<<"=================\n";
cout<<"\nEnter the uper and lower (a,b) limits : ";
cin>>a>>b;
cout<<"\nEnter the total number strips : ";
cin>>n;
h=(b-a)/n;cout<<”\n\n\tx\t\tf(x)\n”;
cout<<”\t------------------------\n”;
for (i=1;i<n;i+=2)
sum1+=f(a+h*i);
cout<<”\t”<<a+i*h”\t\t<<f(a+i*h)<<endl;
for (i=2;i<n-1;i+=2)
sum2+=f(a+h*i);
S13R=(h/3.0)*(f(a)+4.0*sum1+2.0*sum2+f(b));
Cout<<”\n\n==============\n”;
cout<<"RESULT " <<S13R;
cout<<”\n================”;
getch(); }
Remark: Students have to make necessary changes in line # 3 of this program while putting the
function whose integration is evaluated.
(III) Combination of Trapezoidal Rule And Simpson’s 1/3 Rule
We know that Simpson’s 1/3 rule is used when the interval [a, b] is divided into even number of
sub-intervals. This rule is still applicable when n is odd. This is done by using the combination of
Trapezoidal and Simpson’s 1/3 rules. The process is as under:
Suppose that n = 11 and the data values are y0, y1, y2 , …, y11.

i. Use Simpson’s 1/3 rule by using taking the values y0, y1, y2 , …, y10 and compute

IS = h/3 [y0 + 4(y1 + y3 + y5 + y7 + y9) + 2(y2 + y4 + y6 + y8) + y10]

ii. Now use the Trapezoidal rule for the last two values of y, i.e., IT = h/2 [y10 + y11].

iii. Then I = IS + IT gives the approximate value of the required integral.


1

 dx / (1  x
2
Students are advised to solve the integral ) taking n = 11 whereas the x values may
0
be taken as 1/11, 2/11, 3/11, 4/11, 5/11, 6/11, 7/11, 8/11, 9/11, 10/11, and 11/11 = 1 in order to
get some better results.
(III) Simpson’s 3/8 Rule
Putting n = 3 in Newton’s Cote’s formula derived above and taking the curve through four points
(x0, y0), (x1, y1), (x2, y2), (x3, y3) as a polynomial of degree three so that the differences of order
higher than three vanish, we get:
x3
 3 3 1  3
 f (x)dx  3h  y0  2 y0  2  y0  3 y0   (y0  3y1  3y 2  y3 )
2

x0
8  8

-:78:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

x6 x9
3 3
Similarly,  f (x)dx   8 (y3  3y4  3y5  y6 ),  f (x)dx  8 (y6  3y7  3y8  y9 ) and so on.
x3 x6
Now adding the above integrals, we get:
b xn
3h
I   f (x)dx   f (x)dx 
8
 y0  3(y1  y2  y4  y5  ...  yn 1)  2(y3  y6  ...  yn 3 )  yn 
a x0
This is known as Simpson’s 3/8 Rule. It may be noted that as in Simpson’s 1/3 rule, n must be an
even similarly, in case of Simpson’s 3/8 rule n must be multiple of 3, viz; n may be 3 or 6 or 9 or
12 or 15, etc.
1
Example 03: Evaluate  dx / (1  x 2 ) using Simpson’s 3/8 rule taking n = 15. Comparing the
0
result obtained with that of trapezoidal rule, Simpson’s 1/3 rule and combined rule of trapezoid
and Simpson’s rules.
Solution: Here h = 1/15. The tabulated values are as under:
x: 0 1/15 2/15 3/15 4/15 5/15 6/15 7/15 8/15 9/15 10/15 11/15 12/15 13/15 14/15 1
y: 1 .996 .983 .961 .934 .900 .862 .821 .779 .735 .692 .650 .610 .571 .534 .5
y0 y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15
Now using the above formula, we get:
3h
I
8
 y0  3(y1  y2  y4  y5  y7  y8  y10  y11  y13  y14 )  2(y3  y6  y9  y12 )  y15 
3 1 1
 1  3(7.860)  2(3.168)  0.5  1  23.58  6.336  0.5  0.7854
8  15 40
This agrees with the exact value of given integral. Now we use combined Trapezoidal and
Simpson’s rule as n is an odd integer. If we use Simpson’s 1/3 rule, take the values from y0 to y14,
so that
IS = h/3 [y0 + 4(y1 + y3 + y5 + y7 + y9+ y11 + y13) + 2(y2 + y4 + y6 + y8 + y10 + y12 ) + y14]
=1/40[1 + 4(4.634) + 2(4.862) + 0.534] = 0.74485
Now, use Trapezoidal rule by taking y14 and y15 only, we get
IT = h/2 [y14 + y15] = 1/30 [0.534 + 0.5] = 0.0345 Note: h = 1/15
Thus approximate value of given integral is: Now, IS + IT = 0.74485 + 0.0345 = 0.77935 ~ 0.78
1
Remark: The exact value of I =  dx / (1  x 2 ) = 0.7854
0
COMPUTER PROGRAM FOR SIMPSON 3/8 RULE
#include<constream.h>
#include<math.h>
#define f(x)(1/(1+x*x))
void main()
{ clrscr();
float a, b, h, sum, sum1=0.00, sum2=0.00;
int i, j, n;
gotoxy(30, 2); cout<<”SIMPSON’S 3/8 RULE”;

-:79:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

gotoxy(30, 3); cout<<”\t---------------------\n”;


cout<<"Enter the Lower and Upper (a,b) Limits : "; OUTPUT
SIMPSON’S 3/8 RULE
cin>>a>>b; Enter the lower & upper
limits(a, b): 0, 1
cout<<"Enter the total strips : "; cin>>n; Enter the total no. of
h=(b-a)/n; strips: 15
The strip length is 0.1
cout<<”\n\n\tx\t\tf(x)\n”; x
. f(x)
cout<<”\t---------------------\n”; 0 1.000
for (i=0 ;i<=n-1;i++) 0.066 0.996
0.133 0.983
cout<<”\t”<<a+i*h<<”\t\t<<fa + i*h)<<endl; 0.200 0.961
j = 3.0; 0.267 0.934
for(i = 1; i <=n; i++) 0.334 0.900
0.400 0.862
{ if(i = = j) 0.467 0.821
{ sum2=sum2 + f(a + h*i); j = j+3; 0.534 0.779
} 0.600 0.735
0.667 0.692
else 0.733 .650
sum1=sum1+f(a+h*i); } 0.800 0.610
0.867 0.571
sum = ((3.0*h)/8.0)*(f(a)+3.0*sum1+2.0*sum2+f(b));
0.934 0.534
cout<<”\n\n=================\n”<<”Result:”<<sum; 1.000 0.5
cout<<”\n\n=================\n”;getch(); } ===============
Result: 0.7854
Remark: Students have to make necessary changes in line # 3 of this
program while putting the function whose integration is evaluated.
.

(IV) Boole’s Rule


Substituting n = 4 in the Newton-Cote’s formula and using a polynomial of degree 4 with
elimination of the forward differences of order higher than four, we obtain:
x4
2h
 f (x)dx  45  7y0  32y1  12y 2  32y3  7y 4 
x0
This is known as Boole’s Rule. It may be noted that the value of n must be multiple of 4.
(V) Weddle’s Rule
Substituting n = 6 in the Newton-Cote’s formula and using a polynomial of degree 6 with
elimination of the forward differences of order higher than six, we obtain:
x6
3h
 f (x)dx  10  y0  5y1  y2  6y3  y4  5y5  y6 
x0
It may be noted that the value of n must be multiple of 6.
Remark: Students are advised to complete the above formulae by taking sufficient number of
terms in Newton’s Cote’s formula. This is left as an exercise.

WORKSHEET 05
1. Find the first and second derivatives of f(x) at x = 1.5 if
x 1.5 2.0 2.5 3.0 3.5 4.0
F(x) 3.375 7 13.625 24 38.875 59
2. A rod is rotating in a plane. The following table gives the angle  (radians) through which the
rod has turned for various values of the time t second.
t 0 0.2 0.4 0.6 0.8 1.0 1.2
 0 0.12 0.49 1.12 2.02 3.20 4.67

Calculate the angular velocity and the angular acceleration of the rod, when t = 0.6 sec.
-:80:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

3. The population of a certain town (as obtained from census data) is shown in the following
table:
Year 1961 1971 1981 1991 2001
Population (in thousands) 19.96 39.65 58.81 77.21 94.61

Find the rate of growth of population in 1991.


4. The following data gives corresponding values of pressure and specific volume of a
superheated steam.
v 1.70 1.74
. 1.78 1.82 1.86
p 0.9916 0.9857
. 0.9781 0.9691 0.9584

Find the rate of change of


i. pressure with respect to volume when v = 2
ii. volume with respect to pressure when p = 105.
5. A slider in a machine moves along a fixed straight rod. Its distance x (cm) along the rod is
given below for various values of the time t seconds. Find the velocity of the slider and its
acceleration when t = 0.3.
t 0 0.1 0.2 0.3 0.4 0.5 0.6
x 30.13 31.62 32.87 33.64 33.95 33.81 33.24

6. The following table represents the values of Sine function


x 0.0 0.1 0.2 0.3 0.4 0.5 0.6
F(x) 0.000 0.0998 0.1987 0.2955 0.3894 0.4794 0.5646

Compute the integral of sin x from 0 to 0.6 using:


a. Trapezoidal rule b. Simpson’s 1/3 & 3/8 rules d. Boole’s rule e. Weddle’s rule
6
1
7. Evaluate  1  x2 dx using the following rules and compare the results.
0
i. Trapezoidal rule ii. Simpson’s 1/3 rule iii. Simpson’s 3/8 rule iv. Weddle’s rule.
8. A river is 80ft wide, the depth d in feet at a distance x feet from one bank are given by the
following table:
x . 0 10 20 30 40 50 60 70 80
D 0 4 7 9 12 15 14 8 3

Find approximately the area of the cross-section.


9. The velocity v(km/min) of a moped which starts from rest, is given at fixed intervals of time
t(min) as follows:
t
. 2 4 6 8 10 12 14 16 18 20
. v 10 18 25 29 32 20 11 5 2 0

10. A solid of revolution is formed by rotating about the x-axis, the area between the x-axis, the
lines x = 0, and x = 1 and a curve through the points with the following co-ordinates:
. x 0 0.25 0.5 0.75 1
. y 1 0.9896 0.9589 0.9089 0.8415
Estimate the volume of the solid formed using Simpson’s rule.

-:81:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION

11. A rocket is launched from the ground; its acceleration is registered during the first
80 seconds and is given in the table below. Using
i. Trapezoidal rule ii. Simpson’s 1/3 rule and iii. Simpson’s 3/8 rule respectively.
Find the velocity of the rocket at t = 80 sec.
t (Sec)
. 0 10 20 30 40 50 60 70 80
a (cm/sec2)
. 0 4 7 9 12 15 14 8 3
12. The velocity v of a particle at distance s from a point on its path is given by the table.
s (ft)
. 0 10 20 30 40 50 60
v (ft/sec)
. 47 58 64 65 61 52 38

Estimate the time taken to travel 60 ft by using Simpson’s 1/3 rule and compare the result with
Simpson’s 3/8 rule.
13. Suppose that a particle moves in such a way that it covers the following distances at different
time intervals as shown below:
Time (t) in Minutes: 0 2 4 6 8 10 12 14
Distance (s) in Meters: 0 100 500 1000 1700 2600 4000 5500
Find the magnitudes of velocity and acceleration at time t = 2 minutes and at t = 14 minutes.
14. The following table shows current flow in a circuit at different time intervals.
t (sec):
. 0 3 6 9 12 15 18 21 24 27
I (A): 0 2 5 10 19 24 39 50 66 86
Find the total charge Q in the circuit when t = 27 seconds.
15. The following data gives corresponding values of pressure and specific volume of
superheated steam. Find the rate of change of pressure w. r.t. volume v = 6 and v = 15.
v
. 3 6 9 12 15
p
. 90 55 35 21 13

16. A jet fighter’s position on aircraft carrier runway was noted during landing:

. t (sec) 0 0.5 1.0 1.5 2.0 2.5 3.0


. x (miles) 154 186 209 250 269 285 299

where x is the distance from the end carrier. Estimate the velocity and acceleration.
17. In hottest week the temperature of Hyderabad is recorded as under;
Day 1 2 3 4 5 6 7
Temp Co 43.7 44.0 43.8 44.1 43.5 42.9 44.0
th
Calculate the rate of change in the temperature at 5 day.
18. Find the first and derivation back roller speed in ring frame at 3.0, if
Draft 2.5 3 3.5 4 4.5
Back roller speed 21 22 23.5 25.5 28
19. The velocity of warping machine winds the length of warp yarn is given by table
Twist gear 40 42 44 46 48
Twist 7.25 6.91 6.59 6.31 6.04
Estimate time taken to wind 6000 yards of yarn by using Simpson’s 1/3rd rule and compare with
Trapezoidal rule?

-:82:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

CHAPTER
SIX NUMERICAL SOLUTIONS
OF ORDINARY
DIFFERENTIAL EQUATIONS

INTRODUCTION
Many problems in engineering and science are formulated into ordinary differential equations
satisfying certain given conditions. If these conditions are set at only one point, the differential
equation together with these conditions is known as an initial value problem. If the conditions are
lay down at two or more points the same problem is termed as boundary value problem. We
know that whenever a differential equation is studied, we have to find its solution. Solution of a
differential equation (say in x and y) is the relation between x and y that satisfies the given
equation. This solution of differential equation may be in explicit form i.e. y = f(x) or in implicit
form F(x, y) = C. For example, y = Ae2x + Be3x is the solution of differential equation
y`` –5y` + 6y = 0 in explicit form, where A, B are some arbitrary constants. Readers are familiar
with the method of solving above differential equation. The methods that you have learnt in your
previous semesters are known as analytical methods. Unfortunately, analytical methods are
applicable to a selected class of differential equations giving a solution in explicit or implicit form
(closed forms). As most of the differential equations that appear in the field of engineering and
science are due to physical and natural phenomena, they do not belong to these classes and hence,
can not have closed solution. In such cases, we approximate a particular solution by finding the
numerical values of y1, y2, y3, … corresponding to x1, x2, x3, … so that an ordered pairs
(xi, yi), i = 1, 2, 3… satisfy approximately the particular solution. This type of solution is called
point wise solution.
Let us consider a first order differential equation dy/dx = f(x, y) along with an initial condition
y(x0) = y0. Let y = f(x) be the exact solution and y1, y2, y3, … be the approximate solutions at
x = x0, x1, x2, …Both solutions are shown in the
figure below.
The dotted curve shows the approximate/
numerical solution curve and the smooth curve
shows the exact solution curve of the differential
equation defined above. The difference between
them is termed as an error. There are many
numerical methods for the approximate solutions
to ordinary differential equations with initial
conditions. In this chapter the most important and frequently used methods are discussed.

-:83:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

Semi-Analytic And Semi-Numerical Methods


There are methods of solving initial value problems that are not fully numerical. As they involve
differentiation and integration, it seems that they are analytical in their nature. While using them
we have to go through the iterative process and moreover, they provide the approximate solution
to the given initial value problem, hence it seems that they are numerical methods as well. Thus
these methods are termed as “Semi-analytical and Semi-numerical” methods. In this section we
shall discuss TWO methods namely (i) Picard’s Method and (ii) Taylor’s Method which belong
to the above class.
Picard’s Method
Consider the first order equation
dy/dx = f(x, y) (1)
Subject to condition: y(x0) = y0
Integrating (1) between limits, we have
y x x

 dy   f (x, y)dx or y  y 0   f (x, y)dx (2)


y0 x0 x0
This is an integral equation equivalent to (1), for it contains the unknown y under the integral
sign. As a first approximation y1 to the solution, we put y = y0 in f(x, y) and integrate (2), giving
x
y1  y0   f (x, y0 )dx
x0

For a second approximation y2, we put y = y1 in f(x, y) and integrate (2), giving
x
y 2  y0   f (x, y1 )dx
x0
x
Similarly, a third approximation is y3  y 0   f (x, y2 )dx
x0
x
Continuing this process, we obtain nth approximation y n  y0   f (x, yn 1 )dx
x0
We observe that this method gives a sequence of approximations y1 , y 2 , y3 ,...y n each giving a
better result than the preceding one.
Example 01: Using Picard’s process of successive approximations, obtain a solution up to the
fifth approximation of the equation dy/dx = x + y, such that y = 1 when x = 0. Also solve the
equation analytically and compare the results. Find the exact & approximate values when y = 0.5.
Solution: Here f(x, y) = y + x, x0 = 0 and y0 = 1. Now Picard’s formula is
x
yn  1   (yn 1  x)dx
0
1st approximation: Put n = 1 and y0 = 1,
x x
x2
y1  1   (x  y0 )dx  1   (x  1)dx  1  x
0 0
2
2nd approximation: Put n = 2, and y1 = 1 + (x2 /2) + x,

-:84:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

x x
 x2  x3
y2  1    y1  x  dx  1   1  2x   dx 1  x  x 2 
 2  6
0 0 

rd x3
3 approximation: Put n = 2 and y 2  1  x  x 2  ,
6
x x
 x3  x3 x 4
y3  1   (x  y2 )dx  1   1  2x  x 2   dx  1  x  x 2  
 6  3 24
0 0 
x3
4th approximation: Put n = 3 and y 2  1  x  x 2 
6
x x
 x 3
x 
4
x3 x 4 x5
y4  1   (x  y3 )dx  1   1  2x  x 2    dx 1  x  x 2
  
 3 24 3 12 120
0 0  
3 4 5
th x x x
5 approximation: Put n = 4 and y 4  1  x  x 2    ,
3 12 120
x x
 x3 x 4 x5 
y5  1   (x  y 4 )dx  1   1  2x  x 2     dx
 3 24 120 
0 0 

x3 x 4 x5 x6
 y5  1  x  x 2     (1)
3 12 60 720
Analytical solution: Given differential equation may be written as:
y` – y = x
Which is a linear differential equation of the form y` + P(x) y = Q(x). It may be solved by using
an “Integrating Factor” I.F = e-x. The solution we get after using the initial conditions is:
y = 2 ex – x –1 (2)
x
Using the infinite series for e , we obtain:
or y = 2[1 + x + x2/2! + x3/3! + x4/4! + x5/5! +…] – x – 1
x3 x 4 x5 x 6
Simplifying, we get: y 1 x  x2     (3)
3 12 60 360
5
Comparing (1) and (3), we observe that both solutions are same up to the term in x . Now
substituting y = 0.5 in (1) and (2), we get: y5 = 3.434 and y = 3.44. For better accuracy, more
terms are required. Students are advised to work to seven iterations and the find the values of y
and y7 at x = 0.5 and compare the results.
Although Picard’s method is straight forward to be used for solving given initial value problem
but as it involves evaluation of integration, it may happen that at some point of computation, the
integrand becomes complex to be evaluated. In this case we may not be able to continue further
computation. Readers are advised to solve the initial value problem y` = (y – x)/(y + x), y(0) = 1
and convince themselves the difficulties in using Picard’s method. They will find that second
iteration would involve an integrand that cannot be evaluated analytically.
Taylor’s Series Method
Given an initial value problem dy / dx  y'(x)  f (x, y) , y (x0) = y0. The solution curve y(x) can
be expressed in a Taylor Series around x = x0 as:
(x  x 0 )2 (x  x 0 )3
y(x)  y0  (x  x 0 )y  y  y  (1)
2! 3!
-:85:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

We have to truncate the series at some term to get approximate value of y for any x.
Example 02: Solve y  x 2 y  1, y  0  1, and find y at x = 0.2, using Taylor Series.
Solution: y` = x2 y – 1 or y`(0) = –1
2
y`` = 2xy + x y` or y``(0) = 0
2
y``` = 2y + 4 xy` + x y`` or y```(0) = 2
2
yiv(0) = –6
iv
y = 6y + 6 xy`` + x y``` or
Now substituting the values of the derivatives and x0 = 0, y0 = 1 in (1), we get:
x2 x3 x 4 iv
y(x)  1  xy(0)  y(0)  y(0)  y (0)...
2! 3! 4!
x3 x 4
y(x)  1  x   or y(0.2) = 1 – 0.2 + (0.2)3/3! – (0.2)4/4! ~ 0.80227
3 4
Numerical Methods
The difficulties in using Picard’s and Taylor’s methods are apparent as they involve integration
and differentiation. If the function f(x, y) is cumbersome both process fail to provide the solution
of initial value problem. In this section we shall discuss the numerical methods to find the
approximate solution of an initial value problem: y` = f(x, y), y(x0) = y0
Euler’s Method
Euler’s method perhaps is the first fully numerical method ever used and developed before any
other method. Before we discuss its limitations, let us first derive it and show how simple it is to
be used.
Derivation: Consider an initial value problem dy/dx = f(x, y), y(x0) = y0. By Taylor’s series,
dy (x  x 0 )2 d 2 y (x  x 0 )3 d3 y
y(x)  y0  (x  x 0 )   (1)
dx 2! dx 2 3! dx 3
Let h = x – x0 be so small that it’s higher powers h2, h3, …are negligible, in which case equation
(1) becomes:
y(x) = y0 + h f(x, y), where f(x, y) = dy/dx (2)
Now define an iterative scheme: yn+1 = yn + h f(xn, yn) (3)
This is known as Euler’s formula for solving numerically initial value problem as mentioned
above.
Derivation of Euler Formula Using Geometrical
Phenomena
Consider a curve f(x, y) = c as shown below. Let xn
and xn+1 be two points on the x-axis. Then the
ordinates at x = xn and x = xn+1 are |MP| = yn and |NQ|
= yn+1 respectively. Join PQ. Then triangle PKQ is a
right triangle. The chord PQ approximates the true
solution say F(x, y) = C provided h = |MN| is too small. Now from the figure we see that:
yn 1  yn yn 1  yn  dy 
tan    . But tan     f (x n , yn )
x n 1  x n h  dx  (x n ,yn )

-:86:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

yn 1  yn
Hence,  f (x n , yn )  yn 1  yn  h f (x n , yn ) . This is same as (3).
h
Example 01: Solve y` = f(x, y) = x + y, y(0) = 1, find y at x = 0.3 by using Euler’s Methods with
h = 0.1
Solution: Here x0 = 0, y0 = 1 and f(x, y) = x + y. Now we know that xn = x0 + nh
For n = 1, x1  0  (1)(0.1)  0.1 , for n=2 x 2  0  (2)(0.1)  0.2
and for n = 3, x 3  0.3 . Using Euler’s formula, we get:
yn+1 = yn + h(xn + yn) (1)
Putting n = 0 in (1), we get
y1  y(x1 )  y0  h(x 0  y 0 )  1  (0.1)(0  1)  1.1
Putting n = 1, y 2  y1  hf (x1 , y1 )  1.1  (0.1)(0.1  1.1)  1.22
Putting n = 2, y3  y 2  hf (x 2 , y 2 )  1.22  (0.1)(0.2  1.22)  1.362
Hence the solution of given initial value problems at x = 0.3 is y = 1.362.
The exact solution is y = 2ex – x – 1 so that when x = 0.3, y = 2e0.3 – 0.3 – 1 = 1.399. You may
observe that Euler’s solution is having an error of 0.037 to an exact solution.
COMPUTER PROGRAM
#include<constream.h>
#define f(x,y)(x+y)
void main()
{
clrscr();
float x[10],y[10],h,xn,n;
char option;
cout<<"Enter the initial condition (xo, yo) : ";
cin>> x[0] >> y[0];
cout <<"\nEnter the nth value of xn: ";
cin>> xn;
cout<< "\nEnter the step size value: ";
cin>> h;
n = (xn - x[0])/h;
cout<< "\n\n------------------------";
cout<<" \nEULER's METHOD\n";
cout<<"----------------------------";
for(int i=1;i< = n;i++)
{
x[i] = x[0]+ i*h;
y[i]=y[i-1] + h*f(x[i-1],y[i-1]);
cout<< "\n\nx["<<i<<"] = "<<x[i]<<"\ty["<<i<<"] = "<<y[i]<<endl;
}
getch();
}

-:87:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

OUTPUT
Enter the initial condition (xo, yo) : 0 1
Enter the nth value of xn: 0.3
Enter the step size value: 0.1
EULER's METHOD
x[1] = 0.1 y[1] = 1.1
x[2] = 0.2 y[2] = 1.22
x[3] = 0.3 y[3] = 1.362
Remark: While using the above program students are advised to change line three where an
appropriate value of f(x, y) is to be incorporated.
We have mentioned above that Euler’s Method is very easy to use but it has a drawback that if h
is not small the results will be inaccurate. Hence this method is virtually rarely used in practice.
However, because of its simplicity, it is convenient to use it as introduction to numerical
techniques for solving ordinary differential equations.
From the derivation of Euler’s method by using Taylor’s series we have truncated the series after
second term. As the second term involves h, hence the error in each step of Euler’s method is
expected to be of order O(h).
Example 02: A bungee jumper with a mass of 68.1kg leaps from a stationary hot air balloon. Use
dvi
vi1  vi  t, to compute velocity with Euler’s method for the first 12 seconds of free fall.
dt
Also determine the terminal velocity that will be attained for an infinitely long card
(or alternatively, the Jumper master is having a particularly bad day!). Use a drag coefficient of
0.25kg/m. Employ a step size of 2 seconds for the calculation.
Note: Take dvi /dt = g – (c d vi2)/m
 cd 
Solution: vi1  vi   g  vi 2  t
 m 
where at the start of the computation t0 = 0, the velocity of the jumper is zero i.e. v0 = 0, g = 9.8
2
m/s , cd = 0.25kg/m, m = 68.1 kg and t  2  h .
For the First interval (from time 0 to 2 seconds), at i = 0, we have
 cd   0.25 2 
v1  v0   g  v02  t  0   9.8   0   2  19.62m / s
 m   68.1 
For the Second interval (from time 2 to 4 seconds), the computation is repeated, with the result at
i =1, we have
  
19.62 2   2  36.4137m / s
cd 0.25
v2  v1   g  v12  t  19.62   9.8 
 m   68.1 
Continuing the calculations in a similar way, we obtain the following results.
t i  sec  0 2 4 6 8 10 12 
vi  m / s  0 19.6200 36.4137 46.2983 50.1802 51.3123 51.6008 51.6938

The results are plotted in figure shown below along with the exact solution. We can see that the
numerical method captures the essential features of the exact solution. However, because we have

-:88:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

employed straight line segments to approximate a continuously curving function, there is some
discrepancy between the two results.

Comparison of the numerical and analytical solutions for the bungee jumper problem
To reduce the inherent error in the simple Euler’s Method, two variations of Euler’s method are
commonly used. They are:
Modified Euler’s Method
This method is based on the formula:
 h h 
yn 1  y(x n 1 )  yn  hf  x n  , yn  f  x n , yn  
 2 2 
The derivation of this formula may be found in any “Advance Numerical Analysis Book”. This
formula is used in coming examples.

COMPUTER PROGRAM
#include<constream.h>
#define f(x,y)(x+y)
void main()
{ clrscr();
float x[10],y[10],h,xn,n;
char option;
cout<<"Enter the initial condition (xo, yo) : ";
cin>>x[0]>>y[0];
cout<<"\nEnter the nth value of xn: ";
cin>>xn;
cout<<"\nEnter the step size value: ";
cin>>h;
n=(xn-x[0])/h;
cout<<"\n\n----------------------------------";
cout<<"\nMODIFIED EULER's METHOD\n";
cout<<"--------------------------------------";

-:89:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

for(int i=1;i<=n;i++)
{ x[i]=x[0]+i*h;
y[i]=y[i-1]+h*f(x[i-1]+(h/2),y[i-1]+(h/2)*f(x[i-1],y[i-1]));
cout<<"\n\nx["<<i<<"] = "<<x[i]<<"\ty["<<i<<"] = "<<y[i]<<endl;
}
getch();
}
OUTPUT
Enter the initial condition (xo, yo) : 0 1
Enter the nth value of xn: 0.3
Enter the step size value: 0.1
MODIFIED EULER's METHOD
x[1] = 0.1 y[1] = 1.11
x[2] = 0.2 y[2] = 1.24205
x[3] = 0.3 y[3] = 1.398465
Improved Euler’s Method (Heun’s Method)
This method is based on the formula:
h
 f (x n , yn )  f (x n  h, yn  hf (x n , yn )) 
yn 1  y(x n 1 )  yn 
2
This formula is also known as Heun’s Method for solving initial value problem..
Both methods give a definite improvement in accuracy. A major drawback of Euler’s Methods as
mentioned earlier is that the order of accuracy is low.
Example 02: Use modified Euler’s method (Heun’s method) solve the initial value problem y`
f(x, y) = x + y, y(0) = 1. Find y at x = 0.3 taking h = 0.1
Solution: Putting n = 0, the modified Euler’s formula becomes
 h h 
y1  y(x1 )  y0  hf  x 0  , y0  f  x 0 , y0   where f (x, y)  x  y (1)
 2 2 
Now substituting h = 0.1, x0 = 0 and y0 = 1 in (1), we get
 0.1 0.1 
y1  y(0.1)  1  (0.1)f  0  ,1   0  1   1  (0.1)f  0.05,1.05  1  (0.1)(0.05  1.05)  1.11
 2 2 
 h h 
Putting n =1, we get : y2  y(x 2 )  y1  hf  x1  , y1  f  x1, y1  
 2 2 
Substituting x1 = 0.1, h = 0.1 and y1 = 1.11, we obtain

 0.1  1.11   1.11  (0.1)f  0.15,1.1705
0.1 0.1
y2  y(0.2)  1.11  (0.1)f  0.1  ,1.11 
 2 2 
 1.11  (0.1)  0.15  1.1705  1.24205
 h h 
Putting n = 2, we get: y3  y(x 3 )  y2  hf  x 2  , y 2  f  x 2 , y 2  
 2 2 
Substituting x2 = 0.2, h = 0.1 and y2 = 1.24205, we obtain

 0.2  1.24205 
0.1 0.1
y3  y(0.3)  1.24205  (0.1)f  0.2  ,1.24205 
 2 2 
 1.24205  (0.1)f (0.01,1.3141525)  y3  1.24205  (0.1)  0.01  1.341525  1.3772025

-:90:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

Comparing the results by Euler and modified Euler methods, we see that
By Euler’s method: y3 = 1.362 .
By modified Euler’s method: y3 = 1.3772025

Remark: Students are advised to solve above initial value problem using “Improved Euler’s
Method” and also write its programming coding.
Runge-Kutta Methods
The R. K. methods are a family of methods derived from the Taylor Series. We know that
Taylor’s series involve higher order derivatives hence is a lengthy process. The Runge-Kutta
methods give greater accuracy without calculating the higher order derivatives. These methods
agree with Taylor’s Series solution up to the term hn, where n differs from the method to method
and is known as the order of that methods. For example, as it will be shown shortly that Euler’s
Method, Modified Euler’s Method and Improved Euler’s methods are Runge-Kutta Methods of
first, second and third order respectively. The Fourth order Runge-Kutta Method is most
commonly used and is often referred to as Runge-Kutta Method only.
(I) RK Method of First Order
By definition y1 = y(x0 + h). Expanding by Taylor’s series, we obtain:
h 2 '' h 3 '''
y1  y0  hy0' y0  y0 
2! 3!
If h2 and higher powers are neglected, we obtain:
y1 = y0 + h y`0 = y0 + h f(x0, y0) [Note: y`0 = f(x0, y0)]
This is the Euler’s Formula. Hence RK-Method of first order is an Euler’s formula. When this
formula is recursively applied it may be written as:
y n 1  yn  k1 , where k1  hf (x n , y n )
(II) RK Method of Second Order
Algorithm: Compute y n 1  y n  k , for n = 1, 2, 3,
Here k  (k1  k 2 ) / 2 and k1  hf (x n , yn ), k 2  hf  x n  h, yn  k1 
This in fact is an “Improved Euler’s Method” or “Huen’s Method”.
(III) RK Method of Third Order
Algorithm: Compute y n 1  y n  k , where k  (k1  4k 4  k 3 ) / 6 . Here,
k1  hf (x n , yn ), k 2  hf  x n  h, yn  k1  , k 3  hf  x n  h, y n  k 2  & k 4  hf  x n  h / 2, y n  k1 / 2 
(IV) RK Method of Forth Order
Algorithm: Compute yn 1  yn  k where k  [k1  2  k 2  k 3   k 4 ] / 6 and
k1  hf (x n , y n ), k 2  hf  x n  h / 2, y n  k1 / 2  ,
k 3  hf  x n  h / 2, y n  k 2 / 2  , k 4  hf  x n  h, y n  k 3 
Remark: The derivations of these formulae may be found in any book on “Advance Numerical
Analysis”.
Example 03: Given y  x 2  y, y(0)  1, find y(0.1), y(0.2), using analytical and
(i) Second order RK- Method (ii) Third order RK- Method (iii) (iii) Fourth order RK- Method

-:91:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

Comparing the results and enlighten which one these numerical method is accurate and near to
the exact solution.
Solution: (I) Exact Solution by Analytical Method
The given equation may be written as y` + y = x2. This is a linear differential equation of the
form: y` + P(x) y = Q(x)

To find its solution, we find an “Integrating Factor”: I.F  e   e


p(x) d x 1d x
 ex .
Multiplying given equation by an I.F, we obtain:
d  x
ex[y` + y] = x2ex or ye  x 2ex . Integrating both sides, we get:
dx  
y ex = ∫ x2 ex dx = ex[x2 – 2x + 2] + C. Dividing both sides by ex, we obtain
y = (x2 – 2x + 2) + C e-x (1)
This is the general solution. To find a particular solution, we use the condition y(0) = 1, that is,
put x = 0 and y = 1, we get: 1 = 2 + C or C = – 1
2 -x
Put this value of C in equation (1), we get: y = (x – 2x + 2) – e (2)
Equation (2 is the particular solution. Now:
If x = 0.1 then y = 0.90516 and if x = 0.2 then y = 0.821269
nd
(II) Solution by RK Method of 2 Order
Given y` = x2 – y or f(x, y) = x2 – y, x0 = 1 and y0 = 1
Algorithm: Compute y n 1  y n  k , n = 0, 1
Where k  (k1  k 2 ) / 2 and k1  hf (x n , yn ), k 2  hf  x n  h, yn  k1 
Substituting n = 0, we get: y1 = y0 + h k, where k = (k1 + k2)/2. Now,
. k1 = h f(x0, y0) = (0.1) f(0, 1) = (0.1)(02 – 1) = –0.1
. k2 = h f(x0 + h, y0 + k1) = (0.1) f(0 + 0.1, 1 – 0.1) = (0.1) f(0.1, 0.9) = 0.1[(0.1)2 – 0.9] = –0.089
Thus, k = (k1 + k2)/2 = (–0.1 – 0.089)/2 = –0.0945
Now, y1 = y0 + k = 1 –0.0945 = 0.9055. This is the solution at x = 0.1
Now substituting n = 1, we get: y2 = y1 + k, where k = (k1 + k2)/2. Now,
. k1 = h f(x1, y1) = 0.1 f(0.1, 0.9055) = 0.1 (0.12 – 0.9055) = – 0.08955
2
. k2 = h f(x1 + h, y1 + k1) = 0.1 f(0.1 + 0.1, 0.9055 – 0.08955) = 0.1[(0.2) – 0.81595] = –0.077595
Now, k = (k1 + k2)/2 = –0.0835725
Thus, y2 = y1 + k = 0.9055 – 0.0835725 = 0.8219275. This is the solution at x = 0.2
(III) Solution by RK Method of 3rd Order
Algorithm: Compute y n 1  y n  k where, k  (k1  4k 4  k 3 ) / 6 [Note the value of k]
k1  hf (x n , yn ), k 2  hf  x n  h, yn  k1  , k 3  hf  x n  h, y n  k 2  & k 4  hf  x n  h / 2, y n  k1 / 2 
Substituting n = 0, we get y1  y0  k where k  (k1  4k 2  k 3 ) / 6. Here,

 
k1  hf (x 0 , y0 )  (0.1)f (0,1)  (0.1) (0) 2  1  0.1,
k 2  hf  x 0  h, y0  k1   (0.1)f  0.1,0.99   0.089
k 3  hf  x 0  h, y0  k 2   (0.1)f  0.1,0.911  0.0901
k 4  hf (x 0  h / 2, y0  k1 / 2)  (0.1)f (0.05,0.95)  0.09475

-:92:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

Now, k = (k1 + 4k4 + k3)/6 = –0.09485


Hence, y1 = y0 + k = 1 – 0.09485 = 0.90515. This is the solution at x = 0.1
Substituting n = 1, we get y 2  y1  k where k  (k1  4k 2  k 3 ) / 6. Now,
k1  hf (x1 , y1 )  (0.1)f (0.1,0.90515)  0.089515
k 2  hf  x1  h, y1  k1   (0.1)f  0.2,0.815635   0.0775635
k 3  hf  x1  h, y1  k 2   (0.1)f  0.2,0.8275865   0.0787586
k 4  hf (x1  h / 2, y1  k1 / 2)  (0.1)f (0.15,0.8603925)  0.0837892
But, k = (k1 + 4k4 + k3)/6 = –0.0839051
Hence, y2 = y1 + k = 0.90515 – 0.0839051 = 0.8212449. This is the solution at x = 0.2.
(IV) Solution by RK Method of 4th Order
Algorithm: yn+1 = yn + k, where k  [k1  2  k 2  k 3   k 4 ] / 6
k1  hf (x n , y n ), k 2  hf  x n  h / 2, y n  k1 / 2  ,
k 3  hf  x n  h / 2, y n  k 2 / 2  , k 4  hf  x n  h, y n  k 3 
Substituting n = 0: y1  y 0  k, where k  [k1  2  k 2  k 3   k 4 ] / 6


Now, k1  hf (x 0 , y0 )  (0.1) (0) 2  1  0.1 
 h k   0.1 0.1 
k 2  hf  x 0  , y0  1   (0.1)f  0  ,1    (0.1)f  0.05,0.95 
 2 2  2 2 

 
 (0.1) (0.05) 2  0.95  0.09475

 h k   0.1 0.09475 
k 3  hf  x 0  , y 0  2   (0.1)f  0  2 ,1  
 2 2   2 

 (0.1)f  0.05,0.952625   (0.1) (0.05) 2  0.952625  0.0950125 
k 4  hf  x 0  h, y 0  k 3   (0.1)f  0  0.1,1  0.0950125 


 (0.1)f (0.1,0.9049875)  (0.1) (0.1) 2  0.9049875  0.0894987 
 k  [0.1  2  0.09475  0.0950125   0.0894987] / 6  0.0948372
Thus, y1  1  (0.0.0948372)  0.9051627 . This is the solution at x = 0.1
Now substituting n = 1: y2  y1  k, where k  [k1  2  k 2  k 3   k 4 ] / 6
Now, k1  hf (x1 , y1 )  (0.1)(0.1  0.9051627)  0.0805162
 h k   0.1 0.0805162 
k 2  hf  x1  , y1  1   (0.1)f  0.1  ,0.9051627  
 2 2  2 2 

 (0.1)f  0.15,0.8649045  (0.1) (0.05)2  0.8604046  0.0837904 
 h k   0.1 0.09475 
k 3  hf  x1  , y1  2   (0.1)  0  2 ,1  
 2 2   2 

 (0.1)f  0.15,0.8632674   (0.1) (0.15)2  0.8632674  0.0840767 
k 4  hf  x1  h, y1  k 3   (0.1)f  0.2,0.8210859   0.0781085
 k  [0.0895162  2(0.0837904  2(0.0840767  0.0781085]  0.0838931

Thus, y2 = y1 + k = 0.9051627 – 0.0838931 = 0.8212696. This is the solution at x = 0.2

-:93:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

Method x = 0.1 x = 0.2


Analytical Method 0.90516 0.821269
RK Method of 2nd Order 0.9055 0.8219275
RK Method of 3rd Order 0.90515 0.8212449
RK Method of 4th Order 0.9051627 0.8212696

From the above table one can see that RK Method of 4th order is near to exact solution.
COMPUTER PROGRAM
#include<constream.h>
#define f(x,y)((x*x - y)
void main()
{ clrscr();
float x[10],float y[10],float h,xn,n,k1,k2,k3,k4;
cout<<"\nEnter the given x0 and y0 values : "; cin>>x[0]>>y[0];
cout<<"\nEnter the step size value (h) : "; cin>>h;
cout<<"\nEnter the value of xn where find y "; cin>>xn;
n=(xn-x[0])/h;
for(int i=1;i<=n;i++)
x[i]=x[0]+i*h;
int choice;
cout<<"\n..............RUNGE KUTTA METHODS...............\n";
cout<<"\n FOR 2ND ORDER RUNGE KUTTA METHOD...... 1";
cout<<"\n FOR 3RD ORDER RUNGE KUTTA METHOD...... 2";
cout<<"\n FOR 4TH ORDER RUNGE KUTTA METHOD...... 3\n";
cout<<"\nSelect your required method ";
cin>>choice;
switch(choice)
{ case 1:
for(i=1;i<=n;i++)
{ k1=h*f(x[i-1],y[i-1]);
k2=h*f(x[i-1]+h,y[i-1]+k1);
y[i]=y[i-1]+(k1+k2)/2;
cout<<"\ny["<<i<<"] = "<<y[i]<<" at x["<<i<<"] = "<<x[i]<<"\n";
}break;
case 2:
for(i=1;i<=n;i++)
{ k1=h*f(x[i-1],y[i-1]);
k2=h*f(x[i-1]+h/2,y[i-1]+k1/2);
k3=h*f(x[i-1]+h,y[i-1]-k1+2*k2);
y[i]=y[i-1]+(k1+4*k2+k3)/6;
cout<<"\ny["<<i<<"] = "<<y[i]<<" at x["<<i<<"] = "<<x[i]<<"\n";
}break;
case 3:

-:94:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

for(i=1;i<=n;i++)
{ k1=h*f(x[i-1],y[i-1]);
k2=h*f(x[i-1]+h/2,y[i-1]+k1/2);
k3=h*f(x[i-1]+h/2,y[i-1]+k2/2);
k4=h*f(x[i-1]+h,y[i-1]+k3);
y[i]=y[i-1]+(k1+2*(k2+k3)+k4)/6;
cout<<"\ny["<<i<<"] = "<<y[i]<<" at x["<<i<<"] = "<<x[i]<<"\n";
}break;
default: cout<<"\n U have wrong choice";
}
getch();
}
OUTPUT
Enter the given x0 and y0 values: 1, 1
Enter the step size value (h): 0.1
Enter the value of xn where find y 0.2
..............RUNGE KUTTA METHODS...............
FOR 2ND ORDER RUNGE KUTTA METHOD...... 1
FOR 3RD ORDER RUNGE KUTTA METHOD...... 2
FOR 4TH ORDER RUNGE KUTTA METHOD...... 3
Select your required method 3
y[1] = 0.9051627 at x[1] = 0.1
y[2] = 0.8212696 at x[2] = 0.2
Remark: The necessary changes may be made in line # 02 where the function’s f(x, y) value is to
be inserted.
Predictor Corrector Methods
In the previous sections we studied numerical methods for solving an initial value problem:
dy/dx = f(x, y) subject to condition y(x0) = y0
This initial value problem was solved over an interval say [xi, xi+1] where only the value of y at
the beginning of the interval was required. These methods are not very much perfect if high class
of accuracy is needed.
In Predictor-Corrector methods, four prior values are required for finding the value of y at
x = xi+1. A predictor is used to guess the value of y at xi+1 and then a corrector is applied to
improve this value. In this section, we shall discuss only TWO such methods. They are:
(i) Milne’s Method
(ii) Adams Bash-forth Method
Milne’s Method
Given an initial value problem: dy/dx = f(x, y), y(x0) = y0
i. Compute: y1, y2 and y3 using Euler’s, method or Picard’s method or Taylor’s series
method whichever is convenient looking at the behavior of the function f(x, y).
ii. Compute f0 = f(x0, y0), f1 = f(x1, y1), f2 = f(x2, y2) and f3 = f(x3, y3)

-:95:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

iii. Now compute y4 = y(x4) = y(x0 + 4h) using Newton’s forward interpolation formula,
n(n  1) 2 n(n  1)(n  2) 3
f (x, y)  f 0  nf 0   f0   f 0  ... in the relation :
2 6
x 0  4h x 0  4h
 n(n  1) 2 n(n  1)(n  2) 3 
y 4  y0   f (x, y)dx   f 0  nf 0 
 2
 f0 
6
 f 0  ... dx

x0 x0

Substituting x = x0 + nh or dx = h dn. If x = x0, then n = 0 and if x = x0 + 4h, then


n = 4. Thus
4
 n(n  1) 2 n(n  1)(n  2) 3 
y 4  y0  h  f 0  nf 0   f0   f 0  ... dn
0 
2 6
 20 2 8 
 y0  h  4f 0  8f 0   f 0   3f 0  , neglecting higher differences.
 3 3 
2 3
Now Δf0 = (f1 – f0), Δ f0 = (f2 –2f1 + f0) and Δ f0 = (f3 –3f2 + 3f1 + f0)
iv. Substituting these in the above equation and simplifying, we get:
4h
y 4  y0 
3
2f1  f2  2f3  . This is called a “Predictor” (1)
v. Now find f4 = f(x4, y4)
vi. A better value of y4 is found by using “Simpson’s Rule” as
h
y4  y2 
3
f2  4f3  f4  . This is called a “Corrector” (2)
vii. Now an improved value of f4 is computed, that is, find f4 = f(x4, y4) using equation
(2) for the value of y4.
viii. Now use the corrector to find a still better value of y4
ix. Repeat this process until y4 is settled down, that is, difference between two
consecutive values of y4 is same.
x. Once y4 and f4 are obtained to desired accuracy, y5 is found from the “Predictor”
4h
as: y5  y1   2f 2  f3  2f 4  . Then calculate f5 = f(x5, y5).
3
xi. Now use the “Corrector” to find a better approximate value of y5 using:
h
y5  y3  f3  4f 4  f5 
3
xii. Repeat steps (iv) to (xi) until y5 becomes stationary and then we proceed to find y6 as
discussed above.
The algorithm defined above is due to Milne’s and is called “Milne’s Predictor-Corrector”
method. In order to have a better accuracy, we must ensure that starting values must be accurate
and then subdivide the intervals [xi, xi+1]. It may be noted that “Predicator-Corrector Methods”
are also called Multi-Step Methods, and are not self-starting as they require three additional
values of y. They require four initial points, (x0, y0), (x1, y1), (x2, y2), (x3, y3), in order to generate
the point (x4, y4).
The basic principle behind the multi-step method is to utilize four prior values of y to construct a
polynomial that approximates the derivatives of the function f (x, y) and to extrapolate this into
the next interval.

-:96:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

If we use four prior points, the approximating polynomial will be cubic. The more points we use
the higher is the order of the approximating polynomial and the better is the accuracy. In these
methods, the accuracy is controlled by the corrector formula whereas the predicator formula
simply helps to provide an initial approximation.
Example 01: Use Milne’s method to find a solution of the differential equation y` = x – y2 in the
range 0 ≤ x ≤ 1 using y(0) = 0 with h = 0.2, find y at x = 1.0.
Solution: We use Picard’s method to compute three initial values y1, y2 and y3
x x
x2
1st approximation: y1  y0   f (x, y0 )dx  0   (x  0 )dx 
2

x0 0
2
2nd approximation:
x x x
x 2 x5
y 2  y0   f (x, y1)dx  0   (x  y 1)dx   (x  x / 4)dx   Note : y1  x 2 / 2
2 4

x0 0 0
2 20
rd
3 approximation:
x
 x 2 x5  
x x 2
x 2 x 5 x8 x11
y3   f (x, y 2 )dx   (x  y 2 )dx    x  
2
   dx    
0
  2 20   2 20 160 4400
x0 0 
Now we are ready to compute the starting values needed for employing the Milne’s method.
Taking x0 = 0 and h = 0.2 and f(x, y) = x – y2, we have:
x0 = 0.0, y0 = 0.000 f0 = f(x0, y0) = f(0, 0) = 0.0000
x1 = 0.2, y1 = 0.0200 f1 = f(x1, y1) = f(0.2, 0.02) = 0.1996
x2 = 0.4, y2 = 0.0795 f2 = f(x2, y2) = f(0.4, 0.0795) = 0.3937
x3 = 0.6, y3 = 0.1762 f3 = f(x3, y3) = f(0.6, 0.1762) = 0.5689
Remark: The values of y1, y2 and y3 are computed by substituting x = 0.2, 0.4, and 0.6
respectively in the above approximations obtained from Picard’s method.
Using the “Predictor” :
4h
y 4  y0 
3
2f1  f2  2f3  = 0 + 4(0.2)[2(.1996) – 0.3937 + 2(0.5689)]/3 = 0.3049
2
or f4 = f(x4, y4) = f(0.8, 0.3049) = 0.8 – (0.3049) = 0.7070.
Now using the “Corrector” for y4:
h
y4  y2 
3
f2  4f3  f4  = 0.3046  f4 = f(x4, y4) = f(0.8, 0.3046) = 0.7072 (1)

h
Again using the “Corrector”, y4  y2  f 2  4f3  f 4  = 0.3046
3
[Note: Use f4 = 0.7072]. This value of y4 is same as found in equation (1).
4h
Now using the “Predictor”: y5  y1   2f 2  f3  2f 4  = 0.4554
3
or f5 = f(x5, y5) = f(1, 0.4554) = 0.7926 (2)
h
Using the “Corrector”, y5  y3  f3  4f 4  f5  = 0.4555 and f5 = 0.7925 (3)
3
Using the “Corrector” again, we get: y5 = 0.4555 which is same as in (3). From this we conclude
that y(1) = 0.4555.

-:97:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

Adams-Bash forth Method


Given an initial value problem: dy/dx = f(x, y), y(x0) = y0
i. Compute: y-1= y(x0 – h), y-2 = y(x0 – 2h) and y-3 = y(x0 – 3h) using Euler’s, method or
Picard’s method or Taylor’s series method whichever is convenient looking at the
behavior of the function f(x, y).
ii. Compute f-1 = f(x0 – h , y-1), f-2 = f(x0 – 2h, y-2), f-3 = f(x0 – 3h, y-3)
iii. Now compute y1 = y(x1) = y(x0 + h) using Newton’s backward interpolation formula,
n(n  1) 2 n(n  1)(n  2) 3
f (x, y)  f0  nf0   f0   f0  ...
2 6
x0 h
in the relation : y1  y0   f (x, y)dx (1)
x0
x1
 n(n  1) 2 n(n  1)(n  2) 3 
 y1  y0   f0  nf0  2
 f0 
6
 f0  ... dx

x0
Substituting x = x0 + nh or dx = h dn. If x = x0, then n = 0 and if
x = x1= x0 + h, then n = 1. Thus,
1
 n(n  1) 2 n(n  1)(n  2) 3 
y1  y0  h  f0  nf0   f0   f0  ... dn
0 
2 6

 1 15 3 
 y0  h f0  f0  2f0  3f0  , (neglecting higher differences)
 2 12 8 
Now  f0 = (f0 – f-1),  f0 = (f0 –2f-1 + f-2) and  3f0 = (f0 –3f-1 + 3f-2 + f-3)
2

iv. Substituting these in the above equation and simplifying, we get:


h
y1  y0 
24
55f0  59f1  37f2  9f3  (2)

This is called a “Adam-Bash-forth Predictor” formula.


v. Now find f1 = f(x1, y1)
vi. A better value of y1 is found by substituting Newton’s Backward interpolation
formula for f(x, y) obtained at f1, that is,
 n(n  1) 2 n(n  1)(n  2) 3 
f (x, y)  f1  nf1   f1   f1  ...
 2 6 
in equation (1) above to get:
x1
 n(n  1) 2 n(n  1)(n  2) 3 
y1  y0   f1  nf1  2
 f1 
6
 f1  ... dx

x0
Putting x  x1  nh,dx  h dn and changing the limits, we get :
0
 n(n  1) 2 n(n  1)(n  2) 3 
y1  y0  h  f1  nf1   f1   f1  ... dn
1  
2 6
 1 1 1 
 y0  h  f1  f1   2 f1  3f1  ... . Neglecting the higher order
 2 12 24 
differences and putting the values of f1 ,  2 f1 , 3f1 , we get :
y1  y0   h / 24   9f1  19f0  5f 1  f 2  (3)

-:98:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

Equation (3) is called "Corrector" formula


vii. Now an improved value of f1 is computed, that is, find f1 = f(x1, y1)
viii. Now use the corrector (3) to find a still better value of y1
ix. Repeat this process until y1 is settled down, that is, difference between two
consecutive values of y1 is same.
x. Once y1 and f1 are obtained to desired accuracy, we calculate y2 as above.
The algorithm defined above is due to Adam-Bash-forth and is called “Adam-Bashforth
Predictor-Corrector” method. In order to have a better accuracy, we must ensure that starting
values must be accurate.
2
Example 02: Solve initial value problem dy/dx = x (1 + y), y(1) = 1 using Adams-Bash-forth
method. Given that y(1.1) = 1.233, y(1.2) = 1.548, y(1.3) = 1.979, compute y(1.4).
2
Solution: Here f(x, y) = x (1 + y), h = 0.1. Observe that we are given one initial condition at
x = 1 and three conditions at x = 1.1, 1.2, and 1.3.
The least value of x = 1, hence we consider first value of y at this point as y -3. The different
values of y for different x are to be taken as shown below:
2
At x = 1.0, y-3 = 1.000 then f -3 = (1) (1 + 1.000) = 2.000
2
At x = 1.1, y-2 = 1.233 then f -2 = (1.1) (1 + 1.233) = 2.702
At x = 1.2, y-1 = 1.548 then f -1 = (1.2)2 (1 + 1.548) = 3.669
At x = 1.3, y0 = 1.979 then f 0 = (1.3)2 (1 + 1.979) = 5.035
h
Now using the Predictor: y1  y0 
24
55f0  59f1  37f2  9f3 
At x = x1 = 1.4, y1 = 2.573, or f1 = f(x1, y1) = f(1.4, 2.573) = (1.4)2 (1 + 2.573) = 7.004
(y1 is obtained by placing the values of f0, f-1, f-2 and f-3 in the above equation).
h
Using the Corrector: y1  y0  9f1  19f0  5f1  f2   2.575
24
To get more accurate value till y becomes stationary, we compute:
f1 = f(x1, y1) = f(1.4, 2.575) = (1.4)2[1 + 2.575) = 7.007
Using the Corrector again with new value of f1, we get,
y1 = 1.979 + 0.1[9(7.007) + 19(5.035) – 5(3.669) + 2.702]/24 = 2.575
This is same as above. Hence y(1.4) = 2.575.
Second Order Differential Equations
In the above sections we tried to study the numerical solutions of first order differential equations.
In this section, we produce a method to solve second order differential equation.
We know that the most general form of differential equation of second order is:
d2 y  dy 
 f  x, y, 
2
(1)
dx  dx 
If we write dy/dx = z then d2y/dx2 = dz/dx then equation (1) becomes:
dz
 f  x, y,z  (2)
dx
Equation (2) along with the equation dy / dx  z (3)

-:99:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

may be solved simultaneously as described in the methods presented in the above sections. The
method of solving second order differential equation is presented in the following example.
Readers are advised to see the steps taken during the calculation.
Example 01: Solve the differential equation y`` = x y`2 – y2 for x = 0.2 correct to four decimal
places given that y(0) = 1 and y`(0) = 0.
2 2
Solution: Let dy/dx = z, then given equation becomes z` = xz – y . We also have x0 = 0, y0 = 1
and y`0 = z0 = 0. Thus we have simultaneous equations:
dy / dx  z and dz / dx  xz 2  y2 along with x 0  0, y0  1,z0  0
We denote f (x, y,z)  z and (x, y,z)  xz 2  y2
We also denote k1, k2,… for f(x, y, z) and m1, m2, … for φ(x, y, z). Now,
k1 = h f(x0, y0, z0) : m1 = h φ(x0, y0, z0)
= 0.2(0) = 0 : = 0.2(–1) = – 0.2
k2 = h f(x0 + h/2, y0 + k1/2, z0 + m1/2) : m2 = h φ(x0 + h/2, y0 + k1/2, z0 + m1/2)
= – 0.02 : = – 0.1998
k3 = h f(x0 + h/2, y0 + k2/2, z0+ m2/2) : m3 = h φ(x0 + h/2, y0 + k2/2, z0 + m2/2)
= – 0.02 : = – 0.1958
k4 = h f(x0 + h, y0 + k3, z0 + m3) : m4 = h φ(x0 + h, y0 + k3, z0 + m3)
= – 0.0392 : = – 0.1905
Now, k = (k1 + 2k2 + 2k3 + k4)/6 = – 0.0199 and m = (m1 + 2m2 + 2m3 + m4)/6 = – 0.1970
Thus at x = 0.2:
y = y0 + k = 1 – 0.0199 = 0.9801 and y` = z = z0 + m = 0 – 0.1970 = - 0.1970

WORKSHEET 06
1. Solve the initial value problem dy / dx  x 2 y  1, y(0)  1 , using Taylor’s series method, find
the values y at x = 0.1 and x = 0.2 to five decimal places.
2. Employ Taylor’s method to obtain approximate value of y at x = 0.2 for the differential
equation y  2y  3e x , y(0)  0 .
3. Given dy / dx  (y  x) / (y  x) with initial condition y = 1 at x = 0. Find y for
x = 0.1 by Euler’s method.
4. Using Euler’s method, find approximate value of y when x = 0.6 for dy / dx  1  2xy , given
that y = 0 when x = 0 (take h = 0.2).
5. Solve the following by Euler’s modified method:
dy / dx  ln(x  y), y(0)  2 at x = 1.2 and 1.4 with h = 0.2.

6. Solve differential equation dy / dx   xy 2 , y  2 at x  0 , by modified Euler’s method and


obtained y at x = 0.2 in two steps of 0.1 each.
7. Apply Runge-Kutta fourth order to find an approximate value of y when x = 0.2 given that
dy/dx = x + y and y = 1 when x = 0.
8. Using Runge-Kutta of fourth order dy/dx = (y2 – x2)/ (y2 – x2) with y(0) = 1 at x = 0.2 and
x = 0.4.
-:100:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS

dy 2xy  e x
9. Using Runge-Kutta of fourth order, solve for y at x = 1.2, 1.4 from  with
dx x 2  xe x
x0 = 1, y0 = 0.
dy y  x
10. Find the value of y for x = 0.1 by Picard’s method, given that  , y(0)  1 .
dx y  x
11. Employ Picard’s method to obtain, correct to four places of decimal, solution of the
differential equation for x = 0.4, given that y = 0 when x = 0.
12. Find y(0.2) by using RK method, the initial value problem: y`` = y + xy`, y(0) = 1 and
y`(0) = 0.
13. The angular displacement θ of a simple pendulum is given by the equation:
d 2 g
2
 sin   0
dt L
Where L = 98 cm and g = 980 cm/sec and when t = 0, θ = 0 and d θ/dt = 4. Compute θ and
2

dθ /dt when t = 0.2 sec.


14. The amount of uniformly distributed radioactive contaminant contained in a closed reactor is
measured by its concentration c (Becquerel/liter or Bq/L). The contaminant decreases at a decay
rate proportional to its concentrations; that is Decay rate = – kc, where k is a constant with units
of day-1. Therefore, according to (change = increase – decrease) equation, a mass balance for the
reactor can be written as dc/dt = –kc. Use Euler’s method to solve above equation from t = 0 to
1day with k = 0.2d-1. Employ a step size of t  0.1day . The concentration at t = 0 is 10 Bq/L.

-:101:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

CHAPTER
SEVEN NOUMERICAL SOLUTIONS
OF PARTIAL
DIFFERENTIAL EQUATIONS

INTRODUCTION
Partial differential equations are the chief means of providing mathematical models in science,
engineering and other fields. Generally these models must be solved numerically. This chapter
provides a concise introduction to standard numerical techniques, once chosen on the basis of
their general utility for practical problems.
The main focus is emphasized on finite difference methods for simple examples of parabolic,
hyperbolic and elliptic equations.
It will be an excellent choice for students and teachers in mathematics, engineering and computer
science seeking a concise introduction to the subject.
Like ordinary differential equations, partial differential equations are equations to be solved in
which the unknown element is a function, but in PDEs the function is one of several variables,
and so of course the known information relates the function and its partial derivatives with
respect to the several variables. Again, one generally looks for qualitative statements about the
solution. For example, in many cases, solutions exist only if some of the parameters lie in a
specific set (say, the set of integers). Various broad families of PDEs admit general statements
about the behavior of their solutions. This area has a long-standing close relationship with the
physical sciences, especially physics, thermodynamics, and quantum mechanics: for many of the
topics in the field, the origins of the problem and the qualitative nature of the solutions are best
understood by describing the corresponding result in physics, as we shall do below.
Roughly, corresponding to the initial values in an ODE problem, PDEs are usually solved in the
presence of boundary conditions. For example, the Dirichlet problem (actually introduced by
Riemann) asks for the solution of the Laplace condition on an open subset D of the plane, with
the added condition that the value of “u” the dependent variable on the boundary of D was to be
some prescribed function f. (Physically this corresponds to asking, for example, for the steady-
state distribution of electrical charge within D when prescribed voltages are applied around the
boundary.) It is a nontrivial task to determine how much boundary information is appropriate for
a given PDE.
Linear differential equations occur perhaps, most frequently in applications (in settings in which a
superposition principle is appropriate.) When these differential equations are first-order, they
share many features with ordinary differential equations. (More precisely, they correspond to
families of ODEs, in which considerable attention must be focused on the dependence of the
solutions on the parameters.)

-:102:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

Historically, three equations were of fundamental interest and exhibit distinctive behaviors. These
led to the clarification of three types of second-order linear differential equations of great interest.
2u 2u
For example, The Laplace equation   0 applies to potential energy functions
x 2 y2
u = u(x, y) for a conservative force field in the plane. PDEs of this type are called elliptic.
2u 2u u
The Heat Equation   applies to the temperature distribution u(x, y) in the plane
x 2
y 2 t
when heat is allowed to flow from warm areas to cool ones. PDEs of this type are parabolic.
2u 2u 2u
The Wave Equation   applies to the heights u(x, y) of vibrating membranes and
x 2 y2 t 2
other wave functions. PDEs of this type are called hyperbolic.
The analyses of these three types of equations are quite distinct in character. Allowing non-
constant coefficients, we see that the solution of a general second-order linear PDE may change
its characteristics from point to point. These behaviors generalize to nonlinear PDEs as well.
A general linear PDE may be viewed as seeking the kernel of a linear map defined between
appropriate function spaces. (Determining which function space is best suited to the problem is
itself a nontrivial problem and requires careful functional analysis as well as a consideration of
the origin of the equation. Indeed, it is the analysis of PDEs which tends to drive the development
of classes of topological vector spaces.) The perspective of differential operators allows the use of
general tools from linear algebra, including eigenspace decomposition (spectral theory) and index
theory.
Modern approaches seek methods applicable to non-linear PDEs as well as linear ones. In this
context existence and uniqueness results, and theorems concerning the regularity of solutions, are
more difficult. Since it is unlikely that explicit solutions can be obtained for any but the most
special of problems, methods of "solving" the PDEs involve analysis within the appropriate
function space -- for example, seeking convergence of a sequence of functions which can be
shown to approximately solve the PDE, or describing the sought-for function as a fixed point
under a self-map on the function space, or as the point at which some real-valued function is
minimized. Some of these approaches may be modified to give algorithms for estimating
numerical solutions to a PDE.
Generalizations of results about partial differential equations often lead to statements about
function spaces and other topological vector spaces. For example, integral techniques (solving a
differential equation by computing a convolution, say) lead to integral operators (transforms on
functions spaces); these and differential operators lead in turn to general pseudo-differential
operators on function spaces.
Classification Of 2nd Order Partial Differential Equations
The general form of 2nd order partial differential equation is
Au xx  2Bu xy  Cu yy  Du x  Eu y  Fu  G  0
Where A, B, C are linear functions of independent variable x and y or constants. This equation is
known as: Elliptical if B2  AC  0 , Parabolic if B2  AC  0 , Hyperbolic if B2  AC  0

-:103:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

Example 01: Classify the partial differential equation uxx + uyy = 0


Solution: Here A =1, B = 0 and C = 1.
B2  AC   0   11  1  0
2
Using:
Therefore, given differential equation is Elliptical.
Example 02: Classify the partial differential equation u xx   2 u t (Diffusion equation)

Solution: Writing the given differential equation as u xx   2 u t  0

Here A =1, B = 0 and C = 0. Using B2  AC   0   1 0   0


2

Therefore, given differential equation is Parabolic.


Example 03: Classify the partial differential equation u xx  1/ c2 u tt  
Solution: Writing the given differential equation as u xx  1/ c2 u tt  0  
Here A =1, B = 0 and C =-1/c2  B2  AC   0   1  1/ c   1/ c
2 2 2
0

Therefore, given differential equation is hyperbolic.


Finite Differences
A difference quotient is the quotient obtained by dividing the difference between values of
function by difference between two corresponding values of the independent variables.
Thus for a function f(x) of single variable, the difference coefficient is:
[f(x + h) – f(x)]/h
whose limiting value is the derivative of f(x) w. r. t. x or df/dx. Here in our study u(x, y) is the
function of two variables x, y, and we consider the differences in both directions x and y.
Let us consider the differences in x direction: The Taylor series for u(x, y0) at the point u(x0, y0) is

u  x, y0   u  x 0 , y0    x  x 0  u x  x 0 , y 0 
 x  x0 
2

xx  , y 0   ...
 u (1)
2!
where x 0    x . Substituting x  x 0  h in equation (1), we have
h2
u  x 0  h, y0   u  x 0 , y 0   hu x  x 0 , y 0   u xx  , y 0   ...
2!
u  x 0  h, y0   u  x 0 , y0  h
or  u x  x 0 , y0   u xx  , y0   ...
h 2!
u  x 0  h, y0   u  x 0 , y0  h
 u x  x 0 , y0    u xx  , y0   ... (2)
h 2!
where x 0    x 0  h.
Equation (2) implies that if we replace u x  x 0 , y0  by
u  x 0  h, y0   u  x 0 , y 0 
u x  x 0 , y0  
h
h
then truncation error will be  u xx  , y0   ... where x 0    x 0  h
2!
Hence we have finite difference formula for the first partial derivative at  x 0 , y0  as
u  x 0  h, y0   u  x 0 , y0 
u x  x 0 , y0   (3)
h
which is called forward difference approximation to u x  x 0 , y0  .
-:104:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

Similarly by putting x  x 0  h in (1),we get


u  x 0 , y0   u  x 0  h, y0 
u x  x 0 , y0   (4)
h
which is called backward difference approximation to u x  x 0 , y0  .
Now to obtain an approximation for uxx, we use both forward and backward differences. Using
forward difference for ux, we get
u  x  h, y0   u x  x 0 , y0 
u xx  x 0 , y0   x 0 (5)
h
Using above results, we get
u  x 0  h, y0   2u  x 0 , y0   u  x 0  h, y0 
u xx  x 0 , y0   (6)
h2
Proceeding on the same lines, we have the following formula for derivatives in y direction taking
step size of k.
u  x 0 , y0  k   u  x 0 , y0 
u y  x 0 , y0   (7)
k
u  x 0 , y0   u  x 0 , y0  k 
u y  x 0 , y0   (8)
k
u  x 0 , y0  k   2u  x 0 , y0   u  x 0 , y0  k 
u yy  (9)
k2
Equations (3) and (7) are called forward difference formulas and equations (4) and (8) are called
backward difference formulas. Equations (6) and (9) are called forward difference formulas of
second order.
Geometrical Representation Of Partial Difference Quotients
The following figure shows how xy- plane is divided into a series of the rectangles of sides
x  h and y  k by equidistant lines drawn parallel to the axis of coordinates points, that is;
 x, y  ,  x  h, y  ,  x  2h, y  ,  x  h, y  ,  x  2h, y ... x, y  k  ,  x, y  2k 

(x,y+2k)

(x, y+k)

y  k
(x-2h,y) (x-h,y) (x,y) (x+h, y) (x+2h,y)

(x,y-k)

(x,y-2k)
x  h
Fig(1)
We can also interpret the above idea in a different notation by drawing two sets of parallel lines
x = ih and y = jk i, j = 0,1,2,3,….The point of intersection of these families of lines are called
mesh points, lattice points or grid points. Point (i, j) is called the grid point and is surrounded by
the neighboring points, shown in fig (2).

-:105:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

If u is function of two variables x and y then, the values of u(x, y) at point (i, j) is denoted by ui,j.
With reference to the above fig (1) an fig (2), we can write
u i 1, j  u i, j u i, j  u i 1, j
ux  (Forward differences), u x  (Backward differences)
h h
u i, j1  u i, j u i, j  u i, j1
uy  (Forward differences), u y  (Backward differences)
k k
ui1, j  2ui, j  ui 1, j ui, j1  2ui, j  ui, j1
u xx  and u yy 
h2 k2
y

(i, j+2)

(i, j+1)

(i-2, j) (i-1, j) (i, j) (i+1, j) (i+2, j)


k=1

(i, j-1)

(i, j-2)

h=1 x
Fig (2)
ELLIPTICAL EQUATION
An important type of elliptical equation is:
2u 2u
  0 or u xx  u yy  0 (1)
x 2 y2
This is called Laplace equation. Replacing the derivatives by the corresponding difference
expressions in equation (1), we get after simplification
ui1, j  2ui, j  ui 1, j ui, j1  2ui, j  ui, j1
u xx  u yy  0  2
 0
h k2
Taking a square mesh and putting h = k, we get
1
ui, j   ui1, j  ui 1, j  ui, j1  ui, j1  (2)
4
-:106:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

This indicates that value of u at any interior mesh point is the arithmetic mean of its values at the
four neighboring mesh point to the left, right, below and above. This is “Standard Five-Point
Formula” (SFPF). Instead of equation (2) we may also use the formula:
1
ui, j   ui 1, j1  ui 1, j1  ui 1, j1  ui 1, j1  (3)
4
which shows that the value of ui, j is the arithmetic mean of its values at the four neighboring
diagonal mesh points. This is called “Diagonal Five Point Formula” (DFPF). The
diagrammatical representation of these two different schemes is shown as under:

ui,j+1

ui-1,j ui , j Ui+1,j

ui,j-1

SFPF

ui-1,j+1 ui+1,j+1

ui-1,j-1 ui+1,j-1

DFPF
It may be noted that in SFPF scheme if the four values on the right side are not available then four
values that are near to boundaries or the values on the boundaries are to be utilized. As this
scheme is iterative scheme hence the values at the required mesh will automatically be refined.
The iterative process is repeated till the solution is obtained to the required accuracy.
It may further be noted that both schemes may be used simultaneously depending upon the
problem’s statement and available information.
Laplace Equation (Liebmann’s Iteration Process)
Consider the Laplace equation u xx  u yy  0
with some given boundary conditions. For the sake of simplicity we assume that function u(x, y)
is required over a rectangle region R with boundary C. Let R be divided into a network of small
squares of sides h. Let the values of u(x, y) on boundary C be given by Ci and the interior mesh
points and boundary points are shown as in following figure.

-:107:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

c1 c2 c3 c4 c5

c16 c6

u1 u2 u3
c15 c7
u4 u5 u6
c14 c8
u7 u8 u9

c13 c12 c11 c10 c9

Now we know that the value of u(x, y) satisfying the Laplace equation can be replaced by either
SFPF or DFPF to start the iteration process initially, first of all we find the rough values at
interior mesh points and then improve it by iterative process, mostly by SFPF.
First of all we find u5 at the centre of squares by taking the average of four boundary values using
SFPF.
u5   c15  c7  c3  c11  / 4
Next we find initial values at centre of four large inner squares by DFPF, Thus
u1   u 5  c1  c3  c15  / 4 , u 3   u 5  c5  c3  c 7  / 4
u 7   u 5  c13  c11  c15  / 4 , u 9   u 5  c9  c7  c11  / 4
The values at the remaining interior mesh points are obtained by SFPF. Thus,
u 2   u1  u 3  c3  c5  / 4 , u 4   c15  c5  u1  u 7  / 4
u 6   u 5  c7  u 3  u 9  / 4 , u 8   u 7  u 9  u 5  c11  / 4
We have got all boundary values of u and rough values at every mesh (grid) point in the interior
of the region R. Thus we can now proceed with an iteration process to improve their accuracy.
We start with u1 and iterate it using the latest available values of the four adjacent points. Thus
we iterate all mesh points symmetrically from left to right along successive rows. The iterative

formula is: ui, j(n 1) 


1
4

ui 1, j(n 1)  ui 1, j(n)  ui, j1(n)  ui, j1(n 1) 
Here the superscript denotes iteration number. This is known as Liebmanns Iteration Process.
Example 01: Solve u xx  u yy  0, in 0  x  4, 0  y  4 , given that

u  0, y   0, u  4, y   8  2y, u  x,0  x 2 / 2 and u  x,4   x 2 . Taking h = k =1 and obtain the


result correct to one decimal.
Solution: Let us divide the given region R, that is, 0  x  4 , 0  y  4 into sixteen square
meshes. The numerical values of boundaries using the given analytical expression are calculated
and are shown in the following figure.

-:108:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

.u(x, 4) = x2
0 1 4 9 16

0 14
.u1 u2
. u3
.

0 12
. u(0, y) = 0 u(4, y)= 8 + 2y
.

.u4 u5
. u6
.

0 10
. u7 . u8 . u9

0 0.5 2 4.5 8
2
.u(x, 0) = x /2

Now let u1 , u 2 , u 3 , u 4 ,...u n be the values of u at the interior mesh points. The initial values of u
are calculated either by SFPF OR DFPF as given below.
0  12  4  2 0  4  0  4.5
u5(0)   4.5 (SFPF) , u1(0)   2.125 (DFPF)
4 4
4.5  16  4  12 0  2  0  4.5
u3(0)   9.125 (DFPF) , u 7(0)   1.625 (DFPF)
4 4
4.5  8  2  12 4  4.5  2.125  9.125
u9(0)   6.625 (DFPF) u 2(0)   4.9375 (SFPF)
4 4
0  4.5  2.125  1.625 4.5  12  9.125  6.625
u 4(0)   2.0625 (SFPF), u 6(0)   8.0625 (SFPF)
4 4
1.625  6.625  4.5  2
and u8(0)   3.6875 (SFPF)
4
Now by using LIEBMANNS Iteration formula to improve the above results
1
4
ui, j(n 1)  
ui 1, j(n 1)  ui 1, j(n)  ui, j1(n)  ui, j1(n 1) 
When we use this formula at points u1 , u 2 , u 3 ,...u 9 ,we get the following equations for iterations:

 
u1(n 1)  0  u 2   u 4(n 1)  1 / 4 ,
n
 
u 2(n 1)  u1(n 1)  u 3   u 5(n)  4 / 4
n

u 3(n 1)  u  14  u    9  / 4 ,
(n 1)
u 4(n 1)  0  u    u /4 (n 1)
n n
2 6 5 u 7
(n)
1

u 5(n 1)  u 4 u   u   u
(n 1)
6
n
 / 4, 8
n
2
(n 1)
u 6(n 1)  u 5 12  u    u
(n 1)
/4 9
n
3
(n 1)

u 7(n 1)   0  u    0.5  u


8
n
/4 , 4
(n 1)
u8(n 1)  u 7u   2u
(n 1)
9 /4
n
5
(n 1)


u 9(n 1)  u8(n 1)  10  4.5  u 6(n 1) / 4 
1st iteration: u1(1) 
1
4
 0
4

0  u 2   u 4(0)  1   0  4.9375  2.0625  1  2
1

-:109:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

u 2(1) 
4
 0 1
4

u1  u 3   u 5(0)  4   2  2.915  4.5  4   4.90625
1 (1)

4
 0

u 3(1)  u 2(1)  14  u 6   9   4.90625  14  8.0625  9   8.9921875
1 1
4

4
 0 1
4

u 4(n)  0  u 5   u 7 (0)  u1(1)   0  4.5  1.625  2   2.03125
1

4
 0 0 1
4

u 5(1)  u 4(1)  u 6   u 8   u 2(1)   2.03125  8.0625  3.6875  4.90625   4.671875
1

4
 0 1
4

u 6(1)  u 5(1)  12  u 9   u 3(1)   4.671875  12  6.625  8.9921875   8.0722656
1

1
4
  0 
1
u 7 (1)  0  u 8  0.5  u 4(1)   0  3.6785  0.5  2.03125   1.5546875
4

4
 0 1
4

u 8(1)  u 7 (1)  u 9   2  u 5(1)  1.5546875  6.625  2  4.671875   3.712806
1

1
4
 1
u 9(1)  u 8(1)  10  4.5  u 6(1)   3.7128906  10  4.5  8.0722656   6.5712891
4
Similarly after two more iterations, we get
u1  1.99 u 2  4.91 u 3  8.99 u 4  2.06 u 5  4.69
u 6  8.06 u 7  1.57 u8  3.71 u 9  6.57
Poisson Equation
The Partial differential equation of the form
2u 2u
2 u  f (x, y) or   f (x, y) or u xx  u yy  f (x, y) (1)
x 2 y2
where f(x, y) is a given function of x and y is called Poisson equation. It is an elliptical
differential equation. To solve Poisson equation numerically, the derivatives in equation are
replaced by difference expressions at the points x = i h, y = j k (here h = k). Then (1) will
1 1
become 2  u i 1, j  2u i, j  u i 1, j   2 ui, j1  2ui, j  ui, j1   f (ih, jk)
h h
or ui1, j  ui 1, j  ui, j1  ui, j1  4ui, j  h 2f (ih, jh)  Assuming that mesh is square so h  k 
Applying the above formula at each mesh point, we get
similar equations in the pivotal values i, j. These
equations can be solved by any suitable iteration
technique.
Example 02: Solve the equation  2 u  10(x 2  y 2  10)
over the square mesh with sides x = 0, y = 0, x = 3, y = 3
with u = 0 on the boundary and mesh length of 1.
Solution: According to the statement of given problem
geometry/ mesh will be as under. The given differential equation is:
2u 2u
  10(x 2  y2  10) (1)
x 2
y 2

Let u1 , u 2 , u 3 , u 4 be the values of u at the four mesh points A, B, C and D. Replacing RHS of (1)
by finite difference expressions and putting x = ih, y = jh. But h = 1, hence x = i, y = j.

-:110:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

Thus equation (1) becomes:


u i 1, j  ui 1, j  ui, j1  ui, j1  4ui, j  10 i 2  j2  10  (2)

Now using equation (2):


At A, putting i = 1, j = 2 we get:
1
0  u 2  u3  0  4u1  10 1  4  10  or u1   u 2  u3  150 (3)
4
At B, putting i = 2, j = 2 we get:
u 2   u1  u 4  180  / 4 (4)
At C, putting i = 1, j = 1 we get:
u3   u1  u 4  120  / 4 (5)
At D, putting i = 2, j = 1we get:
u 4   u 2  u3  150  / 4 (6)
The right sides of equations (3) and (6) are equal hence we deduce that u 4  u1 . It is enough to
evaluate u1, u2 and u3. Thus equations (3), (4) and (5) become:
1
u1   u 2  u 3  150  (i)
4
1
u 2   u1  90  (ii)
2
1
u 3   u1  60  (iii)
2
Now using Gauss-Seidal iterative method, we obtain:
First Iteration: Putting u 2  u 3  0 in (i) we get, u1(1)  150 / 4  37.5

Now putting u1 = 37.5 in (ii) we get u 2(1)   37.5  90 / 2  63.75

Again putting u1 = 37.5 in (iii) we get u3(1)   37.5  60.2  48.75


Second Iteration: Using new values in (i), (ii) and (iii), we get
u1(2)   63.75  48.75  150  / 4  65.625, u 2(2)   65.625  90  / 2  77.8125
u 3(2)   65.625  60  / 2  62.8125
Third Iteration: Using new values in (i), (ii) and (iii), we get
u1(3)   77.8125  62.8125  150  / 4  72.65625, u 2(3)   72.65625  90  / 2  81.328125
u 3(3)   72.65625  60  / 2  66.32815
Fourth Iteration: Using the new values and repeating the same process, we get
u1(4)  74.414063, u 2(4)  82.207031, u 3(4)  67.207031
Fifth Iteration:
u1(5)  74.853516, u 2(5)  82.426758, u3(5)  67.426758
Sixth Iteration:
u1(6)  74.963379, u 2(6)  82.481689, u 3(6)  67.481689
Since the values are almost same as in fifth iteration, hence we have
u1  75, u 2  82.5, u 3  67.5 . Since u4 = u1 hence, u 4  75 .
-:111:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

PARABOLIC EQUATION
In this section we shall study the numerical solution of Parabolic type of differential equation.
One Dimensional Heat Equation,
u  2u k
 2 2 , where 2  is diffusivity of the substance (cm2/sec), c is specific heat of
t x c
the material,  is the density and k is the thermal conductivity is well known example of
parabolic equation. The solution of this is a function of x and t that u = u (x, t).
Benders Schmidt Method
Benders Schmidt method is one of the methods used in solving one dimensional heat equation
when certain conditions are to be satisfied. Consider the equation
u 2u k
  2 2 , where  2  (1)
t x c
Writing a = 1/α2, equation (1) becomes:
2u u
a OR u xx  au t (2)
2
x t
Bender-Schmidt makes use of the following boundary conditions:
u( 0, t) = T0 and u(l, t) = T1 (3)
and initial condition: u(x, 0) = f(x) (4)
Now selecting h for the space variable x and k for the time variable t and using the finite
differences for the derivatives, that is, substituting:
1 1
u xx  2  u i 1, j  2u i, j  u i 1, j  and u t   u i, j1  u i, j 
h k
1 a
Equation (2) becomes: 2  u i 1, j  2u i, j  u i1, j    u i, j1  u i, j 
h k
k
or 2  u i 1, j  2u i, j  u i 1, j   ui, j1  u i, j
h a
Now suppose that   k / h 2a , then above equation becomes after simplification:
u i, j1  u i 1, j  1  2  u i, j  u i 1, j (5)
The boundary and initial conditions (3) and (4) become:
u 0, j  T0 and u n, j  T1 , where j  1, 2,...  here nh  l  and u i,0  f  ih  ,i  1, 2, ...
Now if we let 1  2  0 or   1/ 2 , then equation (5) becomes
1
ui, j1   ui 1, j  ui 1, j  (6)
2 

In this case h and k are related by k  ah 2 / 2 .


Now equation (6) implies that the value of u at x = xi at time tj+1 is equal to the average of the
values of u at surrounding points xi-1 and xi+1 at previous time tj. Equation (6) is called Benders
Schmidt Recurrence Equation.
u 1  2 u
Example 01: Solve  using the conditions u(0, t ) = 0, u(4, t) = 0 and u(x, 0) = x (4 – x )
t 2 x 2
taking h = 1 = k by using Bender Schmidt recurrence equation.

-:112:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

Solution: The general form of heat equation is: au t  u xx .

Given that u xx  2u t implies that a = 2. Since h = k = 1 and   k / ah 2    1 / 2 .


Now using Bender Schmidt recurrence relation the values of uij are calculated as follows:
Range of x is 0  x  4 and x = ih = i since h = 1. Also t = jk = j since k = 1.
Also given that: u(x, 0) = x (4 – x ) or u(i, 0 ) = i (4 – i).
Thus for i = 0, 1, 2, 3 and 4, u(i, 0) = 0, 3, 4, 3, 0 respectively. These value are filled in the first
row of the table.
In addition to these boundary conditions, we have initial conditions, that is,
u(0, t) = 0 t or u(0, j ) = 0 j . Hence the entries in the first column are zero. Also u(4, t) = 0
t or u(4, j) = 0 j . Therefore the entries in the last column are also zero.
The Bender Schmidt recurrence relation is
1
ui, j1  ui1, j  ui 1, j  (1)
2
1
Putting j = 0, we get ui,1   ui 1,0  ui 1,0  (2)
2
1 1
Now putting i = 1 in (2), we get u1,1   u 2,0  u 0,0    4  0  2
2 2
1 1
Again putting i = 2 in (2) we get u 2,1   u3,0  u1,0   3  3  3
2 2
1 1
Similarly putting i = 3 in (2) we get u3,1   u 4,0  u 2,0   0  4  2 .
2 2
In the same way by putting j = 1, 2, 3, 4, 5, 6, 7, 8, 9 in (1) we shall get other values of u which
nd rd th
are placed in the 2 , 3 up to 12 rows as given in the following table.
i
0 1 2 3 4

0 0 3 4 3 0
1 0 2 3 2 0

2 0 1.5 2 1.5 0

3 0 1 1.5 1 0

j 4 0 0.75 1 0.75 0

5 0 0.5 0.75 0.5 0

6 0 0.375 0.5 0.375 0


7 0 0.25 0.375 0.25 0

8 0 0.1875 0.25 0.1875 0

9 0 0.125 0.1875 0.125 0


10 0 0.094 0.125 0.094 0

-:113:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

Hyperbolic Equation
2u 2u
The wave equation  c2 is the simplest example of hyperbolic partial differential
 t2  x2
equations. Its solution is the displacement function u(x, t) defined for x from 0 to l and for t from
0 to ∞, satisfying the initial and boundary conditions. The solution, as for parabolic equations,
advances in an open-ended region. However, in hyperbolic equations, we must have two initial
and two boundary conditions. It may be noted that hyperbolic equations arise from convection
type of problems in vibration, wave mechanics and gas dynamics.
Solution of Wave Equation
2u 2u
Consider the wave equation c 2
(1)
 t2  x2
u
subject to the initial conditions u  f (x),  g(x), f or 0  x  l, at t = 0 (2)
t
and the boundary conditions u(0, t)  (t), u(l, t)   (t) (3)
Consider a rectangular mesh in the x-t plane spacing h along x direction and k along t direction.
Denoting a mesh point (x, t) = (ih, jk) as simply i, j, we have
 2u ui 1, j  2 ui, j  ui 1, j 2u ui, j1  2 ui, j  ui, j1
 , 
x 2 h2 t 2 k2
Replacing the derivatives in (1) by above approximations, we obtain
c2 k 2
ui, j1  2ui, j  ui 1, j  2 (ui 1, j  2 ui, j  ui, j1 )
h
or u i, j1  2(1  2c2 )u i, j  2c2 (u i 1, j  u i 1, j )  u i, j1 (4)
Here α = k / h. Now replacing the derivatives in (2) by its central different approximation, we get
u i, j1  u i, j1 u
  g(x) and u i,1  u i, 1  2kg(x)
2k t
or u i, j1  u i, j1  2kg(x) for t = 0
and u i,1  u i, 1  2kg(x) for j = 0 (5)
Using initial conditions u = f(x) at t = 0 become u i,1  f (x) (6)
Combining (5) and (6), we have u i,1  f (x)  2k g(x) u i,1
Now (3) gives u 0, j  (t) and ul, j  (t)
Hence the explicit equation (4) gives the values of ui, j+1 at the (j + 1)th level when the nodal
values at (j – 1)st and jth level are known from (6) and (7).
Remark:
1. The coefficient of ui, j in (4) vanish if αc = 1 or k = h/c. Then (4) reduces
to u i, j+1 = u i-1, j + u i+1, j – u i, j-1
2. For α = 1/c, the solution of (4) is stable and coincides with the solution of (1)
3. For α < 1/c, the solution of (4) is stable but inaccurate
4. For α > 1/c, the solution of (4) is unstable
5. The formula (4) converges for α ≤ 1; that is k ≤ h
-:114:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

Example 01: Evaluate the pivotal values of the equation utt = 16 uxx, taking h = 1 up to
x = 1.25. The boundary conditions are u (0, t) = u (5, t) = ut (x, 0) = 0 and u(x, 0) = x (5 – x).
2

2
Solution: Here c = 16, hence the difference equation for the given PDE is
ui, j + 1 = 2 ( 1 – 16 α2 ) ui, j + 16 α2 (ui – 1, j + ui + 1, j ) – ui, j – 1 (1)
where α = k/h. Taking h = 1and choosing k so that the coefficient of ui, j vanishes, we have
16 α2 = 1.
16 ( k/h)2 = 1  k / h = 1 / 4  k = h / 4  k = 1/4. Thus equation (1) reduces to:
ui, j + 1 = ui – 1, j + ui + 1, j – ui, j – 1 (2)
which gives a convergent solution. Its solution coincides with the solution of the given partial
differential equation.
Now, u(0, t) = u(5, t) = 0 or u0, j = 0 and u5, j = 0 for all values of j
Since u(x, 0) = x2 (5 – x) or ui, 0 = i2 (5 – i) for all values of i
 u1,0 = 4, u2, 0 = 12, u3, 0 = 18 and u4, 0 = 16, are the entries of the first row.
u i, j1  u i, j1
Since ut (x, 0) = 0 or  0 . Also when j = 0, ui, 1 = ui, – 1 (3)
2k
Thus the entries of the second row are the same as those of the first row. Putting j = 0 in (2) and
using (3), we get
ui, 1 = ui – 1, 0 + ui + 1, 0 – ui, – 1 = ui – 1, 0 + ui + 1, 0 – ui, 1
or ui, 1 = (1/2) (ui – 1, 0 + ui + 1, 0) (4)
Taking, i = 1, 2, 3, 4 successively, we get
[ u1, 1 = (1/2) (u0, 0 + u2, 0) = (1/2) (0 + 12) = 6
u2, 1 = (1/2) (u1, 0 + u3, 0) = (1/2) (4 + 18) = 11
.

u3, 1 = (1/2) (u2, 0 + u4, 0) = (1/2) (12 + 16) = 14


.

u4, 1 = (1/2) (u3, 0 + u5, 0) = (1/2) (18 + 0) = 9


.

These are the entries of the second row. Similarly, by putting j = 1 in (2), we get
ui, 2 = ui – 1, 1 + ui + 1, 1 – ui, 0 (5)
Taking, i = 1, 2, 3, 4 successively, we get
u1, 2 = u0, 1 + u2, 1 – u1, 0 = 0 + 11 – 4 = 7
u2, 2 = u1, 1 + u3, 1 – u2, 0 = 6 + 14 – 12 = 8
u3, 2 = u2, 1 + u4, 1 – u3, 0 = 11 + 9 – 18 = 2
u4, 2 = u3, 1 + u5, 1 – u4, 0 = 14 + 0 – 16 = – 2
These are the entries of the third row. Similarly by putting j = 2, 3, 4 in (2) and then i = 1, 2, 3, 4,
we get the entries of the 4th, 5th and 6th rows as well. The results are shown in the following table.

-:115:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS

i 0 1 2 3 4 5
j
0 0 4 12 18 16 0
1 0 6 11 14 9 0
2 0 7 8 2 –2 0
3 0 2 –2 –8 –7 0
4 0 –9 –14 –11 –6 0
5 0 –16 –18 –12 –4 0

WORKSHEET 07
1. Classify the following partial differential equations
(i)3u xx  u xy  4u yy  30y  0  ii  u xx  u yy  0

 
(iii) x 2 u xx  a 2  y2 u yy  0    x  ,   y  
(iv)  x  1 u xx  2(x  2)u xy   x  3 u yy  cos  x  2y 
2. Solve the equation uxx+ uyy= 0 for following mesh
1 2

1 4

2 5

4 5
3. Show that the solution of parabolic equation uxx= 2ut, subject to conditions u(0, t) = u(4, t) = 0,
and u(x, 0) = x(4 – x), taking h = 1, and values of t up to t = 5 is given as under:
i
0 1 2 3 4
j
0 0 3 4 3 0
1 0 2 3 2 0
2 0 1.5 2 1.5 0
3 0 1 1.5 1 0
4 0 0.75 1 0.75 0
5 0 0.5 0.75 0.5 0

 2u   2u
4. Solve given that u ( x, 0 ) = sin  x
t 2 x 2
ut (x, 0 ) = 0, u (0, t) = 0 and u (1, t) = 0.
2 u 2 u
5. Solve  given that u (x, 0) = sin  x/8
t 2 x 2
ut (x, 0 ) = 0, for 0  x  1, u (0, t ) = u (1, t) = 0 for t  0

-:116:-
APPENDIX INTRODUCTION TO C++

APPENDIX
INTRODUCTION
TO C++

INTRODUCTION TO C++
This appendix contains some important features of C++ coding which are important to understand the
programs in this book. We do not desire to make you to learn entire C++ language. An introduction to
C++ with examples is given for those users who intend to write programs on numerical methods
using this language. This will enable the reader to understand all programs presented in this book.
Basic knowledge of computer as well as Integrated Development Environment (IDE) is the pre-
requisite. Students may ask their tutors about IDE and compiling and running a program using menus
of the C++.
C++ is a vast and complex language. It has evolved over C and is a better version of it. In other words
we may say that it is the superset of C. C++ has become more popular than all other computer
languages for a variety of reasons. In the world of computers, there is a big group of users of C++
who write programs for a variety of applications. Most of these programmers shifted to C++, taking
advantage of its advanced and easy-to-use features. Traditionally, programs on engineering
applications were written in FORTRAN. C++ provides a better and easy environment to write
portable programs with object-oriented approach. In addition, there are features for generating
attractive graphics. This makes software development a fun. C++ is used for scientific computing as
well as system programming.
The programs presented in this book were developed using the Turbo C compiler (version 3.0)
running on DOS, Windows 98, and Windows XP operating systems. However, all these programs
will also run on UNIX with minor modifications.
Elements of C++ Coding
To write C++ programs in a simple and straightforward manner is sometimes referred to as KIS
(“Keep It Simple“). Do not “stretch” the language by trying bizarre/strange usage. Here is the first
C++ program. We shall explain contents of each line in detail so that a new user of this programming
language would certainly feel no any kind of difficulty to understand the coding.
COMPUTER PROGRAM
//Area of circle
#include<constream.h>
#include<math.h>
const float Pi = 3.14159;
#define Radius 4.4
void main()
{ clrscr( );
-:117:-
APPENDIX INTRODUCTION TO C++
double Area;
Area= 2*Pi*pow(Radius, 2);
cout<<"\nArea of circle is “<<Area;
getch();
}

Comment Statement of C++ Coding


Above program start with comment statement (i.e., // Area of circle). Comments are important part of
any program. They help the programmer and anyone else who reads the source file (coding of the
program) to understand the purpose of the program. It may be noted that the compiler ignores the
comments, so they do not add to the file size or execution time of the program.
Comments can start with a double slash ‘//‘or with /* and terminates with */. For example, the
comment written in the above program may also be written as: /* Area of circle*/
Preprocessor Directives
The C++ preprocessor modifies a source code file before handing it over to the compiler. Most likely
the preprocessor includes files directly into other files. Preprocessor treats text in the file specified by
filename as if it appeared in the current file.
Syntax: #include <filename>
Here, #include <filename> searches the source path first and then include path. It does not search the
source directory.
Header Files
The declarations for standard input and output, library functions, various formatting instructions, and
process handling are written in the header files. These are also called include files. These files are also
known as header files. They may be included in any order. A header file must be included before any
use of something it declares. Some of the headers files that are frequently used in this book are given
below:

Header file Purpose


conio.h Declares the some function like clrscr(), getch(), getche()
Contains declarations, which are used for standard input and output
iostream.h
operations of a C++ program.
constream.h It is combination of conio.h and iostream.h
math.h Declares mathematical functions and macros
stdio.h Declares input and output functions, types and macros.
process.h Declares symbols and structures for the management of a process.
Contains declarations for the input/output manipulators (i.e. the
iomanip.h
operators used to modify the way data are displayed).

Declaration of Constants / Macros


A constant may be specified using the keyword, const. The data type is placed after the keyword. An
example is
const float Pi = 3.14159;

-:118:-
APPENDIX INTRODUCTION TO C++
This keyword specifies that the value of Pi will not change throughout the program. Therefore, the
above statement ensures that the value of Pi will not be altered by mistake, which is intended to be a
constant. When we deal with universal constants, for example, gas constant, Planck’s constant, or
electronic charge, such declaration is useful.
There is another way of declaring constant. This approach uses the #define directive. This
preprocessor directive defines an identifier to be equivalent to a particular string of characters. The
syntax of this is:
#define [name of the identifiers] [replacement text]
The replacement text can be any sequence of characters, not just numbers. An example is
#define radius 4.4 (Writing radius = 4.4 is incorrect here)
After this definition, wherever the identifier radius appears in the program (excluding its occurrence
in quotes or as a part of another name) will be replaced by the number 4.4. It may be noted that the
data type (e.g., integer or double precision) can not be specified by using #define. This can lead to
bugs in the program. The use of these two methods of declaring a constant is illustrated in above
program. There is no semicolon after the #define directive, but a semicolon follows the const
statement.
Main Function
The parentheses after main indicate that main is a program building block called a function. C++
programs contain one or more functions, exactly one of which must be a main. C++ programs
normally begin executing at function main, even if main is not the first function in the program.
Programming Body
All those statements, which are enclosed between braces { } is called programming body. The left
brace ”{“ shows that programming statements start from here and corresponding right brace “}“,
shows the end of programming statements.
C++ Keywords
Keyword means reserved words by the language to implement various features (the words whose
meaning is predefined to the compiler), for example for, do-while, if, switch, etc are key words of
C++. Keywords may not be used as identifiers, for example, switch = 2.0*area. Here “area” is a
variable a name, “switch” is a keyword that has been used as an identifier. A complete list of C++
keywords is shown below.
C and C++ keywords
Asm do inline short typeid
Auto double int signed typename
Bool dynamic_cast long sizeof union
Break dlse mutable static unsigned
Case dnum namespace static_cast using
Catch dxplicit new struct virtual
Char dxtern operator switch void
Class false private template volatile
Const float protected this wchar_t
const_cast for public throw while
continue friend register true
Default goto reinterpret_cast try
Delete if return typedef

-:119:-
APPENDIX INTRODUCTION TO C++
C++ Identifiers
An “identifier” is a sequence of characters (letters, digits and special characters). When numbers are
stored in various locations of memory, it becomes necessary to name each of the memory location.
The name of the memory location is called variable.
Suppose programmer wants to find the product of the two numbers. Following are the steps to be
done by the computer.
o Read two numbers
o Calculate the Product
o Output of the Product
Actually the computer steps as follows:
 Read two numbers and stores them in two locations of the memory
 Multiplying the contents of two locations, and the result is stored in a third location
 The content of third location is displayed / printed as an output
Memory locations may contain integer, real or character constants. Depending upon the data
contained in the memory location, the identifiers are classified as integer, real, and character
variables.
Data Types
C++ has six major data types, having keywords. Following tree structure describes the C++ data
types.

C++ Data Types

Integer Data Types Real Data Types Character Data Types

Char
(1 Byte)
int long float double long double
(2 Bytes) (4 Bytes) (4 Bytes) (8 Bytes) (10 Bytes)

The data type description in given below.


Numerical Range Digits of Memory
Keyword
Low High Precision occupied in Bytes
Char - 128 127 ----- 1
Int - 32,768 32767 ----- 2
Long - 2,147,483,648 2,147,483,647 ----- 4
Float 3.4*10-38 3.4*1038 7 4
Double 1.7 * 10-308 1.7 * 10308 15 8
long double 3.4 * 10-4932 3.4 * 104932 19 10
unsigned char 0 256 ----- 1
unsigned int 0 65,535 ----- 2
unsigned long 0 4,294,967,295 ----- 4

-:120:-
APPENDIX INTRODUCTION TO C++
Integer and Float Conversions
In order to effectively developed C++ programs, it will be necessary for programmer to understand
the rules that are used for implicit conversion of floating point and integer values in C++. These are
described below:
Operation Output Operation Output
11 / 5 2 5 / 11 0
7.0 / 3 2.2 5.0 / 11 0.45
7 / 3.0 2.2 5 / 11.0 0.45
7.0 / 3.0 2.2 5.0 / 11.0 0.45
It means;
 An arithmetic operation between an integer and integer always capitulate/yield an integer
output.
 An arithmetic operation between a floating point and a floating point always capitulate a
floating point output.
 An arithmetic operation between a floating point and an integer always capitulate a floating
point output.
Casting
It is easy to convert between integer type and real type and vice versa in C++. This is known as
casting. The following example shows the use of casting. It may be coded in two different ways.
float a = 3.4; OR float a = 3.4;
int b = int(a); int b;
b = int(a);
Escape Sequence Character
In C++ there are some special characters called escape sequence characters. A special set of
characters constants is used as escape sequences. These characters are called escape characters
because their output is different from the normal way the char type is used. It causes an escape from
the normal interpretation of a stripy, so that the next character is recognized as having a special
meaning. Escape sequences are written along with the “\” back slash symbol which is considered as
escape character. Some of the escape sequences are mentioned in below table.

Escape Escape
Meaning Meaning
sequence sequence
\a Alert, Single Beep \\ Comments
\b Backspace \’ Single quotation mark
\f Form feed \” Double quotation mark
\n Carriage New Line \* Asterisk mark
\t Tab \xDB Solid square
\r Carriage Return Key \xCD Equal to sign

Operators
A C++ expression usually carries out one or more operators, such as assignment, any arithmetic
operation. The operations are represented by operators. An operand is a data item on which an

-:121:-
APPENDIX INTRODUCTION TO C++
operator acts. The precedence of all operators is summarized in tree structure which are used in C++
compiler.

Operators

Assignment
operator =
Conditional operators
Insertion and Relational operators
Extraction operators Logical operators
Increment operators
Arithmetic Operators ? and :

<< and >> &&, ¦¦, !


<, <=, >, >=, ==, !=
+, -, *, /, % Postfix Prefix

Input Statement
Input statement allows the user to input the data. cin>> is an input statement, which is used to ask for
an input from keyboard. cin is an object and it is defined as a keyword. It is used along with the
extraction (input) operator i.e. >>. For the cin>> statement iostream.h or constream.h header file is
required.
Example: Statement
cin>>x; // insert a value from the keyboard for x operand.

Output Statement
Output statement is used to display the required results or message. cout<< is an output statement,
which is used for displaying the required message/ numerical values. cout is an object and it is
defined as a keyword. It is used along with the insertion (output) operator i.e. <<.
Example: Statement
cout<<"\Area of circle is “<<Area;

This means to display the message “Area of circle is “ and then value of “Area” on the monitor
screen.

Arithmetic Operators
There are five arithmetic operators in C / C++. They are

Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder after integer division

The % operator some times is referred to as modulus operator.

-:122:-
APPENDIX INTRODUCTION TO C++
Arithmetic Increment / Decrement Operators
Arithmetic operators +, -, *, /, % are also used for port addition, subtraction, multiplication and
division purpose. There use is shown in the following example:
 a + = b; //Same as a = a + b; statement.
 a - = b; //Same as a = a - b; statement.
 a * = b; //Same as a = a * b; statement.
 a / = b; //Same as a = a / b; statement.

Increment / Decrement Operators


i. Postfix increment operator which is written as (a++) has the same effect as we write a = a + 1.
Here, first the operand value is evaluated and then the operand is increased by 1.
ii. Prefix increment operator is written as (++a). Here, first the operand is increased by 1 and then
operand value is evaluated. (Note: ++a means a = a + 1).
iii. Postfix decrement operator is written as (a--). This means the operand value is evaluated and
then the operand is decreased by 1. (Note: a-- means a = a -1).
iv. Prefix decrement operator is shown as (--a). First the operand is decreased by 1 and then
operand value is evaluated. (Note: - -a means a = a -1).

Relational Operators
The relational operators are used to determine whether two numbers are equal, or if one is greater or
less than the other. The relational statement assigns the value either 1 (True) or 0 (False). There are
six relational operators. The following table shows these relational operators, their use with a simple
code.
Operator Interpretation Simple Evaluates
100 == 50: False
== Is equal to
50== 50: True
100 != 50: True
!= Is not equal to
50 !== 50: False
100> 50: True
> Greater than
50>50: False
100 > = 50: True
>= Greater than or equal to
50> = 50: True
100< 50: False
< Less than
50< 50: False
100 < = 50: False
<= Less than or equal to
50 < = 50: True

Control Statements
Before writing a program to solve a particular problem, it is essential to have a thorough
understanding of the problem and a carefully planned approach to solving the problem. When writing
a program, it is equally essential to understand the types of building blocks that are available and to

-:123:-
APPENDIX INTRODUCTION TO C++
employ proven program constriction/limitation principles. Here we discuss all of these issues in
presentation of theory and principles of structured programming.

Control Statements

Decision making statements Loop statements

For Loop statements While Loop statements do-while Loop statements

Simple for statement compound for / while / do- Nested for / while / do-
while statements while statements

If decision making switch decision making conditional operator


statements statement decision making statement

Simple If compound if If-else else-If /nested if-else


statement statement statement statement
Control Instructions specify the order in which the various instructions in a program are to be
executed by the C++ computer. They define the flow of control in a program. C++ has only six
control structures, three types of decision making and three types of control-counter-repetition, which
are shown in the above tree structure.
Decision Making Statements
Decision making statements allow us to take various actions according to as certain condition(s) are
satisfied. There are:
if-statement
“if” is a keyword and is followed by parenthesis, which contains a conditional expression using a
relational operator that allows a programmer to make a decision based on the truth-ness or falsity of
conditional expression. If the condition is met i.e., the condition is true, the statement in the body of if
structure is executed. If the condition is not met, the body statement is not executed. The body of “if
statement” consists either a single statement (which is known as simple if statement) or group of
(multiple) statements enclosed by braces (which is known as compound if statement).
Syntax of simple if statement
if(test expression)
Statement;
Syntax of compound if statement
if(test expression)
{ Statement(s);}

-:124:-
APPENDIX INTRODUCTION TO C++
Example:
Simple if-statement Compound if-statement
If (a < b) if (a < b)
x = x + y; {
x = x + y;
y = v + w;
z = p + q;
}

If-else Decision Making Statement


The “if-else” statement executes one of two alternative statements, according to the value of the
specified condition. It has the syntax:
Syntax of if –else decision making
if(condition)
{ statement(s); }
else
{ statement(s); }
Example:
If (a <= b)
{
y = 2;
z = 3;
}
else
{
y = 20;
z = 30;
}
Here “if ” body structure is executed when specified condition is true otherwise, “if “ body
is skipped and “else” body structure is executed.
Conditional Operators
C++ provides a very intelligent facility of conditional operators (? and :) that are closely related to the
“if-else” structure. The conditional operator in C++ is a ternary operator because it takes three
operands.
Syntax of conditional operator
Relational Expression ? Expression 1 : Expression 2
Example:
(a < b) ? z = a – b : z = b – a;
This means that if “a” is less than b then compute z = a – b otherwise compute z = b – a.
The operands together with the conditional operator form a conditional expression. The first
expression (operand) is the condition, the second expression (operand) is the value for the entire
conditional expression if the condition is true and the third expression (operand) is the value for the
entire conditional expression if the condition is false.
-:125:-
APPENDIX INTRODUCTION TO C++
Nested if-else (else-if) statement
Nested if-else structures test for multiple cases is formed by placing if-else selection structures inside
if-else selection structures.
Syntax of else-if statement
if(condition1)
{ statement(s); }
else
if (condition2)
{ statement(s); }
.
.
.
else
if(condition n)
{ statement1; }
else
{ statement(s); }
Example:
if (a < b)
z = a + b;
else
if (a > b)
z = b – a;
else
if (a = b)
z = a * b;
else
cout<<”No other choice is left”;
A nested if-else structure is much faster than a series of single selection if structure because of the
possibility of early exit after one of the specified condition is satisfied. It is not mandatory to place the
last else statement because by default the statement after it will automatically be executed.

Logical Operators
We have discussed so far two families of operators, first one is the arithmetic operators (+, -
, *, / and %), and the second one is the relational operators (<, <=, >, >=, ==, and !=). Now we
examine the third family of operators, called Logical Operators. These operators allow to logically
combine the Boolean variables (i.e. variables of type bool, with true (1) or false (0) values).
Logical operators table
Operators Meaning
&& AND
¦¦ OR
! NOT

-:126:-
APPENDIX INTRODUCTION TO C++
These operators are used for certain logics in different control statements. This is describing in table
below:
Truth tables of logical operators
Truth Table of && operator Truth Table of ¦ ¦ operator Truth Table of ! operator
x y x &&y x Y x¦ ¦y x y x!y
1 1 1 1 1 1 1 1 0
1 0 0 1 0 1 1 0 1
0 1 0 0 1 1 0 1 1
0 0 0 0 0 0 0 0 0

switch( ) Statement
The switch statement is a multiple-selection statement that allows handling of a decision from the
number of given choices.
switch-case-default: These three keywords go together and develop the sense of the control statement.
The switch statement consists of a series of case labels, and an optional default case.
Note
1. The keyword case is followed by an integer or a character constant.
2. Each constant in each case must be different from others.
3. “cases” can be arranged in ascending / descending / random order ( in fact the cases can be
put in any order).
4. If there are multiple statements to be executed in each case, there is no need to enclose these
statements within a pair of braces.
5. The “switch” statement always appears together with “break” statement which takes the
control outside the “switch” statement.
6. “default” case is executed if none of the case is true.
Syntax of switch decision making statement
switch(expression)
{ case 1: statement(s);break;
case 2: statement(s);break;
.
.
case n: statement(s);break;
default: statement(s);}
Example:
switch(option)
{ case’+’: cout<<”x + y =”<<x + y; break;
case’-’: cout<<”x - y =”<<x - y; break;
case’*’: cout<<”x * y =”<<x * y; break;
case’/’: cout<<”x / y =”<<x / y; break;
default:cout<<”you have pressed wrong key”;
}
Here “option” must be declared as “character” type variable.

-:127:-
APPENDIX INTRODUCTION TO C++
for loop (Counter-Controlled Repetition)
The process of repeating some portion of the program either a specified number of times or until a
particular condition is satisfied is called “Counter-Controlled Repetition” loop.
Syntax of for loop
.f o r (initial expression; control expression; increment / decrement expression)
{ statement (s);
}
Here we must assign:
a. The name of a control variable (or loop counter).
b. The initial value of the control variable.
c. The condition that tests for the final value of the control variable.
d. The increment / decrement value by which the control variable is modified each time through
the loop.
“for loop” is executed repeatedly until the control expression is false. Before the first iteration, initial
expression is evaluated. This is usually used to initialize variables for the loop. After, each iteration of
loop is executed the increment / decrement value is evaluated. This is usually used to increment /
decrement a loop counter. “for loop” executes the section of a code, a fixed number of times. It is
usually used when you know, before entering the loop that how many times a code is to be executed.
Example:
for ( int n = 1; n <= 10; n ++)
cout<<n<<endl;
while loop
A repetition structure allows the programmer to specify that an action is to be repeated while some
condition remains true. The statement(s) contained in the while repetition structure constitute the
body of the while loop. When the control / test condition becomes false, the repetition terminates
automatically.
Syntax of while loop
initial expression;
while(control / test expression)
{statement(s);
increment / decrement statement ;
}
Example:
int n =;
while(n < = 100)
{ cout<<n<<endl;
n + = 2;}
do-while loop
do-while loop body is executed as long as specified condition is true (or remains non-zero). The test
takes place after do-while body is executed. The syntax is under:
initial expression;
do
-:128:-
APPENDIX INTRODUCTION TO C++
{ statement(s);
increment / decrement statement ;
}while(condition);
“do-while” loop is executed at-least one time if specified condition is even initially failed.
Example:
int n = 1;
do
{ cout<<n<<endl;
n + = 2;
}while(n <= 99);

Nested Loops
Loop within a loop is called nested loop. There are many types of nested loops, for-for, for-while, for-
do while, while-while, while-for etc are the examples of nested loop. We shall only discuss those
nested loops which are frequently used in our programs presented in this book.
Syntax of for-for loop
f o r (initial expression1; control expression1; increment / decrement expression1)
{
f o r (initial expression2; control expression2; increment / decrement expression2)
{statement (s);
}
}
Example:
for (int i = 1; i <= 10; i++)
{ cout<<endl;
for (int j = 1; j <= 10; j++)
cout<<i * j << setw(6);
}
Syntax of while-while loop
initial expression1;
while(control expression1)
{
initial expression2;
while(control expression2)
{
statement (s);
increment / decrement expression2;
}
increment / decrement expression1;
}
Example: int i = 1;
while (i <= 5)

-:129:-
APPENDIX INTRODUCTION TO C++
{ cout<<endl;
int j=1;
while (j <=i)
{
cout<<” * “;
j++;
}
}

Array
An array is a collection of variables of same data type, placed contiguously in memory. An array is a
consecutive group of memory locations that have the same name and the same type. Like other
variables, the array needs to be defined, so that computer should know what kind of array, and how
large an array is under use.
Syntax of declaration of an array
int temp[n] ;
Here “int” specifies the type of variable, just as it does with simple variable, and the word temp is the
tag (name) of the array variable. The subscript [n], tells how many variables of type “int” will be in
the array. “n” is the subscript of an array and it must be declared in an unsigned integer. For example
n = 7, declares an array “temp” with 7 subscripts. First subscript element in every array is the zeroth
element. Thus, the first subscript element in the array temp is referred to as temp [0], the second
subscript element as temp [1] and so on. The following figure is a schematic representation of what
the array looks like.

0 1 2 3 4 5 6

temp

int

Multi-dimensional Array
An array variable can have two or more dimensions is known as multiple dimensional array. This
permits to emulate multidimensional objects such as tables with rows and columns. Syntax of
declaration of multi-dimensional array
double table[m][n];
User-Define Functions (UDF)
The functions that are defined by the users are known as the user defined functions. They can be
defined in the same file or in a separate header file.
Syntax of UDF
<data type> <name of function / function argument>(<data type> argument>)
{ statement(s);
return <argument / variable>;
}
A user-define function has two parts, its header and its body. The header of a function specifies its
return type, name, and parameter list.
-:130:-
APPENDIX INTRODUCTION TO C++
Syntax of user-define header
<data type> <name of function / function argument>(<data type> argument>)
for example; long fact(int a)
The body of a function is the block of code that follows its header. It contains the code that performs
the function’s action, including the return statement that specifies the value that the function sends
back to the place where it was called by function argument.
Syntax of function body
{ statement(s);
return <argument / variable>;
}
The function’s return statement serves two purposes: it terminates the function, and/or it returns a
value to the calling program.

User Defined Header File


A user defined function can be defined in the same programming code but we can also make a header
file of that UDF by saving programming code with .h extension in the include subdirectory. For
calling the function, we just have to call by preprocessor directives i.e. #include
<filename.h>.
Structure
Ordinary identifiers can hold one piece of information and arrays can hold information of the same
data type. C++ uses structured data that contains a group of different or similar data type identifiers
into a single data type identifier. For this, a keyword struct is used. One may ask how the elements of
the structure can be accessed. The elements of the structure can be accessed by using the dot (.)
operator.
Syntax of Structure
struct <structure name>
{ <data type> identifier 1;
<data type> identifier 2;
<data type> identifier 3;
.
.
.
<data type> identifier n;
};
Some Useful Built-In-Library Functions

Clear screen function: clrscr( );


It clears / remove the previous output on the screen or it clears/remove the most recent text which is
appeared on the screen before using clrscr( ) statement. For the clrscr( ) statement programmer
required the conio.h header file.

Get character functions: getch( ); and getche( );


getch( ) and getche( ) reads a single character directly from the keyboard, but getch( ) does not echo
(echo off) to the screen and getche( ) echoes (echo on) to the screen. For the getch( ) and getche( )
-:131:-
APPENDIX INTRODUCTION TO C++
statements conio.h header file is required. The programmer usually uses this statement to hold the
screen.

Output function: printf( “<format string>”, <list of identifiers>);


printf( ) is a function which is used to display the value contained in a variable or message. It is a
powerful and versatile function. printf( ) is purely C compiler statement but it can also be used in
C++.
The printf( ) do the following:
 Accept a series of arguments
 Apply to each argument a format specifier contained in the format string
 Output the formatted data
In printf( )format strings, format specifies have the following possible forms:
 %[data type format specifier]
 %[.][precision size / field-width][data type format specifier]

Format Specifiers:

The format specifier define in printf( ) and tell to compiler what format to use in output of the printf( )
arguments. The following table shows format specifiers with appropriate meaning;
Format specifier Meaning / data types
%c For single character value
%d For decimal integer value
%e For floating point value (exponential notation)
%f For floating pint value (decimal notation)
%l For long integer value
%lf for double value
%s For string value
%u For unsigned integer value

For hexadecimal value


%x

Field-width specifiers
A digit preceding the decimal point in the field-width specifier controls / sets the minimum width of
the space to be used to contain the number when it is printed the output.
The format specifier in printf( ) determines the interpretation of an identifier’s type, the width of the
field, the number of decimal places printed and justification.

Input function: scanf(“<format string>”,<list of address of identifiers>);


scanf function allows you to input the data of one or more than one identifiers via keyboard.
Programmer sending addresses of identifiers, addresses are obtained by using the ‘&’ address
operator (represented by the ampersand ‘&’) to scanf function.
The format specifiers for scanf( ) are similar to all those for printf( ) as well.
(Note: for the printf( ) and scanf( ) functions, stdio.h header file is required.)

-:132:-
APPENDIX INTRODUCTION TO C++
Set width function: setw(argument);
setw( ) is used to set specific width between two messages or values to be printed on the screen. For
the setw(), statement constream.h header file is required.

Exit function: e x i t ( ) ;
This function immediately terminates the process and control is shifted back to calling entity. For the
e x i t ( ) statement process.h or stdlib.h header file is required.

Sound (unsigned frequency)


Produce a sound (sound turns on the PC speaker) on at specified frequency. The frequency specifies
the frequency of the sound in hertz (cycles per sound).
nosound( )
It is used for terminating the sound (turns off the PC speaker) after it has been turned on by a call to
sound function.

delay(milliseconds)
With a call delay, the current program is suspended from execution for the time specified by the
arguments milliseconds, which is enclosed in parenthesis. It is a time lag between two executions. Its
header file is dos.h.

Keyboard hit function: k b h i t ( ) ;


k b h i t ( ) checks for currently available keystrokes. Any available keystrokes can be retrieved with
g e t c h ( ) or g e t c h e ( ) .
For example, when using the !kbhit() function in the loop as a condition, the loop body terminates as
soon as key is pressed.
Math Functions
The following table shows some common math library functions.

Type of
Functions Meaning Argument (x)
value

abs(x) x integer integer

fabs(x) x float float

sqrt(x) x float float

exp(x) ex float float

log(x) log e x float float


i. If both arguments are integer i. integer
pow(x,y) xy
ii. If any one float ii. float
sin(x) Sin x float in radian measure float

-:133:-
APPENDIX INTRODUCTION TO C++
cos(x) Cos x float in radian measure float
tan(x) Tan x float in radian measure float
-1
sin (x) antisin x float in radian measure float
-1
cos (x) anticos x float in radian measure float
-1
Tan (x) antitan x float in radian measure float
Rounding the smallest integer
ceil(x) float float
greater than the argument
Rounding the largest integer less
Floor(x) float float
than the argument

(Note: for the math library functions, math.h or complex.h header file is required.)

-:134:-
OTHER BOOKS OF FARKALEET SERIES

 Applied Calculus
 Linear Algebra and Analytical Geometry
 Differential Equations and Fourier Series
 Complex Variable and Transforms
 Complex Analysis, Probability and Statistics
 Statistical Methods and Estimations
 Numerical Analysis and Computational
Applications
 Laplace, Fourier and Z-Transforms
 Linear Algebra & Numerical Methods
 MCQs in Mathematics for F. Sc.-I
 MCQs in Mathematics for F. Sc-II
 Textbook with Key (Mathematics) for F. Sc-I
 Textbook with Key (Mathematics) for F. Sc-II
 Partial Differential Equations with Applications

PRICE:
Rs. 200/

JAHANGIR PUBLICATIONS, LARKANA


Cell: 0333-756-5595, 0304-453-2100

You might also like