Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views44 pages

Maths Lab Manual-Sem I

The document outlines the lab component for the First Semester Engineering Mathematics course as prescribed by Visvesvaraya Technological University. It includes evaluation methods, a list of lab activities focused on calculus and linear algebra, and an introduction to Python programming structures. The document also provides specific lab objectives and examples for finding partial derivatives and Jacobians using Python.
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)
9 views44 pages

Maths Lab Manual-Sem I

The document outlines the lab component for the First Semester Engineering Mathematics course as prescribed by Visvesvaraya Technological University. It includes evaluation methods, a list of lab activities focused on calculus and linear algebra, and an introduction to Python programming structures. The document also provides specific lab objectives and examples for finding partial derivatives and Jacobians using Python.
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/ 44

Lab Component

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.

Dr. Smita S. Nagouda Dr. Madhukar Krishnamurthy


CHRIST(Deemed to be University), BMS College of Engineering,
Central Campus, Bengaluru, INDIA. Bull Temple Road, Bengaluru, INDIA.

Dr. Chandra Shekara G. Mr. Sonam Kumar


BMS College of Engineering, AMC Engineering college,
Bull Temple Road, Bengaluru, INDIA. Bannerghatta Road, 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.

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

2
COMPUTER SCIENCE & ENGINEERING STREAM
Scheme:2025 Semester: I
Subject Name/Code: CALCULUS AND LINEAR ALGEBRA (BMATS101)

List of Lab activities:


Ex.No CONTENT
1 Finding partial derivatives and Jacobian
2 Expansion of Taylor’s and Maclaurin’s series
3 Finding Gradient, divergence and curl
4 Finding rank, reduced echelon form, solving
system of linear equations using Gauss
elimination method
5 Solving system of linear equations using Gauss-
Seidel method
6 Determine Eigenvalues and Eigenvectors

7 Linearly Independence and Dependence sets


8 Basis and dimension

9 Linear transformation-range space and null space


10 Verification of the rank nullity theorem

3
I. Introduction to PYTHON
https://drive.google.com/file/d/1gVG2IJ8BIjhYDwDx6jWJns59h9dGOGVi/view?usp=
share_link

II. Programming Structures


Conditional structure
What is conditioning in Python?
• Based on certain conditions, the flow of execution of the program is determined
using proper syntax.

• Often called decision-making statements in Python.

How to use if conditions?


- if statement — for implementing one-way branching

- if..else statements —for implementing two-way branching

- nested if statements —for implementing multiple branching

- if-elif ladder — for implementing multiple branching

# Syntax :

if condition :
statements

# Check if the given number is positive


a = int ( input ( " Enter an integer : " ) )
if a > 0 :
print ( " Entered value is positive " )

Enter an integer: 5
Entered value is positive

# Synatx :
# if condition :
# statements 1
# else :
# statements 2

# If condition is True - statements 1 will be executed


# otherwise - statements 2 will be executed

a = int ( input ( " Enter an integer : " ) )


if a > 0 :

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

# If condition 1 is True - Statements 1 will be executed .


# else if condition 2 is True - Statements 2 will be executed and so on
.
# If any of the conditions is not True then statements in else block is
executed .

# 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 the percentage of marks obtained by a student:65


65.0 % - Grade: First class

# To check if a number is divisble by 7


num1 = int ( input ( " Enter a number : " ) )
if ( num1 % 7 = = 0 ) :
print ( " Divisible by 7 " )
else :
print ( " The given number is not divisible by 7 " )

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

Control flow (Loops)


Loop types:
while loop
- Repeats a statement or group of statements while a given condition is TRUE. It tests
the condition before executing the loop body.

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

# Print multiplication table


n = int ( input ( " Enter the number : " ) )
i=1
while ( i < 11 ) :
print (n , 'x ' ,i , '= ' ,n * i )
i=i+1

Enter the number: 45


