0% found this document useful (0 votes)
68 views43 pages

Practical1-12 Sybcs

Partical

Uploaded by

ashwini biradar
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)
68 views43 pages

Practical1-12 Sybcs

Partical

Uploaded by

ashwini biradar
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/ 43

Dr. D. Y.

Patil Unitech Society’s

Dr. D. Y. Patil Arts, Commerce &


Science College
Sant Tukaram Nagar, Pimpri, Pune - 411018

Name:

Roll No:

Class:

Div:

Batch:

Subject:

Subject Code:
INDEX
Sr.
List of Practical Date Remark Signature
No.
1 Introduction to Python Data Types - I

2 Python Data Types - II

3 Control Statements in Python - I

4 Control Statements in Python - II

5 Matrices

Determinants, System of Linear


6
Equations

7 System of Equation

8 Eigenvalues and Eigenvectors - I

9 Eigenvalues and Eigenvectors - II

10 Roots of Equations

11 Numerical Integration - I

12 Numerical Integration - II
Practical – 01
Introduction to Python Data Types - I
Name of Student:
Roll No:
Batch:

Q.1) Find the data type of the following.


1. A=36
type(A)
Output: <class 'int'>

2. A=3+2j
type(A)
Output: <class 'complex'>

3. A=3.14
type(A)
Output: <class 'float'>

4. A='python'
type(A)
Output: <class 'str'>

5. A=[1,2,3,4,5]
type(A)
Output: <class 'list'>

Q.2) Print the following string using print statement.


1. A='Hello World !'
print(A)
Output: Hello World !

2. B='Mathematics'
print(B)
Output: Mathematics

Q.3) Evaluate the following expression.


1. A=10%2+7-(3+7)*10/2
print(A)
Output: -43.0

2. B=3*10//3+10%3
print(B)
Output: 11
Q.4) Compare the following expression.
1. A=21
B=15
print(A==B)
Output: False

2. A=4
B=5
print(A!=B)
Output: True

3. A=10
B=15
print(A<=B)
Output: True

4. A=4
B=8
print(A<B)
Output: True

Q.5) Print the following terms by importing math and cmath module.
1. log(10) with base 10
import math
print(math.log10(10))
Output: 1.0

2. sin(30)
import math
print(math.sin(30))
Output: -0.9880316240928618

3. Value of pi
import math
print(math.pi)
Output: 3.141592653589793

4. tan(45)
import math
print(math.tan(45))
Output: 1.6197751905438615

5. 3+5j
a = 3+5j
print(a)
Output: (3+5j)
Q.6) Accept the different value by using input() statement and print it from the following
code.
1. A=input("A:")
A:30
print(A)
Output: 30

2. Name = input("Enter the Name:")


Name: Akash
Age = int(input("Age:"))
Age: 20
City = input("City Name:")
City: Pune
Practical – 02
Python Data Types - II
Name of Student:
Roll No:
Batch:

Q.1) Define string x = "Math is Easy" & y = "To All"


1. Find length of x and y.
x = "Math is Easy"
y = "To All"
len(x)
Output: 12

len(y)
Output: 6

2. Pick character with index 3 and -1 in x and 2 & 4 in y.


x = "Math is Easy"
y = "To All"
x[3]
Output: 'h'

x[-1]
Output: 'y'

y[2]
Output: ' '

y[4]
Output: 'l'

3. Using operator + & * find 2x + y, 3 * x.


2*x + y
Output: 'Math is EasyMath is EasyTo All'

3*x
Output: 'Math is EasyMath is EasyMath is Easy'
Q.2) Write a python program of concatenation and repetition of string and list.
Ans:
1. Concatenation of string:
x = "hello "
y = "I am in sybsc(cs)"
x+y
Output: 'hello I am in sybsc(cs)'

2. Repetition of string:
x = "hello "
y = "I am in sybsc(cs)"
x*2
Output: 'hellohello'

y*2
Output: 'I am in sybsc(cs)I am in sybsc(cs)'
3. Concatenation of list:
L1 = ['mango','apple','banana',45,78,'kiwi']
L2 = ['bottle','book',66,90,'pen']
L1 + L2
Output: ['mango', 'apple', 'banana', 45, 78, 'kiwi', 'bottle', 'book', 66, 90, 'pen']

4. Repetition of list:
L1 = ['mango','apple','banana',45,78,'kiwi']
L2 = ['bottle','book',66,90,'pen']
L1 * 2
Output: ['mango', 'apple', 'banana', 45, 78, 'kiwi', 'mango', 'apple', 'banana', 45, 78,
'kiwi']

L2 * 2
Output: ['bottle', 'book', 66, 90, 'pen', 'bottle', 'book', 66, 90, 'pen']

Q.3) Write a python program to create a nested list & display its element.
Ans:
L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']
print(L)
Output: ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']

