0% found this document useful (0 votes)
9 views

Main

The document is a manual titled 'Python for Mathematicians' that covers various mathematical concepts using Python programming. It includes sections on algebra, calculus, ordinary and partial differential equations, and practical applications of matrices and numerical methods. Each section provides theoretical explanations along with Python code examples to illustrate the concepts effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Main

The document is a manual titled 'Python for Mathematicians' that covers various mathematical concepts using Python programming. It includes sections on algebra, calculus, ordinary and partial differential equations, and practical applications of matrices and numerical methods. Each section provides theoretical explanations along with Python code examples to illustrate the concepts effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 195

Python for Mathematicians: A Manual

Part I

Ms. Menaka B
Gokul S Unnikrishnan
Devika Menon
Francin Mathew

Editor: Dr. Britto Jacob S


Contents

1 Algebra I and Calculus I 5

1.1 Introduction to Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.2 Introduction to Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.3 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.4 Trace and Transpose of Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.5 Rank of a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

1.6 Inverse of a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

1.7 Solve the System of Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

1.8 Eigenvectors And Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

1.9 nth Derivative of Standard Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

1.10 Taylor’s and Maclaurin’s series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

1.11 Tracing of Standard Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

2 Algebra II and Calculus II 49

2.1 Binary Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

2.2 Cayley Table for Different Binary Operations . . . . . . . . . . . . . . . . . . . . . . . 55

2.3 Generators and Subgroups of cyclic group . . . . . . . . . . . . . . . . . . . . . . . . . 59

2.4 Coset Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

2.5 Lagrange’s Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

2.6 Euler Phi Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

2.7 Partial Derivatives of Variable Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 77

2.8 Euler’s theorem for homogeneous function . . . . . . . . . . . . . . . . . . . . . . . . 81

2.9 Maclaurin’s expansion for the function of two variables . . . . . . . . . . . . . . . . . 86

2
2.10 Line integrals with constant and variable limits . . . . . . . . . . . . . . . . . . . . . . 90

2.11 Double Integrals with constant and variable limits . . . . . . . . . . . . . . . . . . . . 94

2.12 Triple Integrals with constant and variable limits . . . . . . . . . . . . . . . . . . . . . 97

3 Ordinary Differential Equations and Real Analysis 99

3.1 Variable Separable Form - Solution of ODE . . . . . . . . . . . . . . . . . . . . . . . . 99

3.2 Homogeneous Differential Equations - Solution of ODE . . . . . . . . . . . . . . . . . 103

3.3 Linear Differential Equations - Solution of ODE . . . . . . . . . . . . . . . . . . . . . 108

3.4 Bernoulli Form - Solution of ODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

3.5 Complementary Function of the Ordinary Linear Differential Equation . . . . . . . . 118

3.6 Ordinary Linear Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . 122

3.7 Test the Convergence of the Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

3.8 Comparison Test - Test the Convergence of the Series . . . . . . . . . . . . . . . . . . 131

3.9 D’ Alembert’s Ratio Test - Test for Convergence of the Series . . . . . . . . . . . . . . 136

3.10 Raabe’s Test - Test for Convergence of the Series . . . . . . . . . . . . . . . . . . . . . 142

4 Partial Differential Equations and Integral Transforms Practical 147

4.1 Solution of Total Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . 147

4.2 Solution of Partial Differential Equations of Type 1 . . . . . . . . . . . . . . . . . . . . 151

4.3 Solution of Partial Differential Equations of Type 2 . . . . . . . . . . . . . . . . . . . . 155

4.4 Solution of Partial Differential Equations of Type 3 and Type 4 . . . . . . . . . . . . . 159

4.5 Solution of Second order linear Partial Differential Equations with constant coeffi-
cients of the Form, F(D,D’)=0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

4.6 Solution of PDE when the Equation is of the Form F(D,D’)=f(x,y) . . . . . . . . . . . 168

4.7 Finding the Laplace Transform of Standard Functions . . . . . . . . . . . . . . . . . . 173

4.8 Finding the Inverse Laplace Transform of Standard Functions . . . . . . . . . . . . . 180

4.9 Plotting of Periodic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

4.10 Solution of ODE using Lapalace Transform . . . . . . . . . . . . . . . . . . . . . . . . 187

Appendices 191

3
4
Chapter 1

Algebra I and Calculus I

Practical 1.1 Introduction to Python


In this practical, we will explore the essential elements of Python programming. First, we will
examine the syntax, variable declarations, and functions that form the backbone of Python’s func-
tionality. By grasping these fundamentals, we will be well-equipped to tackle more sophisticated
programming tasks. We will also explore loops and conditionals, learning how to apply these
powerful tools to efficiently handle common programming scenarios. Furthermore, we will study
Python’s control flow and iterative processes, gaining insight into how these mechanisms allow us
to create efficient programs.

1 Write a Python function to compare two numbers using if statement.

Code:

def compare(a, b):


if a > b:
print(f’{a} > {b}’)
elif a == b:
print(f’{a} = {b}’)
else:
print(f’{a} < {b}’)

5
Output:

2 Write a Python function to find the sum of the first ”n” natural numbers using a while loop.

Code:

def sum(n):
i, s = 0, 0
while i <= n:
s += i
i +=1
print(f’Sum of first {n} natural numbers is: {s}’)

Output:

3 Write a Python function to find the factors of a number using for loop.

Code:

def factors(n):
print(f’The factors of {n} are: ’)
for i in range(1, n + 1):
if n % i == 0:
print(i, end=’ ’)

Output:

6
4 Write a Python function to check whether the given number is prime or not.

Code:

def prime(n):
c = 1
for i in range(2, n):
if n % i == 0:
c = 0
print(f’{n} is a {"prime" if c == 1 else "composite"} number.’)

Output:

5 Write a Python function to find the factorial of a number.

Code:

def factorial(n):
p = 1
for i in range(1, n + 1):
p *= i
print(f’{n}! = {p}’)

Output:

6 Write a Python function to find the biggest of three numbers.

Code:

def big(a, b, c):

7
if (a >= b and a >= c):
print(f’{a} is bigger’)
elif (b >= a and b >= c):
print(f’{b} is bigger’)
else:
print(f’{c} is bigger’)

Output:

7 Write a Python program to illustrate the logical operators AND, OR, and NOT.

Code:

a, b = False, False
print(f’{a} and {b} is {a and b}’)
print(f’{a} or {b} is {a or b}’)
print(f’not {a} is {not a}’)
print(f’not {b} is {not b}’)

a, b = 2, 3
p = a > 1
q = b > 5
print(a > 1 and b > 1)
print(a > 1 or b > 1)
print(not p, not q)

8
Output:

9
Practical 1.2 Introduction to Matrices

In this practical, we will explore the fundamental operations of matrix addition and subtraction.
These operations play a vital role in linear algebra, forming the basis for more advanced matrix
computations. By performing matrix addition and subtraction, we will understand their key
properties, such as commutativity and associativity, and recognize their importance in solving
mathematical and real-world problems involving matrices. This practical will help build a strong
foundation for further studies in topics like systems of linear equations, transformations, and more.

1 Write a Python program to compute the addition and subtraction of the given matrices A
and B where,

   
12 7 3 5 8 1
   
A= 4 5 6  and B = 6 7 3 .
   
7 8 9 4 5 9

Manual:
   
12 7 3 5 8 1
   
Given, A = 
 4
5 6 B =  6 7 3 
 

7 8 9 4 5 9
   
12 7 3 5 8 1
   
A+B =
 4 5 6 + 6 7 3 
  

7 8 9 4 5 9
 
12 + 5 7 + 8 3 + 1
 
⇒A+B = 4 + 6 5 + 7 6 + 3 

7+4 8+5 9+9
 
17 15 4
 
⇒A+B = 10 12 9 

11 13 18
   
12 7 3 5 8 1
   
A−B =
 4 5 6 − 6 7 3 
  

7 8 9 4 5 9

10
 
12 − 5 7 − 8 3 − 1
 
⇒A−B =  4−6 5−7 6−3 

7−4 8−5 9−9
 
7 −1 2
 
=
 −2 −2 3 .

3 3 0

Code:

from sympy import *

# Define matrices A and B


A = Matrix([[12, 7, 3], [4, 5, 6], [7, 8, 9]])
B = Matrix([[5, 8, 1], [6, 7, 3], [4, 5, 9]])

# Compute the sum of matrices A and B


sum_matrix = A + B
print("Sum is: ")
pprint(sum_matrix)

# Compute the difference of matrices A and B


difference_matrix = A - B
print("Difference is: ")
pprint(difference_matrix)

Output:

11
2 Write a Python program to compute the addition and subtraction of the given matrices A
and B where,
   
2 5 7 −1 3 0
A=  and B =  .
−2 4 3 8 −1 5

Code:

from sympy import *


A = Matrix([[2, 5, 7], [-2, 4, 3]])
B = Matrix([[-1, 3, 0], [8, -1, 5]])
sum_matrix = A + B
print("Sum is: ")
pprint(sum_matrix)
difference_matrix = A - B
print("Difference is: ")
pprint(difference_matrix)

Output:

3 Write a Python program to compute the addition and subtraction of the given matrices A
and B where,
   
−1 2 −1 3
   
−3 4  and B =  5
A= 0.
 

1 0 2 −2

Code:

from sympy import *


A = Matrix([[2, -1], [-3, 4], [1, 0]])
B = Matrix([[-1, 3], [5, 0], [2, -2]])
sum_matrix = A + B

12
print("Sum is: ")
pprint(sum_matrix)
difference_matrix = A - B
print("Difference is: ")
pprint(difference_matrix)

Output:

Practical Application:

Matrix addition and subtraction have diverse applications, including analyzing financial data
in economics, enhancing images in image processing, and combining forces or displacements in
physics for structural engineering or mechanics. They also support informed decision-making in
data analysis, particularly in social sciences and business analytics.

13
Practical 1.3 Matrix Multiplication
In this practical, we will explore the concept of matrix multiplication, an essential operation in linear
algebra. Matrix multiplication plays a crucial role in various fields such as computer graphics,
machine learning, physics, and engineering, where it is used to model transformations, solve
systems of equations, and perform data analysis. We will focus on understanding how to multiply
two matrices, covering the necessary conditions for multiplication and the step-by-step process of
calculating matrix products.

1 Write a Python program to compute the multiplication of the given matrices A and B where,
   
12 7 3 5 8 1 2
   
 4 5 6 and B = 6 7 3 0.
A=   

7 8 9 4 5 9 1

Manual:
   
12 7 3 5 8 1 2
   
Given, A = 
 4 5 6  and B = 6 7 3 0
  
7 8 9 4 5 9 1

 
60 + 42 + 12 96 + 49 + 15 12 + 21 + 27 24 + 0 + 3
 
AB = 
20 + 30 + 24 32 + 35 + 30 4 + 15 + 54 8 + 0 + 6 

35 + 48 + 36 56 + 56 + 45 7 + 24 + 81 14 + 0 + 9

 
114 160 60 27
 
 74 97 73 14 .
∴ AB =  

119 157 112 23

Code:

from sympy import *

# Define matrices A and B


A = Matrix([[12, 7, 3], [4, 5, 6], [7, 8, 9]])
B = Matrix([[5, 8, 1, 2], [6, 7, 3, 0], [4, 5, 9, 1]])

14
# Compute the product of matrices A and B
product_matrix = A * B
print("Product is: ")
pprint(product_matrix)

Output:

2 Write a Python program to compute the multiplication of the given matrices A and B where,
   
1 3 3 1 2
   
 2 6 4 and B = 7 0.
A=   

−2 0 3 5 1

Manual:
   
1 3 3 1 2
   
Given, A = 
 2 6 4 and B = 7 0
  

−2 0 3 5 1

 
1 + 21 + 15 2+0+3
 
AB = 
 2 + 42 + 20 4+0+4  
−2 + 0 + 15 −4 + 0 + 3

 
37 5
 
∴ AB = 64 8 .
 
13 −1

Code:

from sympy import *


A = Matrix([[1, 3, 3], [2, 6, 4], [-2, 0, 3]])
B = Matrix([[1, 2], [7, 0], [5, 1]])
product_matrix = A * B

15
print("Product is: ")
pprint(product_matrix)

Output:

3 Write a Python program to compute the multiplication of the given matrices A and B where,
   
1 −2 3 1 0 2
   
A=
2 3  and B = 0 1 2.
−1  

−3 1 2 1 2 0

Manual:
  

1 −2 3
1 0 2
   
Given, A = 
2 3 −1  and B = 0 1 2.
 

−3 1 2 1 2 0
 
1+0+3 0−2+6 2−4+0
 
AB =   2+0−1 0+3−2 4+6+0 

−3 + 0 + 2 0 + 1 + 4 −6 + 2 + 0

 
4 4 −2
 
∴ AB = 
 1 1 10 

−1 5 −4

Code:

from sympy import *


A = Matrix([[1, -2, 3], [2, 3, -1], [-3, 1, 2]])
B = Matrix([[1, 0, 2], [0, 1, 2], [1, 2, 0]])
product_matrix = A * B
print("Product is: ")
pprint(product_matrix)

16
Output:

Practical Application:

Matrix multiplication is essential in computer graphics for transforming images or 3D models. It


also enables computations in neural networks for tasks like image recognition and natural language
processing, describes transformations in quantum mechanics, and aids in encoding and decoding
messages in cryptography.

17
Practical 1.4 Trace and Transpose of Matrices
In this practical, we will explore two fundamental operations on matrices: calculating the trace
and finding the transpose. The trace is obtained by summing the diagonal elements of a square
matrix and serves as an important tool in various mathematical fields, including linear algebra and
differential equations, for analyzing matrix properties. The transpose, obtained by interchanging
the rows and columns of a matrix, is an essential operation in simplifying computations, solving
systems of equations, and transforming data.

1 Write a Python program to compute the trace and transpose of the 2x2 matrix
 
12 7
A= .
4 5

Manual:
 
12 7
Given, A =  
4 5

Trace of A = 12 + 5
Trace of A = 17
Transpose
 of the
 matrix
12 7
A= 
4 5
 
12 4
AT =  .
7 5

Code:

from sympy import *

# Define matrix A
A = Matrix([[12, 7], [4, 5]])

# Compute and display the trace of matrix A


print("The trace is: ")

18
pprint(A.trace())

# Compute and display the transpose of matrix A


print("The transpose is: ")
pprint(A.transpose())

Output:

2 Write a Python program to compute the trace and transpose of the 3x3 matrix
 
2 7 4
 
 4 9 2 .
A= 

3 2 1

Code:

from sympy import *


A = Matrix([[2, 7, 4], [4, 9, 2], [3, 2, 1]])
print("The trace is: ")
pprint(A.trace())
print("The transpose is: ")
pprint(A.transpose())

Output:

19
3 Write a Python program to compute the transpose of the 3x4 matrix
 
2 7 4 −6
 
A=
 4 −2 9 .
2 
3 −5 2 1

Code:

from sympy import *