45 x 1 = 45
45 x 2 = 90
45 x 3 = 135
45 x 4 = 180
45 x 5 = 225
45 x 6 = 270
45 x 7 = 315
45 x 8 = 360
45 x 9 = 405
45 x 10 = 450

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.

# Use of break ststement


i=1
while i < 6 :
print ( i )
if i = = 3 :
break
i+=1

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

• it falls under the category of definite iteration

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

The range() function


Syntax:
• range(a) : Generates a sequence of numbers from 0 to a, excluding a, incrementing
by 1.

• range(a,b): Generates a sequence of numbers from a to b excluding b, increment-


ing by 1.

• range(a,b,c): Generates a sequence of numbers from a to b excluding b, incre-


menting by c.

# 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 )

# printing the elements of a list


fruits = [ ' apple ' , ' banana ' , ' cherry ' , ' orange ']
for x in fruits :
print ( x )

apple
banana
cherry
orange

Exercise:
1. Finding the factors of a number using for loop.

2. Check the given number is prime or not.

3. Find largest of three numbers.

4. Write a program to print even numbers between 25 and 45.

5. Write a program to print all numbers divisible by 3 between 55 and 75.

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.

Syntax for the commands used:


1. To create a matrix:
Matrix ( [ [ row1 ] ,[ row2 ] ,[ row3 ] .... [ rown ] ] )

Ex: A 3 × 3 matrix can be defined as


Matrix ( [ [ a11 , a12 , a13 ] ,[ a21 , a22 , a23 ] ,[ a31 , a32 a33 ] ] )

2. Evaluate the determinant of a matrix M .


Determinant ( M )
det ( M )

3. To evaluates derivative of function w.r.t variable.


diff ( function , variable )

4. If function is of two or more than two independent variable then it differentiates


the function partially w.r.t variable.
If u = u(x, y) then,
• ∂u
∂x
= dif f (u, x)
• ∂u
∂y
= dif f (u, y)
∂2u
• ∂x2
= dif f (u, x, x)
∂2u
• ∂x∂y
= dif f (u, x, y)

3.2 I. Partial derivatives


The partial derivative of f (x, y) with respect to x at the point (x0 , y0 ) is
∂f f (x0 + h, y0 ) − f (x0 , y0 )
fx = at(x0 , y0 ) = lim .
∂x h→0 h
The partial derivative of f (x, y) with respect to xy at the point (x0 , y0 ) is
∂f f (x0 , y0 + h) − f (x0 , y0 )
fy = at(x0 , y0 ) = lim .
∂y h→0 h

27

11
1. Prove that mixed partial derivatives , uxy = uyx for u = exp(x)(xcos(y) −
ysin(y)).

from sympy import *


x , y = symbols ( 'x y ')

u = exp ( x ) * ( x * cos ( y ) - y * sin ( y ) ) # input mutivariable function u = u (x , y )


dux = diff (u , x ) # Differentate u w . r . t x
duy = diff (u , y ) # Differentate u w . r . t . y
duxy = diff ( dux , y ) # or duxy = diff (u ,x , y )
duyx = diff ( duy , x ) # or duyx = diff (u ,y , x )
# Check the condtion uxy = uyx
if duxy = = duyx :
print ( ' Mixed partial derivatives are equal ')
else :
print ( ' Mixed partial derivatives are not equal ')

2. Prove that if u = ex (x cos(y) − y sin(y)) then uxx + uyy = 0.

from sympy import *


x , y = symbols ( 'x y ')

u = exp ( x ) * ( x * cos ( y ) - y * sin ( y ) )


display ( u )
dux = diff (u , x )
duy = diff (u , y )
uxx = diff ( dux , x ) # or uxx = diff (u ,x , x ) second derivative of u w . r . t x
uyy = diff ( duy , y ) # or uyy = diff (u ,y , y ) second derivative of u w . r . t y
w = uxx + uyy # Add uxx and uyy
w1 = simplify ( w ) # Simply the w to get actual result
print ( ' Ans : ' , float ( w1 ) )

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

