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

Lab Manual Engineering Maths 2 VTU

The document outlines the lab component of the Second Semester Engineering Mathematics course, detailing evaluation methods and lab objectives. Each lab is worth 20 marks, with specific tasks including solving differential equations and applying various mathematical methods using Python. The labs cover topics such as Green’s theorem, interpolation methods, and the Runge-Kutta method among others.

Uploaded by

ms0602487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
302 views

Lab Manual Engineering Maths 2 VTU

The document outlines the lab component of the Second Semester Engineering Mathematics course, detailing evaluation methods and lab objectives. Each lab is worth 20 marks, with specific tasks including solving differential equations and applying various mathematical methods using Python. The labs cover topics such as Green’s theorem, interpolation methods, and the Runge-Kutta method among others.

Uploaded by

ms0602487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

Lab Component

of
Second Semester Engineering Mathematics
Instructions and method of evaluation
1. In each Lab student have to show the record of previous Lab.

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.

5. Student has to score minimum 8 marks out of 20 to pass Lab component.

2
Contents: Electrical & Electronics Engineering Stream
Lab 1. Finding gradient, divergent, curl and their geometrical interpretation and Verification of
Green’s theorem
Lab 2. Solution of algebraic and transcendental equation by Regula-Falsi and Newton- Raphson
method

Lab 3. Interpolation /Extrapolation using Newton’s forward and backward difference formula
Lab 4. Solution of ODE of first order and first degree by Taylor’s series and Modified Euler’s
method

Lab 5. Solution of ODE of first order and first degree by Runge-Kutta 4th order method and
Milne’s predictor and corrector method

Lab 6. Solution of Second Order Ordinary Differential Equation with initial/boundary


conditions

Lab 7. Solution of Differential Equation of Oscillations of Spring with Various Load

Lab 8. Visualization in time and frequency domain of standard functions


Lab 9. Computing Laplace transform and inverse Laplace transform of standard functions

Lab 10. Laplace Transform of convolution of two functions.

3
LAB 1: Finding gradient, divergent, curl and their
geometrical interpretation and Verification of Green’s
theorem
Objectives:
Use python

1. to find the gradient of a given scalar function.

2. to find divergence and curl of a vector function.

3. to evaluate integrals using Green’s theorem.

1.1 Method I:
1. To find gradient of ϕ = x2y + 2xz − 4.
# To find gradient of scalar point function.
from sympy. vector import *
from sympy import symbols
N= Coord Sys3 D ('N') # Setting the coordinate system
x,y, z= sy mbols( ' 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
grad A = gra dient( A) # Gradient function is used
print( f"\ n Gradient of { A} is \ n")
display ( grad A )

2. To find divergence of F⃗ = x 2 yz î + y 2 z x ĵ + z 2 xyk̂

4
# To find curl of a vector point function
from sympy. vector import *
from sympy import symbols
N= CoordSys3D(' N')
x,y, z= symbols( 'x y z')
A=N. x ** 2 * N. y* N. z* N. i+N. y ** 2 * N. z* N. x* N. j+N. z ** 2 * N. x* N. y* N. k
delop =Del()
curlA = delop . cross( A)
display ( curlA )

print( f"\ n Curl of { A} is \ n")


display ( curl( A))

5
1.2 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
𝝏𝑸 𝝏𝑷
∮(𝑷 𝒅𝒙 + 𝑸 𝒅𝒚) =∬𝑹 ( 𝝏𝒙 − 𝝏𝒚 ) 𝒅𝒙 𝒅𝒚.

1. Using Green’s theorem, evaluate ∮𝑐 [(x + 2y)dx + (x − 2y)dy], where c is the

region 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

1. Using Green’s theorem, evaluate ∮𝑐 [(xy + y2)dx + x2dy], where c is the closed
curve 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

6
LAB 2: Solution of algebraic and transcendental equa-
tion by Regula-Falsi and Newton-Raphson method
2.1 Objectives:
Use python

2.1.1 to solve algebraic and transcendental equation by Regula-Falsi method.

2.1.2 to solve algebraic and transcendental equation by Newton-Raphson method.

2.2 Regula-Falsi method to solve a transcendental equation


Obtain a root of the equation x3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method.
Perform 5 iterations.

% % %

Using tolerance value we can write the same program as follows:


Obtain a root of the equation x3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method.
Correct to 3 decimal places.

7
# % x^3 - 2 *x- 5 ; function

2.3 Newton-Raphson method to solve a transcendental equa-


tion
Find a root of the equation 3x = cos x+ 1, near 1, by Newton Raphson method. Perform
5 iterations

#% 1

8
# n= 5 ;

9
LAB 3: Interpolation /Extrapolation using Newton’s
forward and backward difference formula
3.1 Objectives:
Use python
1. to interpolate using Newton’s Forward interpolation method.

2. to interpolate using Newton’s backward interpolation method.

3. to extrapolate using Newton’s backward interpolation method.


1.
Use Newtons forward interpolation to obtain the interpolating polynomial and hence
x: 1 3 5 7 9
calculate y(2) for the following:
y: 6 10 62 210 502

10
2.
Use Newtons backward interpolation to obtain the interpolating polynomial and
x: 1 3 5 7 9
hence calculate y(8) for the following data:
y: 6 10 62 210 502

n number

Making numpy array of n &

11
12
13
LAB 4: Solution of ODE of first order and first degree
by Taylor’s series and Modified Euler’s method
4.1 Objectives:
Use python

1. to solve ODE by Taylor series method.

2. to solve ODE by Modified Euler method.

3. to trace the solution curves.

4.2 Taylor series method to solve ODE


Solve: dy
dx − 2y = 3ex with y(0) = 0 using Taylor series method at x = 0.1(0.1)0.3.

D =
H =

H
h
# Append

14
The required values are :at x= 0.00, y=0.00000, x=0.10, y=0.34850,
x = 0.20, y=0.81079,x = 0.30, y=1.41590

4.3 Modified Euler’s method


The iterative formula is:
(𝑛+1) ℎ (𝑛)
𝑦1 = 𝑦0 + 2 [𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1 )], n=0,1,2,3…….