print(L[2])
Output: ['cc', 'dd', ['eee', 'fff']]
Q.4) Write a python program to create list using range function.
Ans:
My_list = [*range(10, 20, 1)]
print(My_list)
Output: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Q.5) Write a python program using membership operator ('not in' and 'is' operator) for
list.
Use: x=[1,2,3,'Rose','Pink','math','easy']
Ans:
'in' operator:
L1=[1,2,3,'Rose','Pink','math','easy']
L2=[2,3,4,'red']
if L1 in L2:
print("True")
else:
print("False")
Output: False

'not in' operator:


L1=[1,2,3,'Rose','Pink','math','easy']
L2=[2,3,4,'red']
if L1 not in L2:
print("True")
else:
print("False")
Output: True

Q.6) Define tuple with single element & verify its type.
Ans:
tuple1 = (5,)
type(tuple1)
Output: <class 'tuple'>

Q.7) Write a python program to write a function that return area and circumference of
Circle.
Ans:
from math import *
def f(r):
c = 2 * pi * r
a = pi * r * r
return c, a
print(f(5))
Output: (31.41592653589793, 78.53981633974483)
Q.8) Write a python program to find area of rectangle by passing arguments.
Ans:
def rect_area(l, b):
return l * b
print(rect_area(5, 7))
Output: 35

Q.9) Write a python program to find maximum and minimum value from list of numbers.
Ans:
L1 = [5, 8, 7, 3, 12]
print("Maximum value from list is", max(L1))
print("Minimum value from list is", min(L1))
Output:
Maximum value from list is 12
Minimum value from list is 3
Practical – 03
Control Statements in Python - I
Name of Student:
Roll No:
Batch:

Q.1) Find the range of the following function

a) range(10)
L1 = range(10)
for i in L1:
print(i, end=' ')
print()
Output: 0 1 2 3 4 5 6 7 8 9

b) range(1,5)
L1 = range(1, 5)
for i in L1:
print(i, end=' ')
print()
Output: 1 2 3 4
c) range(1, 10, 2)
L1 = range(1, 10, 2)
for i in L1:
print(i, end=' ')
print()
Output: 1 3 5 7 9

d) range(5,2)
L1 = range(5,2)
for i in L1:
print(i, end=' ')
print()
Output: 2 3 4 5

e) range(10, -10, -2)


L1 = range(10, -10, -2)
for i in L1:
print(i, end=' ')
print()
Output: 10 8 6 4 2 0 -2 -4 -6 -8
Q.2) Write a python program to find gcd of two numbers using for loop.
Ans:
import math
def phi(n):
for x in range(1, n):
if math.gcd(n, x) == 1:
print(x, end=' ')
phi(10)
Output: 1 3 7 9

Q.3) Write a python program to find table of 22 using for loop.


Ans:
def table(n):
for i in range(1, 11):
print(n * i, end=' ')
table(22)
Output: 22 44 66 88 110 132 154 176 198 220

Q.4) Write a python program to find table of 19 using while loop.


Ans:
i=1
n = int(input("Table of: "))
while i <= 10:
print(n * i)
i=i+1
Output:
Table of: 19
19
38
57
76
95
114
133
152
171
190

Q.5) Write a python program to find first 10 terms of Fibonacci sequence using for loop.
Ans:
a=0
b=1
for i in range(10):
a, b = b, a + b
print(a, end=' ')
Output: 1 1 2 3 5 8 13 21 34 55
Q.6) Write a python program to check if the student is pass, fail, or distinction.
Ans:
name = input("Name of the student: ")
marks = int(input('Marks = '))
if marks > 39:
if marks > 75:
print("First class with distinction")
elif 60 <= marks <= 75:
print("First class")
else:
print("Second class")
else:
print("Fail")
Output:
Name of the student: Nisha
Marks = 71
First class
Practical – 04
Control Statements in Python - II
Name of Student:
Roll No:
Batch:

Q.1) Write a program to find divisors of any number using input function.
Ans:
n = int(input("Divisor of: "))
for i in range(1, n + 1):
if n % i == 0:
print(i)
Output:
Divisor of: 10
1
2
5
10
Q.2) Write a python program to print the absolute value of a given number.
Ans:
n = float(input("Enter the value: "))
absolute = abs(n)
print("\nThe absolute value of the number is: ->", absolute)
Output:
Enter the value: 5.23
The absolute value of the number is: -> 5.23
Q.3) Write a python program to print the table of a given number.
Ans:
i=1
n = int(input("Table of: "))
while i <= 10:
print(n * i)
i=i+1
Output:
Table of: 5
5
10
15
20
25
30
35
40
45
50
Q.4) Define a function that prints all integers between 1 to n that are relatively prime.
Ans:
import math
def phi(n):
for x in range(1, n):
if math.gcd(n, x) == 1:
print(x, end=' ')
phi(10)
Output: 1 3 7 9
Q.5) Define Euler’s phi function in python.
Ans:
import math
def phi(n):
i=0
for i in range(1, n):
if math.gcd(n, i) == 1:
i=i+1
print(i, end=' ')
Output: phi(10)
4
Q.6) Write a program to check if a person is eligible for voting or not and if they are a
senior citizen or not.
Ans:
a = int(input("Enter your age: "))
if a >= 60:
print("You are eligible for voting and you are a senior citizen.")
elif 18 <= a < 60:
print("You are eligible for voting and you are not a senior citizen.")
else:
print("You are not eligible for voting.")
Output:
Enter your age: 5
You are not eligible for voting.
Enter your age: 55
You are eligible for voting and you are not a senior citizen.
Enter your age: 70
You are eligible for voting and you are a senior citizen.
Practical – 05
Matrices
Name of Student:
Roll No:
Batch:

Q.1) For vectors


U =[5]
[0]
&
V =[1]
[-2]

a) Find U + V
from sympy import Matrix
U = Matrix([[5], [0]])
V = Matrix([[1], [-2]])
result = U + V
print(result)
Output:
[ 6]
[-2]

b) Find 3V
from sympy import Matrix
V = Matrix([[1], [-2]])
result = 3 * V
print(result)
Output:
[ 3]
[-6]

c) Find 2U + 3V
from sympy import Matrix
U = Matrix([[5], [0]])
V = Matrix([[1], [-2]])
result = 2 * U + 3 * V
print(result)
Output:
[ 7]
[-6]
Q.2) Construct the following matrices using Python:

a) Identity matrix of order 6


from sympy import eye
identity_matrix = eye(6)
print(identity_matrix)
Output:
[1 0 0 0 0 0]
[0 1 0 0 0 0]
[0 0 1 0 0 0]
[0 0 0 1 0 0]
[0 0 0 0 1 0]
[0 0 0 0 0 1]

b) Zero matrix of order 5x6


from sympy import zeros
zero_matrix = zeros(5, 6)
print(zero_matrix)
Output:
csharp
Copy code
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]

c) Ones matrix of order 5x4


from sympy import ones
ones_matrix = ones(5, 4)
print(ones_matrix)
Output:
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]

d) Diagonal matrix with (4, -5, 1) as diagonal elements


from sympy import diag
diagonal_matrix = diag(4, -5, 1)
print(diagonal_matrix)
Output:
[ 4 0 0]
[ 0 -5 0]
[ 0 0 1]
Q.3) For the following matrices
A =[4 2 4]
[4 -1 1]
[2 4 2]
and
B =[5 2 3]
[3 -7 5]
[3 1 -1]

a) Find A + B
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = A + B
print(result)
Output:
[9 4 7]
[7 -8 6]
[5 5 1]

b) Find A - B
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = A - B
print(result)
Output:
[-1 0 1]
[ 1 6 -4]
[-1 3 3]

c) Find A^(-1)
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
inverse_A = A.inv()
print(inverse_A)
Output:
[-1/6 1/3 1/6]
[-1/6 0 1/3]
[ 1/2 -1/3 -1/3]
d) Find B * A
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = B * A
print(result)
Output:
[34 20 28]
[-6 33 15]
[14 1 11]

e) Find B^(-1) * A * B
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = B.inv() * A * B
print(result)
Output:
[ 522/59 -303/59 405/59]
[ -22/59 46/59 -72/59]
[-108/59 435/59 -273/59]

f) Find A^4
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
result = A**4
print(result)
Output:
[2060 1198 1622]
[1106 613 857]
[1456 848 1120]

Q.4) For matrix A = [-5 2 3]


[3 -7 5]
[-3 10 -11]

a) Delete 2nd column


from sympy import Matrix
A = Matrix([[-5, 2, 3], [3, -7, 5], [-3, 10, -11]])
A.col_del(1)
print(A)
Output:
[-5 3]
[ 3 5]
[-3 -11]
b) Add row [2, 3, 0] in the first row
from sympy import Matrix
A = Matrix([[-5, 2, 3], [3, -7, 5], [-3, 10, -11]])
A.row_insert(0, Matrix([[2, 3, 0]]))
print(A)
Output:
[ 2 3 0]
[-5 2 3]
[ 3 -7 5]
[-3 10 -11]

c) Delete the last row


from sympy import Matrix
A = Matrix([[2, 3, 0], [-5, 2, 3], [3, -7, 5], [-3, 10, -11]])
A.row_del(3)
print(A)
Output:
[ 2 3 0]
[-5 2 3]
[ 3 -7 5]

Q.5) Using Python, apply the following operations on


A = [1 7 1]
[4 -2 1]
[3 1 2]

a) Delete 3rd row


from sympy import Matrix
A = Matrix([[1, 7, 1], [4, -2, 1], [3, 1, 2]])
A.row_del(2)
print(A)
Output:
[1 7 1]
[4 -2 1]

b) Delete 1st column


from sympy import Matrix
A = Matrix([[1, 7, 1], [4, -2, 1]])
A.col_del(0)
print(A)
Output:
[7 1]
[-2 1]
c) Delete the last column
from sympy import Matrix
A = Matrix([[7, 1], [-2, 1]])
A.col_del(1)
print(A)
Output:
[7]
[-2]

Q.6) For matrix


A =[1 0 4]
[2 1 -1]
[3 4 2]

a) Insert [3] as the 3rd column