1. If u = xy/z, v = yz/x, w = zx/y then prove that J = 4.

from sympy import *

x ,y , z = symbols ( 'x ,y , z ')

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 )

# construct the Jacobian matrix


J = Matrix ( [ [ dux , duy , duz ] ,[ dvx , dvy , dvz ] ,[ dwx , dwy , dwz ] ] ) ;

print ( " The Jacobian matrix is \ n " )


display ( J )

# Find the determinat of Jacobian Matrix


Jac = det ( J ) . doit ()
print ( '\ n \ n J = ' , Jac )

2. If u = x + 3y 2 − z 3 , v = 4x2 yz, w = 2z 2 − xy then prove that at (1, −1, 0), J = 20.

from sympy import *

x ,y , z = symbols ( 'x ,y , 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 ] ] ) ;

print ( " The Jacobian matrix is " )


display ( J )

Jac = Determinant ( J ) . doit ()


print ( '\ n \ n J = \ n ')
display ( Jac )

J1 = J . subs ( [ (x , 1 ) , (y , - 1 ) , (z , 0 ) ] )

print ( '\ n \ n J at (1 , -1 , 0 ) :\ n ')

29

13
Jac1 = Determinant ( J1 ) . doit ()
display ( Jac1 )

∂(X,Y,Z)
3. X = ρ ∗ cos(ϕ) ∗ sin(θ), Y = ρ ∗ cos(ϕ) ∗ cos(θ), Z = ρ ∗ sin(ϕ) then find ∂(ρ,ϕ,θ)
.

from sympy import *


from sympy . abc import rho , phi , theta

X = rho * cos ( phi ) * sin ( theta ) ;


Y = rho * cos ( phi ) * cos ( theta ) ;
Z = rho * sin ( phi ) ;

dx = Derivative (X , rho ) . doit ()


dy = Derivative (Y , rho ) . doit ()
dz = Derivative (Z , rho ) . doit ()
dx1 = Derivative (X , phi ) . doit () ;
dy1 = Derivative (Y , phi ) . doit () ;
dz1 = Derivative (Z , phi ) . doit ()
dx2 = Derivative (X , theta ) . doit ()
dy2 = Derivative (Y , theta ) . doit () ;
dz2 = Derivative (Z , theta ) . doit () ;

J = Matrix ( [ [ dx , dy , dz ] ,[ dx1 , dy1 , dz1 ] ,[ dx2 , dy2 , dz2 ] ] ) ;


print ( ' The Jacobian matrix is ')
display ( J )

print ( '\ n \ n J = \ n ')


display ( simplify ( Determinant ( J ) . doit () ) )

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

3. If x = u − v, y = v − uvw and z = uvw find Jacobian of x, y, z w.r.t u, v, w.


Ans: uv
∂(x,y)
4. If x = rcos(t) and y = rsin(t) then find the ∂(r,t)
.
Ans: J = r
∂(u,v,w)
5. If u = x + 3y 2 − z 3 , v = 4x2 yz and w = 2z 2 − xy find ∂(x,y,z)
at (-2,-1,1).
Ans: 752

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

1. to find find the maxima and minima of function of two variables.

2. to expand the given single variable funtion as Taylor’s and Maclaurin series.

3. to find the limiting value of the given function f (x) as x → a.

Syntax for the commands used:


1. To solve
sympy . solve ( expression )

Returns the solution to a mathematical expression/polynomial.

2. To evaluate an expression
sympy . evalf ()

Returns the evaluated mathematical expression.

3. To construct an instant function


sympy . lambdify ( variable , expression , library )

Converts a SymPy expression to an expression that can be numerically evaluated.


lambdify acts like a lambda function, except it, converts the SymPy names to the
names of the given numerical library, usually NumPy or math.

4. To find the limit of a function


Limit ( expression , variable , value )

Returns the limit of the mathematical expression under given conditions.

4.2 Maxima and minima problem


Find the Maxima and minima of f (x, y) = x2 + y 2 + 3x − 3y + 4.

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 ')

q1 = s1 . subs ( { y : criticalpoints1 , x : criticalpoints2 } ) . evalf ()


q2 = s2 . subs ( { y : criticalpoints1 , x : criticalpoints2 } ) . evalf ()
q3 = s3 . subs ( { y : criticalpoints1 , x : criticalpoints2 } ) . evalf ()
delta = s1 * s2 - s3 ** 2
print ( delta , q1 )

if ( delta > 0 and s1 < 0 ) :


print ( " f takes maximum " )
elif ( delta > 0 and s1 > 0 ) :
print ( " f takes minimum " )
if ( delta < 0 ) :
print ( " The point is a saddle point " )
if ( delta = = 0 ) :
print ( " further tests required " )

4.3 Taylor series expansion


(x−x0 )2 ′′
f (x) = f (x0 ) + (x − x0 )f ′ (x0 ) + 2!
f (x).... is called Taylor series expansion of f(x).

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 )

