100% found this document useful (2 votes)
1K views92 pages

Numerical Analysis

This document outlines the syllabus for a third-year numerical analysis class taught in the Chemical Engineering Department at the University of Technology. The syllabus covers five main topics: root finding, interpolation and approximation, numerical integration, systems of equations, and approximation of solutions to ordinary differential equations. Specific numerical methods that will be covered under each topic are listed. References for the course include textbooks on numerical analysis and numerical methods for engineers and scientists. The document provides introductory information on numerical analysis versus analytical solutions and discusses sources of error in numerical procedures.

Uploaded by

Juch Cowank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
1K views92 pages

Numerical Analysis

This document outlines the syllabus for a third-year numerical analysis class taught in the Chemical Engineering Department at the University of Technology. The syllabus covers five main topics: root finding, interpolation and approximation, numerical integration, systems of equations, and approximation of solutions to ordinary differential equations. Specific numerical methods that will be covered under each topic are listed. References for the course include textbooks on numerical analysis and numerical methods for engineers and scientists. The document provides introductory information on numerical analysis versus analytical solutions and discusses sources of error in numerical procedures.

Uploaded by

Juch Cowank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

Numerical

Analysis
Third Class

Chemical Engineering Department

University of Technology

Asst. Prof. Dr. Zaidoon Mohsin shakor


Numerical Analysis

Syllabus
U

1. ROOT FINDING (4 Hrs)


• Bisection method
• Secant method
• Newton's method
• Roots of polynomials

2. INTERPOLATION AND APPROXIMATION (CURVE FITTING) (6 Hrs)


• Lagrange form of the interpolation polynomial
• Piecewise linear interpolation
• Cubic spline interpolation
• Regression

3. NUMERICAL INTEGRATION (8 Hrs)


• Equally spaced base points
• Newton-cotes closed integration formulas
• Newton-cotes open integration formulas
• Numerical Integration with in-equally spaced base points
• Orthogonal polynolmials
• Numerical differentiation

4. SYSTEM OF EQUATIONS (6 Hrs)


• Elementary transformation of matrix
• Gaussian elimination
• Gauss-Jordan elimination
• Jacobi iterative method
• Newton-Raphson iteration for non-linear equations

5. APPROXIMATION OF SOLUTION ORDINARY DIFFERENTIAL EQUATIONS (6 Hrs)


• Solution of first-order ordinary differential equations
• Taylor's methods
• Eular method
• Runge-Kutta methods
• Simultaneous ordinary differential equations
• Predictor-corrector methods

Numerical Analysis
Third Class
References:
U

1- Gerald, C.F., and Wheatly, P.O., “Applied numerical analysis”, 7th ed., Addison-
P P

Wesley, 2004.
2- Pradeep, A., “Introduction to Numerical Methods in Chemical Engineering” New
Delhi, 2010.
3- Hoffman, J. D. “Numerical Methods of Engineers and Scientists”, 2nd ed., P P

McGraw Hill, 1992.


4- Chapra, S. C. “Numerical Methods of Engineers”, 5th ed., McGraw Hill, 2006.
P P

5- Mathews, J. H. and Fink, K.D., “Numerical Methods using MATLAB”, 3rd ed., P P

Prentice Hall, 1999.

Numerical Analysis
Third Class
Introduction to Numerical Analysis
Analysis versus Numerical Analysis
U

The word analysis in mathematics usually means who to solve a problem through
equations. The solving procedures may include algebra, calculus, differential
equations, or the like.

Numerical analysis is similar in that problems solved, but the only procedures that
are used are arithmetic: add, subtract, multiply, divide and compare.

Differences between analytical solutions and numerical solutions:


1) An analytical solution is usually given in terms of mathematical functions. The
behavior and properties of the function are often apparent. However, a numerical
solution is always an approximation. It can be plotted to show some of the
behavior of the solution.
2) An analytical solution is not always meaningful by itself.
Example: 3 as one of the roots of x 3 − x 2 − 3x + 3 = 0 .
3) While the numerical solution is an approximation, it can usually be evaluated as
accurate as we need. Actually, evaluating an analytic solution numerically is
subject to the same errors.

Computers and Numerical Analysis


U

Numerical Methods Computers


Numerical Analysis
+ Programs

• As you will learn enough about many numerical methods, you will be able to
write programs to implement them.
• Programs can be written in any computer language. In this course all programs
will be written in Matlab environment.
• Actually, writing programs is not always necessary. Numerical analysis is so
important that extensive commercial software packages are available.

Errors in Numerical Procedures


U

There are two common ways to express the size of the error in a computed result:
absolute error and relative error.
• Absolute error = | true value – approximate value |, which is usually used when the
magnitude of the true value is small.
absolute error
• Relative error = , which is a desirable one.
| true value |

Numerical Analysis -1-


Third Class
Root Finding

Solutions of Non-Linear Equations in One Variable

1.1 Graphical Methods


U

A simple method for obtaining a root of the equation f(x) = 0 is to plot the function
and observe where it crosses the x axis. There are many available software that will
facilitate making a plot of a function. We will use Matlab exclusively for the course
notes; however you can use other software such as Excel or Matcad for your work.

Example 1.1
(1 − e−0.15x) =50 using the graphical method.
600
Solve P P

x
Solution
(1 − e−0.15x) − 50 can be plotted in Figure 1.1 using the
600
The function f(x) = P P

x
following Matlab statements.

x=4:0.1:20;
fx=600*(1-exp(-0.15*x))./x-50;
plot(x,fx,[0 20],[0 0])
xlabel('x');
ylabel('f(x)')
grid on; zoom on

20

15

10

0
f(x)

-5

-10

-15

-20

-25
0 2 4 6 8 10 12 14 16 18 20
x

Figure 1.1 The graphical method for roots finding.


Numerical Analysis /Lec. 1 -2-
Third Class
The Matlab Zoom on statement allows the function to be zoomed in at the cursor
with left mouse click (right mouse click will zoom out). Each time you click the axes
limits will be changed by a factor of 2 (in or out). You can zoom in as many times as
necessary for the desired accuracy. Figure 1.2 shows the approximate root x to be
8.79

0.06

0.04

0.02

0
f(x)

-0.02

-0.04

-0.06

-0.08

8.75 8.76 8.77 8.78 8.79 8.8 8.81 8.82


x

Figure 1.2 The graphical method for roots finding with Matlab Zoom on.

The plot of a function between x 1 and x 2 is important for understanding its behavior
R R R R

within this interval. More than one root can occur within the interval when f(x 1 ) and R R

f(x 2 ) are on opposite sides of the x axis. The roots can also occur within the interval
R R

when f(x 1 ) and f(x 2 ) are on the same sides of the x axis. Since the functions that are
R R R R

tangent to the x axis satisfy the requirement f(x) = 0 at this point, the tangent point is
called a multiple root.

1.2 The Bisection Method


U

The bisection method or interval halving can be used to determine the solution to f(x)
= 0 on an interval [x 1 = a, x 2 = b] if f(x) is real and continuous on the interval and
R R R R

f(x 1 ) and f(x 2 ) have opposite signs. We assume for simplicity that the root in this
R R R R

interval is unique. The location of the root is then calculated as lying at the midpoint
of the subinterval within which the functions have opposite signs. The process is
repeated to any specified accuracy.
The procedure can be summarized in the following steps

Numerical Analysis /Lec. 1 -3-


Third Class
Let f(x 1 ) f(x 2 ) < 0 on an interval [x 1 = a, x 2 = b]
R R R R R R R R

1
Step 1 Let x x = R R (x 1 + x 2 ); f 1 = f(x 1 ); f 2 = f(x 2 )
R R R R R R R R R R R R

2
Step 2 Evaluate f x = f(x x ) R R R R

If f x f 1 > 0 then
R R R R

x1 = xx; f1 = fx R R R R R R R R

else
x2 = xx; f2 = fx R R R R R R R R

end
Step 3 If abs(x 2 − x 1 ) > an error tolerance, go back to Step 1
R R R R

Figure 1.3 shows first three iterations x 3 , x 4 , and x 5 of the bisection method. R R R R R R

15

10

x5 x3 x2=b
f(x)

0
x1 = a x4

-5

-10

-15
4 6 8 10 12 14 16
x

Figure 1.3 The first three iterations x 3 , x 4 , and x 5 of the bisection method. R R R R R R

x 1 =6 ⇒ f(x 1 ) =9.3430
R R R R and x 2 =14 ⇒
R R f(x 2 ) = -12.3910
R R

1 1
x3 =
R R (x 1 + x 2 ) = (6 + 14) = 10
R R R R ⇒ f(x 3 ) = -3.3878 R R

2 2
1 1
f(x 1 ) f(x 3 ) < 0 ⇒ x 4 =
R R R R R R (x 1 + x 3 ) = (6 + 10) = 8
R R R R ⇒ f(x 4 ) =2.4104R R

2 2
1 1
f(x 3 ) f(x 4 ) < 0 ⇒ x 5 =
R R R R R R (x 3 + x 4 ) = (10 + 8) = 9
R R R R ⇒ f(x 4 ) = -0.6160
R R

2 2

Numerical Analysis /Lec. 1 -4-


Third Class
1 1
Since f(x 1 ) and f(x 2 ) bracket the root and x 3 =
R R R R R R (x 1 + x 2 ) = (a + b), the error after
R R R R

2 2
1
the first iteration is less than or equal to (b − a).
2
A Matlab program for the bisection method is listed in Table 1.1 where the function
f(x) is an input to the program. The statement Inline is used to define the function at a
given value of x.

Table 1.1
% Bisection Method
fun=inline('600*(1-exp(-0.15*x)) /x-50')
x1=6;
f1= fun (x1);
x2=14;
f2= fun (x2);
tol=1e-5;
for i=1:100
x3=(x1+x2)/2;
f3= fun(x3);
if f1*f3<0
x2=x3;
f2=f3;
else
x1=x3;
f1=f3;
end
if abs(x2-x1)<tol; break;end
end
x3
x3 =
8.7892

Example 1.2
Use the bisection method to find the root of the equation x-cos(x) = 0 with a percent
relative true error |ε t |≤ 1%. (The exact value is 0.7391)
R R

Solution
f(x)= x-cos(x)

Numerical Analysis /Lec. 1 -5-


Third Class
We have seen before that there is a single root lies in the interval [0,1]. Therefore, we
start with x l =0 and x 2 =1, then iterate using the same procedure followed in example
R R R R

1.1 to get the following tabulated results:


Iter XlR X2R X3R f(X l )R R f(X 2 )
R R f(X 3 )
R R |ε t |
R R

1 0.0 1.0 0.5 -1.0000 0.4597 -0.3776 32.35


2 0.5 1.0 0.75 -0.3776 0.4597 0.0183 1.48
3 0.5 0.75 0.625 -0.3776 0.0183 -0.1860 15.44
4 0.625 0.75 0.6875 -0.1860 0.0183 -0.0853 6.98
5 0.6875 0.75 0.7188 -0.0853 0.0183 -0.0339 2.75
6 0.7188 0.75 0.7344 -0.0339 0.0183 -0.0079 0.64
Then x=0.7344

Example 1.3
The friction factor f depends on the Reynolds number Re for turbulent flow in smooth
pipe according to the following relationship.
1
= −0.40 + 3 ln(Re f )
f
Use the bisection method to compute f for Re = 25200 that lies between [0.001 ,0.1 ].

Solution
Re-write the above equation in the form
1
E ( f ) = −0.40 + 3 ln(25200 f ) −
f
Iter fl
R f2
R f3
R E(f l ) R R E(f 2 ) R R E(f 3 ) R R

1 0.0010 0.1000 0.0505 -20.4514 11.9973 10.1179


2 0.0010 0.0505 0.0258 -20.4514 10.1179 7.7528
3 0.0010 0.0258 0.0134 -20.4514 7.7528 4.7705
4 0.0010 0.0134 0.0072 -20.4514 4.7705 1.0841
5 0.0010 0.0072 0.0041 -20.4514 1.0841 -3.2373
6 0.0041 0.0072 0.0056 -3.2373 1.0841 -0.6453
7 0.0056 0.0072 0.0064 -0.6453 1.0841 0.2946
8 0.0056 0.0064 0.0060 -0.6453 0.2946 -0.1536
Then according to above table f = 0.006

Numerical Analysis /Lec. 1 -6-


Third Class
1.3 Secant Method (Linear Interpolation Method)
The bisection method is generally inefficient, it requires more function evaluations in
comparison with the secant method which is linear interpolation using the latest two
points. Figure 1.4 shows graphically the root x 3 obtained from the intersection of the R R

line AB with the x-axis.


f(x)
f(x2) B

x1 x3 x4
x5 x2

A
f(x1)

Figure 1.4 Graphical depiction of the secant method.

The intersection of the straight line with the x-axis can be obtained by using similar
triangles x 3 x 1 A and x 3 x 2 A or by using linear interpolation with the following
R RR R R RR R R RR R R RR R

points.
x x1 x3 x2 R R R

f(x) f(x 1 ) 0 f(x 2 ) R R R R

x3 − x 2 0 − f ( x2 ) x2 − x1
= ⇒ x 3 = x 2 − f(x 2 )
x2 − x1 f ( x 2 ) − f ( x1 ) f ( x2 ) − f ( x1 )
R R R R R R

The next guess is then obtained from the straight line through two points [x 2 , f(x 2 )] R R R R

and [x 3 , f(x 3 )]. In general, the guessed valued is calculated from the two previous
R R R R

points [x n-1 , f(x n-1 )] and [x n , f(x n )] as


R R R R R R R R

xn − xn −1
x n+1 = x n − f(x n )
f ( xn ) − f ( xn −1 )
R R R R R R

The secant method always uses the latest two points without the requirement that they
bracket the root as shown in Figure 1.4 for points [x 3 , f(x 3 )] and [x 4 , f(x 4 )]. As a R R R R R R R R

consequence, the secant method can sometime diverge. A Matlab program for the

Numerical Analysis /Lec. 1 -7-


Third Class
secant method is listed in Table 1.2 where the function f(x) is an input to the program.

Table 1.2
% Secant method
fun=inline('600*(1-exp(-0.15*x))/x-50')
tol=1e-5;
x(1)=1;
f(1)= fun(x(1));
x(2)=14;
f(2)= fun(x(2));
for i=2:20
x(i+1)=x(i)-f(i)*(x(i)-x(i-1))/(f(i)-f(i-1))
f(i+1)= fun(x(i+1));
if abs(x(i+1)-x(i))<tol;
break;
end
end
x

x=
1.0000
14.0000
10.4956
8.3724
8.8209
8.7897
8.7892
8.7892
The last value of x vector is the solution of the equation

Example 1.4
Use secant method to estimate the root of f(x)=e-x-x. with the two initial guesses x 0 =
P P R R

0. and x 1 = 1.
R R

Solution:
Iter x f(x)
Starting 0 1.0000
Values 1.0 -0.6321
1 0.6127 -0.0708
2 0.563838 0.00518
3 0.56717 -0.0000418
Numerical Analysis /Lec. 1 -8-
Third Class
Example 1.5
Repeat Examples 1.4 using the secant method with the two initial guesses x 0 = 2. and R R

x 1 = 3.
R R

Solution:
Iter x f(x)
Starting 2 -1.8647
Values 3 -2.9502
1 0.2823 0.4718
2 0.6570 -0.1385
3 0.5719 -0.0075
4 0.5671
This method converges with the required accuracy after 5 iterations.

Example 1.6
Use secant method with initial guesses T = 300 and T = 350 to calculate the bubble
point of binary system (VCM 18 mol%, Water 82 mol%). The vapor pressure for this
components is calculated by:
VCM Po vcm =exp(14.9601-1803.84/(T-43.15)) P PR R

Water Po w =exp(18.3036-3816.44/(T-46.13)) P PR R

Where: K i = P o i /P t R R P PR R R R

P t =760
R R

y i =K i ×x i
R R R R R

At Bubble point ∑y i =∑K i ×x i =1 R R R R R R

Solution
1803.84 3816.44
(14.9601- ) 0.18 (18.3036 - T - 46.13 ) 0.82
f(T)= K i ×x i -1 = e R R R R
T - 43.15
× +e × -1
760 760
Iter T f(T)
Initial 300.0000 -0.3086
Values 350.0000 1.4192
1 308.9299 -0.1132
2 311.9636 -0.0378
3 313.4866 0.0018
4 313.4157 -0.000028
Then at bubble point T=313.457 K

Numerical Analysis /Lec. 1 -9-


Third Class
1.4 The Newton-Raphson Method
The Newton-Raphson method and its modification is probably the most widely used
of all root-finding methods. Starting with an initial guess x 1 at the root, the next guess R R

x 2 is the intersection of the tangent from the point [x 1 , f(x 1 )] to the x-axis. The next
R R R R R R

guess x 3 is the intersection of the tangent from the point [x 2 , f(x 2 )] to the x-axis as
R R R R R R

shown in Figure 1.5. The process can be repeated until the desired tolerance is
attained.

f(x)
f(x1) B

x3 x2 x1

Figure 1.5 Graphical depiction of the Newton-Raphson method.


The derivative or slope f(x n ) can be approximated numerically as
R R

f ( xn + ∆x ) − f ( xn )
f’(x n ) =
∆x
R R

The Newton-Raphson method can be derived from the definition of a slope


f ( x1 ) − 0 f ( x1 )
f’(x 1 ) = ⇒ x2 = x1 −
x1 − x2
R R R R R R

f ' ( x1 )
In general, from the point [x n , f(x n )], the next guess is calculated as
R R R R

f ( xn )
x n+1 = x n −
R R R R

f ' ( xn )

Example 1.7
Use Newton-Raphson method to estimate the root of f(x)=e-x-x. Show all details of P P

the iterations. Hint: the root is located between 0 and 1.


Solution:
Iter Xi f(X i ) f’(X i )
R X i+1 |ε a (%)|
R R R R R R R

1 0.0 1.0 -2.0 0.5 100.00


2 0.5 0.1065 -1.6065 0.5663 11.71
3 0.5663 0.0013 -1.5676 0.5671 0.15
4 0.5671 0.0000 -1.5676 0.5671 0.00
Numerical Analysis /Lec. 1 - 10 -
Third Class
Example 1.8
Repeat Example 1.7 starting with x o = 5. R R

Solution:
Iter Xi f(X i ) f’(X i )RX i+1 R R R R R |ε a (%)|
R R

1 5.0 -4.9933 -1.0067 0.04016 12351


2 0.04016 0.92048 -1.9606 0.5096 92.12
3 0.5096 0.0911 -1.6007 0.5665 10.04
4 0.5665 0.0010 -1.5675 0.5671 0.000

Example 1.9
Apply Newton-Raphson method to solve Redlich-Kwong equation which used to
estimate the molar volume of saturated vapor of methyl chloride at 333.15 K and
13.76 bar
RT a
P= − 0.5
V − b T V (V + b)
If you know that:-
A=1.5651×108 cm6 bar mol-2 K1/2
P P P P P P P

b=44.891 cm3 mol-1 P P P