where,1 y(n) is the nth approximation to y1.


(0)
The first iteration will use Euler’s method: 𝑦11 = y0 + hf(x0, y0).
Solve y′ = −ky with y(0) = 100 using modified Euler’s method at x = 100, by taking
h = 25.

15
h = 25
n = 4

The required value at x= 100.00, y=37.25290

16
LAB 5: Solution of ODE of first order and first de-
gree by Runge-Kutta 4th order method and Milne’s
predictor and corrector method
5.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.

5.2 Runge-Kutta method


Apply the Runge Kutta method to find the solution of dy/dx = 1 + (y/x) at y(2) taking
h = 0.2. Given that y(1) = 2.
from sympy import *
import numpy as np
def Runge Kutta (g, x0 ,h, y0 , xn):

x, y= sy mbols( 'x, y')


f= lambdify ([x, y],g)
xt=x0 +h
Y=[ y0 ]
while xt<=xn:
k1 =h* f( x0 , y0 )
k2 =h* f( x0 +h/2 , y0 +k1 / 2 )
k3 =h* f( x0 +h/2 , y0 +k2 / 2 )
k4 =h* f( x0 +h, y0 +k3 )
y1 =y0 +( 1 / 6 )*( k1 + 2 * k2 + 2 * k3 +k4 )
Y. append ( y1 )
# print(' y( % 3 . 3f'% xt,') is % 3 . 3f'% y1 )
x0 =xt
y0 =y1
xt=xt+h
return np. round (Y, 2 )
Runge Kutta ('1 +( y/ x)',1 , 0 .2 ,2 , 2 )

array([2. , 2.62, 3.27, 3.95, 4.66, 5.39])

5.3 Milne’s predictor and corrector method


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.

y0 = 2

17
x1 =x0 +h
x2 =x1 +h
x3 =x2 +h
x4 =x3 +h

predicted value of y4 is 3.079


corrected value of y4 after iteration 1 is 3.07940
corrected value of y4 after iteration 2 is 3.07940
corrected value of y4 after iteration 3 is 3.07940

18
LAB 6: Solution of second order ordinary differential
equation with initial/boundary conditions and plotting the
solution curve
6.1 Objectives:
Use python

1. to solve second order differential equations.

2. to plot the solution curve of differential equations.

A second order differential equation is defined as


d2y dy
+ P (x) + Q(x)y = f (x), where P (x), Q(x) and f (x) are functions of x.
dx2 dx
When f (x) = 0, the equation is called homogenous second order differential equa-
tion. Otherwise, the second order differential equation is non-homogenous.

Example 1:

19
Example 2:
Plot the solution curves of y′′ + 2y′ + 2y = cos(2x), y(0) = 0, y′(0) = 0
We can turn this into two first-order equations by defining a new depedent variable.
For example,
z = y′ ⇒ z′ + 2z + 2y = cos(2x), z(0) = y(0) = 0.
y′ = z; y(0) = 0
z′ = cos(2x) − 2z − 2y; z(0) = 0.

import numpy as np
from scipy. integrate import odeint
import matplotlib . pyplot as plt

def d U_dx(U, x):


# Here U is a vector such that y=U[ 0 ] and z=U[ 1 ]. This function
should return [ y', z']
return [ U[ 1 ], - 2 * U[ 1 ] - 2 * U[ 0 ] + np. cos( 2 * x)]

U0 = [0 , 0 ]
xs = np. linspace (0 , 10 , 200 )
Us = odeint( dU_dx , U0 , xs)

ys = Us[:,0 ] # all the rows of the first column


ys1 =U s[:,1 ] # all the rows of the second column

plt. xlabel(" x")


plt. ylabel (" y")
plt. title (" Solution curves")
plt. plot( xs , ys , label='y');
plt. plot( xs , ys1 , label='z');
plt. legend ()
plt. show ()

20
LAB 7: Solution of differential equation of oscillations
of a spring with various load
7.1 Objectives:
Use python

1. to solve the differential equation of oscillation of a spring.

2. to plot the solution curves.


2
The motion of the spring mass system is given by the differential equation mdtd2 x +
adx
dt
+ kx = f (t) where, m is the mass of a spring coil,x is the displacement of the mass
from its equillibrium position, a is damping constant, k is spring constant.