plt . plot (x , yat ( x ) , color = ' red ')


plt . plot (x , f ( x ) , color = ' green ')
plt . ylim ( [ -3 , 3 ] )
plt . grid ()
plt . show ()

4.4 Maclaurin Series


2. Find the Maclaurin series expansion of sin(x) + cos(x) upto 3rd degree term.
Calculate sin(10) + cos(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 )

plt . plot (x , yat ( x ) , color = ' red ')


plt . plot (x , f ( x ) , color = ' green ')
plt . ylim ( [ -3 , 3 ] )
plt . grid ()
plt . show ()

4.5 L’Hospital’ rule


We can evaluate inderminate forms easily in python using Limit command

33

17
sin(x)
1. lim x
x→0

from sympy import Limit , Symbol , exp , sin


x = Symbol ( 'x ')
l = Limit (( sin ( x ) ) /x ,x , 0 ) . doit ()
print ( l )

((5x4 −4x2 −1)


2. Evaluate lim 3
x→1 (10−x−9x )

from sympy import *


x = Symbol ( 'x ')
l = Limit (( 5 * x ** 4 - 4 * x ** 2 - 1 ) / ( 10 - x - 9 * x ** 3 ) ,x , 1 ) . doit ()
print ( l )

1 x

3. Prove that limx→∞ 1 + x
=e

from sympy import *


from math import inf
x = Symbol ( 'x ')
l = Limit (( 1 + 1 / x ) ** x ,x , inf ) . doit ()
display ( l )

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)

3. Find the Taylor Series expansion of y = cos(x) at x = π3 .


Ans:0.010464x4 + 0.00544x3 − 0.155467x2 − 0.1661389657x + 0.827151505
−1
4. Find the Maclaurin’s series expansion of y = e−sin (x) at x = 0 upto x3 term. Also
Plot the graph.
Ans:−0.0833333333333333x3 + 0.166666666666667x2 − 0.5x + 1.0
2sinx–sin2x
5. Evaluate limx→0 x–sinx
Ans:6
√ √ 
6. Evaluate limx→∞ x2 + x + 1 − x2 + 1 .
Ans:0.5

34

18
LAB 3: 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 find divergence and curl of a vector function.

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 )

To find divergence of F⃗ = x2 yz î + y 2 zxĵ + z 2 xy k̂


# To find divergence 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 ()
divA = delop . dot ( A )
display ( divA )

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


display ( divergence ( A ) )

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 )

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


display ( curl ( A ) )

1.3 Method II:


