Bangalore University
LABORATORY MANUAL
FOR
MATHEMATICS PRACTICAL
(WITH FOSS TOOLS)
I SEMESTER B.SC.(2022-23 BATCH)
MATHEMATICS
1
Table of Content
Sl.No Title Page .No
01 Introduction to Python 3-14
02 Simple Python codes using Script file 15-15
03 Matrices: 16-19
1. Algebra of matrices
2. Computation of rank of a matrix
3. Echelon form of a Matrices
04 Solving the system of Homogeneous and 20-21
Non-Homogeneous Linear equations
05 Finding the inverse of the matrix using Caley Hamilton 22-22
theorem
06 Finding the angle between the radius vector and the 23-23
tangent, angle between two curves.
07 Finding the radius of curvature of the given curve. 24-24
08 Verification of Cauchy’s mean value theorem 25-25
09 Taylor’s and Maclaurin’s expansion of a given function 26-26
10 Evaluation of limits by L-Hospital’s rule 27-27
11 Finding the nth derivatives with out Leibnit’z rule 28-28
12 Finding the nth derivatives with Leibnit’z rule 29-29
2
Lab1- Introduction to Python
Python is an interpreted, high-level, and general-purpose programming language. It is a
Free Open Source Software [FOSS].
Python is a widely used general purpose, high level programming language.it was initially
designed by Guido van Rossum in 1991 and developed by Python Software Foundation.
It was mainly developed emphasis on code readability, and its syntax allows programmers
to express concepts in fewer lines of code. Python is a programming language that lets you
work quickly and integrate systems more efficiently There are two major Python versions -
Python 2 and Python 3. Both are quite different.
Installing Python
1. Go to => https://www.python.org/downloads
2. Download the file
3. Double click on downloaded file (check THE BOX add python to path)
4. Install Now
5. Open python IDLE in search or in all program
6. Start working on IDLE
Installation of Packages for Python
Step1: In the command prompt go to the particular location/directory where
python file stored using cd command.
Step2 : Type <python-m pip install—upgrade pip>
Step 3: Type <pip install numpy>
Step4: Type <pip install sympy>
Step5: Type <pip install matplotlib>
How to work in Python
After installation of python, we can work on it in the following ways:
1. in interactive mode
3
2. in Script mode
TOKENS IN PYTHON
The smallest unit in a program is known as a Token. Following are the python tokens.
a) Key words
b) Identifiers (Names)
c) Literals
d) Operators
e) Punctuations
a) Key words: Key words are pre defined words with special meaning and these are
reserved words must not be used as normal identifier names. These key words consist of
False, True, None, and, or, not, as, is, in, if, elif, else, for, while, assert, del, break, class,
continue, def, except, global, finally, from, import, lambda, non-local, pass, return, try, with,
yield.
b) Identifiers: Identifiers are the names given to variables, objects, classes, functions, lists
and soon. Variable names must not be key words and must not contain blank spaces.
Variable names are made up of (A-Z, a-z, 0-9, underscore) and must not start with a digit.
Valied variable names are ABCD, abcd, AbCd, A345, a_bG78, product.
Invalid variable names are 6ab, avfd, for, and.
c) Literals:
a) String literals: String literals are sequence of characters surrounded by quotes( both
single, both double, or both triple).
Single line literals are like 'hello world'
Double line literals are like "my function", "Mera Bharath Mahan"
Multiline string are like ‘’’ program to fund the sum of the first natural umbers ‘’’.
Invalid string literals are ‘hello world", "Hello world'
4
b) Numeric literals: Numeric literals are integers, float, complex numbers (in python
complex numbers are identified by a+bJ).
c) Boolean literals: Boolean literals in python are True, False.
d) Special literal: The special literal is None (Absence of a value)
Operators:
Arithmetic Operator: Arithmetic Operator are used to perform arithmetic operations like
addition, multiplication, division etc.
Table: Arithmetic operators
Operators Description Command
+ Perform addition of two number a+b
- Perform subtraction of two number a-b
/ Perform division of two number a/b
* Perform multiplication of two number a*b
% Modulus = returns remainder a%b
// Floor Division = remove digits after the a//b
decimal point, i.e., gives quotients
** Perform raise to power a**b
Some standard Trigonometric and inverse Trigonometric functions
Before using any trigonometric /math function need to import math library.
>> from math import* OR >> import math
Note: By default, python will take x in radians
Table: Trigonometric functions.
Command Description
sin(x) Returns sine of x in radians
cos(x) Returns cosine of x in radians
5
Tan(x) Returns tangent of x in radians
acos(x) Returns arc cosine of x in radians
atan(x) Returns arc tangent of x in radians
Commands using math library
The math module is a standard module in Python and is always available. To use
mathematical functions under this module, you have to import the module using
import math. It gives access to the underlying C library functions.
For example, # To calculate Square root - import mat.sqrt
1. >>>math.pi
3.141592653589793
The math.pi constant returns the value of PI: 3.141592653589793.
2. >>>pi
3.141592653589793
3. >>>e
2.718281828459045
Note: If you import math, you can use math functions with a dot notation. math. sqrt, for
example. If you from math import *, all math names are available without the dot notation.
Command in Python
1. >>> 2+3
Output: 5
Explanation: 2+3=5
2. >>> 2-3
Output: -1
Explanation: 2-3=-1
3. >>> 2*3
Output: 6
Explanation: 2*3=6
4. >>> 2/3
Output: 0.666666666666
Explanation: 2/3=: 0.666666666666 (2 divided by 3 gives)
6
5. >>> 10**2
Output : 100
Explanation: Square of 10 is 100
6. >>> 2***3
Output: 8
Explanation: 23 =8
7. >>> 2//3
Output: 0
Explanation: Quotient obtained, when dividing 2 by 3
(It displays only integer part in quotient)
8. >>>int (3/2)
Output: 1
Explanation: It display only integer part of the quotient
9. >>>float (33/56)
Output :0.5892857142857143
Explanation: It display both integer and decimal part of the quotient
10. x,y,z= 1,2,3
>>> x, y, z=1,2,3
>>> x
Output :1 (It display the value of x)
>>> y
Output: 2 (It display the value of y)
>>> z
Output :3 (It display the value of z)
7
>>>x+y
Output : 3 ( sum of x and y) [x=1, y=2]
>>>x+y+z
Output : 6 ( sum of x , y and z)
Increment and decrement operator
>>>a = 4
>>> a += 5
>>>a
Output : 9 # add 5 to a, and assign the result into a
>>> a=5
>>> a-=3
>>> a
Output: 2 # Subtract 3 from a, and assign the result into a
Working with Array values
>>> a=[73, 98 ,86, 61, 96]
>>> a
Output : [73, 98 ,86, 61, 96]
>>>a[0] >>>a[-1]
Output :73 Output: 96
Expl: It display the first number of Expl: It display the last number
the array of the array
>>>a[1] >>>a[-2]
Output :98 Output: 61
Expl: It display the second number Expl: It display the last second
of the array number of the array
8
Append commands:
>>> a=[10,20,30,40]
>>>a.append (50)
>>> a
Output :[10, 20, 30, 40, 50] # It insert the number 50 to the end of the array a.
b= [60,70,80]
>>>b.append (30)
>>> b
Output: [60, 70, 80, 30]
Explanation:It insert the number 30 to the end of the array b.
Insert Commands
a= [10,20,30,40]
>>>a. insert (2,90)
>>> a
Output: [10, 20, 90, 30, 40]
Explanation: The insert () method inserts the specified value at the specified position.
a= [10,20,30,40]
a. insert(0,100)
>>> a
Output:[100, 10, 20, 30, 40]
Pop Commands
a= [10,20,30,40]
>>>a.pop ()
40
9
>>> a
Output: [10, 20, 30]
Explanation : Removes an item at the specified index from the list. You can also use
pop in Python without mentioning the index value. In such cases, the pop() function will
remove the last element of the list.
1. a=[10,20,30,40]
>>>a.pop(1)
Output: 20
2. >>> a
Output: [10, 30, 40] # a.pop (1) removes the a[1] =20 of the array a.
a= [10,20,30,40]
3. >>>a.pop(0)
Output: 10
4. >>> a
Output :[10, 30, 40] # a.pop(1) removes the a[0] =10 of the array a.
a=[10,20,30,40]
5. >>> a
[10, 20, 30, 40]
6. >>>a.pop(-1)
40
>>> a
Output :[10, 20, 30]
Explanation :a.pop(-1) removes the last value 40 from the array a.
a.pop(-2) removes the last second value 30 from the array a.
10
If Statement
if statement
The syntax of if statement is
if condition:
Statement 1
Statement 2
Statement 3.
Statement 4
Statement 5
Statement 6
Statement 7
Here the "condition" is a logical expression which is either true or false if the condition”. Is
true then all the statements in the block (Statement 1 to statement 6) are executed and then
the control moves to execute statement 7. If the 'condition " is false, then Statement 1 to
statement 6 are skipped and the control moves to execute statement 7. It may be observed
that statement to statement 7 are placed with same indent which is called the block of if
statement.
Output:
11
if else statement
The syntax of if else statement is
if condition:
Statement 1
Statement 2
Statement 3
Statement-4
Statement 5
Statement6
else:
Statement 7
Statement 8
Statement 9
Statement 10
Here the "condition" is a logical, expression which is either true or false if the
"conditionis true then all the statements in the block (Statement 1 to statement 6) are
executed and then the control moves to execute statement 10. If the 'condition" is false then
Statement 1 to statement are skipped, Statement 7 to Statement 9 are executed and the
control moves to execute statement 10. It may be observed that statement 1 to statement 7
are placed with same indent which is called the true block and Statement 7 to Statement 9
is called false block.
12
Output :
while loop
The syntax of while loop is
while condition:
S1
S2
S3
print()
The while loop is executed as long as condition is true if the condition is false then the
control comes out of the loop and the next statement is executed.
Output:
for loop
The syntax of for loop is
for in range(n):
13
S1
S2
S3
print()
First time i is initialized to zero and all the statements in the body of for loop .i.e.
S1,S2,S3: are executed once, then i increased by 1, again all the statements in the body are
executed, again increased by 1 and the process continues ,when i reaches n then the control
comes out of for loop and the next statement (in this case it is the print statement) is executed
(i takes the values from to n-1).
for i in range(1,n):
S1
S2
S3
print ()
In this case i start with 1 and takes the values up to n-1.
for i in range (1, n, 2):
S1
S2
S3
print()
In this case i start with 1 and takes the values less than n with increment 2.
14
Lab 2 - Simple Python codes using Script file
Python code to find the factorial of a given natural number.
Output:
Output
Exercise
1. Python code to Compare two numbers (like less than, greater than).
2. Python code to find the sum of ‘n’ natural numbers.
3. Python code to find the roots of a quadratic numbers.
4. Python code to print the multiple of three between any two numbers
5. Python code to reverse the given number.
6. Python code to check the given number is palindrome or not.
7. Python code to write the n Fibonacci numbers of the series
8. Python code to check the given number is prime or not.
15
Lab 3 :-Matrices -Algebra of matrices
1. from sympy import*
>>> zeros(3,2)
Matrix([
[0, 0],
[0, 0],
[0, 0]])
2. >>> ones(5,4)
Matrix([
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
3. >>> eye(3,3)
Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
4. a=Matrix(([1,2,4],[2,4,1],[3,1,4]))
>>> a
Matrix([
[1, 2, 4],
[2, 4, 1],
[3, 1, 4]])
>>> min(a)
>>> max(a)
4
16
>>> shape(a)
(3, 3)
transpose(a)
Matrix([
[1, 2, 3],
[2, 4, 1],
[4, 1, 4]])
5. >>> a=Matrix(([1,2,4],[2,4,1],[3,1,4]))
>>> a
Matrix([
[1, 2, 4],
[2, 4, 1],
[3, 1, 4]])
>>> b=Matrix(([3,5,3],[3,6,1],[2,3,1]))
>>> b
Matrix([
[3, 5, 3],
[3, 6, 1],
[2, 3, 1]])
>>> a+b
Matrix([
[4, 7, 7],
[5, 10, 2],
[5, 4, 5]])
>>> a-b
Matrix([
[-2, -3, 1],
[-1, -2, 0],
[ 1, -2, 3]])
>>> a*b
Matrix([
17
[17, 29, 9],
[20, 37, 11],
[20, 33, 14]])
>>> a.inv()
Matrix([
[-3/7, 4/35, 2/5],
[ 1/7, 8/35, -1/5],
[ 2/7, -1/7, 0]])
>>> det(a)
-35
Computation of Rank of a Matrix:
from sympy import*
1. >>> A=Matrix(([1,3,2],[4,2,1],[5,7,2]))
>>> A
Matrix([
[1, 3, 2],
[4, 2, 1],
[5, 7, 2]])
>>> A.rank()
3
2. >>> from sympy import*
>>> A=Matrix(([1,2],[3,4]))
>>> A
Matrix([
[1, 2],
[3, 4]])
>>> A.rank()
2
18
Echelon form of a Matrices
𝟒 𝟔 𝟕
I. Reduce the following matrix to echelon form
𝟐 𝟒 𝟔
Output:
Exercise
𝟑 −𝟏 𝟐
1 −𝟔 𝟐 𝟒
−𝟑 𝟏 𝟐
19
Lab 4 : Solving the System of Homogeneous and Non-Homogeneous Linear
Equations
Solve the system of equations : x+2y+3z=0, 3x+4y+4z=0, 7x+10y+12z=0
Output:
Exercise :
Solve the system: x+3y-2z=0, 2x-y+4z=0, x-11y+14z=0
20
Non homogeneous System of equation
Check for the consistency and hence solve: x+ y+ z=-3, 3x+y-2z=-2, 2x+4y+7z=7
Output:
Coeff Matrix A:
Exercise
Check for the consistency and hence solve:
1) x+2y-z=3, 3x-y+2z=1, 2x-2y+3z=2 2. x +y+ z=6, x+2y+3z=14, x+4y+7z=30
21
Lab 5 - Finding the inverse of the Matrix using Caley Hamilton theorem
1. Using Caley Hamilton theorem find the inverse of the matrix, if it exist for
𝟏 𝟐
A=
𝟑 𝟐
Output:
Exercise
Using Caley Hamilton theorem find the inverse of the following matrix, if it exists
𝟏 𝟎 𝟐 𝟐 −𝟏 𝟏
1. 𝟎 𝟐 𝟏 2. −𝟏 𝟐 −𝟏
𝟐 𝟎 𝟑 𝟏 −𝟏 𝟐
22
Lab: 6 Finding the angle between the radius vector and the tangent, angle between
two curves.
# Angle between the radius and the tangent for the curve r=a*(1+cot) at t=pi/6
Output:-
Exercise:
Find the angle between the radius vector and the tangent
𝑝𝑖 2𝑎 𝑝𝑖 𝑝𝑖
1. r=a*(1-cost) at t= 2. r= at t= 3. r=a(1+sint) at t=
3 1−𝑐𝑜𝑠𝑡 6 3
Find the angle between the curves r=a(1-cost) and r=2acost at t=acos(1/3)
Output:
23
Lab 7: Finding the radius of curvature of the given curve.
# Radius of curvature for the curve y2=4ax
Output:
#Radius of curvature for the curve xy=c2
Output:
24
Lab 8: Verification of Cauchy’s mean value theorem
#Verification of Cauchy’s mean value theorem for f(x)=x2 and g(x) =x4 in [1,2]
Output
Exercise:
Verification of Cauchy’s mean value theorem for f(x)=x2 and g(x) =x3 in [1,2]
25
Lab 9 – Taylor’s and Maclaurin’s expansion of a given function
1. Write the Maclaurin’s series of (𝑥)=ex up to 5 terms.
Output:
Exercise:
1 Expand f(x)=log(1+x) about x=1 upto 4 terms.
2 Expand cos x about the origin containing x5 terms.
3 Expand ex cosx in powers of x upto 4 terms.
26
Lab 10 - Evaluation of limits by L Hospital’s rule
𝑥−𝑠𝑖𝑛𝑥
Apply L Hospital’s rule and evaluate: lim
𝑥3
𝑥→0
Output:-
Exercise :
1 lim 𝑥𝑙𝑜𝑔(𝑡𝑎𝑛𝑥)
𝑥→0
2 lim (𝑐𝑜𝑠𝑒𝑐𝑥 − 𝑐𝑜𝑡𝑥)
𝑥→0
𝜋 𝑡𝑎𝑛𝑥
3 lim
𝑛→∞ 2 tan 3𝑥
27
Lab 11 - Finding the nth derivatives without Leibnit’z rule
1. Find the nth derivative of 𝑠𝑖𝑛2𝑥, take n=2,4
Output:
Exercise
1. Find the nth derivative of 𝑐𝑜𝑠𝑥 𝑐𝑜𝑠2𝑥 𝑐𝑜𝑠3x , take n=1,2
2. Find the nth derivative e2x cos2x , take n=2,3.
𝑥−3
3. Find the nth derivative of for n=2
(𝑥−1)(𝑥+2)
28
Lab 12 - Finding the nth derivatives with Leibnit’z rule
1. Find the nth derivative of 𝑥2𝑠𝑖𝑛𝑥, take n=4
Output:
Exercise
1. Find the nth derivative of x3𝑐𝑜𝑠𝑥 take n=4
2. Find the nth derivative of 𝑥2𝑙𝑜𝑔𝑥 take n=3.
3. Find the nth derivative of 𝑥4𝑒𝑥 for n=5
29