from sympy import Matrix
A = Matrix([[1, 0, 4], [2, 1, -1], [3, 4, 2]])
A.col_insert(2, Matrix([[3], [3], [0]]))
print(A)
Output:
[1 0 3 4]
[2 1 3 -1]
[3 4 0 2]
Practical – 06
Determinants, System of Linear Equations
Name of Student:
Roll No:
Batch:

Q.1) For matrix


D =[10 2 3]
[12 -7 15]
[-15 10 -11]

a) Find its transpose


from sympy import Matrix
D = Matrix([[10, 2, 3], [12, -7, 15], [-15, 10, -11]])
transpose_D = D.T
print(transpose_D)
Output:
[10 12 -15]
[2 -7 10]
[3 15 -11]

b) Find its determinant


from sympy import Matrix
D = Matrix([[10, 2, 3], [12, -7, 15], [-15, 10, -11]])
determinant_D = D.det()
print(determinant_D)
Output:
-871

c) Find its inverse if it exists


from sympy import Matrix
D = Matrix([[10, 2, 3], [12, -7, 15], [-15, 10, -11]])
inverse_D = D.inv()
print(inverse_D)
Output:
[ 73/871 -4/67 -51/871]
[93/871 5/67 114/871]
[-15/871 10/67 94/871]

Q.2) For matrix


B =[1 -1 -2 4]
[2 -1 -1 2]
[2 1 4 16]
a) Find reduced row echelon form
from sympy import Matrix
B = Matrix([[1, -1, -2, 4], [2, -1, -1, 2], [2, 1, 4, 16]])
rref_B, _ = B.rref()
print(rref_B)
Output:
[1 0 0 24]
[0 1 0 72]
[0 0 1 -26]

b) Find column space


from sympy import Matrix
B = Matrix([[1, -1, -2, 4], [2, -1, -1, 2], [2, 1, 4, 16]])
column_space_B = B.columnspace()
print(column_space_B)
Output: [Matrix([ [1], [2], [2]]), Matrix([ [-1], [-1], [1]]), Matrix([ [-2], [-1], [4]])]

c) Find null space


from sympy import Matrix
B = Matrix([[1, -1, -2, 4], [2, -1, -1, 2], [2, 1, 4, 16]])
null_space_B = B.nullspace()
print(null_space_B)
Output: [Matrix([[-24], [-72], [26], [1]])]

Q.3) For matrix


A =[-5 2 3]
[-3 10 -11]
[3 -7 5] Find the rank of A
from sympy import Matrix
A = Matrix([[-5, 2, 3], [3, -7, 5], [-3, 10, -11]])
rank_A = A.rank()
print(rank_A)
Output: 3

Q.4) Find the rank of matrix


M =[2 0 3]
[0 1 2]
[3 0 4]
from sympy import Matrix
M = Matrix([[2, 0, 3], [0, 1, 2], [3, 0, 4]])
rank_M = M.rank()
print(rank_M)
Output: 3
Practical – 07
Determinants, System of Linear Equations
Name of Student:
Roll No:
Batch:

Q.1) Find the solution of AX = b by Gauss elimination method where


𝟑 𝟐 −𝟏 𝟑
A = [𝟐 −𝟐 𝟒 ] B= 𝟔
𝟐 −𝟏 𝟐 𝟗

Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[3, 2, -1], [2, -2, 4], [2, -1, 2]])
B = Matrix([[3], [6], [9]])
print(linsolve((A, B), [x, y, z]))

OUTPUT:
{(6, -11, -7)}

Q.2) Find the solution of AX=b by Gauss Jordan method where


7 6 -8 3
A = 7 -2 2 B= 0
6 -1 2 9

Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[7, 6, -8], [7, -2, 2], [6, -1, 2]])
B = Matrix([[3], [0], [9]])
sol, param = A.gauss_jordan_solve(B)
OUTPUT:
Matrix([ [7/11],
[106/11],
[163/22]])

Q.3) Find the solution of system AX=b where


A = (1 2 3
456
7 8 9) and b = (1
2
3)
Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
B = Matrix([[1], [2], [3]])
print(linsolve((A, B), [x, y, z]))
OUTPUT:
{(z - 1/3, 2/3 - 2*z, z)}

Q.4) Find the solution by Gauss elimination method and Gauss Jordan method where
A = (2 1
1 2) and b = (5
7)
1) by Gauss elimination method Program:
from sympy import *
x, y = symbols("x, y")
A = Matrix([[2, 1], [1, 2]])
B = Matrix([[5], [7]])
print(linsolve((A, B), [x, y]))
OUTPUT:
{(1, 3)}

2) Gauss Jordan method Program:


from sympy import *
x, y = symbols("x, y")
A = Matrix([[2, 1], [1, 2]])
B = Matrix([[5], [7]])
sol, param = A.gauss_jordan_solve(B)
OUTPUT:
Matrix([ [1],
[3]])

Q.5) Using linsolve command find the solution of