Case 1: Free and undamped motion - a = 0, f (t) = 0


2
d x
Differential Equation : m
dt2
+ kx = 0
Case 2: Free and damped motion: f (t) = 0
2
Differential Equation : md2 x + adx + kx = 0
dt dt
2
Case 3: Forced and damped motion: Differential Equation : md x + adx + kx = f (t)
dt2 dt

Example 1:
2
Solve d x + 64x = 0, x(0)=1/4, x′(0) = 1 and plot the solution curve.
dt2
import numpy as np
from scipy. integrate import odeint
import matplotlib . pyplot as plt

def f(u, x):


return ( u[ 1 ],- 64 * u[ 0 ])

y0 =[ 1 /4 , 1 ]
xs=np. linspace (0 ,5 , 50 )

us= o dei nt(f , y0 , xs)


ys=us[:,0 ]
print( ys)
plt. plot( xs , ys , ' r - ')

plt. xlabel(' Time ')


plt. ylabel(' A mplitude ')

plt. title (' Solution of free and undamed case ')


plt. grid ( True )
plt. show ()

21
Example 2:
2
Solve 9 d x + 2 dx + 1.2x = 0, x(0) = 1.5, x′(0) = 2.5 and plot the solution curve.
dt2 dt
import numpy as np
from scipy. integrate import odeint
import matplotlib . pyplot as plt

def f(u, x):


return ( u[ 1 ] , - ( 1 / 9 )*( 1 . 2 * u[ 1 ] + 2 * u[ 0 ]))

y0 =[ 2 .5 , 1 . 5 ]
xs=np. linspace (0 , 20 * np. pi , 2000 )

us= o dei nt(f , y0 , xs)


print( us)
ys=us[:,0 ]

plt. plot( xs , ys , ' r - ')

plt. xlabel(' Time ')


plt. ylabel(' A mplitude ')

plt. title (' Solution of free and damped case ')


plt. grid ( True )
plt. show ()

22
LAB 8: Visualization in time and frequency domain of
standard functions
8.1 Objectives:
Use python
1. to use the standard in-built function of Laplace transform.

2. to graphically plot time and frequency domain of standard functions.


Represent the Laplace transform of f(t) = sin 2t, both in time and frequency domains
from sympy import *
import matplotlib . pyplot as plt
s, t= sy mbols( 's t', positive = True )
f=sin(2*t)
F= laplace_ transfor m (f,t, s)
print(' The Laplace Transform of f is',F[ 0 ])
print( F[ 0 ]. expand ())
p1 = plot(f, show =False , xlim =(- 10 , 10 ), line_color =' blue ', legend = True )
p2 = plot( F[ 0 ], show =False , xlim =(- 10 , 10 ), li ne_ col o r=' red ', legend = True )

plotgrid = plotting . PlotGrid (2 , 1 , p1 , p2 , show =False , size =( 5 ., 3 . 5 ))


plotgrid . show ()

Represent the Laplace transform of f(t) = e2t, both in time and frequency domains

23
24
LAB 9: Computing Laplace transform and inverse
Laplace transform of standard functions
9.1 Objectives:
Use python

1. to compute Laplace transform using in-built function.

2. to compute inverse Laplace transform using in-built function.

9.2 Laplace Transform


from sympy import *
t, s = symbols( 't, s')
a = s y mbo l s ( 'a' , real=True , positive = True )
init_printing ()
f=sin ( a* t)
integrate ( f* exp (- s* t), (t, 0 , oo))

n ocon d s= Tru e ))

n ocon d s= Tru e ))

25
9.3 Inverse Laplace Transform

26
27
LAB 10: Laplace transform of convolution of two
functions
10.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


𝑡
(𝑓 ⊛ g)(t)=∫0 𝑓(𝜏)𝑔(𝑡 − 𝜏)𝑑𝜏

The Convolution Theorem states that L(f ⊛ g) = L(f)L(g).


1. Find the Laplace transform of the convolution of the functions f(t) = t and g(t) = et.

28
2. Verify the Convolution Theorem for Laplace transform of the functions f(t) = t and
g(t) = et.

F
=

29
Contents: Computer Science and Engineering Stream
Lab 1. Finding gradient, divergent, curl and their geometrical interpretation
Lab 2. Solution of algebraic and transcendental equation by Regula-Falsi and Newton- Raphson
method

Lab 3. Interpolation /Extrapolation using Newton’s forward and backward difference formula
Lab 4. Solution of ODE of first order and first degree by Taylor’s series and Modified Euler’s
method

Lab 5. Solution of ODE of first order and first degree by Runge-Kutta 4th order method

Lab 6. Solution of ODE of first order and first degree by Milne’s predictor-corrector method

Lab 7. Solution of Second Order Ordinary Differential Equation with initial/boundary


conditions

Lab 8. Solution of Differential Equation of Oscillations of Spring with Various Load

Lab 9. Programme to compute area, volume and center of gravity.

Lab 10. Evaluation of improper integrals , Beta and Gamma functions.

30
LAB 1: Finding gradient, divergent, curl and their
geometrical interpretation
1.1 Objectives:
Use python

