Numerical Analysis and Computational Applications Farkaleet Series
Numerical Analysis and Computational Applications Farkaleet Series
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
EDITED BY:
Dr. Sania Niazamani
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
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 xy
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.
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.12 0.13 0.14 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 106 , 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 106 . 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 106 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) | 103.
n 1
(0.5) n 1
Now, by “Hit & Trial” method, for 103 0.001 :
n 1
(0.5)11
If n = 1, then = 0.125 ≤ 0.001 (This is not true)
11
(0.5) 21
If n = 2, then = 0.0147 ≤ 0.001 (This is not true)
2 1
(0.5)31
If n = 3, then = 0.0156 ≤ 0.001 (This is not true)
3 1
(0.5) 41
If n = 4, then = 0.0063 ≤ 0.001 (This is not true)
4 1
(0.5)51
If n = 5, then = 0.0026≤ 0.001 (This is not true)
5 1
(0.5)61
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 106 7,4 02
-: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
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 104 , e2 105 , e3 105
2 2 2
-:8:-
CHAPTER ONE ERROR ANALYSIS
1 1 1
A.E e1 e2 e3 104 105 105 0.6 104
2 2 2
A.E. 0.6 104
R.E 0.2335 104
z 2.56986
The result lies in the range z A.E 2.56986 0.6 104 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 104 , e2 105 , and e3 105 AE e1 e2 e3
2 2 2
1 1 1 AE 6 105
Now, A.E 104 105 105 6 105 R.E 2.23892 105
2 2 2 z 2.67986
The result lies in the range z A.E 2.67986 0.6 104
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
-:10:-
CHAPTER ONE ERROR ANALYSIS
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 104 and e2 0.5 105 . Hence,
1 0.5 104 0.5 105 0.5 104 1 0.5 104 0.5 104 0.5 104 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 104 1.37035 0.2223 104 2.223 105
Thus the answer lies in the range z A.E 1.37035 2.223 105 .
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 104
0.0041
R.E 0.5 104 0.00205 104 0.205 106
0.9999
A.E R.E z 0.205 106 0.9999 0.2049 106
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
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
.
a = 2.75
.
.m3 = 2.875 b=3
.
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
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
-: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)
ba
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
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 8e2t 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.25l 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 106 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 103 .
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
-: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.
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
-:29:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS
-:30:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS
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
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 l21 1 0 0 1 0
1 2 76 1 l31 l32 1 0 0 1
1 0 0
1
Here we have assumed that L l21 1 0
l31 l32 1
Now multiplying left side and equating the terms, weget
3 3 1 7 9 7 7
l21 0 l21 , l21 l31 0 l31 , l32 0 l32
2 2 2 6 4 6 6
1 0 0
L 3 2
1
1 0 . Now UA 1 L1 .
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
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
T
Now to find A-1 we proceed as follows: Since, A 1 L1 L1
Now, let
l11 0 0 3 0 0 l11 0 0 1 0 0
L1 l21 l22 0 LL1 I 2 1 0 l21 l22 0 0 1 0
l31 l32 l33 1 2 l31 l32 l33 0 0 1
-1
-:35:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS
1 l11
3l11 13 , l21 0 l21 2l11
2l11 23 , l22 1
l21 2 l31 0 2 l31 l11
l11 l21 13 23 1 l31 1 ,
2
1 0 0
3
T
1 2
Thus, L 3 1 0 . Now, A 1 L1 L1 ; 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.
j1
-:36:-
CHAPTER THREE SOLUTIONS OF LINEAR EQUATIONS
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
-: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
-: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
-: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
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. 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.
-: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
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 .
-: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
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:
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:
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
(ii)
2 x3
E 12 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
2 2
x 2x 1 2 x 1 x 2x
Solution: (i) x x 1 (1)
Ex x 1 2 x 1
ii
2 x
E 12 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 16 x 16
And Equation (2) becomes:
2 x x 2 3 2 x 13 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
-: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
(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
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
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
-: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
yn y0 ny0
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!
y1 5 y1 ...
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 ny0 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 ny1/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 ny0 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 y1 2 y1
n(n 1) 2
2!
y1 3 y1
n(n 1)(n 2) 3
3!
y1 4 y1
n(n 1)(n 2)(n 3) 4
4!
y1 5 y1 ...
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 ny1 y1 y2 y2 ... (2)
2! 3! 4!
This is known as Gauss Interpolation For mula.
Taking average of for mulae (1) and (2), we get:
(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.
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
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
74
3 12 1
19 12 4 1
7
4 19 43
-:66:-
CHAPTER FOUR INTERPOLATION AND EXTRAPOLATION
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
Applied stress
5 10 15 20 25 30 35 40
X kg/ mm 2
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
-: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
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
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
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
. 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
-: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 ny0 y0 y0
2! 3!
-:70:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION
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
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
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
-:72:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION
s`(2.5) = 5.966 which means that body’s speed at time t = 2.5 minutes is approximately 5.97
.
-:73:-
CHAPTER FIVE NUMERICAL DIFFERENTIATION AND INTEGRATION
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 ny0 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 ny0 2! y0 3!
y0
4!
y0 ... dn
0 0
-: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
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
Now, f (x)
iv
2 30x 4 8x 3 16x 2 36x 12
1 x
5
2
-: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
ii. Now use the Trapezoidal rule for the last two values of y, i.e., IT = h/2 [y10 + y11].
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
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
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:
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
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
vi1 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: vi1 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)
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, 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
-:93:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS
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 nf 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 nf 0
2
f0
6
f 0 ... dx
x0 x0
-: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
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
-:98:-
CHAPTER SIX NUMERICAL SOLUTIONS OF ORDINARY DIFFERENTIAL EQUATIONS
-: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.
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
-: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
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
(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, j1 u i, j u i, j u i, j1
uy (Forward differences), u y (Backward differences)
k k
ui1, j 2ui, j ui 1, j ui, j1 2ui, j ui, j1
u xx and u yy
h2 k2
y
(i, j+2)
(i, j+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
ui1, j 2ui, j ui 1, j ui, j1 2ui, j ui, j1
u xx u yy 0 2
0
h k2
Taking a square mesh and putting h = k, we get
1
ui, j ui1, j ui 1, j ui, j1 ui, j1 (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, j1 ui 1, j1 ui 1, j1 ui 1, j1 (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
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
-: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, j1(n) ui, j1(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 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, j1 2ui, j ui, j1 f (ih, jk)
h h
or ui1, j ui 1, j ui, j1 ui, j1 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
u i 1, j ui 1, j ui, j1 ui, j1 4ui, j 10 i 2 j2 10 (2)
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, j1 u i, j
h k
1 a
Equation (2) becomes: 2 u i 1, j 2u i, j u i1, j u i, j1 u i, j
h k
k
or 2 u i 1, j 2u i, j u i 1, j ui, j1 u i, j
h a
Now suppose that k / h 2a , then above equation becomes after simplification:
u i, j1 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, j1 ui 1, j ui 1, j (6)
2
-:112:-
CHAPTER SEVEN NUMERICAL SOLUTIONS OF PARTIAL DIFFERENTIAL EQUATIONS
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
-: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, j1 2 ui, j ui, j1
,
x 2 h2 t 2 k2
Replacing the derivatives in (1) by above approximations, we obtain
c2 k 2
ui, j1 2ui, j ui 1, j 2 (ui 1, j 2 ui, j ui, j1 )
h
or u i, j1 2(1 2c2 )u i, j 2c2 (u i 1, j u i 1, j ) u i, j1 (4)
Here α = k / h. Now replacing the derivatives in (2) by its central different approximation, we get
u i, j1 u i, j1 u
g(x) and u i,1 u i, 1 2kg(x)
2k t
or u i, j1 u i, j1 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, j1 u i, j1
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
.
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();
}
-: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.
Char
(1 Byte)
int long float double long double
(2 Bytes) (4 Bytes) (4 Bytes) (8 Bytes) (10 Bytes)
-: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 :
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
-: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.
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
Simple for statement compound for / while / do- Nested for / while / do-
while statements while statements
-: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;
}
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.
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
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.
-: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.
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.
Type of
Functions Meaning Argument (x)
value
-: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/