NC v1.0
NC v1.0
Numerical
Computing
Lab Manual
Karachi Institute of Economics & Technology
College of Computing & Information Sciences
Numerical
Computing
Lab Manual
Compiled by:
Hafiz Muhammad Waqas
INDEX
Lab Course
Date Particulars Instructor Teacher
Signature Signature
Lab 1: ERRORS
Lab 2: DIFFERENTIATION
LAB 1
ERRORS
Objective:
Find the true , relative true error, approximate and relative approximate error, floating point
error and truncate error. Also know the concept of significant digits
Measuring Errors:
The derivative of a function f ( x ) at a particular value of x can be approximately calculated by
' f ( x +h )−f ( x )
f ( x )≈
h
0 .5 x
of f ( 2 ) For f ( x )=7 e
'
and h=0 .3 , find
'
a) the approximate value of f ( 2 )
'
b) the true value of f ( 2 )
exactly. For example, π and √ 2 are numbers that need to be approximated in computer
calculations.
LAB 2
DIFFERENTIATION
Primer of differentiation :
After reading this chapter, you should be able to:
Practice Problems :
Find the slope of the secant line of the curve y=4 x 2 between points (3,36) and (5,100).
250
200
150
f(x)
100
(5,100)
50 (3,36)
0
-2 -1 0 1 2 3 4 5 6 7 8
Find the slope of the tangent line of the curve y=4 x 2 at point (3,36).
2
Find f ( 3 ) if f ( x )=4 x .
'
2
Find f ( 3 ) if f ( x )=4 x by using the form
'
f ( x )−f ( a )
f ' ( a)=lim
x →a x−a
LAB 3
NON LINEAR EQUATIONS
BISECTION METHOD
Objective :
Write an algorithm for solution of nonlinear equations by bisection method. Implement this
algorithm by PYTHON coding and fill the following observation table.
Introduction :
Suppose f is a continuous function defined on the interval [a, b], with f(a) and f(b) of opposite
sign. By the intermediate value theorem, there exists p in (a, b) with f(p) = 0. Although the
procedure will work for the case when f(a)and f(b) have opposite signs and there is more than
one root in the interval (a, b), we assume for simplicity that the root in this interval is
unique .The method calls for a repeated halving of subintervals of (a, b) and at each step,
locating the half containing p.
To begin, set a1 = a and b1 = b, and let p1 be the midpoint of [a, b]; that is,
p1 = 1/2 (a1+b1)
Theorem :
An equation f ( x )=0 , where f ( x ) is a real continuous function, has at least one root
between x ℓ and
x u if f ( x ℓ )f ( x u )<0 (See Figure 1).
x
Note that if f ( x ℓ )f ( x u )>0 , there may or may not be any root between x ℓ and u
(Figures 2 and 3). If f ( x ℓ )f ( x u )<0 , then there may be more than one root between x ℓ and
x u (Figure 4). So the theorem only guarantees one root between x ℓ and x u .
The steps to apply the bisection method to find the root of the equation f ( x )=0 are
1. Choose
x ℓ and x u as two guesses for the root such that f ( x ℓ )f ( x u )<0 , or in other
x
words, f ( x ) changes sign between ℓ and u .
x
x x
2. Estimate the root, m , of the equation f ( x )=0 as the mid-point between ℓ and u as
x
xℓ + xu
xm =
2
x new
m - x old
m
|∈a| = | |× 100
x new
m
where
x new
m = estimated root from present iteration
x old
m = estimated root from previous iteration
The equation that gives the depth x to which the ball is submerged under water is given by
x 3 −0 .165 x 2 +3 . 993×10−4 =0
Use the bisection method of finding roots of equations to find the depth x to which the ball
is submerged under water. Conduct three iterations to estimate the root of the above
equation. Find the absolute relative approximate error at the end of each iteration, and the
number of significant digits at least correct at the end of each iteration.
|∈a|
Iteration x ℓ xu xm f ( xm )
%
REGULA FALSI
Objective
Follow the algorithm of the false-position method of solving a nonlinear equation, apply the
false-position method to find roots of a nonlinear equation.
Diagram
xU f ( x L ) −x L f ( x U )
xr=
f ( x L )−f ( x U )
2
Find the root of f ( x )= ( x −4 )
( x+2 )=0 , using the initial guesses of x L =−2 .5 and
x U =−1 .0 , and a pre-specified tolerance of ∈s =0 . 1% .
3 2 −4
2. Root of f ( x )=x −0 . 165 x +3 .993×10 =0 for false-position method. using the initial
guesses of xL 2.5 and xU 1.0, and a pre-specified tolerance of s 0.1% .
College of Computing & Information Sciences 7
Course Title: Lab Work
Numerical Computing Version 1.0
Theorem :
The steps to apply the this to find the root of the equation f ( x )=0 are as follows.
1. Choose x L and
x U as two guesses for the root such that f ( x L ) f ( xU ) < 0 , or in other words,
f ( x ) changes sign between x L and x U .
xU f ( x L ) −x L f ( x U )
xr=
f ( x L )−f ( x U )
If
f ( x L ) f ( xr ) < 0 x =x r .
, then the root lies between x L and x r ; then x L =x L and U
If
f ( x L ) f ( xr ) > 0 x x =x U .
, then the root lies between x r and U ; then x L =x r and U
f ( x L ) f ( xr ) =0
If , then the root is x r . Stop the algorithm.
xU f ( x L ) −x L f ( x U )
xr=
f ( x L )−f ( x U )
x new old
r −x r
|∈a|=| |×100
x new
r where
x new
r = estimated root from present iteration
x old
r = estimated root from previous iteration
tolerance
∈s . If|∈a|>∈s , then go to step 3, else stop the algorithm. Note one should also check
whether the number of iterations is more than the maximum number of iterations allowed. If
so, one needs to terminate the algorithm and notify the user about it.
Note that the false-position and bisection algorithms are quite similar. The only difference is the formula
used to calculate the new estimate of the root
x r as shown in steps #2 and #4!
Observations
Starting No. of
S# Functions Tolerance Root
Interval Iterations
- 0.01
1 Cos( x ) – 1.3 x - 0 - 0.001
- 0.0001
- 0.01
x Cos( x ) – 2x2 + 3 x –1=
2 - 0.001
0
- 0.0001
- 0.01
2 x cos(2 x ) –
3 ( x +1)2 = 0 - 0.001
- 0.0001
Conclusion
LAB 4
SECANT METHOD
Objective:
Write an algorithm for solution of nonlinear equations by Secant method. Implement this
algorithm by PYTHON coding and fill the following observation table.
Formula:
f ( xi )( x i−x i−1 )
x i+1 =x i−
f ( x i )−f ( x i−1 )
Graph:
f (x)
f (xi) B
f (xi–1) C
E D A
x
xi+1 xi–1 xi
Observations
Starting No. of
S# Functions Tolerance Root
Interval Iterations
- 0.01
1 Cos( x ) – 1.3 x - 0 - 0.001
- 0.0001
- 0.01
x Cos( x ) – 2x2 + 3 x –1=
2 - 0.001
0
- 0.0001
- 0.01
2 x cos(2 x ) –
3 ( x +1)2 = 0 - 0.001
- 0.0001
Practice Problem :
You are working for ‘DOWN THE TOILET COMPANY’ that makes floats (Figure 2) for ABC
commodes. The floating ball has a specific gravity of 0.6 and a radius of 5.5 cm. You are asked
to find the depth to which the ball is submerged when floating in water. The equation that gives
the depth x to which the ball is submerged under water is given by
x 3 −0 .165 x 2 +3 . 993×10−4 =0
Use the secant method of finding roots of equations to find the depth x to which the ball is
submerged under water. Conduct three iterations to estimate the root of the above equation.
Find the absolute relative approximate error and the number of significant digits at least correct
at the end of each iteration.
LAB 5
NEWTON RAPHSON
Objective
Write an algorithm for solution of nonlinear equations by Newton Raphson method. Implement
this algorithm by PYTHON coding and fill the following observation table.
Diagram
f (x)
f (xi+1)
θ
x
xi+2 xi+1 xi
Algorithm:
The steps of the Newton-Raphson method to find the root of an equation f ( x )=0 are
'
1. Evaluate f ( x ) symbolically
2. Use an initial guess of the root,
x i , to estimate the new value of the root, x i+1 , as
f ( xi)
x i +1 = x i− '
f ( xi)
4. Compare the absolute relative approximate error with the pre-specified relative
∈ |∈ | ∈
error tolerance, s . If a > s , then go to Step 2, else stop the algorithm. Also,
check if the number of iterations has exceeded the maximum number of iterations
allowed. If so, one needs to terminate the algorithm and notify the user.
Observations
Starting No. of
S# Functions Tolerance Root
Interval Iterations
- 0.01
1 Cos( x ) – 1.3 x - 0 - 0.001
- 0.0001
- 0.01
x Cos( x ) – 2x2 + 3 x –1=
2 - 0.001
0
- 0.0001
- 0.01
2 x cos(2 x ) –
3 ( x +1)2 = 0 - 0.001
- 0.0001
Practice Problem :
You are working for ‘DOWN THE TOILET COMPANY’ that makes floats for ABC commodes. The
floating ball has a specific gravity of 0.6 and has a radius of 5.5 cm. You are asked to find the
depth to which the ball is submerged when floating in water.
The equation that gives the depth x in meters to which the ball is submerged under water is
given by
x 3 −0 .165 x 2 +3 . 993×10−4 =0
a) the depth x to which the ball is submerged under water. Conduct three iterations
to estimate the root of the above equation.
b) the absolute relative approximate error at the end of each iteration, and
c) the number of significant digits at least correct at the end of each iteration.
LAB 6
MULLER METHOD
Objective
Write an algorithm for solution of nonlinear equations by Muller method. Implement this
algorithm by PYTHON coding and fill the following observation table.
Diagram
Observations
Starting No. of
S# Functions Tolerance Root
Interval Iterations
- 0.01
1 Cos( x ) – 1.3 x - 0 - 0.001
- 0.0001
x Cos( x ) – 2x2 + 3 x –1= - 0.01
2
0 - 0.001
- 0.0001
2 x cos(2 x ) –
3 ( x +1)2 = 0 - 0.01
Conclusion :
It has been observed that because of different decimal places values, approximate root is
changing
LAB 7
SIMULTANEOUS LINEAR EQUATIONS
Addition
Add the following two matrices
[5
A= 1 2 7
2 3
] [6 7 −2
B = 3 5 19 ]
Subtraction :
Subtract matrix [B] from matrix [A]
[5
A= 1 2 7
2 3
] [6 7 −2
B = 3 5 19 ]
Multiplication :
Given
[ ]
3 −2
[
5 2 3
A= 1 2 7 ] B = 5 −8
9 −10
Find C = A x B
Scalar Multiplication :
Find 2A
Linear Combination:
[5
A= 1 2 7
2 3
] [6 7 −2
B = 3 5 19 ] [6 7 −2
C = 3 5 19 ]
Find A + 2B – 0.5C
College of Computing & Information Sciences 18
Course Title: Lab Work
Numerical Computing Version 1.0
LAB 8
JACOBI METHOD
Objective
Write an algorithm for solution of nonlinear equations by Jacobi method. Implement this
algorithm by PYTHON coding and fill the following observation table.
Diagram
Observations
No. of
S# Functions Tolerance Root
Iterations
x =
0.01 y =
z =
83x + 11y – 4z = 95
x =
1
7x + 52y + 13z = 104 0.001 y =
3x + 8y + 29z = 71 z =
x =
0.0001 y =
z =
x =
0.01 y =
z =
8x – 3y + 2z = 45
x =
2
4x + 11y – z = 71 0.001 y =
6x + 3y + 12z = 35 z =
x =
0.0001 y =
z =
x =
0.01 y =
z =
8x – 3y + 2z = 45
x =
3
4x + 11y – z = 71 0.001 y =
6x + 3y + 12z = 35 z =
x =
0.0001 y =
z =
Conclusion :
LAB 9
GUASS ELIMINATION
Objective:
Solve a set of simultaneous linear equations using Naïve Gauss elimination find the determinant
of a square matrix using Gaussian elimination, and understand the relationship between the
determinant of a coefficient matrix and the solution of simultaneous linear equations
Process :
Gaussian elimination consists of two steps
Time, t Velocity, v
(s) (m/s)
5 106.8
8 177.2
12 279.2
The coefficients
a 1 , a 2 , and a 3 for the above expression are given by
[ ][ ] [ ]
25 5 1 a1 106. 8
64 8 1 a 2 = 177. 2
144 12 1 a 279. 2
3
Observations
No. of
S# Functions Tolerance Root
Iterations
x =
0.01 y =
z =
83x + 11y – 4z = 95
x =
1
7x + 52y + 13z = 104 0.001 y =
3x + 8y + 29z = 71 z =
x =
0.0001 y =
z =
x =
0.01 y =
z =
8x – 3y + 2z = 45
x =
2
4x + 11y – z = 71 0.001 y =
6x + 3y + 12z = 35 z =
x =
0.0001 y =
z =
x =
0.01 y =
z =
8x – 3y + 2z = 45
x =
3
4x + 11y – z = 71 0.001 y =
6x + 3y + 12z = 35 z =
x =
0.0001 y =
z =
Conclusion :
LAB 10
GAUSS SEIDEL
Gauss-Seidel method, give the user control of the round-off error. Also, if the physics of the
problem are well known, initial guesses needed in iterative methods can be made more
judiciously leading to faster convergence.
Process :
Example 1
The upward velocity of a rocket is given at three different times in the following table
Velocity, v
Time, t (s)
(m/s)
5 106.8
8 177.2
12 279.2
[ ][]
a1 1
a2 = 2
a3 5
and conduct two iterations.
Observations
No. of
S# Functions Tolerance Root
Iterations
x =
0.01 y =
z =
83x + 11y – 4z = 95
x =
1
7x + 52y + 13z = 104 0.001 y =
3x + 8y + 29z = 71 z =
x =
0.0001 y =
z =
x =
0.01 y =
z =
8x – 3y + 2z = 45
x =
2
4x + 11y – z = 71 0.001 y =
6x + 3y + 12z = 35 z =
x =
0.0001 y =
z =
3 8x – 3y + 2z = 45 x =
4x + 11y – z = 71 0.01 y =
z =
6x + 3y + 12z = 35
x =
0.001 y =
z =
0.0001 x =
y =
College of Computing & Information Sciences 25
Course Title: Lab Work
Numerical Computing Version 1.0
z =
LAB 11
LU DECOMPOSITION
Objective :
1. Identify when LU decomposition is numerically more efficient than Gaussian elimination,
2. Decompose a nonsingular matrix into LU, and
3. Show how LU decomposition is used to find the inverse of a matrix.
Example 2
[ ]
25 5 1
[ A ] = 64 8 1
144 12 1
[ ][ ]
1 0 0 u11 u 12 u 13
= ℓ 21 1 0 0 u 22 u 23
[ A ] =[ L ] [ U ] ℓ 31 ℓ32 1 0 0 u 33
Example 2
Use the LU decomposition method to solve the following simultaneous linear equations.
[ ][ ] [ ]
25 5 1 a1 106. 8
64 8 1 a 2 = 177. 2
144 12 1 a 279. 2
3
Example 3
Use LU decomposition to find the inverse of
[ ]
25 5 1
[ A ] = 64 8 1
144 12 1
Observations
No. of
S# Functions Tolerance Root
Iterations
x =
0.01 y =
z =
83x + 11y – 4z = 95
x =
1
7x + 52y + 13z = 104 0.001 y =
3x + 8y + 29z = 71 z =
x =
0.0001 y =
z =
x =
0.01 y =
z =
8x – 3y + 2z = 45
x =
2
4x + 11y – z = 71 0.001 y =
6x + 3y + 12z = 35 z =
x =
0.0001 y =
z =
x =
0.01 y =
z =
8x – 3y + 2z = 45
x =
3
4x + 11y – z = 71 0.001 y =
6x + 3y + 12z = 35 z =
x =
0.0001 y =
z =
Conclusion :
LAB 12
LAGRANGIAN INTERPOLATION
Objective:
1. Derive Lagrangian method of interpolation
2. Solve problems using Lagrangian method of interpolation
3. Use Lagrangian interpolants to find derivatives and integrals of discrete functions.
Diagram :
x3 , y3
x1, y1
x2 , y2 f x
x0 , y0
x
The Lagrangian interpolating polynomial is given by
n
f n ( x )=∑ Li ( x ) f ( x i )
i=0
¿ j≠i ¿ ¿ ¿ ¿
n
Example 1
The upward velocity of a rocket is given as a function of time in Table 1.
t v (t )
(s) (m/s)
0 0
10 227.04
15 362.78
20 517.35
22.
602.97
5
30 901.67
Determine the value of the velocity at t =16 seconds using a first order Lagrange polynomial.
LAB 13
NEWTON’S DIVIDED DIFFERENCE INTERPOLATION
Objective :
1. Derive Newton’s divided difference method of interpolation,
2. Apply Newton’s divided difference method of interpolation, and
3. Apply Newton’s divided difference method interpolants to find derivatives and integrals.
Diagram :
y
x3 , y3
x1, y1
x2 , y2 f x
x0 , y0
x
Example 1
The upward velocity of a rocket is given as a function of time in Table 1 (Figure 3).
t ( s) v(t ) (m/s )
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
Determine the value of the velocity at t =16 seconds using first order polynomial interpolation
by Newton’s divided difference polynomial method.
Example 2
The upward velocity of a rocket is given as a function of time in Table 2.
t ( s) v (t ) (m/s )
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
30 901.67
Determine the value of the velocity at t=16 seconds using second order polynomial
interpolation using Newton’s divided difference polynomial method.
Example 3
The upward velocity of a rocket is given as a function of time in Table 3.
t ( s) v(t ) (m/s )
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
a) Determine the value of the velocity at t =16 seconds with third order polynomial
interpolation using Newton’s divided difference polynomial method.
b) Using the third order polynomial interpolant for velocity, find the distance covered by the
rocket from t=11 s to t=16 s .
c) Using the third order polynomial interpolant for velocity, find the acceleration of the rocket
at t=16 s .
Conclusion
LAB 14
TRAPEZOIDAL RULE OF INTEGRATION
Objective :
1. derive the trapezoidal rule of integration,
2. use the trapezoidal rule of integration to solve problems,
3. derive the multiple-segment trapezoidal rule of integration,
4. use the multiple-segment trapezoidal rule of integration to solve problems, and
5. derive the formula for the true error in the multiple-segment trapezoidal rule of
integration.
Diagram :
Example 1
( [ ] )
30
140000
x=∫ 2000 ln −9 . 8 t dt
8 140000−2100 t
a) Use the single segment trapezoidal rule to find the distance covered for t =8 to
t =30 seconds.
E
b) Find the true error, t for part (a).
c) Find the absolute relative true error for part (a).
Example 2
The vertical distance covered by a rocket from t =8 to t =30 seconds is given by
( [ ] )
30
140000
x=∫ 2000 ln −9 . 8 t dt
8 140000−2100 t
a) Use the two-segment trapezoidal rule to find the distance covered from t =8 to
t =30 seconds.
E
b) Find the true error, t for part (a).
c) Find the absolute relative true error for part (a).
Example 3
Use the multiple-segment trapezoidal rule to find the area under the curve
300 x
f ( x )=
1+e x
Approximat
n e Et |∈t|
Value
16
32
LAB 15
SIMPSON’S 1/3 RULE OF INTEGRATION
Objective
1. derive the formula for Simpson’s 1/3 rule of integration,
2. use Simpson’s 1/3 rule it to solve integrals,
3. develop the formula for multiple-segment Simpson’s 1/3 rule of integration,
4. use multiple-segment Simpson’s 1/3 rule of integration to solve integrals, and
5. derive the true error formula for multiple-segment Simpson’s 1/3 rule.
Simpson’s 1/3 rule is an extension of Trapezoidal rule where the integrand is approximated by a
second order polynomial.
Example 1
Example 2
Use 4-segment Simpson’s 1/3 rule to approximate the distance covered by a rocket in meters
from t =8 s to t =30 s as given by
30
140000
x 2000 ln 9.8t dt
8 140000 2100t
Fill out Values of Simpson’s 1/3 rule for Example 2 with multiple-segments
n Approximate Value Et
10