1. to find the gradient of a given scalar function.

2. to find divergence and curl of a vector function.

1.2 Method I:
2. To find gradient of ϕ = x2y + 2xz − 4.
# To find gradient of scalar point function.
from sympy. vector import *
from sympy import symbols
N= Coord Sys3 D ('N') # Setting the coordinate system
x,y, z= sy mbols( ' 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
grad A = gra dient( A) # Gradient function is used
print( f"\ n Gradient of { A} is \ n")
display ( grad A )

3. To find divergence of F⃗ = x 2 yz î + y 2 z x ĵ + z 2 xyk̂

31
# To find curl of a vector point function
from sympy. vector import *
from sympy import symbols
N= CoordSys3D(' N')
x,y, z= symbols( 'x y z')
A=N. x ** 2 * N. y* N. z* N. i+N. y ** 2 * N. z* N. x* N. j+N. z ** 2 * N. x* N. y* N. k
delop =Del()
curlA = delop . cross( A)
display ( curlA )

print( f"\ n Curl of { A} is \ n")


display ( curl( A))

32
LAB 2: Solution of algebraic and transcendental equa-
tion by Regula-Falsi and Newton-Raphson method
2.1 Objectives:
Use python

2.1.1 to solve algebraic and transcendental equation by Regula-Falsi method.

2.1.2 to solve algebraic and transcendental equation by Newton-Raphson method.

2.2 Regula-Falsi method to solve a transcendental equation


Obtain a root of the equation x3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method.
Perform 5 iterations.

% % %

Using tolerance value we can write the same program as follows:


Obtain a root of the equation x3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method.
Correct to 3 decimal places.

33
# % x^3 - 2 *x- 5 ; function

2.3 Newton-Raphson method to solve a transcendental equa-


tion
Find a root of the equation 3x = cos x+ 1, near 1, by Newton Raphson method. Perform
5 iterations

#% 1

34
# n= 5 ;

35
LAB 3: Interpolation /Extrapolation using Newton’s
forward and backward difference formula
3.1 Objectives:
Use python
1. to interpolate using Newton’s Forward interpolation method.

2. to interpolate using Newton’s backward interpolation method.

3. to extrapolate using Newton’s backward interpolation method.


1.
Use Newtons forward interpolation to obtain the interpolating polynomial and hence
x: 1 3 5 7 9
calculate y(2) for the following:
y: 6 10 62 210 502

36
3.
Use Newtons backward interpolation to obtain the interpolating polynomial and
x: 1 3 5 7 9
hence calculate y(8) for the following data:
y: 6 10 62 210 502

n number

Making numpy array of n &

37
38
39
LAB 4: Solution of ODE of first order and first degree
by Taylor’s series and Modified Euler’s method
4.1 Objectives:
Use python

1. to solve ODE by Taylor series method.

2. to solve ODE by Modified Euler method.

3. to trace the solution curves.

4.2 Taylor series method to solve ODE


Solve: dy
dx − 2y = 3ex with y(0) = 0 using Taylor series method at x = 0.1(0.1)0.3.

D =
H =

H
h
# Append

40
The required values are :at x= 0.00, y=0.00000, x=0.10, y=0.34850,
x = 0.20, y=0.81079,x = 0.30, y=1.41590

4.3 Modified Euler’s method


The iterative formula is:
(𝑛+1) ℎ (𝑛)
𝑦1 = 𝑦0 + 2 [𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1 )], n=0,1,2,3…….

where,1 y(n) is the nth approximation to y1.


(0)
The first iteration will use Euler’s method: 𝑦11 = y0 + hf(x0, y0).
Solve y′ = −ky with y(0) = 100 using modified Euler’s method at x = 100, by taking
h = 25.

41
h = 25
n = 4

The required value at x= 100.00, y=37.25290

42
LAB 5: Solution of ODE of first order and first de-
gree by Runge-Kutta 4th order method
5.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.

5.2 Runge-Kutta method


1. Apply the Runge Kutta method to find the solution of dy/dx = 1 + (y/x) at y(2) taking
h = 0.2. Given that y(1) = 2.
from sympy import *
import numpy as np
def Runge Kutta (g, x0 ,h, y0 , xn):

x, y= sy mbols( 'x, y')


f= lambdify ([x, y],g)
xt=x0 +h
Y=[ y0 ]
while xt<=xn:
k1 =h* f( x0 , y0 )
k2 =h* f( x0 +h/2 , y0 +k1 / 2 )
k3 =h* f( x0 +h/2 , y0 +k2 / 2 )
k4 =h* f( x0 +h, y0 +k3 )
y1 =y0 +( 1 / 6 )*( k1 + 2 * k2 + 2 * k3 +k4 )
Y. append ( y1 )
# print(' y( % 3 . 3f'% xt,') is % 3 . 3f'% y1 )
x0 =xt
y0 =y1
xt=xt+h
return np. round (Y, 2 )
Runge Kutta ('1 +( y/ x)',1 , 0 .2 ,2 , 2 )

array([2. , 2.62, 3.27, 3.95, 4.66, 5.39])

43
2. Find y(0.1) by Runge Kutta method when y′ = x − y2, y(0) = 1.