R=83.14 cm 3.bar.K-1.mol-1
P P P P P P

Solution
RT a
f (V ) = − 0.5 −P
V − b T V (V + b)
83.14 × 333.15 1.5651 × 108
f (V ) = − − 13.76
V − 44.891 333.150.5V (V + 44.891)
27698 8574764.131
f (V ) = − − 13.76
V − 44.891 V (V + 44.891)
- 27698 8574764.131 8574764.131
f ′(V) = + + 2
(V − 44.891) 2
V(V + 44.891) 2
V (V + 44.891)
It’s better to start with ideal molar volume as initial value of V
RT 83.14 × 333.15
V= = = 2012.943
P 13.76
Iter Vi f(V i ) f’(V i ) V i+1
R R R R R R

1 2012.943 -1.75623 -0.00512 1669.717


2 1669.717 0.291628 -0.00695 1711.673
3 1711.673 0.005731 -0.00668 1712.531
4 1712.531 0.00000231 -0.00667 1712.531
The molar volume equal to 1712.531 cm3 mol-1 P P P P P

Numerical Analysis /Lec. 1 - 11 -


Third Class
Interpolation and Approximation

What is interpolation?
Many times, data is given only at discrete points such as (x 1 , y 1 ), (x 2 , y 2 ),………. (x n ,
R R R R R R R R R R

y n ), (x n+1 , y n+1 ). So, how then does one find the value of y at any other value of x?
R R R R R R

Well, a continuous function f(x) may be used to represent the n+1 data values with f(x)
passing through the n+1 point (Figure 2.1). Then we can find the value of y at any other
value of x. This is called interpolation.
Of course, if x falls outside the range of x for which the data is given, it is no longer
interpolation, but instead, is called extrapolation.
y

(x3 , y3 )

(x1, y1 )

f (x )
(x2 , y2 )
(x0 , y0 )
x

Figure 2.1 Interpolation of discrete data

For n+1 data points, there is one and only one polynomial of order n that passes
through all the points. For example, there is only one straight line (that is, a
first-order polynomial) that connects two points. Similarly, only one parabola
connects a set of three points.
Polynomial Interpolation consists of determining the unique nth order polynomial that P P

fits n+1 data points. This polynomial then provides a formula to compute
intermediate values.
One of the methods used to find this polynomial is called the Lagrange method of
interpolation. Other methods include Newton’s divided difference polynomial method
and the direct method.

Numerical Analysis /Lec. 2 - 12 -


Third Class
2.1 Lagrange Interpolating Polynomial
Consider a function f(x) that passes through the two distinct points (x 0 , f(x 0 )) and (x 1 , R R R R R R

f(x 1 )) as shown in Figure 2.2. The first order polynomial that approximates the
R R

function between these two points can be expressed as


f(x) = a + bx
Where a and b are constants. f(x) can also be written in Lagrangian form as
f(x) = c 0 (x − x 1 ) + c 1 (x − x 0 )
R R R R R R R R

f(x1) f(x2)

f(x) f(x)
f(x1)

f(x0) f(x0)

x0 x1 x x0 x1 x2 x
Figure 2.2 First and second order polynomial approximation.

I. Linear
By weighting the average of the two values used to produce the coordinates of the
line the formula:
x − x2 x − x1
f1 ( x) = L1 f ( x1 ) + L2 f ( x 2 ) where: L1 = L2 =
x1 − x 2 x 2 − x1
Example 2.1
Compute a 4-decimal place value of ln 9.2 from ln 9.0 = 2.1972, ln 9.5 = 2.2513 by
linear Lagrange interpolation and determine the error, using the exact value of ln 9.2
= 2.2192.
Solution:
x 1 = 9.0 , x 2 = 9.5 , f 1 = ln 9.0= 2.1972 , f 2 = ln 9.5= 2.2513 ; hence we get
R R R R R R R R

x − 9.5
L1 ( x) = = −2.0( x − 9.5), L1 (9.2) = −2.0(−0.3) = 0.6
− 0.5
x − 9.0
L2 ( x) = = 2.0( x − 9.0), L2 (9.2) = 2 × 0.2 = 0.4
0.5
ln 9.2 ≈ p 1 (9.2) = L 1 (9.2)f 1 + L 2 (9.2)f 2 = 0.6×2.1972 + 0.4×2.2513 = 2.2188
R R R R R R R R R R

The absolute error is 2.2192 - 2.2188 = 0.0004

Numerical Analysis /Lec. 2 - 13 -


Third Class
II. Quadratic
By weighting the average of the three points that produce the parabola we can derive
the formula:
f 2 ( x) = L1 f ( x1 ) + L2 f ( x2 ) + L3 f ( x3 )
where:
( x − x2 )( x − x3 ) ( x − x1 )( x − x3 ) ( x − x1 )( x − x2 )
L1 = L2 = L3 =
( x1 − x2 )( x1 − x3 ) ( x2 − x1 )( x2 − x3 ) ( x3 − x1 )( x.3 − x2 )
Example 2.2
Compute ln 9.2 from the data in the previous example 2.1 and the additional third
value ln 11.0 = 2.3979.
Solution:
( x − 9.5)( x − 11 .0)
L1 ( x ) = = x 2 − 20.5x + 104.5 ⇒ L1 (9.2) = 0.5400
(9.0 − 9.5)(9.0 − 11 .0)
( x − 9.0)( x − 11 .0) 1
L 2 (x) = =− ( x 2 − 20 x + 99) ⇒ L 2 (9.2) = 0.4800
(9.5 − 9.0)(9.5 − 11 .0) 0.75
( x − 9.0)( x − 9.5) 1
L3 (x) = = ( x 2 − 18.5x + 85.5) ⇒ L 3 (9.2) = −0.0200
(11 .0 − 9.0)(11 .0 − 9.5) 3

ln 9.2 ≈ p 2 (9.2) = 0.5400 × 2.1972 + 0.4800 ×2.2513 − 0.0200×2.3979 = 2.2192.


R R

The absolute error is 2.2192 - 2.2192 = 0.0000

III. General Lagrange Interpolating Polynomial


In general, the Lagrange polynomial can be represented as:
n n
x − xj
f n−1 ( x) = ∑ Li ( x) f ( xi ) where Li ( x) = ∏
i =1 j =1 xi − x j
j ≠i

Example 2.3
Find the Lagrange interpolation polynomial that takes the values prescribed below
xk 0 R 1 2 4
f(x k ) 1 R 1
R 2 5
Solution
3
P 3 (x) =
R R

∑L
k =0
3,k ( x ) f(x k )
R R

( x − 1)( x − 2)( x − 4) ( x − 0)( x − 2)( x − 4)


P 3 (x) = (1) + (1)
(0 − 1)(0 − 2)(0 − 4) (1 − 0)(1 − 2)(1 − 4)
R R

( x − 0)( x − 1)( x − 4) ( x − 0)( x − 1)( x − 2)


+ (2) + (5)
( 2 − 0)( 2 − 1)( 2 − 4) ( 4 − 0)( 4 − 1)( 4 − 2)

Numerical Analysis /Lec. 2 - 14 -


Third Class
When working with grids having large numbers of intervals one typically assigns a
set of low degree (n = 1, 2, or 3) basis functions to each adjacent set of n+1 = 2, 3, or
4 nodes.

Example 2.4
The following table gives the value of density of saturated water for various
temperatures of saturated stream.
Temp oC P P ( = T) 100 150 200 250
Density kg/m3 (= d)P P 958 917 865 799
1) Use third order Lagrange interpolating polynomials to correlate density as a
function of temperature.
2) Find the densities when the temperatures are 130oC . P P

Solution
(T − 150)(T − 200)(T − 250)
L1 = = - 1.3333 × 10-6 T 3 + 4 × 10-3 T 2 - 0.1566T + 10
(100 − 150)(100 − 200)(100 − 250)
(T − 100)(T − 200)(T − 250)
L2 = = 4 × 10-6 T 3 - 2.2 × 10-3 T 2 + 0.38T - 20
(150 − 100)(150 − 200)(150 − 250)
(T − 100)(T − 150)(T − 250)
L3 = = - 4 × 10-6 T 3 + 2 × 10-3 T 2 - 0.31T + 15
(200 − 100)(200 − 150)(200 − 250)
(T − 100)(T − 150)(T − 200)
L4 = = 1.3333 × 10-6 T 3 + 6 × 10-4 T 2 - 0.08666 - 4
(250 − 100)(250 − 150)(250 − 200)

f 4 ( x ) = L1f ( x1 ) + L 2f ( x 2 ) + L3f ( x 3 ) + L 4f ( x 4 ) = - 4 ×10-6 T 3 - 4 ×10-4 T 2 - 0.53T + 1019

f 4 (130) = 934.5520

Example 2.5
Use Lagrange global interpolation by one polynomial and piecewise polynomial
interpolation with quadratic for the following nodes.
xk R 0 1 2 4 5
f(x k ) R0 R 16 48 88 0
Solution
4
Global interpolation by one polynomial: P(x) = ∑L
k =0
4 ,k ( x ) f(x k )
R R

( x − 1)( x − 2)( x − 4)( x − 5) ( x − 0)( x − 2)( x − 4)( x − 5)


P 4 (x) = (0) + (16)
(0 − 1)(0 − 2)(0 − 4)(0 − 5) (1 − 0)(1 − 2)(1 − 4)(1 − 5)
R R

( x − 0)( x − 1)( x − 4)( x − 5) ( x − 0)( x − 1)( x − 2)( x − 5)


+ (48) + (88) + 0
(2 − 0)(2 − 1)(2 − 4)(2 − 5) (4 − 0)(4 − 1)(4 − 3)(4 − 5)
= - 4.6667x 4 + 33.33x 3 - 59.3333x 2 + 46.6667x
Numerical Analysis /Lec. 2 - 15 -
Third Class
Piecewise polynomial interpolation with quadratic
( x − 1)( x − 2) ( x − 0)( x − 2) ( x − 0)( x − 1)
P 2 (x) = (0) + (16) + (48); 0≤x≤2
(0 − 1)(0 − 2) (1 − 0)(1 − 2) ( 2 − 0)( 2 − 1)
R R

= 8x + 8x 2
( x − 4)( x − 5) ( x − 2)( x − 5) ( x − 2)( x − 4)
P 2 (x) = (48) + (88) + (0); 2≤x≤5
( 2 − 4)( 2 − 5) ( 4 − 2)( 4 − 5) (5 − 2)(5 − 4)
R R

= - 280 + 236x - 36x 2

2.1 Newton Divided Difference Interpolating


The Lagrangian interpolation polynomials are useful in discussions on numerical
integration. An alternative in interpolation is ‘Newton’s Divided Difference
Interpolation’. It involves fewer arithmetical operations.
Another advantage of Newton’s rests with the following scenario. Suppose we need to
improve the accuracy and increase the number of grid points. From the forms of
Lagrange interpolation polynomials, all the terms have to be evaluated once again,
and this is a huge amount of work if the number of points is large. Newton’s does not
suffer from this drawback, and just one additional term needs to be computed.

I. Linear Interpolation
Consider the diagram below in which a curve is modeled (poorly) by x1 x 2 :

Using similar triangles the slopes are the same and hence:
f1 ( x) − f ( x1 ) f ( x 2 ) − f ( x1 )
=
x − x1 x 2 − x1

Numerical Analysis /Lec. 2 - 16 -


Third Class
And thus the coordinate on the curve at x 1 can be approximated by rearranging the
R R

above to become:
f ( x 2 ) − f ( x1 )
f 1 ( x) = f ( x1 ) + ( x − x1 )
x 2 − x1

Example 2.6
Estimate the common logarithm of 10 using linear Newton’s interpolation.
(a) Interpolate between log 8 = 0.9030900 and log 12 = 1.0791812.
(b) Interpolate between log 9 = 0.9542425 and log 11 = 1.0413927.
For each of the interpolations, compute the percent relative error based on the true
value.
Solution
U

1.0791812 − 0.90309
a) f1 (10) = 0.90309 + (10 − 8) = 0.991136
12 − 8
1 − 0.991136
εt = × 100% = 0.886%
1
1.0413927 − 0.9542425
b) f1 (10) = 0.9542425 + (10 − 9) = 0.997818
11 − 9
1 − 0.997818
εt = × 100% = 0.218%
1

II. Quadratic Interpolation


To reduce the error, a quadratic interpolation that introduces some curvature into the
interpolation is used. The form:
f 2 ( x) = b1 + b2 ( x − x1 ) + b3 ( x − x1 )( x − x 2 )
Let x = x 1 to produce:
R R

b1 = f ( x1 )
Let x = x 2 and use the previous identity to produce:
R R

f ( x 2 ) − f ( x1 )
f ( x 2 ) = f ( x1 ) + b 2 ( x 2 − x1 ) + b3 ( x − x1 )( x − x 2 ) ⇒ b 2 =
x 2 − x1
And again by substitution of b 1 and b 2 we derive that:
R R R R

f ( x 3 ) − f ( x 2 ) f ( x 2 ) − f ( x1 )

f ( x 2 ) − f ( x1 ) x3 − x 2 x 2 − x1
f ( x 3 ) = f ( x1 ) + ( x 3 − x1 ) + b3 ( x 3 − x1 )( x 3 − x 2 ) ⇒ b3 =
x 2 − x1 x 3 − x1

f ( x3 ) − f ( x2 ) f ( x2 ) − f ( x1 )

f ( x2 ) − f ( x1 ) x3 − x2 x2 − x1
f 2 ( x) = f ( x1 ) + ( x − x1 ) + ( x − x1 )( x − x2 )
x2 − x1 x3 − x1
Numerical Analysis /Lec. 2 - 17 -
Third Class
Example 2.7
Fit a second-order Newton’s Interpolating polynomial to estimate log 10 using the
data from Example 2.7 at x = 8, 9, and 11. Compute the true percent relative error.
Solution
First, order the points
x 1 = 9 f(x 1 ) = 0.9542425
R R R R

x 2 = 11 f(x 2 ) = 1.0413927
R R R R

x 3 = 8 f(x 3 ) = 0.9030900
R R R R

b 1 = 0.9542425
R R

1.0413927 − 0.9542425
b2 = = 0.0435751
11 − 9
0.9030900 − 1.0413927
− 0.0435751
8 − 11 0.0461009 − 0.0435751
b3 = = = −0.0025258
8−9 8−9
Substituting these values yields the quadratic formula
f 2 ( x) = 0.9542425 + 0.0435751( x − 9) − 0.0025258( x − 9)( x − 11)
which can be evaluated at x = 10 for
f 2 (10) = 0.9542425 + 0.0435751(10 − 9) − 0.0025258(10 − 9)(10 − 11) = 1.0003434
1 − 1.0003434
εt = × 100% = 0.03434%
1

III. General form of Newton Divided Difference Interpolating Polynomial


In general, if we find the finite differences defined as:
f (x i ) − f (x j )
f [x i , x j ] =
xi − x j
f [x i , x j ] − f [x j , x k ]
f [x i , x j , x k ] =
xi − xk

f [ x n , x n −1 ,..., x 2 ] − f [ x n −1 , x n −2 ,..., x1 ]
f [ x n , x n −1 ,..., x1 ] =
x n − x1
Then the general Newton Interpolating Polynomial of order n – 1with n data points is
defined as:

f n −1 ( x ) = b1 + b 2 ( x − x 1 ) + b 3 ( x − x 1 )( x − x 2 ) + .... + b n ( x − x 1 )( x − x 2 )...( x − x n )

Numerical Analysis /Lec. 2 - 18 -


Third Class
b1 = f ( x1 )
b2 = f [ x 2 , x1 ]
Where b3 = f [ x3 , x 2 , x1 ]

bn = f [ x n , x n −1 ,..., x1 ]

b0
x0 f ( x0 ) b1
f [ x1 , x0 ] b2
x1 f ( x1 ) f [ x 2 , x1 , x0 ] b3
f [ x 2 , x1 ] f [ x3 , x 2 , x1 , x0 ]
x2 f ( x2 ) f [ x3 , x 2 , x1 ]
f [ x3 , x 2 ]
x3 f ( x3 )

For an example of a third order polynomial, given ( x0 , y 0 ), ( x1 , y1 ), ( x 2 , y 2 ), and ( x3 , y 3 ),

f 3 ( x) = f [ x0 ] + f [ x1 , x0 ]( x − x0 ) + f [ x 2 , x1 , x0 ]( x − x0 )( x − x1 )
+ f [ x3 , x 2 , x1 , x0 ]( x − x0 )( x − x1 )( x − x 2 )

Example 2.8
The following table gives the value of density of saturated water for various temperatures of saturated
stream.
Tempo C ( = T) P :P 100 150 200 250 300
Density kg/m3 (= d) : P P 958 917 865 799 712
Using Newton divided difference interpolating find the densities when the temperatures are
130oC and 275oC respectively.
P P P P

Solution
i T D f[x i+1 ,x i ] f[x i+2 ,x i+1 ,x i ] f[x i+3 ,x i+2 ,x i+1 ,x i ] f[x i+4 ,x i+3 ,x i+2 ,x i+1 ,x i ]
R R R R R R R R R R R R R R R R R R R R R R R R R R R R

1 100 958
-0.8200
2 150 917 -0.0022
-1.0400 -4×10-6 P P

3 200 865 -0.0028 -2.6667×10-8 P P

-6
-1.3200 -9.333×10 P P

4 250 799 -0.0042


-1.7400
5 300 712
Numerical Analysis /Lec. 2 - 19 -
Third Class
P4 = 958 - 0.8200 × (T - 100) - 0.0022 × (T - 100) * (T - 150) - 4 × 10-6 × (T - 100) * (T - 150) * (T - 200)
- 2.6667 × 10-8 × (T - 100) * (T - 150) * (T - 200) * (T - 250)

P4 = 999 - 0.0167T - 0.0051T 2 + 1.4667 × 10-5 T 3 - 2.6667 × 10-8 T 4

P4 (130) = 934.6864 kg/m3

P4 (275) = 758.7187 kg/m3

Or by direct substitution
P4 (130) = 934.6864 kg/m3

P(130)= 958-0.82×(130-100)-0.0022×(130-100)×(130-150) -4×10-6×(130-100) × P P

(130-150) ×(130-200) -2.6667×10-8×(130-100) ×(130-150)×(130-200) ×(130-250)


P P

= 934.6864 kg/m3 P

P(275)=958-0.82×(275-100)-0.0022×(275-100)×(275-150)-4×10-6×(275-100) P P

×(275-150) ×(275-200) -2.6667×10-8 ×(275-100)×(275-150)×(275-200) ×(275-250)


P P

= 758.7188 kg/m3 P

Numerical Analysis /Lec. 2 - 20 -


Third Class
Curve Fitting

What is regression?
Regression analysis gives information on the relationship between a response variable
and one or more independent variables to the extent that information is contained in the
data. The goal of regression analysis is to express the response variable as a function of
the predictor variables.
Once regression analysis relationship is obtained, it can be used to predict values of the
response variable, identify variables that most affect response, or verify hypothesized
casual models of the response.

3.1 Linear regression


