0% found this document useful (0 votes)
35 views21 pages

Completed Lab Manual 3vy23ue059

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)
35 views21 pages

Completed Lab Manual 3vy23ue059

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/ 21

Visvesvaraya Technological University, Belagavi

POST GRADUATION CENTRE, KALABURAGI


BACHELOR OF TECHNOLOGY (B.Tech)

IN

ELECTRONICS AND COMUTER ENGINEERING

A
Laboratory Report On
MATHEMATICS-II LABORATORY

Submitted as a part of Academic Requirement for Second Semester


By

VARUN R MALASHETTI

(USN: 3VY23UE059)

Department of Electronics & Communication Engineering,


Post Graduation Center, Visvesvaraya Technological University,

KALABURAGI

2023-2024
VISVESVARAYA TECHNOLOGICAL UNIVERSITY, BELAGAVI
CPGS, Kalaburagi

U.S.N:3VY23UE059 DATE:

CERTIFICATE

This is to certify that Mr./M....................... Varun R Malashetti............................................


Studying in the B.Tech in ECE……II…........Semester has successfully completed all
the Programs in ……Mathematics-II……… with subject code.....BMATE201..as
prescribed by the Visvesvaraya Technological University, Belagavi CPGS Kalaburagi
for the academic year 2023- 2024.

Faculty In-charge Programme Coordinator

Examiners:

1. Internal Examiner 2. Internal Examiner


SL No. Index Page No. Sign

1 Finding gradient , divergent , curl and their


geometrical interpretation.

2 Computation of basics and dimension for a vector


space and graphical representation of linear
transformation

3 Visualization in time and frequency domain of


standard function.

4 Computing Laplace transform of standard functions

5 Laplace transform of convolution of two functions

6 Solution of algebraic and transcendental equation by


Regula-Falsi and Newton-Raphson method

7 Interpolation/Extrapolation using Newton’s forward


and backward difference formula

8 Computation of area under the curve using


Trapezoidal, Simphson’s ( 𝟏 /𝟑 )𝒓𝒅 and Simphson’s
(𝟑/𝟖 )𝒕𝒉 rule

9 Solution of ODE of first order and first degree by


Taylor’s series and Modified Euler’s method

10 Solution of ODE of first order and first degree by


Range-kutta 4th order method and Milne’s predictor
and corrector method
LAB 1 :- Finding gradient, divergent, curl and their geometrical
interpretation .

To find gradient of ϕ =x^2y + 2xz – 4.

from sympy.vector import*


from sympy import symbols
N=CoordSys3D('N')
x,y,z=symbols('x y z')
A=N.x**2*N.y+2*N.x*N.z-4
delop=Del()
print(delop(A))
gradA=gradient(A)
print(f"\n Gradient of{A}is \n")
print(gradA)

OUTPUT :
(Derivative(N.x**2*N.y + 2*N.x*N.z - 4, N.x))*N.i +
(Derivative(N.x**2*N.y + 2*N.x*N.z - 4, N.y))*N.j +
(Derivative(N.x**2*N.y + 2*N.x*N.z - 4, N.z))*N.k Gradient of
N.x**2*N.y + 2*N.x*N.z - 4 is (2*N.x*N.y + 2*N.z)*N.i + N.x**2*N.j +
2*N.x*N.k

To find divergence of F ⃗ = 𝑥2yz^i +𝑦 2zx^j +𝑧 2xy^k.

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
divA=Del()
print(f"\n Divergence of{A}is \n")
print(divergence(A))

OUTPUT :
(Derivative(N.x**2*N.y*N.z*N.i + N.x*N.y**2*N.z*N.j +
N.x*N.y*N.z**2*N.k, N.x))*N.i + (Derivative(N.x**2*N.y*N.z*N.i +
N.x*N.y**2*N.z*N.j + N.x*N.y*N.z**2*N.k, N.y))*N.j +
(Derivative(N.x**2*N.y*N.z*N.i + N.x*N.y**2*N.z*N.j +
N.x*N.y*N.z**2*N.k, N.z))*N.k
divergence of N.x**2*N.y*N.z*N.i + N.x*N.y**2*N.z*N.j +
N.x*N.y*N.z**2*N.kis
6*N.x*N.y*N.z
To find curl of F⃗ = 𝑥2yzˆi + 𝑦2zxˆj + 𝑧 2xykˆ.

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)
print(curlA)
print(f"\n Curl of {A} is \n")
print(curl(A))

