Lab Manual
Lab Manual
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.
I Basics of Python
II Programming Structure
Lab 2. Finding Angle Between Two Polar Curves, Curvature and Radius of Curvature
Lab 5. Solution of First Order Differential Equations and Plotting the Solution Curve
Lab 8. Numerical Solution of System of Equations, Test for Consistency and Graphical
Representation of the Solution.
Lab 10. Compute Eigen Value and Corresponding Eigen Vectors, Find the Dominant Eigen
Value and Corresponding Eigen Vector by Rayleigh Power Method.
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
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
3
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
4
# 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.
5
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
6
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
7
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
8
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.
9
LAB 1: 2D plots of Cartesian and polar curves.
1.1 Objectives:
Use python
1. to plot Cartesian curves.
2. to plot polar curves.
3. to plot implicit functions.
3. Return num evenly spaced numbers over a specified interval [start, stop]. The
endpoint of the interval can optionally be excluded.
numpy . linspace ( start , stop , num = 50 , endpoint = True , retstep = False ,
dtype = None , axis = 0 )
4. Return evenly spaced values within a given interval. arange can be called with a
varying number of positional arguments.
numpy . arange ( [ start , ] stop , [ step , ] dtype = None , * , like = None )
https://matplotlib.org/stable/api/pyplot_summary.html#module-matplotlib.pyplot
x = [1 ,2 ,3 ,4 ,6 ,7 , 8 ] # x axis values
y = [2 ,7 ,9 ,1 ,5 , 10 , 3 ] # corresponding y axis values
plt . scatter (x , y ) # plotting the points
plt . xlabel ( 'x - axis ') # naming the x axis
plt . ylabel ( 'y - axis ') # naming the y axis
plt . title ( ' Scatter points ') # giving a title to my graph
plt . show () # function to show the plot
10
1.3 Example: Plotting a line(Line plot)
# importing the required module
import matplotlib . pyplot as plt
x = [1 ,2 ,3 ,4 ,6 ,7 , 8 ] # x axis values
y = [2 ,7 ,9 ,1 ,5 , 10 , 3 ] # corresponding y axis values
plt . plot (x , y , 'r + - - ') # plotting the points
plt . xlabel ( 'x - axis ') # naming the x axis
plt . ylabel ( 'y - axis ') # naming the y axis
plt . title ( ' My first graph ! ') # giving a title to my graph
plt . show () # function to show the plot
11
1.4 Functions
1. Exponential curve, y = ex
import numpy as np
import matplotlib . pyplot as plt
x = np . arange ( - 10 , 10 , 0 . 001 )
y1 = np . sin ( x )
y2 = np . cos ( x )
plt . plot (x , y1 ,x , y2 ) # plotting sine and cosine function together with
same values of x
plt . title ( " sine curve and cosine curve " )
plt . xlabel ( " Values of x " )
plt . ylabel ( " Values of sin ( x ) and cos ( x ) " )
plt . grid ()
plt . show ()
12
# A simple graph
import matplotlib . pyplot as plt
import numpy as np
x = np . linspace (0 , 2 , 100 )
plt . title ( " Simple Plot " ) # Add a title to the axes .
plt . legend () # Add a legend
plt . show () # to show the complete graph
13
1.5 Implicit Function
Syntax:
14
2. Strophoid: y 2 (a − x) = x2 (a + x), a > 0
p3 = plot_implicit (
Eq (( y ** 2 ) * ( 2 - x ) , ( x ** 2 ) * ( 2 + x ) ) , (x , -5 , 5 ) , (y , -5 , 5 ) ,
title = ' Strophoid : $y ^ 2 (a - x ) = x ^ 2 ( a + x ) , a > 0$ ') # a=2
3. Cissiod: y 2 (a − x) = x3 , a > 0
15
p4 = plot_implicit (
Eq (( y ** 2 ) * ( 3 - x ) ,x ** 3 ) ,(x , -2 , 5 ) ,(y , -5 , 5 ) ) # a = 3
4. Lemniscate: a2 y 2 = x2 (a2 − x2 )
p5 = plot_implicit (
Eq ( 4 * ( y ** 2 ) ,( x ** 2 ) * ( 4 - x ** 2 ) ) ,(x , -5 , 5 ) ,(y , -5 , 5 ) ) # a = 2
16
5. Folium of De-Cartes: x3 + y 3 = 3axy
p6 = plot_implicit (
Eq ( x ** 3 + y ** 3 , 3 * 2 * x * y ) ,(x , -5 , 5 ) ,(y , -5 , 5 ) ) # a = 2
• r: It is the distance.
import numpy as np
import matplotlib . pyplot as plt
17
# plotting the circle
for i in rads :
plt . polar (i , r , 'g . ')
plt . show ()
18
4. Four leaved Rose: r = 2|cos2x|
import numpy as np
import matplotlib . pyplot as plt
import math
rad = np . arange (0 , ( 2 * np . pi ) , 0 . 01 )
# plotting the cardioid
for i in rad :
r = a + ( a * np . cos ( i ) )
plt . polar (i ,r , 'g . ')
r1 = a - ( a * np . cos ( i ) )
plt . polar (i , r1 , 'r . ')
# display the polar plot
plt . show ()
19
1.7 Parametric Equation
1. Circle: x = acos(θ); y = asin(θ)
import numpy as np
import matplotlib . pyplot as plt
def circle ( r ) :
x = [ ] # create the list of x coordinates
y = [ ] # create the list of y coordinates
20
2. Cycloid: x = a(θ − sinθ); y = a(1 − sinθ)
def cycloid ( r ) :
x = [ ] # create the list of x coordinates
y = [ ] # create the list of y coordinates
1.8 Exercise:
Plot the following:
1. Parabola y 2 = 4ax
x2 y2
2. Hyperbola a2
− b2
=1
3. Lower half of the circle: x2 + 2x = 4 + 4y − y 2
4. cos( πx
2
)
5. 1 + sin(x + π4 )
6. Spiral of Archimedes: r = a + bθ
7. Limacon: r = a + b cosθ
21
LAB 2: Finding angle between two polar curves, cur-
vature and radius of curvature.
2.1 Objectives:
Use python
2. Derivative()
Derivative ( expression , reference variable )
3. doit()
doit ( x )
5. simplify()
simplify ( expression )
8. display()
display ( expression )
22
math_expression . subs ( variable , substitute )
1. Find the angle between the curves r = 4(1 + cos t) and r = 5(1 − cos t).
r1 = 4 * ( cos ( t ) ) ;
r2 = 5 * ( sin ( t ) ) ;
dr1 = diff ( r1 , t )
dr2 = diff ( r2 , t )
t1 = r1 / dr1
t2 = r2 / dr2
q = solve ( r1 - r2 , t )
23
w1 = t1 . subs ( { t : float ( q [ 0 ] ) } )
w2 = t2 . subs ( { t : float ( q [ 0 ] ) } )
y1 = atan ( w1 )
y2 = atan ( w2 )
w = abs ( y1 - y2 )
print ( ' Angle between curves in radians is % 0 . 4f '% float ( w ) )
24
from sympy import *
from sympy . abc import rho , x ,y ,r ,K ,t ,a ,b ,c , alpha # define all symbols
required
y = ( sqrt ( x ) - 4 ) ** 2
y = a * sin ( t ) # input the parametric equation
x = a * cos ( t )
dydx = simplify ( Derivative (y , t ) . doit () ) / simplify ( Derivative (x , t ) . doit () )
# find the derivative of parametric
equation
rho = simplify (( 1 + dydx ** 2 ) ** 1 . 5 / ( Derivative ( dydx , t ) . doit () / ( Derivative (x ,
t ) . doit () ) ) ) # substitute the
derivative in radius of curvature
formula
print ( ' Radius of curvature is ')
display ( ratsimp ( rho ) )
t1 = pi / 2
r1 = 5
rho1 = rho . subs (t , t1 ) ;
rho2 = rho1 . subs (a , r1 ) ;
print ( '\ n \ nRadius of curvature at r = 5 and t = pi / 2 is ' , simplify ( rho2 ) ) ;
curvature = 1 / rho2 ;
print ( '\ n \ n Curvature at (5 , pi / 2 ) is ' , float ( curvature ) )
2.5 Exercise:
Plot the following:
1. Find the angle between radius vector and tangent to the folloing polar curves
a) r = aθ and r = aθ
Ans: Angle between curves in radians is 90.000
b) r = 2sin(θ) and r = 2cos(θ)
Ans: Angle between curves in radians is 90.000
25
2. Find the radius of curvature of r = a(1 − cos(t)) at t = π2 .
2 )1.5
Ans: 0.942809041582063(a
a2
5. Find the radius of curvature of x = a(t − sin(t)) andy = a(1 − cos(t)) at t = π/2.
Ans: ρ = 2.82842712 and κ = 0.353553
26
LAB 3: 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
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
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
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