Linear regression is the most popular regression model. In this model we wish to
predict response to n data points (x 1 ,y 1 ), (x 2 ,y 2 ), ....., (x n , y n ) data by a regression model
R R R R R R R R R R R R

given by.
y = a0 + a1 x

Where a 0 and a 1 are the constants of the regression model.


R R R R

A measure of goodness of fit, that is, how a0 + a1 x predicts the response variable y is

the magnitude of the residual, ε i at each of the n data points.

ε i = y i − (a 0 + a1 xi )

Ideally, if all the residuals ε i are zero, one may have found an equation in which all

the points lie on the model. Thus, minimization of the residual is an objective of
obtaining regression coefficients.
The most popular method to minimize the residual is the least squares method, where
the estimates of the constants of the models are chosen such that the sum of the squared
n
residuals is minimized, that is minimize ∑ ε i 2 .
i =1

Let us use the least squares criterion where we minimize


n n 2

S r = ∑ ε i = ∑ ( yi − a0 − a1 xi )
2

i =1 i =1

S r is called the sum of the square of the residuals.


R R

Numerical Analysis /Lec. 3 - 21 -


Third Class
y

xi , y i

ε i = yi − a0 − a1 xi xn , y n

x2 , y 2
x3 , y 3

x1 , y1 y = a0 + a1 x

Figure 3.1 Linear regression of y vs. x data showing residuals at a typical point, x i . R R

To find a 0 and a 1 , we minimize S r with respect to a 0 and a 1 :


R R R R R R R R R R

∂S r n
= 2∑ ( yi − a0 − a1 xi )(− 1) = 0
∂a0 i =1

∂S r n
= 2∑ ( yi − a0 − a1 xi )(− xi ) = 0
∂a1 i =1

Giving
n n n
− ∑ yi + ∑ a0 + ∑ a1 xi = 0
i =1 i =1 i =1
n n n
− ∑ y i x i + ∑ a 0 x i + ∑ a1 x i2 = 0
i =1 i =1 i =1
n
Noting that ∑a
i =1
0 = a 0 + a 0 + . . . + a 0 = na 0
n n
na 0 + a1 ∑ xi =∑ y i (3.1)
i =1 i =1
n n n
a 0 ∑ x i + a1 ∑ x i2 = ∑ x i y i (3.2)
i =1 i =1 i =1

Solving the above equations gives:


n n n
n∑ x i y i −∑ x i ∑ y i
i =1 i =1 i =1
a1 = 2
n
 n
n∑ x − ∑ x i  2
i
i =1  i =1 
n n n n

∑ xi2 ∑ y i − ∑ xi ∑ xi y i
i =1 i =1 i =1 i =1
a0 = 2
 n
 n
n∑ x i2 − ∑ x i 
i =1  i =1 
Or from equation (3.2)
Numerical Analysis /Lec. 3 - 22 -
Third Class
n n

∑ yi a1 ∑ xi
ao = i =1
− i =1
= y − a1 x
n n

Example 3.1
The following y vs. x data is given
x 1 7 13 19 25
y 1 49 169 361 625

y vs x

800
600
y 400
200
0
0 5 10 15 20 25 30
x

Figure 3.1 Data points of the y vs x data

Although y = x is an exact fit to the data, a scientist thinks that y = a0 + a1 x can


2

explain the data. Find constants of the model, a0 , and a1 ,

Solution
First find the constants of the assumed model
y = a 0 + a1 x

a 0 = y − a1 x

n=5
n 5

∑x y = ∑x y
i =1
i i
i =1
i i = 1 × 1 + 7 × 49 + 13 × 169 + 19 × 361 + 25 × 625 = 25025
n 5

∑x
i =1
2
i = ∑ xi2 = 12 + 7 2 + 13 2 + 19 2 + 25 2 = 1205
i =1
n 5

∑y =∑y
i =1
i
i =1
i = 1 + 49 + 169 + 361 + 625 = 1205

Numerical Analysis /Lec. 3 - 23 -


Third Class
n 5

∑ xi = ∑ xi = 1 + 7 + 13 + 19 + 25 = 65
i =1 i =1
n n n
n∑ x i y i −∑ x i ∑ y i
i =1 i =1 i =1
a1 = 2
 n
 n
n∑ x − ∑ x i  2
i
i =1  i =1 
_ _
a 0 = y − a1 x

5(25025) − (65)(1205)
a1 = = 26
5(1205) − (65)
2

= (241) − 26(13)
1205 65
a 0 = y − a1 x = − 26 = −97
5 5
This gives
y = a 0 + a1 x
y = −97 + 26 x

Example 3.2
U

The following table gives the value of density of saturated water for various
temperatures of saturated stream.
TempoC ( = T) P P 100 150 200 250 300
Density kg/m3 (= D) P P 958 917 865 799 712
a) Use curve fitting to fit the results to a first-order polynomial D = A + B T .
b) Find the densities when the temperatures are 130oC and 275oC respectively. P P P P

Solution:
U

a 0 and a 1 can be computed by constructing the following table:


R R R R

Ti Di Ti2 RTi Di R R RP R R R

100 958 10000 95800


150 917 22500 137550
200 865 40000 173000
250 799 62500 199750
300 712 90000 213600
∑1000 4251 225000 819700
5 × 819700 −1000 × 4251
a1 = = - 1.22
5 × 225000 − (1000) 2
4251 1000
a0 = − a1 = 1094.2
5 5
D=1094.2-1.22×T
Numerical Analysis /Lec. 3 - 24 -
Third Class
To compare the predicted values to the experimental values:

Ti
R D i (estimated)
Di R R R

D=1094.2-1.22×T
100 958 972.2
150 917 911.2
200 865 850.2
250 799 789.2
300 712 728.2
D(130)= 1094.2-1.22×130=935.6
D(175)= 1094.2-1.22×175=880.7

3.2 Polynomial Models


Given N data points (x 1 , y 1 ), (x 2 , y 2 ). . , (x N , y N ) use least squares method to regress
R R R R R R R R R R R R

the data to an nth order polynomial.


P P

In the development, we use n as the degree of the polynomial and N as the number of
data pairs ( xi , yi ). We will always have N > n + 1 in the following.
Assume the functional relationship for fitting
Y ( x ) = a0 + a1 x + a2 x 2 +  + an x n
with errors defined by
ei = yi − Y ( xi ) = yi − a0 − a1 xi − a2 xi 2 −  − an xi n ,
in which i = 1, 2, 3,…, N.
We minimize the sum of error squares,
N N
=
S ∑ e=
i
=i 1 =i 1
2
∑( y i − a0 − a1 xi − a2 xi 2 −  − an xi n ) 2 .

At the minimum, all the first partial derivatives with respect to ai ’s vanish. We have
∂S N
=0 =2∑ ( yi − a0 − a1 xi − a2 xi 2 −  − an xi n )( −1) ,
∂a0 i =1

∂S N
=0 =2∑ ( yi − a0 − a1 xi − a2 xi 2 −  − an xi n )( − xi ) ,
∂a1 i =1

∂S N
=0 =2∑ ( yi − a0 − a1 xi − a2 xi 2 −  − an xi n )( − xi 2 ) ,
∂a2 i =1


∂S N
=0 =2∑ ( yi − a0 − a1 xi − a2 xi 2 −  − an xi n )( − xi n ) ,
∂an i =1

Rearrange them to get


N N N N
a0 N + a1 ∑ xi + a2 ∑ xi 2 +  + an ∑ xi n =
∑ yi ,
=i 1 =i 1 =i 1 =i 1

Numerical Analysis /Lec. 3 - 25 -


Third Class
N N N N N
a0 ∑ xi + a1 ∑ xi 2 + a2 ∑ xi 3 +  + an ∑ xi n +1 =
∑ xi yi ,
=i 1 =i 1 =i 1 =i 1 =i 1
N N N N N
a0 ∑ xi 2 + a1 ∑ xi 3 + a2 ∑ xi 4 +  + an ∑ xi n + 2 =
∑ xi 2 yi ,
=i 1 =i 1 =i 1 =i 1 =i 1


N N N N N
a0 ∑ xi + a1 ∑ xi
n n +1
+ a2 ∑ xi n+2
+  + an ∑ xi 2n
∑ xi n yi ,
=
=i 1 =i 1 =i 1 =i 1 =i 1

or, in matrix form,


 N N N
n   N 
 N ∑ xi ∑ xi 2
 ∑ xi   ∑ yi 
 =i 1 =i 1 =i 1
 =  i1 
 n +1   0   N 
N N N N a
 ∑ xi ∑ ∑  ∑ xi     ∑ xi yi 
2 3
xi xi
a

=i 1 =i 1 =i 1 =i 1
  1=  i1 
 N N N N
  a  =  N
. (3.3)
 ∑ xi ∑ ∑ xi 4  ∑ xi n + 2   2   ∑ xi 2 yi 
2
xi 3


=i 1 =i 1 =i 1 =i 1
    i =1 
    an    
N N N N  N 
 ∑ xi n ∑ xi n +1 ∑ xi n + 2  ∑ xi 2 n   ∑ xi n yi 
= i 1 =i 1 =i 1 =i 1   i =1 

Equations (3.3) represent a linear system. However, this system is usually


ill-conditioned and round-off errors can distort the solution of ai ’s. Up to degree-3 or
4, the problem is not too great. It is very infrequent to use a degree higher than 4.

Example 3.3
Rotameter calibration data (flow rate versus Rotameter reading) are as follows:
Rotameter Reading R 10 30 50 70 90
Flow rate V(L/min) 20 52.1 84.6 118.3 151
a) Using curve fitting to fit the calibration data to second order polynomial.
b) Calculate the flowrate (V) at rotameter reading R=73.
Solution:
a) 2nd order polynomial
P P

n n
S r = ∑ ε = ∑ ( yi − ao − a1 xi − a2 xi ) 2
i
2

i =1 i =1
n
dS r
= 2∑ ( yi − ao − a1 xi − a2 xi2 ) × (−1) = 0
dao i =1
n
dS r
= 2∑ ( yi − ao − a1 xi − a2 xi2 ) × (− xi ) = 0 (1)
da1 i =1
n
dS r
= 2∑ ( yi − ao − a1 xi − a2 xi2 ) × (− xi2 ) = 0
da2 i =1

Re arranging above equations

Numerical Analysis /Lec. 3 - 26 -


Third Class
n n n
ao n + a1 ∑ xi + a2 ∑ xi2 = ∑ yi
i =1 i =1 i =1
n n n n
ao ∑ xi + a1 ∑ xi2 + a2 ∑ xi3 = ∑ xi yi (2)
i =1 i =1 i =1 i =1
n n n n
ao ∑ xi2 + a1 ∑ xi3 + a2 ∑ xi4 = ∑ xi2 yi
i =1 i =1 i =1 i =1

Making required table


R V R2
P R3
P
R4P
RV R2y
P P

10 20 100 1000 10000 200 2000


30 52.1 900 27000 810000 1563 46890
50 84.6 2500 125000 6250000 4230 211500
70 118.3 4900 343000 2401000 8281 579670
90 151 8100 729000 6561000 13590 1223100
∑ 250 426 16500 1225000 9669000 27864 2063160
By substitution in equation 2
5a o + 250a1 + 16500a 2 = 426
250a o + 16500a1 + 1225000a 2 = 27864
16500a o + 1225000a1 + 96690000a 2 = 2063160
Solving above equation simultaneously gives;
ao =3.8786 , a1 =1.5981 , a2 = 4.2857×10-4 P

then
V = 3.8786 + 1.5981 × R + 4.2857 ×10-4 × R 2

B)
V (73) = 3.8786 + 1.5981 × 73 + 4.2857 × 10-4 × 732 = 122.83

Numerical Analysis /Lec. 3 - 27 -


Third Class
3.3 Nonlinear Data
Whenever data from experimental tests are not linear, we need to fit to them some
function other than a first-degree polynomial. Popular forms that are tried are the
power form
y = ax b
or the exponential form
y = aebx .
Since such nonlinear equations are much more difficult to solve than linear equations,
they are usually linearized by taking logarithms before determining the parameters:
ln=y ln a + b ln x ,
or
ln=y ln a + bx .

linear non-linear
Figure 3.2 Linear vs non-linear data

In cases when such linearization of the function is not desirable, or when no method
of linearization can be discovered, graphical methods are frequently used; one merely
plots the experimental values and sketches in a curve that seems to fit well.

Example 3.4
The progress of a homogeneous chemical reaction is followed and it is desired to
evaluate the rate constant and the order of the reaction. The rate law expression for
the reaction is known to follow the power function form − r = kC n
Use the data provided in the table to obtain n and k .
C A (gmol/l) 4 2.25 1.45 1.0 0.65 0.25 0.006
− rA ( gmol/l ⋅ s) 0.398 0.298 0.238 0.198 0.158 0.098 0.048

Solution
Taking the natural log of both sides of Equation, we obtain
ln (− r ) = ln (k ) + n ln (C )
Numerical Analysis /Lec. 3 - 28 -
Third Class
Let
z = ln (− r )
w = ln (C )
a 0 = ln(k ) implying that k = e a0
a1 = n
We get
z = a 0 + a1 w
This is a linear relation between z and w , where
n n n
n∑ wi z i − ∑ wi ∑ z i
a1 = i =1 i =1 i =1
2

n
 n
n∑ w −  ∑ wi 
2
i
i =1  i =1 

 n   n 
 ∑ zi   ∑ wi 
a 0 =  i =1 − a  i =1 
 n  1 n 
   
   
Table: Kinetics rate law using power function
i C −r w z w× z w2
1 4 0.398 1.3863 -0.92130 -1.2772 1.9218
2 2.25 0.298 0.8109 -1.2107 -0.9818 0.65761
3 1.45 0.238 0.3716 -1.4355 -0.5334 0.13806
4 1 0.198 0.0000 -1.6195 0.0000 0.00000
5 0.65 0.158 -0.4308 -1.8452 0.7949 0.18557
6 0.25 0.098 -1.3863 -2.3228 3.2201 1.9218
7 0.006 0.048 -5.1160 -3.0366 15.535 26.173
7

∑i =1
-4.3643 -12.391 16.758 30.998

n=7
7

∑w
i =1
i = −4.3643

∑z
i =1
i = −12.391

∑w z
i =1
i i = 16.758

∑w
i =1
2
i = 30.998

From above equations

Numerical Analysis /Lec. 3 - 29 -


Third Class
7 × (16.758) − (− 4.3643) × (− 12.391)
a1 =
7 × (30.998) − (− 4.3643)
2

= 0.31943
− 12.391 − 4.3643
a0 = − (.31943)
7 7
= −1.5711
Then
k = e −1.5711
= 0.20782
n = a1
= 0.31941
Finally, the model of progress of that chemical reaction is
− r = 0.20782 × C 0.31941

Example 3.5
It is suspected from theoretical considerations that the rate of water flow from a
firehouse is proportional to some power of the nozzle pressure. Assume pressure data
is more accurate. You are transforming the data.
Flow rate, F (gallons/min) 96 129 135 145 168 235
Pressure, p (psi) 11 17 20 25 40 55
What is the exponent b of the nozzle pressure in the regression model F = ap
b

Numerical Analysis /Lec. 3 - 30 -


Third Class
Solution
The linearization of the above data is done as follows.
F = ap b
ln( F ) = ln(a ) + b ln( p )
z = a 0 + bx
Where
z = ln (F )
x = ln( p)
a0 = ln (a )
Implying
a = e a0
There is a linear relationship between z and x.
Linear regression constants are given by
n n n
n∑ xi zi −∑ xi ∑ zi
b= i =1 i =1 i =1
2
n
  n
n∑ x −  ∑ xi 
2
i
i =1  i =1 
n n n n

∑x ∑z −∑x ∑x z
2
i i i i i
a0 = i =1 i =1 i =1 i =1
2
n
 n 
n∑ xi2 −  ∑ xi 
i =1  i =1 
Since
n=6
6

∑x z
i =1
i i = ln(11) × ln(96) + ln(17) × ln(129) + ln(20) × ln(135) + ln(25) × ln(145) + ln(40) × ln(168)

+ ln(55) × ln(235) = 96.208


6

∑x
i =1
i = ln(11) + ln(17) + ln(20) + ln(25) + ln(40) + ln(55) = 19.142
6

∑z
i =1
i = ln(96) + ln(129) + ln(135) + ln(145) + ln(168) + ln(235) = 29.890
6

∑x
i =1
2
i = (ln(11)) 2 + (ln(17)) 2 + (ln(20)) 2 + (ln(25)) 2 + (ln(40)) 2 + (ln(55)) 2 = 62.779

then
6 × 96.208 − 19.142 × 29.890
b=
6 × 62.779 − 19.1422
577.25 − 572.15
=
376.67 − 366.41
= 0.49721

Numerical Analysis /Lec. 3 - 31 -


Third Class
Example 3.6
The following data have been obtained for the decomposition of benzene diazonium
chloride to chlorobenzene:
T (K) 313 319 323 328 333
-1
k (s )
P P 0.0043 0.0103 0.018 0.0355 0.0717
From this data, determine the pre-exponential factor A and activation energy E,
assuming that the rate constant follows an Arrhenius form.
−E
k = A exp 
 RT 
Solution:
E
ln k = ln A −
RT
y = ln k
x = 1/ T
ao = ln A
−E
a1 =
R
We get
y = ao + a1 x
T (K) k (s-1)P P
x=1/T y=ln k x2
P
xy
313 0.0043 0.00319 -5.44914 1.02073e-05 -0.01741
319 0.0103 0.00313 -4.57561 9.82695e-06 -0.01434
323 0.018 0.00310 -4.01738 9.58506e-06 -0.01244
328 0.0355 0.00305 -3.33822 9.29506e-06 -0.01018
333 0.0717 0.00300 -2.63526 9.01803e-06 -0.00791
∑ 0.01548 -20.0156 4.79324e-05 -0.06228
n n n
n∑ xi yi −∑ xi ∑ yi
0.08
a1 = i =1 i =1 i =1
2
= - 14612 k Exp.
n
 n 
n∑ x −  ∑ xi 
2 0.07 k Pred.
Reaction rate constant (k)

i
i =1  i=1  0.06
ao = y − a1 x = 41.2272 0.05

0.04
ao = ln A = 41.2272 ⇒
A = exp(40.2272) = 8.0303 × 1017 0.03

a1 = − E / R ⇒ 0.02
E = −a1 × R = −(−14612) × 8.314 = 121480 0.01

0
310 315 320 325 330 335
Temperature (K)

Numerical Analysis /Lec. 3 - 32 -


Third Class
Numerical Integration
Numerical Integration Approximation.
Integration is the process of measuring the area under a function plotted on a graph.
Sometimes, the evaluation of expressions involving these integrals can become
daunting, if not indeterminate. For this reason, a wide variety of numerical methods
have been developed to find the integral.
Here we discuss six different methods for approximating the value of a definite
integral. Each method revolves around associating a definite integral with area under a
curve. The first three use areas of rectangles, the fourth uses areas of trapezoids, and
the final approximation technique uses areas of shapes that include a portion of a
parabola.