A = Matrix([[2, 7, 4, -6], [4, -2, 9, 2], [3, -5, 2, 1]])
print("The transpose is: ")
pprint(A.transpose()

Output:

Practical Application:

The trace and transpose of matrices have important applications, including tensor analysis for
stress-strain relations in materials, determining system stability in control systems, and efficient
data storage and manipulation in computer science, particularly in graph algorithms.

20
Practical 1.5 Rank of a Matrix
In this practical, we will into the fundamental concept of matrix rank, an important measure that
determines the number of linearly independent rows or columns in a matrix. By exploring the
rank of a matrix, students will gain valuable insights into its properties and its ability to represent
linear transformations, essential knowledge for various fields such as computer graphics, machine
learning, physics, and engineering. This practical will also cover methods for determining the rank
and discuss its significance in solving systems of equations.

1 Write a Python program to find the rank of the given matrix by reducing it to its row reduced
Echelon form
 
1 2 1
 
A= 3 4 7
 .

3 6 3

Manual:
 
1 2 1
 
Given, A = 
 3 4 7 

3 6 3
h i
R2 → R2 − 3R1 = 0 −2 4
h i
R3 → R3 − 3R1 = 0 0 0

1 h i
R2 → − R2 = 0 1 −2
2
h i
R1 → R1 − 2R2 = 1 0 5
 
1 0 5
 
RREF= 
 0 1 −2 

0 0 0

The rank of matrix A is 2.

21
Code:

from sympy import *

# Define matrix A
A = Matrix([[1, 2, 1], [3, 4, 7], [3, 6, 3]])

# Compute the reduced row-echelon form (RREF) of matrix A and extract it


echelon_form = A.rref()[0]

# Display the RREF of matrix A


print(’The RREF is: ’)
pprint(echelon_form)

# Compute and display the rank of matrix A


print(f’The rank is: {A.rank()}’)

Output:

2 Write a Python program to find the rank of the given matrix by reducing it to its row reduced
Echelon form
 
4 0 2 1
 
A= 2 1 3 4 .
 
2 3 4 7

Code:

from sympy import *


A = Matrix([[4, 0, 2, 1], [2, 1, 3, 4], [2, 3, 4, 7]])
echform = A.rref()[0]

22
print(’The RREF is: ’)
pprint(echform)
print(f’The rank is: {A.rank()}’)

Output:

3 Write a Python program to find the rank of the given matrix by reducing it to its row reduced
Echelon form
 
1 2 3 2
 
A=  2 3 5 1 

1 3 4 5

Code:

from sympy import *


A = Matrix([[1, 2, 3, 2], [2, 3, 5, 1], [1, 3, 4, 5]])
echform = A.rref()[0]
print(’The RREF is: ’)
pprint(echform)
print(f’The rank is: {A.rank()}’)

Output:

23
Practical Application:

The rank of a matrix determines the dimension of its column or row space, reflecting the indepen-
dent information it holds. It supports dimensionality reduction techniques like PCA in machine
learning, aids in solving systems of equations in engineering, and is used in econometrics and
structural analysis to assess multicollinearity and stability.

24
Practical 1.6 Inverse of a Matrix

In this practical, we will learn how to compute the inverse of a matrix, a key concept in linear
algebra. The inverse plays a vital role in solving systems of linear equations, as it enables us to find
unique solutions when they exist. Understanding the conditions for invertibility is essential, as not
all matrices possess an inverse. We will also explore various methods for calculating the inverse,
equipping us with the tools necessary to tackle a range of mathematical problems. Mastering these
concepts will deepen our understanding of matrix theory and its applications in mathematical
analysis.

1 Write a Python program to find the inverse of the given matrix using the Cayley-Hamilton
theorem
 
2 1
A= .
7 4

Manual:
 
2 1
Given, A =  
7 4

Compute the characteristic polynomial.

 
2−λ 1
det(A − λ) = det   = (2 − λ)(4 − λ) − (1 × 7) = λ2 − 6λ − 1
7 4−λ

Apply the Cayley-Hamilton theorem:

p(A) = A2 − 6A + I = 0

Multiply the characteristic equation by A−1 :

A−1 × (A2 − 6A + I) = 0

25
   
1 0 2 1
A−1 = 6I − A = 6  − 
0 1 7 4

 
4 −1
∴ A−1 =  .
−7 2

Code:

from sympy import *

# Define matrix A and the identity matrix I


A = Matrix([[2, 1], [7, 4]])
I = eye(2)

# Compute the determinant and trace of matrix A


d = A.det()
b = A.trace()

# Check if the determinant is non-zero


if d == 0:
print("Determinant is 0, inverse does not exist.")
else:

# Compute the inverse of matrix A using the formula: (b * I - A) / d}


inverse_matrix = ((b * I) - A) / d

# Display the inverse matrix


print("The inverse is: ")
pprint(inverse_matrix)

26
Output:

2 Write a Python program to find the inverse of the given matrix using the Cayley-Hamilton
Theorem.
 
2 1
A= .
4 2

Code:

from sympy import *


A= Matrix([[2, 1], [4, 2]])
I= eye(2)
d= A.det()
b= A.trace()
if d == 0:
print("Determinant is 0, inverse does not exist.")
else:
print("The inverse is: ")
pprint(((b * I) - A) / d)

Output:

Determinant is 0, inverse does not exist.

3 Write a Python program to find the inverse of the given matrix using the Cayley-Hamilton
Theorem.
 
−4 9
A= .
3 −7

Code:

from sympy import *


A = Matrix([[-4, 9], [3, -7]])

27
I = eye(2)
d = A.det()
b = A.trace()
if d == 0:
print("Determinant is 0, inverse does not exist.")
else:
print("The inverse is: ")
pprint(((b * I) - A) / d)

Output:

Practical Application:

The inverse of a matrix is essential for solving systems of linear equations. It is used in electrical
engineering for circuit analysis, in computer vision for camera calibration, in economics for solving
input-output models, and in robotics for determining joint movements in robotic arm kinematics.

28
Practical 1.7 Solve the System of Equations
In this practical, we will solve systems of linear equations using matrix operations in Python
with the SymPy library. This approach allows us to represent the system of equations in matrix
form, facilitating efficient computation. We will apply the LU decomposition method to find
the solution, which is a powerful technique for simplifying complex systems. The focus will be
on constructing matrices and utilizing the LUsolve() function to derive the unknown variables
effectively. By mastering these techniques, we will enhance our problem-solving skills and deepen
our understanding of linear algebra in a computational context.

1 Write a Python program to solve the following system of equations using the Matrix method
5x + 2y = 4.

7x + 3y = 5.

Manual:

We are given the system of equations:

5x + 2y = 4

7x + 3y = 5

Step 1: The augmented matrix corresponding to this system is:


 
5 2 | 4
 
7 3 | 5

Step 2: Perform row elementary operations to obtain row reduced Echelon form

7
We subtract times the first row from the second row:
5

7
New element at (2, 1) : 7 − ×5=7−7=0
5
7 14
New element at (2, 2) : 3 − × 2 = 3 − = 3 − 2.8 = 0.2
5 5
7 28
New element at (2, 3) : 5 − × 4 = 5 − = 5 − 5.6 = −0.6
5 5

29
The augmented matrix is now:
 
5 2 | 4
 
0 0.2 | −0.6

#AX=Y
    
5 2 x 4
   =  
0 0.2 y −0.6

The above expression lead us to the following equations:

5x + 2y = 4

0.2y = −0.6

Step 3: Solve for y and x

Solve for y using the second row:

From the second row of the matrix, we have:

0.2y = −0.6

Solving for y:
−0.6
y= = −3
0.2

Substituting y = −3 into the first row equation:

5x + 2(−3) = 4

Simplifying:

5x − 6 = 4

5x = 10

x=2

The solution to the system of equations is:

x = 2, y = −3.

30
Code:

from sympy import *


from sympy.abc import x, y

# Define matrix A, vector B, and variable matrix X


A = Matrix([[5, 2], [7, 3]])
B = Matrix([4, 5])
X = Matrix([x, y])

# Solve the system of linear equations


solution = A.LUsolve(B)

# Display the solution


print("The solution is: ")
pprint(Eq(X, solution))

Output:

2 Write a Python program to solve the following system of equations using the Matrix method
x + 2y + 3z = 0.

y + 5z = 0.

2x + 3y = 0.

Code:

from sympy import *


from sympy.abc import x, y, z
A = Matrix([[1, 2, 3], [0, 1, 5], [ 2, 3, 0]])
B = Matrix([0, 0, 0])
X = Matrix([x, y, z])
print("The solution is: ")
pprint(Eq(X, A.LUsolve(B)))

31
Output:

3 Write a Python program to solve the following system of equations using the Matrix method
1x + 2y − 1z = 3.
3x − 1y + 2z = 1.
2x − 2y + 3z = 2.

Code:

from sympy import *


from sympy.abc import x, y, z
A = Matrix([[1, 2, -1], [3, -1, 2], [2, -2, 3]])
B = Matrix([3, 1, 2])
X = Matrix([x, y, z])
print("The solution is: ")
pprint(Eq(X, A.LUsolve(B)))

Output:

Practical Application:

The matrix method is crucial for solving large systems of linear equations. It is widely applied in
fields like mathematics, engineering, computer science, and economics, supporting circuit analysis,
machine learning, and market behavior modeling.

32
Practical 1.8 Eigenvectors And Eigenvalues

In this practical, we will compute the eigenvectors and eigenvalues of a matrix using Python’s
SymPy library. Eigenvalues and eigenvectors are fundamental concepts in linear algebra, providing
insights into the properties of linear transformations and the behavior of matrices. We will begin by
defining a matrix, then calculate its eigenvalues and extract the corresponding eigenvectors. The
practical will involve iterating through the eigenvalues and displaying their respective eigenvectors
using the eigenvects() method. By mastering these concepts, we will enhance our understanding of
matrix theory and its significance in various mathematical applications.

1 Write a Python program to find the eigenvectors and eigenvalues for the following matrix:
 
2 −1
A= .
0 1

Manual:  
2 −1
Given, A =  
0 1
 
2 −1
To find the eigenvalues and eigenvectors for the matrix A =  
0 1
Step 1: we find the eigenvalues by solving the characteristic equation, which is given by:
det(A − λI) = 0

where I is the identity matrix and λ is a scalar. Compute A − λI:


     
2 −1 1 0 2 − λ −1
A − λI =   − λ = 
0 1 0 1 0 1−λ

Compute the determinant of this matrix:

det(A − λI) = (2 − λ)(1 − λ) − (−1)(0) = (2 − λ)(1 − λ)

The characteristic equation is:

(2 − λ)(1 − λ) = 0

So, the eigenvalues are:

λ1 = 2, λ2 = 1.

33
Step 2: we find the eigenvectors for each eigenvalue by solving:
(A2 − λI)v = 0

where v is the eigenvector associated with the eigenvalue λ.


For λ1 = 2:
Substituteλ = 2 into A −
λI:  
2 − 2 −1 0 −1
A − 2I =  = 
0 1−2 0 −1
Solve (A−2I)v
  = 0: 
0 −1 x 0
   =  
0 −1 y 0

This gives the equation:


0x − y = 0

So, y = 0, and x can be any value. Thus, the eigenvector corresponding to λ1 = 2 is:
 
1
v1 =  
0

For λ2 = 1:
Substituteλ = 1 into A −
λI:  
2 − 1 −1 1 −1
A − 1I =  = 
0 1−1 0 0

Solve (A − 1I)v = 0:
    
1 −1 x 0
   =  
0 0 y 0

This gives the equation:


x−y =0

So, x = y, and the eigenvector corresponding to λ2 = 1 is:


 
1
v2 =  
1

Thus,
theeigenvalues
 of A are λ1 = 2 and λ2 = 1, with corresponding eigenvectors:
1 1
v1 =   , v2 =  .
0 1

34
Code:

from sympy import *

# Define matrix A
A = Matrix([[2, -1], [0, 1]])

# Iterate over each eigenvalue, its algebraic multiplicity,


and corresponding eigenvectors

for eigenvalue, al_mult, eigenvectors in A.eigenvects():


print(f’The eigenvalue is: {eigenvalue} and the corresponding
eigenvectors are: ’)
# Iterate over each eigenvector corresponding to the current
eigenvalue
for eigenvector in eigenvectors:
pprint(eigenvector)

Output:

Practical Application:

Eigenvectors are used in physics to describe principal axes of rotation and vibration modes in
mechanical structures, in engineering for analyzing stress and strain in materials, in computer
science for dimensionality reduction in tasks like face recognition, and in finance for portfolio
optimization and machine learning for feature extraction.

35
Practical 1.9 nth Derivative of Standard Functions
In this practical, we will compute the nth derivatives of various standard functions using SymPy’s
differentiation capabilities. Differentiation is a fundamental operation in calculus that allows
us to analyze the behavior of functions and their rates of change. We will differentiate a range
of functions, including powers of x, trigonometric functions, and exponential expressions. By
exploring these derivatives, we will gain a deeper understanding of the principles of calculus and
the practical applications of differentiation in mathematical analysis.

1 Write a Python program to find the nth derivative of x10

Manual:

Let y = x10
Compute the First Derivative
dy d 10
y1 = = (x ) = 10x9
dx dx

Compute the Second Derivative


d d
y2 = (y1 ) = (10x9 ) = 90x8
dx dx

Compute the Third Derivative


d d
y3 = (y2 ) = (90x8 ) = 720x7
dx dx

Identify the Pattern for the nth Derivative yn

yn = 10 × 9 × 8 × · · · × (10 − n + 1) × x10−n

Say if n=5,
then,
y5 = 30240x5 .

Code:

from sympy.abc import x


n = 5
print("The value of n is: ",n)
y = x ** 10

36
print(y.diff(x, n))

Output:

2 Write a Python program to find the nth derivative of x5

Code:

from sympy.abc import x


n = input("Enter n: ")
y = x ** 5
print(y.diff(x, n))

Output:

The value of n is: 5


120

3 Write a Python program to find the nth derivative of (2x + 3)4

Manual:

Given (2x + 3)4


The nth derivative of y = (ax + b)m is given by:

yn = m(m − 1)(m − 2)...(m − n + 1) an (ax + b)m−n

If n = 5,
y5 = 4 · 2x 60 · 23x + 150 · 22x (2x + 3) + 45 · 2x (2x + 3)2 + (2x + 3)3 log(2)5


Code:

from sympy.abc import x


n = 5
print("The value of n is: ",n)
y = (2 ** x + 3) ** 4
print(y.diff(x, n))

37
Output:

4 Write a Python program to find the nth derivative of e2x

Manual:

Let y = eax ,
then,
yn = an eax

if n = 5,
then,
y5 = 32e2 x.

Code:

from sympy import exp


from sympy.abc import x
n = 5
print("The value of n is: ",n)
y = exp(2 * x)
print(y.diff(x, n))

Output:

5 Write a Python program to find the nth derivative of log(cos x)

Manual:

Let y = log(cos x)

d − sin x
y1 = (log(cos x)) = = − tan x
dx cos x

38
d
y2 = (− tan x) = − sec2 x
dx

.
.
.
3 sin4 (x) 5 sin2 (x)
 
+ + 2 sin(x)
cos4 (x) cos2 (x)
∴ y5 = −8 · /
cos(x)

Code:

from sympy import cos, log


from sympy.abc import x
n = 5
print("The value of n is: ",n)
y = log(cos(x))
print(y.diff(x, n))

Output:

6 Write a Python program to find the nth derivative of sin3 x

Manual:

Given, sin3 x = sin2 x · sin x

1 − cos 2x sin x · (1 − cos 2x) sin x − sin x · cos 2x


sin3 x = sin x · = =
2 2 2
1 1 1
sin3 x = · sin x − · sin 3x + sin x
2 4 4
1   π  1   π  1  π 
yn = sin n + x − · 3n sin n + 3x + sin n + x
2 2 4 2 4 2

for n = 5, y5 = 3 · (61 sin2 x − 20 cos2 x) · cos x.

39
Code:

from sympy import sin


from sympy.abc import x
n = 5
print("The value of n is: ",n)
y = sin(x) ** 3
print(y.diff(x, n))

Output:

7 Write a Python program to find the nth derivative of ex sinx

Manual:

Given, ex sinx
If y = eax · sin x

n nπ
then yn = 2 2 · ex · sin(x + )
4

for n = 5,
y5 = −4 · (sin x + cos x) · ex .

Code:

from sympy import sin, exp


from sympy.abc import x
n = 5
print("The value of n is: ",n)
y = exp(x) * sin(x)
print(y.diff(x, n))

Output:

40
Practical Application:

The nth derivative of functions is useful in various fields, including physics for motion analysis,
engineering for vibration analysis, economics for studying the rate of change of economic indicators,
and signal processing for filtering out noise.

41
Practical 1.10 Taylor’s and Maclaurin’s series
In this practical, we will calculate the Taylor and Maclaurin series expansions for the cosine function
using SymPy. Series expansions are powerful tools in mathematics that allow us to approximate
functions using polynomials, providing insights into their behavior near specific points. We will
utilize the series() function to generate the expansions around different points, specifically at
x = a and x = 0. This practical will demonstrate how to derive and display these series to a
specified order of approximation, enhancing our understanding of function approximation and the
significance of series in mathematical analysis.

1 Write a Python program to find Taylor’s and Maclaurin’s series expansion for the following
function: f(x)=cosx

Manual:

The Taylor series of f (x) = cos(x) centered at a is given by:

(x − a)2 (x − a)3
f (x) = cos(x) = cos(a) + (x − a) (− sin(a)) + cos(a) + (− sin(a)) + · · ·
2! 3!

x2
where a is the center point. The Maclaurin series of f (x) = cos(x) is: cos(x) = 1 − .
2
Code:

from sympy import *


from sympy.abc import x, a
print(series(cos(x), x, a, 4))
print(series(cos(x), x, 0, 4))

Output:

42
Practical Application:

Taylor’s and Maclaurin’s series are used to approximate solutions to differential equations in
physics, render 3D models in computer graphics, optimize mechanical systems in engineering, and
model stock prices and option pricing in finance.

43
Practical 1.11 Tracing of Standard Curves
This practical will explore the generation and plotting of standard curves in polar coordinates using
the NumPy and Matplotlib libraries. The primary goal is to create a range of angles using NumPy
and utilize Matplotlib to generate accurate polar plots of the curves. The focus is on configuring
and displaying polar plots to accurately represent various standard curves. Through this practical,
we will gain insights into the complexities of plotting in polar coordinates and strengthen their
data visualization skills for future applications.

1 Write a Python program to trace a cardioid.

Code:

from numpy import linspace, cos, pi


from matplotlib.pyplot import polar
theta = linspace(0, 2 * pi, 1000)
polar(theta, 5 * (1 + cos(theta)), ’r’)

Output:

2 Write a Python program to trace a cardioid and its reflection intersecting each other.

Code:

from numpy import linspace, cos, pi


from matplotlib.pyplot import polar
theta = linspace(0, 2 * pi, 1000)
polar(theta, 5 * (1 + cos(theta)), ’r’)

44
polar(theta, 5 * (1 - cos(theta)), ’g’)

Output:

3 Write a Python program to trace a three-leaved rose.

Code:

from numpy import linspace, cos, pi


from matplotlib.pyplot import polar
theta = linspace(0, 2 * pi, 1000)
polar(theta, 5 * cos(3 * theta), ’r’)

Output:

4 Write a Python program to trace a astroid.

Code:

from sympy.plotting import plot_implicit


from sympy.abc import x, y
plot_implicit(x ** (2 / 3) + y ** (2 / 3) - 4 ** (2 / 3))

45
Output:

5 Write a Python program to trace a four-leaved rose.

Code:

from numpy import linspace, cos, pi


from matplotlib.pyplot import polar
theta = linspace(0, 2 * pi, 1000)
polar(theta, 3 * cos(4 * theta), ’r’)

Output:

6 Write a Python program to trace a cissoid.

Code:

from sympy.plotting import plot_implicit


from sympy.abc import x, y
plot_implicit(y ** 2 * (2 - x) + -x ** 3)

46
Output:

Practical Application:

Tracing standard curves is important in engineering for analyzing stress-strain relationships, in


physics for studying particle trajectories, in economics for understanding market equilibrium, and
in medicine for tracking patient growth and predicting health trends.

47
48
Chapter 2

Algebra II and Calculus II

Practical 2.1 Binary Operation

In this practical, we will explore binary operations within the context of algebraic groups. Binary
operations, such as addition and multiplication, play a crucial role in defining group structures,
which are fundamental in abstract algebra. We will examine how these operations can establish
group properties, including closure, associativity, identity, and the existence of inverses. By
verifying these properties, we will gain a deeper understanding of the structure and behavior of
algebraic groups, highlighting their importance in various areas of mathematics. This practical will
enhance our ability to analyze and work with algebraic systems systematically.

1 Write a Python program to verify commutativity and associativity of the set Z = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
with the binary operation: a ∗ b = 1 + ab , ∀ a, b ∈ Z.

Manual:

Given, Z = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

a ∗ b = 1 + (a × b)

Commutative law:

Let a, b ∈ Z, a ∗ b = b ∗ a

49
1 + (a × b) = 1 + (b × a)

The binary operation * in Z is commutative.

Associative Law

Let a, b, c ∈ Z, (a ∗ b) ∗ c = a ∗ (b ∗ c)

(1 + ab) ∗ c = a ∗ (1 + bc)

abc + 1 + c ̸= abc + 1 + a

∴ The binary operation * in Z is not Associative.

Code:

import sys as s
z={0,1,2,3,4,5,6,7,8,9,10}
def f(a,b):
return(1+(a*b))

flag=1
for a in z:
for b in z:
if f(a,b)!=f(b,a):
flag=0

if flag!=0:
print("The binary operation * in z is commutative")
else:
print("The binary operation * in z is not commutative")
s.exit()

flag1=1
for a in z:
for b in z:
for c in z:
if f(a,f(b,c))!=f(f(a,b),c):
flag1=0

50
if flag1!=0:
print("The binary operation * in z is associative")
else:
print("The binary operation * in z is not associative")
s.exit()

Output:

2 Write a Python program to show that the identity element is 1 and inverse of 2 is 0 of the set
Z = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} with the binary operation: a ∗ b = a + b − 1 ∀ a, b ∈ Z.

Manual:

Given, Z = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

a∗b=a+b−1

i)Identity element

Let a ∈ Z, ∃ e ∈ Z such that a ∗ e = a = e ∗ a

a∗e=a=e∗a

a+e−1=a=e+a−1

e=1

∴ Identity element is 1

ii)Existence of Inverse

Let a ∈ Z, ∃ b ∈ Z such that a ∗ b = e = b ∗ a

2∗b=1⇒2+b−1=1

b=0

∴ Inverse of 2 is 0.

51
Code:

import sys as s
z={0,1,2,3,4,5,6,7,8,9,10}
def f(a,b):
return(a+b-1)

e=90 # A dummy value outside the given set


for a in z:
for b in z:
if f(a,b)==b and f(b,a)==b:
e=a

if e in z:
print("Identity element=",e)
else:
print("Identity does not exist")

for a in z:
for b in z:
if f(a,b)==1 and f(b,a)==1:
if b==2:
print("Inverse of ",b," is ",a)

Output:

3 Write a Python program to find the inverse of all the square roots of unity under usual
multiplication.

Code:

import sys as s
z={1,-1}

52
def f(a,b):
return(a*b)

e=5
for a in z:
for b in z:
if f(a,b)==b and f(b,a)==b:
e=a

if e in z:
print("Identity element=",e)
else:
print("Identity does not exist")

for a in z:
for b in z:
if f(a,b)==1 and f(b,a)==1:
print("Inverse of ",b," is ",a)

Output:

4 Write a Python program to find the inverse of all the elements of the set Z={0,1,2,3,4,5 }
under addition modulo 6.

Code:

import sys as s
z={0,1,2,3,4,5}
def f(a,b):
return((a+b)%6)

e=20

53
for a in z:
for b in z:
if f(a,b)==b and f(b,a)==b:
e=a

