Lab Manual-MatLab CSE
Lab Manual-MatLab CSE
Lab Component
of
Second Semester Engineering Mathematics
as prescribed by Visvesvaraya Technological University, Belagavi
Compiled by:
Dr. Ramananda H. S. Dr. K. Sushan Bairy
St Joseph Engineering College, SOAS, REVA University,
Mangaluru, INDIA. Bengaluru, INDIA.
1
lOMoARcPSD|370 367 39
College, Bengaluru for their continuous effort to prepare this lab manual.
I am very much thankful to Dr. Vidyashankar S. Vice Chancellor VTU, Dr. Ran-
gaswamy B E, Registrar VTU, Dr. T Srinivasa, Registrar Evaluation VTU, Dr. Sadashive
Gowda, Dean Academic VTU, Dr. Sadashiv Halbhavi, Special officer VTU, All the BOS
members, Basic Science & Humanities for their guidance, support and encouragement for
bringing out this manual.
Dr Suresha M
Chairman Board of Studies in Basic Sciences & Humanities
2
lOMoARcPSD|370 367 39
2. Each Lab will be evaluated for 15 marks and finally average will be taken for 15
marks.
3. Viva questions shall be asked in labs and attendance also can be considered for
everyday Lab evaluation.
4. Tests shall be considered for 5 marks and final Lab assessment is for 20 marks.
3
lOMoARcPSD|370 367 39
Lab 4. Computation of basis and dimension for a vector space and graphical representation
of linear transformation
Lab 7. Interpolation /Extrapolation using Newton’s forward and backward difference for-
mula
1 rd
Lab 8. Computation of area under the curve using Trapezoidal, Simpson’s and Simp-
3
3 th
sons 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
51
lOMoARcPSD|370 367 39
2. integrate:
i nt eg rat e ( functi on ,( vari able , mi n_li mit , max _l i mi t ) )
1/3
Example 2:
∫3 3∫−x 3−∫x−y
Evaluate the integral (xyz)dzdydx
0 0 0
from sy mpy i mport *
x = Sy mbol ( 'x ')
y = Sy mbol ( 'y ')
z= Sy mbol ( 'z ')
w2 = i nt eg rat e (( x* y * z) ,(z , 0 , 3 - x - y) ,( y , 0 , 3 - x) ,( x , 0 , 3 ) )
pri nt ( w2 )
81/80
52
lOMoARcPSD|370 367 39
Example 3:
∫∫ ∫∫
Prove that (x2 + y 2)dydx = (x2 + y2)dxdy
from sy mpy i mport *
x = Sy mbol ( 'x ')
y = Sy mbol ( 'y ')
z= Sy mbol ( 'z ')
w3 = i nt eg rat e ( x ** 2 +y ** 2 , y , x )
ppri nt ( w3 )
w4 = i nt eg rat e ( x ** 2 +y ** 2 , x , y )
ppri nt ( w4 )
Example 4:
√
∫a (b/a) ∫ a 2 −x2
Find the area of an ellipse by double integration. A=4 dydx
0 0
from sy mpy i mport *
x = Sy mbol ( 'x ')
y = Sy mbol ( 'y ')
# a= Sym bol ( ' a ')
# b= Sym bol ( ' b ')
a= 4
b= 6
w3 = 4 * i nt eg rat e ( 1 ,( y , 0 ,( b/ a) * sqrt ( a ** 2 - x ** 2 ) ) ,( x , 0 , a) )
pri nt ( w3 )
24.0*pi
∫ ∫
1.4 Area of the region R in the polar form is rdrdθ
R
Example 5:
Find the area of the cardioid r = a(1 + cosθ) by double integration
from sy mpy i mport *
r= Sy mbol ( 'r ')
t = Sy mbol ( 't ')
a= Sy mbol ( 'a ')
# a= 4
53
lOMoARcPSD|370 367 39
∫ ∫∫
1.5 Volume of a solid is given by dxdydz
V
Example 6:
Find the volume of the tetrahedron bounded by the planes x=0,y=0 and z=0, x + y + z = 1
a b c
from sy mpy i mport *
x = Sy mbol ( 'x ')
y = Sy mbol ( 'y ')
z= Sy mbol ( 'z ')
a= Sy mbol ( 'a ')
b= Sy mbol ( 'b ')
c= Sy mbol ( 'c ')
w2 = i nt eg rat e ( 1 ,( z, 0 , c* ( 1 - x / a- y / b) ) ,(y , 0 , b*( 1 - x / a) ) ,( x , 0 , a) )
pri nt ( w2 )
a*b*c/6
rad = np . arang e ( 0 , ( 2 * np . pi ) , 0 . 01 )
5*a/6
54
lOMoARcPSD|370 367 39
1.7 Exercise:
∫1∫x
1. Evaluate (x + y)dydx
0 0
Ans: 0.5
lo∫
g(2) ∫x x+l∫og(y)
2. Find the (ex+y+z)dzdydx
0 0 0
Ans: -0.2627
3. Find the area of positive quadrant of the circle x2 + y2 = 16
Ans: 4π
4. Find the volume of the tetrahedron bounded by the planes x=0,y=0 and z=0,
x
+y+z =1
2 3 4
Ans: 4
55
lOMoARcPSD|370 367 39
Parameters :
x : The number whose gamma value needs to be computed.
2. beta
mat h . bet a ( x , y )
Parameters :
x ,y: The numbers whose beta value needs to be computed.
3. Note: We can evaluate improper integral involving infinity by using inf.
Example 1:
∫∞
Evaluate e−xdx.
0
from sy mpy i mport *
x = sy mbol s ( 'x ')
w1 = i nt eg rat e ( exp (- x ) ,(x , 0 , fl oat ( ' inf ') ) )
pri nt ( s i mpl if y ( w1 ) )
1
∫∞
Gamma function is x(n) = e−xx n−1dx
0
Example 2:
Evaluate Γ(5) by using definition
from sy mpy i mport *
x = sy mbol s ( 'x ')
w1 = i nt eg rat e ( exp (- x ) * x ** 4 ,( x , 0 , fl oat ( ' inf ') ) )
pri nt ( s i mpl if y ( w1 ) )
24
56
lOMoARcPSD|370 367 39
Example 3:
∫∞
Evaluate e−st cos(4t)dt . That is Laplace transform of cos(4t)
0
from sy mpy i mport *
t , s= sy mbol s ( 't , s ')
# for infinity in sympy we use oo
w1 = i nt eg rat e ( exp (- s * t ) * cos ( 4 * t) ,( t , 0 , oo ) )
di s pl ay ( s i mpli f y ( w1 ) )
Example 4:
Find Beta(3,5), Gamma(5)
# beta and gamma functions
from sy mpy i mport beta , g amma
m= i nput ( 'm : ') ;
n= i nput ( 'n : ') ;
m= f l oat ( m) ;
n= f l oat ( n) ;
s = beta ( m , n);
t= gamma ( n)
pri nt ( ' g amma ( ',n , ') is % 3 . 3f '% t )
pri nt ( ' Beta ( ',m , n , ') is % 3 . 3f '% s )
m :3
n :5
gamma ( 5.0 ) is 24.000
Beta ( 3.0 5.0 ) is 0.010
Example 5:
Calculate Beta(5/2,7/2) and Gamma(5/2).
# beta and gamma functions
# If the number is a fraction give it in decimals . Eg 5 / 2 = 2 . 5
from sy mpy i mport beta , g amma
m= f l oat ( i nput ( 'm : ') ) ;
n= f l oat ( i nput ( 'n : ') ) ;
s = beta ( m , n);
t= gamma ( n)
pri nt ( ' g amma ( ',n , ') is % 3 . 3f '% t )
pri nt ( ' Beta ( ',m , n , ') is % 3 . 3f '% s )
57
lOMoARcPSD|370 367 39
m : 2.5
n :3.5
gamma ( 3.5 ) is 3.323
Beta ( 2.5 3.5 ) is 0.037
Example 6:
Verify that Beta(m, n) = Gamma(m)Gamma(n)/Gamma(m + n) for m=5 and n=7
from sy mpy i mport beta , g amma
m= 5 ;
n= 7 ;
m= f l oat ( m) ;
n= f l oat ( n) ;
s = beta ( m , n);
t =( g amma ( m) * g amma ( n) ) / g amma ( m+n) ;
pri nt (s , t )
if ( abs( s- t) <= 0 . 00001 ):
pri nt ( ' bet a and g amma are rel at ed ')
else :
pri nt ( ' gi ven v al ue s are wrong ')
0.000432900432900433 0.000432900432900433
beta and gamma are related
2.2 Exercise:
∫∞
1. Evaluate e−tcos(2t)dt
0
Ans: 1/5
2. Find the value of Beta(5/2,9/2)
Ans: 0.0214
58
lOMoARcPSD|370 367 39
1.2 Method I:
To find gradient of ϕ = x2y + 2xz − 4.
# To find gradient of scalar point function .
from sy mpy . vect or i mport *
from sy mpy i mport sy mbol s
N= Coor d Sy s 3 D ( 'N ') # Sett in g the c o or d in a te s yste m
x , y , z= s y mbol s ( 'x y z ')
A= N. x ** 2 * N. y+ 2 * N. x * N. z - 4 # Var ia bl e s x , y , z to be used with co or d in at e
system N
del op = Del () # Del op er at or
di s pl ay ( del op ( A) ) # Del op er at or a pp li ed to A
g rad A = g radie nt ( A) # Grad ie n t fu nc ti on is used
pri nt ( f " \ n Gradi ent of { A} is \ n" )
di s pl ay ( grad A )
59
lOMoARcPSD|370 367 39
60
lOMoARcPSD|370 367 39
G =G . subs ( [ ( v[ 0 ] , x ) ,( v [ 1 ] , y) ,( v [ 2 ] , z) ] )
pri nt ( " Di vergence of F=" )
di s pl ay ( G )
G =G . subs ( [ ( v[ 0 ] , x ) ,( v [ 1 ] , y) ,( v [ 2 ] , z) ] )
pri nt ( " curl of F=" )
di s pl ay ( G )
61
lOMoARcPSD|370 367 39
1.4 Exercise:
1. If u = x + y + z, v = x2 + y2 + z2, w = yz + zx + xy, find gradu, gradv and gradw.
Ans: î + ĵ + k̂, 2(xî + y ĵ + zk̂), (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 xy3z2 = 4 at the point (−1, −1, 2).
Ans: −4î − 12ĵ + 4k̂.
⃗ = xî + yĵ + zk̂, show that (i) ∇ · R
5. If R ⃗ = 3, (ii) ∇ × R
⃗ = 0.
62
lOMoARcPSD|370 367 39
63
lOMoARcPSD|370 367 39
Extract the linearly independent rows in given matrix : Basis of Row space
from numpy i mport *
i mport sy mpy 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 rang e ( 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 ]
di s pl ay ( " REF of given matrix : " , sp . Mat ri x ( AB ) )
temp = { ( 0 , 0 , 0 , 0 )}
res ul t = [ ]
for idx , row in enumerat e ( map ( tuple , AB) ) :
if row not in temp :
res ul t . append ( idx )
pri nt ( " \ n Bas is are non - zero rows of A: " )
di s pl ay ( sp . Mat ri x ( AB [ res ul t ] ))
64
lOMoARcPSD|370 367 39
Another example.
from mat h i mport pi , sin , cos
65
lOMoARcPSD|370 367 39
coords = np . array ( [[ 0 , 0 ] , [ 0 . 5 , 0 . 5 ] ,[ 0 . 5 , 1 . 5 ] ,[ 0 , 1 ] ,[ 0 , 0 ] ] )
coords = coords . trans pos e ()
coords
x = coords[0 ,:]
y = coords[1 ,:]
A = np . array ( [[ 2 , 0 ] ,[ 0 , 1 ] ] )
A_coords = A@ coords
x_LT1 = A_ coor ds[ 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 ')
66
lOMoARcPSD|370 367 39
4.4.2 Reflection:
Another example.
B = np . array ( [ [- 1 , 0 ] ,[ 0 , 1 ] ] )
B_coords = B@ coords
# 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 ')
67
lOMoARcPSD|370 367 39
4.4.3 Rotation:
68
lOMoARcPSD|370 367 39
Another example.
t het a = pi / 6
R = np . array ( [[ cos ( t het a ) , - sin ( t het a ) ] ,[ sin ( t het a ) , cos ( t het a ) ] ] )
R_coords = R@ coords
# 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 ')
69
lOMoARcPSD|370 367 39
70
lOMoARcPSD|370 367 39
Another example.
S = np . array ( [[ 1 , 2 ] ,[ 0 , 1 ] ] )
S_coords = S@ coords
# 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 ')
71
lOMoARcPSD|370 367 39
4.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.
i mport numpy as np
i mport mat pl ot l i b . py pl ot as plt
V = np . array ( [[ 2 , 3 ]] )
origi n = np . array ( [[ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ]) # origi n p oin t
A= np . mat rix ( [[ 0 ,- 1 ] ,[ 1 , 0 ] ] )
B= np . mat rix ( [[ 2 , 0 ] ,[ 0 , 1 ] ])
V1 = np . mat rix ( V )
V2 = A* np . t rans pos e ( V1 )
V3 =B * V2
V2 = np . array ( V2 )
V3 = np . array ( V3 )
pri nt ( " I mage of gi ven v ect ors is : " , V3 )
plt . qui v er ( * origin , V [: , 0 ] , V[ : , 1 ] , color =[ 'b '] , s c al e = 20 )
plt . qui v er ( * origin , V2 [ 0 , :] , V2 [ 1 ,: ] , col or =[ 'r '] , scale = 20 )
plt . qui v er ( * origin , V3 [ 0 , :] , V3 [ 1 ,: ] , col or =[ 'g '] , scale = 20 )
plt . t itl e ( ' Blue = original , Red = R ot at ed , Green = R ot at e d + St reached ')
plt . show ()
72
lOMoARcPSD|370 367 39
Another example.
C = np . array ( [ [- cos ( t het a ) , sin ( t het a ) ] ,[ sin ( t het a ) , cos ( t het a ) ] ] )
C_coords = C@ coords
# 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 ')
73
lOMoARcPSD|370 367 39
4.5 Exercise:
1. Verify the rank nullity theorem for the following linear transformation
a) Horizontal stretch
b) Reflection
c) Shear
d) Rotation
74
lOMoARcPSD|370 367 39
# initialize arrays
A = np . array ([ 2 , 1 , 5 , 4 ] )
B = np . array ([ 3 , 4 , 7 , 8 ] )
# dot product
out put = np . dot ( A , B)
77
# initialize arrays
A = np . array ([ 2 , 1 , 5 , 4 ] )
B = np . array ([ 3 , 4 , 7 , 8 ] )
# dot product
out put = np . dot ( A , B)
pri nt ( ' I nner product is : ', out put )
if output == 0 :
pri nt ( ' given vectors are ort hog nal ')
else :
pri nt ( ' gi ven vectors are not ort hog nal ')
Inner product is : 77
given vectors are not orthognal
75
lOMoARcPSD|370 367 39
5.4 Exercise:
1. Find the inner product of (1, 2, 3) and (3, 4, 5).
Ans: 26
76
lOMoARcPSD|370 367 39
for i in rang e ( 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
pri nt ( ' it rati on % d \ t the root % 0 . 3f \ t f unc ti on v al ue % 0 . 3f \ n '%
(i,c, f( c)));
77
lOMoARcPSD|370 367 39
78
lOMoARcPSD|370 367 39
df = l ambdi f y (x , dg )
x0 = fl oat ( i nput ( ' E nter the i nt i al appr o x i mat i on ') ) ; # x0 = 1
n= int ( i nput ( ' E nter the number of i t erat i ons ') ) ; #n=5;
for i in rang e ( 1 , n+ 1 ) :
x1 =( x0 - ( f( x0 )/ df( x0 )))
pri nt ( ' it rati on % d \ t the root % 0 . 3f \ t f unc ti on v al ue % 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
79
lOMoARcPSD|370 367 39
for i in rang e ( 0 , n) :
pri nt ( '% 0 . 2f ' %( x [ i ]) , end = '')
for j in range ( 0 , n- i ):
pri nt ( '\ t \ t % 0 . 2f ' %( y[ i ] [ j ] ) , end = '')
pri nt ()
# obtaining the polynomial
t = sy mbol s ( 't ')
f=[] # f is a list type data
p=( t- x[ 0 ])/( x[ 1 ]- x[ 0 ])
f. append ( p)
for i in rang e ( 1 , n- 1 ) :
f . append ( f [ i - 1 ] *( p- i ) /( i+ 1 ) )
poly =y [ 0 ] [ 0 ]
for i in rang e ( n- 1 ):
poly = poly +y [ 0 ] [ i+ 1 ] * f [ i ]
80
lOMoARcPSD|370 367 39
81
lOMoARcPSD|370 367 39
for i in rang e ( 0 , n) :
pri nt ( '% 0 . 2f ' %( x [ i ]) , end = '')
for j in range ( 0 , i + 1 ) :
pri nt ( '\ t % 0 . 2f ' %( y [ i ][ j ] ) , end = '')
pri nt ()
p=( t- x[ n- 1 ]) /( x[ 1 ]- x[ 0 ])
f. append ( p)
for i in rang e ( 1 , n- 1 ) :
f . append ( f [ i - 1 ] *( p+i ) /( i + 1 ))
poly =y [ n- 1 ][ 0 ]
pri nt ( poly )
for i in rang e ( n- 1 ):
poly = poly +y [ n- 1 ] [ i + 1 ] * f [ i]
s i mp_ pol y = s i mpl if y ( poly )
pri nt ( '\ nTHE I NT ER PO L AT I NG PO L Y NO MI AL IS \ n ') ;
ppri nt ( si mp_ pol y )
# if you want to interpolate at some point the next session will help
i nter = i nput ( ' Do you want to i nt erpol at e at a poi nt ( y / n) ? ')
if i nter == 'y ':
a= f l oat ( i nput ( ' enter the poi nt ') )
i nt erpol = l ambdi f y (t , si mp_pol y )
res ul t = i nterpol ( a)
pri nt ( '\ nT he v al ue of the f unct i on at ' ,a , ' is \ n ', resul t ) ;
82
lOMoARcPSD|370 367 39
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
83
lOMoARcPSD|370 367 39
1. to find area under the curve represented by a given function using Trapezoidal rule.
1 rd
2. to find area under the curve represented by a given function using Simpson’s 3
rule.
3 th
3. to find area under the curve represented by a given function using Simpson’s 8
rule.
4. to find the area below the curve when discrete points on the curve are given.
# Input section
l ow er_l i mi t = fl oat ( i nput ( " E nt er l ower li mi t of i nt eg rat i on : " ) )
up per_l i mi t = fl oat ( i nput (" E nt er upper li mi t of i nt eg rat i on : " ) )
s ub_i nt erv al = int ( i nput ( " E nter number of sub i nt erv als : " ) )
# Print result
pri nt ( " I nt egrat i on res ul t by Trapezoi d a l met hod is : " , res ul t )
84
lOMoARcPSD|370 367 39
1 rd
8.3 Simpson’s 3
Rule
∫5 1
Evaluate 1+x 2
.
0
# Definition of the function to integra te
def my _f unc ( x ) :
return 1 / ( 1 + x ** 2 )
def s i mps on 13 ( x0 , xn , n) :
h = ( xn - x0 ) / n # ca lcu lati ng step size
# Finding sum
i nt eg rat i on = ( my _f unc ( x0 ) + my _f unc ( xn) )
k = x0
for i in range ( 1 , n) :
if i % 2 = = 0 :
i nt eg rat i on = i nt eg rat i on + 4 * my_ f unc ( k )
else :
i nt eg rat i on = i nt eg rat i on + 2 * my_ f unc ( k )
k += h
# Finding final integration value
i nt eg rat i on = i nt eg rat i on * h * ( 1 / 3 )
ret urn i nt eg rat i on
# Input section
l ow er_l i mi t = fl oat ( i nput ( " E nte r l ower li mi t of i nt eg rat i on : "))
up per_l i mi t = f l oat ( i nput ( " E nt er upper li mi t of i nt eg rat i on : "))
s ub_i nt erv al = int ( i nput ( " E nter number of sub i nt erv als : " ) )
85
lOMoARcPSD|370 367 39
h = (b - a) / n
s = f( a) + f( b)
for i in rang e ( 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:
∫1 1
x2 rule.
1. Evaluate the integral dx using Simpson’s
1 + x3 3
0
Ans: 0.23108
∫0.6
3 2
2. Use Simpson’s rule to find e−x dx by taking seven ordinates.
8
0
Ans: 0.5351
∫π
3. Evaluate using trapezoidal rule sin 2xdx. 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
86
lOMoARcPSD|370 367 39
1
Estimate the volume of the solid formed using Simpson’s rd rule. Hint: Required
∫1 3
volume is 0 y 2 ∗ π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 sh al l use si mp so n 's 1 / 3 rule di re c tl y to e st im at e
h= 2
y = [ 0 , 10 , 18 , 25 , 29 , 32 , 20 , 11 , 5 , 2 , 0 ]
res ul t =( 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.
87
lOMoARcPSD|370 367 39
88
lOMoARcPSD|370 367 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
89
lOMoARcPSD|370 367 39
return D
# Define parameters
f = l ambda 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 rang e ( 0 , n) :
x [ i+ 1 ] =x [ i ] +h
y[ i + 1 ] = y[ i] + h* f( x[ i], y[ i])
90
lOMoARcPSD|370 367 39
Solve: y′ = −2y + x3e−2x with y(0) = 1 using Euler’s method at x = 0.1, 0.2.
i mport numpy as np
i mport mat pl ot l i b . py pl ot as plt
# Define parameters
f = l ambda 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 rang e ( 0 , n) :
x [ i+ 1 ] =x [ i ] +h
y[ i + 1 ] = y[ i] + h* f( x[ i], y[ i])
plt . pl ot ( x , y , 'bo - - ', l abel =" Approx i m a t e ( E uler 's met hod ) " )
91
lOMoARcPSD|370 367 39
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
92
lOMoARcPSD|370 367 39
x0 = 0 . 0
y0 = 100 . 0
h = 25
n = 4
x , y = modi f i ed_eul er (f , x0 , y0 , h , n)
9.5 Exercise:
1. Find y(0.1) by Taylor Series exapnsion when y′ = x − y2, y(0) = 1.
Ans: y(0.1) = 0.9138
93
lOMoARcPSD|370 367 39
94
lOMoARcPSD|370 367 39
2. To write a python program to solve first order differential equation using Milne’s
predictor and corrector method.
95
lOMoARcPSD|370 367 39
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 )
pri nt ( ' predicted v al ue of y4 is % 3 . 3f '% y4p )
y14 =f( x4 , y4p );
for i in rang e ( 1 , 4 ) :
y4 =y2 +( h/ 3 )*( y14 + 4 * y13 +y12 );
pri nt ( ' correct ed v al ue of y4 after \ t it erati on %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 sy mpy i mport *
def Mi l ne ( g , x0 , h , y0 , y1 , y2 , y3 ) :
x , y = sy mbol s ( 'x , y ')
f = l ambdi f y ( [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 )
pri nt ( ' predi ct ed v al ue of y4 ', y4p )
y14 =f( x4 , y4p )
for i in range ( 1 , 4 ) :
y4 =y2 +( h/ 3 )*( y14 + 4 * y13 +y12 )
pri nt ( ' correct ed v al ue of y4 , it erati on % d '% i , y4 )
96
lOMoARcPSD|370 367 39
y14 =f( x4 , y4 )
Mi l ne ( '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 − y2 , y(0)=2 obtain
y(0.8). Take h=0.2. Use Runge-Kutta method to calculate required initial values.
Y = R ung e Kut t a ( 'x - y ** 2 ',0 , 0 . 2 , 0 , 0 . 8 )
pri nt ( 'y v al ues from R ung e - Kutt a met hod : ', Y )
Mi l ne ( '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 − y2, y(0) = 1.
Ans: y(0.1) = 0.91379
97