4.1 Left-Endpoint Approximation


On each of the four subintervals shown below, we create a rectangle whose width is the
length of the subdivision and whose height is determined by the function value at the
left endpoint of each subdivision.

width: ∆x, height: f(x0) = f(0)


width: ∆x, height: f(x1) = f(1/4)
width: ∆x, height: f(x2) = f(1/2)
y = f ( x) = e − x
2
width: ∆x, height: f(x3) = f(3/4)

The sum of the areas of the four rectangles represents our approximation for the area
under the curve and therefore represents an approximation for the value of the definite
integral:

Numerical Analysis /Lec. 4 - 33 -


Third Class
1

∫ e dx ≈ ∆x ⋅ f ( x0 ) + ∆x ⋅ f ( x1 ) + ∆x ⋅ f ( x2 ) + ∆x ⋅ f ( x3 )
−x 2

0
≈ ∆x( f ( x0 ) + f ( x1 ) + f ( x2 ) + f ( x3 ) )
3
≈ ∆x ∑ f ( xi )
i =0

This same sequence of steps can be generalized for left-endpoint approximation of the
b
definite integral ∫ f ( x)dx using n subdivisions:
a

∫ f ( x)dx ≈ ∆x ⋅ f ( x0 ) + ∆x ⋅ f ( x1 ) +  + ∆x ⋅ f ( xn − 2 ) + ∆x ⋅ f ( xn −1 )
a
≈ ∆x( f ( x0 ) + f ( x1 ) +  + f ( xn − 2 ) + f ( xn −1 ) )
n −1
≈ ∆x ∑ f ( xi )
i =0

4.2 Right-Endpoint Approximation


Again we create rectangles whose widths are each the length of a subdivision, but here
each height is determined by the function value at the right endpoint of each
subinterval.

width: ∆x, height: f(x1) = f(1/4)

width: ∆x, height: f(x2) = f(1/2)


− x2
y = f ( x) = e width: ∆x, height: f(x3) = f(3/4)
width: ∆x, height: f(x4) = f(1)

Numerical Analysis /Lec. 4 - 34 -


Third Class
The sum of the areas of these four rectangles represents a right-endpoint approximation
for the area under the curve and therefore is an approximation for the value of the
definite integral:
1

∫ e dx ≈ ∆x ⋅ f ( x1 ) + ∆x ⋅ f ( x2 ) + ∆x ⋅ f ( x3 ) + ∆x ⋅ f ( x4 )
−x2

0
≈ ∆x( f ( x1 ) + f ( x2 ) + f ( x3 ) + f ( x4 ) )
4
≈ ∆x ∑ f ( xi )
i =1

This same sequence of steps can be generalized for right-endpoint approximation of


b
the definite integral ∫ f ( x)dx using n subdivisions:
a

∫ f ( x)dx ≈ ∆x ⋅ f ( x1 ) + ∆x ⋅ f ( x2 ) +  + ∆x ⋅ f ( xn −1 ) + ∆x ⋅ f ( xn )
a
≈ ∆x( f ( x1 ) + f ( x2 ) +  + f ( xn −1 ) + f ( xn ) )
n
≈ ∆x ∑ f ( xi )
i =1

4.3 Midpoint Approximation


For a third time we create rectangles each of whose width is the length of the
subdivision, but now each height is determined by the function value at the midpoint of
each subdivision.

width: ∆x, height: f((x0+x1)/2) = f(1/8)

width: ∆x, height: f((x1+x2)/2) = f(3/8)

y = f ( x) = e − x
2
width: ∆x, height: f((x2+x3)/2) = f(5/8)

width: ∆x, height: f((x3+x4)/2) = f(7/8)

Numerical Analysis /Lec. 4 - 35 -


Third Class
The sum of the areas of these four rectangles represents a midpoint approximation for
the area under the curve and therefore is another approximation for the value of the
definite integral:

x +x  x +x  x +x  x +x 
1

∫e
− x2
dx ≈ ∆x ⋅ f  0 1  + ∆x ⋅ f  1 2  + ∆x ⋅ f  2 3  + ∆x ⋅ f 3 4
0  2   2   2   2 

 x +x   x + x2   x + x3   x + x4  
≈ ∆x f  0 1  + f  1 + f 2 + f 3 
  2   2   2   2 
3
 x + xi +1 
≈ ∆x ∑ f  i 
i =0  2 

This same sequence of steps can be generalized for midpoint approximation of the
b
definite integral ∫ f ( x)dx using n subdivisions:
a

  x + x1     x + x2  
b

∫ f ( x)dx ≈  ∆x ⋅

f 0
 2  
  +  ∆x ⋅ f 1
 2 
 + 
a

  x + xn     x + xn+1  
+  ∆x ⋅ f  n−1   +  ∆x ⋅ f  n 
  2    2 
  x + x1   x + x2   x + xn   x + xn+1  
≈ ∆x f  0 + f 1  +  + f  n−1 + f n 
  2   2   2   2 
n −1
 x + xi +1 
≈ ∆x ∑ f  i 
i =0  2 

Numerical Analysis /Lec. 4 - 36 -


Third Class
4.4 Trapezoidal Rule
Trapezoidal rule is based on the Newton-Cotes formula that if one approximates the
integrand by an nth order polynomial, then the integral of the function is approximated
P P

by the integral of that nth order polynomial. Integrating polynomials is simple and is
P P

based on the calculus formula. The height of each trapezoid is the length of the
subdivision. The two bases of each trapezoid correspond to the values of the function at
the endpoints of the subinterval on which the trapezoid has been drawn.
height: ∆x, bases: f(1/4) and f(1/2)

− x2 height: ∆x, bases: f(0) and f(1/4)


y = f ( x) = e
height: ∆x, bases: f(1/2) and f(3/4)

height: ∆x, bases: f(3/4)and f(1)

It may be useful to remove the first of these trapezoids and rotate it into a more
conventional orientation as we calculate its area.
base length: f(0)

height:∆x Area = 1/2(∆x) (f(0) + f(1/4))

base length: f(1/4)

The sum of the areas of these four trapezoids represents an approximation for the area
under the curve and therefore is one more approximation for the value of the definite
integral:

Numerical Analysis /Lec. 4 - 37 -


Third Class
1  1 
1

∫e
−x 2
dx ≈  ∆x ⋅ (f ( x 0 ) + f ( x 1 ) ) +  ∆x ⋅ (f ( x 1 ) + f ( x 2 ) ) +
0 2  2 
1  1 
 ∆x ⋅ (f ( x 2 ) + f ( x 3 ) ) +  ∆x ⋅ (f ( x 3 ) + f ( x 4 ) )
2  2 
≈ ∆x (f ( x 0 ) + 2f ( x 1 ) + 2f ( x 2 ) + 2f ( x 3 ) + f ( x 4 ) )
1
2
3
≈ ∆x ∑ (f ( x i ) + f ( x i+1 ) )
1
2 i =0

This same sequence of steps can be generalized for trapezoid approximation of the
b
definite integral ∫ f ( x)dx using n subdivisions:
a

1  1 
b

∫ f ( x )dx ≈  2 ∆x ⋅ (f ( x
a
) + f ( x 1 ) ) +  ∆x ⋅ (f ( x 1 ) + f ( x 2 ) ) + 
0
 2 
1  1 
+  ∆x ⋅ (f ( x n −1 ) + f ( x n −1 ) ) +  ∆x ⋅ (f ( x n −1 ) + f ( x n ) )
2  2 
≈ ∆x (f ( x 0 ) + 2f ( x 1 ) + 2f ( x 2 ) + 
1
2
+ 2f ( x n − 2 ) + 2f ( x n −1 ) + f ( x n ) )

Single Segment Trapezoidal Rule


b

∫ f ( x)dx ≈ 2 ∆x( f ( xi ) + f ( xi +1 ))
1
a

Multiple Segments Trapezoidal Rule


1 n −1
b

∫ f ( x)dx ≈ ∆x ∑ ( f ( xi ) + f ( xi +1 ) )
a
2 i =0

Example 4.1
1
dx
Evaluate the integral I = ∫ by trapezoidal rule dividing the interval [0, 1] into
0 1 + x2
five equal parts.
Solution

Numerical Analysis /Lec. 4 - 38 -


Third Class
n=5
1− 0
∆x = = 0/2
5

x 0 0.2 0.4 0.6 0.8 1.0

1
1.0 0.98058 0.92848 0.85749 0.78087 0.70711
1 + x2

From Trapezoidal Rule;


∆x
I= [ f ( x1 ) + 2( f ( x2 ) + f ( x3 ) + f ( x4 ) + f ( x5 )) + f ( x6 )]
2
0.2
= [1 + 2(0.98058 + 0.92848 + 0.85749 + 0.78087) + 0.70711]
2
= 0.88016

Example 4.2
300 x
Use Multiple-segment Trapezoidal Rule to find the area under the curve f ( x) =
1+ ex
from x = 0 to x = 10 .

Solution
Using two segments, we get
10 − 0
∆x = =5
2
300(0)
f (0) = =0
1 + e0
300(5)
f (5) = = 10.039
1 + e5
300(10)
f (10) = = 0.136
1 + e10

Area =
5
[ f (0) + 2 f (5) + f (10)] = 5 [0 + 2(10.039) + 0.136] = 50.535
2 2
10
300 x
So what is the true value of this integral? ∫1+ e
0
x
dx = 246.59

246.59 − 50.535
Making the relative true error ∈t = × 100% = 79.506 %
246.59

Numerical Analysis /Lec. 4 - 39 -


Third Class
10
300 x
Table: Values obtained using Multiple-segment Trapezoidal Rule for ∫ 1 + e x dx
0

n Approximate Et ∈t
Value
1 0.681 245.91 99.724%
2 50.535 196.05 79.505%
4 170.61 75.978 30.812%
8 227.04 19.546 7.927%
16 241.70 4.887 1.982%
32 245.37 1.222 0.495%
64 246.28 0.305 0.124%

Example 4.3
The average values of a function can be determined by:-
T2

∫ CpdT
Cpmh =
T1

T2 − T1

C p = 0.99403 +1.617×10-4T+9.7215×10-8T2 – 9.5838 × 10-11 T3 + 1.9520 × 10-14


R R P P P P P P P P P P P P

4
TP

C p in KJ/(Kg K)
R R

Use this relationship to verity the average value of specific heat of dry air in the
range from 300 K to 450 K:
1) Analytically
2) Numerically using five points Trapezoidal Rule
Solution
450

∫ 0.99403 + 1.617 ×10


-4
T + 9.7215 × 10-8 T 2 - 9.5838 × 10-11 T 3 + 1.9520 × 10-14 T 4 dT
1) Cpmh = 300
450 − 300

450
1.617 × 10-4 2 9.7215 × 10-8 3 9.5838 × 10-11 4 1.9520 × 10-14 5
0.99403 T + T + T - T + T
2 3 4 5
Cpmh = 300
450 − 300
465.73 - 306.18
Cpmh = = 1.0637
450 − 300

Numerical Analysis /Lec. 4 - 40 -


Third Class
450 - 150
2) ∆T = = 37.5
4

T 300 337.5 375 412.5 450


Cp 1.0489 1.0562 1.0637 1.0711 1.0785
(dT/2) * (Cp(1) + 2 * (Cp(2) + Cp(3) + Cp(4)) + Cp(5))
Cpmh =
T 2−T1
(37.5/2) * (1.0489 + 2 * (1.0562 + 1.0637 + 1.0711) + 1.0785)
= = 1.0637
450 − 300

Analytical Solution - Numerical Solution 1.0637 - 1.0637


Error % = %= % = 0%
Analytical Solution 1.0637

4.5 Simpson’s Rule (1/3 Simpson’s Rule)


The final approximation technique we develop in this section is called Simpson’s Rule.
It is different from the first four methods because we are not creating polygons on each
subinterval but rather we create a figure with a non-straight component to it. For this
method, it is required that the number of subintervals be an even number.

A parabola is created that contains the


points (x0,f(x0)), (x1,f(x1)), and (x2,f(x2)).

y = f ( x) = e − x
2

Another parabola is
created that contains
the points (x2,f(x2)),

Simpson’s Rule uses pairs of subdivisions and creates over each pair a parabola that
contains the points (x 2i-2 , f(x 2i-2 )), (x 2i-1 , f(x 2i-1 )), and (x 2i , f(x 2i )) for i going from 1 to n/ 2 .
R R R R R R R R R R R R P P R R

A shape is created using the resulting parabola, two vertical segments—one from
Numerical Analysis /Lec. 4 - 41 -
Third Class
(x 2i-2 ,0) to (x 2i–2 , f(x 2i-2 )) and one from (x 2i+2 ,0) to (x 2i+2 , f(x 2i+2 ))—and the segment on
R R R R R R R R R R R R

the x-axis with endpoints (x 2i-2 ,0) and (x 2i+2 ,0). The area of the resulting shape—such
R R R R

as of the red-shaded figure above or the green-shaded figure above-is calculated using

the formula ∆x ⋅ ( f ( x2i −1 ) + 4 f ( x2i ) + f ( x2i +1 ) ).


1
3
The sum of the areas of these shapes represents an approximation for the area under the
curve and therefore is an approximation for the value of the definite integral:
1
   
dx ≈  ∆x ⋅ ( f ( x0 ) + 4 f ( x1 ) + f ( x2 ) ) +  ∆x ⋅ ( f ( x2 ) + 4 f ( x3 ) + f ( x4 ) )
1 1
∫e
− x2

0  3   3 

This same sequence of steps can be generalized for the Simpson’s Rule approximation
b
of the definite integral ∫ f ( x)dx using n subdivisions:
a

b
   
∫ f ( x)dx ≈  ∆x ⋅ 3 ( f ( x0 ) + 4 f ( x1 ) + f ( x2 )) +  +  ∆x ⋅ 3 ( f ( xn − 2 ) + 4 f ( xn −1 ) + f ( xn ))
1 1
a

≈ ∆x ⋅
1
( f ( x0 ) + 4 f ( x1 ) + f ( x2 ) +  + f ( xn − 2 ) + 4 f ( xn −1 ) + f ( xn ) )
3
Single Segment 1/3 Simpson’s Rule

∆x
b

∫ f ( x)dx ≈ ( f ( x0 ) + 4 f ( x1 ) + f ( x2 ) )
a
3

Multiple Segment 1/3 Simpson’s Rule


n
∆x 2
b

∫ f ( x)dx ≈ ∑ ( f ( x2i − 2 ) + 4 f ( x2i −1 ) + f ( x2i ))


3 i =1
a

Example 4.4
0.8
dx
Evaluate the integral I = ∫ 1 + x2
by 1/3 Simpson’s rule dividing the interval [0,
0

0.8] to 4 equal sub-intervals.


Solution

Numerical Analysis /Lec. 4 - 42 -


Third Class
n=4
0.8 − 0
∆x = = 0.2
4

x 0 0.2 0.4 0.6 0.8

1
1.0 0.91287 0.84515 0.79057 0.74536
1 + x2

From Simpson’s 1/3rd Rule P P

∆x
0.8
I= ∫ f ( x)dx =
3
[( f ( x0 ) + 4 f ( x1 ) + f ( x2 )) + ( f ( x2 ) + 4 f ( x3 ) + f ( x4 ))]
0

∆x
= [ f ( x0 ) + 4[ f ( x1 ) + f ( x3 )] + 2 f ( x2 ) + f ( x4 )]
3
0.2
= [1.0 + 4[0.91287 + 0.79051] + 2 × 0.84515 + 0.74536]
3
= 0.68329

4.6 Simpson’s Rule (3/8 Simpson’s Rule)


If we connect the points of the curve using a 3rd order Lagrange polynomial, the area
P P

under the curve can be approximated by the following formula:


b 3∆x
∫a f ( x ) dx ≅
8
[ f ( x0 ) + 3 f ( x1 ) + 3 f ( x 2 ) + 2 f ( x3 ) + 3 f ( x4 ) + 3 f ( x5 )

+ 2 f ( x6 ) + ..... + 2 f ( x n −3 ) + 3 f ( x n − 2 ) + 3 f ( x n −1 ) + f ( x n )]

Single Segment 3/8 Simpson’s Rule


3∆x
b

∫ f ( x)dx ≈ 8
[ f ( x0 ) + 3 f ( x1 ) + 3 f ( x2 ) + f ( x3 )]
a

Multiple Segment 3/8 Simpson’s Rule


n
3∆x 3
b

∫ f ( x)dx ≈ ∑ ( f ( x3i −3 ) + 3 f ( x3i − 2 ) + 3 f ( x3i −1 ) + f ( x3i ))


8 i =1
a

Numerical Analysis /Lec. 4 - 43 -


Third Class
Example 4.5
Evaluate the integral of the following tabular data with
(a) The trapezoidal rule.
(b) Simpson’s rules.
x 0 0.1 0.2 0.3 0.4 0.5
F(x) 1 8 4 3.5 5 1
Solution
U

(a) Trapezoidal rule (n = 5):


0.1
I= [1 + 2(8 + 4 + 3.5 + 5) + 1] = 2.15
2
(b) Simpson’s rules (n = 5):
0.1 3
I= [1 + 4(8) + 4] + 0.1 [4 + 3(3.5 + 5) + 1] = 1.233333 + 1.14375 = 2.377083
3 8

Example 4.6
The volume of is given by following expression:
0.9
FA0 d xA
V =
CAo ∫ 0
k (1 − x A )

19000 x A
with k = 2.7 ×10 7 exp(−6500 / T ) min −1 and T = 325 + using
120.35 x A + 143.75

CA0 =2.5 mol L , ε =-0.2


-1
FA0 = 1500mol/min, P P

Calculate the volume of the reactor using Simpsons rule with five points (4 steps).
Solution
Xa T k 1
k (1 − x A )
0 325.0000 0.0433 23.1031
0.2250 350.0251 0.1809 7.1346
0.4500 368.2020 0.4523 4.0195
0.6750 382.0035 0.8560 3.5947
0.9000 392.8396 1.3687 7.3063

Numerical Analysis /Lec. 4 - 44 -


Third Class
V = (1500 / 2.5) * (0.225 / 3) * (23.1031 + 4 * 7.1346 + 2 * 4.0195 + 4 * 3.5947 + 7.3063)
= 3661.4 L

Numerical Analysis /Lec. 4 - 45 -


Third Class
Numerical Differentiation

Numerical Differentiation is a method used to approximate the value of a derivative


over a continuous region [a,b].
Let f(x) is a continuous function with step size h. There are forward, backward and
centered difference methods to approximate the derivatives of f(x) at a point x i .
R R

5.1 Forward Difference Approximation of the First Derivative


We know
f ( x + ∆x ) − f ( x )
f ′( x ) = lim
∆x → 0 ∆x
For a finite ' ∆x' .
f ( x + ∆x ) − f ( x )
f ′( x ) ≅
∆x

f (x)

x x + ∆x x

Figure 5.1: Graphical representation of forward


difference approximation of first derivative
So if you want to find the value of f ′(x ) at x = xi , we may choose another point

