0% found this document useful (0 votes)
12 views

NC v1.0

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

NC v1.0

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Karachi Institute of Economics & Technology

College of Computing & Information Sciences

Numerical
Computing
Lab Manual
Karachi Institute of Economics & Technology
College of Computing & Information Sciences

Numerical
Computing
Lab Manual
Compiled by:
Hafiz Muhammad Waqas

CID: ______________ Semester: ______________


Student ID: ________________________________
Student Name: _____________________________
Instructor Name: ___________________________
KARACHI INSTITUTE OF ECONOMICS & TECHNOLOGY
College of Computing & Information Sciences
Programming Fundamental Lab Manual

INDEX
Lab Course
Date Particulars Instructor Teacher
Signature Signature
Lab 1: ERRORS

Lab 2: DIFFERENTIATION

Lab 3: BISECTION , REGULA FALSI METHOD

Lab 4: SECANT METHOD

Lab 5: NEWTON RAPHSON

Lab 6: MULLER METHOD

Lab 7: SIMULTANEOUS LINEAR EQUATIONS

Lab 8: JACOBI METHOD

Lab 9: GAUSS ELIMINATION

Lab 10: GAUSS SEIDEL

Lab 11: LU DECOMPOSITION

Lab 12: LAGRANGIAN INTERPOLATION

Lab 13: NEWTON’S DIVIDED DIFFERENCE INTERPOLATION

Lab 14: TRAPEZOIDAL RULE OF INTEGRATION

Lab 15: SIMPSON’S 1/3 RULE OF INTEGRATION


Table of Contents
LAB 1 ERRORS..............................................................................................................................................1
LAB 2 DIFFERENTIATION..............................................................................................................................2
LAB 3 NON LINEAR EQUATIONS..................................................................................................................3
BISECTION METHOD....................................................................................................................................3
REGULA FALSI..............................................................................................................................................6
LAB 4 SECANT METHOD...............................................................................................................................9
LAB 5 NEWTON RAPHSON.........................................................................................................................11
LAB 6 MULLER METHOD............................................................................................................................13
LAB 7 SIMULTANEOUS LINEAR EQUATIONS..............................................................................................15
LAB 8 JACOBI METHOD..............................................................................................................................16
LAB 9 GAUSS ELIMINATION.......................................................................................................................18
LAB 10 GAUSS SEIDEL................................................................................................................................20
LAB 11 LU DECOMPOSITION......................................................................................................................22
LAB 12 LAGRANGIAN INTERPOLATION......................................................................................................24
LAB 13 NEWTON’S DIVIDED DIFFERENCE INTERPOLATION.......................................................................26
LAB 14 TRAPEZOIDAL RULE OF INTEGRATION...........................................................................................29
LAB 15 SIMPSON’S 1/3 RULE OF INTEGRATION.........................................................................................31
Course Title: Lab Work
Numerical Computing Version 1.0

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 )

c) the true error for part (a)

Floating Point Error:


A machine stores floating-point numbers in a hypothetical 10-bit binary word. It employs the
first bit for the sign of the number, the second one for the sign of the exponent, the next four
for the exponent, and the last four for the magnitude of the mantissa.

a) Find how 0.02832 will be represented in the floating-point 10-bit word.


b) What is the decimal equivalent of the 10-bit word representation of part (a)?

Round off Error:


1
a number like 3 may be represented as 0.333333 on a PC. Then the round off error in this case
1
−0 .333333=0 . 0000003 { 3̄ ¿
is 3 . Then there are other numbers that cannot be represented

College of Computing & Information Sciences 1


Course Title: Lab Work
Numerical Computing Version 1.0

exactly. For example, π and √ 2 are numbers that need to be approximated in computer
calculations.

College of Computing & Information Sciences 2


Course Title: Lab Work
Numerical Computing Version 1.0

LAB 2
DIFFERENTIATION
Primer of differentiation :
After reading this chapter, you should be able to:

1. understand the basics of differentiation,


2. relate the slopes of the secant line and tangent line to the derivative of a function,
3. find derivatives of polynomial, trigonometric and transcendental functions,

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

Figure : Calculation of the secant line for the function y=4 x 2 .

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

College of Computing & Information Sciences 3


Course Title: Lab Work
Numerical Computing Version 1.0

of the definition of a derivative.

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)