To find gradient of ϕ = x2 yz.
# To find gradient of a scalar point function x ^ 2yz
from sympy . physics . vector import *
from sympy import var , pprint
var ( 'x ,y , z ')
v = ReferenceFrame ( 'v ')
F = v [ 0 ] ** 2 * v [ 1 ] * v [ 2 ]
G = gradient (F , v )
F = F . subs ( [ ( v [ 0 ] ,x ) ,( v [ 1 ] ,y ) ,( v [ 2 ] ,z ) ] )
print ( " Given scalar function F = " )
display ( F )
G = G . subs ( [ ( v [ 0 ] ,x ) ,( v [ 1 ] ,y ) ,( v [ 2 ] ,z ) ] )
print ( " \ n Gradient of F = " )
display ( G )

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 )

To find curl of F⃗ = xy 2 î + 2x2 yz ĵ − 3yz 2 k̂


# To find curl of F = xy ^ 2i + 2x ^ 2yzj - 3yz ^ 2k
from sympy . physics . vector import *
from sympy import var
var ( 'x ,y , z ')
v = ReferenceFrame ( 'v ')
F = v [ 0 ] * v [ 1 ] ** 2 * v . x + 2 * v [ 0 ] ** 2 * v [ 1 ] * v [ 2 ] * v . y - 3 * v [ 1 ] * v [ 2 ] ** 2 * v . z
G = curl (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 ( " 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

1. to check whether the given system is diagonally dominant or not.

2. to find the solution if the system is diagonally dominant.

Gauss Seidel method is an iterative method to solve system of linear


P equations.′ The
method works if the system is diagonally dominant. That is |aii | ≥ |aij | for all i s.
i̸=j

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

# Reading tolerable error


e = float ( input ( ' Enter tolerable error : ') )

# Implementation of Gauss Seidel Iteration


print ( '\ nCount \ tx \ ty \ tz \ n ')

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

print ( '\ nSolution : x = % 0 . 3f , y = % 0 . 3f and z = % 0 . 3f \ n '% ( x1 , y1 , z1 ) )

Enter tolerable error: 0.001

Count x y z

1 0.8500 -1.0275 1.0109

2 1.0025 -0.9998 0.9998

3 1.0000 -1.0000 1.0000

Solution: x=1.000, y=-1.000 and z = 1.000

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

# Reading tolerable error


e = float ( input ( ' Enter tolerable error : ') )
# Implementation of Gauss Seidel Iteration
print ( '\ t Iteration \ t x \ t y \ t z \ n ')
for i in range (0 , 25 ) :
x1 = f1 ( x0 , y0 , z0 )
y1 = f2 ( x1 , y0 , z0 )
z1 = f3 ( x1 , y1 , z0 )
# Printing the values of x , y , z in ith iteration
print ( '% d \ t % 0 . 4f \ t % 0 . 4f \ t % 0 . 4f \ n ' % (i , x1 , y1 , z1 ) )
e1 = abs ( x0 - x1 ) ;
e2 = abs ( y0 - y1 ) ;
e3 = abs ( z0 - z1 ) ;

x0 = x1
y0 = y1
z0 = z1

if e1 > e and e2 > e and e3 > e :


continue
else :

48

24
break

print ( '\ nSolution : x = % 0 . 3f , y = % 0 . 3f and z = % 0 . 3f \ n '% ( x1 , y1 , z1 ) )

Enter tolerable error: 0.001


Iteration x y z

0 0.3333 1.3333 0.6667

1 0.3333 1.6667 0.7778

Solution: x=0.333, y=1.667 and z = 0.778

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 ] ] )

# We shall check for diagonally dominant


for i in range (0 , len ( a ) ) :
asum = 0
for j in range (0 , len ( a ) ) :
if ( i ! = j ) :
asum = asum + abs ( a [ i ] [ j ] )

if ( asum < = a [ i ] [ i ] ) :
continue
else :

sys . exit ( " The system is not diagonally dominant " )

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

''' ___MAIN___ '''

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

2. Solve the following system of equations using Gauss-Seidel Method.