' Δx' ahead as x = xi +1 . This gives

f ( xi +1 ) − f ( xi )
f ′( xi ) ≅
∆x

f ( xi +1 ) − f ( xi )
= Where Δx = xi +1 − xi
xi +1 − xi

Numerical Analysis /Lec. 5 - 45 -


Third Class
Example 5.1
 14 × 104 
The velocity of a rocket is given by ν (t ) = 2000 ln   − 9.8t , 0 ≤ t ≤ 30
14 × 10 4
− 2100t 

Where 'ν' is given in m/s and 't ' is given in seconds.


Use forward difference approximation of the first derivative of ν(t ) to calculate the

acceleration at t = 16 s . Use a step size of ∆t = 2 s .

Solution
ν (ti +1 ) − ν (ti )
a(ti ) ≅
∆t
ti = 16
Δt = 2

ti +1 = ti + ∆t = 16 + 2 = 18

ν (18) −ν (16)
a(16 ) =
2

 14 × 10 4 
ν (18) = 2000 ln   − 9.8(18) = 453.02m / s
14 × 10 − 2100(18)
4

 14 × 10 4 
ν (16) = 2000 ln   − 9.8(16) = 392.07 m / s
14 × 10 − 2100(16)
4

Hence
ν(18) − ν(16) 453.02 − 392.07
a(16) = = = 30.475m / s 2
2 2

The exact value of a(16) can be calculated by differentiating


d  14 × 10 4    14 × 10 4 − 2100 t  d  14 × 10 4 
a (t ) =  2000 ln   − 9.8t  = 2000  
  − 9.8
14 × 10 − 2100 t  14 × 10  dt  14 × 10 − 2100 t 
4 4 4
dt   

 14 × 10 4 − 2100 t   14 × 10 4 
= 2000 
 (− 1) (14 × 10 4 − 2100 t )2 (− 2100 ) − 9.8
  = 29.674m / s 2
 14 × 10 4
  

The absolute relative true error is


True Value - Approximate Value 29.674 − 30.475
∈t = × 100 = × 100 = 2.6993%
True Value 29.674

Numerical Analysis /Lec. 5 - 46 -


Third Class
5.2 Backward Difference Approximation of the First Derivative
We know
f ( x + ∆x ) − f (x )
f ′( x ) = lim
∆x →0 ∆x
For a finite ' Δx' ,
f ( x + ∆x ) − f ( x )
f ′( x ) ≅
∆x
If ' Δx' is chosen as a negative number,
f (x + ∆x ) − f (x )
f ′(x ) ≅
∆x
f (x ) − f (x − ∆x )
f ′(x ) =
∆x
This is a backward difference approximation as you are taking a point backward
from x . To find the value of f ′(x ) at x = xi , we may choose another point ' Δx'

behind as x = xi −1 . This gives

f (xi ) − f (xi −1 )
f ′(xi ) ≅
∆x

f ( xi ) − f ( xi −1 )
= where ∆x = xi − xi −1
xi − xi −1

f (x)

x
x − ∆x x

Figure 5.2 Graphical representation of backward


difference approximation of first derivative

Numerical Analysis /Lec. 5 - 47 -


Third Class
Example 5.2
The velocity of a rocket is given by
 14 × 10 4 
ν(t ) = 2000 ln   − 9.8t , 0 ≤ t ≤ 30
14 × 10 − 2100 t 
4

Use backward difference approximation of the first derivative of ν(t ) to calculate the

acceleration at t = 16 s . Use a step size of ∆t = 2 s .

Solution
ν (ti ) −ν (ti −1 )
a(t ) ≅
∆t
ti = 16
Δt = 2
ti −1 = ti − ∆t = 16 − 2 = 14
ν (16) −ν (14)
a(16) =
2

 14 × 104 
ν (16) = 2000 ln   − 9.8(16) = 392.07m / s
14 × 10 − 2100(16) 
4

 14 × 10 4 
ν (14) = 2000 ln   − 9.8(14 ) = 334.24m / s
14 × 10 − 2100(14 ) 
4

ν (16) −ν (14) 392.07 − 334.24


a(16) = = = 28.915m / s 2
2 2
The absolute relative true error is
29.674 − 28.915
∈t = × 100 = 2.557%
29.674

Numerical Analysis /Lec. 5 - 48 -


Third Class
5.3 Central Difference Approximation of the First Derivative
As shown above, both forward and backward divided difference approximation of the
first derivative are accurate on the order of 0(Δx ) . Can we get better approximations?

Yes, another method to approximate the first derivative is called the Central
difference approximation of the first derivative.
From Taylor series
f ′′( xi ) ′′′( )
f ( xi +1 ) = f ( xi ) + f ′( xi )Δx + (Δx )2 + f xi (Δx )3 +  (1)
2! 3!
f ′′( xi )
(∆x )2 − f (xi ) (∆x )3 + 
′′′
f ( xi −1 ) = f ( xi ) − f ′( xi )∆x + (2)
2! 3!
Subtracting equation (2) from equation (1)
2 f ′′′( xi )
f ( xi +1 ) − f ( xi −1 ) = f ′( xi )(2∆x ) + (∆x )3 + 
3!
f ( xi +1 ) − f ( xi −1 ) f ′′′( xi )
f ′( xi ) = − (∆x )2 + 
2∆x 3!
f ( xi +1 ) − f (xi −1 )
f ′( xi ) = + 0(∆x )
2
2∆x

f (xi +1 ) − f (xi −1 )
f ′( xi ) =
2∆x

Hence showing that we have obtained a more accurate formula as the error is of the
order of 0(∆x )2 .

f (x)

x − ∆x x x + ∆x

Figure 5.3 Graphical Representation of central


difference approximation of first derivative.

Numerical Analysis /Lec. 5 - 49 -


Third Class
Example 5.3
The velocity of a rocket is given by
 14 × 10 4 
ν (t ) = 2000 ln   − 9.8t ,0 ≤ t ≤ 30 .
14 × 10 − 2100t 
4

Use central divided difference approximation of the first derivative of ν(t ) to

calculate the acceleration at t = 16s . Use a step size of ∆t = 2s .


Solution
ν (t i +1 ) − ν (t i −1 )
a(t i ) ≅
2∆t
ti = 16
t i +1 = t i + Δt = 16 + 2 = 18
t i −1 = t i − ∆t = 16 − 2 = 14
ν(18) − ν(14) ν(18) − ν(14 )
a(16 ) = =
2(2 ) 4

 14 × 10 4 
ν (18) = 2000 ln   − 9.8(18) = 453.02m / s
14 × 10 − 2100(18)
4

 14 × 10 4 
ν (14 ) = 2000 ln   − 9.8(14 ) = 334.24m / s
 14 × 10 4
− 2100(14 ) 

ν(18) − ν(14) 453.02 − 334.24


a(16 ) = = = 29.695m / s 2
4 4
The absolute relative true error is
29.674 − 29.695
∈t = × 100 = 0.070769%
29.674
The results from the three difference approximations are given in Table 1.
Table 1 Summary of a(16) using different divided difference approximations.

Type of Difference a(16 )


∈t %
Approximation (m / s )
2

Forward 30.475 2.6993


Backward 28.915 2.557
Central 29.695 0.070769
Clearly, the central difference scheme is giving more accurate results because the
order of accuracy is proportional to the square of the step size.

Numerical Analysis /Lec. 5 - 50 -


Third Class
5.4 Higher Order Derivatives

Example: Second order derivative:


Note that for the centered formulation, it is a derivation of a derivative:
f (xi +1 ) − f ( xi ) f (xi ) − f ( xi −1 )

∆x ∆x f ( xi +1 ) − 2 f ( xi ) + f ( xi −1 )
f ' ' (x ) ≅ =
∆x (∆x) 2
f (xi + 2 ) − 2 f ( xi +1 ) + f ( xi )
f ' ' (x ) ≅
Forward
(∆x) 2
f (xi ) − 2 f ( xi −1 ) + f ( xi − 2 )
f ' ' (x ) ≅
Backward
(∆x) 2
f ( xi +1 ) − 2 f ( xi ) + f ( xi −1 )
f ' ' (x ) ≅
Centered
(∆x) 2

I) Forward Difference Methods


First Derivative
f (xi +1 ) − f (xi )
f ' ( xi ) =
∆x
Second Derivative
f ( xi + 2 ) − 2 f ( xi +1 ) + f ( xi )
f ' ' ( xi ) =
(∆x) 2

Third Derivative
f ( xi + 3 ) − 3 f ( xi + 2 ) + 3 f ( xi +1 ) − f ( xi )
f (3) ( xi ) =
(∆x)3

Fourth Derivative
f (xi + 4 ) − 4 f ( xi + 3 ) + 6 f (xi + 2 ) − 4 f ( xi +1 ) + f (xi )
f (4 ) ( xi ) =
(∆x) 4

II) Backward Difference Methods


First Derivative
f ( xi ) − f ( xi −1 )
f ' ( xi ) =
∆x
Second Derivative
f ( xi ) − 2 f ( xi −1 ) + f ( xi − 2 )
f ' ' ( xi ) =
(∆x) 2
Numerical Analysis /Lec. 5 - 51 -
Third Class
Third Derivative
f ( xi ) − 3 f ( xi −1 ) + 3 f ( xi − 2 ) − f ( xi −3 )
f (3) ( xi ) =
(∆x)3

Fourth Derivative
f ( xi ) − 4 f ( xi −1 ) + 6 f ( xi − 2 ) − 4 f ( xi − 3 ) + f (xi − 4 )
f (4 ) ( xi ) =
(∆x) 4

III) Central Difference Methods


First Derivative
f ( xi +1 ) − f ( xi −1 )
f ' ( xi ) =
2∆x
Second Derivative
f (xi +1 ) − 2 f (xi ) + f (xi −1 )
f ' ' ( xi ) =
(∆x) 2

Third Derivative
f ( xi + 2 ) − 2 f ( xi +1 ) + 2 f ( xi −1 ) − f ( xi − 2 )
f (3) ( xi ) =
2(∆x)3

Fourth Derivative
f (xi + 2 ) − 4 f ( xi +1 ) + 6 f ( xi ) − 4 f ( xi −1 ) + f ( xi − 2 )
f (4 ) ( xi ) =
(∆x) 4

Numerical Analysis /Lec. 5 - 52 -


Third Class
Solving System of Linear Equations

6.1 Linear Equation


y = mx is an equation, in which variable y is expressed in terms of x and the constant
m, is called Linear Equation. In Linear Equation exponents of the variable is always
‘one’.

6.2 Linear Equation in n variables:


a1 x1 + a2 x2 + a3 x3 + .... + an xn = b
Where x1 , x2 , x3 ,..., xn are variables and
a1 , a2 , a13 ,..., an and b are constants.

6.3 System of Linear Equations:


A Linear System of m linear equations and n unknowns is:
a11 x1 + a12 x2 + a13 x3 + .... + a1n xn = b1
a21 x1 + a22 x2 + a23 x3 + .... + a2 n xn = b2
a31 x1 + a32 x2 + a33 x3 + .... + a3n xn = b3
.......................................................
........................................................
am1 x1 + am 2 x2 + am3 x3 + .... + amn xn = bm
Where x1 , x2 , x3 ,..., xn are variables or unknowns and a’s and b’s are constants.

6.4 Augmented Matrix


System of linear equations:
a11 x1 + a12 x2 + a13 x31 = b1
a21 x1 + a22 x2 + a23 x31 = b2
a31 x1 + a32 x2 + a33 x31 = b3
Can be written in the form of matrices product
 a11 a12 a13   x1   b1 
a
 21 a22 a23   x2  = b2 
 a31 a32 a33   x3  b3 
Or we may write it in the form AX=b,
 a11 a12 a13   x1   b1 
Where A= a21 a22 a23  , X =  x2  , b = b2 
 a31 a32 a33   x3  b3 
 a11 a12 a13 b1 
Augmented matrix is [A : b] = a21 a22 a23 b2 
 a31 a32 a33 b3 
Numerical Analysis /Lec. 6 - 53 -
Third Class
Example 6.1:
Write the matrix and augmented form of the system of linear equations
3x – y + 6z = 6
x+y+z=2
2x + y+4z = 3
Solution: Matrix form of the system is
3 − 1 6   x  6 
1 1 1   y  =  2 
    
2 1 4  z  3
 3 − 1 6 6
Augmented form is [A : b] = 1 1 1 2  .
2 1 4 3
6.5 Methods for Solving System of Linear Equations
1. Gaussian Elimination Method
2. Gauss -Jorden Elimination Method

6.5.1 Gaussian Elimination.


Gaussian elimination is a general method of finding possible solutions to a linear
system of equations.
Gaussian Elimination Method
Step 1. By using elementary row operations
 a11 a12 a13 b1  1 A12 A13 B1 
a
 21 a22 a23 b2  → 0 1 A23 B2 
 a31 a32 a33 b3  0 0 1 B3 
Step 2. Find solution by back – substitutions.

Example 6.2:
Solve the system of linear equations by Gaussion- Elimination method
 x1 + x2 + x3 = 3

 2 x1 − x2 − 2 x3 = 6
4 x + 2 x + 3x =
 1 2 3 7

Solution:
Step 1.
Augmented matrix is
R2 = r2 − 2r1
R3 = r3 − 4r1

Numerical Analysis /Lec. 6 - 54 -


Third Class
1 1 1 3
 
2 − 1 − 2 6
4 2 3 7 
1 1 1 3
  R3 = 3r3 − 2r1
0 − 3 − 4 0 
0 − 2 − 1 − 5

1 1 1 3 
  R2 = −r2
 0 − 3 − 4 0 
0 0 r3
5 − 15 R3 =
5
1 1 1 3 
 
0 3 4 0 
0 0 1 − 3
Equivalent system of equations form is:
Step 2. Back Substitution
x1 + x2 + x3 = 3 x3 = −3
3 x2 + 4 x3 = 0 ⇒ x2 = −4 x3 / 3 = 12 / 3 = 4
x3 = −3 x1 = 3 − x2 − x3 = 3 − 4 + 3 = 2
Solutions are x1 = 2, x2 = 4, x3 = −3

Example 6.3:
For the below figure calculate the values of the unknown flow rates F 1 , F 2 and F 3 byR R R R R R

using Gaussion- Elimination method

. F1=? F2=?
99% Benzene 5% Benzene
1% Toluene 92% Toluene
3 % Xylene
Tower 1

Tower 2

F=1000 kg/hr

40% Benzene
40% Toluene
20% Xylene
F3=?
10% Toluene
90% Xylene

Component material balance gives these three equations of three variables

Numerical Analysis /Lec. 6 - 55 -


Third Class
F1 + F2 + F3 = 1000
0.99 F1 + 0.05 F2 + 0 F3 = 400
0.01F1 + 0.92 F2 + 0.1F3 = 400
Augmented matrix is
0B

 1 1 1 1000 
0.99 0.05 0 400  R 2 =r 2 – (0.99/1)×r 1
 
R R R R R

 0.01 0.92 0.1 400 R 3 =r 3 – (0.01/1)×r 1


R R R R R

1 1 1 1000 
0 − 0.94 − 0.99 - 590
  R 3 =r 3 -(0.91/(-0.94))r 2
R R R R R

0 0.91 0.09 390 


1 1 1 1000 
0 − 0.94
 − 0.99 - 590  R 2 =r 2 /(-0.94)
R R R R

0 0 - 0.8684 - 181.17  R 3 =r 3 /( -0.8684)


R R R R

1 1 1 1000 
0 1 1.0532 627.6596
 
0 0 1 208.6253 

Equivalent system of equations form is:


F 1 + F 2 + F 3 = 1000
R R R R R R

F 2 +1.0532F 3 = 627.6596 R R R R

F 3 = 208.6253 R R

Step 2. Back Substitution


F 3 = 208.6253
R R

F 2 = 627.6596-1.0532F 3 =627.6596-1.0532×208.6253 =407.9354


R R R R R R

F 1 =1000- F 2 -F 3 =1000 -208.6253 - 407.9354= 383.4393


R R R R R R

6.5.2 Gauss - Jorden Elimination Method


Gauss - Jorden Method
By using elementary row operations
 a11 a12 a13 b1  1 0 0 B1 
a
 21 a22 a23 b2  → 0 1 0 B2 
 a31 a32 a33 b3  0 0 1 B3 

Example 6.4:
Solve the system of linear equations by Gauss-Jorden elimination method
x 1 + x 2 + 2x 3 = 8
R R R R R R

- x 1 - 2x 2 + 3x 3 = 1
R R R R R R

3x 1 -7x 2 + 4x 3 = 10
R R R R R R

Numerical Analysis /Lec. 6 - 56 -


Third Class
Solution:
Augmented matrix is
1 1 2 8
− 1 − 2 3 1  R 2 = r 2 +r 1
 
R R R R R

 3 − 7 4 10 R 3 = r 3 -3r 1 R R R R R

1 1 2 8 
0 − 1
 5 9  R 2 =-r 2 R R R

0 − 10 − 2 - 14 R 3 =r 3 -10r 2 R R R R R

1 1 2 8 
0 1 − 5
 - 9  R 3 =-r 3 /52 R R R R

0 0 − 52 - 104

1 1 2 8 R 1 =r 1 -2r 3 R R R R R

0 1 − 5 − 9  R 2 =r 2 +5r 3
 
R R R R R

0 0 1 2 
1 1 0 4
0 1 0 1  R 1 =r 1 -r 2
 
R R R R R

0 0 1 2
1 0 0 3
0 1 0 1 
 
0 0 1 2
Equivalent system of equations form is:
x1 = 3
R R

x2 = 1
R R

x3 = 2
R R

is the solution of the system.

Example 6.5:
Total and component material balance on a system of distillation columns gives the
flowing equations:-
F1 + F2 +R RF 3 + F 4 = 1690
R R R R R R

0.4F 1 + 0.15F 2 + 0.25F 3 + 0.2F 4 = 412.5


R R R R R R R R

0.25F 1 + 0.8F 2 + 0.3F 3 + 0.45F 4 = 701


R R R R R R R R

0.08F 1 + 0.05F 2 + 0.45F 3 + 0.3F 4 = 487.3


R R R R R R R R

Use Gauss - Jorden method to compute the four un-known's in above equations:-

Numerical Analysis /Lec. 6 - 57 -


Third Class
Solution:
Augmented matrix is
 1 1 1 1 1690 
 0.4 0.15 0.25 0.2 412.5 R 2 = r 2 -(0.4/1)r 1
 
R R R R R

0.25 0.8 0.3 0.45 701  R 3 =r 3 -(0.25/1)r 1


R R R R R

  R 4 =r 4 -(0.08/1)r 1
0.08 0.05 0.45 0.3 487.3
R R R R R

1 1 1 1 1690 
0 − 0.25 − 0.15 − 0.2 − 263.5
 
0 0.55 0.05 0.2 278.5  R 3 =r 3 -(0.55/(-0.25))r 2
R R R R R

  R 4 =r 4 -((-0.03)/(-0.025))r 2
0 − 0.03 0.37 0.22 352.1 
R R R R R

