Maths Lab Manual-Sem I
Maths Lab Manual-Sem I
of
First 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
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.
2
COMPUTER SCIENCE & ENGINEERING STREAM
Scheme:2025 Semester: I
Subject Name/Code: CALCULUS AND LINEAR ALGEBRA (BMATS101)
3
I. Introduction to PYTHON
https://drive.google.com/file/d/1gVG2IJ8BIjhYDwDx6jWJns59h9dGOGVi/view?usp=
share_link
# Syntax :
if condition :
statements
Enter an integer: 5
Entered value is positive
# Synatx :
# if condition :
# statements 1
# else :
# statements 2
4
print ( " Number entered is positive " )
else :
print ( " Number entered is negative " )
Enter an integer: -5
Number entered is negative
# Syntax :
# if condition 1 :
# statements 1
# elif condition 2 :
# statements 2
# elif condition 3 :
# statements 3
# else :
# statements 4
# Example :
perc = float ( input ( " Enter the percentage of marks obtained by a student : "
))
if perc > = 75 :
print ( perc , ' % - Grade : Distinction ')
elif perc > = 60 :
print ( perc , ' % - Grade : First class ')
elif perc > = 50 :
print ( perc , ' % - Grade : Second class ')
else :
print ( perc , ' % - Grade : Fail ')
Enter a number:45
The given number is not divisible by 7
5
# Conversion Celsius to Fahrenheit and vice - versa :
def print_menu () :
print ( " 1 . Celsius to Fahrenheit " )
print ( " 2 . Fahrenheit to Celsius " )
def Far () :
c = float ( input ( " Enter Temperature in Celsius : " ) )
f = c * ( 9 / 5 ) + 32
print ( " Temperature in Fahrenheit : { 0 : 0 . 2f } " . format ( f ) )
def Cel () :
f = float ( input ( " Enter Temperature in Fahrenheit : " ) )
c = ( f - 32 ) * ( 5 / 9 )
print ( " Temperature in Celsius : { 0 : 0 . 2f } " . format ( c ) )
print_menu ()
choice = input ( " Which conversion would you like : " )
if ( choice = = '1 ') :
Far ()
elif ( choice = = '2 ') :
Cel ()
else : print ( " INVALID " )
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Which conversion would you like: 1
Enter Temperature in Celsius: 34
Temperature in Fahrenheit: 93.20
for loop
- Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.
nested loops
- You can use one or more loop inside any another while, for or do..while loop.
6
1. While loop
• Is used to execute a block of statements repeatedly until a given condition is satis-
fied.
• When the condition becomes false, the line immediately after the loop in the pro-
gram is executed
• Syntax:
while expression :
statement ( s )
# Fibonacci series :
# the sum of two elements defines the next
a, b = 0, 1 # First step : a = 0 ; b = 1 second step : a = 1 ; b = 1 + 0
while a < 10 :
a , b =b , a + b
print ( a )
1
1
2
3
5
8
13
7
break statement
• It terminates the current loop and resumes execution at the next statement.
• The most common use for break is when some external condition is triggered re-
quiring a hasty exit from a loop.
• The break statement can be used in both while and for loops.
• If you are using nested loops, the break statement stops the execution of the inner-
most loop and start executing the next line of code after the block.
1
2
3
Continue statement
• The continue statement rejects all the remaining statements in the current iteration
of the loop and moves the control back to the top of the loop.
• The continue statement can be used in both while and for loops.
i=0
while i < 6 :
i+=1
if i = = 3 :
continue
print ( i )
1
2
4
5
6
8
2. for loop
• are used for sequential traversal
• also used to access elements from a container (for example list, string, tuple) using
built-in function range()
• Syntax:
for variable_name in sequence :
statement_1
statement_2
....
# Print numbers from 101 to 130 with a step length 2 excluding 130 .
for i in range ( 101 , 130 , 2 ) :
print ( i )
101
103
105
107
109
111
113
115
117
119
121
123
125
127
129
9
One can type the following examples and observe the outputs.
# Sum of first n natural numbers
sum = 0
n = int ( input ( " Enter n : " ) )
for i in range (1 , n + 1 ) : # i =1 , sum = 1 ; i =2 , sum = 3 ; i =4 , sum =7 , ....
sum = sum + i
print ( " Sum of first " ,n , " natural numbers = " , sum )
# Multiplication table
n = int ( input ( " Enter the number " ) )
for i in range (1 , 11 ) :
print (n , 'x ' ,i , '= ' ,n * i )
apple
banana
cherry
orange
Exercise:
1. Finding the factors of a number using for loop.
10
LAB 13: Finding partial derivatives and Jacobian of
functions of several variables.
3.1 Objectives:
Use python
1. to find partial derivatives of functions of several variables.
2. to find Jacobian of function of two and three variables.
27
11
1. Prove that mixed partial derivatives , uxy = uyx for u = exp(x)(xcos(y) −
ysin(y)).
3.3 II Jacobians
Let x = g(u, v) and y = h(u, v) be a transformation of the plane. Then the Jacobian of
this transformation is
∂x ∂x
∂(x, y)
J= = ∂u
∂y
∂v
∂y .
∂(u, v) ∂x ∂v
u=x*y/z
v=y*z/x
w=z*x/y
# find the all first order partial derivates
dux = diff (u , x )
28
12
duy = diff (u , y )
duz = diff (u , z )
dvx = diff (v , x )
dvy = diff (v , y )
dvz = diff (v , z )
dwx = diff (w , x )
dwy = diff (w , y )
dwz = diff (w , z )
u = x + 3 * y ** 2 - z ** 3
v = 4 * x ** 2 * y * z
w = 2 * z * z ** 2 - x * y
dux = diff (u , x )
duy = diff (u , y )
duz = diff (u , z )
dvx = diff (v , x )
dvy = diff (v , y )
dvz = diff (v , z )
dwx = diff (w , x )
dwy = diff (w , y )
dwz = diff (w , z )
J = Matrix ( [ [ dux , duy , duz ] ,[ dvx , dvy , dvz ] ,[ dwx , dwy , dwz ] ] ) ;
J1 = J . subs ( [ (x , 1 ) , (y , - 1 ) , (z , 0 ) ] )
29
13
Jac1 = Determinant ( J1 ) . doit ()
display ( Jac1 )
∂(X,Y,Z)
3. X = ρ ∗ cos(ϕ) ∗ sin(θ), Y = ρ ∗ cos(ϕ) ∗ cos(θ), Z = ρ ∗ sin(ϕ) then find ∂(ρ,ϕ,θ)
.
3.4 Exercise:
Plot the following:
∂2u ∂2u
1. If u = tan−1 (y/x) verify that ∂y∂x
= ∂x∂y
.
Ans:True
2 2
2. If u = log( xx+y
+y
) show that xux + yuy = 1.
Ans: True
30
14
LAB 24: Expansion of Taylor's
Applications and and
of Maxima Maclaurin's
MinimaSeries
of func-
tions of two variables, Taylor series expansion and
L’Hospital’s Rule
4.1 Objectives:
Use python
2. to expand the given single variable funtion as Taylor’s and Maclaurin series.
2. To evaluate an expression
sympy . evalf ()
import sympy
from sympy import Symbol , solve , Derivative , pprint
x = Symbol ( 'x ')
y = Symbol ( 'y ')
f = x ** 2 + x * y + y ** 2 + 3 * x - 3 * y + 4
31
15
d1 = Derivative (f , x ) . doit ()
d2 = Derivative (f , y ) . doit ()
criticalpoints1 = solve ( d1 )
criticalpoints2 = solve ( d2 )
s1 = Derivative (f ,x , 2 ) . doit ()
s2 = Derivative (f ,y , 2 ) . doit ()
s3 = Derivative ( Derivative (f , y ) ,x ) . doit ()
print ( ' function value is ')
1. Expand sin(x) as Taylor series about x = pi/2 upto 3rd degree term. Also
find sin(1000 )
import numpy as np
from matplotlib import pyplot as plt
from sympy import *
x = Symbol ( 'x ')
y = sin ( 1 * x )
format
x0 = float ( pi / 2 )
dy = diff (y , x )
d2y = diff (y ,x , 2 )
d3y = diff (y ,x , 3 )
yat = lambdify (x , y )
dyat = lambdify (x , dy )
d2yat = lambdify (x , d2y )
d3yat = lambdify (x , d3y )
y = yat ( x0 ) + (( x - x0 ) / 2 ) * dyat ( x0 ) + (( x - x0 ) ** 2 / 6 ) * d2yat ( x0 ) + (( x - x0 ) ** 3 / 24 ) *
d3yat ( x0 )
print ( simplify ( y ) )
yat = lambdify (x , y )
print ( " % . 3f " % yat ( pi / 2 + 10 * ( pi / 180 ) ) )
32
16
def f ( x ) :
return np . sin ( 1 * x )
x = np . linspace ( - 10 , 10 )
import numpy as np
from matplotlib import pyplot as plt
from sympy import *
x = Symbol ( 'x ')
y = sin ( x ) + cos ( x )
format
x0 = float ( 0 )
dy = diff (y , x )
d2y = diff (y ,x , 2 )
d3y = diff (y ,x , 3 )
yat = lambdify (x , y )
dyat = lambdify (x , dy )
d2yat = lambdify (x , d2y )
d3yat = lambdify (x , d3y )
y = yat ( x0 ) + (( x - x0 ) / 2 ) * dyat ( x0 ) + (( x - x0 ) ** 2 / 6 ) * d2yat ( x0 ) + (( x - x0 ) ** 3 / 24 ) *
d3yat ( x0 )
print ( simplify ( y ) )
yat = lambdify (x , y )
print ( " % . 3f " % yat ( 10 * ( pi / 180 ) ) )
def f ( x ) :
return np . sin ( 1 * x ) + np . cos ( x )
x = np . linspace ( - 10 , 10 )
33
17
sin(x)
1. lim x
x→0
1 x
3. Prove that limx→∞ 1 + x
=e
4.6 Exercise:
Plot the following:
1. Find the Taylor Series expansion of y = e−2x at x = 0 upto third degree term.
Ans: −0.333333333333333 ∗ x3 + 0.666666666666667 ∗ x2 − 1.0 ∗ x + 1.0
2
2. Expand y = xe−3x as Maclaurin’s series upto fifth degree term.
Ans: x ∗ (0.75 ∗ x4 − 0.75 ∗ x2 + 0.5)
34
18
LAB 3: Finding gradient, divergent, curl and their
geometrical interpretation
1.1 Objectives:
Use python
1.2 Method I:
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 )
59
19
To find curl of F⃗ = x2 yz î + y 2 zxĵ + z 2 xy k̂
# 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 )
60
20
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 )
61
21
1.4 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̂.
⃗ = xî + y ĵ + z k̂, show that (i) ∇ · R
5. If R ⃗ = 3, (ii) ∇ × R
⃗ = 0.
62
22
LAB 59: Solution of system of linear equations by
Gauss-Seidel method.
9.1 Objectives:
Use python
Example 1:
Solve the system of equations using Gauss-Seidel method: 20x+y−2z = 17; 3x+20y−z =
−18; 2x − 3y + 20z = 25.
# Gauss Seidel Iteration
# Defining equations to be solved
# in diagonally dominant form
f1 = lambda x ,y , z : ( 17 - y + 2 * z ) / 20
f2 = lambda x ,y , z : ( - 18 - 3 * x + z ) / 20
f3 = lambda x ,y , z : ( 25 - 2 * x + 3 * y ) / 20
# Initial setup
x0 = 0
y0 = 0
z0 = 0
count = 1
condition = True
while condition :
x1 = f1 ( x0 , y0 , z0 )
y1 = f2 ( x1 , y0 , z0 )
z1 = f3 ( x1 , y1 , z0 )
print ( '% d \ t % 0 . 4f \ t % 0 . 4f \ t % 0 . 4f \ n ' % ( count , x1 , y1 , z1 ) )
e1 = abs ( x0 - x1 ) ;
e2 = abs ( y0 - y1 ) ;
e3 = abs ( z0 - z1 ) ;
count + = 1
x0 = x1
y0 = y1
z0 = z1
47
23
condition = e1 > e and e2 > e and e3 > e
Count x y z
Example 2:
Solve x + 2y − z = 3; 3x − y + 2z = 1; 2x − 2y + 6z = 2 by Gauss-Seidel Iteration method.
# Defining equations to be solved
# in diagonally dominant form
f1 = lambda x ,y , z : ( 1 + y - 2 * z ) / 3
f2 = lambda x ,y , z : ( 3 - x + z ) / 2
f3 = lambda x ,y , z : ( 2 - 2 * x + 2 * y ) / 6
# Initial setup
x0 , y0 , z0 = 0 ,0 , 0
x0 = x1
y0 = y1
z0 = z1
48
24
break
Example 3:
Apply Gauss-Siedel method to solve the system of equations: 20x + y − 2z = 17; 3x +
20y − z = −18; 2x − 3y + 20z = 25.
from numpy import *
def seidel (a , x ,b ) :
# Finding length of a ( 3 )
n = len ( a )
# for loop for 3 times as to calculate x , y , z
for j in range (0 , n ) :
# temp variable d to store b [ j ]
d = b[j]
# to calculate respective xi , yi , zi
for i in range (0 , n ) :
if ( j ! = i ) :
d=d-a[j][i] * x[i]
# updating the value of our solution
x[j] = d / a[j][j]
# returning our updated solution
return x
a = array ( [ [ 20 .0 , 1 .0 , - 2 . 0 ] ,[ 3 .0 , 20 .0 , - 1 . 0 ] ,[ 2 .0 , - 3 .0 , 20 . 0 ] ] )
x = array ( [ [ 0 . 0 ] ,[ 0 . 0 ] ,[ 0 . 0 ] ] )
b = array ( [ [ 17 . 0 ] ,[ - 18 . 0 ] ,[ 25 . 0 ] ] )
for i in range (0 , 25 ) :
x = seidel (a , x , b )
print ( x )
[[ 1.]
[-1.]
[ 1.]]
Note: In the next example we will check whether the given system is diagonally
dominant or not.
49
25
Example 4:
Solve the system of equations 10x + y + z = 12; x + 10y + z = 12; x + y + 10z = 12 by
Gauss-Seidel method.
from numpy import *
import sys
# This programme will check whether the given system is diagonally
dominant or not
def seidel (a , x ,b ) :
# Finding length of a ( 3 )
n = len ( a )
# for loop for 3 times as to calculate x , y , z
for j in range (0 , n ) :
# temp variable d to store b [ j ]
d = b[j]
# to calculate respective xi , yi , zi
for i in range (0 , n ) :
if ( j ! = i ) :
d=d-a[j][i] * x[i]
# updating the value of our solution
x[j] = d/a[j][j]
# returning our updated solution
return x
a = array ( [ [ 10 .0 , 1 .0 , 1 . 0 ] ,[ 1 .0 , 10 .0 , 1 . 0 ] ,[ 1 .0 , 1 .0 , 10 . 0 ] ] )
x = array ( [ [ 1 . 0 ] ,[ 0 . 0 ] ,[ 0 . 0 ] ] )
b = array ( [ [ 12 . 0 ] ,[ 12 . 0 ] ,[ 12 . 0 ] ] )
if ( asum < = a [ i ] [ i ] ) :
continue
else :
for i in range (0 , 25 ) :
x = seidel (a , x , b )
print ( x )
# Note here that the inputs if float gives the output in float .
[[1.]
[1.]
[1.]]
Note: In the next example, the Upper triangular matrix is calculated by the numpy
function for finding lower triangular matrix. this upper triangular matrix is multiplied by
50
26
the chosen basis function and subtracted by the rhs B column matrix. the new x found is
the product of inverse(lower triangular matrix) and the B-UX. This program is available
on github
Example 5:
Apply Gauss-Siedel method to solve the system of equations: 5x−y−z = −3; x−5y+z =
−9; 2x + y − 4z = −15.
import numpy as np
from scipy . linalg import solve
def gauss (A , b , x , n ) :
L = np . tril ( A )
U = A - L
for i in range ( n ) :
xnew = np . dot ( np . linalg . inv ( L ) , b - np . dot (U , x ) )
x = xnew
print ( x )
# print ( x )
return x
A = np . array ( [ [ 5 .0 , - 1 .0 , - 1 . 0 ] , [ 1 .0 , - 5 .0 , 1 . 0 ] , [ 2 .0 , 1 .0 , - 4 . 0 ] ] )
b = [ - 3 .0 , - 9 .0 , - 15 . 0 ]
x = [1 , 0 , 1 ]
n = 20
gauss (A , b , x , n )
solve (A , b )
[1. 3. 5.]
array([1., 3., 5.])
9.2 Exercise:
1. Check whether the following system are diagonally dominant or not
a. 25x + y + z = 27, 2x + 10y − 3z = 9 and 4x − 2x − 12z = −10.
b. x + y + z = 7, 2x + y − 3z = 3 and 4x − 2x − z = −1.
Ans: a. Yes b. No
51
27
6 Determine
LAB 10: Eigenvaluesand
Compute eigenvalues andcorresponding
Eigenvectors eigen-
vectors. Find dominant and corresponding eigenvec-
tor by Rayliegh power method.
10.1 Objectives:
Use python
• w(. . . , M) array
The eigenvalues, each repeated according to its multiplicity. The eigenvalues
are not necessarily ordered. The resulting array will be of complex type, unless
the imaginary part is zero in which case it will be cast to a real type. When
a is real the resulting eigenvalues will be real (0 imaginary part) or occur in
conjugate pairs.
• v(. . . , M, M) array
The normalized (unit “length”) eigenvectors, such that the column v[:,i] is the
eigenvector corresponding to the eigenvalue w[i].
• This function can have any number of arguments but only one expression,
which is evaluated and returned.
• They are are syntactically restricted to a single expression.
• Example: f=lambda x : x ∗ ∗2 − 3 ∗ x + 1 (Mathematically f (x) = x2 − 3x + 1)
52
28
10.2 Eigenvalues and Eigenvectors
Eigenvector of a matrix A is a vector represented by a matrix X such that when X is
multiplied with matrix A, then the direction of the resultant matrix remains same as
vector X.
Example 1:
Obtain the eigen values and eigen vectors for the given matrix.
4 3 2
1 4 1 .
3 10 4
import numpy as np
I = np . array ( [ [4 ,3 , 2 ] ,[1 ,4 , 1 ] ,[3 , 10 , 4 ] ] )
print ( " \ n Given matrix : \ n " , I )
# x = np . linalg . eigvals ( I )
w , v = np . linalg . eig ( I )
Given matrix:
[[ 4 3 2]
[ 1 4 1]
[ 3 10 4]]
Eigen values:
[8.98205672 2.12891771 0.88902557]
Eigen vectors:
[[-0.49247712 -0.82039552 -0.42973429]
[-0.26523242 0.14250681 -0.14817858]
[-0.82892584 0.55375355 0.89071407]]
Eigen value:
8.982056720677654
53
29
Example 2:
Obtain the eigen values and eigen vectors for the given matrix.
1 −3 3
A = 3 −5 3 .
6 −6 4
import numpy as np
I = np . array ( [ [1 , -3 , 3 ] ,[3 , -5 , 3 ] ,[6 , -6 , 4 ] ] )
w , v = np . linalg . eig ( I )
Given matrix:
[[ 1 -3 3]
[ 3 -5 3]
[ 6 -6 4]]
Eigen values:
[ 4.+0.00000000e+00j -2.+1.10465796e-15j -2.-1.10465796e-15j]
Eigen vectors:
[[-0.40824829+0.j 0.24400118-0.40702229j 0.24400118+0.40702229j]
[-0.40824829+0.j -0.41621909-0.40702229j -0.41621909+0.40702229j]
Example 4:
6 −2 2
Compute the numerically largest eigenvalue of P = −2 3 −1 by power method.
2 −1 3
import numpy as np
def normalize ( x ) :
fac = abs ( x ) . max ()
x_n = x / x . max ()
54
30
return fac , x_n
x = np . array ( [1 , 1 , 1 ] )
a = np . array ( [ [6 , -2 , 2 ] ,
[ -2 ,3 , - 1 ] ,[2 , -1 , 3 ] ] )
for i in range ( 10 ) :
x = np . dot (a , x )
lambda_1 , x = normalize ( x )
Eigenvalue: 7.999988555930031
Eigenvector: [ 1. -0.49999785 0.50000072]
Example 5:
1 1 3
Compute the numerically largest eigenvalue of P = 1 5 1 by power method.
3 1 1
import numpy as np
def normalize ( x ) :
fac = abs ( x ) . max ()
x_n = x / x . max ()
return fac , x_n
x = np . array ( [1 , 1 , 1 ] )
a = np . array ( [ [1 ,1 , 3 ] ,
[1 ,5 , 1 ] ,[3 ,1 , 1 ] ] )
for i in range ( 10 ) :
x = np . dot (a , x )
lambda_1 , x = normalize ( x )
Eigenvalue: 6.001465559355154
Eigenvector: [0.5003663 1. 0.5003663]
10.4 Exercise:
1. Find the eigenvalues
and eigenvectors of the following matrices
25 1
a. P =
1 3
Ans. Eigenvalues are 25.04536102 and 2.95463898; and corresponding eigenvectors
are [0.99897277 − 0.04531442] and [0.04531442 0.99897277].
25 1 2
b. P = 1 3 0
2 0 −4
Ans. Eigenvalues are 25.18215138, −4.13794129 and 2.95578991; and corresponding
55
31
eigenvectors are [0.9966522 0.06880398 0.04416339], [0.04493037 − 0.00963919 −
0.99894362] and [0.0683056 − 0.99758363 0.01269831].
11 1 2
c. P = 0 10 0
0 0 12
Ans. Eigenvalues are 11., 10. and 12.; and corresponding eigenvectors are [1. −
0.70710678 0.89442719], [0. 0.70710678 0.], and [0. 0. 0.4472136].
3 1 1
d. P = 1 2 1
1 1 12
Ans. Eigenvalues are 12.22971565, 3.39910684 and 1.37117751; and eigenvectors are
[−0.11865169 −0.85311963 0.50804396], [−0.10808583 −0.49752078 −0.86069189]
and [−0.98703558 0.1570349 0.03317846].
25 1 2
2. Find the dominant eigenvalue of the matrix P = 1 3 0 by power method.
2 0 −4
T
Take X0 = (1, 0, 1) .
Ans. 25.182151221680012
6 1 2
3. Find the dominant eigenvalue of the matrix P = 1 10 −1 by power method.
2 1 −4
T
Take X0 = (1, 1, 1) .
Ans. 10.107545112667367
5 1 1
4. Find the dominant eigenvalue of the matrix P = 1 3 −1 by power method.
2 −1 −4
T
Take X0 = (1, 0, 0) .
Ans. 5.544020973078026
56
32
LAB7,8 & 10: Linearly of
4: Computation Independent and Dependence
basis and dimension sets
for a vec-
Basis and representation
tor space and graphical Dimension of linear trans-
formation Verification of the rank Nullity Theorem
4.1 Objectives:
Use python
63
33
4.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 ] ) )
64
34
4.4 Graphical representation of a transformation
4.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
65
35
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 ')
66
36
4.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 ')
67
37
ax . axhline ( y =0 , color = " k " , ls = " : " )
ax . grid ( True )
ax . axis ( [ -2 ,2 , -1 , 2 ] )
ax . set_aspect ( ' equal ')
ax . set_title ( " Reflection " ) ;
4.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 ()
68
38
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 ')
69
39
4.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 ()
70
40
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 ')
71
41
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.
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 ()
72
42
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 ')
73
43
4.5 Exercise:
1. Verify the rank nullity theorem for the following linear transformation
a) Horizontal stretch
b) Reflection
c) Shear
d) Rotation
74
44