a. 4x + y + z = 6, 2x + 5y − 2z = 5 and x − 2x − 7z = −8.
b. 27x + 6y − z = 85, 6x + 15y + 2z = 72 and x + y + 54z = 110
Ans: a. [1,1,1] b. [2.42, 3.57, 1.92]

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

1. to find eigenvalues and corresponding eigenvectors.

2. to find dominant and corresponding eigenvector by Rayleigh power method.

Syntax for the commands used:


1. np.linalg.eig(A): Compute the eigenvalues and right eigenvectors of a square
array
np . linalg . eig ( A )

Returns the following:

• 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].

2. np.linalg.eigvals(A): Computes th eigenvalues of a non-symmetric array.

3. np.array(parameter): Creates ndarray

• np.array([[1,2,3]]) is a one-dimensional array


• np.array([[1,2,3,6],[3,4,5,8],[2,5,6,1]]) is a multi-dimensional array

4. lambda arguments:expression: Anonymous function or function without a name

• 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)

5. np.dot(vector a, vector b): Returns the dot product of vectors a and b.

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 )

print ( " \ n Eigen values : \ n " , w )

print ( " \ n Eigen vectors : \ n " , v )

# # To display one eigen value and c orr es po nd in ge ig en vector

print ( " Eigen value :\ n " , w [ 0 ] )


print ( " \ n Corresponding Eigen vector : " , v [ : ,0 ] )

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

Corresponding Eigen vector : [-0.49247712 -0.26523242 -0.82892584]

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 ] ] )

print ( " \ n Given matrix : \ n " , I )

w , v = np . linalg . eig ( I )

print ( " \ n Eigen values : \ n " , w )

print ( " \ n Eigen vectors : \ n " , v )

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]

10.3 Largest eigenvalue and corresponding eigenvector by Rayleigh


method
For a given Matrix A and a given initial eigenvector X0 , the power method goes as
follows: Consider AX0 and take the largest number say λ1 from the column vector and
write AX0 = λ1 X1 . At this stage , λ1 is the approximate eigenvalue and X1 will be
the corresponding eigenvector. Now multiply the Matrix A with X1 and continue the
iteration. This method is going to give the dominant eigenvalue of the Matrix.

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 )

print ( ' Eigenvalue : ' , lambda_1 )


print ( ' Eigenvector : ' , 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 )

print ( ' Eigenvalue : ' , lambda_1 )


print ( ' Eigenvector : ' , 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

1. to verify the Rank nullity theorem of given linear transformation

2. to compute the dimension of vector space

3. to represent linear transformations graphically

4.2 Rank Nullity Theorem


Verify the rank-nullity theorem for the linear transformation T : R3 → R3 defined by
T (x, y, z) = (x + 4y + 7z, 2x + 5y + 8z, 3x + 6y + 9z).
import numpy as np
from scipy . linalg import null_space

# Define a linear transformation interms of matrix


A = np . array ( [ [1 , 2 , 3 ] , [4 , 5 , 6 ] , [7 , 8 , 9 ] ] )

# Find the rank of the matrix A


rank = np . linalg . matrix_rank ( A )
print ( " Rank of the matrix " , rank )

# Find the null space of the matrix A


ns = null_space ( A )
print ( " Null space of the matrix " , ns )
# Find the dimension of the null space
nullity = ns . shape [ 1 ]
print ( " Null space of the matrix " , nullity )
# Verify the rank - nullity theorem
if rank + nullity = = A . shape [ 1 ] :
print ( " Rank - nullity theorem holds . " )
else :
print ( " Rank - nullity theorem does not hold . " )

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.

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

# Define the vector space V


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

Basis of the matrix 3


Dimension of the matrix 3

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

coords = np . array ( [ [0 , 0 ] ,[ 0 .5 , 0 . 5 ] ,[ 0 .5 , 1 . 5 ] ,[0 , 1 ] ,[0 , 0 ] ] )


coords = coords . transpose ()
coords
x = coords [0 , : ]
y = coords [1 , : ]

A = np . array ( [ [2 , 0 ] ,[0 , 1 ] ] )
A_coords = A@coords
x_LT1 = A_coords [0 , : ]
y_LT1 = A_coords [1 , : ]

# Create the figure and axes objects


fig , ax = plt . subplots ()

# 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 ')

# Connect the points by lines


ax . plot (x ,y , 'r ' , ls = " --" )
ax . plot ( x_LT1 , y_LT1 , 'b ')

# Edit some settings


ax . axvline ( x =0 , color = " k " , ls = " : " )
ax . axhline ( y =0 , color = " k " , ls = " : " )
ax . grid ( True )
ax . axis ( [ -2 ,2 , -1 , 2 ] )
ax . set_aspect ( ' equal ')
ax . set_title ( " Horizontal Stretch " ) ;

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 , : ]