if e in z:
print("Identity element=",e)
else:
print("Identity does not exist")

for a in z:
for b in z:
if f(a,b)==0 and f(b,a)==0:
print(b," is the inverse of ",a)

Output:

Practical Application:

In digital signal processing, binary operations help manipulate binary data for tasks like filtering
and compression. In mathematics, binary operations are foundational in algebraic structures
such as groups, rings, and fields, used to model symmetry and transformations in physics and
engineering.

54
Practical 2.2 Cayley Table for Different Binary Operations
In this practical, we will create a Cayley table to represent the binary operation on a given group.
The Cayley table is a valuable tool in group theory, providing a visual representation of how each
pair of group elements interacts under the specified operation. We will systematically compute
the results of the operation for every combination of group elements and organize these results in
a table format. This exercise will not only reinforce our understanding of binary operations and
group structures but also illustrate the properties of the group in a clear and concise manner.

1 Write a Python program to construct the Cayley Table for the set G = {0, 1, 2, 3, 4, 5} with
respect to addition modulo 6.

Manual:

Given, G = {0, 1, 2, 3, 4, 5}.


The Cayley Table for G = {0, 1, 2, 3, 4, 5} with respect to addition modulo 6 is:

+6 0 1 2 3 4 5
0 0 1 2 3 4 5
1 1 2 3 4 5 0
2 2 3 4 5 0 1
3 3 4 5 0 1 2
4 4 5 0 1 2 3
5 5 0 1 2 3 4

Code:

from sympy import *


G = [0, 1, 2, 3, 4, 5]
n = len(G)
c = zeros(n, n)
def f(a, b):
return (a + b) % n
for i in range(0, n):
for j in range(0, n):
c[i, j] = f(G[i], G[j])

55
print("Cayley’s Table is")
pprint(c)

Output:

2 Write a Python program to construct Cayley Table for the set G={ 1, 3, 7, 9 } with respect to
multiplication modulo 10.

Code:

from sympy import *


G = [1,3,7,9]
n = len(G)
c = zeros(n, n)

def f(a, b):


return (a * b) % 10

for i in range(0, n):


for j in range(0, n):
c[i, j] = f(G[i], G[j])

print("Cayley’s Table is")


pprint(c)

56
Output:

3 Write a Python program to construct Cayley Table for multiplication modulo 4, (Z4 , ×4 ), Z4 =
G = {1, 2, 3} .

Code:

from sympy import *


G = [1,2,3]
n = len(G)
c = zeros(n, n)
def f(a, b):
return (a * b) % 4
for i in range(0, n):
for j in range(0, n):
c[i, j] = f(G[i], G[j])
print("Cayley’s Table is")
pprint(c)

Output:

57
4 Write a Python program to construct Cayley Table for the set (Z - { 3 } ) with respect to
addition modulo 3.

Code:

from sympy import *


G = [1,2]
n = len(G)
c = zeros(n, n)

def f(a, b):


return (a + b) % 3

for i in range(0, n):


for j in range(0, n):
c[i, j] = f(G[i], G[j])

print("Cayley’s Table is")


pprint(c)

Output:

Practical Application:

In cryptography, Cayley tables help understand the structure of encryption keys and operations. In
computer science, they are applied in error-correcting codes and algorithms to identify patterns
and symmetries, and in quantum mechanics, they analyze the symmetries of physical systems.

58
Practical 2.3 Generators and Subgroups of cyclic group
In this practical, we will identify generators and subgroups of a cyclic group defined by specific
elements. Generators are fundamental in understanding the structure of cyclic groups, as they can
produce all elements of the group through repeated application of the group operation. Using
Python, we will determine which elements of the group serve as generators and compute the
subgroups generated by each element. This exploration will deepen our understanding of cyclic
groups and their properties, highlighting the relationships between generators and the subgroups
they create.

1 Write a Python program to find the generators, number of generators and subgroups gener-
ated by the elements of a cyclic group G, where G= { 1, -1, i, -i}.

Manual:

Given, G = {1, −1, i, −i}

O(G) = 4

We know that 1 is the identity element

i1 = i

i2 = −1

i3 = −i

i4 = 1

∴ i is a generator of G

(−i)1 = −i

(−i)2 = −1

(−i)3 = i

(−i)4 = 1

∴ −i is a generator of G

∴ The number of generators is 2

59
Subgroup generated by 1 is {1}.

Subgroup generated by -i is {−i, −1, i, 1}.

Subgroup generated by i is {i, −1, −i, 1}.

Subgroup generated by -1 is {1, −1}.

Code:

G={1,-1,1j,-1j}
s=set()
for a in G:
for i in range(1,len(G)+1):
if a**i==1:
if i==len(G):
print(a," is a generator")
s.update([a])
break
if len(s)==0:
print("G has no generator")
else:
print("Number of generators= ",len(s))

for a in G:
s=set()
for i in range(1,len(G)+1):
s.update([a**i])
print("Subgroup generated by ",a," is ",s)

Output:

60
2 Write a Python program to find the generators, number of generators and subgroups gener-
ated by the elements of a cyclic group G, where G= { 0,1,2,3,4 } with respect to addition modulo
5.

Code:

G={0,1,2,3,4}
s=set()
for a in G:
for i in range(1,len(G)+1):
if (a*i)%5==0:
if i==len(G):
print(a," is a generator")
s.update([a])
break
if len(s)==0:
print("G has no generator")
else:
print("Number of generators= ",len(s))
for a in G:
s=set()
for i in range(1,len(G)+1):
s.update([(a*i)%5])
print("Subgroup generated by ",a," is ",s)

Output:

61
3 Write a Python program to find the generators, number of generators and subgroups gen-
erated by the elements of a cyclic group G, where G= { 2,4,6,8 } with respect to multiplication
modulo 10.

Code:

G={2,4,6,8}
s=set()
for a in G:
for i in range(1,len(G)+1):
if (a**i) % 10==6:
if i==len(G):
print(a," is a generator")
s.update([a])
break
if len(s)==0:
print("G has no generator")
else:
print("Number of generators= ",len(s))
for a in G:
s=set()
for i in range(1,len(G)+1):
s.update([(a**i) % 10])
print("Subgroup generated by ",a," is ",s)

Output:

62
Practical Application:

Generators and subgroups of cyclic groups are used in cryptography for secure communication
in algorithms like RSA and Diffie-Hellman. In chemistry, they help analyze the symmetry of
molecules and crystals, and in physics, they describe rotational symmetries in atomic structures
and spin systems.

63
Practical 2.4 Coset Decomposition
In this practical, we will perform a coset decomposition for a given group G with a subgroup
H. Cosets are essential in group theory, as they help us understand the relationship between a
group and its subgroups. We will calculate and print the number of distinct cosets formed by the
subgroup Hwithin the group G. By applying the elements of the subgroup to each element of G,
we will list each distinct coset. The focus of this practical is on computing the distinct cosets and
their representation, which will enhance our understanding of the structure of groups and the role
of subgroups within them.

1 Write a Python program to find distinct cosets of a subgroup H={ 1,-1 } of a group
G= { 1, -1, i, -i } with respect to multiplication.

Manual:

Given, G= { 1, -1, i, -i } H={ 1,-1 }.


Here O(G)=4, O(H)=2

We have O(G)/O(H) = number of distinct right(left) cosets of H in G.


4
Hence number of right cosets are =2
2
They are H.1 = {−1, 1} and H.i = {−i, i}.

Code:

G={1,-1,-1j,1j}
n=len(G)
H={1,-1}
m=len(H)
cs=[{1,-1}]
print("Number of distinct cosets are ",n/m)
print("Distinct cosets are ")
for a in G:
s=set()
for b in H:
s.update([a*b])

64
if len(s)==m:
if s not in cs:
cs.append(s)
print(cs)

Output:

2 Write a Python program to find distinct cosets of a subgroup H={ 0, 2, 4 } of a group


G={0,1,2,3,4,5} with respect to addition modulo 6.

Code:

G={0,1,2,3,4,5}
n=len(G)
H={0,2,4}
m=len(H)
cs=[{0,2,4}]
print("Number of distinct cosets are ",n/m)
print("Distinct cosets are ")
for a in G:
s=set()
for b in H:
s.update([(a+b)%6])
if len(s)==m:
if s not in cs:
cs.append(s)
print(cs)

Output:

65
3 Write a Python program to find distinct cosets of a subgroup H={ 1 ,3 ,9 } of a group G={ 1,
2, 3, 4, 5, 6, 7, 8, 9, 10 , 11, 12 } with respect to multiplication modulo 13.

Code:

G={1,2,3,4,5,6,7,8,9,10,11,12}
n=len(G)
H={1,3,9}
m=len(H)
cs=[{1,3,9}]
print("Number of distinct cosets are ",n/m)
print("Distinct cosets are ")
for a in G:
s=set()
for b in H:
s.update([(a*b)%13])
if len(s)==m:
if s not in cs:
cs.append(s)
print(cs)

Output:

4 Write a Python program to find distinct cosets of a subgroup H=1,2,3,4 of a group G={1,2,3,4}
with respect to multiplication modulo 5.

Code:

G={1,2,3,4}
n=len(G)
H={1,2,3,4}

66
m=len(H)
cs=[{1,2,3,4}]
print("Number of distinct cosets are ",n/m)
print("Distinct cosets are ")
for a in G:
s=set()
for b in H:
s.update([(a*b)%5])
if len(s)==m:
if s not in cs:
cs.append(s)
print(cs)

Output:

Practical Application:

Coset decomposition is applied in group theory to study symmetry and structure in mathematical
systems. In physics, it is used to model particle systems and quantum states by breaking down
complex symmetries. In computer science, it helps in error correction and optimization algorithms.

67
Practical 2.5 Lagrange’s Theorem
In this practical, we will verify Lagrange’s theorem for a given group G and its subgroup H.
Lagrange’s theorem is a fundamental result in group theory that states the order of a subgroup
divides the order of the group. We will first check whether H satisfies the properties of a valid
subgroup of G. Then, we will compare the orders of G and H to confirm the theorem’s validity.
This practical not only reinforces our understanding of subgroup properties but also highlights the
significance of Lagrange’s theorem in the study of group structures and their relationships.

1 Write a Python program to verify Lagrange’s theorem for the set H = {1, −1} in the multi-
plicative group G = {1, −1, i, −i}.

Manual:

Given, G={1, -1, i, -i} and H={1, -1}

Hence O(H) = 2, O(G) = 4

Hence O(H) divides O(G)

Hence Lagrange’s theorem is satisfied

To prove that H = {1, −1} is a subgroup, we will construct the Cayley table.

× 1 -1
1 1 -1
-1 -1 1

i) From table closure property is satisfied

ii) Associative property is true in multiplication.

iii)Existence of identity: 1 is the identity element.

iv) Existence of inverse: inverse of 1 is 1, inverse of -1 is -1

Thus H is a group by itself

Hence H is a subgroup of G.

68
Code:

G = {1,-1, 1j,-1j}
H= {1,-1}
s=set()
for a in H:
for b in H:
if (a*b)==1:
s.update([b])
print(s)
sg=1 #sg is initialized to 1 to assume H is a subgroup until proven otherwise
if H==s:
for a in H:
for b in H:
if a*b not in H:
sg=0

if sg==1:
if len(G)%len(H) == 0:
print("Lagrange’s theorem is satisfied.")
else:
print("Lagrange’s theorem is not satisfied.")
else:
print("H is not a subgroup of G")
else:
print("H is not a subgroup of G")

Output:

69
2 Write a Python program to verify Lagrange’s theorem for the set H = {i, −i} in the multi-
plicative group G = {1, −1, 1j, −1j}.

Code:

G = {1,-1, 1j,-1j}
H= {1j,-1j}
s=set()
for a in H:
for b in H:
if (a*b)==1:
s.update([b])
print(s)
sg=1
if H==s:
for a in H:
for b in H:
if a*b not in H:
sg=0

if sg==1:
if len(G)%len(H) == 0:
print("Lagrange’s theorem is satisfied.")
else:
print("Lagrange’s theorem is not satisfied.")
else:
print("H is not a subgroup of G")
else:
print("H is not a subgroup of G")

Output:

70
3 Write a Python program to verify Lagrange’s theorem for the set H = {0, 2, 4} in the multi-
plicative group G = {0, 1, 2, 3, 4, 5}.

Code:

G = {0,1,2,3,4,5}
H= {0,2,4}
s=set()
for a in H:
for b in H:
if (a+b)%6==0:
s.update([b])
print(s)
sg=1
if H==s:
for a in H:
for b in H:
if a+b%6 not in H:
sg=0

if sg==1:
if len(G)%len(H) == 0:
print("Lagrange’s theorem is satisfied.")
else:
print("Lagrange’s theorem is not satisfied.")
else:
print("H is not a subgroup of G")
else:
print("H is not a subgroup of G")

Output:

71
4 Write a Python program to verify Lagrange’s theorem for the set H = {4, 6} in the group
G = {2, 4, 6, 8} under multiplication modulo 10.

Code:

G = {2,4,6,8}
H= {4,6}
s=set()
for a in H:
for b in H:
if (a*b)%10==6:
s.update([b])
print(s)
sg=1
if H==s:
for a in H:
for b in H:
if (a*b)%10 not in H:
sg=0

if sg==1:
if len(G)%len(H) == 0:
print("Lagrange’s theorem is satisfied.")
else:
print("Lagrange’s theorem is not satisfied.")
else:
print("H is not a subgroup of G")
else:
print("H is not a subgroup of G")

Output:

72
Practical Application:

Lagrange’s Theorem determines the possible orders of subgroups in a finite group, with applications
in cryptography for understanding cryptographic system security. It also helps in classifying
symmetries in physics and plays a role in analyzing the motion and constraints of robotic arms.

73
Practical 2.6 Euler Phi Function
In this practical, we will compute Euler’s phi function for a given group G. Euler’s phi function,
denoted as ϕ(n), is a crucial concept in number theory that counts the number of integers less than
n that are relatively prime to n. We will begin by determining the order of the group G and then
apply the formula for Euler’s phi function based on the group’s order. The focus of this practical is
on effectively using the formula to find ϕ(n) and understanding its significance in relation to the
structure of the group. Through this practical, we will gain insights into the relationship between
group theory and number theory, enhancing our overall comprehension of these mathematical
concepts.

1 Write a Python program to find Euler Phi function of the group G = {1, −1, −i, i}.

Manual:

Given, n = 4, P = 22
     
1 1 1
ϕ(n) = n. 1 − . 1− .... 1 −
P1 P2 Pn
 
1
ϕ(4) = 4 1 − =2
2
∴ ϕ(4) = 2.

Code:

from sympy import*


G={1,-1,1j,-1j}
n=len(G)
print("Order of G = ",n)
phifn=n
for i in range(2,n+1):
if isprime(i) and n%i==0:
phifn = phifn * (1-1/i)
print("Euler phi function ",n," is ",phifn)

74
Output:

2 Write a Python program to find Euler Phi function of the group G = {0, 1, 2, 3, 4}.

Code:

from sympy import*


G={0,1,2,3,4}
n=len(G)
print("Order of G = ",n)
phifn=n
for i in range(2,n+1):
if isprime(i) and n%i==0:
phifn = phifn * (1-1/i)
print("Euler phi function ",n," is ",phifn)

Output:

3 Write a Python program to find Euler Phi function of the group G = {2, 4, 6, 8}.

Code:

from sympy import*


G={2,4,6,8}
n=len(G)
print("Order of G = ",n)
phifn=n
for i in range(2,n+1):
if isprime(i) and n%i==0:
phifn = phifn * (1-1/i)
print("Euler phi function ",n," is ",phifn)

75
Output:

4 Write a Python program to find Euler Phi function of the group G = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}.

Code:

from sympy import*


G={1,2,3,4,5,6,7,8,9,10,11,12}
n=len(G)
print("Order of G = ",n)
phifn=n
for i in range(2,n+1):
if isprime(i) and n%i==0:
phifn = phifn * (1-1/i)
print("Euler phi function ",n," is ",phifn)

Output:

Practical Application:

The Euler Phi function is used in number theory and cryptography, particularly in algorithms
like RSA, to determine the number of integers that are coprime to a given number. It also helps
in optimization algorithms and appears in the study of periodic functions and cyclic behavior in
systems like electrical circuits.

76
Practical 2.7 Partial Derivatives of Variable Functions
In this practical, we will compute the partial derivatives of a function involving two variables.
Partial derivatives are essential in multivariable calculus, as they allow us to analyze how a function
changes with respect to one variable while keeping the other variable constant. We will calculate
both first-order partial derivatives and mixed partial derivatives, which involve differentiating
with respect to one variable and then the other. This practical will deepen our understanding
of differentiation in multiple dimensions and highlight the importance of partial derivatives in
analyzing complex functions.

∂2u ∂2u
1 Write a Python program to verify = for the function u = xy + y x .
∂x∂y ∂y∂x

Manual:

Given, u = xy + y x
∂u
= y · xy−1 + y x · logy
∂x
∂u
= xy logx + x.y x−1
∂y
∂2u xy
LHS = = + logx(y.xy−1 ) + y x−1 + (xy x−1 logy)
∂x∂y x
∂2u xy
RHS = = (y.xy−1 )logx + + y x−1 + (xy x−1 logy)
∂y∂x x
∴ LHS=RHS.
Hence proved.

Code:

from sympy import *


x,y = symbols(’x y’)
u= x**y+y**x
ux = diff(u,x)
uy = diff(u,y)
uxy = diff(ux,y)
uyx = diff(uy,x)
print("LHS =", uxy)

77
print("RHS =", uyx)
if(uxy==uyx):
print("=> LHS=RHS . Hence proved")

Output:

∂f ∂f ∂2f ∂2f
2 Write a Python program to find & and hence verify = for the function
  ∂x ∂y ∂x∂y ∂y∂x
x
f (x, y) = tan−1 .
y

Code:

from sympy import *


x,y = symbols(’x y’)
u= atan(x/y)
ux = diff(u,x)
uy = diff(u,y)
print("ux = ",ux)
print("uy = ",uy)
uxy = diff(ux,y)
uyx = diff(uy,x)
print("LHS =", uxy)
print("RHS =", uyx)
if(uxy==uyx):
print("=> LHS=RHS . Hence proved")

Output:

78
x
!

3 Write a Python program to find ux , uy , uxx , uyy , uxy , uyx for the function f (x, y) = y.e y .

Code:

from sympy import *


x,y = symbols(’x y’)
u= y*exp(-(x/y))
ux = diff(u,x)
uy = diff(u,y)
uxx = diff(u,x,2)
uyy = diff(u,y,2)
uxy = diff(ux,y)
uyx = diff(uy,x)
print("ux = ",ux)
print("uy = ",uy)
print("uxx = ",uxx)
print("uyy = ",uyy)
print("uxy = ",uxy)
print("uyx = ",uyx)

Output:

∂2u ∂2u ∂2u


 
4 Write a Python program to show that (x2 +y 2 +z 2 ). + 2 + 2 = 1 for the function u =
p ∂x2 ∂y ∂z
log x2 + y 2 + z 2 .

Code:

from sympy import *


x,y,z = symbols(’x y z’)
u= ln((x**2+y**2+z**2)**(1/2))
uxx = diff(u,x,2)

79
uyy = diff(u,y,2)
uzz = diff(u,z,2)
LHS = simplify((x**2+y**2+z**2)*(uxx+uyy+uzz))
RHS=1
if LHS==RHS:
print(’(x**2+y**2+z**2)*(uxx+uyy+uzz)=1, Hence proved’)
else:
print("LHS not equal to RHS")