5x - 2y + 3z = 2
x+y+z=1
x + 2y - 2z = -10
Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[5, -2, 3], [1, 1, 1], [1, 2, -2]])
B = Matrix([[2], [1], [-10]])
print(linsolve((A, B), [x, y, z]))
OUTPUT:
{(-44/23, -13/23, 80/23)}
Q.6) Find LU factorization of A = (4 5 6
7 8 9)
Program:
from sympy import *
A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
L, U, _ = A.LUdecomposition()
OUTPUT:
>>> L
Matrix([
[1, 0, 0],
[4, 1, 0],
[7, 2, 1]])
>>> U
Matrix([
[1, 2, 3],
[0, -3, -6],
[0, 0, 0]])

Q.7) Use LU factorization to solve the system


2x + 2y + z = 6
2x + y + 2z = 8
x + 2y + 2z = 7
Program:
from sympy import *
x, y, z = symbols("x, y, z")
AB = Matrix([[2, 2, 1, 6], [2, 1, 2, 8], [1, 2, 2, 7]])
print(solve_linear_system_LU(AB, [x, y, z]))
OUTPUT:
{x: 7/5, y: 2/5, z: 12/5}

Q.8) Solve the system by using Gauss elimination method, Gauss Jordan method and LU
decomposition method.
(1 2 3 (x (6
a) 2 1 4 y = 7
2 5 1) z) 8
1) By Gauss Elimination Method Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[1, 2, 3], [2, 1, 4], [2, 5, 1]])
B = Matrix([[6], [7], [8]])
print(linsolve((A, B), [x, y, z]))
OUTPUT:
{(1, 1, 1)}
2) By Gauss Jordan Method Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[1, 2, 3], [2, 1, 4], [2, 5, 1]])
B = Matrix([[6], [7], [8]])
sol, param = A.gauss_jordan_solve(B)
OUTPUT:
Matrix([ [1],
[1],
[1]])

3) By LU Decomposition Program:
from sympy import *
x, y, z = symbols("x, y, z")
AB = Matrix([[1, 2, 3, 6], [2, 1, 4, 7], [2, 5, 1, 8]])
print(solve_linear_system_LU(AB, [x, y, z]))
OUTPUT:
{x: 1, y: 1, z: 1}

Q.9) – x + 2y + 2z = -1
x + y + 2z = 2
5x + 2z = 8
1) By Gauss Elimination Method Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[-1, 2, 2], [1, 1, 2], [5, 0, 2]])
B = Matrix([[-1], [2], [8]])
print(linsolve((A, B), [x, y, z]))
OUTPUT:
{(3/2, 0, 1/4)}

2) By Gauss Jordan Method Program:


from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[-1, 2, 2], [1, 1, 2], [5, 0, 2]])
B = Matrix([[-1], [2], [8]])
sol, param = A.gauss_jordan_solve(B)
OUTPUT:
Matrix([ [3/2],
[0],
[1/4]])
3) By LU Decomposition Program:
from sympy import *
x, y, z = symbols("x, y, z")
AB = Matrix([[-1, 2, 2, -1], [1, 1, 2, 2], [5, 0, 2, 8]])
print(solve_linear_system_LU(AB, [x, y, z]))
OUTPUT:
{x: 3/2, y: 0, z: 1/4}
Practical – 08
Determinants, System of Linear Equations
Name of Student:
Roll No:
Batch:

Q.1) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (4 2 2
242
2 2 4).
Ans-
from sympy import *
A = Matrix([[4,2,2],[2,4,2],[2,2,4]])
>>> A.eigenvals()
Output:
{8: 1, 2: 2}
>>> A.eigenvects()
O/p:
[(2, 2, [Matrix([
[-1],
[ 1],
[ 0]]), Matrix([
[-1],
[ 0],
[ 1]])]), (8, 1, [Matrix([
[1],
[1],
[1]])])]

Q.2) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (1 2
2 1).
Ans-
from sympy import *
A = Matrix([[1, 2], [2, 1]])
>>>A.eigenvals()
Output:
{-1: 1, 3: 1}

>>>A.eigenvects()
Output:
[(3, 1, [Matrix([
[1],
[1]])]), (-1, 1, [Matrix([
[-1],
[1]])])]
Q.3) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (2 3 1
321
1 1 2).
Ans-
from sympy import *
A = Matrix([[2, 3, 1], [3, 2, 1], [1, 1, 2]])
>>> A.eigenvals()
Output:
{4: 1, 1: 2}
>>> A.eigenvects()
Output:
[(4, 1, [Matrix([
[1],
[1],
[1]])]), (1, 2, [Matrix([
[-1],
[1],
[0]]), Matrix([
[0],
[1],
[-1]])])]

Q.4) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (3 1 1
131
1 1 3).
Ans-
from sympy import *
A = Matrix([[3, 1, 1], [1, 3, 1], [1, 1, 3]])
>>> A.eigenvals()
Output:
{4: 1, 2: 2}
>>> A.eigenvects()
Output:
[(4, 1, [Matrix([
[1],
[1],
[1]])]), (2, 2, [Matrix([
[-1],
[1],
[0]]), Matrix([
[0],
[1],
[-1]])])
Q.5) Using Sympy module of python, find the eigen values and the corresponding
eigenvectors of the matrix A = (1 0 0
000
0 0 1).
Ans-
from sympy import *
A = Matrix([[1, 0, 0], [0, 0, 0], [0, 0, 1]])
>>> A.eigenvals()
Output:
{1: 1, 0: 2}