from sympy import *


import numpy as np
def Runge Kutta (g, x0 ,h, y0 , xn):

x, y= sy mbols( 'x, y')


f= lambdify ([x, y],g)
xt=x0 +h
Y=[ y0 ]
while xt<=xn:
k1 =h* f( x0 , y0 ) k2
=h* f( x0 +h/2 , y0 +k1 / 2 ) k3 =h*
f( x0 +h/2 , y0 +k2 / 2 ) k4 =h* f(
x0 +h, y0 +k3 )
y1 =y0 +( 1 / 6 )*( k1 + 2 * k2 + 2 * k3 +k4 ) Y.
append ( y1 )
# print(' y( % 3 . 3f'% xt,') is % 3 . 3f'% y1 ) x0
=xt
y0 =y1
xt=xt+h
return np. round (Y, 2 )
Runge Kutta ('x-y**2',1 , 0 .1 ,1 , 0.1 )

y(0.1) = 0.91379

44
LAB 6: Solution of ODE of first order and first de-
gree by Milne’s predictor and corrector method

6.1 Milne’s predictor and corrector method


1. 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.

# Milne 's method to solve first orderDE


# Use corrector formula thrice
x0 = 1
y0 = 2
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 );

predicted value of y4 is 3.079


corrected value of y4 after iteration 1 is 3.07940
corrected value of y4 after iteration 2 is 3.07940
corrected value of y4 after iteration 3 is 3.07940

45
2. Solve by Milnes method: y′ = x – y^2, y(0)=0, y(0.2)=0.02, y(0.4)=0.0795,
y(0.6)=0.1762.Compute y at x=0.8 Use corrector formula thrice.

# Milne 's method to solve first orderDE


# Use corrector formula thrice
x0 = 0
y0 = 0
y1 = 0.02
y2 = 0.0795
y3 = 0.1762
h= 0 . 2
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 );

predicted value of y4 is 0.3049


corrected value of y4 after iteration 1 is 0.3046
corrected value of y4 after iteration 2 is 0.3046
corrected value of y4 after iteration 3 is 0.3046

46
LAB 7: Solution of second order ordinary differential
equation with initial/boundary conditions and plotting the
solution curve
7.1 Objectives:
Use python

1. to solve second order differential equations.

2. to plot the solution curve of differential equations.

A second order differential equation is defined as


d2y dy
+ P (x) + Q(x)y = f (x), where P (x), Q(x) and f (x) are functions of x.
dx2 dx
When f (x) = 0, the equation is called homogenous second order differential equa-
tion. Otherwise, the second order differential equation is non-homogenous.

Example 1:

47
Example 2:
Plot the solution curves of y′′ + 2y′ + 2y = cos(2x), y(0) = 0, y′(0) = 0
We can turn this into two first-order equations by defining a new depedent variable.
For example,
z = y′ ⇒ z′ + 2z + 2y = cos(2x), z(0) = y(0) = 0.
y′ = z; y(0) = 0
z′ = cos(2x) − 2z − 2y; z(0) = 0.

import numpy as np
from scipy. integrate import odeint
import matplotlib . pyplot as plt

def d U_dx(U, x):


# Here U is a vector such that y=U[ 0 ] and z=U[ 1 ]. This function
should return [ y', z']
return [ U[ 1 ], - 2 * U[ 1 ] - 2 * U[ 0 ] + np. cos( 2 * x)]

U0 = [0 , 0 ]
xs = np. linspace (0 , 10 , 200 )
Us = odeint( dU_dx , U0 , xs)

ys = Us[:,0 ] # all the rows of the first column


ys1 =U s[:,1 ] # all the rows of the second column

plt. xlabel(" x")


plt. ylabel (" y")
plt. title (" Solution curves")
plt. plot( xs , ys , label='y');
plt. plot( xs , ys1 , label='z');
plt. legend ()
plt. show ()

48
LAB 8: Solution of differential equation of oscillations
of a spring with various load
8.1 Objectives:
Use python

1. to solve the differential equation of oscillation of a spring.

2. to plot the solution curves.


2
The motion of the spring mass system is given by the differential equation mdtd2 x +
adx
dt
+ kx = f (t) where, m is the mass of a spring coil,x is the displacement of the mass
from its equillibrium position, a is damping constant, k is spring constant.

Case 1: Free and undamped motion - a = 0, f (t) = 0


2
d x
Differential Equation : m
dt2
+ kx = 0
Case 2: Free and damped motion: f (t) = 0
2
Differential Equation : md2 x + adx + kx = 0
dt dt
2
Case 3: Forced and damped motion: Differential Equation : md x + adx + kx = f (t)
dt2 dt

Example 1:
2
Solve d x + 64x = 0, x(0)=1/4, x′(0) = 1 and plot the solution curve.
dt2
import numpy as np
from scipy. integrate import odeint
import matplotlib . pyplot as plt

def f(u, x):


return ( u[ 1 ],- 64 * u[ 0 ])

y0 =[ 1 /4 , 1 ]
xs=np. linspace (0 ,5 , 50 )

us= o dei nt(f , y0 , xs)


ys=us[:,0 ]
print( ys)
plt. plot( xs , ys , ' r - ')

