BMATE201_LAB MANUAL
BMATE201_LAB MANUAL
of
Second Semester
Mathematics-II for Electrical and Electronics
Engineering stream
(BMATE201)
NAME : ________________________________
SUBJECT : _________________________________
BRANCH : _________________________________
SECTION : _________________________________
Compiled by:
1|Page
Course Title: Mathematics-II for Electrical and Electronics
Engineering stream
Course Code: BMATE201 CIE Marks 50
Contents
Lab 1: Finding gradient, divergent, curl and their geometrical interpretation and Verification
of Green’s theorem
Lab 2: Computation of basis and dimension for a vector space and graphical representation
of linear transformation
Lab 4: Computing Laplace transform and inverse Laplace transform of standard functions
Lab 7: Interpolation /Extrapolation using Newton’s forward and backward difference formula
1 𝑟𝑑
Lab 8: Computation of area under the curve using Trapezoidal, Simpson’s (3) and
3 𝑡ℎ
Simpson’s (8) rule
Lab 9: Solution of ODE of first order and first degree by Taylor’s series and Modified
Euler’s method
Lab 10: Solution of ODE of first order and first degree by Runge-Kutta 4th order method
and Milne’s predictor and corrector method
2|Page
1
Instructions and method of evaluation
3. Viva questions shall be asked in labs and attendance also can be considered for everyday
lab evaluation.
5. Scaled-down marks of write-up evaluations and test added will be CIE marks for the
laboratory component for 25 marks.
6. Student must score minimum 4 marks out of 10 in Lab IA & 6 out of 15 in write-ups to
pass lab component.
3|Page
2
LAB 1: Finding gradient, divergent, curl and their
geometrical interpretation and Verification of Green’s
theorem
1.1 Objectives:
Use python
1.2 Method I:
1. To find gradient of ϕ = x2 y + 2xz − 4.
# To find gradient of scalar point function .
from sympy . vector import *
from sympy import symbols
N = CoordSys3D ( 'N ') # Setting the coordinate system
x ,y , z = symbols ( 'x y z ')
A = N . x ** 2 * N . y + 2 * N . x * N . z - 4 # Variables x ,y , z to be used with coordinate
system N
delop = Del () # Del operator
display ( delop ( A ) ) # Del operator applied to A
gradA = gradient ( A ) # Gradient function is used
print ( f " \ n Gradient of { A } is \ n " )
display ( gradA )
3
display ( divergence ( A ) )
4
2. To find divergence of F⃗ = x2 y î + yz 2 ĵ + x2 z k̂.
# To find divergence of F = x ^ 2yi + yz ^ 2j + x ^ 2zk
from sympy . physics . vector import *
from sympy import var
var ( 'x ,y , z ')
v = ReferenceFrame ( 'v ')
F = v [ 0 ] ** 2 * v [ 1 ] * v . x + v [ 1 ] * v [ 2 ] ** 2 * v . y + v [ 0 ] ** 2 * v [ 2 ] * v . z
G = divergence (F , v )
F = F . subs ( [ ( v [ 0 ] ,x ) ,( v [ 1 ] ,y ) ,( v [ 2 ] ,z ) ] )
print ( " Given vector point function is " )
display ( F )
G = G . subs ( [ ( v [ 0 ] ,x ) ,( v [ 1 ] ,y ) ,( v [ 2 ] ,z ) ] )
print ( " Divergence of F = " )
display ( G )
G = G . subs ( [ ( v [ 0 ] ,x ) ,( v [ 1 ] ,y ) ,( v [ 2 ] ,z ) ] )
print ( " curl of F = " )
display ( G )
5
1.4 Green’s theorem
Statement of Green’s theorem in the plane: If P (x, y) and Q(x, y) be two con-
tinuous functions having continuous partial derivatives in a region R of the xy-plane,
bounded by a simple closed curve C, then
I Z Z
∂Q ∂P
(P dx + Qdy) = − dxdy.
∂x ∂y
R
H
1. Using Green’s theorem, evaluate [(x + 2y)dx + (x − 2y)dy], where c is the region
c
bounded by coordinate axes and the line x = 1 and y = 1.
from sympy import *
var ( 'x , y ')
p=x+2*y
q=x-2*y
f = diff (q , x ) - diff (p , y )
soln = integrate (f , [x ,0 , 1 ] ,[y ,0 , 1 ] )
print ( " I = " , soln )
I= -1
H
2. Using Green’s theorem, evaluate [(xy + y 2 )dx + x2 dy], where c is the closed curve
c
bounded by y = x and y = x2 .
from sympy import *
var ( 'x , y ')
p = x * y + y ** 2
q = x ** 2
f = diff (q , x ) - diff (p , y )
soln = integrate (f , [y , x ** 2 , x ] ,[x ,0 , 1 ] )
print ( " I = " , soln )
I= -1/20
1.5 Exercise:
1. If u = x + y + z, v = x2 + y 2 + z 2 , w = yz + zx + xy, find gradu, gradv and gradw.
Ans: î + ĵ + k̂, 2(xî + y ĵ + z k̂), (y + z)î + (z + x)ĵ + (z + x)k̂.
2. Evaluate divF and curlF at the point (1,2,3), given that F⃗ = x2 yz î+xy 2 z ĵ +xyz 2 k̂.
Ans: 6xyz, x(z 2 − y 2 )î + y(x2 − z 2 )ĵ + z(y 2 − x2 )k̂.
3. Prove that the vector (yz − x2 )î + (4y − z 2 x)ĵ + (2xz − 4z)k̂ is solenoidal.
4. Find the vector normal to the surface xy 3 z 2 = 4 at the point (−1, −1, 2).
Ans: −4î − 12ĵ + 4k̂.
6
⃗ = xî + y ĵ + z k̂, show that (i) ∇ · R
5. If R ⃗ = 3, (ii) ∇ × R
⃗ = 0.
H
6. Using Green’s theorem, evaluate [(3x + 4y)dx + (2x − 3y)dy], where c is the
c
boundary of the circle x2 + y 2 = 4.
Ans: −8π
7
LAB 2: Computation of basis and dimension for a vec-
tor space and graphical representation of linear trans-
formation
2.1 Objectives:
Use python
10
8
2.3 Dimension of Vector Space
Find the dimension of subspace spanned by the vectors (1, 2, 3), (2, 3, 1) and (3, 1, 2).
import numpy as np
Extract the linearly independent rows in given matrix : Basis of Row space
from numpy import *
import sympy as sp
A = [ [1 , -1 ,1 , 1 ] ,[2 , -5 ,2 , 2 ] ,[3 , -3 ,5 , 3 ] ,[4 , -4 ,4 , 4 ] ]
AB = array ( A )
S = shape ( A )
n = len ( A )
for i in range ( n ) :
if AB [i , i ] = = 0 :
ab = copy ( AB )
for k in range ( i +1 , S [ 0 ] ) :
if ab [k , i ] ! = 0 :
ab [i , : ] = AB [k , : ]
ab [k , : ] = AB [i , : ]
AB = copy ( ab )
for j in range ( i +1 , n ) :
Fact = AB [j , i ] / AB [i , i ]
for k in range (i , n ) :
AB [j , k ] = AB [j , k ] - Fact * AB [i , k ]
display ( " REF of given matrix : " , sp . Matrix ( AB ) )
temp = { (0 , 0 , 0 , 0 ) }
result = [ ]
for idx , row in enumerate ( map ( tuple , AB ) ) :
if row not in temp :
result . append ( idx )
print ( " \ n Basis are non - zero rows of A : " )
display ( sp . Matrix ( AB [ result ] ) )
11
9
2.4 Graphical representation of a transformation
2.4.1 Horizontal stretch:
Represent the horizontal stretch transformation T : R2 ßR2 geometrically
Find the image of vector (10, 0) when it is stretched horizontally by 2 units.
import numpy as np
import matplotlib . pyplot as plt
V = np . array ( [ [ 10 , 0 ] ] )
origin = np . array ( [ [0 , 0 , 0 ] ,[0 , 0 , 0 ] ] ) # origin point
A = np . matrix ( [ [2 , 0 ] ,[0 , 1 ] ] )
V1 = np . matrix ( V )
V2 = A * np . transpose ( V1 )
V2 = np . array ( V2 )
plt . quiver ( * origin , V [ : ,0 ] , V [ : ,1 ] , color = [ 'b '] , scale = 50 )
plt . quiver ( * origin , V2 [0 , : ] , V2 [1 , : ] , color = [ 'r '] , scale = 50 )
plt . show ()
Another example.
from math import pi , sin , cos
12
10
import matplotlib . pyplot as plt
import numpy as np
A = np . array ( [ [2 , 0 ] ,[0 , 1 ] ] )
A_coords = A@coords
x_LT1 = A_coords [0 , : ]
y_LT1 = A_coords [1 , : ]
# Plot the points . x and y are original vectors , x_LT1 and y_LT1 are
images
ax . plot (x ,y , ' ro ')
ax . plot ( x_LT1 , y_LT1 , ' bo ')
13
11
2.4.2 Reflection:
Represent the reflection transformation T : R2 → R2 geometrically.
Find the image of vector (10, 0) when it is reflected about y axis.
import numpy as np
import matplotlib . pyplot as plt
V = np . array ( [ [ 10 , 0 ] ] )
origin = np . array ( [ [0 , 0 , 0 ] ,[0 , 0 , 0 ] ] ) # origin point
A = np . matrix ( [ [ -1 , 0 ] ,[0 , 1 ] ] )
V1 = np . matrix ( V )
V2 = A * np . transpose ( V1 )
V2 = np . array ( V2 )
plt . quiver ( * origin , V [ : ,0 ] , V [ : ,1 ] , color = [ 'b '] , scale = 50 )
plt . quiver ( * origin , V2 [0 , : ] , V2 [1 , : ] , color = [ 'r '] , scale = 50 )
plt . show ()
Another example.
B = np . array ( [ [ -1 , 0 ] ,[0 , 1 ] ] )
B_coords = B@coords
x_LT2 = B_coords [0 , : ]
y_LT2 = B_coords [1 , : ]
# Plot the points . x and y are original vectors , x_LT1 and y_LT1 are
images
ax . plot (x ,y , ' ro ')
ax . plot ( x_LT2 , y_LT2 , ' bo ')
14
12
ax . axhline ( y =0 , color = " k " , ls = " : " )
ax . grid ( True )
ax . axis ( [ -2 ,2 , -1 , 2 ] )
ax . set_aspect ( ' equal ')
ax . set_title ( " Reflection " ) ;
2.4.3 Rotation:
Represent the rotation transformation T : R2 → R2 geometrically.
Find the image of vector (10, 0) when it is rotated by π/2 radians.
import numpy as np
import matplotlib . pyplot as plt
V = np . array ( [ [ 10 , 0 ] ] )
origin = np . array ( [ [0 , 0 , 0 ] ,[0 , 0 , 0 ] ] ) # origin point
A = np . matrix ( [ [0 , - 1 ] ,[1 , 1 ] ] )
V1 = np . matrix ( V )
V2 = A * np . transpose ( V1 )
V2 = np . array ( V2 )
plt . quiver ( * origin , V [ : ,0 ] , V [ : ,1 ] , color = [ 'b '] , scale = 50 )
plt . quiver ( * origin , V2 [0 , : ] , V2 [1 , : ] , color = [ 'r '] , scale = 50 )
plt . show ()
15
13
Another example.
theta = pi / 6
R = np . array ( [ [ cos ( theta ) ,- sin ( theta ) ] ,[ sin ( theta ) , cos ( theta ) ] ] )
R_coords = R@coords
x_LT3 = R_coords [0 , : ]
y_LT3 = R_coords [1 , : ]
# Plot the points . x and y are original vectors , x_LT1 and y_LT1 are
images
ax . plot (x ,y , ' ro ')
ax . plot ( x_LT3 , y_LT3 , ' bo ')
16
14
2.4.4 Shear Transformation
Represent the Shear transformation T : R2 → R2 geometrically.
Find the image of (2, 3) under shear transformation.
import numpy as np
import matplotlib . pyplot as plt
V = np . array ( [ [2 , 3 ] ] )
origin = np . array ( [ [0 , 0 , 0 ] ,[0 , 0 , 0 ] ] ) # origin point
A = np . matrix ( [ [1 , 2 ] ,[0 , 1 ] ] )
V1 = np . matrix ( V )
V2 = A * np . transpose ( V1 )
V2 = np . array ( V2 )
print ( " Image of given vectors is : " , V2 )
plt . quiver ( * origin , V [ : ,0 ] , V [ : ,1 ] , color = [ 'b '] , scale = 20 )
plt . quiver ( * origin , V2 [0 , : ] , V2 [1 , : ] , color = [ 'r '] , scale = 20 )
plt . show ()
17
15
Another example.
S = np . array ( [ [1 , 2 ] ,[0 , 1 ] ] )
S_coords = S@coords
x_LT4 = S_coords [0 , : ]
y_LT4 = S_coords [1 , : ]
# Plot the points . x and y are original vectors , x_LT1 and y_LT1 are
images
ax . plot (x ,y , ' ro ')
ax . plot ( x_LT4 , y_LT4 , ' bo ')
18
16
2.4.5 Composition
Represent the composition of two 2D transformations.
Find the image of vector (10, 0) when it is rotated by π/2 radians then stretched hori-
zontally 2 units.
import numpy as np
import matplotlib . pyplot as plt
V = np . array ( [ [2 , 3 ] ] )
origin = np . array ( [ [0 , 0 , 0 ] ,[0 , 0 , 0 ] ] ) # origin point
A = np . matrix ( [ [0 , - 1 ] ,[1 , 0 ] ] )
B = np . matrix ( [ [2 , 0 ] ,[0 , 1 ] ] )
V1 = np . matrix ( V )
V2 = A * np . transpose ( V1 )
V3 = B * V2
V2 = np . array ( V2 )
V3 = np . array ( V3 )
print ( " Image of given vectors is : " , V3 )
plt . quiver ( * origin , V [ : ,0 ] , V [ : ,1 ] , color = [ 'b '] , scale = 20 )
plt . quiver ( * origin , V2 [0 , : ] , V2 [1 , : ] , color = [ 'r '] , scale = 20 )
plt . quiver ( * origin , V3 [0 , : ] , V3 [1 , : ] , color = [ 'g '] , scale = 20 )
plt . title ( ' Blue = original , Red = Rotated , Green = Rotated + Streached ')
plt . show ()
19
17
Another example.
C = np . array ( [ [ - cos ( theta ) , sin ( theta ) ] ,[ sin ( theta ) , cos ( theta ) ] ] )
C_coords = C@coords
x_LT5 = C_coords [0 , : ]
y_LT5 = C_coords [1 , : ]
# Plot the points . x and y are original vectors , x_LT1 and y_LT1 are
images
ax . plot (x ,y , ' ro ')
ax . plot ( x_LT5 , y_LT5 , ' bo ')
20
18
2.5 Exercise:
1. Verify the rank nullity theorem for the following linear transformation
a) Horizontal stretch
b) Reflection
c) Shear
d) Rotation
21
19
LAB 3: Visualization in time and frequency domain
of standard functions
3.1 Objectives:
Use python
1. to use the standard in-built function of Laplace transform.
Represent the Laplace transform of f (t) = e2t , both in time and frequency domains
from sympy import *
import matplotlib . pyplot as plt
s , t = symbols ( 's t ' , positive = True )
f = exp ( 2 * t )
F = lapla ce_tra nsform (f ,t , s )
print ( ' The Laplace Transform of f is ' ,F [ 0 ] )
print ( F [ 0 ] . expand () )
22
20
p1 = plot (f , show = False , xlim = ( - 10 , 10 ) , line_color = ' blue ' , legend = True )
p2 = plot ( F [ 0 ] , show = False , xlim = ( - 10 , 10 ) , line_color = ' red ' , legend = True )
3.2 Exercise:
1. Represent the following functions in time and frequency domains
a) cos t
b) sinh t
c) cosh t
d) e−2t
23
21
LAB 4: Computing Laplace transform and inverse
Laplace transform of standard functions
4.1 Objectives:
Use python
24
22
4.3 Inverse Laplace Transform
# import i n v e r s e _ l a p l a c e _ t r a n s f o r m
import sympy as sp
s = sp . symbols ( 's ')
# Using i n v e r s e _ l a p l a c e _ t r a n s f o r m () method
gfg = sp . i n v e r s e _ l a p l a c e _ t r a n s f o r m ( a / ( s ** 2 + a ** 2 ) , s , t)
print ( gfg )
gfg = sp . i n v e r s e _ l a p l a c e _ t r a n s f o r m ( s / ( s ** 2 + a ** 2 ) , s , t)
print ( gfg )
gfg = sp . i n v e r s e _ l a p l a c e _ t r a n s f o r m ( 1 / ( s ** 4 ) , s , t )
print ( gfg )
gfg = sp . i n v e r s e _ l a p l a c e _ t r a n s f o r m ( 1 / ( s ** 2 - a ** 2 ) , s , t)
print ( gfg )
gfg = sp . i n v e r s e _ l a p l a c e _ t r a n s f o r m ( s / ( s ** 2 - a ** 2 ) , s , t)
print ( gfg )
gfg = sp . i n v e r s e _ l a p l a c e _ t r a n s f o r m ( 1 / ( s ** 2 - a ** 2 ) , s , t)
print ( gfg )
gfg = sp . i n v e r s e _ l a p l a c e _ t r a n s f o r m (( 2 ) / ( s - 4 ) , s , t )
print ( gfg )
25
23
4.4 Exercise:
1. Find the Laplace transform of t cos 4t
s2 −16
Ans: s4 +32s2 +256
sin 2t
2. Find the Laplace transform of t
Ans: tan−1 (2/s)
s2 +s−2
3. Find the inverse Laplace transform of s(s−2)(s+3)
4 −3t
Ans: 52 e2t + 13 + 15
e
s
4. Find the inverse Laplace transform of s4 +4a4
sin at sinh at
Ans: 2a2
26
24
LAB 5: Laplace transform of convolution of two func-
tions
5.1 Objectives:
Use python
1. to calculate Laplace Transform for convolution of two functions.
2. to verify Convolution Theorem for two given functions.
Let f (t), g(t) be two functions, then the convolution f ⊛ g is given by
Zt
(f ⊛ g)(t) = f (τ )g(t − τ )dτ
0
# Given functions
f = t
g = exp ( t )
# Convolution
fog = integrate (
f . subs ( { t : tau } ) * g . subs ( { t : t - tau } ) ,
( tau ,0 , t )
)
# Calculation
FOG = LT ( fog )
27
25
2. Verify the Convolution Theorem for Laplace transform of the functions f (t) = t and
g(t) = et .
from sympy import *
t , s , tau = symbols ( 't ,s , tau ')
# Given functions
f = t
g = exp ( t )
# Convolution
fog = integrate (
f . subs ( { t : tau } ) * g . subs ( { t : t - tau } ) ,
( tau ,0 , t )
)
# Calculation
F = LT ( f )
G = LT ( g )
FOG = LT ( fog )
28
26
5.2 Exercise:
sin 3t
1. Find the Laplace transformation of the convolution of f (t) = sin t and g(t) = .
3
Verify the convolution theorem for Laplace transformation.
sin 3t 1 1 sin 3t
L sin t ⊛ = 2 × = L(sin t) × L .
3 s + 1 s2 + 9 3
2. Find the Laplace transformation of the convolution of f (t) = t cos t and g(t) =
t2 sin t. Verify the convolution theorem for Laplace transform.
11s8 + 76s6 + 1058s4 − 1904s2 + 171
L (t cos t ⊛ t2 sin t) =
8(s10 + 5s8 + 10s6 + 10s4 + 5s2 + 1)
2
s −1 2(3s2 − 1)
= 4 × = L(t cos t) × L (t2 sin t) .
s + 2s2 + 1 s6 + 3s4 + 3s2 + 1
3. Find the Laplace transformation of the convolution of f (t) = cosh 3t and g(t) = t5 .
Verify the convolution theorem for Laplace transform.
5 120 s 120 5
L (cosh 3t ⊛ t ) = 5 2 = 2 × 6 = L(cosh 3t) × L (t ) .
s (s − 9) s −9 s
4t + 5
4. Find the Laplace transformation of the convolution of f (t) = and g(t) =
e2t
4t − 5
. Verify the convolution theorem for Laplace transform.
e−2t
196 − 25s2
4t + 5 4t − 5 5s + 14 14 − 5s
L 2t
⊛ −2t
= = ×
e e s4
− 8s2 + 16 s2 + 4s + 4 s2 − 4s + 4
4t + 5 4t − 5
=L ×L
e 2t e−2t
29
27
LAB 6: Solution of algebraic and transcendental equa-
tion by Regula-Falsi and Newton-Raphson method
6.1 Objectives:
Use python
for i in range (1 , N + 1 ) :
c=(a*f(b)-b*f(a))/(f(b)-f(a))
if (( f ( a ) * f ( c ) < 0 ) ) :
b=c
else :
a=c
print ( ' itration % d \ t the root % 0 . 3f \ t function value % 0 . 3f \ n '%
(i ,c , f ( c ) ) ) ;
30
28
# Regula Falsi method while loop2
from sympy import *
x = Symbol ( 'x ')
g = input ( ' Enter the function ') # % x ^3 - 2 *x - 5 ; % function
f = lambdify (x , g )
a = float ( input ( ' Enter a valus : ') ) # 2
b = float ( input ( ' Enter b valus : ') ) # 3
N = float ( input ( ' Enter tolarence : ') ) # 0 . 001
x=a;
c=b;
i=0
while ( abs ( x - c ) > = N ) :
x=c
c = (( a * f ( b ) - b * f ( a ) ) / ( f ( b ) - f ( a ) ) ) ;
if (( f ( a ) * f ( c ) < 0 ) ) :
b=c
else :
a=c
i=i+1
print ( ' itration % d \ t the root % 0 . 3f \ t function value % 0 . 3f \ n '%
(i ,c , f ( c ) ) ) ;
print ( ' final value of the root is % 0 . 5f '% c )
31
29
df = lambdify (x , dg )
x0 = float ( input ( ' Enter the intial approximation ') ) ; # x0 = 1
n = int ( input ( ' Enter the number of iterations ') ) ; #n=5;
for i in range (1 , n + 1 ) :
x1 = ( x0 - ( f ( x0 ) / df ( x0 ) ) )
print ( ' itration % d \ t the root % 0 . 3f \ t function value % 0 . 3f \ n '%
(i , x1 , f ( x1 ) ) ) ; # print all
iteration value
x0 = x1
6.4 Exercise:
1. Find a root of the equation 3x = cos x+1, between 0 and 1, by Regula-falsi method.
Perform 5 iterations.
Ans: 0.607
32
30
LAB 7: Interpolation /Extrapolation using Newton’s
forward and backward difference formula
7.1 Objectives:
Use python
1. to interpolate using Newton’s Forward interpolation method.
for i in range (0 , n ) :
print ( '% 0 . 2f ' % ( x [ i ] ) , end = ' ')
for j in range (0 , n - i ) :
print ( '\ t \ t % 0 . 2f ' % ( y [ i ] [ j ] ) , end = ' ')
print ()
# obtaining the polynomial
t = symbols ( 't ')
f = [ ] # f is a list type data
p=(t-x[0])/(x[1]-x[0])
f . append ( p )
for i in range (1 , n - 1 ) :
f . append ( f [ i - 1 ] * ( p - i ) / ( i + 1 ) )
poly = y [ 0 ] [ 0 ]
for i in range ( n - 1 ) :
poly = poly + y [ 0 ] [ i + 1 ] * f [ i ]
33
31
simp_poly = simplify ( poly )
print ( '\ nTHE INTERPOLATING POLYNOMIAL IS \ n ') ;
pprint ( simp_poly )
# if you want to interpolate at some point the next session will help
inter = input ( ' Do you want to interpolate at a point ( y / n ) ? ') # y
if inter = = 'y ':
a = float ( input ( ' enter the point ') ) # 2
interpol = lambdify (t , simp_poly )
result = interpol ( a )
print ( '\ nThe value of the function at ' ,a , ' is \ n ' , result ) ;
34
32
print ( ' Enter data for x and y : ')
for i in range ( n ) :
x [ i ] = float ( input ( 'x [ '+ str ( i ) + ' ]= ') )
y [ i ] [ 0 ] = float ( input ( 'y [ '+ str ( i ) + ' ]= ') )
for i in range (0 , n ) :
print ( '% 0 . 2f ' % ( x [ i ] ) , end = ' ')
for j in range (0 , i + 1 ) :
print ( '\ t % 0 . 2f ' % ( y [ i ] [ j ] ) , end = ' ')
print ()
p=(t-x[n-1])/(x[1]-x[0])
f . append ( p )
for i in range (1 , n - 1 ) :
f . append ( f [ i - 1 ] * ( p + i ) / ( i + 1 ) )
poly = y [ n - 1 ] [ 0 ]
print ( poly )
for i in range ( n - 1 ) :
poly = poly + y [ n - 1 ] [ i + 1 ] * f [ i ]
simp_poly = simplify ( poly )
print ( '\ nTHE INTERPOLATING POLYNOMIAL IS \ n ') ;
pprint ( simp_poly )
# if you want to interpolate at some point the next session will help
inter = input ( ' Do you want to interpolate at a point ( y / n ) ? ')
if inter = = 'y ':
a = float ( input ( ' enter the point ') )
interpol = lambdify (t , simp_poly )
result = interpol ( a )
print ( '\ nThe value of the function at ' ,a , ' is \ n ' , result ) ;
35
33
7.2 Exercise:
1. Obtain the interpolating polynomial for the following data
x: 0 1 2 3
y: 1 2 1 10
Ans: 2x3 − 7x2 + 6x + 1
2. Find the number of men getting wage Rs. 100 from the following table:
wage: 50 150 250 350
No. of men: 9 30 35 42
Ans: 23 men
3. Using Newton’s backward interpolation method obtain y(160) for the following data
x: 100 150 200 250 300
y: 10 13 15 17 18
Ans: 13.42
4. Using Newtons forward interpolation polynomial and calculate y(1) and y(10).
x: 3 4 5 6 7 8 9
y: 4.8 8.4 14.5 23.6 36.2 52.8 73.9
Ans: 3.1 and 100
36
34
LAB 8: Computation of area under the curve using
rd th
Trapezoidal, Simpson’s 13 and Simpsons 38
rule
8.1 Objectives:
Use python
1. to find area under the curve represented by a given function using Trapezoidal rule.
rd
2. to find area under the curve represented by a given function using Simpson’s 13
rule.
th
3. to find area under the curve represented by a given function using Simpson’s 83
rule.
4. to find the area below the curve when discrete points on the curve are given.
# Input section
lower_limit = float ( input ( " Enter lower limit of integration : " ) )
upper_limit = float ( input ( " Enter upper limit of integration : " ) )
sub_interval = int ( input ( " Enter number of sub intervals : " ) )
# Print result
print ( " Integration result by Trapezoidal method is : " , result )
37
35
1 rd
8.3 Simpson’s 3 Rule
R5 1
Evaluate 1+x2
.
0
# Definition of the function to integrate
def my_func ( x ) :
return 1 / ( 1 + x ** 2 )
def simpson13 ( x0 , xn , n ) :
h = ( xn - x0 ) / n # calculating step size
# Finding sum
integration = ( my_func ( x0 ) + my_func ( xn ) )
k = x0
for i in range (1 , n ) :
if i % 2 = = 0 :
integration = integration + 4 * my_func ( k )
else :
integration = integration + 2 * my_func ( k )
k += h
# Finding final integration value
integration = integration * h * ( 1 / 3 )
return integration
# Input section
lower_limit = float ( input ( " Enter lower limit of integration : " ) )
upper_limit = float ( input ( " Enter upper limit of integration : " ) )
sub_interval = int ( input ( " Enter number of sub intervals : " ) )
38
36
h = (b - a) / n
s = f(a) + f(b)
for i in range (1 , n , 3 ) :
s += 3 * f(a + i * h)
for i in range (3 , n -1 , 3 ) :
s += 3 * f(a + i * h)
for i in range (2 , n -2 , 3 ) :
s += 2 * f(a + i * h)
return s * 3 * h / 8
def f ( x ) :
return 1 / ( 1 + x ** 2 ) # function here
a = 0 # lower limit
b = 6 # upper limit
n = 6 # number of sub intervals
1.27631
8.5 Exercise:
Z1
x2 1
1. Evaluate the integral 3
dx using Simpson’s rule.
1+x 3
0
Ans: 0.23108
Z0.6
3 2
2. Use Simpson’s rule to find e−x dx by taking seven ordinates.
8
0
Ans: 0.5351
Zπ
3. Evaluate using trapezoidal rule sin2 xdx. Take n = 6.
0
Ans: π/2
4. A solid of revolution is formed by rotating about the x-axis, the area between the
x-axis, the lines x = 0 and x = 1, and a curve through the points with the following
co-ordinates:
x y
0.00 1.0000
0.25 0.9896
0.50 0.9589
0.75 0.9089
1.00 0.8415
39
37
1
Estimate the volume of the solid formed using Simpson’s rd rule. Hint: Required
R1 2 3
volume is 0 y ∗ πdx. **[Ans: 2.8192]**
5. The velocity v(km/min) of a moped which starts from rest, is given at fixed intervals
of time t(min) as follows:
t: 2 4 6 8 10 12 14 16 18 20
v: 10 18 25 29 32 20 11 5 2 0
Estimate approximately the distance covered in twenty minutes.
Answer for 5.
We know that ds/dt=v. So to get distance (s) we have to integrate.
Here h = 2.2, v0 = 0, v1 = 10, v2 = 18, v3 = 25 etc.
# we shall use simpson 's 1 / 3 rule directly to estimate
h=2
y = [0 , 10 ,18 , 25 , 29 , 32 ,20 , 11 ,5 ,2 , 0 ]
result = ( h / 3 ) * (( y [ 0 ] + y [ 10 ] ) + 4 * ( y [ 1 ] + y [ 3 ] + y [ 5 ] + y [ 7 ] + y [ 9 ] ) + 2 * ( y [ 2 ] + y [ 4 ] + y [
6]+y[8]))
309.33333 km.
40
38
LAB 9: Solution of ODE of first order and first degree
by Taylor’s series and Modified Euler’s method
9.1 Objectives:
Use python
41
39
D[0] = [ 2 * y [ 0 ] + 3 * exp ( x ) ]
D[1] = [ 4 * y [ 0 ] + 9 * exp ( x ) ]
D[2] = [ 8 * y [ 0 ] + 21 * exp ( x ) ]
D[3] = [ 16 * y [ 0 ] + 45 * exp ( x ) ]
return D
42
40
return D
# Define parameters
f = lambda x , y : np . exp ( - x ) # ODE
h = 0 . 2 # Step size
y0 = - 1 # Initial Condition
n=3
# Explicit Euler Method
y [ 0 ] = y0
x[0]=0
for i in range (0 , n ) :
x[i+1]=x[i]+h
y[i + 1] = y[i] + h*f(x[i], y[i])
43
41
The required values are at x= 0.00, y=-1.00000, x=0.20, y=-0.80000,
x = 0.40, y=-0.63625,x = 0.60, y=-0.50219
Solve: y ′ = −2y + x3 e−2x with y(0) = 1 using Euler’s method at x = 0.1, 0.2.
import numpy as np
import matplotlib . pyplot as plt
# Define parameters
f = lambda x , y : - 2 * y + ( x ** 3 ) * np . exp ( - 2 * x ) # ODE
h = 0 . 1 # Step size
y0 = 1 # Initial Condition
n=2
# Explicit Euler Method
y [ 0 ] = y0
x[0]=0
for i in range (0 , n ) :
x[i+1]=x[i]+h
y[i + 1] = y[i] + h*f(x[i], y[i])
plt . plot (x , y , 'bo - - ' , label = " Approximate ( Euler 's method ) " )
44
42
The required values are at x= 0.00, y=1.00000, x=0.10, y=0.80000,
x=0.20, y=0.64008
def modified_euler (f , x0 , y0 , h , n ) :
x = np . zeros ( n + 1 )
y = np . zeros ( n + 1 )
x [ 0 ] = x0
y [ 0 ] = y0
for i in range ( n ) :
x[i+1] = x[i] + h
k1 = h * f ( x [ i ] , y [ i ] )
k2 = h * f ( x [ i + 1 ] , y [ i ] + k1 )
y [ i + 1 ] = y [ i ] + 0 . 5 * ( k1 + k2 )
return x , y
45
43
def f (x , y ) :
return - 0 . 01 * y # ODE dy / dx = - ky
x0 = 0 . 0
y0 = 100 . 0
h = 25
n = 4
x , y = modified_euler (f , x0 , y0 , h , n )
9.5 Exercise:
1. Find y(0.1) by Taylor Series exapnsion when y ′ = x − y 2 , y(0) = 1.
Ans: y(0.1) = 0.9138
46
44
3. Evaluate by modified Euler’s method: y ′ = ln(x + y), y(0) = 2 at x = 0(0.2)0.8.
Ans: 2.0656, 2.1416, 2.2272, 2.3217
47
45
LAB 10: Solution of ODE of first order and first de-
gree by Runge-Kutta 4th order method and Milne’s
predictor and corrector method
10.1 Objectives:
1. To write a python program to solve first order differential equation using 4th order
Runge Kutta method.
2. To write a python program to solve first order differential equation using Milne’s
predictor and corrector method.
48
46
y1 = 2 . 2156
y2 = 2 . 4649
y3 = 2 . 7514
h=0.1
x1 = x0 + h
x2 = x1 + h
x3 = x2 + h
x4 = x3 + h
def f (x , y ) :
return x ** 2 + ( y / 2 )
y10 = f ( x0 , y0 )
y11 = f ( x1 , y1 )
y12 = f ( x2 , y2 )
y13 = f ( x3 , y3 )
y4p = y0 + ( 4 * h / 3 ) * ( 2 * y11 - y12 + 2 * y13 )
print ( ' predicted value of y4 is % 3 . 3f '% y4p )
y14 = f ( x4 , y4p ) ;
for i in range (1 , 4 ) :
y4 = y2 + ( h / 3 ) * ( y14 + 4 * y13 + y12 ) ;
print ( ' corrected value of y4 after \ t iteration %d is \ t % 3 . 5f \ t '%
(i , y4 ) )
y14 = f ( x4 , y4 ) ;
In the next program, function will take all the inputs from the user and display the
answer.
Apply Milne’s predictor and corrector method to solve dy/dx = x2 + (y/2) at y(1.4).
Given that y(1)=2, y(1.1)=2.2156, y(1.2)=2.4649, y(1.3)=2.7514. Use corrector formula
thrice.
from sympy import *
def Milne (g , x0 ,h , y0 , y1 , y2 , y3 ) :
x , y = symbols ( 'x , y ')
f = lambdify ( [x , y ] ,g )
x1 = x0 + h
x2 = x1 + h
x3 = x2 + h
x4 = x3 + h
y10 = f ( x0 , y0 )
y11 = f ( x1 , y1 )
y12 = f ( x2 , y2 )
y13 = f ( x3 , y3 )
y4p = y0 + ( 4 * h / 3 ) * ( 2 * y11 - y12 + 2 * y13 )
print ( ' predicted value of y4 ' , y4p )
y14 = f ( x4 , y4p )
for i in range (1 , 4 ) :
y4 = y2 + ( h / 3 ) * ( y14 + 4 * y13 + y12 )
print ( ' corrected value of y4 , iteration % d '%i , y4 )
49
47
y14 = f ( x4 , y4 )
Milne ( 'x ** 2 + y / 2 ' ,1 , 0 .1 ,2 , 2 . 2156 , 2 . 4649 , 2 . 7514 )
Apply Milne’s predictor and corrector method to solve dy/dx = x − y 2 , y(0)=2 obtain
y(0.8). Take h=0.2. Use Runge-Kutta method to calculate required initial values.
Y = RungeKutta ( 'x - y ** 2 ' ,0 , 0 .2 ,0 , 0 . 8 )
print ( 'y values from Runge - Kutta method : ' ,Y )
Milne ( 'x - y ** 2 ' ,0 , 0 .2 , Y [ 0 ] ,Y [ 1 ] ,Y [ 2 ] ,Y [ 3 ] )
y values from Runge -Kutta method: [0. 0.02 0.08 0.18 0.3 ]
predicted value of y4 0.3042133333333334
corrected value of y4 , iteration 1 0.3047636165214815
corrected value of y4 , iteration 2 0.3047412758696499
corrected value of y4 , iteration 3 0.3047421836520892
10.4 Exercise:
1. Find y(0.1) by Runge Kutta method when y ′ = x − y 2 , y(0) = 1.
Ans: y(0.1) = 0.91379
50
48