# Create the figure and axes objects


fig , ax = plt . subplots ()

# 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 ')

# Connect the points by lines


ax . plot (x ,y , 'r ' , ls = " --" )
ax . plot ( x_LT2 , y_LT2 , 'b ')

# Edit some settings


ax . axvline ( x =0 , color = " k " , ls = " : " )

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 , : ]

# Create the figure and axes objects


fig , ax = plt . subplots ()

# 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 ')

# Connect the points by lines


ax . plot (x ,y , 'r ' , ls = " --" )
ax . plot ( x_LT3 , y_LT3 , 'b ')

# Edit some settings


ax . axvline ( x =0 , color = " k " , ls = " : " )
ax . axhline ( y =0 , color = " k " , ls = " : " )
ax . grid ( True )
ax . axis ( [ -2 ,2 , -1 , 2 ] )
ax . set_aspect ( ' equal ')

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 , : ]

# Create the figure and axes objects


fig , ax = plt . subplots ()

# 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 ')

# Connect the points by lines


ax . plot (x ,y , 'r ' , ls = " --" )
ax . plot ( x_LT4 , y_LT4 , 'b ')

# Edit some settings


ax . axvline ( x =0 , color = " k " , ls = " : " )
ax . axhline ( y =0 , color = " k " , ls = " : " )
ax . grid ( True )
ax . axis ( [ -2 ,4 , -1 , 2 ] )
ax . set_aspect ( ' equal ')
ax . set_title ( " Shear " ) ;

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 , : ]

# Create the figure and axes objects


fig , ax = plt . subplots ()

# 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 ')

# Connect the points by lines


ax . plot (x ,y , 'r ' , ls = " --" )
ax . plot ( x_LT5 , y_LT5 , 'b ')

# Edit some settings


ax . axvline ( x =0 , color = " k " , ls = " : " )
ax . axhline ( y =0 , color = " k " , ls = " : " )
ax . grid ( True )
ax . axis ( [ -2 ,2 , -1 , 2 ] )
ax . set_aspect ( ' equal ')

73

43
4.5 Exercise:
1. Verify the rank nullity theorem for the following linear transformation

a) T : R2 → R3 defined by T (x, y) = (x + 4y, 2x + 5y, 3x + 6y).


Ans: Rank=2, Nullity=1, RNT verified
b) T : R3 → R4 defined by T (x, y, z) = (x+4y−z, 2x+5y+8z, 3x+y+2z, x+y+z).
Ans: Rank=3, Nullity=1, RNT verified

2. Find the dimension of the subspace spanned following set of vectors

a) S = (1, 2, 3, 4), (2, 4, 6, 8), (1, 1, 1, 1)


Ans: Dimension of subspace is 2
b) S = (1, −1, 3, 4), (2, 1, 6, 8), (1, 1, 1, 1), (3, 3, 3, 3)
Ans: Dimension of subspace is 3

3. Find the image of (1, 3) under following 2D transformations

a) Horizontal stretch
b) Reflection
c) Shear
d) Rotation

74

44

You might also like