plt. xlabel(' Time ')


plt. ylabel(' A mplitude ')

plt. title (' Solution of free and undamed case ')


plt. grid ( True )
plt. show ()

49
Example 2:
2
Solve 9 d x + 2 dx + 1.2x = 0, x(0) = 1.5, x′(0) = 2.5 and plot the solution curve.
dt2 dt
import numpy as np
from scipy. integrate import odeint
import matplotlib . pyplot as plt

def f(u, x):


return ( u[ 1 ] , - ( 1 / 9 )*( 1 . 2 * u[ 1 ] + 2 * u[ 0 ]))

y0 =[ 2 .5 , 1 . 5 ]
xs=np. linspace (0 , 20 * np. pi , 2000 )

us= o dei nt(f , y0 , xs)


print( us)
ys=us[:,0 ]

plt. plot( xs , ys , ' r - ')

plt. xlabel(' Time ')


plt. ylabel(' A mplitude ')

plt. title (' Solution of free and damped case ')


plt. grid ( True )
plt. show ()

50
LAB 9: Programme to compute area, volume and cen-
ter of gravity
9.1 Objectives:
Use python

1. to evaluate double integration.

2. to compute area and volume.

3. to calculate center of gravity of 2D object.

Syntax for the commands used:


1. Data pretty printer in Python:
pprint ()

2. integrate:
integrate ( function ,( variable , min_limit , max_limit))

9.2 Double and triple integration


Example 1:

1/3

Example 2:

81/80

51
Example 3:

9.3 Area and Volume


∫∫
Area of the region R in the cartesian form is dxdy
R

Example 4:
2 2

dydx

b= 6

24.0*pi
∫∫
9.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 sympy import *
r= Symbol( 'r')
t= Symbol('t')
a= Symbol('a')
# a= 4

w3 = 2 * integrate (r,( r,0 , a*( 1 +cos( t))) ,(t,0 , pi))


pprint( w3 )

52
∫∫ ∫
9.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 sympy import *
x= Symbol( 'x')
y= Symbol('y')
z= Symbol('z')
a= Symbol('a')
b= Symbol('b')
c= Symbol('c')
w2 = integrate (1 ,( z,0 , c*( 1 - x/ a- y/ b)) ,(y,0 , b*( 1 - x/ a)) ,(x,0 , a))
print( w2 )

a*b*c/6

9.6 Center of Gravity


Find the center of gravity of cardioid . Plot the graph of cardioid and mark the center
of gravity.
import numpy as np
import matplotlib . pyplot as plt
import math
from sympy import *
r= Symbol( 'r')
t= Symbol('t')
a= Symbol( 'a')
I1 = integrate ( cos( t)* r ** 2 ,( r,0 , a*( 1 +cos( t))) ,(t,- pi , pi))
I2 = integrate (r,( r,0 , a*( 1 +cos( t))) ,(t,- pi , pi))
I=I1 / I2
print( I)
I=I. subs(a, 5 )
plt. axes( projection = ' polar ')
a= 5

rad = np. arange (0 , ( 2 * np. pi), 0 . 01 )

# plotting the cardioid


for i in rad :
r = a + ( a* np. cos( i))
plt. p olar(i,r, ' g. ')

plt. polar(0 ,I,'r.')


plt. show ()

5*a/6

53
54
LAB 10 : Evaluation of improper integrals, Beta and
Gamma functions
10.1 Objectives:
Use python
1. to evaluate improper integrals using Beta function.
2. to evaluate improper integrals using Gamma function.

Syntax for the commands used:


1. gamma
math . gamma ( x)