Output:

Practical Application:

Partial derivatives are widely used in physics, particularly in fluid dynamics, thermodynamics, and
electromagnetism, to describe how a system changes with respect to one variable. In economics,
they help understand marginal changes in supply, demand, and production. In machine learning,
they are used in optimization algorithms like gradient descent.

80
Practical 2.8 Euler’s theorem for homogeneous function
In this practical, we will apply Euler’s theorem for homogeneous functions using SymPy. Homoge-
neous functions play a significant role in various fields of mathematics and physics, as they exhibit
a specific scaling behavior. We will begin by defining a homogeneous function and computing
its degree of homogeneity. Next, we will calculate the partial derivatives of the function. Finally,
we will verify Euler’s theorem by comparing the left-hand side and right-hand side expressions
derived from the function and its derivatives. This practical will enhance our understanding of
homogeneous functions and their properties, while also illustrating the application of Euler’s
theorem in a computational context.

x2 + y 2
1 Write a Python program to verify Euler’s theorem of the homogeneous function .
x+y

Manual:
x2 + y 2
Given, u =
x+y
To prove that u is a homogenous function
(tx)2 + (ty)2
u(tx, ty) = = tu
tx + ty
u is a homogeneous function of degree 1
∂u ∂u
By Euler’s theorem we have: x. +y =u
∂x ∂y
(x + y) · 2x − (x2 + y 2 ) · 1 (x + y) · 2x − (x2 + y 2 ) · 1 y(2xy + y 2 − x2 )
=x + y +
(x + y)2 (x + y)2 (x + y)2
x3 + y 3 + x2 y + xy 2 (x + y)(x2 + y 2 ) x2 + y 2
= = =u
(x + y)2 (x + y)2 x+y
Hence verified.

Code:

from sympy import *


from sympy.abc import x, y

# Define the function u


u = (x**2 + y**2) / (x + y)
n = homogeneous_order(u, x, y) # Determine the degree of homogeneity of u

81
ux, uy = diff(u, x), diff(u, y) # Calculate the partial derivatives of u with
respect to x and y

# Compute the left-hand side (LHS) of Euler’s theorem


lhs = simplify(x*ux + y*uy)
print("LHS is: ")
print(lhs)
print()

# Compute the right-hand side (RHS) of Euler’s theorem


rhs = n * u
print("RHS is: ")
print(rhs)
print()

# Verify if Euler’s theorem is satisfied


print(f"Thus, Euler’s theorem is{’’ if lhs == rhs else ’ not’} satisfied.")

Output:

x3 + y 3
2 Write a Python program to verify Euler’s theorem for the homogeneous function .
x+y

Code:

from sympy import *


from sympy.abc import x, y

# Define the function u


u = (x**3 + y**3) / (x + y)
n = homogeneous_order(u, x, y) # Determine the degree of homogeneity of u

82
ux, uy = diff(u, x), diff(u, y) # Calculate the partial derivatives of u with
respect to x and y

# Compute the left-hand side (LHS) of Euler’s theorem


lhs = simplify(x*ux + y*uy)
print("LHS is: ")
print(lhs)
print()

# Compute the right-hand side (RHS) of Euler’s theorem


rhs = n * u
print("RHS is: ")
print(rhs)
print()

# Verify if Euler’s theorem is satisfied


print(f"Thus, Euler’s theorem is{’’ if lhs == rhs else ’ not’} satisfied.")

Output:

 
x
3 Write a Python program to verify Euler’s theorem for the homogeneous function xy sin .
y

Code:

from sympy import *


from sympy.abc import x, y
u = x*y*sin(x / y)
n = homogeneous_order(u, x, y)

83
ux, uy = diff(u, x), diff(u, y)
lhs = simplify(x*ux + y*uy)
print("LHS is: ")
print(lhs)
print()
rhs = n*u
print("RHS is: ")
print(rhs)
print()
print(f"Thus, Euler’s theorem is{’’ if lhs == rhs else ’ not’} satisfied.")

Output:

x
4 Write a Python program to verify Euler’s theorem for the homogeneous function .
x−y

Code:

from sympy import *


from sympy.abc import x, y
u = (x) / (x - y)
n = homogeneous_order(u, x, y)
ux, uy = diff(u, x), diff(u, y)
lhs = simplify(x*ux + y*uy)
print("LHS is: ")
print(lhs)
print()
rhs = n*u
print("RHS is: ")
print(rhs)

84
print()
print(f"Thus, Euler’s theorem is{’’ if lhs == rhs else ’ not’} satisfied.")

Output:

Practical Application:

Euler’s Theorem for homogeneous functions has applications in thermodynamics to describe


relationships between state variables in systems like gases and fluids. In economics, it helps model
production functions and economies of scale, and in physics, it is used for scaling transformations
in fluid dynamics.

85
Practical 2.9 Maclaurin’s expansion for the function of two
variables
In this practical, we will compute the Maclaurin series expansion for various functions using
Python. The Maclaurin series is a special case of the Taylor series, centered at the point (0, 0)), and
is useful for approximating functions near this point. We will utilize the SymPy library to derive
the series expansion and analyze the terms up to a specified degree for both variables x and y. This
process will involve expanding the function in terms of its power series, allowing us to gain insights
into the behavior of the function near the origin. This practical will enhance our understanding of
series expansions and their applications in mathematical analysis and problem-solving.

1 Write a Python program to expand the function ex sin(y) in the power of x and up to degree
three using Maclaurin expansions.

Manual:
Given, ex sin(y)
Maclaurin’s Table for the function ex sin(y) is as follows:

Expression Value at (0,0)


f (x, y) = ex sin y f (0, 0) = 0
fx (x, y) = ex sin y fx (0, 0) = 0
fy (x, y) = ex cos y fy (0, 0) = 1
fxy (x, y) = ex cos y fxy (0, 0) = 1
fxx (x, y) = ex sin y fxx (0, 0) = 0
fyy (x, y) = −ex sin y fyy (0, 0) = 0
fxxx (x, y) = ex sin y fxxx (0, 0) = 0
fyyy (x, y) = −ex cos y fyyy (0, 0) = −1
fxxy (x, y) = ex cos y fxxy (0, 0) = 1
fyyx (x, y) = −ex sin y fyyx (0, 0) = 0

By Maclaurin’s series we have:


1
fxx (0, 0)x2 + 2fxy (0, 0)xy + fyy (0, 0)y 2 + ...

f (x, y) = f (0, 0) + fx (0, 0)x + fy (0, 0)y +
2

86
∴ For the given function the Maclaurin’s series is
y 3 x2 y
ex sin y = y + xy − + + ...
6 2

Code:

from sympy import *


x, y, a, b = symbols(’x, y, a, b’)
n = 3 #The order of the Maclaurin expansion.
Y = zeros(n+1)
def f(x, y):
return exp(x) * sin(y)
for i in range(n+1):
for j in range(n+1-i):
Y[i, j] = diff(diff(f(x, y), x, i), y, j)
Y[i, j] = Y[i, j].subs({x: 0, y: 0})
s = Y[0, 0]
for i in range(1, n+1):
S1 = 0
for j in range(0, i+1):
S1 = S1 + binomial(i, j) * x**(i-j) * y**j * Y[i-j, j]
s = s + S1/factorial(i)
print("Maclaurin expansion is ", s)

Output:

2 Write a Python program to expand the function cos x cos y in the power of x and up to degree
three using Maclaurin expansion.

Code:

from sympy import *


x, y, a, b = symbols(’x, y, a, b’)
n = 3
Y = zeros(n+1)
def f(x, y):

87
return cos(x)*cos(y)
for i in range(n+1):
for j in range(n+1-i):
Y[i, j] = diff(diff(f(x, y), x, i), y, j)
Y[i, j] = Y[i, j].subs({x: 0, y: 0})
s = Y[0, 0]
for i in range(1, n+1):
S1 = 0
for j in range(0, i+1):
S1 = S1 + binomial(i, j) * x**(i-j) * y**j * Y[i-j, j]
s = s + S1/factorial(i)
print("Maclaurin expansion is ", s)

Output:

3 Write a Python program to expand the function cos(x + y) in the power of x and up to degree
three using Maclaurin expansion.

Code:

from sympy import *


x, y, a, b = symbols(’x, y, a, b’)
n = 3
Y = zeros(n+1)
def f(x, y):
return cos(x+y)
for i in range(n+1):
for j in range(n+1-i):
Y[i, j] = diff(diff(f(x, y), x, i), y, j)
Y[i, j] = Y[i, j].subs({x: 0, y: 0})

s = Y[0, 0]
for i in range(1, n+1):
S1 = 0
for j in range(0, i+1):

88
S1 = S1 + binomial(i, j) * x**(i-j) * y**j * Y[i-j, j]
s = s + S1/factorial(i)
print("Maclaurin expansion is ", s)

Output:

Practical Application:

Maclaurin’s expansion for functions of two variables is used in physics to approximate complex
systems near equilibrium, such as in quantum mechanics and fluid dynamics. In engineering, it
is applied to model vibrations and stress in structures, and in economics, it helps approximate
functions for optimization problems.

89
Practical 2.10 Line integrals with constant and variable lim-
its
In this practical, we will compute line integrals with both constant and variable limits. Line
integrals are essential in vector calculus, as they allow us to evaluate integrals along a specified
path in space. We will define a parameterized path and calculate the line integral by integrating
the product of the function values and the derivatives of the path parameters. This practical will
involve evaluating these integrals over the specified interval, enabling us to find the result and
understand the geometric interpretation of line integrals.

Z
1 Write a Python program to evaluate xy dx + x2 z dy + xyz dz; c is given by x = et , y =
c
e−t , z = t2 ; 0 ≤ t ≤ 1.

Manual:
Z
Given, xy dx + x2 z dy + xyz dz
C

Where x = et , y = e−t , z = t2

dx = et dt; dy = −e−t dt; dz = 2tdt


Z 1
et e−t et dt − e2t t2 e−t dt + et e−t t2 2t dt
   
0
Z 1
et − t2 et + 2t3 dt

0
1
t4
   
t 2 t t t 1
= e − t e + 2te − 2e + = e − e + 2e − 2e + − (1 − 2)
2 0 2
1
= +1
2
3
= .
2
Code:

from sympy import *


init_printing()
t = symbols(’t’)
x = exp(t)

90
y = exp(-t)
z = t**2
F = [x*y, x**2*z, x*y*z]
line_integral = integrate(F[0]*diff(x,t) + F[1]*diff(y,t) + F[2]*diff(z,t),
(t, 0, 1))
print("The required solution is ",line_integral)

Output:

The required solution is 3/2

Z
2 Write a Python program to evaluate (3x − 2y)dx + (y + 2z)dy − x2 dz; c is given by x = t, y =
c
2t2 , z = 3t3 ; 0 ≤ t ≤ 1

Code:

from sympy import *


init_printing()
t = symbols(’t’)
x = t
y = 2*t**2
z = 3*t**3
F = [(3*x-2*y),(y+2*z),(x**2)]
line_integral=integrate(F[0]*diff(x,t)+ F[1]*diff(y,t)-F[2]*diff(z,t),(t,0,1))
print("The required solution is ",line_integral)

Output:

The required solution is 31/6

91
x2
Z
3 Write a Python program to evaluate (x+2y)dx+(4−2x)dy; c is bounded by the ellipse +
c 16
y2
= 1.
25

Code:

from sympy import *


init_printing()
t=symbols(’t’)
x=4*cos(t)
y=5*sin(t)
F=[(x+2*y),(4-2*x)]
line_integral=integrate(F[0]*diff(x,t) + F[1]*diff(y,t), (t, 0, 2*pi))
print("The required solution is ",line_integral)

Output:

The required solution is −80π

Z
4 Write a Python program to evaluate (3xy)dx − (5z)dy + 10xdz given x = t2 + 1; y = 2t2 ; z =
c
t2 ; 1 ≤ t ≤ 2.

Code:

from sympy import *


init_printing()
t = symbols(’t’)
x = t**2+1
y = 2*t**2
z=t**2
F = [(3*x*y),(5*z),(10*x)]
line_integral=integrate(F[0]*diff(x,t)- F[1]*diff(y,t)+F[2]*diff(z,t), (t, 1, 2))
print("The required solution is ",line_integral)

Output:

The required solution is 201

92
Practical Application:

Line integrals are used in physics to calculate work done by a force field along a path, such as in
electromagnetism or fluid dynamics. In computer graphics, they help render curves and calculate
distances, and in economics, they are applied in optimization problems to determine costs or
benefits over a path.

93
Practical 2.11 Double Integrals with constant and variable
limits
In this practical, we are going to learn about double integrals, which are used to calculate the
volume under a surface in a two-dimensional space. Double integrals extend the concept of single
integrals to functions of two variables, allowing us to evaluate the accumulation of quantities over
a specified region. We will explore the computation of double integrals with both constant and
variable limits, examining how these limits affect the evaluation process. Through this practical,
we will gain a deeper understanding of double integrals, their applications in calculating areas and
volumes, and their significance in fields such as physics, engineering, and probability theory.

Z 1Z 2
1 Write a Python program to evaluate (x − y)dxdy.
0 1

Manual:
Z 1Z 2
Given, (x − y)dxdy
0 1
Z 1 2 2
x
= − xy dy
0 2 1
Z 1 
3
= − y dy
0 2
1
3y − y 2

3 1
= = −
2 0 2 2
= 1.

Code:

from sympy import*


init_printing()
x,y=symbols(’x y’)
v=integrate(integrate(x-y,(x,1,2)),(y,0,1))
print(’The value of double integral is’,v)

Output:

The value of double integral is 1

94
Z 1Z 2
2 Write a Python program to evaluate (x + y)dydx.
0 1

Code:

from sympy import*


init_printing()
x,y=symbols(’x y’)
v=integrate(integrate(x+y,(y,1,2)),(x,0,1))
print(’The value of double integral is’,v)

Output:

The value of double integral is 2

Z 3Z 2
3 Write a Python program to evaluate (x2 + 3y 2 )dydx.
0 1

Code:

from sympy import*


init_printing()
x,y=symbols(’x y’)
v=integrate(integrate(x**2+3*y**2,(y,1,2)),(x,0,3))
print(’The value of double integral is’,v)

Output:

The value of double integral is 30

Z 2 Z x2
4 Write a Python program to evaluate x(x2 + y 2 )dydx .
0 0

Code:

from sympy import*


init_printing()
x,y=symbols(’x y’)
v=integrate(integrate(x*(x**2+y**2),(y,0,x**2)),(x,0,2))
print(’The value of double integral is’,v)

95
Output:

The value of double integral is 64/3

Z 1 Z 1−x
5 Write a Python program to evaluate xydydx.
0 0

Code:

from sympy import*


init_printing()
x,y=symbols(’x y’)
v=integrate(integrate(x*y,(y,0,1-x)),(x,0,1))
print(’The value of double integral is’,v)

Output:

The value of double integral is 1/24

Practical Application:

Double integrals are used in physics to calculate areas and volumes, such as in fluid dynamics
and electromagnetism. In engineering, they calculate the center of mass and moments of inertia,
and in environmental science, they model pollutant dispersion and other processes involving two
variables.

96
Practical 2.12 Triple Integrals with constant and variable
limits
In this practical, we are going to learn about triple integrals, which extend the concept of double
integrals to three dimensions. Triple integrals enable us to compute volumes and other quantities
in higher-dimensional spaces, making them essential in various fields such as physics, engineering,
and probability theory. By understanding the process of integrating functions of three variables,
we will gain insights into the accumulation of quantities in three-dimensional space. This practical
will enhance our skills in working with multivariable calculus and deepen our appreciation for the
applications of triple integrals in real-world scenarios.

Z 1Z 2Z 2
1 Write a Python program to evaluate x2 yz dx dy dz .
0 0 1

Manual:
Z 1Z 2Z 2
Given, x2 yz dx dy dz
0 0 1
Z 1Z 2 3 2
x yz
= dy dz
0 0 3 1
1Z 2
7y 2
Z
= dy dz
0 0 3
Z 1 
14z 14
= dz =
0 3 6
7
= .
3
Code:

from sympy import*


init_printing()
x,y,z=symbols(’x y z’)
v=integrate(integrate(integrate(x**2*y*z,(x,1,2)),(y,0,2)),(z,0,1))
print(’The value of triple integral is’,v)

Output:

97
Z 1Z 2Z 2
2 Write a Python program to evaluate ex+y+z dx dy dz .
0 0 1

Code:

from sympy import*


init_printing()
x,y,z=symbols(’x y z’)
v=integrate(integrate(integrate(exp(x+y+z),(x,1,2)),(y,0,2)),(z,0,1))
print(’The value of triple integral is’,v)

Output:

Z 1 Z 1−x Z 1−x−y
1
3 Write a Python program to evaluate dz dy dx .
0 0 0 (x + y + z + 1)3

Code:

from sympy import*


init_printing()
x,y,z=symbols(’x y z’)
v=integrate(integrate(integrate(1/(x+y+z+1)**3,(z,0,1-x-y)),(y,0,1-x)),(x,0,1))
print(’The value of triple integral is’,v)

Output:

Practical Application:

Triple integrals are used in physics and engineering to calculate volumes and mass distributions
in three-dimensional space, such as in fluid dynamics and electromagnetism. In environmental
science, they model the spread of pollutants, and in computer graphics, they help render and model
complex 3D shapes.

98
Chapter 3

Ordinary Differential Equations and Real


Analysis

Practical 3.1 Variable Separable Form - Solution of ODE

In this practical, we are going to learn about solving ordinary differential equations (ODEs) using
the variable separable method. This technique is particularly useful for first-order differential
equations, as it allows us to separate the variables so that all terms involving one variable are on
one side of the equation and all terms involving the other variable are on the opposite side. By
doing so, we can integrate both sides independently, leading to a solution for the ODE. Through
this practical, we will have a solid grasp of the variable separable method and its significance in
the broader context of differential equations.

 dy
1 Write a Python program to solve x2 + 1 = 1.
dx

Manual:
 dy
Given, x2 + 1 =1
dx
dy 1
⇒ = 2
dx x +1
dx
⇒ dy =
x2+1

99
R R 1
On Integrating dy = dx + c
x2 +1

y = tan−1 x + c

∴ y − tan−1 x = c.

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
x_term=integrate(1/(x**2+1),x)
y_term=integrate(1,y)
Ans=simplify(y_term-x_term)
print(Ans,"= C")

Output:

 dy
Write a Python program to solve x2 − yx2 + y 2 + xy 2 = 0.

2
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
x_term=integrate((1+x)/x**2,x)
y_term=integrate((1-y)/y**2,y)
Ans=simplify(y_term+x_term)
print(Ans,"= C")

Output:

100
dy
3 Write a Python program to solve + xy = xy 3 .
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
x_term=integrate(x,x)
y_term=integrate(1/(y*(y**2-1)),y)
Ans=simplify(y_term-x_term)
print(Ans,"= C")

Output:

4 Write a Python program to solve (ey + 1) cos xdx + ey sin xdy = 0.

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
x_term=integrate(cot(x),x)
y_term=integrate(exp(y)/(exp(y)+1),y)
Ans=simplify(y_term+x_term)
print(Ans,"= C")

Output:

dy
5 Write a Python program to solve xy =y+2.
dx

Code:

from sympy import *


x=Symbol(’x’)