>>> A.eigenvects()
Output:
[(1, 1, [Matrix([
[1],
[0],
[0]])]), (0, 2, [Matrix([
[0],
[1],
[0]]), Matrix([
[0],
[0],
[1]])])]
Practical – 09
Determinants, System of Linear Equations
Name of Student:
Roll No:
Batch:

Q.1) Write a python program to diagonalizes the matrix [ 3 -2 ] and find matrix P and D.
Program:
from sympy import *
A = Matrix([[3, -2], [6, -4]])
P, D = A.diagonalize()
Output:
P = Matrix([ [1, 2],
[2, 3]])
D = Matrix([ [-1, 0],
[0, 0]])

Q.2) Using python program check whether the matrix A= [0 0 1 ]


[0 0 3]
is diagonalizable or not.
Program:
from sympy import *
A = Matrix([[0, 1, 0], [0, 0, 1], [0, 0, 3]])
Output:
A.is_diagonalizable() -> False

Q.3) Find the eigenvalues of the following matrix and hence check whether it is
diagonalizable or not.
Program:
from sympy import *
A = Matrix([[1, 2, 2], [2, 1, 2], [2, 2, 1]])
Output:
A.eigenvals() -> {5: 1, -1: 2}
A.is_diagonalizable() -> True

Q.4) Using python program check whether the matrix A= [1 1]


[1 1]
is diagonalizable or not.
Program:
from sympy import *
A = Matrix([[1, 1], [1, 1]])
Output:
A.is_diagonalizable() -> True
Q.5) Write a python program to diagonalize the matrix
[1 -1 1]
[-1 1 -1]
[1 -1 1]
and find matrix P and D.
Program:
from sympy import *
A = Matrix([[1, -1, 1], [-1, 1, -1], [1, -1, 1]])
P, D = A.diagonalize()
Output:
P = Matrix([
[1, -1, 1],
[1, 0, -1],
[0, 1, 1]])
D = Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 3]])
Practical – 10
Roots of Equations
Name of Student:
Roll No:
Batch:

Q1 Write a python program to find the correct root of the equation


𝑥3 − 4𝑥2 − 11𝑥 + 30 using Newton Raphson method correct upto 4
decimal places. Take 𝑥0 = 10.
def nr(f, g, x0, e):
step = 1
condition = True
while condition:
if g(x0) == 0.0:
print("Divided by zero error.")
break
x1 = x0 - (f(x0) / g(x0))
print('Iteration =', step, 'x1 =', x1, 'and f(x1) =', f(x1))
x0 = x1
step += 1
condition = abs(f(x1)) > e
print('Required root is :', round(x1, 4))

def f(x):
return x**3 - 4*x**2 - 11*x + 30

def g(x):
return 3*x**2 - 8*x - 11

nr(f, g, 10, 0.0001)


Output:
Iteration = 1 x1 = 7.511961722488039 and f(x1) = 145.54690711661283
Iteration = 2 x1 = 6.029708528510192 and f(x1) = 37.46810047097503
Iteration = 3 x1 = 5.2778576911468225 and f(x1) = 7.539290423642491
Iteration = 4 x1 = 5.029400988117186 and f(x1) = 0.7151577286837636
Iteration = 5 x1 = 5.000387815638643 and f(x1) = 0.009309229796429008
Iteration = 6 x1 = 5.0000000689141375 and f(x1) = 1.653939342816102e-06
Required root is : 5.0
Q2: Using python programming find the correct root of the function 𝑥3 + 2𝑥, in
[-10, 10] using Newton Raphson method correct upto 4 decimal places. Take X0=3
def newtonraphson(f, g, x0, e):
step = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divided by zero error!')
break
x1 = x0 - (f(x0) / g(x0))
print('Iteration =', step, 'x1 =', x1, 'and f(x1) =', f(x1))
x0 = x1
step += 1
condition = abs(f(x1)) > e
print('Required root is :', round(x1, 4))

def f(x):
return x**3 + 2*x

def g(x):
return 3*x**2 + 2

newtonraphson(f, g, 3, 0.0001)
Output:
Iteration = 1 x1 = 1.35 and f(x1) = 5.160375
Iteration = 2 x1 = 0.839071782178218 and f(x1) = 2.2688848832741244
Iteration = 3 x1 = 0.5165318379707146 and f(x1) = 1.1708770250983709
Iteration = 4 x1 = 0.286911684752419 and f(x1) = 0.5974414559037061
Iteration = 5 x1 = 0.12637258636310406 and f(x1) = 0.25476334679541873
Iteration = 6 x1 = 0.03400797677253169 and f(x1) = 0.06805528521500116
Iteration = 7 x1 = 0.0031305686951099317 and f(x1) = 0.006261168071234248
Iteration = 8 x1 = 2.911262335528837e-05 and f(x1) = 5.822524673525099e-05
Iteration = 9 x1 = 2.5424001309411746e-09 and f(x1) = 5.084800261882349e-09
Required root is : 0.0