OUTPUT:
(Derivative(N.x**2*N.z*N.i + N.x*N.y**2*N.z*N.j + N.x*N.y*N.z**2*N.k,
N.x))*N.i + (Derivative(N.x**2*N.z*N.i + N.x*N.y**2*N.z*N.j +
N.x*N.y*N.z**2*N.k, N.y))*N.j + (Derivative(N.x**2*N.z*N.i +
N.x*N.y**2*N.z*N.j + N.x*N.y*N.z**2*N.k, N.z))*N.k
curl of N.x**2*N.z*N.i + N.x*N.y**2*N.z*N.j + N.x*N.y*N.z**2*N.k is
(-N.x*N.y**2 + N.x*N.z**2)*N.i + (N.x**2 - N.y*N.z**2)*N.j +
N.y**2*N.z*N.k
LAB 2 :- Computation of basis and dimension for a vector space
and graphical representation of linear transformation.
Rank Nullity Theorem:

Verify the rank-nullity theorem for the linear transformation T :𝑅 3→ 𝑅3defined by


T(x, y, z) = (x + 4y + 7z, 2x + 5y + 8z, 3x + 6y + 9z).

from numpy import *


from scipy . linalg import null_space
A = array ([[1 , 2 , 3], [4 , 5 , 6], [7 , 8 , 9]])
rank =linalg . matrix_rank ( A )
print (" Rank of the matrix ", rank )
ns = null_space ( A )
print (" Null space of the matrix ", ns )
nullity = ns . shape [1]
print (" Null space of the matrix ", nullity )
if rank + nullity == A . shape [1]:
print ("Rank - nullity theorem holds .")
else :
print ("Rank - nullity theorem does not hold .")

OUTPUT:
Rank of the matrix 2
Null space of the matrix [[-0.40824829]
[ 0.81649658]
[-0.40824829]]
Null space of the matrix 1
Rank - nullity theorem holds .

Dimension of vector space:

Find the dimension of subspace spanned by the vectors (1, 2, 3),(2, 3, 1) and (3, 1, 2).

from numpy import *


V= array ([ [1 , 2 , 3], [2 , 3 , 1], [3 , 1 , 2]])
basis = linalg . matrix_rank ( V )
dimension = V.shape [0]
print (" Basis of the matrix ", basis )
print (" Dimension of the matrix ", dimension)

OUTPUT:
Basis of the matrix 3
Dimension of the matrix 3
LAB 3 :- Visualization in time and frequency domain of standard
functions.

Laplace Transformation:

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

from sympy import * import