College of Computing & Information Sciences 4


Course Title: Lab Work
Numerical Computing Version 1.0

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 .

Algorithm for the bisection method

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

3. Now check the following


a) If f ( x ℓ )f ( x m )<0 , then the root lies between
x ℓ and x m ; then x ℓ =x ℓ and x u =x m .

b) If f ( x ℓ )f ( x m )>0 , then the root lies between


x m and x u ; then x ℓ =x m and x u =xu
.
c) If f ( x ℓ )f ( x m )=0 ; then the root is
x m . Stop the algorithm if this is true.
4. Find the new estimate of the root
x +x
xm = ℓ u
2

Find the absolute relative approximate error as

x new
m - x old
m
|∈a| = | |× 100
x new
m

where

x new
m = estimated root from present iteration

College of Computing & Information Sciences 5


Course Title: Lab Work
Numerical Computing Version 1.0

x old
m = estimated root from previous iteration

5. Compare the absolute relative approximate error


|∈a| with the pre-specified relative

error 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.
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
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 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 )
%

College of Computing & Information Sciences 6


Course Title: Lab Work
Numerical Computing Version 1.0

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

Every next Root finding formula in False Position Method

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 .

2. Estimate the root, x r of the equation f ( x )=0 as

xU f ( x L ) −x L f ( x U )
xr=
f ( x L )−f ( x U )

3. Now check the following

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.

4. Find the new estimate of the root

xU f ( x L ) −x L f ( x U )
xr=
f ( x L )−f ( x U )

Find the absolute relative approximate error as

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

5. Compare the absolute relative approximate error


|∈a|with the pre-specified relative error

tolerance
∈s . If|∈a|>∈s , then go to step 3, else stop the algorithm. Note one should also check

College of Computing & Information Sciences 8


Course Title: Lab Work
Numerical Computing Version 1.0

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

College of Computing & Information Sciences 9


Course Title: Lab Work
Numerical Computing Version 1.0

Conclusion

College of Computing & Information Sciences 10


Course Title: Lab Work
Numerical Computing Version 1.0

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

Figure 1 Geometrical representation of the secant method.

College of Computing & Information Sciences 11


Course Title: Lab Work
Numerical Computing Version 1.0

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.

College of Computing & Information Sciences 12


Course Title: Lab Work
Numerical Computing Version 1.0

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) [xi, f (xi)]

f (xi+1)

θ
x
xi+2 xi+1 xi

Figure : Geometrical illustration of the Newton-Raphson method

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

College of Computing & Information Sciences 13


Course Title: Lab Work
Numerical Computing Version 1.0

f ( xi)
x i +1 = x i− '
f ( xi)

3. Find the absolute relative approximate error


|∈a| as
xi+1 − x i
|∈a| = | |×10 0
x i+1

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.

College of Computing & Information Sciences 14


Course Title: Lab Work
Numerical Computing Version 1.0

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

Use the Newton-Raphson method of finding roots of equations to find

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

College of Computing & Information Sciences 15


Course Title: Lab Work
Numerical Computing Version 1.0

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

College of Computing & Information Sciences 16


Course Title: Lab Work
Numerical Computing Version 1.0

- 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

College of Computing & Information Sciences 17


Course Title: Lab Work
Numerical Computing Version 1.0

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

College of Computing & Information Sciences 19


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

College of Computing & Information Sciences 20


Course Title: Lab Work
Numerical Computing Version 1.0

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 :

College of Computing & Information Sciences 21


Course Title: Lab Work
Numerical Computing Version 1.0

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

1. Forward Elimination of Unknowns: In this step, the unknown is eliminated in each


equation starting with the first equation. This way, the equations are reduced to one
equation and one unknown in each equation.
2. Back Substitution: In this step, starting from the last equation, each of the unknowns is
found.
Problem : The upward velocity of a rocket is given at three different times in Table 1.
Table 1 Velocity vs. time data.

Time, t Velocity, v
(s) (m/s)

5 106.8

8 177.2

12 279.2

The velocity data is approximated by a polynomial as

v ( t )=a 1 t 2 +a2 t +a3 , 5≤t≤12

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

College of Computing & Information Sciences 22


Course Title: Lab Work
Numerical Computing Version 1.0