Q3: Using python find the correct root of the function 𝑒𝑥 − sin 𝑥, in [0, 1] using
Newton Raphson method correct upto 3 decimal places. Take 𝑥0 = 0.4.
from math import exp, sin, cos
def newtonraphson(f, g, x0, e):
step = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divided by zero error!')
break
x1 = x0 - (f(x0) / g(x0))
print('Iteration =', step, 'x1 =', x1, 'and f(x1) =', f(x1))
x0 = x1
step += 1
condition = abs(f(x1)) > e
print('Required root is :', round(x1, 3))

def f(x):
return exp(x) - sin(x)

def g(x):
return exp(x) - cos(x)

newtonraphson(f, g, 0.4, 0.0001)


Output:
Iteration = 1 x1 = -1.5314584096802757 and f(x1) = 1.2154464636832412
Iteration = 2 x1 = -8.402567407072686 and f(x1) = 0.8534871583517203
Iteration = 3 x1 = -10.038523764622981 and f(x1) = -0.575889990450599
Iteration = 4 x1 = -9.334105736641906 and f(x1) = 0.09063639101087893
Iteration = 5 x1 = -9.425107915627608 and f(x1) = -0.0002492819574764926
Iteration = 6 x1 = -9.42485865376524 and f(x1) = 1.017479777190057e-11
Required root is : -9.425