1 1 1 1 1690 
0 − 0.25 − 0.15 − 0.2 − 263.5
 
0 0 − 0.28 − 0.24 − 301.2
  R 4 =r 4 -((0.0388)/(-0.028))r 3
0 0 0.388 0.244 383.72  R R R R R

1 1 1 1 1690 
0 − 0.25 − 0.15 − 0.2 − 263.5 
 R 2 = r 2 /(-0.25)
R R R R

0 0 − 0.28 − 0.24 − 301.2  R 3 =r 3 /(-0.028)


 
R R R R

0 0 0 − 0.08857 − 33.657 R 4 =r 4 /(-0.0887)


R R R R

1 1 1 1 1690  R 1 = r 1 -r 4
R R R R R

0 1 0.6 0.8 1054  R 2 =r 2 -(0.8/1)r 4



R R R R R

0 0 1 0.85714 1075.74 R 3 =r 3 -(0.85714/1)r 4


R R R R R

 
0 0 0 1 380 
1 1 1 0 1310 R 1 = r 1 -r 3
R R R R R

0 1 0.6 0 750  R 2 =r 2 -((-0.6)/1)r 3



R R R R R

0 0 1 0 750 
 
0 0 0 1 380 
1 1 0 0 560
0 1 0 0 300 R 1 =r 1 -r 2

R R R R R

0 0 1 0 750
 
0 0 0 1 380
1 0 0 0 260
0 1 0 0 300  Equivalent system of equations form:

0 0 1 0 750 F 1 = 260 , F 2 = 300 , F 3 = 750 and
R R R R R R

  F 4 = 380 is the solution of the system.


0 0 0 1 380 
R R

Numerical Analysis /Lec. 6 - 58 -


Third Class
Iterative Methods for Solving System of Linear Equation

7.1 Jacobi Method


Let the given equation be
a2 x + b1 y + c1 z = d1
a2 x + b2 y + c2 z = d 2
a3 x + b3 y + c3 z = d 3
If the given system of equation is diagonally dominant then
x (i +1) =
1
a1
(
d1 − b1 y (i ) − c1 z (i ) )
y (i +1) =
1
b2
(
d 2 − a2 x (i ) − c2 z (i ) )
z (i +1) =
1
c3
(
d 3 − a3 x (i ) − b3 y (i ) )
7.1.1 Condition for Jacobi method of converges:
The sufficient condition is
a1 ≥ b1 + c1
b2 ≥ a2 + c2
c3 ≥ a3 + b3
The absolute value of the diagonal element in each row of the coefficient matrix must
be greater than the sum of the absolute values of the off-diagonal elements in the
same row.

Example 7.1:
Use the Jacobi iteration method to obtain the solution of the following equations:
6x 1 -2 x 2 + x 3 = 11
R R R R R R

x 1 +2x 2 -5x 3 = -1
R R R R R R

-2x 1 +7 x 2 +2x 3 = 5
R R R R R R

Solution
Step 1: Re-write the equations such that each equation has the unknown with largest
coefficient on the left hand side:
6x 1 = 11+2 x 2 -x 3
R R R R R R

7x 2 = 5+2x 1 -2x 3
R R R R R

5x 3 =1+x 1 +2x 2
R R R R R R

2 x2 − x3 + 11
x1 =
6

Numerical Analysis /Lec. 7 - 59 -


Third Class
2 x1 − 2 x3 + 5
x2 =
7
x + 2 x2 + 1
x3 = 1
5
Step 2: Assume the initial guesses x1 = x2 = x3 = 0 then calculate x1 , x2 and x3 :
0 0 0 1 1 1

2( x20 ) − ( x30 ) + 11 2(0) − (0) + 11


x11 = = =1.833
6 6
2( x10 ) − 2( x30 ) + 5 2(0) − 2(0) + 5
x12 = = = 0.714
7 7
( x10 ) + 2( x20 ) + 1 (0) + 2(0) + 1
x31 = = = 0.200
5 5
Step 3: Use the values obtained in the first iteration, to calculate the values for the 2nd
P P

iteration:
2( x12 ) − ( x3`1 ) + 11 2(0.714) − (0.200) + 11
x12 = = = 2.038
6 6
2( x11 ) − 2( x31 ) + 5 2(1.833) − 2(0.200) + 5
x22 = = =1.181
7 7
( x11 ) + 2( x12 ) + 1 (1.833) + 2(0.714) + 1
x32 = = = 0.852
5 5
and so on for the next iterations so that the next values are calculated using the
current values:
2( x2i ) − ( x3i ) + 11
x1i +1 =
6
2( x1i ) − 2( x3i ) + 5
x2i +1 =
7
( x1i ) + 2( x2i ) + 1
x3i +1 =
5
The results for 9 iterations are:
Unknowns
Iter. x1 R x2 R x3
R

1 1.833 0.714 0.200


2 2.038 1.181 0.852
3 2.085 1.053 1.080
4 2.004 1.001 1.038
. . . .
. . . .
9 2.000 1.000 1.000

Numerical Analysis /Lec. 7 - 60 -


Third Class
Example 7.2:
Solve the equations by Jacobi method
20x 1 + x 2 – 2x 3 = 17
R R R R R R

3x 1 + 20x 2 – x 3 = –18
R R R R R R

2x 1 – 3x 2 + 20x 3 = 25
R R R R R R

Solution
Rewrite the given equation in the form:
1
x1i +1 = (17 − x2i + 3x3i )
20
1
x2i +1 = (−18 − 3x1i + x3i )
20
1
x3i +1 = (25 − 2 x1i + 3x2i )
20
Using x=
0
1
x=
0
2
x 30 = 0, we obtain
17
x=
1
1
= 0.85
20
−18
x12 = = −0.90
20
25
x=
1
3
= 1.25
20
Putting these values on the right of equations to obtain
(17 − x12 − 2x=3)
1
x=
2
1
1
1.02
20
1
(
x 22 = −18 − 3x11 + x13 =
20
−0.965 )
x=3
2 1
20
( 25 − 2x11 + 3x12 =) 1.1515
These and further iterates are listed in the table below:
i x1i x2i x3i
0 0 0 0
1 0.85 –0.90 1.25
2 1.02 –0.965 1.1515
3 1.0134 –0.9954 1.0032
4 1.0009 –1.0018 0.9993
5 1.0000 –1.0002 0.9996
6 1.0000 –1.0000 1.0000

The values in 5th and 6th iterations being practically the same, we can stop. Hence the
P P P P

solutions are:
x 1 = 1, x 2 = –1 and x 3 = 1
R R R R R R

Numerical Analysis /Lec. 7 - 61 -


Third Class
7.2 Gauss-Seidel Method
If the given system of equation is diagonally dominant then
x (i +1) =
1
a1
(
d1 − b1 y (i ) − c1 z (i ) )
y (i +1) =
1
b2
(
d 2 − a2 x (i +1) − c2 z (i ) )
z (i +1) =
1
c3
(
d 3 − a3 x (i +1) − b3 y (i +1) )

Example 7.3:
Use the Gauss-Seidel method to obtain the solution of the following equations:
6x 1 -2 x 2 + x 3 = 11
R R R R (1) R R

x 1 +2 x 2 -5x 3 = -1
R R R (2)
R R R

-2x 1 +7 x 2 +2x 3 = 5
R R R (3) R R R

Solution
Step 1: Re-write the equations such that each equation has the unknown with largest
`coefficient on the left hand side:
2 x − x + 11
x1 = 2 3 from eq. (1)
6
2 x − 2 x3 + 5
x2 = 1 from eq. (3)
7
x + 2 x2 + 1
x3 = 1 from eq. (2)
5
Step 2: Assume the initial guesses x20 = x30 = 0 , then calculate x11 :
2( x20 ) − ( x30 ) + 11 2(0) − (0) + 11
x11 = = =1.833
6 6
Use the updated value x11 = 1.833 and x30 = 0 to calculate x12
2( x11 ) − 2( x30 ) + 5 2(1.833) − 2(0) + 5
x12 = = =1.238
7 7
Similarly, use x11 = 1.833 and x12 = 1.238 to calculate x31
( x11 ) + 2( x12 ) + 1 (1.833) + 2(1.238) + 1
x31 = = =1.062
5 5

Step 3: Repeat the same procedure for the 2nd iteration P P

2( x12 ) − ( x31 ) + 11
2(1.238) − (1.062) + 11
x12 = = 2.069 =
6 6
2( x12 ) − 2( x31 ) + 5 2(2.069) − 2(1.062) + 5
x22 = = =1.002
7 7

Numerical Analysis /Lec. 7 - 62 -


Third Class
( x12 ) + 2( x22 ) + 1 (2.069) + 2(1.002) + 1
x32 = = =1.015
5 5
and so on for the next iterations so that the next values are calculated using the
current values:
2( xi2 ) − ( x3i ) + 11
x1i +1 =
6
2( x1i +1 ) − 2( x3i ) + 5
xi2+1 =
7
( x1i +1 ) + 2( xi2+1 ) + 1
x3i +1 =
5
and continue the above iterative procedure until [(x k )i+1 - (x k )i]/ (x k )i+1 < Є for i=1,2 and R R P P R R P P R R P P R R

3.
The procedure yields the exact solution after 5 iterations only:

Unknown
Iter. x1
R x2 R x3 R

1 1.833 1.238 1.062


2 2.069 1.002 1.015
3 1.998 0.995 0.998
4 1.999 1.000 1.000
5 2.000 1.000 1.000

Example 7.4:
Solve by Gauss – Seidel method, the equations:
20x 1 + x 2 – 2x 3 = 17
R R R R R R

3x 1 + 20x 2 – x 3 = – 18
R R R R R R

2x 1 – 3x 2 + 20x 3 = 25
R R R R R R

Solution
As before, we start with initial estimate x= x=
0 0
1 2
x 30 = 0. We write the given
equation in the form
1
x1i +1 = (17 − x2i + 2 x3i )
20
1
x2i +1 = (−18 − 3x1i +1 + x3i )
20
1
x3i +1 = (25 − 2 x1i +1 + 3x2i +1 )
20

These and further iterates are listed in the table below:


Numerical Analysis /Lec. 7 - 63 -
Third Class
i x1i x2i x3i
0 0 0 0
1 0.8500 –1.0275 1.0109
2 1.0025 –0.9998 0.9998
3 1.0000 –1.0000 1.0000
nd rd
The value in the 2 and 3 iterations being particularly the same, we can stop. Hence
P P P P

the solutions is x 1 = 1, x 2 = – 1 and x 3 = 1.


R R R R R R

Example 7.5:
For the below figure calculate the values of the unknown flow rates F1, F2 and F3 by
using Gauss-Seidel Method F1=? F2=?
. 99% Benzene 5% Benzene
1% Toluene 92% Toluene
3 % Xylene
Tower 1

Tower 2
F=1000 kg/hr

40% Benzene
40% Toluene
20% Xylene
F3=?
10% Toluene
90% Xylene
Component material balance gives these three equations of three variables
0.99 F1 + 0.05 F2 + 0 F3 = 400
0.01F1 + 0.92 F2 + 0.1F3 = 400
0 F1 + 0.03F2 + 0.9 F3 = 200
Re-arranging the above equations
F1 = (400 − 0.05F2 ) / 0.99
F2 = (400 − 0.01F1 − 0.1F3 ) / 0.92
F3 = (200 − 0.03F2 ) / 0.9
Starting with F1=F2=F3=1000/3
Iteration F1 F2 F3
333.3333 333.3333 333.3333
1.0000 387.2054 394.3420 209.0775
2.0000 384.1241 407.8815 208.6262
3.0000 383.4403 407.9380 208.6243
4.0000 383.4375 407.9383 208.6243
5.0000 383.4375 407.9383 208.6243

Numerical Analysis /Lec. 7 - 64 -


Third Class
Solving System of Non-Linear Systems of Equations

A nonlinear system of equations has at least one equation which is not first degree.

Examples: x2 + y2 = 25
P P P P y = 3x2 – 4x + 2 xy = 9
P P

2
2x + 3y = 7 x +y=8 P P 3x2 – y2 = 12 P P P P

The solutions of a nonlinear system are the points of intersection of the graphs of the
equations. Some systems have one point of intersection; some have more than one
point of intersection; and some have no points of intersection.

8.1 Analytical Methods for Solving Systems of Equations


Solutions of nonlinear systems of equations can be found using the substitution or the
elimination method. The substitution method is preferable for a system with one
linear equation. The elimination method is preferable in most, but not all, cases when
both equations are nonlinear.

8.1.1. The Substitution Method


Solve one of the equations for a first degree variable. Substitute the resulting
expression in for that variable in the other equation. Solve for the remaining variable.
Back substitute to find the value(s) of the first variable. Write your solutions as
ordered pairs.

Example 8.1
Solve x2 + 2x = y + 6
P P

x + y = –2
Solution
x + y = –2 → y = –2 – x → x2 + 2x = –2 – x + 6 →
P P

x2 + 3x – 4 = 0 → (x + 4) (x – 1) = 0 → x = –4 & x = 1
P P

y = –2 – (–4) = 2 & y = –2 – 1 = –3 →
Solution: (–4, 2) (1, –3)

To check graphically, enter y 1 = x2 + 2x – 6


R R P P & y2 = –2 – x
R R

Find the points of intersection.


Numerical Analysis /Lec. 8 - 65 -
Third Class
8.1.2. The Elimination Method

Line the equations up vertically so like terms are underneath each other. If needed,
multiply each equation by a number so that when the equations are added together one
of the variables is eliminated. Solve for the remaining variable. Back substitute to find
the value(s) of the eliminated variable. Write your solutions as ordered pairs.

Example 8.2
Solve 3x2 + 5y2 = 17 P P P P

2x2 – 3y2 = 5 P P P P

Solution:
3x2 + 5y2 = 17 3R 1
P P 9x2 + 15y2 = 51
P P R R → P P P P

2x2 – 3y2 = 5 5R 2
P P → 10x2 – 15y2 = 25
P P R R U UP UP UP PU

19x2 = 76 P P

x2 = 4
P P → x = –2 & x = 2
2 2
3(–2) + 5y = 17 → 12 + 5y2 = 17
P P → P P P P

2 2
5y = 5 P → y =1
P → y = –1 & y=1 P P

2 2 2
3(2) + 5y = 17 → P12 + 5y = 17
P → P P P P

2 2
5y = 5 P → y =1
P → y = –1 & y=1 P P

Solutions: (–2, –1) (–2, 1) (2, –1) (2, 1)

8.2 Numerical Methods for Solving Systems of Equations


8.2.1 Fixed point iteration for systems of non-linear equations
Using an initial guess, solve for each variable in the system and use fixed-point
iteration to estimate the solution.
One of the most important drawbacks of the fixed iteration method is that the
convergence of the method is dependent on how the equations are formulated. May
diverge quickly, in that case try solving for the variables in a different way.

It can be shown that sufficient convergence criteria for two equations are:
∂f1 ∂f
+ 1 <1
∂x1 ∂x2
and
∂f 2 ∂f 2
+ <1
∂x1 ∂x2
This represents a very restrictive criteria and that’s why fixed point iteration method
is not used to solve systems of non-linear equations.

Numerical Analysis /Lec. 8 - 66 -


Third Class
Example 8.3
Solve using 5 iteration of successive substitution where x = y = 1.5 initially:
 x 2 + y 2 = 5

 y − x 2 = −1
Solution
Using x = 5 − y 2 and y = x2 – 1 it is easy to show the following iterations:
P P

Iteration xnR yn
R

0 1.5 1.5
1 1.658 1.75
2 1.392 0.9375
3 2.030 3.121
4 Non-real Non-real
5
So it is apparent that successive substitution this will not work using these formulas.
Using y = 5 − x 2 and x = 1 + y we can just as easily show the following iterations:
Iteration xn
R yn
R

0 1.5 1.5
1 1.5811 1.5811
2 1.607 1.555
3 1.599 1.564
4 1.601 1.561
5 1.600 1.562
 1 + 17 − 1 + 17 
Which is rapidly converging on the true solution of  , 
 2 2 
 
As with the Jacobi iterative process, convergence is assured only for a system of
diagonally dominant linear equations. For systems that are neither linear nor
diagonally dominant, convergence is a function of the equations themselves as well
as the values of x’s chosen to start the iterations.

Example 8.4
As an example of applying the Jacobi method to a system of non-linear equations,
consider the following system:
4 x1 − x 2 + x 3 =7
4x1 −8( x 2 ) + x 3 =−21
2

−2x1 + x 2 +5( x 3 ) =15


2

Solving the equations for each of the unknowns (x’s), we have the following:

Numerical Analysis /Lec. 8 - 67 -


Third Class
 7+ x 2 − x 3
2

x1 = 
 4 
−21−4x1 − x 3
x2 =
−8
15+2x1 − x 2
x3 =
5
Using these relationships in a Jacobi algorithm with starting values of x 1 = x 2 = x 3 = R R R R R R

0, we can show the convergence of the algorithm over ten iterations in the following
table:
Itr x1 x2 x3 R R R

0 0 0 0
1 3.0625 1.620185 1.732051
2 2.9654 2.091114 1.975086
3 3.164866 2.086764 1.941118
4 3.191267 2.109519 1.961783
5 3.193133 2.113257 1.963314
6 3.195105 2.113523 1.963314
7 3.195343 2.113757 1.963501
8 3.195385 2.113790 1.963514
9 3.195403 2.113796 1.963516
10 3.195406 2.113798 1.963518

8.2.2 Newton-Raphson for solving systems of non-linear equations


The Newton-Raphson formula is the following:
f ( xi )
xi +1 = xi −
f ' ( xi )
This formula can be obtained using Taylor series expansion. We can do the same
approach for a system of equations, but considering a Taylor series that account for
the presence of both variables:
∂f1(i ) ∂f1(i )
f1(i +1) = f1(i ) + (x1(i +1) − x1(i ) ) + (x2(i +1) − x2(i ) ) + ...
∂x1 ∂x2
and
∂f 2(i ) ∂f 2(i )
f 2(i +1) = f 2(i ) + (x1(i +1) − x1(i ) ) + (x2(i +1) − x2(i ) ) + ...
∂x1 ∂x2
For the root estimate f1(i+1) and f 2(i+1) must be equal zero.
Therefore:

Numerical Analysis /Lec. 8 - 68 -


Third Class
∂f1(i ) ∂f ∂f ∂f
x1(i+1) + 1(i ) x2 (i+1) = − f1(i ) + x1(i ) 1(i ) + x2(i ) 1(i )
∂x1 ∂x2 ∂x1 ∂x2
and
∂f 2(i ) ∂f ∂f ∂f
x1(i+1) + 2(i ) x2(i+1) = − f 2(i ) + x1(i ) 2(i ) + x2(i ) 2(i )
∂x1 ∂x2 ∂x1 ∂x2
Finally;
∂f 2 (i ) ∂f1(i )
f1(i ) − f 2 (i )
∂x 2 ∂x 2
x 1(i +1) = x 1(i ) −
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x 1 ∂x 2 ∂x 2 ∂x 1
∂f1(i ) ∂f 2 (i )
f 2 (i ) − f1(i )
∂x 1 ∂x 1
x 2 (i +1) = x 2 (i ) −
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x 1 ∂x 2 ∂x 2 ∂x 1
Which is an iterative method to solve the system of nonlinear equations.

Note also that the Newton-Raphson method can be generalized to solve N


simultaneous equations.

Example 8.5
Solve the following system using Newton-Raphson method:
x 2 + y 2 − 8x − 4 y + 11 = 0
x 2 + y 2 − 20 x + 75 = 0
By tacking a starting point as (x=2; y=4) and ε = 10−5 .
Solution
∂f 2 (i ) ∂f1(i )
f 1(i ) − f 2 (i )
∂y ∂y
x ( i +1 ) = x ( i ) −
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x ∂y ∂y ∂x
∂f1(i ) ∂f 2 (i )
f 2 (i ) − f1(i )
y ( i +1 ) = y ( i ) − ∂x ∂x
∂f1(i ) ∂f 2 (i ) ∂f1(i ) ∂f 2 (i )

∂x ∂y ∂y ∂x

Let f1 = x 2 + y 2 − 8x − 4 y + 11 and f 2 = x 2 + y 2 − 20x + 75


∂f1,i ∂f1,i ∂f 2,i ∂f 2,i
Thus: = 2 x − 8, = 2 y − 4, = 2 x − 20, and = 2y
∂x ∂y ∂x ∂y
Hence when x =2 and y = 4 we find that:

Numerical Analysis /Lec. 8 - 69 -


Third Class
∂f1, 0 ∂f1, 0 ∂f 2, 0 ∂f 2, 0
= 2(2) − 8 = −4, = 2(4) − 4 = 4, = 2(2) − 20 = −16, and = 2(4) = 8
∂x ∂y ∂x ∂y
Also f1, 0 = 22 + 42 − 8(2) − 4(4) + 11 = −1 and f 2, 0 = 22 + 42 − 20(2) + 75 = 55
∂f1, 0 ∂f 2, 0 ∂f1, 0 ∂f 2, 0
− = (−4)(8) − (4)(−16) = 32
∂x ∂y ∂y ∂x
So for the first iteration we see that:
(−1)(8) − (55)(4)
x1 = 2 − ≈ 9.1250
32
(55)(−4) − (−1)(−16)
y1 = 4 − ≈ 11.3750
32
x 0 − x1 2 − 9.125
ε= = = 3.5625
x0 2
Now we find that iteration 2 produces:
∂f1,1 ∂f1,1 ∂f 2,1
= 2(9.1250) − 8 = 10.25, = 2(11.375) − 4 = 18.75, = 2(9.1250) − 20 = -1.75,
∂x ∂y ∂x
∂f 2,1
and = 2(11.375) = 22.75
∂y
Also f1,1 = 9.1252 + 11.375 2 − 8(9.125) − 4(11.375) + 11 = 105.1563 and
f 2,1 = 9.1252 + 11.375 2 − 20(9.125) + 75 = 105.1563
∂f1,1 ∂f 2,1 ∂f1,1 ∂f 2,1
− = (10.25)(22.75) − (18.75)(−1.75) = 266
∂x ∂y ∂y ∂x
So for the second iteration we see that:
(105.1563)(22.75) − (105.1563)(18.75)
x 2 = 9.125 − ≈ 7.543703
266
(105.1563)(10.25) − (105.1563)(-1.75)
y 2 = 11.375 − ≈ 6.631109
266
x 0 − x1 9.125 − 7.543703
ε = = = 0.173293
x0 9.125
∂f1 ∂f1 ∂f 2 ∂f 2
Itr f1 f2 x y εs
∂x ∂y ∂x ∂y
2 4
1 -1 55 -4 4 -16 8 9.125 11.375 3.5625
2 105.1563 105.1563 10.25 18.75 -1.75 22.75 7.543703 6.631109 0.173293
3 25.005 25.005 7.087406 9.262218 -4.91259 13.26222 6.826694 4.480083 0.095047
4 5.141014 5.141014 5.653389 4.960166 -6.34661 8.960166 6.576327 3.728981 0.036675
5 0.626838 0.626838 5.152654 3.457963 -6.84735 7.457963 6.535955 3.607865 0.006139
6 0.016299 0.016299 5.07191 3.215731 -6.92809 7.215731 6.534848 3.604543 0.000169
7 1.23E-05 1.23E-05 5.069696 3.209087 -6.9303 7.209087 6.534847 3.604541 1.28×10-7
P

Numerical Analysis /Lec. 8 - 70 -


Third Class
Solution of First-Order Ordinary Differential Equations

An equation that consists of derivatives is called a differential equation. Differential


equations have applications in all areas of science and engineering. Mathematical
formulation of most of the physical and engineering problems lead to differential
equations. So, it is important for engineers and scientists to know how to set up
differential equations and solve them. Differential equations are of two types
1) Ordinary differential equation (ODE).
2) Partial differential equations (PDE).
An ordinary differential equation is that in which all the derivatives are with respect
to a single independent variable. Examples of ordinary differential equation include:
dy
1) + y = sin(x) , y (0) = 1 ,
dx
d2y dy dy
2) 2
+2 + y=0 , (0) = 2 , y (0) = 4
dx dx dx
d3y d2y dy d2y dy
3) 3
+ 3 2
+ 5 + y = sin x , 2
(0) = 12 , (0) = 2 , y (0) = 4
dx dx dx dx dx
First order ordinary differential equations are of the form:
dx
= f=( x, t ) with x(0) x0
dt
On the left hand side is the derivative of the dependent variable x with respect to
the independent variable t. On the right hand side, there is a function that may depend
on both x and t.
Many differential equations cannot be solved exactly. Numerical methods have
been developed to approximate solutions. Numerical analysis is a field in mathematics
that is concerned with developing approximate numerical methods and assessing their
accuracy, for instance for solving differential equations. We will discuss the most basic
method such Taylor, Euler and Runge-Kutta methods.