Find the values of


a 1 , a 2 , and a3 using the Naïve Gauss elimination method. Find the velocity at
t=6 , 7 .5 ,9 , 11 seconds.

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 :

College of Computing & Information Sciences 23


Course Title: Lab Work
Numerical Computing Version 1.0

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.

Solve a set of simultaneous linear equations using Gauss-Seidel method.

Process :

a n1 x 1 +an 2 x 2 +an 3 x 3 +. ..+ann x n =cn

c n−an 1 x 1 −a n2 x 2 −……−an , n−1 x n−1


x n=
a nn

These equations can be rewritten in a summation form as (for any row i)

¿ j≠i ¿¿¿ ¿ ¿,i=1,2,…,n.¿


n aii

Example 1
The upward velocity of a rocket is given at three different times in the following table

Table 1 Velocity vs. time data.

Velocity, v
Time, t (s)
(m/s)

5 106.8

8 177.2

12 279.2

College of Computing & Information Sciences 24


Course Title: Lab Work
Numerical Computing Version 1.0

The velocity data is approximated by a polynomial as

v ( t )=a 1 t 2 +a2 t +a3 , 5≤t≤12

Find the values of


a 1 , a 2 , and a 3 using the Gauss-Seidel method. Assume an initial guess of the
solution as

[ ][]
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 =

College of Computing & Information Sciences 26


Course Title: Lab Work
Numerical Computing Version 1.0

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

College of Computing & Information Sciences 27


Course Title: Lab Work
Numerical Computing Version 1.0

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 :

College of Computing & Information Sciences 28


Course Title: Lab Work
Numerical Computing Version 1.0

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

where n in f n ( x ) stands for the n


th
order polynomial that approximates the function
y=f ( x ) given at n+1 data points as ( x 0 , y 0 ) , ( x 1 , y 1 ) , .. .. . ., ( x n−1 , y n−1 ) , ( x n , y n ) , and

¿ j≠i ¿ ¿ ¿ ¿
n

College of Computing & Information Sciences 29


Course Title: Lab Work
Numerical Computing Version 1.0

Li ( x ) is a weighting function that includes a product of n−1 terms with terms of j =i


omitted.

Example 1
The upward velocity of a rocket is given as a function of time in Table 1.

Table 1 Velocity as a function of time.

t v (t )
(s) (m/s)

0 0

10 227.04

15 362.78

20 517.35

22.
602.97
5

30 901.67

Figure 2 Graph of velocity vs. time data for the rocket


example.

College of Computing & Information Sciences 30


Course Title: Lab Work
Numerical Computing Version 1.0

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

College of Computing & Information Sciences 31


Course Title: Lab Work
Numerical Computing Version 1.0

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.

Table 2 Velocity as a function of time.

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.

Table 3 Velocity as a function of time.

t ( s) v(t ) (m/s )

0 0

College of Computing & Information Sciences 32


Course Title: Lab Work
Numerical Computing Version 1.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

College of Computing & Information Sciences 33


Course Title: Lab Work
Numerical Computing Version 1.0

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

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

College of Computing & Information Sciences 34


Course Title: Lab Work
Numerical Computing Version 1.0

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

from x=0 to x=10 .


10
∫ 300 xx dx
Table 2 Fill values obtained using multiple-segment trapezoidal rule for 0 1+e .

Approximat
n e Et |∈t|
Value

College of Computing & Information Sciences 35


Course Title: Lab Work
Numerical Computing Version 1.0

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

College of Computing & Information Sciences 36


Course Title: Lab Work
Numerical Computing Version 1.0

The distance covered by a rocket in meters from t =8 s to t =30 s is given by


30
  140000  
x   2000 ln    9 . 8t  dt
8   140000  2100t  

a) Use Simpson’s 1/3 rule to find the approximate value of x .


b) Find the true error,
Et .

c) Find the absolute relative true error,


|∈t|.

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  

a) Use four segment Simpson’s 1/3rd Rule to estimate x.


b) Find the true error,
Et for part (a).

c) Find the absolute relative true error,


|∈t|for part (a).

Fill out Values of Simpson’s 1/3 rule for Example 2 with multiple-segments

n Approximate Value Et

10

College of Computing & Information Sciences 37

You might also like