101
y=Symbol(’y’)
x_term=integrate(1/x,x)
y_term=integrate(y/(y+2),y)
Ans=simplify(y_term-x_term)
print(Ans,"= C")

Output:

Practical Application:

The solution of ordinary differential equations (ODEs) in the variable separable form is applied
in fields like physics and biology. For instance, in population modeling, it describes the growth
of populations where the rate of change is proportional to the current population size. It is also
used in chemistry to model the rate of reactions and in physics for solving problems related to heat
conduction or fluid dynamics.

102
Practical 3.2 Homogeneous Differential Equations - Solu-
tion of ODE

In this practical, we are going to learn about homogeneous differential equations, which are a
specific type of ordinary differential equation characterized by their structure. A differential
equation is considered homogeneous if it can be expressed in a form where all terms are of the same
degree when the variables are scaled. Through examples, we will gain insights into identifying
homogeneous equations and applying appropriate methods to find their solutions.Through this
practical, we will have a deeper understanding of homogeneous differential equations and their
significance in mathematical modeling and analysis.

1 Write a Python program to solve(x − y)dy − (2x − y)dx = 0 .

Manual:

Given, (x − y)dy − (2x − y)dx = 0

(x − y)dy = (2x − y)dx

dy 2x − y
= .....(1)
dx x−y
dv 2x − vx
(1) becomes v + x =
dx x − vx

dv 2 − v − v(1 − v) v 2 − 2v + 2
x = =
dx 1−v 1−v
dx 1−v
= 2
x v − 2v + 2

On integrating:

1−v
Z Z
dx
dv = +c
v 2 − 2v + 2 x
1
− log(v 2 − 2v + 2) = log x + log c
2

log(v 2 − 2v + 2) + 2 log x = log c

log x2 (v 2 − 2v + 2) = log c

103
y2
 
2 2y
x − +2 =c
x2 x