9.1 Taylor Series Method


U

Function y (x) can be expanded over a small interval x using the Taylor series from a
start or reference point x
1 2 1 1
y( x + h ) = y( x ) + hy′( x ) + h y′′( x ) + h 3 y′′′( x ) + h 4 y ( 4 ) ( x ) +  (1)
2! 3! 4!
Where hi = xi +1 − xi = h, a constant.
Numerical Analysis /Lec. 9 - 71 -
Third Class
Example 9.1
Solve the following ordinary differential equation (ODE) using Taylor’s method of
order 2 with h= 0.2
dy
= y − x2 + 1 P P , for 0 ≤ x ≤ 2 , with y(0) = 0.5
dx
Solution
d2y dy
2
= − 2x = y − x2 + 1 − 2x P P

dx dx
1
y n+1 = y n + (y n − x n 2 + 1)h +
R R R R R R R RP P ( ynR R − x n 2 − 2x n + 1)h2
R RP P R R P

2
i x y
1 0 0.5000
2 0.2000 0.8300
3 0.4000 1.2158
4 0.6000 1.6521
5 0.8000 2.1323
6 1.0000 2.6486
7 1.2000 3.1913
8 1.4000 3.7486
9 1.6000 4.3061
10 1.8000 4.8463
11 2.0000 5.3477

9.2. Euler’s Method


U

Euler’s method is the simplest and least useful of these three methods. If we are
dy
solving a first-order differential equation of the form = f ( t , y ) with the initial
dt
condition y(0)=A, Euler’s method begins by approximating the first derivative as
dy y (t + ∆t ) − y (t )

dt ∆t

Setting this equal to f(t,y) and solving for y (t + ∆t ) yields the following algorithm for

advancing the numerical solution of an ordinary differential equation:


dy
= f ( x, y ) y( 0 ) = y0
dx
yn +1 = yn + h × f ( x, y )

Numerical Analysis /Lec. 9 - 72 -


Third Class
Using Euler’s method we have the following consideration:
x1 = x0 + h y1 = y 0 + h ⋅ f ( x0 , y 0 )
x 2 = x1 + h y 2 = y1 + h ⋅ f ( x1 , y1 )
x3 = x 2 + h y3 = y 2 + h ⋅ f ( x2 , y 2 )
 
 
 
Exercise 9.2:
Apply Euler’s method to approximate the solution of the initial value problem
dy
= 2 y with y (0) = 5 (2)
dx
Solution
We know that the analytical solution of equation (2) is , y = 5 exp(2 x) . We numerically
solve equation (2) using Euler’s method with h=0.1 in the time interval [0, 0.5], and
then check how well this method performs. We have f ( y ) = 2 y . Then
x0 = 0
x1 = x0 + h = 0 + 0.1 = 0.1
x 2 = x1 + h = 0.1 + 0.1 = 0.2
x3 = x 2 + h = 0.2 + 0.1 = 0.3
x 4 = x3 + h = 0.3 + 0.1 = 0.4
x5 = x 4 + h = 0.4 + 0.1 = 0.5
And
y0 = 5
y1 = y 0 + hf ( y 0 ) = 5 + (
0.1)( 2)(5) = 6

h f ( y0 )

y 2 = y1 + hf ( y1 ) = 6 + (0.1)(2)(6) = 7.2
y 3 = y 2 + hf ( y 2 ) = 7.2 + (0.1)(2)(7.2) = 8.64
y 4 = y 3 + hf ( y 3 ) = 8.64 + (0.1)(2)(8.64) = 10.368
y 5 = y 4 + hf ( y 4 ) = 10.368 + (0.1)(2)(10.368) = 12.4416
We summarize this in the following table. If h=0.1, then
x y Exact Difference
0 5 5 0
0.1 6 6.107014 0.107014
0.2 7.2 7.459123 0.259123
0.3 8.64 9.110594 0.470594
0.4 10.368 11.1277 0.759705
0.5 12.4416 13.59141 1.149809
Numerical Analysis /Lec. 9 - 73 -
Third Class
The third column contains the exact values, y = 5 exp(2 x) . The last column contains
the absolute error after each step, computed as |y-y Exact |. We see that when h=0.1, the
R R

numerical approximation is not very good after five steps. If we repeat the same
approximation with a smaller value for h, say h=0.01, the following table results for
the first five steps:
x y Exact Difference
0 5 5 0
0.01 5.1 5.101007 0.001007
0.02 5.202 5.204054 0.002054
0.03 5.30604 5.309183 0.003143
0.04 5.412161 5.416435 0.004275
0.05 5.520404 5.525855 0.005451

Doing five steps only gets us to x=0.05. We can do more steps until we reach x=0.5.
We find that the final point will be:
x y Exact Difference
0.5 13.45794 13.59141 0.133469

Choosing a smaller value for h resulted in a better approximation at x=0.5 but also
required more steps. One source of error in the approximation comes from the
approximation itself.

9.3 Fourth order Runge-Kutta Method


dy
To find numerical solution to the initial value problem = f ( x, y ), y (0) = y 0 using
dx
Runge-Kutta method we have the following consideration:

yi +1 = yi +
1
(k1 + 2k2 + 2k3 + k4 )h
6

k1 = f (xi , yi )

 1 1 
k2 = f  xi + h, yi + k1h 
 2 2 
 1 1 
k3 = f  xi + h, yi + k2 h 
 2 2 
k4 = f (xi + h, yi + k3h )

This method gives more accurate result compared to Euler’s method

Numerical Analysis /Lec. 9 - 74 -


Third Class
Example 9.3:
Solve the following ordinary differential equation (ODE) using fourth order
Runge-Kutta method to calculate y(x=0.2)
dy
= x + y ; y(0) = 1 , h = 0.1
dx
Solution:
k 1 = 0 + 1= 1
R

k 2 = (0+0.05)+(1+1×0.05)=1.10
R

k 3 = (0+0.05) +(1+1.1×0.05)= 1.1050


R

k 4 = (0+0.1)+(1+1.1050×0.1) = 1.2105
R

h
y1 = y o + ( k1 + 2k 2 + 2k 3 + k 4 )
6
0.1
y(0.1) = 1 + × (1 + 2 × 1.1 + 2 × 1.105 + 1.2105) = 1.11034
6

k 1 = 0.1 + 1.11034 = 1.21034


R

k 2 = (0.1+0.05)+(1.11034+1.21034×0.05) = 1.3209
R

k 3 = (0.1+0.05) +(1.11034+1.3209×0.05)= 1.3264


R

k 4 = (0.1+0.1)+( 1.11034+1.3264×0.1) = 1.4430


R

0.1
y(0.2) = 1.11034 + × (1.21034 + 2 × 1.3209 + 2 × 1.3264 + 1.4430) = 1.2428
6
at x=0.2 y=1.2428

Example 9.4:
A ball at 1200 K is allowed to cool down in air at an ambient temperature of 300 K.
Assuming heat is lost only due to radiation, the differential equation for the
temperature of the ball is given by
= −2.2067 × 10 −12 (T 4 − 81 × 108 ) , T(0 ) = 1200 K
dT
dt
where T is in K and t in seconds. Find the temperature at t = 480 seconds using
Runge-Kutta 4th order method. Assume a step size of h = 240 seconds.

Numerical Analysis /Lec. 9 - 75 -


Third Class
Solution
= −2.2067 × 10 −12 (T 4 − 81 × 108 )
dT
dt

f (t , T ) = −2.2067 × 10 −12 (T 4 − 81 × 108 )

h
Ti+1 = Ti + (k1 + 2k 2 + 2k 3 + k 4 )
6
For i = 0 , t 0 = 0 , T0 = 1200 K

k1 = f (t 0 , T0 ) = f (0,1200 ) = −2.2067 × 10 −12 (1200 4 − 81 × 108 ) = −4.5579

   
k 2 = f  t 0 + h , T0 + k1h  = f  0 + (240 ),1200 + (− 4.5579 ) × 240 
1 1 1 1
 2 2   2 2 
(
= f (120,653.05) = −2.2067 × 10 −12 653.054 − 81 × 108 )
= −0.38347
   
k 3 = f  t 0 + h , T0 + k 2 h  = f  0 + (240 ),1200 + (− 0.38347 ) × 240 
1 1 1 1
 2 2   2 2 
( )
= f (120,1154 .0 ) = −2.2067 × 10 −12 1154 .0 4 − 81 × 108 = −3.8954

k 4 = f (t 0 + h , T0 + k 3h ) = f (0 + 240,1200 + (− 3.894 ) × 240 ) = f (240,265.10 )

= −2.2067 × 10 −12 (265.10 4 − 81 × 108 ) = 0.069750

h
T1 = T0 + ( k1 + 2k 2 + 2k 3 + k 4 )
6
= 1200 +
240
(− 4.5579 + 2(− 0.38347 ) + 2(− 3.8954) + (0.069750))
6
= 675.65 K
T1 is the approximate temperature at t=t 1 R

t = t 0 + h = 0 + 240 = 240

For i = 1, t1 = 240, T1 = 675.65 K

k1 = f (t1 , T1 ) = f (240,675.65) = −2.2067 × 10 −12 (675.654 − 81 × 108 )

= −0.44199
   
k 2 = f  t1 + h , T1 + k1h  = f  240 + (240 ),675.65 + (− 0.44199 )240 
1 1 1 1
 2 2   2 2 
= f (360,622.61) = −2.2067 × 10 −12 (622.614 − 81 × 108 )
= −0.31372

Numerical Analysis /Lec. 9 - 76 -


Third Class
   
k 3 = f  t1 + h , T1 + k 2 h  = f  240 + (240 ),675.65 + (− 0.31372 ) × 240 
1 1 1 1
 2 2   2 2 
= f (360,638.00 )
(
= −2.2067 × 10 −12 638.00 4 − 81 × 10 8 )
= −0.34775
k 4 = f (t1 + h , T1 + k 3h ) = f (240 + 240,675.65 + (− 0.34775) × 240 ) = f (480,592.19 )

= 2.2067 × 10 −12 (592.19 4 − 81 × 108 )

= −0.25351
h
T2 = T1 + (k1 + 2k 2 + 2k 3 + k 4 )
6
= 675.65 +
240
(− 0.44199 + 2(− 0.31372) + 2(− 0.34775) + (− 0.25351))
6
= 594.91 K
T2 is the approximate temperature at time t 2 R

t 2 = t1 + h = 240 + 240 = 480

Table 1 and Figure 2 show the effect of step size on the value of the calculated
temperature at t = 480 seconds.
Table 1 Value of temperature at time, t = 480 s for different step sizes
Step size, h T(480) Et | εt | %
480 -90.278 737.85 113.94
240 594.91 52.660 8.1319
120 646.16 1.4122 0.21807
60 647.54 0.033626 0.0051926
30 647.57 0.00086900 0.00013419
Example 9.5
Using Matlab Commands solve the following equation using both Eular and
Runge-Kutta method and to approximate the solution of the initial value problem
dy
= x + y, y (0) = 1 with step size h = 0.1.
dx
Solution:
Eular Runge Kutta
clear all, clc,format short clear all, clc,format short
x(1)=0; x(1)=0;y(1)=1;h=0.1;
y(1)=1; f=inline('x+y');
h=0.5 % f(x,y) = x+y
for i=1:5 for i=1:5

Numerical Analysis /Lec. 9 - 77 -


Third Class
x(i+1)=x(i)+h; x(i+1)=x(i)+h;
dy=x(i)+y(i); k1 = f(x(i),y(i));
y(i+1)=y(1)+h*dy; k2 = f(x(i)+h/2,y(i)+k1*h/2);
end k3 = f( x(i) + h/2,y(i)+k2*h/2);
y_exact= -1-x+2*exp(x); k4 = f( x(i) + h,y(i)+k3*h);
error=y_exact-y y(i+1)=y(i)+(1/6)*h*(k1 +2*k2 + 2*k3 +k4);
table=[x',y',y_exact',error'] end
y_exact= -1-x+2*exp(x); error=y_exact-y
table=[x',y',y_exact',error']
table = table =
0 1.0000 1.0000 0 0 1.0000 1.0000 0
0.1000 1.1000 1.1103 0.0103 0.1000 1.1103 1.1103 0.0000
0.2000 1.1200 1.2428 0.1228 0.2000 1.2428 1.2428 0.0000
0.3000 1.1320 1.3997 0.2677 0.3000 1.3997 1.3997 0.0000
0.4000 1.1432 1.5836 0.4404 0.4000 1.5836 1.5836 0.0000
0.5000 1.1543 1.7974 0.6431 0.5000 1.7974 1.7974 0.0000

Numerical Analysis /Lec. 9 - 78 -


Third Class
Solving Simultaneous First-Order Ordinary Differential Equations

10.1 Integration two simultaneous first-order ordinary differential equations


Consider the following system of first-order ODE’s describing the dependence of two
dependent variables y and z on one independent variable x:
dy
= f ( x, y , z )
dx
dz
= g ( x, y , z )
dx
These two differential equations are coupled and must be integrated simultaneously
because both equations involve both dependent variables.
Initial conditions are required giving the values of y and z at the initial value of x. The
algorithm for 4th-order Runge-Kutta integration of two coupled ODEs is:

h
yi + 1 = yi + ( k11 + 2k 21 + 2k31 + k41 )
6
h
zi + 1 = zi + ( k12 + 2k 22 + 2k32 + k42 )
6

k11 = f ( xi , yi , zi )
k12 = g( xi , yi , zi )
k 21 = f ( xi + 0.5 h , yi + 0.5 hk11 , zi + 0.5 hk12 )
k 22 = g ( xi + 0.5 h , yi + 0.5 hk11 , zi + 0.5 hk12 )
k32 = f ( xi + 0.5 h , yi + 0.5 hk 21 , zi + 0.5 hk 22 )
k32 = g( xi + 0.5 h , yi + 0.5 hk 21 , zi + 0.5 hk 22 )
k41 = f ( xi + h , yi + hk31 , zi + hk32 )
k42 = g( xi + h , yi + hk31 , zi + hk32 )

As example an exothermic reaction in unsteady-state continuous stirred tank reactor


and exothermic reaction in a plug flow reactor with heat exchange through the reactor
wall.
From the one and two ODE examples, you can extend the method to integration of
three coupled ODE’s. Three coupled ODE’s would be encountered, for example, for
reaction of gases in a steady-state non-isothermal plug flow reactor with significant
pressure drop (dC/dx =, dT/dx =, and dP/dx=).

Numerical Analysis /Lec. 10 - 79 -


Third Class
10.2 Integration of a system of first-order ordinary differential equations
dy1
= f1(x, y1 , y2 ,…, ym )
dx
dy2
= f 2 (x, y1 , y2 ,…, ym )
dx
.....................
......................
dym
= f m (x, y1 , y2 ,…, ym )
dx
The solution of the above equations is:

h
yin + 1 = yin + (k1,i + 2k 2,i + 2k3,i + k4,i ) Where i = 1, 2, ..., m and
6

k1,i = f i (x n , y1n , y2n …, ymn )

h n hk1,1 n hk1,2 hk
k 2,i = f i (x n + , y1 + , y2 + …, ymn + 1,m )
2 2 2 2
h n hk 2 ,1 n hk 2 ,2 hk
k3,i = f i (x n + , y1 + , y2 + …, ymn + 2 ,m )
2 2 2 2

k4,i = f i (x n + h , y1n + hk3 ,1 , y2n + hk3 ,2 …, ymn + hk3 ,m )

The idea of the solution to a system of differential equations is similar to a solution of a


single differential equation.

Example 10.1:
Using fourth order Runge-Kutta method with step size h = 0.1 solve
dy1
= y1 y2 + x
R R R R , y 1 (0) = 1
R

dx
R

dy 2
= xy 2 + y 1 R R , y 2 (0) = -1
R

dx
R R R

To calculate y 1 (0.1) and y 2 (0.1) R

R
R

Solution
At x = 0, y 1 = 1, y2 = -1 R

R
R

k 1,1 = y1 y2 + x = (1)(-1) + 0 = -1
0T R R R R R R

k 1,2 = xy 2 + y 1 = (0)(-1) + 1= 1
0T R R R R R R

k 2,1 = (y 1 +0.5hk 1,1 )(y 2 +0.5hk 1,2 ) + (x+0.5h) =(0.95)(-0.95) + 0.05 = -0.8525
0T R R R R R R R R R R

Numerical Analysis /Lec. 10 - 80 -


Third Class
k 2,2 = (x+0.5h)(y 2 +0.5hk 1,2 ) +(y 1 +0.5hk 1,1 ) =(0.05)(-0.95) + 0.95 = 0.9025
0T R R R R R R R R R R

k 3,1 = (y 1 +0.5hk 2,1 )(y 2 +0.5hk 2,2 ) + (x+0.5h) =(0.9574)(-0.9549) + 0.05= -0.8642
0T R R R R R R R R R R 0T

k 3,2 = (x+0.5h)(y 2 +0.5hk 2,2 ) +(y 1 +0.5hk 2,1 ) =(0.05)(-0.9549) + 0.9574 = 0.9096
0T R R R R R R R R R R 0T

k 4,1 = (y 1 +hk 3,1 )(y 2 +hk 3,2 ) + (x+h) =(0.9136)(-0.9091) + 0.1 = -0.7305
0T R R R R R R R R R R 0T

k 4,2 = (x+h)(y 2 +hk 3,2 ) +(y1 +hk 3,1 ) =(0.1)(-0.9091) + 0.9136= 0.8227
0T R R R R R R R R R R 0T

at x=0.1
y 1 (0.1) = y 1 (0) + (h/6)(k 1,1 + 2k 2,1 + 2k 3,1 + k 4,1 )
R R R R R
R

R R R
R

R R

y1 (0.1) = 1 +(0.1/6) [(-1) + 2(-0.8525) + 2(-0.864) + (-0.730)]= 0.9139


R R

y2 (0.1) = y 2 (0) +(h/6) (k 1,2 + 2k 2,2 + 2k 3,2 + k 4,2 )


R R R R R
R

R
R

R
R

R R

y2 (0.1) = -1 + [(1) + 2(0.9025) + 2(0.909) + (0.823)]= -0.9092


R R

Example 10.2
Use ode45 Matlab Command to solve the following first order system for y1 and y2 at R R R R R R

0 ≤ x ≤ 1.
dy1
= y1 y2 + x R R R R , y 1 (0) = 1
0T R R

dx
dy2
= xy 2 + y1 , y 2 (0) = -1
0T R R

dx
R R

Using fourth order Runge-Kutta method with step size h = 0.1


Solution
The Matlab routines ode45 can be used to solve the system. A Matlab function must
be created to evaluate the slopes as a column vector. The function name in this
example is exode(x, y) which must be saved first in the hard drive with the same
name exode.m.
function dydx = exode(x,y)
dydx (1,1)=y(1)*y(2)+x;
dydx (2,1)=x*y(2)+y(1);
The command ode45 is then evaluated from the command windows. Matlab will set
the step size to achieve a preset accuracy that can be changed by user.
The independent variable can also be specified at certain locations between the initial
and final values and Matlab will provide the dependent value at these locations.

Numerical Analysis /Lec. 10 - 81 -


Third Class
xspan=0:0.1:1;
[x,y]=ode45('exode',xspan,[1 , -1])
x= y=
0 1.0000 -1.0000
0.1000 0.9139 -0.9092
0.2000 0.8522 -0.8341
0.3000 0.8106 -0.7711
0.4000 0.7863 -0.7174
0.5000 0.7772 -0.6705
0.6000 0.7817 -0.6283
0.7000 0.7987 -0.5889
0.8000 0.8274 -0.5504
0.9000 0.8675 -0.5108
1.0000 0.9188 -0.4681

Exercise 10.3:
U

Let’s consider a simple example of a model of a plug flow reactor that is described by a
system of ordinary differential equations. A plug flow reactor is operated as shown in
Figure (9.1) below.

A →
k1
B →
k2
C

Figure (9.1) Isothermal plug flow reactor


The plug flow initially has only reactant A, the components A react to form component
B. The mole balance for each component is given by the following differential
equations
dC A
u = − k 1C A
dz
dC
u B = k 1C A − k 2 C B
dz
dC
u C = k 2CB
dz
With the following initial values
C A (z=0) =1 kmol/m3
R R P P C B (z=0) =0
R R C C (z=0) =0
R R and k 1 =2
R R k 2 =3
R R

Numerical Analysis /Lec. 10 - 82 -


Third Class
If u=0.5 m/s and reactor length z=3 m. Solve the differential equations and plot the
concentration of each species along the reactor length
Solution:
We’ll start by writing the function defining the right hand side (RHS) of the ODEs. The
following function file ‘example3’ is used to set up the ode solver.

function dC= Example4 ( z, C)


u = 0.5;
k1=2; k2=3;
dC(1,1) = -k1 *C(1) / u;
dC(2,1) = (k1 *C(1)-k2 *C(2)) / u;
dC(3,1) = k2 *C(2)/ u;
Now we’ll write a main script file to call ode45. CA, CB and CC must be defined
within the same matrix, and so by calling CA as C(1), CB as C(2) and CC as c(3), they
are listed as common to matrix C.
The following run file is created to obtain the solution:

clear all, clc


[z , C] = ode45(' Example3', [0:0.1:3], [1 0 0])
plot (z,C(:,1),'k+-',z,C(:,2),'k*:',z,C(:,3),'kd-.')
xlabel ('Length (m)');
ylabel ('Concentrations (kmol/m^3) ');
legend ('A', 'B', 'C')
The produced plot is as in Figure (9.2)

1
A
Concentrations (kmol/m3)

B
0.8
C
0.6

0.4

0.2

0
0 1 2 3
Length (m)
Figure (9.2): A, B and C concentrations along plug flow reactor

Numerical Analysis /Lec. 10 - 83 -


Third Class
10.3 Solving Higher Order Ordinary Differential Equations
We have learned Euler’s and Runge-Kutta methods to solve first order ordinary
differential equations of the form
= f ( x, y ), y (0) = y0
dy
dx
What do we do to solve differential equations that are higher than first order? For
example an n th order differential equation of the form
dny d n −1 y
+ ao y = f (x )
dy
an n
+ a n −1 n −1
+  + a1
dx dx dx
with n − 1 initial conditions can be solved by assuming
y = z1 (1)

dy dz1
= = z2 (2)
dx dx
d 2 y dz 2
= = z3 (3)
dx 2 dx

d n −1 y dz n −1
= = zn (n)
dx n −1 dx
d n y dzn
=
dx n dx
1  d n −1 y 
− a0 y + f ( x )
dy
=  − an −1 n −1  − a1
an  dx dx 

=
1
(− a n−1 z n  − a1 z 2 − a0 z1 + f (x )) (n+1)
an
The above Equations from (2) to (n+1) represent n first order differential equations as
follows
dz1
= z 2 = f 1 ( z1 , z 2 , , x )
dx
dz 2
= z 3 = f 2 ( z1 , z 2 , , x )
dx

dz n
=
1
(− a n−1 z n  − a1 z 2 − a0 z1 + f (x ))
dx an
Each of the n first order ordinary differential equations is accompanied by one initial
condition. These first order ordinary differential equations are simultaneous in nature
but can be solved by the methods used for solving first order ordinary differential
equations that we have already learned.
Numerical Analysis /Lec. 10 - 84 -
Third Class
Higher Order Ordinary Differential System of first Order Ordinary
Equations Differential Equations
d2y dy dy
2
− 2 + 2 y = e 2 x sin x =z , y (0) = −0.4
dx dx dx
with dz
= e 2 x sin x − 2 y + 2 z , z (0) = −0.6
dy dx
y(0)= -0.4 , (0) = −0.6
dx
d3y d2y dy dy
3
+ 2 2
− 3 = x 2 + y, = z, y (0) = 4
dx dx dx dx
with dz
2
=u, z (0) = 2
d y dy dx
2
(0) = 1, (0) = 2 , y (0) = 4
dx dx du
= x 2 + y − 2u + 3z , u (0) = 1
dx
d4y dy y dy
4
+ − + 3y = , = z, y ( 0) = 1
dx dx x dx
with dz
=u, z (0) = 1
3
d y d y 2
dy dx
3
(0) = 0.5, 2 (0) = 0.25, (0) = 1 , y (0) = 1
dx dx dx du
=v, u (0) = 0.25
dx
dv y
= − 3y + z , v(0) = 0.5
dx x

Example 10.4
Re-write the following differential equation as a set of first order differential equations.
d2y
3 2 + 2 + 5 y = e − x , y (0) = 5, y ′(0) = 7
dy
dx dx
Solution
The ordinary differential equation would be rewritten as follows. Assume
dy d 2 y dz
= z, Then =
dx dx 2 dx
Substituting this in the given second order ordinary differential equation gives
dz
3 + 2z + 5 y = e −x
dx
dz 1 − x
(
= e − 2z − 5 y
dx 3
)
The set of two simultaneous first order ordinary differential equations complete with
the initial conditions then is

Numerical Analysis /Lec. 10 - 85 -


Third Class
= z, y (0) = 5
dy
dx

= (e − 2 z − 5 y ), z (0) = 7 .
dz 1 − x
dx 3
Now one can apply any of the numerical methods used for solving first order ordinary
differential equations.

Example 10.5
Given the third-order ordinary differential equation and associated initial conditions
d3y d2y dy y (0) = 4, dy d2y
3
+ 3 2
+ 5 + y = x2 , = 0.6 , = 0.22
dx dx dx dx x =0 dx 2 x =0

a. Write this differential equation as a system of first-order ordinary differential


equations
b. Using fourth order Runge-Kutaa method to estimate y(0.1) and y(0.2) taking
Δx=0.1

a)
dy
= z , y(0) = 4
dx
dz
= u , z(0) = 0.6
dx
du
= x 2 − 3u − 5z − y , u (0) = 0.22
dx
b) Solution (1)
First step of integration x=0 , y=4 , z=0.6 , u=0.22 , Δx=0.1
k11= z =0.6000
k21= u= 0.2200
k31=x2-3×u-5×z-y= 02-3×0.22-5×0.6-4=-7.6600
k12=z+0.5×Δx×k21=0.6+0.5×0.1×0.22= 0.6110
k22=(u+0.5×Δx×k31) =(0.22+0.5×0.1×(-7.6600)) = -0.1630
k32=(x+0.5×Δx)2 -3×(u+0.5×Δx×k31)-5×(z+0.5×Δx×k21)-(y+0.5×Δx×k11)
=(0+0.5×0.1) 2-3×(0.22+0.5×0.1×(-7.66))-5×(0.6+0.5×0.1×0.22)-(4+0.5×0.1×0.6)
= -6.5935
k13=z+0.5×Δx×k22=0.6+0.5×0.1×(-0.1630)= 0.5918
k23=u+0.5×Δx×k32 =0.22+0.5×0.1×(-6.5935) = -0.1097
k33=(x+0.5×Δx) 2 -3×(u+0.5×Δx×k32)-5×(z+0.5×Δx×k22)-(y+0.5×Δx×k12)
Numerical Analysis /Lec. 10 - 86 -
Third Class
=(0+0.5×0.1)2-3×(0.22+0.5×0.1×(-6.5935))-5×(0.6+0.5×0.1×(-0.1630))-(4+0.5×0.1×
0.6110) = -6.6583
k14=z+Δx×k23=0.6+0.1×(-0.1097)= 0.5890
k24=u+Δx×k33=0.22+0.1×(-6.6583) = -0.4458
k34=(x+Δx) 2 -3×(u+Δx×k33)-5×(z+Δx×k23)-(y+Δx×k13)
=(0+0.1)2-3×(0.22+0.1×(-6.6583))-5×(0.6+0.1×(-0.1097))-(4+0.1×0.5918) = -5.6569
x=x+Δx=0+0.1= 0.1000
y=y+Δx/6×(k11+2×k12+2×k13+k14)=4+0.1/6×(0.6+2×0.6110+2×0.5918+0.5890)
= 4.0599
z=z+Δx/6*(k21+2*k22+2*k23+k24)=0.6+0.1/6×(0.2200+2×(-0.1630)+2×(-0.1097)+
( -0.4458))= 0.5871
u=u+Δx/6*(k31+2*k32+2*k33+k34)=u+0.1/6×(-7.6600+2×(-6.5935)+2×(-6.6583)+
(-5.6569))=-0.4437
dy dz d 2 y
Then at x=0.1 , y=4.0599, z = = 0.5871 , u= = = -0.4437
dx dx dx 2

Second step of integration x=0.1 , z=0.5871 , u=-0.4437 , Δx=0.1


k11= z = 0.5871
k21= u= -0.4437
k31=x2-3×u-5×z-y= 0.12-3×(-0.4437)-5×(0.5871)- 4.0599=-5.6546
k12=z+0.5×Δx×k21=0.5871+0.5×0.1×(-0.4437)= 0.5650
k22=(u+0.5×Δx×k31) =((-0.4437)+0.5×0.1×(-5.6546)) = -0.7264
k32=(x+0.5×Δx)2-3×(u+0.5×Δx×k31)-5×(z+0.5×Δx×k21)-(y+0.5×Δx×k11)
=(0.1+0.5×0.1)2-3×(-0.4437+0.5×0.1×(-5.6546))-5×(0.5871+0.5×0.1×(-0.4437))-(
4.0599+0.5×0.1×0.5871) = -4.7124
k13=z+0.5×Δx×k22=0.5871+0.5×0.1×(-0.7264)= 0.5508
k23=u+0.5×Δx×k32 =-0.4437+0.5×0.1×(-4.7124) = -0.6793
k33=(x+0.5×Δx)2 -3×(u+0.5×Δx×k32)-5×(z+0.5×Δx×k22)-(y+0.5×Δx×k12)
=(0.1+0.5×0.1)2-3×(-0.4437+0.5×0.1×(-4.7124))-5×(0.5871+0.5×0.1×(-0.7264))-(
4.0599+0.5×0.1×0.5650) = -4.7819
k14=z+Δx×k23=0.5871+0.1×(-0.6793)= 0.5192
k24=u+Δx×k33=-0.4437+0.1×(-4.7819) = -0.9219
k34=(x+Δx)2-3×(u+Δx×k33)-5×(z+Δx×k23)-(y+Δx×k13)
=(0.1+0.1)2-3×(-0.4437+0.1×(-4.7819))-5×(0.5871+0.1×(-0.6793))-(4.0599+0.1×0.5
508) = -3.9055
Numerical Analysis /Lec. 10 - 87 -
Third Class
x=x+Δx=0.1+0.1= 0.2
y=y+Δx/6×(k11+2×k12+2×k13+k14)=
4.0599+0.1/6×(0.5871+2×0.5650+2×0.5508+0.5192)= 4.1155
z=z+Δx/6*(k21+2*k22+2*k23+k24)=
0.5871+0.1/6×((-0.4437)+2×(-0.7264)+2×(-0.6793)+( -0.9219))= 0.5175
u=u+Δx/6*(k31+2*k32+2*k33+k34) =
-0.4437 +0.1/6×((-5.6546) +2×( -4.7124 )+2×(-4.7819)+ (-3.9055 ))= -0.9195
dy dz d 2 y
Then at x=0.2 , y=4.1155, z = = 0.5175 , u= = = -0.9195
dx dx dx 2

Solution (2) Using Metlab:


We’ll start by writing the function defining the right hand side (RHS) of the ODEs. The
following function file ‘Ex’ is used to set up the ode solver.
function dq = Ex(x,q)
y=q(1);z=q(2);u=q(3);
dq(1,1)=z;
dq(2,1)=u;
dq(3,1)=x^2-3*u-5*z-y;

The following run file is created to obtain the solution:


clear all,clc,format compact
[x,q]=ode45('Ex',[0:0.1:0.2],[4,0.6,0.22])
y=q(:,1)

The produced results will be


x=
0
0.1000
0.2000
q=
4.0000 0.6000 0.2200
4.0599 0.5871 -0.4437
4.1155 0.5175 -0.9195
y=
4.0000
4.0599
4.1155

Numerical Analysis /Lec. 10 - 88 -


Third Class

You might also like