Parameters :
x : The number whose gamma value needs to be computed.
2. beta
math . beta (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:

1
∫∞
Gamma function is x(n) = e−xxn−1dx
0

Example 2:
Evaluate Γ(5) by using definition
from sympy import *
x= s y mbols ( ' x')
w1 = integrate ( exp (- x)* x ** 4 ,( x,0 , float (' inf')))
print( simplify ( w1 ))

24

55
Example 4:
Find Beta(3,5), Gamma(5)
# beta and gamma functions
from sympy import beta , gamma
m= input('m :');
n= i nput(' n :');
m= float( m);
n= float( n);
s= beta(m, n);
t= gamma ( n)
print(' gamma (',n, ') is % 3 . 3f'% t)
print(' 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 6:
Verify that Beta(m, n) = Gamma(m)Gamma(n)/Gamma(m + n) for m=5 and n=7
from sympy import beta , gamma
m= 5 ;
n= 7 ;
m= float( m);
n= float( n);
s= beta(m, n);
t=( gamma ( m)* gamma ( n))/ gamma ( m+n);
print(s, t)
if ( abs( s- t) <= 0 . 00001 ):
print(' beta and gamma are related ')
else :
pri nt(' given values are wrong ')

0.000432900432900433 0.000432900432900433
beta and gamma are related

56
Contents: Mechanical & Civil Engineering Stream

Lab 1. Finding gradient, divergent, curl and their geometrical interpretation

Lab 2. Verification of Green’s theorem.


Lab 3. Solution of algebraic and transcendental equation by Regula-Falsi and Newton-
Raphson method

Lab 4. Interpolation /Extrapolation using Newton’s forward and backward difference


formula
Lab 5. Solution of ODE of first order and first degree by Taylor’s series and Modified
Euler’s method

Lab 6. Solution of ODE of first order and first degree by Runge-Kutta 4th order method
and Milne’s predictor and corrector method

Lab 7. Solution of one - dimensional heat equation and wave equation.

Lab 8. Visualization in time and frequency domain of standard functions


Lab 9. Computing Laplace transform and inverse Laplace transform of standard functions

Lab 10. Laplace Transform of convolution of two functions.

57
LAB 1: Finding gradient, divergent, curl and their
geometrical interpretation
1.1 Objectives:
Use python

1. to find the gradient of a given scalar function.

2. to find divergence and curl of a vector function.

1.2 Method I:
2. To find gradient of ϕ = x2y + 2xz − 4.
# To find gradient of scalar point function.
from sympy. vector import *
from sympy import symbols
N= Coord Sys3 D ('N') # Setting the coordinate system
x,y, z= sy mbols( ' 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
grad A = gra dient( A) # Gradient function is used
print( f"\ n Gradient of { A} is \ n")
display ( grad A )

3. To find divergence of F⃗ = x 2 yz î + y 2 z x ĵ + z 2 xyk̂

58
# To find curl of a vector point function
from sympy. vector import *
from sympy import symbols
N= CoordSys3D(' N')
x,y, z= symbols( 'x y z')
A=N. x ** 2 * N. y* N. z* N. i+N. y ** 2 * N. z* N. x* N. j+N. z ** 2 * N. x* N. y* N. k
delop =Del()
curlA = delop . cross( A)
display ( curlA )

print( f"\ n Curl of { A} is \ n")


display ( curl( A))

59
Lab 2: Verification of Green’s theorem

2.1 Objectives:
1. To evaluate integrals using Green’s theorem.

2.2 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
𝝏𝑸 𝝏𝑷
∮(𝑷 𝒅𝒙 + 𝑸 𝒅𝒚) =∬𝑹 ( 𝝏𝒙 − 𝝏𝒚 ) 𝒅𝒙 𝒅𝒚.

1. Using Green’s theorem, evaluate ∮𝑐 [(x + 2y)dx + (x − 2y)dy], where c is the

region 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

3 Using Green’s theorem, evaluate ∮𝑐 [(xy + y2)dx + x2dy], where c is the closed
curve 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

60
LAB 3: Solution of algebraic and transcendental equa-
tion by Regula-Falsi and Newton-Raphson method
3.1 Objectives:
Use python

3.1.1 to solve algebraic and transcendental equation by Regula-Falsi method.

3.1.2 to solve algebraic and transcendental equation by Newton-Raphson method.

3.2 Regula-Falsi method to solve a transcendental equation


Obtain a root of the equation x3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method.
Perform 5 iterations.

% % %

Using tolerance value we can write the same program as follows:


Obtain a root of the equation x3 − 2x − 5 = 0 between 2 and 3 by regula-falsi method.
Correct to 3 decimal places.

61
# % x^3 - 2 *x- 5 ; function

3.3 Newton-Raphson method to solve a transcendental equa-


tion
Find a root of the equation 3x = cos x+ 1, near 1, by Newton Raphson method. Perform
5 iterations

#% 1

62
# n= 5 ;

63
LAB 4: Interpolation /Extrapolation using Newton’s
forward and backward difference formula
4.1 Objectives:
Use python
1. to interpolate using Newton’s Forward interpolation method.

2. to interpolate using Newton’s backward interpolation method.

3. to extrapolate using Newton’s backward interpolation method.


1.
Use Newtons forward interpolation to obtain the interpolating polynomial and hence
x: 1 3 5 7 9
calculate y(2) for the following:
y: 6 10 62 210 502

64
2.
Use Newtons backward interpolation to obtain the interpolating polynomial and hence
x: 1 3 5 7 9
calculate y(8) for the following data:
y: 6 10 62 210 502

n number

Making numpy array of n &

65
66
67
LAB 5: Solution of ODE of first order and first degree
by Taylor’s series and Modified Euler’s method
5.1 Objectives:
Use python

1. to solve ODE by Taylor series method.

2. to solve ODE by Modified Euler method.

3. to trace the solution curves.

5.2 Taylor series method to solve ODE


Solve: dy
dx − 2y = 3ex with y(0) = 0 using Taylor series method at x = 0.1(0.1)0.3.

D =
H =

H
h
# Append

68
The required values are :at x= 0.00, y=0.00000, x=0.10, y=0.34850,
x = 0.20, y=0.81079,x = 0.30, y=1.41590

5.3 Modified Euler’s method


The iterative formula is:
(𝑛+1) ℎ (𝑛)
𝑦1 = 𝑦0 + 2 [𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1 )], n=0,1,2,3…….

where,1 y(n) is the nth approximation to y1.


(0)
The first iteration will use Euler’s method: 𝑦11 = y0 + hf(x0, y0).
Solve y′ = −ky with y(0) = 100 using modified Euler’s method at x = 100, by taking
h = 25.

69
h = 25
n = 4

The required value at x= 100.00, y=37.25290

70
LAB 6: Solution of ODE of first order and first de-
gree by Runge-Kutta 4th order method and Milne’s
predictor and corrector method
6.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.

6.2 Runge-Kutta method


Apply the Runge Kutta method to find the solution of dy/dx = 1 + (y/x) at y(2) taking
h = 0.2. Given that y(1) = 2.
from sympy import *
import numpy as np
def Runge Kutta (g, x0 ,h, y0 , xn):

x, y= sy mbols( 'x, y')


f= lambdify ([x, y],g)
xt=x0 +h
Y=[ y0 ]
while xt<=xn:
k1 =h* f( x0 , y0 )
k2 =h* f( x0 +h/2 , y0 +k1 / 2 )
k3 =h* f( x0 +h/2 , y0 +k2 / 2 )
k4 =h* f( x0 +h, y0 +k3 )
y1 =y0 +( 1 / 6 )*( k1 + 2 * k2 + 2 * k3 +k4 )
Y. append ( y1 )
# print(' y( % 3 . 3f'% xt,') is % 3 . 3f'% y1 )
x0 =xt
y0 =y1
xt=xt+h
return np. round (Y, 2 )
Runge Kutta ('1 +( y/ x)',1 , 0 .2 ,2 , 2 )

array([2. , 2.62, 3.27, 3.95, 4.66, 5.39])

6.3 Milne’s predictor and corrector method


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.

y0 = 2

71
x1 =x0 +h
x2 =x1 +h
x3 =x2 +h
x4 =x3 +h

predicted value of y4 is 3.079


corrected value of y4 after iteration 1 is 3.07940
corrected value of y4 after iteration 2 is 3.07940
corrected value of y4 after iteration 3 is 3.07940

72
LAB 7: Solution of Lagrange’s linear partial differen-
tial equations
7.1 Objectives:
Use python

1. to solve linear Partial Differential Equations of first order

1. Solve the PDE, xp + yq = z, where z = f(x, y)


from sympy. solvers. pde import pdsolve
from sympy import Function , Eq , cot , classify_pde , pprint
from sympy. abc import x, y, a
f = Function ('f')
z = f(x, y)
zx = z. diff( x)
zy = z. diff( y)
# Solve xp+yq=z
eq = Eq( x* zx+y* zy , z)
pprint( eq)
print("\ n")
soln = pdsolve ( eq , z)
pprint( soln )

2. Solve the PDE 2p + 3q = 1, where p = ∂z and p = ∂z


∂x ∂y
from sympy. solvers. pde import pdsolve
from sympy import Function , Eq , cot , classify_pde , pprint
from sympy. abc import x, y, a
f = Function ('f')
z = f(x, y)
zx = z. diff( x)
zy = z. diff( y)
# Solve 2p+ 3q= 1
eq = Eq( 2 * zx+ 3 * zy , 1 )
pprint( eq)
print("\ n")
soln = pdsolve ( eq , z)
pprint( soln )

73
74
LAB 8: Visualization in time and frequency domain of
standard functions
8.2 Objectives:
Use python
1. to use the standard in-built function of Laplace transform.

2. to graphically plot time and frequency domain of standard functions.


Represent the Laplace transform of f(t) = sin 2t, both in time and frequency domains
from sympy import *
import matplotlib . pyplot as plt
s, t= sy mbols( 's t', positive = True )
f=sin(2*t)
F= laplace_ transfor m (f,t, s)
print(' The Laplace Transform of f is',F[ 0 ])
print( F[ 0 ]. expand ())
p1 = plot(f, show =False , xlim =(- 10 , 10 ), line_color =' blue ', legend = True )
p2 = plot( F[ 0 ], show =False , xlim =(- 10 , 10 ), li ne_ col o r=' red ', legend = True )

plotgrid = plotting . PlotGrid (2 , 1 , p1 , p2 , show =False , size =( 5 ., 3 . 5 ))


plotgrid . show ()

Represent the Laplace transform of f(t) = e2t, both in time and frequency domains

75
76
LAB 9: Computing Laplace transform and inverse
Laplace transform of standard functions
9.4 Objectives:
Use python

3. to compute Laplace transform using in-built function.

4. to compute inverse Laplace transform using in-built function.

9.5 Laplace Transform


from sympy import *
t, s = symbols( 't, s')
a = s y mbo l s ( 'a' , real=True , positive = True )
init_printing ()
f=sin ( a* t)
integrate ( f* exp (- s* t), (t, 0 , oo))

n ocon d s= Tru e ))

n ocon d s= Tru e ))

77
9.6 Inverse Laplace Transform

78
79
LAB 10: Laplace transform of convolution of two
functions
10.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


𝑡
(𝑓 ⊛ g)(t)=∫0 𝑓(𝜏)𝑔(𝑡 − 𝜏)𝑑𝜏

The Convolution Theorem states that L(f ⊛ g) = L(f)L(g).


1. Find the Laplace transform of the convolution of the functions f(t) = t and g(t) = et.

80
2. Verify the Convolution Theorem for Laplace transform of the functions f(t) = t and
g(t) = et.

F
=

81
82

You might also like