The required solution is y 2 − 2yx + 2x2 = C.

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
y1=(2*x-y)/(x-y)
n=homogeneous_order(y1,x,y)
if(n==None):
print("The given differential equation is not homogeneous")
else:
print("The given differential equation is homogeneous")
v=Symbol(’v’)
lhs=integrate((1-v)/(v**2-2*v+2),v)
RHS=integrate(1/x,x)
LHS=lhs.subs(v,y/x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

Write a Python program to solve y 2 + 2xy dx + 2x2 + 3xy dy = 0.


 
2

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
y1=-(y**2+2*x*y)/(2*x**2+3*x*y)
n=homogeneous_order(y1,x,y)
if(n==None):
print("The given differential equation is not homogeneous")
else:

104
print("The given differential equation is homogeneous")
v=Symbol(’v’)
lhs=integrate((2+3*v)/(-4*v**2-4*v),v)
RHS=integrate(1/x,x)
LHS=lhs.subs(v,y/x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

Write a Python program to solve x2 + 2y 2 dx − xydy = 0.



3

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
y1=(x**2+2*y**2)/(x*y)
n=homogeneous_order(y1,x,y)
if(n==None):
print("The given differential equation is not homogeneous")
else:
print("The given differential equation is homogeneous")
v=Symbol(’v’)
lhs=integrate(v/(1+v**2),v)
RHS=integrate(1/x,x)
LHS=lhs.subs(v,y/x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

105
p
4 Write a Python program to solve xdy − ydx = x2 + y 2 dx.

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
y1=(sqrt(x**2+y**2)+y)/x
n=homogeneous_order(y1,x,y)
if(n==None):
print("The given differential equation is not homogeneous")
else:
print("The given differential equation is homogeneous")
v=Symbol(’v’)
lhs=integrate(1/sqrt(1+v**2),v)
RHS=integrate(1/x,x)
LHS=lhs.subs(v,y/x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

 x x 
x
5 Write a Python program to solve 1 + e y  dx + e y 1 − dy = 0.
y

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
y1=-(exp(x/y)*(1-(x/y)))/(1+exp(x/y))
n=homogeneous_order(y1,x,y)
if(n==None):
print("The given differential equation is not homogeneous")
else:
print("The given differential equation is homogeneous")
v=Symbol(’v’)

106
lhs=integrate((1+exp(v))/(v+exp(v)),v)
RHS=integrate(-1/y,y)
LHS=lhs.subs(v,x/y)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

Practical Application:

Homogeneous differential equations are used in mechanical vibrations and electrical circuits. For
example, they model the behavior of a mass-spring system, where the displacement follows a
homogeneous ODE. In electrical engineering, they are essential for analyzing circuits with resistors,
capacitors, and inductors, as well as in wave propagation and signal processing.

107
Practical 3.3 Linear Differential Equations - Solution of ODE

In this practical, we will solve a linear ordinary differential equation (ODE) by employing the
integrating factor method. This technique is particularly useful for first-order linear ODEs, where
we will determine the integrating factor based on the given functions. By calculating the integrating
factor, we can simplify the equation and find the general solution of the ODE. This process involves
integrating the resulting expressions to obtain the solution, allowing us to understand the behavior
of the system described by the differential equation.

Write a Python program to solve 1 + x2 dy + y − tan−1 x dx = 0.


 
1

Manual:

Given, (1 + x2 )dy + (y − tan−1 x)dx = 0

dy
⇒ (1 + x2 ) + y − tan−1 x = 0
dx
dy 1 tan−1 x
⇒ +y =
dx (1 + x2 ) 1 + x2
dy
It is of the form + Py = Q
dx
Z
1
2
dx −1
I.F = e 1 + x = etan x
Z
−1 x −1
⇒ y.e tan = Q.etan x dx + C

tan−1 x tan−1 x
Z
−1
⇒ y · etan x
= ·e dx + c
1 + x2
1
⇒ tan−1 x = t ⇒ dx = dt
1 + x2
Z
tan−1 x
⇒y·e = tdt · et + c

−1 −1 −1
⇒ y · etan x
= e(tan x)
tan−1 x − e(tan x)
+ c.

108
Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
print("Given equation is linear in y where ")
P=1/(1+x**2)
Q=atan(x)/(1+x**2)
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
LHS=y*IF
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

dy
2 Write a Python program to solve x − 2y = 2x.
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
print("Given equation is linear in y where ")
P=-2/x
Q=2
print(’P =’,P)

109
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
LHS=y*IF
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

2
3 Write a Python program to solve dx + 2xydy = ye−y dy.

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
print("Given equation is linear in x where ")
P=2*y
Q=y*exp(-y**2)
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,y)
IF=exp(InP)
print("Integrating Factor =",IF)
LHS=x*IF
R=IF*Q
RHS=integrate(R,y)
print("Required solution is ",LHS,"=",RHS,"+c")

110
Output:

dy
4 Write a Python program to solve x + y log y = xyex .
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
print("Given equation is linear in y where ")
P=1/x
Q=exp(x)
v=log(y)
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
LHS=v*IF
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c") )

Output:

111
 dy −1
5 Write a Python program to solve x2 + 1 + y = etan x .
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
print("Given equation is linear in y where ")
P=1/(x**2+1)
Q=exp(atan(x))/(x**2+1)
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
LHS=y*IF
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

Practical Application:

Linear differential equations are crucial in fluid mechanics, control theory, and economics. In fluid
mechanics, they describe the flow of fluids, while in control theory, they are used for modeling
systems like temperature regulation. In economics, linear ODEs help model financial systems,
where variables like stock prices or interest rates change based on linear relationships.

112
Practical 3.4 Bernoulli Form - Solution of ODE
In this practical, we will solve a Bernoulli differential equation by reducing it to a linear form.
Bernoulli equations are a specific type of nonlinear ODE that can be transformed into a linear
equation through a suitable substitution. We will calculate the integrating factor using the given
functions and apply it to transform the Bernoulli equation into a linear one. This practical will
involve deriving the integrating factor, substituting it into the transformed equation, and integrating
to obtain the solution.

dy
1 Write a Python program to solve x + (1 − x)y = x2 y 2 .
dx

Manual:

dy
Given, x + (1 − x)y = x2 y 2
dx

Divide throughout by xy 2

1 dy 1
2
+ (1 − x) =x
y dx xy

dy (1 − x) −1
y −2 + y = x....(1)
dx x

put v = y −1

dv dy
= −y −2
dx dx
dv dy
− = y −2
dx dx
−dv (1 − x)
(1) becomes + v=x
dx x
dv x−1
+ v = −x
dx x
R R 1
x− x dx ex
I.F = e P dx =e = ex .e− log x =
x
R
Thus the general solution is V.IF= Q.(I.F )dx + c

ex R ex
v. = −x dx + c
x x
ex
= −ex .dx + c
R
xy

113
ex
Thus the required solution is = −ex + c.
xy

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
v=Symbol(’v’)
print("Reducing Bernoulli’s Equation to linear we get")
P=-(1-x)/x
Q=-x
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
lhs=v*IF
LHS=lhs.subs(v,1/y)
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

dy
2 Write a Python program to solve x + y = x3 y 6 .
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
v=Symbol(’v’)
print("Reducing Bernoulli’s Equation to linear we get")

114
P=-5/x
Q=-5*x**2
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
lhs=v*IF
LHS=lhs.subs(v,1/y**5)
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

dy
3 Write a Python program to solve + y tan x = y 3 sec x.
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
v=Symbol(’v’)
print("Reducing Bernoulli’s Equation to linear we get")
P=-2*tan(x)
Q=-2*sec(x)
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
lhs=v*IF

115
LHS=lhs.subs(v,1/y**2)
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c")

Output:

dy
4 Write a Python program to solve x + y = y 2 log x.
dx

Code:

from sympy import *


x=Symbol(’x’)
y=Symbol(’y’)
v=Symbol(’v’)
print("Reducing Bernoulli’s Equation to linear we get")
P=-1/x
Q=-log(x)/x
print(’P =’,P)
print(’Q =’,Q)
InP=integrate(P,x)
IF=exp(InP)
print("Integrating Factor =",IF)
lhs=v*IF
LHS=lhs.subs(v,1/y)
R=IF*Q
RHS=integrate(R,x)
print("Required solution is ",LHS,"=",RHS,"+c")

116
Output:

Practical Application:

Bernoulli differential equations are applied in biology, chemistry, and fluid dynamics. They
model the spread of diseases, where the rate of infection depends on both the number of infected
individuals and the susceptible population. In fluid dynamics, Bernoulli’s equation describes the
motion of fluids, such as air flow over a wing or water flow through pipes.

117
Practical 3.5 Complementary Function of the Ordinary Lin-
ear Differential Equation
In this practical, we are going to learn about finding the complementary function for linear dif-
ferential equations. The complementary function is a crucial component of the general solution,
representing the solution to the associated homogeneous equation. We will explore the methods
for determining the complementary function, including the characteristic equation approach for
constant coefficient linear differential equations. By working through examples, we will gain
insights into how the complementary function contributes to the overall solution of the differential
equation.

d3 y dy
1 Write a Python program to solve 3
− 13 + 12y = 0.
dx dx

Manual:

d3 y dy
Given, 3
− 13 + 12y = 0
dx dx

A.E = m3 − 13m + 12 = 0

⇒ (m − 1)(m2 + m − 12) = 0

⇒ (m − 1)(m + 4)(m − 3) = 0

⇒ m = 1, −4, 3.

The CF is y = c1 .ex + c2 .e−4x + c3 .e3x .

Code:

import numpy as np
import sympy as sp
x=sp.symbols (’x’)
coeff=[1,0,-13,12]
print("The roots are", np.roots (coeff))
# Enter the value of the roots that are found manually
r1=-4
r2=3

118
r3=1
if ((r1.imag or r2.imag and r3.imag)!=0):
print ("c1", sp.exp(r1*x),’+’,sp.exp(r2.real*x),"(c2 cos",r2.imag,"x+c3
sin",r2.imag,"x)")
if ((r1.imag or r2.imag and r3.imag)==0):
if (r1!=r2!=r3):
print("The general soluition is
y=c1",sp.exp(r1*x),"+c2",sp.exp(r2*x),"+c3",sp.exp(r3*x))
if (r1==r2):
print("The general soluition is y=(c1+c2 x)",sp.exp(r1*x),"+c3",sp.exp(r3*x))
if (r2==r3):
print("The general soluition is y=(c2+c3 x)",sp.exp(r2*x),"+c1",sp.exp(r1*x))
if (r1==r3):
print("The general soluition is y=(c1+c3 x)",sp.exp(r1*x),"+c1",sp.exp(r2*x))

Output:

d3 y d2 y
2 Write a Python program to solve − 3 + 4y = 0.
dx3 dx2

Code:

import numpy as np
import sympy as sp
x=sp.symbols (’x’)
coeff=[1,-3,0,4]
print("The roots are", np.roots (coeff))
r1=-1
r2=2
r3=2
if ((r1.imag or r2.imag and r3.imag)!=0):
print ("c1", sp.exp(r1*x),’+’,sp.exp(r2.real*x),"(c2 cos",r2.imag,"x+c3
sin",r2.imag,"x)")
if ((r1.imag or r2.imag and r3.imag)==0):

119
if (r1!=r2!=r3):
print("The general soluition is
y=c1",sp.exp(r1*x),"+c2",sp.exp(r2*x),"+c3",sp.exp(r3*x))
if (r1==r2):
print("The general soluition is y=(c1+c2 x)",sp.exp(r1*x),"+c3",sp.exp(r3*x))
if (r2==r3):
print("The general soluition is y=(c2+c3 x)",sp.exp(r2*x),"+c1",sp.exp(r1*x))
if (r1==r3):
print("The general soluition is y=(c1+c3 x)",sp.exp(r1*x),"+c1",sp.exp(r2*x))

Output:

d3 y d2 y dy
3 Write a Python program to solve − 2 +4 − 8y = 0.
dx3 dx2 dx

Code:

import numpy as np
import sympy as sp
x=sp.symbols (’x’)
coeff=[1,-2,4,-8]
print("The roots are", np.roots (coeff))
r1=2
r2=2j
r3=-2j
if ((r1.imag or r2.imag and r3.imag)!=0):
print ("c1", sp.exp(r1*x),’+’,sp.exp(r2.real*x),"(c2 cos",r2.imag,"x+c3
sin",r2.imag,"x)")
if ((r1.imag or r2.imag and r3.imag)==0):
if (r1!=r2!=r3):
print("The general soluition is
y=c1",sp.exp(r1*x),"+c2",sp.exp(r2*x),"+c3",sp.exp(r3*x))
if (r1==r2):
print("The general soluition is y=(c1+c2 x)",sp.exp(r1*x),"+c3",sp.exp(r3*x))
if (r2==r3):

120
print("The general soluition is y=(c2+c3 x)",sp.exp(r2*x),"+c1",sp.exp(r1*x))
if (r1==r3):
print("The general soluition is y=(c1+c3 x)",sp.exp(r1*x),"+c1",sp.exp(r2*x))

Output:

Practical Application:

The complementary function of an ordinary linear differential equation is used in mechanical


engineering and electrical circuits. It helps determine the natural frequencies of vibrating systems,
which is essential for designing stable structures. In electrical engineering, it is used to solve circuits
with inductance and capacitance, determining the system’s natural response.

121
Practical 3.6 Ordinary Linear Differential Equations
In this practical, we are going to learn the general approach for solving ordinary linear differential
equations. We will focus on first and second-order equations, examining their standard forms
and the various methods available for finding solutions. By exploring techniques such as the
integrating factor method, the characteristic equation, and variation of parameters, we will develop
a comprehensive understanding of how to tackle these equations effectively. Through examples
and exercises, we will enhance our problem-solving skills and gain insights into the applications of
linear differential equations in various fields.

1 Write a Python program to solve D2 y + 2Dy + y = 2e2x .

Manual:

Given, D2 y + 2Dy + y = 2e2x .

CF = D2 + 2Dy + y = 0

D2 + 2D + 1 = 0

AE = m2 + 2m + 1

m = −1, −1

CF = (c1 + xc2 )e−x

1 1 1
PI = · 2e2x = · 2e2x = · 2e2x
D2 + 2D + 1 4+4+1 9

y = CF + P I
2
= (c1 + xc2 )e−x + e2x .
9

Code:

import sympy as sp
sp.init_printing()
x=sp.symbols(’x’)
y=sp.Function(’y’)
y1=sp.Derivative(y(x),x)

122
y2=sp.Derivative(y1,x)
eqdiff=y2+2*y1+y(x)-2*sp.exp(2*x)
sol=sp.dsolve(eqdiff,y(x))
sp.pprint(sol)

Output:

d3 y d2 y dy
2 Write a Python program to solve + − − y = cos 2x.
dx3 dx2 dx

Code:

import sympy as sp
sp.init_printing()
x=sp.symbols(’x’)
y=sp.Function(’y’)
y1=sp.Derivative(y(x),x)
y2=sp.Derivative(y1,x)
y3=sp.Derivative(y2,x)
eqdiff=y3+y2-y1-y(x)-sp.cos(2*x)
sol=sp.dsolve(eqdiff,y(x))
sp.pprint(sol)

Output:

123
d2 y
3 Write a Python program to solve − y = 2 + 5x.
dx2

Code:

import sympy as sp
sp.init_printing()
x=sp.symbols(’x’)
y=sp.Function(’y’)
y1=sp.Derivative(y(x),x)
y2=sp.Derivative(y1,x)
eqdiff=y2-y(x)-2-5*x
sol=sp.dsolve(eqdiff,y(x))
sp.pprint(sol)

Output:

d2 y dy
4 Write a Python program to solve +3 + 2y = e2x sin x
dx2 dx

Code:

import sympy as sp
sp.init_printing()
x=sp.symbols(’x’)
y=sp.Function(’y’)
y1=sp.Derivative(y(x),x)
y2=sp.Derivative(y1,x)
eqdiff=y2+3*y1+2*y(x)-sp.exp(2*x)*sp.sin(x)
sol=sp.dsolve(eqdiff,y(x))
sp.pprint(sol)

Output:

124
d3 y
5 Write a Python program to solve + y = 3 + e−x + 5ex x2 .
dx3

Code:

import sympy as sp
sp.init_printing()
x=sp.symbols(’x’)
y=sp.Function(’y’)
y1=sp.Derivative(y(x),x)
y2=sp.Derivative(y1,x)
y3=sp.Derivative(y2,x)
eqdiff=y3+y(x)-3-sp.exp(-x)-5*sp.exp(x)*x**2
sol=sp.dsolve(eqdiff,y(x))
sp.pprint(sol)

Output:

Practical Application:

Ordinary linear differential equations (OLDEs) are used in modeling dynamic systems across
various scientific fields. In physics, they describe phenomena like motion under gravity, while in
biology, they model population dynamics. OLDEs are also applied in economics to predict trends
in growth, interest rates, and market behavior.

125
Practical 3.7 Test the Convergence of the Sequence
In this practical, we are going to learn how to determine whether a given sequence is convergent.
We will explore different convergence criteria and apply these criteria to test whether a sequence
approaches a finite limit. By working through various examples, we will develop a deeper under-
standing of the behavior of sequences and the significance of convergence in mathematical analysis.
This practical will equip us with the tools needed to analyze sequences effectively.

1 Write a Python program to test the convergence, divergence and oscillatory of the sequence
n + (−1)n
xn = .
n

Manual:

n + (−1)n
Given, xn =
n
2n + (−1)2n
Consider x2n =
n
2n + (−1)2n
 
lim {x2n } = lim
n→∞ n→∞ 2n
 
1
lim 1 + =1
n→∞ 2n

2n + 1 + (−1)2n+1
Consider x2n+1 =
2n + 1

2n + 1 + (−1)2n+1
lim {x2n+1 } = lim
n→∞ n→∞ 2n + 1
 
1
= lim 1 − =1
n→∞ 2n + 1

Therefore, the given sequence {xn } is convergent.

Code:

from sympy import *


from sympy.abc import n
from numbers import Rational
exp=(n+(-1)**n)/n
lim=limit_seq(exp,n)

126
print("Given Expression: ",exp)
print("Limit: ",lim)
if abs(lim)==oo:
print("The sequence diverges")
elif issubclass(type(lim),Rational):
print("The sequence converges to ",lim)
else:
print("The sequence oscilates between", lim.min,"and ",lim.max)

Output:

2 Write a Python program to test the convergence, divergence and oscillatory of the sequence
xn = 1 + (−1)n .

Code:

from sympy import *


from sympy.abc import n
from numbers import Rational
exp=(1+(-1)**n)
lim=limit_seq(exp,n)
print("Given Expression: ",exp)
print("Limit: ",lim)
if abs(lim)==oo:
print("The sequence diverges")
elif issubclass(type(lim),Rational):
print("The sequence converges to ",lim)
else:
print("The sequence oscilates between", lim.min,"and ",lim.max)

127
Output:

3 Write a Python program to test the convergence, divergence and oscillatory of the sequence
(n + 1)n+1
xn = .
nn

Code:

from sympy import *


from sympy.abc import n
from numbers import Rational
exp=(n+1)**(n+1)/n**n
lim=limit_seq(exp,n)
print("Given Expression: ",exp)
print("Limit: ",lim)
if abs(lim)==oo:
print("The sequence diverges")
elif issubclass(type(lim),Rational):
print("The sequence converges to ",lim)
else:
print("The sequence oscilates between", lim.min,"and ",lim.max)

Output:

4 Write a Python program to test the convergence, divergence and oscillatory of the sequence
3n − 4
xn = .
4n + 3

Code:

from sympy import *


from sympy.abc import n

128
from numbers import Rational
exp=(3*n-4)/(4*n+3)
lim=limit_seq(exp,n)
print("Given Expression: ",exp)
print("Limit: ",lim)
if abs(lim)==oo:
print("The sequence diverges")
elif issubclass(type(lim),Rational):
print("The sequence converges to ",lim)
else:
print("The sequence oscilates between", lim.min,"and ",lim.max)

Output:

5 Write a Python program to test the convergence, divergence and oscillatory of the sequence
2n + 5
xn = .
5n + 2

Code:

from sympy import *


from sympy.abc import n
from numbers import Rational
exp=(2*n+5)/(5*n+2)
lim=limit_seq(exp,n)
print("Given Expression: ",exp)
print("Limit: ",lim)
if abs(lim)==oo:
print("The sequence diverges")
elif issubclass(type(lim),Rational):
print("The sequence converges to ",lim)
else:
print("The sequence oscilates between", lim.min,"and ",lim.max)

129
Output:

Practical Application:

Testing the convergence of sequences is important in numerical analysis and computer science.
It helps in approximating solutions to problems like finding roots of equations or optimizing
functions. In signal processing, it is used to analyze the stability of digital signals and systems, and
in financial mathematics, it models the long-term behavior of financial portfolios.

130
Practical 3.8 Comparison Test - Test the Convergence of the
Series
In this practical, we are going to learn about convergence tests for series. Understanding whether
a series converges or diverges is essential in mathematical analysis, as it helps us determine the
behavior of infinite sums. We will explore various convergence tests. By applying these tests to
different series, we will gain insights into their convergence properties and learn how to make
informed conclusions about their behavior. This practical will enhance our analytical skills and
deepen our understanding of series in the context of calculus and mathematical analysis.

1 Write a Python program to test the convergence, divergence and oscillatory of the series
1 1 1
+ + + ...
1·2 2·3 3·4

Manual:

1 1 1
Given, + + + ...
1.2 2.3 3.4
1 1
Consider Un = = 2
n+1 n +n
P 1 1
Here Un = then vn = 2
n2 n
Un n2 n
lim = lim = lim   =1
n→∞ Vn n→∞ n(n + 1) n→∞ 1
n 1+
n
P P
Comparison test Un and Vn converge or diverge together.
P P 1
But Vn =
n2
P
Here p = 2, so Vn is convergent.
P
Therefore Un converges.

Code:

from sympy import *


n,s=symbols(’n s’)
an=1/(n+n**2)
bn=1/n**2

131
sn=an/bn
L=limit(s,n,oo)
if L!=0:
print("Both an and bn converges and diverges together")
print("Use p - series to find the convergence and divergence of bn")
p=2
print("The value of p is : ",p)
if p>1:
print("Series bn is convergent using p - series")
print("Hence series an is also convergent")
elif p<=1:
print("Series bn is divergent using p - series")
print("Hence series an is also divergent")
else:
print("Series an and bn neither converges nor diverges together")

Output:

2 Write
r r a Python
r program to test the convergence, divergence and oscillatory of the series
1 2 3
+ + + ...
4 6 8

Code:

from sympy import *


n,s=symbols(’n s’)
an=(n/(2*(n+1)))**(1/2)
bn=1
sn=an/bn
L=limit(s,n,oo)
if L!=0:

132
print("Both an and bn converges and diverges together")
print("Use p - series to find the convergence and divergence of bn")
p=0
print("The value of p is : ",p)
if p>1:
print("Series bn is convergent using p - series")
print("Hence series an is also convergent")
elif p<=1:
print("Series bn is divergent using p - series")
print("Hence series an is also divergent")
else:
print("Series an and bn neither converges nor diverges together")

Output:

3 Write a Python program to test the convergence, divergence and oscillatory of the series
1 1 1
√ √ +√ √ +√ √ + ...
1+ 2 2+ 3 3+ 4

Code:

from sympy import *


n,s=symbols(’n s’)
an=1/((n)**(1/2)+(n+1)**(1/2))
bn=1/(n)**(1/2)
sn=an/bn
L=limit(s,n,oo)
if L!=0:
print("Both an and bn converges and diverges together")
print("Use p - series to find the convergence and divergence of bn")
p=0.5
print("The value of p is : ",p)

133
if p>1:
print("Series bn is convergent using p - series")
print("Hence series an is also convergent")
elif p<=1:
print("Series bn is divergent using p - series")
print("Hence series an is also divergent")
else:
print("Series an and bn neither converges nor diverges together")

Output:

4 Write a Python program to test the convergence, divergence and oscillatory of the series
1 3 5
+ + + ...
1·2·3 2·3·4 3·4·5

Code:

from sympy import *


n,s=symbols(’n s’)
an=(2*n-1)/(n*(n+1)*(n+2))
bn=1/n**2
sn=an/bn
L=limit(s,n,oo)
if L!=0:
print("Both an and bn converges and diverges together")
print("Use p - series to find the convergence and divergence of bn")
p=2
print("The value of p is : ",p)
if p>1:
print("Series bn is convergent using p - series")
print("Hence series an is also convergent")
elif p<=1:

134
print("Series bn is divergent using p - series")
print("Hence series an is also divergent")
else:
print("Series an and bn neither converges nor diverges together")

Output:

Practical Application:

The comparison test is widely used in analysis and engineering. In signal processing, it helps
determine the convergence of Fourier series, which represent signals as sums of sinusoids. In
physics, it is applied to series expansions in quantum mechanics and statistical mechanics, and in
algorithm analysis, it evaluates the efficiency and convergence of iterative methods.

135
Practical 3.9 D’ Alembert’s Ratio Test - Test for Convergence
of the Series
In this practical, we are going to learn about D’Alembert’s ratio test for convergence. This widely
used test provides a systematic way to determine the convergence or divergence of infinite series
by examining the ratio of consecutive terms. We will explore the conditions under which the ratio
test is applicable and how to interpret the results. Through a series of examples, we will apply
D’Alembert’s ratio test to various series, enhancing our understanding of its effectiveness and
limitations.

1 Write a Python program to test the convergence of the series

2! 3! 4!
1+ 2
+ 3 + 4 + ...
2 3 4

Manual:

2! 3! 4!
Given, 1 + 2
+ 3 + 4 + ...
2 3 4
n! (n + 1)!
Here Un = n and Un+1 =
n (n + 1)n+1

Un+1 (n + 1)! nn
Now, = × .
Un (n + 1)n+1 n!

nn
 
Un+1 (n + 1)!
lim = lim ×
n→∞ Un n→∞ (n + 1)( n + 1) n!

nn (n + 1)
 
= lim
n→∞ (n + 1)( n + 1)

 n 
= lim
n→∞ n(n+1)
 
1
= lim =0<1
n→∞ nn

By D’Alembert’s ratio text Un is convergent.

Code:

from sympy import *


# Declare a symbolic variable n
n = Symbol(’n’)

136
# Give the nth term of the series
expr = factorial(n)/n**n
# Convert the input string into a SymPy expression
u = Lambda(n, sympify(expr))
# Print the given series
print("The given series is:", u(n))
# Calculate the ratio for D’Alembert’s Ratio Test
ratio = u(n + 1) / u(n)
print("The ratio is:", ratio)
# Compute the limit of the ratio as n tends to infinity
L1 = limit(ratio, n, oo)
print("The limit is:", L1)
# Apply D’Alembert’s Ratio Test
if L1 < 1:
print("The series is convergent by D’Alembert’s Ratio Test.")
elif L1 > 1:
print("The series is divergent by D’Alembert’s Ratio Test.")
else:
print("D’Alembert’s Ratio Test fails. Consider using Raabe’s Test
for convergence.")

Output:

2 Write a Python program to test the convergence of the series

2! 3! 4!
+ 2 + 3 + ...
3 3 3

Code:

from sympy import *


n = Symbol(’n’)
expr = factorial(n+1)/3**n
u = Lambda(n, sympify(expr))

137
print("The given series is:", u(n))
ratio = u(n + 1) / u(n)
print("The ratio is:", ratio)
L1 = limit(ratio, n, oo)
print("The limit is:", L1)
if L1 < 1:
print("The series is convergent by D’Alembert’s Ratio Test.")
elif L1 > 1:
print("The series is divergent by D’Alembert’s Ratio Test.")
else:
print("D’Alembert’s Ratio Test fails. Consider using Raabe’s Test
for convergence.")

Output:

3 Write a Python program to test the convergence of the series

12 22 22 32 32 42
+ + + ...
1! 2! 3!

Code:

from sympy import *


n = Symbol(’n’)
expr = (n**2)*(n+1)**2/factorial(n)
u = Lambda(n, sympify(expr))
print("The given series is:", u(n))
ratio = u(n + 1) / u(n)
print("The ratio is:", ratio)
L1 = limit(ratio, n, oo)
print("The limit is:", L1)
if L1 < 1:
print("The series is convergent by D’Alembert’s Ratio Test.")
elif L1 > 1:

138
print("The series is divergent by D’Alembert’s Ratio Test.")
else:
print("D’Alembert’s Ratio Test fails. Consider using Raabe’s Test
for convergence.")

Output:

4 Write a Python program to test the convergence of the series

2 22 23
+ 2 + 2 + ...
12 +1 2 +1 3 +1

Code:

from sympy import *


n = Symbol(’n’)
expr = 2**n/(n**2+1)
u = Lambda(n, sympify(expr))
print("The given series is:", u(n))
ratio = u(n + 1) / u(n)
print("The ratio is:", ratio)
L1 = limit(ratio, n, oo)
print("The limit is:", L1)
if L1 < 1:
print("The series is convergent by D’Alembert’s Ratio Test.")
elif L1 > 1:
print("The series is divergent by D’Alembert’s Ratio Test.")
else:
print("D’Alembert’s Ratio Test fails. Consider using Raabe’s Test
for convergence.")

139
Output:

5 Write a Python program to test the convergence of the series

1 1 1 1
+ + + . . . + n−1 + ...
2 3 5 2 +1

Code:

from sympy import *


n = Symbol(’n’)
expr = 1/((2**(n-1))+1)
u = Lambda(n, sympify(expr))
print("The given series is:", u(n))
ratio = u(n + 1) / u(n)
print("The ratio is:", ratio)
L1 = limit(ratio, n, oo)
print("The limit is:", L1)
if L1 < 1:
print("The series is convergent by D’Alembert’s Ratio Test.")
elif L1 > 1:
print("The series is divergent by D’Alembert’s Ratio Test.")
else:
print("D’Alembert’s Ratio Test fails. Consider using Raabe’s Test
for convergence.")

Output:

140
Practical Application:

D’Alembert’s ratio test is important in solving differential equations and modeling physical systems.
In fluid dynamics, it helps evaluate the convergence of series used in flow problems, and in electrical
engineering, it is applied to power series expansions for circuit responses, ensuring stable and
efficient circuit design.

141
Practical 3.10 Raabe’s Test - Test for Convergence of the
Series
In this practical, we are going to learn about Raabe’s test, another method for testing the conver-
gence of a series. Raabe’s test is particularly useful for series where the ratio test is inconclusive.
We will compare Raabe’s test with the ratio test, discussing the scenarios in which each test is most
effective. By applying Raabe’s test to various series, we will analyze their convergence properties
and gain insights into the behavior of infinite series. This practical will deepen our understanding
of convergence tests and enhance our ability to evaluate series in mathematical analysis.

1 Write a Python program to test the convergence, divergence and oscillatory of the series

1 1·3 1·3·5
1+ + + + ...
2 2·4 2·4·6

Manual:
1 1·3 1·3·5
Given, 1 + + + + ...
2 2·4 2·4·6
1.3.5(2n − 1)
Here Un =
2.4.6.....2n
1.3.5...(2n+!)
Un+1 =
2.4.6...(2n + 2)

Un+1 1.3.5(2n − 1) 1.3.5...(2n + 1) 1


Consider = × = =1
Un 2.4.6.....2n 2.4.6...(2n + 2) 1
1−
n
Therefore, D’Alembert’s ratio test fails. hence, we apply Raabe’s fails.
 
un
lim n −1
n→∞ un+1
   
2n + 2 1 n 1
lim n − = lim = <1
n→∞ 2n + 1 1 n→∞ 2n + 1 2

Hence by Raabe’s test the given given series divergent.

Code:

from sympy import *


n=Symbol(’n’)
ratio=(2*n+1)/(2*n+2)

142
print("The ratio is", ratio)
L1=limit(ratio,n,oo)
print("The limit is", L1)
if L1<1:
print("By D’Alembert’s Rato Test the given series is convergent")
elif L1>1:
print("By D’Alembert’s Rato Test the given series is divergent ")
else:
print("D’Alembert’s Rato Test fails and we use Raabe’s Test for convergence")
L2=limit(((n/ratio)-n),n,oo)
print("The limit of the series using Raabe’s test is ", L2)
if L2>1:
print("The series is convergent by Raabe’s Test")
elif L2<1:
print("The series is divergent by Raabe’s Test ")
else:
print("Both the test fails")

Output:

2 Write a Python program to test the convergence, divergence and oscillatory of the series

2 1 2·4 1 2·4·6 1
1+ · + · + · + ...
1 2 1·3 3 1·3·5 4

Code:

from sympy import *


n=Symbol(’n’)
ratio=((2*n+2)*(n+1))/((2*n+1)*(n+2))
print("The ratio is", ratio)
L1=limit(ratio,n,oo)
print("The limit is", L1)

143
if L1<1:
print("By D’Alembert’s Rato Test the given series is convergent")
elif L1>1:
print("By D’Alembert’s Rato Test the given series is divergent ")
else:
print("D’Alembert’s Rato Test fails and we use Raabe’s Test for convergence")
L2=limit(((n/ratio)-n),n,oo)
print("The limit of the series using Raabe’s test is ", L2)
if L2>1:
print("The series is convergent by Raabe’s Test")
elif L2<1:
print("The series is divergent by Raabe’s Test ")
else:
print("Both the test fails")

Output:

3 Write a Python program to test the convergence, divergence and oscillatory of the series

2 2 4 2 4 6
+ · + · · + ...
3 3 5 3 5 7

Code:

from sympy import *


n=Symbol(’n’)
ratio=(2*n+2)/(2*n+3)
print("The ratio is", ratio)
L1=limit(ratio,n,oo)
print("The limit is", L1)
if L1<1:
print("By D’Alembert’s Rato Test the given series is convergent")
elif L1>1:

144
print("By D’Alembert’s Rato Test the given series is divergent ")
else:
print("D’Alembert’s Rato Test fails and we use Raabe’s Test for convergence")
L2=limit(((n/ratio)-n),n,oo)
print("The limit of the series using Raabe’s test is ", L2)
if L2>1:
print("The series is convergent by Raabe’s Test")
elif L2<1:
print("The series is divergent by Raabe’s Test ")
else:
print("Both the test fails")

Output:

4 Write a Python program to test the convergence, divergence and oscillatory of the series

3 3·6 3·6·9
1+ + + + ...
7 7 · 10 7 · 10 · 13

Code:

from sympy import *


n=Symbol(’n’)
ratio=(3*n+3)/(3*n+7)
print("The ratio is", ratio)
L1=limit(ratio,n,oo)
print("The limit is", L1)
if L1<1:
print("By D’Alembert’s Rato Test the given series is convergent")
elif L1>1:
print("By D’Alembert’s Rato Test the given series is divergent ")
else:
print("D’Alembert’s Rato Test fails and we use Raabe’s Test for convergence")

145
L2=limit(((n/ratio)-n),n,oo)
print("The limit of the series using Raabe’s test is ", L2)
if L2>1:
print("The series is convergent by Raabe’s Test")
elif L2<1:
print("The series is divergent by Raabe’s Test ")
else:
print("Both the test fails")

Output:

Practical Application:

Raabe’s test is used in number theory, quantum mechanics, and numerical methods. In number
theory, it helps determine the convergence of series related to prime numbers, while in quantum
mechanics, it is applied to Schrödinger’s equation. In numerical methods, it ensures the accuracy
of series-based algorithms used in computational simulations.

146
Chapter 4

Partial Differential Equations and


Integral Transforms Practical

Practical 4.1 Solution of Total Differential Equations

In this practical, we will determine whether a given total differential equation is integrable. We will
use SymPy to check the integrability condition by computing the partial derivatives of the functions
involved and verifying if the integrability condition holds. This process involves comparing the
calculated expressions to ensure they satisfy the integrability criteria for the differential equation.
Through this practical, we will have a clear understanding of how to assess the integrability of total
differential equations and the significance of the integrability condition in solving these equations.

1 Write a Python program to check whether the equation is integrable or not

3x2 dx + 3y 2 dy − x3 + y 3 + e2z dz = 0.


Manual:

Given, 3x2 dx + 3y 2 dy − x3 + y 3 + e2z dz = 0




Where P = 3x2 Q = 3y 2 R = −x3 − y 3 − e2z


     
∂Q ∂R ∂R ∂P ∂P ∂Q
P − +Q − +R − =0
∂z ∂y ∂x ∂z ∂y ∂x

147
∂P ∂Q ∂R
=0 =0 = −3y 2
∂y ∂x ∂y
∂P ∂Q ∂R
=0 =0 = −3x2
∂z ∂z ∂x
     
∂Q ∂R ∂R ∂P ∂P ∂Q
⇒P − +Q − +R −
∂z ∂y ∂x ∂z ∂y ∂x
= 3x2 3y 2 + 3y 2 −3x2 + 0
 

= 3x2 3y 2 + 3y 2 −3x2 + 0
 

= 9x2 y 2 − 9x2 y 2 = 0

∴ The given equation is integrable.

Code:

from sympy import*


x=symbols(’x’)
y=symbols(’y’)
z=symbols(’z’)
p= 3*x**2
q=3*y**2
r=(x**3+y**3+exp(2*z))
py=p.diff(y)
pz=p.diff(z)
qx=q.diff(x)
qz=q.diff(z)
rx=r.diff(x)
ry=r.diff(y)
inte=simplify(p*(qz-ry)+q*(rx-pz)+r*(py-qx))
if(inte==0):
print("The equation is integrable ")
else:
print("The equation is not integrable")

Output:

The equation is integrable

148
2 Write a Python program to check whether the equation is integrable

2x2 + 2xy + 2xz 2 + 1 dx + dy + 2zdz = 0.




Code:

from sympy import*


x=symbols(’x’)
y=symbols(’y’)
z=symbols(’z’)
p= 2*x**2+2*x*y+2*x*z**2+1
q=y**0
r=2*z
py=p.diff(y)
pz=p.diff(z)
qx=q.diff(x)
qz=q.diff(z)
rx=r.diff(x)
ry=r.diff(y)
inte=simplify(p*(qz-ry)+q*(rx-pz)+r*(py-qx))
if(inte==0):
print("The equation is integrable ")
else:
print("The equation is not integrable")

Output:

The equation is integrable

3 Write a Python program to check whether the equation is integrable

z 2 dx + z 2 − 2yz dy + 2y 2 − yz − zx dz = 0.
 

Code:

from sympy import*


x=symbols(’x’)
y=symbols(’y’)

149
z=symbols(’z’)
p= z**2
q=z**2-2*y*z
r=2*y**2-y*z-z*x
py=p.diff(y)
pz=p.diff(z)
qx=q.diff(x)
qz=q.diff(z)
rx=r.diff(x)
ry=r.diff(y)
inte=simplify(p*(qz-ry)+q*(rx-pz)+r*(py-qx))
if(inte==0):
print("The equation is integrable ")
else:
print("The equation is not integrable")

Output:

The equation is integrable.


Practical Application:

Total differential equations are used to model physical systems with multiple variables. In thermo-
dynamics, they describe the change in state variables like pressure, temperature, and volume in a
gas. In economics, they represent how multiple economic factors interact and change over time,
and in biology, they model population growth rates considering various factors.

150
Practical 4.2 Solution of Partial Differential Equations of
Type 1
In this practical, we will solve a first-order partial differential equation (PDE) by determining
the specific solution based on given conditions. We will derive the general solution of the given
PDE and then substitute values into the equation to find specific parameters. By solving for these
parameters, we will obtain the particular solution of the PDE. This practical will enhance our skills
in working with first-order PDEs and deepen our understanding of how to apply boundary or
initial conditions to find specific solutions.

1 Write a Python program to solve p2 + q 2 = 1.

Manual:

Given, p2 + q 2 = 1

let the solution be z = ax + by + c


∂z
p= =a
∂x
∂z
q= =b
∂y
a2 + b2 = 1

b2 = 1 − a2

b = ± 1 − a2
√ 
∴ z = ax + 1 − a2 y + c.

Code:

from sympy import *


x,y,p,q,a,b,c=symbols(’x y p q a b c’)
z=a*x+b*y+c
print("Let the soluition be z=",z)
eq=p**2+q**2-1
eq1=eq.subs({p:a,q:b})

151
ans=solve(eq1,b)
b_value=ans[1]
print("Let the b value be",b_value)
a1=z.subs(b,b_value)
print("z=",a1)

Output:

2 Write a Python program to solve p2 q 3 = 1.

Code:

from sympy import *


x,y,p,q,a,b,c=symbols(’x y p q a b c’)
z=a*x+b*y+c
print("Let the soluition be z=",z)
eq=p**2*q**3-1
eq1=eq.subs({p:a,q:b})
ans=solve(eq1,b)
b_value=ans[2]
print("Let the b value be",b_value)
a1=z.subs(b,b_value)
print("z=",a1)

Output:

152
3 Write a Python program to solve p = eq .

Manual:

p = eq

let the solution be z = ax + by + c


∂z
p= =a
∂x
∂z
q= =b
∂y
a = eb

Taking logarithms on both sides

log a = b

∴ z = ax + log ay + c

Code:

from sympy import *


x,y,p,q,a,b,c=symbols(’x y p q a b c’)
z=a*x+b*y+c
print("Let the soluition be z=",z)
eq=p-exp(q)
eq1=eq.subs({p:a,q:b})
ans=solve(eq1,b)
b_value=ans[0]
print("Let the b value be",b_value)
a1=z.subs(b,b_value)
print("z=",a1)

Output:

153
Practical Application:

Type 1 partial differential equations are used in heat conduction problems, such as designing
heating or cooling systems in engineering. They also describe fluid flow through porous media in
environmental science, modeling the spread of pollutants like contaminants in air or water.

154
Practical 4.3 Solution of Partial Differential Equations of
Type 2

In this practical, we will solve a second-type partial differential equation (PDE) of type 2 using
Python, which is essential for modeling various physical phenomena in fields like physics and
engineering. We will define the PDE, substitute appropriate expressions for the derivatives, and
simplify the equation to fit the standard form. By working with the PDE in terms of its derivatives,
we will apply the type 2 method to derive the general solution. This practical will not only enhance
our programming skills but also deepen your understanding of PDEs, equipping us with valuable
tools for mathematical modeling and analysis.

Write a Python program to solve z 2 p2 + q 2 + 1 = 1.



1

Manual:

Given, z 2 p2 + q 2 + 1 = 1


The given equation is of the form

f (p, q, z) = 0
dz dz
Let u = x + by, p= , q=b
du du
z 2 p2"+ q 2 + 1 = 1

 2  #
dz 2

2 dz
→z + b +1 =1
du du
2
dz 2
  
dz
→ z2 + bz 2 b2+ z2 = 1
du du
 2
dz 2
 
2 dz 2 2
→z +z b b = 1 − z2
du du
 2
2 dz
1 + b2 = 1 − z 2
 
→z
du

dz 2 1 − z 2
 
→ z. =
du 1 + b2

dz 1 − z2
→z· = √
du 1 + b2

155
dz du
→z·√ =√
1 − z2 1 + b2
Integrating on both sides
Z Z
z 1
→ √ dz = √ · du
1 − z2 1 + b2
p 1
→ − 1 − z2 = u+c
1 + b2
p  p  p
→− 1 − z2 1 + b2 = u + c 1 + b2
p  p 
→− 1 − z2 1 + b2 = u + k
p  p 
→− 1 − z2 1 + b2 = x + by + k

Squaring on both sides

→ 1 − z2 1 + b2
 

= (x + by + k)2

This is the required Solution.

Code:

from sympy import *


# Define symbols and function
x, y, z, b, u = symbols(’x y z b u’)
p, q = symbols(’p q’)
Z = Function(’z’)(u)
# Define the equation
Eq1 = Eq(z**2 * (p**2 + q**2 + 1), 1)
# Substitution of p and q in terms of derivatives with respect to u
ode = Eq1.subs([(p, Z.diff(u)), (q, b * Z.diff(u))])
# Simplify the equation to match the standard ODE form
ode = simplify(ode)
# Solve the ODE
soln = dsolve(ode, Z)
# If the solution is a list, take the first element
if type(soln) == list:
soln = soln[0]
# Substitute u back to its original expression

156
soln = soln.subs(u, x + b*y)
pprint(soln)

Output:

Write a Python program to solve p 1 + q 2 = q (z − a).



2

Code:

from sympy import *


x, y, z, b, u,p,q,a = symbols(’x y z b u p q a’)
p, q = symbols(’p q’)
Z = Function(’z’)(u)
Eq1 = Eq(p*(1+q**2),q*(z-a))
ode = Eq1.subs([(p, Z.diff(u)), (q, b * Z.diff(u))])
ode = simplify(ode)
soln = dsolve(ode, Z)
if type(soln) == list:
soln = soln[0]
soln = soln.subs(u, x + b*y)
pprint(soln)

Output:

157
3 Write a Python program to solve p (1 + q) = zq.

Code:

from sympy import *


x, y, z, b, u,p,q,a = symbols(’x y z b u p q a’)
p, q = symbols(’p q’)
Z = Function(’z’)(u)
Eq1 = Eq(p*(1+q),z*q)
ode = Eq1.subs([(p, Z.diff(u)), (q, b * Z.diff(u))])
ode = simplify(ode)
soln = dsolve(ode, Z)
if type(soln) == list:
soln = soln[0]
soln = soln.subs(u, x + b*y)
pprint(soln)

Output:

Practical Application:

Type 2 partial differential equations are important in quantum mechanics, where they model wave
functions of particles. In electromagnetics, they describe the propagation of electromagnetic waves,
and in geophysics, they model seismic wave propagation during earthquakes.

158
Practical 4.4 Solution of Partial Differential Equations of
Type 3 and Type 4

In this practical, we will learn how to solve Type 3 and Type 4 partial differential equations (PDEs),
which involve more complex forms and higher-order equations. These types of PDEs require
advanced methods for obtaining solutions, making them essential for tackling real-world problems
in various fields such as physics, engineering, and applied mathematics. By exploring these
advanced techniques, we will enhance your problem-solving skills and deepen our understanding
of the intricacies involved in working with higher-order PDEs.

1 Write a Python program to solve p2 − q 2 = x − y.

Manual:

Given, p2 − q 2 = x − y

p2 − q 2 = x − y

p2 − x = q 2 − y

f1 (p, x) = f2 (q, y) = a

p2 − x = q 2 − y = a

p2 − x = a, q2 − y = a

p2 = a + x, q2 = a + y
√ √
p= a + x, q= a+y
√ √
Substitute p = a+x and q = a + y in dz = pdx + qdy
√ √
=⇒ dz = a + x dx + a + y dy

Integrating on both sides


√ √
Z Z Z
dz = x + a dx + y + a dy

3 3
2 2
⇒ z = (x + a) 2 + (y + a) 2 + c.
3 3

159
Code:

from sympy import *


x,y,p,q,f,g,a,z=symbols(’x y p q f g a z’)
eq1=Eq((p**2-q**2),(x-y))
f1=p**2-x
f2=q**2-y
f=Eq(f1,a)
g=Eq(f2,a)
p_sol=solve(f,p)
q_sol=solve(g,q)
p=p_sol[1]
q=q_sol[1]
soln=Eq(Integral(1,z),Integral(p,x)+Integral(q,y))
soln=soln.doit()
pprint(soln)

Output:

√ √
2 Write a Python program to solve p+ q = x + y.

Code:

from sympy import *


x,y,p,q,f,g,a,z=symbols(’x y p q f g a z’)
eq1=Eq((p**(1/2)+q**(1/2)),(x+y))
f1=sqrt(p)+x
f2=sqrt(q)+y
f=Eq(f1,a)
g=Eq(f2,a)
p_sol=solve(f,p)
q_sol=solve(g,q)

160
p=p_sol[0]
q=q_sol[0]
soln=Eq(Integral(1,z),Integral(p,x)+Integral(q,y))
soln=soln.doit()
pprint(soln)

Output:

3 Write a Python program to solve p + q = sin x + sin y.

Code:

from sympy import *


x,y,p,q,f,g,a,z=symbols(’x y p q f g a z’)
eq1=Eq((p+q),sin(x)+sin(y))
f1=p-sin(x)
f2=sin(y)-q
f=Eq(f1,a)
g=Eq(f2,a)
p_sol=solve(f,p)
q_sol=solve(g,q)
p=p_sol[0]
q=q_sol[0]
soln=Eq(Integral(1,z),Integral(p,x)+Integral(q,y))
soln=soln.doit()
pprint(soln)

Output:

161
4 Write a Python program to solve z = px + qy + p2 + q 2 .

Manual:

z = px + qy + p2 + q 2

Replace p by a and q by b

z = ax + by + a2 + b2

Code:

from sympy import *


x,y,z,p,q,a,b=symbols(’x y z p q a b’)
qn=Eq(z,p*x+q*y+p**2+q**2)
soln=qn.subs([(p,a),(q,b)])
pprint(soln)

Output:

5 Write a Python program to solve z = px + qy + log pq.

Code:

from sympy import *


x,y,z,p,q,a,b=symbols(’x y z p q a b’)
qn=Eq(z,p*x+q*y+log(p*q))
soln=qn.subs([(p,a),(q,b)])
pprint(soln)

Output:

162
Practical Application:

Type 3 and Type 4 partial differential equations are applied in fluid dynamics and aerodynamics.
In meteorology, they model air mass movements and weather patterns, while in engineering, they
simulate airflow over aircraft wings for efficient design. In biology, they model the diffusion of
substances like oxygen in tissues.

163
Practical 4.5 Solution of Second order linear Partial Differ-
ential Equations with constant coefficients of
the Form, F(D,D’)=0
In this practical, we are going to learn about solving second-order linear partial differential equa-
tions (PDEs) with constant coefficients. We will focus on equations where the differential operator
F (D, D′ ) equals zero, which guides us toward finding the solution. The practical will begin with
identifying the standard form of these equations and discussing the significance of the characteristic
equation in determining the nature of the solutions. We will explore various methods for solving
these PDEs and apply these techniques to specific examples to derive general solutions. Through
this practical, we will have a solid understanding of how to approach and solve second-order
linear PDEs with constant coefficients, as well as their applications in fields such as physics and
engineering.

1 Write a Python program to find the complementary function for the equation
h i
D2 − 2DD′ + (D′ )2 z = 0.

Manual:
h i
Given, D2 − 2DD′ + (D′ )2 z=0

Substituting D as m and D’ as 1

⇒ m2 − 2m + 1 = 0

⇒ (m − 1) (m − 1) = 0

⇒ m = 1, 1

The roots are real and repeated.


The solution is:

CF = f1 (y + x) + xf2 (y + x).

164
Code:

import sympy as sp
m = sp.symbols(’m’)
eqn = m**2-2*m+1
roots = sp.solve(eqn)
if len(roots)==1:
print("CF = f1(y+", roots[0], "x) + xf2(y+", roots[0], "x)")
else:
print("CF = f1(y+", roots[0], "x) + f2(y+", roots[1], "x)")

Output:

2 Write a Python program to find the complementary function for the equation

2r − s − 3t = 0.
∂2z ∂2z ∂2z
Where r= s= t=
∂x2 ∂x∂y ∂y 2

Code:

import sympy as sp
m = sp.symbols(’m’)
eqn = 2*m**2-m-3
roots = sp.solve(eqn)
if len(roots)==1:
print("CF = f1(y+", roots[0], "x) + xf2(y+", roots[0], "x)")
else:
print("CF = f1(y+", roots[0], "x) + f2(y+", roots[1], "x)")

Output:

165
3 Write a Python program to find the complementary function for the equation
h i
D2 − 5DD′ + 4 (D′ )2 z = 0.

Code:

import sympy as sp
m = sp.symbols(’m’)
eqn = m**2-5*m+4
roots = sp.solve(eqn)
if len(roots)==1:
print("CF = f1(y+", roots[0], "x) + xf2(y+", roots[0], "x)")
else:
print("CF = f1(y+", roots[0], "x) + f2(y+", roots[1], "x)")

Output:

4 Write a Python program to find the complementary function for the equation
h i
D2 − 3DD′ + 2 (D′ )2 z = 0.
Code:

import sympy as sp
m = sp.symbols(’m’)
eqn = m**2-3*m+2
roots = sp.solve(eqn)
if len(roots)==1:
print("CF = f1(y+", roots[0], "x) + xf2(y+", roots[0], "x)")
else:
print("CF = f1(y+", roots[0], "x) + f2(y+", roots[1], "x)")

Output:

166
Practical Application:

These equations model wave propagation, such as sound or light waves, in various fields. In
mechanical engineering, they describe vibrations in structures under stress. In acoustics, they
model sound wave behavior in different environments, like concert halls or open spaces.

167
Practical 4.6 Solution of PDE when the Equation is of the
Form F(D,D’)=f(x,y)

In this practical, we are going to learn how to solve partial differential equations (PDEs) where
the operator F (D, D′ ) equals a function of x and y. These equations are more complex than those
with constant coefficients and require additional steps for finding solutions. We will explore
various techniques to tackle these PDEs, including the method of characteristics and appropriate
substitutions. By working through examples, we will develop a deeper understanding of how to
approach these more intricate equations and the strategies needed to derive their solutions.

1 Write a Python program to solve the second order PDE


h i
D2 − 2DD′ + (D′ )2 z = e2x+y .

Manual:

Given, (D2 − 2DD′ + (D′ )2 ) = e2x+y

It is of the form
f (D, D′ ) = f (x, y)

To find CF, substitute

D=m and D′ = 1

Auxiliary equation: m2 − 2m(1) + 1 = 0

⇒ m2 − 2m + 1 = 0

Solving m2 − 2m + 1 = 0

(m − 1)2 = 0

m−1=0 and m − 1 = 0

⇒ m = 1, 1

The general form is: CF = f1 (y + mx) + xf2 (y + mx)

=⇒ CF = f1 (y + x) + xf2 (y + x)

168
1
PI = f (x, y)
F (D, D′ )
1
PI = · e2x+y
D2 − 2DD′ + (D′ )2
Here a = 1, b = 2:

F (a, b) = (1, 2) = 12 − (2)(1)(2) + 22 = 1 ̸= 0


1
P I = .e2x+y
1
P I = e2x+y

The Complete Solution is CF + PI

⇒ f1 (x + y) + xf2 (x + y) + e2x+y .

Code:

import sympy as sp
D, D_prime, x, y, z, m = sp.symbols("D, D’, x, y, z, m")
f1, f2=sp.symbols("f1, f2", cls=sp.Function)
PDE=sp.Eq(D**2-2*D*D_prime+D_prime**2, sp.exp(2*x+y))
aux_eqn=sp.Eq(PDE.lhs.subs([(D, m), (D_prime, 1)]), 0)
roots=sp.solve(aux_eqn)
if len(roots) == 1:
CF=f1(y + roots[0]*x)+x*f2(y+roots[0]*x)
else:
CF=f1(y+roots[0]*x)+f2(y+roots[1]*x)
PI=(1/ PDE.lhs)*PDE.rhs
a, b=PDE.rhs.args[0].coeff(x), PDE.rhs.args[0].coeff(y)
PI=PI.subs([(D, a), (D_prime, b)])
z=CF + PI
sp.pprint(z)

Output:

169
2 Write a Python program to solve the second order PDE

∂2z ∂2x ∂2z


− 2 + = sin (2x + 3y) .
∂x2 ∂x∂y ∂y 2

Manual:

∂2z ∂2x ∂2z


The given Equation − 2 + = sin (2x + 3y) is of the form
∂x2 ∂x∂y ∂y 2
r − 2s + t = sin (2x + 3y)
 
⇒ D2 − 2DD′ + (D′ )2 z = sin (2x + 3y)

The auxilary equation is

D2 − 2DD′ + (D′ )2 = 0

m2 − 2m + 1 = 0

(m − 1)2 = 0

m−1=0 and m − 1 = 0

⇒ m = 1, 1

CF = f1 (y + mx) + xf2 (y + mx)

CF = f1 (y + x) + xf2 (y + x)
1
PI = sin (2x + 3y)
D2 − 2DD′ + (D′ )2

Substitute D2 = −a2 DD′ = −ab (D′ )2 = −b2

here a = 2 b = 3

D2 = −4 DD′ = −6 (D′ )2 = −9
1
∴ PI = sin (2x + 4y)
(−4) − 2(−6) + (−9)
1
PI = sin (2x + 3y)
−4 + 12 − 9
1
PI = sin (2x + 3y)
−1
The complete solution is CF + PI is f1 (y + mx) + xf2 (y + mx) − sin (2x + 3y) .

170
Code:

import sympy as sp
D, D_prime, x, y, z, m = sp.symbols("D, D’, x, y, z, m")
f1, f2=sp.symbols("f1, f2", cls=sp.Function)
PDE=sp.Eq(D**2-2*D*D_prime+D_prime**2, sp.sin(2*x+3*y))
aux_eqn=sp.Eq(PDE.lhs.subs([(D, m), (D_prime, 1)]), 0)
roots=sp.solve(aux_eqn)
if len(roots) == 1:
CF=f1(y + roots[0]*x)+x*f2(y+roots[0]*x)
else:
CF=f1(y+roots[0]*x)+f2(y+roots[1]*x)
PI=(1/ PDE.lhs)*PDE.rhs
a, b=PDE.rhs.args[0].coeff(x), PDE.rhs.args[0].coeff(y)
PI=PI.subs([(D**2, -(a**2)),(D*D_prime,-a*b), (D_prime**2, -(b**2))])
z=CF + PI
sp.pprint(z)

Output:

3 Write a Python program to solve the second order PDE


h i
D2 − 3DD′ + 2 (D′ )2 z = cos (3x − y).

Code:

import sympy as sp
D, D_prime, x, y, z, m = sp.symbols("D, D’, x, y, z, m")
f1, f2=sp.symbols("f1, f2", cls=sp.Function)
PDE=sp.Eq(D**2-3*D*D_prime+2*D_prime**2, sp.cos(3*x-y))
aux_eqn=sp.Eq(PDE.lhs.subs([(D, m), (D_prime, 1)]), 0)
roots=sp.solve(aux_eqn)
if len(roots) == 1:
CF=f1(y + roots[0]*x)+x*f2(y+roots[0]*x)
else:

171
CF=f1(y+roots[0]*x)+f2(y+roots[1]*x)
PI=(1/ PDE.lhs)*PDE.rhs
a,b=PDE.rhs.args[0].coeff(x), PDE.rhs.args[0].coeff(y)
PI=PI.subs([(D**2, -(a**2)),(D*D_prime,-a*b), (D_prime**2, -(b**2))])
z=CF + PI
sp.pprint(z)

Output:

Practical Application:

This type of partial differential equation is used in meteorology to represent temperature or pressure
changes in the atmosphere. In engineering, it models material deformation under stress, and in
economics, it tracks the evolution of market variables like supply and demand.

172
Practical 4.7 Finding the Laplace Transform of Standard
Functions

In this practical, we will compute the Laplace transforms of various standard functions using
Python. We will utilize the SymPy library to find the Laplace transform of functions such as
trigonometric functions, exponentials, and polynomials. This practical will involve defining
the functions in SymPy, applying the Laplace transform, and interpreting the results. Through
this practical, we will gain a solid understanding of how to compute Laplace transforms and
their significance in solving ordinary differential equations and analyzing linear systems. This
knowledge will enhance our mathematical toolkit for tackling a variety of problems in engineering
and applied mathematics.

1 Write a Python program to find the Laplace transform of emt .

Manual:

Given, emt
The definition of the Laplace transform:
Z ∞
L [f (t)] = e−st f (t)dt
0

For f (t) = emt , the Laplace transform is:


Z ∞
e−st · emt dt
 mt 
L e =
0

This expression can be simplified by combining the exponents:


Z ∞
L emt = e(m−s)t dt
 
0

Next, integrate with respect to t :


" #∞
 mt  e(m−s)t
L e =
m−s
0

Evaluate this expression at the limits t = 0 and t = ∞ :


" # " #
 mt  e∞(m−s) e0(m−s)
L e = −
m−s m−s

1
=0−
m−s

173
−1 1
L emt =
 
=
m−s s−m
Therefore, the Laplace transform of emt is:
1
L emt =
 
.
s−m

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=exp(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

2 Write a Python program to find the Laplace transform of cos mt .

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=cos(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

174
3 Write a Python program to find the Laplace transform of sin mt.

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=sin(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

4 Write a Python program to find the Laplace transform of cosh mt.

Manual:

Given, cosh mt
The hyperbolic cosine function is defined as:
emt + e−mt
cosh(mt) =
2
So, the Laplace transform of cosh(mt) is:
e + e−mt
 mt 
L [cosh(mt)] = L
2
This can be broken down into two separate Laplace transforms:
1   mt 
+ L e−mt
 
L [cosh(mt)] = L e
2
 
1 1 1
L [cosh(mt)] = +
2 s−m s+m
1 (s + m) + (s − m)
L [cosh(mt)] = ·
2 (s − m)(s + m)
1 2s
L [cosh(mt)] = ·
2 s2 − m2
s
∴ L [cosh(mt)] = 2
s − m2

175
Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=cosh(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

5 Write a Python program to find the Laplace transform of sinh mt.

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=sinh(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

6 Write a Python program to find the Laplace transform of tn .

Code:

from sympy import *


a,s,t,n=symbols (’a s t n’)

176
trans_fun=t**n
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

7 Write a Python program to find the Laplace transform of eat tn .

Code:

from sympy import *


a,s,t,n=symbols (’a s t n’)
trans_fun=exp(a*t)*t**n
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

8 Write a Python program to find the Laplace transform of eat cos mt.

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=exp(a*t)*cos(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

177
Output:

9 Write a Python program to find the Laplace transform of eat sin mt.

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=exp(a*t)*sin(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

10 Write a Python program to find the Laplace transform of eat cosh mt.

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=exp(a*t)*cosh(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

178
11 Write a Python program to find the Laplace transform of eat sinh mt.

Code:

from sympy import *


a,s,t,m=symbols (’a s t m’)
trans_fun=exp(a*t)*sinh(m*t)
F=laplace_transform(trans_fun, t, s, noconds=True)
pprint(F)

Output:

Practical Application:

The Laplace transform is essential in control systems engineering for analyzing dynamic systems,
such as electrical circuits and mechanical systems. In signal processing, it converts differential
equations into algebraic ones, simplifying system behavior analysis in the frequency domain.

179
Practical 4.8 Finding the Inverse Laplace Transform of Stan-
dard Functions
In this practical, we will compute the inverse Laplace transforms of standard functions using Python.
Utilizing the SymPy library, we will obtain the inverse Laplace transforms of various functions,
including rational functions, trigonometric functions, and exponentials. This practical will involve
defining the functions in SymPy, applying the inverse Laplace transform, and interpreting the
results. Through this practical, we will gain a solid understanding of how to compute inverse
Laplace transforms and their importance in solving differential equations and analyzing dynamic
systems.

1
1 Write a Python program to find the inverse Laplace transform of .
s

Code:

import sympy as sp
a,s,t=symbols (’a s t’)
expression=1/s
F=sp.inverse_laplace_transform(expression, s, t)
pprint(F)

Output:

1
2 Write a Python program to find the inverse Laplace transform of .
s2

Code:

import sympy as sp
a,s,t=symbols (’a s t’)
expression=1/s**2
F=sp.inverse_laplace_transform(expression, s, t)
pprint(F)

180
Output:

1
3 Write a Python program to find the inverse Laplace transform of .
s3

Code:

import sympy as sp
a,s,t=symbols (’a s t’)
expression=1/s**3
F=sp.inverse_laplace_transform(expression, s, t)
pprint(F)

Output:

1
4 Write a Python program to find the inverse Laplace transform of .
sn

Code:

import sympy as sp
a,s,t,n=symbols (’a s t n’)
expression=1/s**n
F=sp.inverse_laplace_transform(expression, s, t)
pprint(F)

Output:

181
1
5 Write a Python program to find the inverse Laplace transform of .
s−k

Code:

import sympy as sp
a,s,t=symbols (’a s t’)
k=sp.symbols(’k’, real=true)
expression=1/(s-k)
F=sp.inverse_laplace_transform(expression, s, t)
pprint(F)

Output:

s
6 Write a Python program to find the inverse Laplace transform of .
s2 + a2

Code:

import sympy as sp
a,s,t=symbols (’a s t’)
a=sp.symbols(’a’, real=true)
expression=s/(s**2+a**2)
F=sp.inverse_laplace_transform(expression, s, t)
pprint(F)

Output:

Practical Application:

The inverse Laplace transform is used to solve for time-domain responses in electrical circuits, such
as voltage and current after a disturbance. In control theory, it helps determine system behaviors
and ensures the design of stable and efficient systems.

182
Practical 4.9 Plotting of Periodic Functions
In this practical, we will plot a periodic function using Matplotlib. We will generate values for the
function over a specified range and visualize it on a graph with customized axes and spines. This
practical involves setting up the plot, configuring axis properties, and displaying the function to
analyze its periodic behavior. Through this practical, we will enhance our skills in data visualization
and gain insights into the characteristics of periodic functions, which are essential in various fields
such as signal processing and physics.

1 Write a Python program to plot the graph of periodic function f (x) = sin x in [−2π, 2π ].

Code:

import matplotlib.pyplot as plt


import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y= np.sin(x)
fig=plt.figure()
ax=fig.add_subplot(1, 1, 1)
ax.spines[’left’].set_position(’center’)
ax.spines[’bottom’].set_position(’center’)
ax.spines[’right’].set_color(’none’)
ax.spines[’top’].set_color(’none’)
ax.xaxis.set_ticks_position(’bottom’)
ax.yaxis.set_ticks_position(’left’)
plt.plot(x,y, ’b’)
plt.show()

183
Output:

2 Write a Python program to plot the graph of periodic function f (x) = tan x in [−π, π ].

Code:

import matplotlib.pyplot as plt


import numpy as np
x = np.linspace(-np.pi,np.pi, 100)
y= np.tan(x)
fig=plt.figure()
ax=fig.add_subplot(1, 1, 1)
ax.spines[’left’].set_position(’center’)
ax.spines[’bottom’].set_position(’center’)
ax.spines[’right’].set_color(’none’)
ax.spines[’top’].set_color(’none’)
ax.xaxis.set_ticks_position(’bottom’)
ax.yaxis.set_ticks_position(’left’)
plt.plot(x,y, ’b’)
plt.show()

184
Output:

3 Write a Python program to plot the graph of periodic function f (x) = cos x in [−2π, 2π ].

Code:

import matplotlib.pyplot as plt


import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y= np.cos(x)
fig=plt.figure()
ax=fig.add_subplot(1, 1, 1)
ax.spines[’left’].set_position(’center’)
ax.spines[’bottom’].set_position(’center’)
ax.spines[’right’].set_color(’none’)
ax.spines[’top’].set_color(’none’)
ax.xaxis.set_ticks_position(’bottom’)
ax.yaxis.set_ticks_position(’left’)
plt.plot(x,y, ’b’)
plt.show()

Output:

185
Practical Application:

Plotting periodic functions is crucial in signal processing, where they represent waveforms used in
communication systems. In physics, they describe oscillatory phenomena, like pendulum motion
or string vibrations. In electrical engineering, they help analyze alternating current (AC) signals for
power system and circuit design.

186
Practical 4.10 Solution of ODE using Lapalace Transform

In this practical, we will solve an ordinary differential equation (ODE) using the Laplace transform.
We will convert the ODE into an algebraic equation by applying the Laplace transform, allowing
us to solve for the transformed variable. After obtaining the solution in the transformed domain,
we will use the inverse Laplace transform to find the solution in the time domain. Additionally, we
will incorporate initial conditions into the solution process to ensure that our final result is specific
to the given problem. By the end of this practical, we will have a comprehensive understanding of
how to apply the Laplace transform method to solve ODEs effectively.

1 Write a Python program to solve an ode using Laplace transform y ′ − 5y = e5x given
y(0) = 2.

Manual:

The given ODE is y ′ − 5y = e5x

Taking Laplace transform on both sides

L [y ′ ] − 5L [y] = L e5x
 

Use the following Laplace transform properties in the given equation:

L {y ′ } = sY (s) − y(0)

L{y} = Y (s)
1
L {eax } =
s−a
We get
1
sY (s) − y(0) − 5Y (s) =
s−5
1
(s − 5)Y (s) − 2 =
s−5
Given y(0) = 2
1 2
Y (s) = +
(s − 5)(s − 5) s − 5
1 2
Y (s) = 2
+
(s − 5) s−5
Now, take the inverse Laplace transform to find y(x):

187
   
1 1
y(x) = L−1 + 2 L−1
(s − 5)2 s−5
y(x) = xe5x + 2e5x

∴ y (x) = (x + 2) e5x .

Code:

from sympy import *


x,s=symbols(’x s’)
y=Function(’y’)
Y=laplace_transform(y(x),x,s)[0]
diff_eqn=Eq(Derivative(y(x),x)-5*y(x),exp(5*x))
initial={y(0):2}
alg_eqn=Eq(laplace_transform(diff_eqn.lhs,x,s)[0].subs(initial),
laplace_transform(diff_eqn.rhs,x,s,noconds=True))
soln=inverse_laplace_transform(solve(alg_eqn,Y)[0],s,x)
pprint(soln)

Output:

2 Write a Python program to solve an ode using Laplace transform 9y ′′ − 6y ′ + y = 0 given

y(0) = 3, y ′ (0) = 1.

Code:

from sympy import *


t,s=symbols(’t s’)
y=Function(’y’)
Y=laplace_transform(y(t),t,s)[0]
diff_eqn=Eq(9*diff(y(t),t,2)-6*diff(y(t),t)+y(t),0)
initial={y(0): 3, diff(y(t),t):1}
alg_eqn=Eq(laplace_transform(diff_eqn.lhs,t,s)[0].subs(initial),
laplace_transform(diff_eqn.rhs,t, s, noconds=True))

188
soln=inverse_laplace_transform(solve(alg_eqn,Y)[0],s,t)
pprint(soln)

Output:

Practical Application:

The Laplace transform is widely used to solve ordinary differential equations in electrical and
mechanical systems. It helps solve for current and voltage in circuits and models the motion of
objects under varying forces. In control systems, it analyzes system stability and response to inputs,
aiding in controller design.

189
190
Appendices

General Numerical Types


• N: Natural Numbers — Positive integers starting from 1.

• W: Whole Numbers — Non-negative integers starting from 0.

• Z: Integers — All whole numbers, positive and negative, including zero.


a
• Q: Rational Numbers — Numbers that can be expressed as a fraction , where a and b are
b
integers and b ̸= 0.

• R: Real Numbers — Includes all rational and irrational numbers.

• C: Complex Numbers — Numbers of the form a + bi, where a and b are real numbers and i is
the imaginary unit.

Python Libraries and Functions


• SymPy: A library for symbolic mathematics.

• NumPy: A library for numerical computing.

• print: Used to display output.

• pprint: Pretty-prints complex data structures for readability.

• len: Returns the length of an object (e.g., string, list).

• range: Generates a sequence of numbers, often used in loops.

• abs: Returns the absolute value of a number.

191
• round: Rounds a floating-point number to a specified number of decimal places.

• max, min: Returns the maximum or minimum value from a list or set.

• def functionname(Parameters): Defines a particular function.

Algebra I and Calculus I


• Matrix: Used to create matrices.

• trace: Computes the trace of a matrix.

• transpose: Computes the transpose of a matrix.

• rref: Computes the Reduced Row Echelon Form (RREF) of a matrix.

• rank: Computes the rank of a matrix.

• eigenvects: Finds the eigenvalues, algebraic multiplicities, and eigenvectors of a matrix.

• LUsolve: Solves a system of linear equations using LU decomposition.

• det: Computes the determinant of a matrix.

• eye: Creates an identity matrix of specified dimensions.

• diff: Computes derivatives of expressions.

• series: Computes a series expansion of an expression.

• polar: Creates a polar plot using matplotlib.

• plot implicit: Plots implicit equations using sympy.plotting.

• input: Reads input from the user as a string.

• pprint: Pretty-prints matrices or expressions for better readability.

• linspace: Generates linearly spaced values within a specified range.

192
Algebra II and Calculus II
• Matrix: Used to create matrices.

• zeros: Creates a matrix of zeros with specified dimensions.

• pprint: Pretty-prints matrices or expressions for better readability.

• symbols: Declares symbolic variables for symbolic computation.

• diff: Computes the derivative of an expression with respect to a variable.

• isprime: Checks if a number is a prime number.

• homogeneous order: Determines the degree of homogeneity of an expression.

• simplify: Simplifies an expression to its most reduced form.

• binomial: Computes the binomial coefficient (n, k).

• factorial: Computes the factorial of a number.

• subs: Substitutes values or expressions for variables in an expression.

• init printing: Initializes pretty printing for symbolic output.

• integrate: Computes definite or indefinite integrals of expressions.

• exp: Represents the exponential function ex .

• sin: Computes the sine of an angle or expression.

• cos: Computes the cosine of an angle or expression.

• line integral: Evaluates a line integral of a vector field.

• set: Creates a set, a collection of unique elements.

• update: Adds elements to a set.

• len: Returns the number of elements in a set or list.

• print: Displays text or values to the console.

• exit: Terminates the program execution.

193
• modulo (%): Computes the remainder of a division operation.

• for loop: Iterates over a range or collection.

• if: Evaluates a condition and executes code if the condition is true.

Ordinary Differential Equations and Partial Fractions


• Symbol: Declares symbolic variables for symbolic computation.

• integrate: Computes definite or indefinite integrals of expressions.

• simplify: Simplifies an expression to its most reduced form.

• homogeneous order: Determines the degree of homogeneity of an expression or differential


equation.

• subs: Substitutes values or expressions for variables in a given expression.

• exp: Represents the exponential function ex .

• atan: Computes the arctangent of a number or expression.

• pprint: Pretty-prints symbolic expressions or solutions for better readability.

• roots: Finds the roots of a polynomial equation.

• dsolve: Solves ordinary differential equations symbolically.

• Derivative: Represents the derivative of a function with respect to a variable.

• Lambda: Defines a symbolic lambda function for reusable expressions.

• factorial: Computes the factorial of a given number.

• limit: Computes the limit of an expression as a variable approaches a value or infinity.

• limit seq: Finds the limit of a sequence as the index approaches infinity.

• Rational: Represents rational numbers in their reduced form.

• oo: Represents infinity in symbolic computations.

• exp: Computes the exponential function ex .

• abs: Computes the absolute value of a number or expression.

194
Partial Differential Equations and Integran Transformations
• Eq: Defines an equation between two symbolic expressions.

• diff: Computes the derivative of a function with respect to a variable.

• subs: Substitutes values or expressions for variables in a given expression.

• simplify: Simplifies an expression to its most reduced form.

• solve: Solves an equation for a given symbol or expression.

• Integral: Represents the integral of an expression with respect to a variable.

• pprint: Pretty-prints symbolic expressions or solutions for better readability.

• dsolve: Solves an ordinary differential equation symbolically.

• Function: Represents a function of a symbolic variable.

• laplace transform: Computes the Laplace transform of a function.

• inverse laplace transform: Computes the inverse Laplace transform of a function.

• exp: Represents the exponential function ex .

• sin: Represents the sine function.

• coeff: Extracts the coefficient of a specific variable in an expression.

• pprint: Pretty-prints the solution of symbolic equations.

• exp: Computes the exponential function ex .

• plot: Plots a function using the Matplotlib library.

• line: Plots a line or curve based on the given x and y values.

195

You might also like