Q4: Using python find the correct root of the function 𝑒 𝑥 , in [-1, 1] using Regula
Falsi method correct upto 4 decimal places.
def falseposition(f, x0, x1, e):
if f(x0) * f(x1) > 0.0:
print('Given guess values do not break the root. Try again with different guess
. values.')
else:
step = 1
condition = True
while condition:
x2 = ((x0 * f(x1)) - (x1 * f(x0))) / (f(x1) - f(x0))
print('Iteration =', step, 'x2 =', x2, 'and f(x2) =', f(x2))
if f(x0) * f(x2) < 0:
x1 = x2
else:
x0 = x2
step += 1
condition = abs(f(x2)) > e
print('Required root is :', round(x2, 4))

def f(x):
return exp(x)

falseposition(f, -1, 1, 0.0001)


Output:
Given guess values do not break the root. Try again with different guess values.

Q5:Write a python program to estimate a root of an equation sin 𝑥 = 8𝑥 in


[-1, 2] using Regula Falsi method correct upto 4 decimal places.
def falseposition(f, x0, x1, e):
if f(x0) * f(x1) > 0.0:
print('Given guess values do not break the root. Try again with different guess
values.')
else:
step = 1
condition = True
while condition:
x2 = ((x0 * f(x1)) - (x1 * f(x0))) / (f(x1) - f(x0))
print('Iteration =', step, 'x2 =', x2, 'and f(x2) =', f(x2))
if f(x0) * f(x2) < 0:
x1 = x2
else:
x0 = x2
step += 1
condition = abs(f(x2)) > e
print('Required root is :', round(x2, 4))

def f(x):
return sin(x) - 8 * x

falseposition(f, -1, 2, 0.0001)


Output:
Iteration = 1 x2 = -0.03477174210342762 and f(x2) = 0.24340920123557913
Iteration = 2 x2 = -0.002472371150056373 and f(x2) = 0.017306600569171028
Iteration = 3 x2 = -0.00017848920441100228 and f(x2) = 0.001249424431824746
Iteration = 4 x2 = -1.289949985729723e-05 and f(x2) = 9.029649900143835e-05
Iteration = 5 x2 = -9.323246172555106e-07 and f(x2) = 6.526272320788709e-06
Required root is : -0.0001
Q 6: Write a python program to estimate a root of an equation 𝑥6 − 𝑥4 − 𝑥3 − 1 = 0
in [1, 2] using Regula Falsi method correct upto 3 decimal places.
def falseposition(f, x0, x1, e):
if f(x0) * f(x1) > 0.0:
print('Given guess values do not break the root. Try again with different guess
values.')
else:
step = 1
condition = True
while condition:
x2 = ((x0 * f(x1)) - (x1 * f(x0))) / (f(x1) - f(x0))
print('Iteration =', step, 'x2 =', x2, 'and f(x2) =', f(x2))
if f(x0) * f(x2) < 0:
x1 = x2
else:
x0 = x2
step += 1
condition = abs(f(x2)) > e
print('Required root is :', round(x2, 3))

def f(x):
return x**6 - x**4 - x**3 - 1

falseposition(f, 1, 2, 0.001)
Output:
Iteration = 1 x2 = 1.048780487804878 and f(x2) = -2.0326812065849147
……
Iteration = 43 x2 = 1.4035942752871293 and f(x2) = -0.00012335744923852587
Iteration = 44 x2 = 1.4035961617193395 and f(x2) = -9.371235608179873e-05
Required root is : 1.404
Practical – 11
Numerical Integration - I
Name of Student:
Roll No:
Batch:
𝟏𝟎
Q1: Estimate the value of the integral ∫𝟎 (𝟏 + 𝒙)𝒅𝒙 dx using the trapezoidal rule (h=0.2)
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result

def f(x):
return 1 + x

trapezoidal(f, 0, 1, 5)
Output:
Integration by trap is: 1.5

𝟏𝟎 𝟏
Q2: Find the value of ∫𝟎 𝟏+𝒙𝟐 𝒅𝒙 using the trapezoidal rule (h=0.5)
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result

def f(x):
return 1 / (1 + x**2)

trapezoidal(f, 0, 5, 10)
Output:
Integration by trap is: 1.5707963267948966
𝟏𝟎
Q3: Solve the integral ∫𝟎 (𝟏 + 𝒙)𝟑 𝒅𝒙 dx using the trapezoidal rule (h=1)
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result

def f(x):
return (x + 1)**3

trapezoidal(f, 0, 10, 10)


Output:
Integration by trap is: 60.0

𝟏,𝟒
Q4: Solve the integral ∫𝟎,𝟐 (𝒔𝒊𝒏𝒙 − 𝒍𝒐𝒈𝒙 + 𝒆𝒙 )𝒅𝒙 using the trapezoidal rule (h=0.2)
from math import sin, log, exp
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result

def f(x):
return sin(x) - log(x) + exp(x)

trapezoidal(f, 0.2, 1.4, 6)


Output:
Integration by trap is: 2.163322392270137

𝟐.𝟓
Q5: Evaluate∫𝟎 𝒆𝒙 𝒅𝒙 using the trapezoidal rule (h=0.5)
from math import exp
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result
def f(x):
return exp(x)

trapezoidal(f, 0, 2.5, 5)
Output:
Integration by trap is: 5.625
Practical – 12
Numerical Integration - II
Name of Student:
Roll No:
Batch:
𝟏𝟎
Q1: Evaluate ∫𝟎 (𝟏 + 𝒙𝟐 )𝒅𝒙 using Simpson’s (1/3)rd rule (h=0.25)
def simpsons13(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)

for i in range(1, n):


k=a+i*h
if i % 2 == 0:
result += 2 * f(k)
else:
result += 4 * f(k)

result = (h / 3) * result
print("Simpson 1/3rd Result:", result)
return result

from math import sqrt

def f(x):
return sqrt(1 + x**2)

simpsons13(f, 0, 10, 40)


Output:
Simpson 1/3rd Result: 1.2644151038948792

𝟓
Q2: Evaluate ∫𝟎 𝒆𝒙 𝒅𝒙 using Simpson’s (1/3)rd rule (8 equal intervals)
def simpsons13(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)

for i in range(1, n):


k=a+i*h
if i % 2 == 0:
result += 2 * f(k)
else:
result += 4 * f(k)

result = (h / 3) * result
print("Simpson 1/3rd Result:", result)
return result

from math import exp

def f(x):
return exp(x)

simpsons13(f, 0, 5, 8)
Output:
Simpson 1/3rd Result: 32.684613110896976

𝟏𝟎
Q3: Evaluate ∫𝟎 √(𝟏 + 𝒙𝟐 ) 𝒅𝒙 using Simpson’s (3/8)th rule (h=0.2)
def simpsons38(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)

for i in range(1, n):


k=a+i*h
if i % 3 == 0:
result += 2 * f(k)
else:
result += 3 * f(k)

result = (3 * h / 8) * result
print("Simpson 3/8th Result:", result)
return result

from math import sqrt

def f(x):
return sqrt(1 + x**2)

simpsons38(f, 0, 10, 5)
Output:
Simpson 3/8th Result: 13.318559665215194

𝟐
Q4: Estimate ∫𝟎 (𝐱² + 𝟐𝐱 − 𝟖) 𝐝𝐱 using Simpson’s (1/3)rd rule (h=0.25)
def simpsons13(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)

for i in range(1, n):


k=a+i*h
if i % 2 == 0:
result += 2 * f(k)
else:
result += 4 * f(k)

result = (h / 3) * result
print("Simpson 1/3rd Result:", result)
return result

def f(x):
return x**2 + 2*x - 8

simpsons13(f, 0, 2, 8)
Output:
Simpson 1/3rd Result: -3.145833333333333

𝟓
Q5: Estimate ∫𝟎 𝐜𝐨𝐬(𝐱) 𝐝𝐱 using Simpson’s (3/8)th rule (h=0.5)
def simpsons38(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)

for i in range(1, n):


k=a+i*h
if i % 3 == 0:
result += 2 * f(k)
else:
result += 3 * f(k)

result = (3 * h / 8) * result
print("Simpson 3/8th Result:", result)
return result

from math import cos

def f(x):
return cos(x)

simpsons38(f, 0, 5, 10)
Output:
Simpson 3/8th Result: 0.7343268508376896

You might also like