Mathematics 6.2 LabManual-1
Mathematics 6.2 LabManual-1
LAB MANUAL
MATDSCP6.2:
PRACTICAL’S ON NUMERICAL ANALYSIS
Contents
1 Program to find root of an equation using bisection and Regula-Falsi
methods 1
1.1.2 Program
1
print ( " Iteration : %d ,\ t x = % 0 . 6f \ t f ( x ) = % 0 . 6f " % (i ,x , f ( x ) ) )
if f ( a ) * f ( x ) < 0 :
b=x
else :
a=x
print ( f " The approximate root is { x } " )
1.1.3 Exercise
3. Use the bisection method in four stages to find the real root of the equation
x log10 x − 102 = 0.
2
1.2.2 Exercise
2. Compute the real root of x log10 x − 1.2 = 0 by the method of false position.
3. Use the method of false position to find the fourth root of 32.
4. Find the real root of the equation xex − 3 = 0 by Regula-Falsi method correct to
four decimal places.
def df ( x ) :
return 3 + sin ( x )
3
2.1.2 Exercise
2. Applying Newton-Raphson method to find the real root of the equation x4 −x−10 =
0 which is near to x = 2.
3. Using Newton-Raphson method find a real root of the equation sin x = 1 − x (near
0.5).
4. Using Newton-Raphson method find a real root of the equation x2 + x = cos x (near
0.5).
4
3 Program to solve system of algebraic equations us-
ing Gauss-elimination method
3.1 Program
Gauss elimination method to solve 10x+2y+z = 9; x+10y−z = −22; −2x+3y+10z = 22.
# Gauss elimination method to solve 10x + 2y + z = 9 ; x + 10y - z = - 22 ; - 2x + 3y + 10z =
22
import numpy as np
a = np . array ( [ [ 10 ,2 , 1 ] ,[1 , 10 , - 1 ] ,[ -2 ,3 , 10 ] ] )
b = np . array ( [9 , - 22 , 22 ] )
n = len ( b )
# Elimination Phase
for k in range (0 , n - 1 ) :
for i in range ( k +1 , n ) :
if a [i , k ] ! = 0 . 0 :
lam = a [i , k ] / a [k , k ]
a [i , k + 1 : n ] = a [i , k + 1 : n ] - lam * a [k , k + 1 : n ]
b [ i ] = b [ i ] - lam * b [ k ]
# Back substitution
for k in range ( n -1 , -1 , - 1 ) :
b [ k ] = ( b [ k ] - np . dot ( a [k , k + 1 : n ] ,b [ k + 1 : n ] ) ) / a [k , k ]
print ( " The solution of the given system is " ,b )
3.2 Exercise
Solve the following system of equations by Gauss elimination method.
1. 5x + 2y + z = 12; x + 4y + 2z = 15; x + 2y + 5z = 0
4.1 Program
Solve by Gauss-Jacobi method 10x + 2y + z = 9; x + 10y − z = −22; −2x + 3y + 10z = 22.
5
# Solve by Gauss - Jacobi method 10x + 2y + z = 9 ; x + 10y - z = - 22 ; - 2x + 3y + 10z = 22
f1 = lambda x ,y , z : ( 9 - 2 * y - z ) / 10
f2 = lambda x ,y , z : ( - 22 - x + z ) / 10
f3 = lambda x ,y , z : ( 22 + 2 * x - 3 * y ) / 10
# Intitial conditions
x0 , y0 , z0 =0 ,0 , 0
itr = 1
e = float ( input ( " Enter the tolerable error : " ) )
condition = True
print ( " \ n \ itr \ t x \ t y \ t z \ n " )
while condition :
x1 = f1 ( x0 , y0 , z0 )
y1 = f2 ( x0 , y0 , z0 )
z1 = f3 ( x0 , y0 , z0 )
print ( " % d \ t % 0 . 4f \ t % 0 . 4f \ t % 0 . 4f \ n " % ( itr , x1 , y1 , z1 ) )
e1 = abs ( x0 - x1 ) ; e2 = abs ( y0 - y1 ) ; e3 = abs ( z0 - z1 )
itr + = 1
x0 = x1 ; y0 = y1 ; z0 = z1
condition = e1 > e and e2 > e and e3 > e
print ( " \ n Solution : x = % 0 . 4f , y = % 0 . 4f , z = % 0 . 4f \ n " % ( x1 , y1 , z1 ) )
4.2 Exercise
Solve the following system of equations by Gauss-Jacobi method.
1. 5x + 2y + z = 12; x + 4y + 2z = 15; x + 2y + 5z = 0
6
itr = 1
e = float ( input ( " Enter the tolerable error : " ) )
condition = True
print ( " \ n \ itr \ t x \ t y \ t z \ n " )
while condition :
x1 = f1 ( x0 , y0 , z0 )
y1 = f2 ( x1 , y0 , z0 )
z1 = f3 ( x1 , y1 , z0 )
print ( " % d \ t % 0 . 4f \ t % 0 . 4f \ t % 0 . 4f \ n " % ( itr , x1 , y1 , z1 ) )
e1 = abs ( x0 - x1 ) ; e2 = abs ( y0 - y1 ) ; e3 = abs ( z0 - z1 )
itr + = 1
x0 = x1 ; y0 = y1 ; z0 = z1
condition = e1 > e and e2 > e and e3 > e
print ( " \ n Solution : x = % 0 . 4f , y = % 0 . 4f , z = % 0 . 4f \ n " % ( x1 , y1 , z1 ) )
5.2 Exercise
Solve the following system of equations by Gauss-Seidel method.
1. 5x − 2y + z = −4; x + 6y − 2z = −1; 3x + y + 5z = 13
7
6 Program to evaluate integral using Trapezoidal rule
6.1 Program
R6 1
Evaluate dx taking n = 6 using trapezoidal rule.
0 1 + x2
6.2 Exercise
R5
1. Evaluate log10 xdx taking n = 6.
1
R3 1
2. Evaluate dx taking n = 6.
1 1+x
R6 1
3. Evaluate dx taking n = 6.
0 1 + x2
8
7 Program to evaluate integral using Weddle’s rule
7.1 Program
R6 1
Evaluate dx taking n = 6 using Weddle’s rule.
0 1 + x2
7.2 Exercise
R5
1. Evaluate log10 xdx taking n = 6.
1
R3 1
2. Evaluate dx taking n = 6.
1 1+x
R6 1
3. Evaluate dx taking n = 6.
0 1 + x2
9
8 Program to evaluate integral using Simpson’s one-
third rule and Simpson’s three-eighth rule
8.1.1 Program
8.1.2 Exercise
R5
1. Evaluate log10 xdx taking n = 6.
1
R3 1
2. Evaluate dx taking n = 6.
1 1+x
R6 1
3. Evaluate dx taking n = 6.
0 1 + x2
8.2.1 Program
10
# Evaluate int_0 ^ 6 ( 1 /( 1 + x ^ 2 ) ) dx taking n = 6 using Simpson 's three - eighth
rule
x0 = int ( input ( " Lower limit of the interval : " ) )
xn = int ( input ( " Upper limit of the interval : " ) )
n = int ( input ( " Number of sub intervals : " ) )
def f ( x ) :
return ( 1 / ( 1 + x ** 2 ) )
h = ( xn - x0 ) / n
sum1 = f ( x0 ) + f ( xn )
for i in range (1 , n ) :
if i % 3 = = 0 :
sum1 = sum1 + 2 * f ( x0 + i * h )
else :
sum1 = sum1 + 3 * f ( x0 + i * h )
print ( " Estimated value of given integration : " ,3 * h / 8 * sum1 )
8.2.2 Exercise
R5
1. Evaluate log10 xdx taking n = 6.
1
R3 1
2. Evaluate dx taking n = 6.
1 1+x
R6 1
3. Evaluate dx taking n = 6.
0 1 + x2
11