matplotlib.pyplot as plt
s,t=symbols(‘s t’, positive=True)
f=sin(2*t)
F=laplace_transform(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), line_color=’red’,
legend=True) plotgrid=plotting.PlotGrid(2,1,p1,p2,
show=false ,size=(5. ,3.5)
plotgrid.show()

OUTPUT:
The Laplace Transform of f is 2/(s**2 + 4) 2/(s**2 + 4)
LAB 4 :- Computing Laplace transform and inverse Laplace
transform of standard functions

from sympy import *

t, s, a = symbols('t s a', real=True, positive=True)


init_printing()

f = sin(a*t)
laplace_transform = integrate(f*exp(-s*t), (t, 0, oo))
laplace_transform

OUTPUT:-
LAB 5 :- Laplace transform of convolution of two functions.
The Convolution Theorem:

Find the Laplace Transform of the Convolution of the functions f(t)= t and g(t)=et

from sympy import *

t, s, tau = symbols('t s tau')


f = t
g = exp(t)
fog = integrate(f.subs({t: tau}) * g.subs({t: t - tau}), (tau, 0, t))

def LT(func):
return laplace_transform(func, t, s, noconds=True)

FOG = LT(fog)
print('Laplace Transform of the convolution of given functions is:')
display(FOG)

OUTPUT:
Laplace Transform of the convolution of given functions is :
1/(s**2*(s - 1))


LAB 6 :- Solution of algebraic and transcendental equation by
Regula – falsi and Newton – Raphson method.

Newton-Raphson Method to solve a transcendental Equation.

Find a root of the equation 3x=cosx+1, near 1, by Newton Rapshon Method .Perform
5 iterations.

from sympy import*


x = Symbol('x')
g = input("enter the function:")
f = lambdify(x,g)
dg = diff(g);
df = lambdify(x,dg)
x0 = float(input('enter the intial approximation'));
n = int(input('enter the number of iterations'));
for i in range(1,n+1):
x1 = (x0-(f(x0)/df(x0)))
print('iteration %d\t the root %0.3f\t function
value %0.3f\n' %(i,x1,f(x1)));

OUTPUT:
enter the function : 3*x -cos(x) -1

enter the initial approximation : 1

enter the number of iterations : 5

iteration 1 the root 0.620 function value 0.046


iteration 2 the root 0.620 function value 0.046
iteration 3 the root 0.620 function value 0.046
iteration 4 the root 0.620 function value 0.046
iteration 5 the root 0.620 function value 0.046
Using tolerance value we can write the same program as follows:

Obtain a root of the equation x 3 − 2x − 5 = 0 between 2 and 3 by regula-falsi


method. Correct to 3 decimal places.
from sympy import *
x= Symbol ('x')
g = input ('Enter the function')
f= lambdify(x,g)
a= float ( input ('Enter a valus :') )
b= float ( input ('Enter b valus :') )
N= float ( input ('Enter tolarence :') )
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 )

Output:
Enter the function x**3-2*x-5

Enter a valus :2

Enter b valus :3

Enter tolarence :0.001

iteration 1 the root 2.059 function value -0.391

iteration 2 the root 2.081 function value -0.147

iteration 3 the root 2.090 function value -0.055

iteration 4 the root 2.093 function value -0.020

iteration 5 the root 2.094 function value -0.007

iteration 6 the root 2.094 function value -0.003

final value of the root is 2.09431


Regula-Falsi method to solve a transcendental equation:

Obtain a root of the equation x 3 − 2x − 5 = 0 between 2 and 3 by regula-falsi


method. Prepare 5 iterations.

from sympy import *


x=Symbol('x')
g=input('enter the function')
f=lambdify(x,g)
a=float(input('enter a value:'))
b=float(input('enter b value:'))
N=int(input('enter number of iterations:'))
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('iteration%d\t the root %0.3f\t function
value %0.3f\n'%(i,c,f(c)))

OUTPUT:
enter the function : x**3-2*x-5

enter a value : 2
enter b value : 3
enter number of iterations : 5

iteration1 the root 2.059 function value -0.391


iteration2 the root 2.081 function value -0.147
iteration3 the root 2.090 function value -0.055
iteration4 the root 2.093 function value -0.020
iteration5 the root 2.094 function value -0.007
LAB 7 :- Interpolation / Extrapolation using Newton’s forward and
backward difference formula

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

from sympy import *


import numpy as np
n = int(input('Enter number of data points: '))
x = np.zeros((n))
y = np.zeros((n, n))
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(1, n):
for j in range(0, n - i):
y[j][i] = y[j + 1][i - 1] - y[j][i - 1]
print('\nFORWARD DIFFERENCE TABLE\n')
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()
t = symbols('t')
f = []
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]
simp_poly = simplify(poly)
print('\nTHE INTERPOLATING POLYNOMIAL IS\n')
pprint(simp_poly)
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, modules=['numpy'])
result = interpol(a)
print('\nThe value of the function at ', a, ' is\n', result)
OUTPUT :
Enter number of data points : 5
Enter data for x and y:
x[0]=1

y[0]= 6

x[1]= 3

y[1]= 10

x[2]= 5

y[2]= 62

x[3]= 7

y[3]= 210

x[4]= 9

y[4]= 502

FORWARD DIFFERENCE TABLE

1.00 6.00 4.00 48.00 48.00 0.00

3.00 10.00 52.00 96.00 48.00

5.00 62.00 148.00 144.00

7.00 210.00 292.00

9.00 502.00
THE INTERPOLATING POLYNOMIAL IS
3 2
1.0⋅t - 3.0⋅t + 1.0⋅t + 7.0

Do you want to interpolate at a point (y/n)?

Enter the point 2

The value of the function at 2.0 is 5.0


LAB 8 :- Computation of area under the curve using Trapezoidal ,
simpson’s (1/3)rd and simpson’s (3/8)th rule .

Trapezoidal Rule:
Evaluate ∫ 50 1 2 dx.
1+𝑥
from sympy import *
var('x')
f=lambda x:1/(1+x**2)
x0=0
xn=5
n=10
h=(xn-x0)/n
I=f(x0)+f(xn)
for i in range(1,n):
k=x0+i*h
i=i+2*f(k)
result=I*(h/2)
print("Trapezoidal y=",result)

OUTPUT :
Trapezoidal y= 0.25961538461538464

Simpson’s (1/3)rd Rule:


Evaluate∫ 60 1 2 dx
1+𝑥
from sympy import *
var('x')
f=lambda x:1/(1+x**2)
x0=0
xn=5
n=100
h=(xn-x0)/n
I=f(x0)+f(xn)
for i in range(1,n):
k=x0+i*h
if i%2==0:
i=i+4*f(k)
else:
i=i+2*f(k)
result=I*(h/3)
print("simpson’s1/3th y=",result)

OUTPUT :
simpsons1/3th y= 0.01730769230769231
Simpson’s (3/8)th Rule:
Evaluate using∫ 60 1 2 dx Simpson’s 3/8 th rule, taking 6 sub intervals
1+𝑥

from sympy import *


var('x')
f=lambda x:1/(1+x**2)
x0=0
xn=6
n=6
h=(xn-x0)/n
s=f(x0)+f(xn)
for i in range(1,n,3):
s+=3*f(x0+i*h)
for i in range(3,n-1,3):
s+=3*f(x0+i*h)
for i in range(2,n-2,3):
s+=2*f(x0+i*h)
result=s*3*(h/8)
print("simpson's 3/8th rule y=",result)

OUTPUT :
simpson's 3/8th rule y= 1.2763116057233705
LAB 9 :- Solution of ODE of first order and first degree by Taylor’s
series and Modified Euler’s method.

Taylor series method to solve ODE:

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

from numpy import array,zeros,exp


def taylor(deriv, x, y, xStop, h):
X = []
Y = []
X.append(x)
Y.append(y)
while x < xStop:
D = deriv(x,y)
H = 1.0
for j in range(3):
H = H * h / (j + 1)
y = y + D[j][0]*H
x = x + h
X.append(x)
Y.append(y)
return array(X), array(Y)
def deriv(x, y):
D = zeros((4,1))
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
x = 0.0
xStop = 0.3
y = array([0])
h=0.1
X,Y = taylor(deriv ,x, y, xStop, h)
print(" The required values are : at x=%0.2f, y=%0.5f, x=%0.2f,
y=%0.5f, x=%0.2f,
y=%0.5f, x=%0.2f, y=%0.5f" %(X[0], Y[0], X[1], Y[1], X[2], Y[2],
X[3], Y[3]))

OUTPUT :
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.
Modified Euler’s method:

Using modified Euler’s method at x= 100 , by taking h= 25.

import numpy as np
import matplotlib.pyplot as plt
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] + h, y[i] + k1)
y[i + 1] = y[i] + 0.5 * (k1 + k2)
return x, y
def f(x, y):
return -0.01 * y
x0 = 0.0
y0 = 100.0
h = 25
n = 4
x, y = modified_euler(f, x0, y0, h, n)
print("The required value at x=%.2f, y=%.5f" % (x[4], y[4]))
print("\n\n")
plt.plot(x, y, 'bo-')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Solution of dy/dx = -ky using Modified Euler\'s Method')
plt.grid(True)
plt.show()
OUTPUT :
required value at x= 100.00, y=37.25290
LAB 10 :- Solution of ODE of first order and first degree by Range –
kutta 4th order method and Milne’s predictor and corrector
method.
Range-Kutta method

def RangeKutta(g, x0, h, y0, xn):


x, y = symbols('x,y')
f = lambdify([x, y], g)
xt = x0
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)
x0 = xt
y0 = y1
xt = xt + h
return np.round(Y, 2)
result = RangeKutta('1+(y/x)', 1, 0.2, 2, 2)
print(result)

OUTPUT :
[2. 2.62 3.36 4.14 4.94 5.77 6.62]

You might also like