0% found this document useful (0 votes)
38 views42 pages

LAB RECORD PYTHON - Merged

The document discusses three Python programming labs on basic concepts, matrix operations, and matrix transpose. Lab 1 covers basics like conversions, triangle constructions, and factorials. Lab 2 covers matrix addition and multiplication. Lab 3 covers transpose of even and uneven matrices.

Uploaded by

yoshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views42 pages

LAB RECORD PYTHON - Merged

The document discusses three Python programming labs on basic concepts, matrix operations, and matrix transpose. Lab 1 covers basics like conversions, triangle constructions, and factorials. Lab 2 covers matrix addition and multiplication. Lab 3 covers transpose of even and uneven matrices.

Uploaded by

yoshita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

LAB RECORD

2022-23

Python Programming for Mathematics


MAT351

Name: Yoshita Lakka Register Number: 2140897


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

LAB 1
19 July 2022

Topic: Basics of Python Programming

Aim: to learn and understand the basics of python programming

In [3]: 

1 #Conversion of celsius and farenheit


2 x= float(input("Enter temperature: "))
3 u= str(input("Enter unit: "))
4 if u=="Celsius" :
5 f= 1.8*x +32
6 print("Temperature", x, "in",u, "is", f, "in Farenheit")
7 else:
8 c= 0.55*(x-32)
9 print("Temperature", x, "in",u, "is", c, "in Celsius")

Enter temperature: 66

Enter unit: Celsius

Temperature 66.0 in Celsius is 150.8 in Farenheit

In [4]: 

1 #Construction of inverted triangle


2 rows = int(input("Enter the number of rows: "))
3 for i in range(rows + 1, 0, -1):
4 for j in range(0, i - 1):
5 print("*", end=' ')
6 print(" ")

Enter the number of rows: 4

* * * *

* * *

* *

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 1/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [5]: 

1 #Construction of diamond
2 rows = int(input("Enter the number of rows: "))
3 # It is used to print the space
4 k = 2 * rows - 2
5 # Outer loop to print number of rows
6 for i in range(0, rows):
7 # Inner loop is used to print number of space
8 for j in range(0, k):
9 print(end=" ")
10 # Decrement in k after each iteration
11 k = k - 1
12 # This inner loop is used to print stars
13 for j in range(0, i + 1):
14 print("* ", end="")
15 print("")
16
17 #Downward triangle Pyramid
18 # It is used to print the space
19 k = rows - 2
20 # Output for downward triangle pyramid
21 for i in range(rows, -1, -1):
22 # inner loop will print the spaces
23 for j in range(k, 0, -1):
24 print(end=" ")
25 # Increment in k after each iteration
26 k = k + 1
27 # This inner loop will print number of stars
28 for j in range(0, i + 1):
29 print("* ", end="")
30 print("")
31

Enter the number of rows: 7

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

* * * * * * * *

* * * * * * *

* * * * * *

* * * * *

* * * *

* * *

* *

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 2/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [6]: 

1 #factorial of a number
2 num= int(input("Enter the number: "))
3 factorial= 1
4 if num<0:
5 print("Factorial cant be computed")
6 elif num==0:
7 print("Factorial is 1")
8 else:
9 for i in range(1, num + 1):
10 factorial= factorial*i
11 print("Factorial of", num, "is" ,factorial)

Enter the number: 6

Factorial of 6 is 720

Conclusion: learnt and understood the basics of python programming

LAB 2
26 July 2022

Topic: Operations of Matrices and Determinants

Aim: to learn and understand the operations on matrices and determinants like
addition and multiplication

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 3/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [1]: 

1 #Question 1: Write a python program to find addition of two matrices


2 #Question 2: Write a python program to find multiplication of two matrices
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix1= []
6 for i in range(a):
7 c=[]
8 for j in range(b):
9 j= int(input("Enter the elememts for first matrix: "))
10 c.append(j)
11 matrix1.append(c)
12 matrix2=[]
13 for i in range(a):
14 d=[]
15 for j in range(b):
16 j= int(input("Enter the element for second matrix: "))
17 d.append(j)
18 matrix2.append(d)
19 print("First Matrix...")
20 for i in range(a):
21 for j in range(b):
22 print(matrix1[i][j], end=" ")
23 print()
24 print("Second Matrix...")
25 for i in range(a):
26 for j in range(b):
27 print(matrix2[i][j], end=" ")
28 print()
29 result1=[[0,0,0], [0,0,0], [0,0,0]]
30 for i in range(a):
31 for j in range(b):
32 result1[i][j]= matrix1[i][j]+ matrix2[i][j]
33 print("Result matrix addition")
34 for i in range(a):
35 for j in range(b):
36 print(result1[i][j], end=" ")
37 print()
38 result2=[[0,0,0], [0,0,0], [0,0,0]]
39 for i in range(a):
40 for j in range(b):
41 result2[i][j]= matrix1[i][j]* matrix2[i][j]
42 print("Result matrix multiplication")
43 for i in range(a):
44 for j in range(b):
45 print(result2[i][j], end=" ")
46 print()

Enter the number of rows: 2

Enter the number of columns: 2

Enter the elememts for first matrix: 1

Enter the elememts for first matrix: 2

Enter the elememts for first matrix: 3

Enter the elememts for first matrix: 4

Enter the element for second matrix: 2

Enter the element for second matrix: 5

Enter the element for second matrix: 6

Enter the element for second matrix: 7

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 4/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

First Matrix...

1 2

3 4

Second Matrix...

2 5

6 7

Result matrix addition

3 7

9 11

Result matrix multiplication

2 10

18 28

Conclusion: learnt and understood the operations on matrices and determinants


like addition and multiplication

LAB 3
27 July 2022

Topic: Operations of Matrices

Aim: to learn and understand the operations on matrices and determinants like
transpose

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 5/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [1]: 

1 #Question 3: Write the transpose of a matrix


2 #For even matrix
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c=[]
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 result= [[0 for i in range(a)] for j in range(b)]
18 for i in range(a):
19 for j in range(b):
20 result[i][j]= matrix[j][i]
21 print("Transpose matrix is: ")
22 for i in range(a):
23 for j in range(b):
24 print(result[i][j], end= " ")
25 print()

Enter the number of rows: 2

Enter the number of columns: 2

Enter the elements of matrix: 1

Enter the elements of matrix: 2

Enter the elements of matrix: 3

Enter the elements of matrix: 4

Your Matrix is:

1 2

3 4

Transpose matrix is:

1 3

2 4

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 6/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [1]: 

1 ## For uneven matrix


2 a= int(input("Enter the number of rows: "))
3 b= int(input("Enter the number of columns: "))
4 matrix= []
5 for i in range(a):
6 c=[]
7 for j in range(b):
8 j= int(input("Enter the elements of matrix: "))
9 c.append(j)
10 matrix.append(c)
11 print("Your Matrix is: ")
12 for i in range(a):
13 for j in range(b):
14 print(matrix[i][j], end=" ")
15 print()
16 result= [[0 for i in range(a)] for j in range(b)]
17 for i in range(a):
18 for j in range(b):
19 result[j][i]= matrix[i][j]
20 print("Transpose matrix is: ")
21 for j in range(b):
22 for i in range(a):
23 print(result[j][i], end= " ")
24 print()

Enter the number of rows: 2

Enter the number of columns: 3

Enter the elements of matrix: 1

Enter the elements of matrix: 2

Enter the elements of matrix: 3

Enter the elements of matrix: 4

Enter the elements of matrix: 5

Enter the elements of matrix: 6

Your Matrix is:

1 2 3

4 5 6

Transpose matrix is:

1 4

2 5

3 6

Conclusion: learnt and understood the operations on matrices and determinants


like transpose

LAB 4
2 August 2022

Topic: Operations of Matrices

Aim: to learn and understand the operations on matrices like determinant and
inverse
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 7/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [4]: 

1 #Question: Write a python program to find the determinant of a matrix


2 #Determinate of a matrix
3 import numpy as np
4 a= int(input("Enter the number of rows: "))
5 b= int(input("Enter the number of columns: "))
6 matrix= []
7 for i in range(a):
8 c= []
9 for j in range(b):
10 j= int(input("Enter the elements of matrix: "))
11 c.append(j)
12 matrix.append(c)
13 print("Your Matrix is: ")
14 for i in range(a):
15 for j in range(b):
16 print(matrix[i][j], end=" ")
17 print()
18 print("Determinate of your matrix is: ")
19 Det1= np.linalg.det(matrix)
20 print(float(Det1))

Enter the number of rows: 2

Enter the number of columns: 2

Enter the elements of matrix: 4

Enter the elements of matrix: 6

Enter the elements of matrix: 3

Enter the elements of matrix: 2

Your Matrix is:

4 6

3 2

Determinate of your matrix is:

-10.000000000000002

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 8/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [1]: 

1 #Inverse of a matrix
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Inverse of your matrix is: ")
18 Det1= np.linalg.inv(matrix)
19 print(Det1)

Enter the number of rows: 2

Enter the number of columns: 2

Enter the elements of matrix: 2

Enter the elements of matrix: 3

Enter the elements of matrix: 4

Enter the elements of matrix: 1

Your Matrix is:

2 3

4 1

Inverse of your matrix is:

[[-0.1 0.3]

[ 0.4 -0.2]]

Conclusion: learnt and understood the operations on matrices and determinants


like determinant and inverse

LAB 5
3 August 2022

Topic: System of Homogeneous and Non-Homogenous Linear


Equations

Aim: to learn and understand the System of Homogeneous and Non-


Homogenous Linear Equations

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 9/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [3]: 

1 #System of homogenrous and non homogeneous linear equations


2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Enter your column matrix: ")
18 matrix1=[]
19 for i in range(1):
20 B=[]
21 for j in range(b):
22 j= int(input("Enter the Elements of the matrix: "))
23 B.append(j)
24 matrix1.append(B)
25 print("Your column matrix is: ")
26 for j in range(b):
27 for i in range(1):
28 print(matrix[i][j], end=" ")
29 print()
30 X= np.linalg.solve(matrix, B)
31 print("Solution of your system of equation")
32 print(X)

Enter the number of rows: 3

Enter the number of columns: 3

Enter the elements of matrix: 1

Enter the elements of matrix: 1

Enter the elements of matrix: 2

Enter the elements of matrix: 3

Enter the elements of matrix: 4

Enter the elements of matrix: 8

Enter the elements of matrix: -2

Enter the elements of matrix: 2

Enter the elements of matrix: 1

Your Matrix is:

1 1 2

3 4 8

-2 2 1

Enter your column matrix:

Enter the Elements of the matrix: 3

Enter the Elements of the matrix: 10

Enter the Elements of the matrix: 7

Your column matrix is:

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 10/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

Solution of your system of equation

Conclusion: learnt and understood the System of Homogeneous and Non-


Homogenous Linear Equations

LAB 6
10 August 2022

Topic: Operations of Matrices

Aim: to learn and understand the operations on matrices like rank

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 11/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [1]: 

1 #Write a python program to find the rank of a given matrix (square matrix)
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Determinate of your matrix is: ")
18 Det1= np.linalg.det(matrix)
19 print(float(Det1))
20 print("Rank of your matrix is: ")
21 rank= np.linalg.matrix_rank(matrix)
22 print(rank)

Enter the number of rows: 4

Enter the number of columns: 4

Enter the elements of matrix: 1

Enter the elements of matrix: 2

Enter the elements of matrix: 3

Enter the elements of matrix: 0

Enter the elements of matrix: 2

Enter the elements of matrix: 4

Enter the elements of matrix: 3

Enter the elements of matrix: 2

Enter the elements of matrix: 3

Enter the elements of matrix: 2

Enter the elements of matrix: 1

Enter the elements of matrix: 3

Enter the elements of matrix: 6

Enter the elements of matrix: 8

Enter the elements of matrix: 7

Enter the elements of matrix: 5

Your Matrix is:

1 2 3 0

2 4 3 2

3 2 1 3

6 8 7 5

Determinate of your matrix is:

2.664535259100386e-15

Rank of your matrix is:

Conclusion: learnt and understood the operations on matrices like rank

LAB 7
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 12/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

16 August 2022

Topic: Operations of Matrices

Aim: to learn and understand the operations on matrices like rank

In [4]: 

1 #Write a python program to find the rank of a given matrix (non square matrix)
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Rank of your matrix is: ")
18 rank= np.linalg.matrix_rank(matrix)
19 print(rank)

Enter the number of rows: 4

Enter the number of columns: 3

Enter the elements of matrix: 1

Enter the elements of matrix: -7

Enter the elements of matrix: 15

Enter the elements of matrix: 2

Enter the elements of matrix: 3

Enter the elements of matrix: -4

Enter the elements of matrix: 3

Enter the elements of matrix: -4

Enter the elements of matrix: 11

Enter the elements of matrix: 5

Enter the elements of matrix: -1

Enter the elements of matrix: 7

Your Matrix is:

1 -7 15

2 3 -4

3 -4 11

5 -1 7

Conclusion: learnt and understood the operations on matrices like rank

LAB 8
17 August 2022

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 13/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

Topic: System of Homogeneous and Non-Homogenous Linear


Equations

Aim: to learn and understand the System of Homogeneous and Non-


Homogenous Linear Equations

Note:

P(A)= P(A:B) then r=n (unique solution)


P(A)= P(A:B) then r<n (infinite solution)
P(A)= P(A:B) then no solution where r is rank of A and rank of A:B and n is the number of variables

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 14/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [3]: 

1 # Write a python program to solve a system of homogenous and non-homogenous systems of


2 #System of homogenous and non homogenous linear equations
3 import numpy as np
4 a= int(input("Enter the number of rows: "))
5 b= int(input("Enter the number of columns: "))
6 matrix= []
7 for i in range(a):
8 c= []
9 for j in range(b):
10 j= int(input("Enter the elements of matrix: "))
11 c.append(j)
12 matrix.append(c)
13 print("Your Matrix is: ")
14 for i in range(a):
15 for j in range(b):
16 print(matrix[i][j], end=" ")
17 print()
18 print("Enter your column matrix: ")
19 matrix1=[]
20 for i in range(1):
21 B=[]
22 for j in range(b):
23 j= int(input("Enter the Elements of Matrix: "))
24 B.append(j)
25 matrix1.append(B)
26 print("Your column matrix is: ")
27 for j in range(b):
28 for i in range(1):
29 print(matrix[i][j], end=" ")
30 print()
31 x=np.linalg.solve(matrix, B)
32 print("Solution of your system of equation: ")
33 print(x)

Enter the number of rows: 3

Enter the number of columns: 3

Enter the elements of matrix: 1

Enter the elements of matrix: 2

Enter the elements of matrix: -1

Enter the elements of matrix: 3

Enter the elements of matrix: -1

Enter the elements of matrix: 2

Enter the elements of matrix: 2

Enter the elements of matrix: -2

Enter the elements of matrix: 3

Your Matrix is:

1 2 -1

3 -1 2

2 -2 3

Enter your column matrix:

Enter the Elements of Matrix: 3

Enter the Elements of Matrix: 1

Enter the Elements of Matrix: 2

Your column matrix is:

-1

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 15/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

Solution of your system of equation:

[-1. 4. 4.]

Conclusion: learnt and understood the System of Homogeneous and Non-


Homogenous Linear Equations

LAB 9
23 August 2022

Topic: Eigenvalue and Eigenvector of Matrix

Aim: to learn and understand the concept of Eigenvalue and Eigenvector of


Matrix

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 16/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [4]: 

1 #Write a python program to find the eigenvalue and eigenvector of a matrix [A-lambda(I)
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Determinate of your matrix is: ")
18 Det1= np.linalg.det(matrix)
19 print(float(Det1))
20 result= [[0 for i in range(a)]for j in range(b)]
21 Inv1=np.linalg.inv(matrix)
22 result.append(Inv1)
23 print("Inverse matrix is: ")
24 for i in range(a):
25 for j in range(b):
26 print(float(Inv1[i][j]), end=" ")
27 print()
28 print("Eigenvalues: ")
29 Eigen1=np.linalg.eigvals(matrix)
30 print(Eigen1)
31

Enter the number of rows: 2

Enter the number of columns: 2

Enter the elements of matrix: 4

Enter the elements of matrix: 1

Enter the elements of matrix: -1

Enter the elements of matrix: 2

Your Matrix is:

4 1

-1 2

Determinate of your matrix is:

8.999999999999998

Inverse matrix is:

0.2222222222222222 -0.1111111111111111

0.1111111111111111 0.4444444444444444

Eigenvalues:

[3.00000001 2.99999999]

Conclusion: learnt and understood the concept of Eigenvalue and Eigenvector


of Matrix

LAB 10

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 17/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

30 August 2022

Topic: Using fsolve to perform various actions

Aim: To learn and understand fsolve and to perform various actions

In [1]: 

1 import numpy as np
2
3 import matplotlib.pyplot as plt
4
5 %matplotlib inline
6
7 from scipy.optimize import fsolve

In [2]: 

1 from scipy.optimize import fsolve


2
3 def f(x):
4
5 return x**2 - 5
6
7 x=fsolve(f,2)
8
9 print(x,f(x))

[2.23606798] [-1.77635684e-15]

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 18/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [3]: 

1 def f(xyz):
2
3 x=xyz[0]
4
5 y=xyz[1]
6
7 z=xyz[2]
8
9
10
11 f0=x**2+y**2-1
12
13 f1=x*y+y*z+1.1
14
15 f2=y**2+z**2-2
16
17
18
19 return np.array([f0,f1,f2])
20
21
22
23 xyz0=np.array([2.0,2,2])
24
25
26
27 xyz=fsolve(f,xyz0)
28
29 x=xyz[0]
30
31 y=xyz[1]
32
33 z=xyz[2]
34
35
36
37 print(x,y,z,f(xyz))

0.10056088509586326 -0.9949309063391776 1.0050435272221339 [ 1.60760294e-13


-1.38555833e-13 5.32907052e-15]

Conclusion: learnt and understoodd fsolve and to perform various actions

LAB 11
6 September 2022

Topic: Using fsolve to perform various actions

Aim: To learn and understand fsolve and to perform various actions

1. Graphically represent the equation 𝑦 = 2.0 ∗ 𝑥2 + 3.0 ∗ 𝑥 − 10.0


localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 19/41
22/11/2022, 22:19
𝑦 Semester 3 Python - Jupyter Notebook

In [11]: 

1 import numpy as np
2 import matplotlib.pyplot as plt
3 from scipy.optimize import fsolve
4 def f(x):
5 y=2.0* x**2 + 3.0*x-10.0
6 return y
7 x= np.linspace(-5, 3)
8 plt.plot(x, f(x))
9 plt.plot(x, np.zeros(len(x)))
10 plt.show

Out[11]:

<function matplotlib.pyplot.show(close=None, block=None)>

2. Solve the function 𝑧 = 𝑥2 + 𝑦2 − 20


In [12]: 

1 def myFunction(z):
2 x=z[0]
3 y=z[1]
4 w=z[2]
5
6 F=np.empty((3))
7 F[0]=x**2+y**2-20
8 F[1]= y - x**2
9 F[2]= w+ 5 - x*y
10 return F
11 zGuess = np.array([1,1,1])
12 z = fsolve(myFunction, zGuess)
13 print(z)

[2. 4. 3.]

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 20/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [15]: 

1 import numpy as np
2 from scipy.optimize import fsolve
3 def myFunction(xyz):
4 x=xyz[0]
5 y=xyz[1]
6 w=xyz[2]
7
8 F=np.empty((3))
9 F[0]=x**2+y**2-1
10 F[1]= xy + y**2 + 1.1
11 F[2]= y**2 + z**2 -2
12 return np.array([f0, f1, f2])
13 xyz0= np.array([2.0,2,2])
14 xyz = fsolve(f, xyz0)
15
16 print(xyz, f(xyz))

[1.60849528 1.60849528 1.60849528] [1.38555833e-13 1.38555833e-13 1.38555833


e-13]

In [14]: 

1 import numpy as np
2 from scipy.optimize import *
3 def myFunction(xyz):
4 x=xyz[0]
5 y=xyz[1]
6 w=xyz[2]
7
8 F=np.empty((3))
9 F[0]=x**2+y**2-1
10 F[1]= xy + y**2 + 1.1
11 F[2]= y**2 + z**2 -2
12 return np.array([f0, f1, f2])
13 xyz0= np.array([2.0,2,2])
14 xyz = fsolve(f, xyz0)
15 x= xyz[0]
16 y= xyz[1]
17 w= xyz[2]
18 print(xyz, f(xyz))

[1.60849528 1.60849528 1.60849528] [1.38555833e-13 1.38555833e-13 1.38555833


e-13]

Conclusion: learnt and understoodd fsolve and to perform various actions

LAB 12
7 September 2022

Topic: Infinite Series


localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 21/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

Aim: to learn and understand the concept of mathematical infinite series

Series
A series is a sum of a sequence of terms. That is, a series is a list of numbers with addition operations between
them.

∞ 𝑥𝑛
1.

𝑛=1 𝑛

In [2]: 

1 n= int(input("Enter the number of terms: "))


2 x= int(input("Enter the value of x: "))
3 sum1=1
4 for i in range(2, n+1):
5 sum1= sum1+((x**i)/i)
6 print("The sum of series is", round(sum1, 2))
7 #round(num) is a default call but round(num,2) specified decimal place

Enter the number of terms: 3

Enter the value of x: 5

The sum of series is 55.17

∞ 1
2.

𝑛=1 𝑛 − 1
2

In [4]: 

1 sum([1/(n**2-1) for n in range(2, 1000)])

Out[4]:

0.7489994994995

Printing a Series

𝑥 + 𝑥2 + 𝑥3 + 𝑥4 + 𝑥5 +.....+ 𝑥𝑛
2 3 4 5 𝑛
3.

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 22/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [9]: 

1 import sympy as sp
2 from sympy import pprint
3 x= sp.Symbol('x')
4 series=x #we create the label, series and set its initial value as x
5 n= int(input("Enter the number of terms you want in the series: "))
6 #we define a for loop that will iterate
7 #over the integers from 2 to n
8 for i in range(2, n+1):
9 series= series + (x**i)/i #Each time the loop iterates, it adds each term to series
10 pprint(series)

Enter the number of terms you want in the series: 5

5 4 3 2

x x x x

── + ── + ── + ── + x

5 4 3 2

Conclusion: learnt and understood the concept of mathematical infinite


series

LAB 13
13 September 2022

Topic: Infinite Series

Aim: to learn and understand the concept of mathematical infinite series

In [2]: 

1 from sympy import Symbol, pprint, init_printing


2 def print_series(n):
3 #Initalize printing system with reverse order
4 # init_printing() helps to print the unicode characters for mathematical expression
5 init_printing(order= 'rev-lex')
6 x= Symbol('x')
7 series= x
8 for i in range(2, n+1):
9 series= series + (x**i)/i
10 pprint(series)
11
12 if __name__ == '__main__':
13 n= input('Enter the number of terms you want in the series: ')
14 print_series(int(n))

Enter the number of terms you want in the series: 5

2 3 4 5

x x x x

x + ── + ── + ── + ──

2 3 4 5

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 23/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

The print series() function accepts an integer n as a parameter that is the number of terms in the series that will
be printed.

In [1]: 

1 from sympy import Symbol, pprint, init_printing


2 def print_series(n, x_values):
3 #Initalize printing system with reverse order
4 # init_printing() helps to print the unicode characters for mathematical expression
5 init_printing(order= 'rev-lex')
6 x= Symbol('x')
7 series= x
8 for i in range(2, n+1):
9 series= series + (x**i)/i
10 pprint(series)
11 #Evaluate the series at x_value
12 series_value= series.subs({x:x_value})
13 #We use the subs() method to perform the evaluation and
14 # the label series_value to refer to the result
15 print('Value of the series at {0}: {1}' .format(x_value, series_value))
16
17 if __name__ == '__main__' :
18 n= input('Enter the number of terms you want in a series: ')
19 x_value= input('Enter the value of x at which you want to evaluate the series: ')
20 print_series(int(n), float(x_value))
21 # the print_series() function now takes additional argument, x_value, which is the

Enter the number of terms you want in a series: 5

Enter the value of x at which you want to evaluate the series: 1

2 3 4 5

x x x x

x + ── + ── + ── + ──

2 3 4 5

Value of the series at 1: 137/60

Conclusion: learnt and understood the concept of mathematical infinite


series

LAB 14
14 September 2022

Topic: Infinite Series

Aim: to learn and understand the concept of mathematical infinite series

1 + 2 + 3 + 4+....+𝑛

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 24/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [19]: 

1 n= int(input("Enter the number of terms: "))


2 sum1=1
3 for i in range(2, n+1):
4 sum1= sum1+ i
5 print("The sum of series is", round(sum1, 2))
6 #round(num) is a default call but round(num,2) specified decimal place

Enter the number of terms: 3

The sum of series is 6

13 + 23 + 33 +...+𝑛3
In [18]: 

1 n= int(input("Enter the number of terms: "))


2 sum1=1
3 for i in range(2, n+1):
4 sum1= sum1 + (i)**3
5 print("The sum of series is", round(sum1, 2))
6 #round(num) is a default call but round(num,2) specified decimal place

Enter the number of terms: 3

The sum of series is 36

2 + 4 + 6 + 8... + 𝑛
In [17]: 

1 n= int(input("Enter the number of terms: "))


2 sum1=0
3 for i in range(n+1):
4 sum1= sum1 + i*2
5 print("The sum of series is", round(sum1, 2))
6 #round(num) is a default call but round(num,2) specified decimal place

Enter the number of terms: 3

The sum of series is 12

9 + 99 + 999+....+𝑛
In [2]: 

1 n= int(input("Enter the number of terms: "))


2 sum1=0
3 for i in range(n+1):
4 sum1= sum1 + (10**i)-1
5 print("The sum of series is", round(sum1, 2))

Enter the number of terms: 2

The sum of series is 108

1 + (1 + 3) + (1 + 3 + 5)+...+𝑛
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 25/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [5]: 

1 n= int(input("Enter the number of terms: "))


2 sum1=0
3 for i in range(1, n+1):
4 l=1
5 for j in range(1,i+1):
6 sum1= sum1+l
7 l= l+2
8 print(sum1)

Enter the number of terms: 3

14

Conclusion: learnt and understood the concept of mathematical infinite


series

LAB 15
20 September 2022

Topic: Gauss Elimination Method

Aim: to understand and learn about the gauss elimination method on


equations

Write a program to solve the matrix using Gauss Elimination Method

𝑥 − 𝑦 + 2𝑧 = 3,𝑥 + 2𝑦 + 3𝑧 = 5,3𝑥 − 4𝑦 − 5𝑧 = −13

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 26/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [3]: 

1 #Importing NumPy Library


2 import numpy as np
3 import sys
4
5 #Reading number of unknowns
6 n= int(input("Enter number of unknowns: "))
7
8 #Making numpy array of n*(n+1) size and initializing to zero for storing argumented mat
9 a= np.zeros((n, n+1))
10
11 #Making numpy array of n size and initializing to zero for storing solution vector
12 x= np.zeros(n)
13
14 #Reading augmented matrix coefficients
15 print("Enter Augemented Matrix Coefficients: ")
16 for i in range(n):
17 for j in range(n+1):
18 a[i][j]= float(input('a[' + str(i) + '][' + str(j) + ']='))
19
20 #Applying Gauss Elimination
21 for i in range(n):
22 if a[i][i] == 0.0:
23 sys.exit("Divide by zero detected! ")
24
25 for j in range(i+1, n):
26 ratio= a[j][i]/ a[i][i]
27
28 for k in range(n+1):
29 a[j][k]= a[j][k]- ratio*a[i][k]
30
31 #Back Substitution
32 x[n-1]= a[n-1][n]/ a[n-1][n-1]
33
34 for i in range(n-2, -1, -1):
35 x[i]= a[i][n]
36
37 for j in range(i +1, n):
38 x[i]= x[i] - a[i][j] * x[j]
39
40 x[i]= x[i]/ a[i][i]
41
42 #Displaying solution
43 print("\nRequired solution is: ")
44 for i in range(n):
45 print("X%d= %0.2f"%(i, x[i]), end="\t")

Enter number of unknowns: 3

Enter Augemented Matrix Coefficients:

a[0][0]=1

a[0][1]=-1

a[0][2]=2

a[0][3]=3

a[1][0]=1

a[1][1]=2

a[1][2]=3

a[1][3]=5

a[2][0]=3

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 27/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

a[2][1]=-4

a[2][2]=-5

a[2][3]=-13

Required solution is:

X0= -1.00 X1= 0.00 X2= 2.00

Conclusion: understood and learnt about the gauss elimination method on


equations

LAB 16
21 September 2022

Topic: Gauss Jordan Method

Aim:to understand and learn about the gauss jordan method on equations

Write a program to solve the matrix using Gauss Jordan method

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 28/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [2]: 

1 #Importing NumPy Library


2 import numpy as np
3 import sys
4
5 #Reading number of unknowns
6 n= int(input("Enter number of unknowns: "))
7
8 #Making numpy array of n*(n+1) size and initializing to zero for storing argumented mat
9 a= np.zeros((n, n+1))
10
11 #Making numpy array of n size and initializing to zero for storing solution vector
12 x= np.zeros(n)
13
14 #Reading augmented matrix coefficients
15 print("Enter Augemented Matrix Coefficients: ")
16 for i in range(n):
17 for j in range(n+1):
18 a[i][j]= float(input('a[' + str(i) + '][' + str(j) + ']='))
19
20 #Applying Gauss Elimination
21 for i in range(n):
22 if a[i][i] == 0.0:
23 sys.exit("Divide by zero detected! ")
24
25 for j in range(n):
26 if i!=j:
27
28 ratio= a[j][i]/ a[i][i]
29 for k in range(n+1):
30 a[j][k]= a[j][k]- ratio*a[i][k]
31
32 #Obtaining solution
33 for i in range(n):
34 x[i]= a[i][n]/ a[i][i]
35
36 #Displaying solution
37 print("\nRequired solution is: ")
38 for i in range(n):
39 print("X%d= %0.2f"%(i, x[i]), end="\t")

Enter number of unknowns: 3

Enter Augemented Matrix Coefficients:

a[0][0]=2

a[0][1]=1

a[0][2]=1

a[0][3]=10

a[1][0]=3

a[1][1]=2

a[1][2]=3

a[1][3]=18

a[2][0]=1

a[2][1]=4

a[2][2]=9

a[2][3]=16

Required solution is:

X0= 7.00 X1= -9.00 X2= 5.00

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 29/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

Conclusion: understood and learnt about the gauss jordan method on


equations

LAB 17
18 October 2022

Topic: Row Echelon Form

Aim: to understand and learn about row echelon form

Write a program to reduce the user defined matrix to an echelon form( lesding entry may not be unity) using
elementary row operations and without using in built function echelon_form

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 30/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [6]: 

1 import numpy as np
2 from sympy import Matrix, pprint
3 A= np.mat(input("Enter a matrix: "))
4 (r,c)= A.shape
5 A=Matrix(A)
6 print("The given matrix is: ")
7 pprint(A)
8 b=0
9 for j in range(c):
10 for i in range(b+1, r):
11 x= 0
12 Flag= False
13 if (A[b,j]==0):
14 for k in range(b+1, r):
15 if (A[k,j] !=0):
16 A= A.elementary_row_op(op="n<->m", row1=k, row2=b )
17 x=1
18 break
19 if (A[b,j]==0 and x==0):
20 Flag= True
21 break
22 A= A.elementary_row_op(op= "n->n+km", row=i, k=-(A[i,j]/A[b,j]), row1=i, row2=b
23 if Flag:
24 continue
25 else:
26 b+=1
27 print("The echelon form of the matrix is: ")
28 pprint(A)

Enter a matrix: 2 3 4; 5 4 6; 7 9 8

The given matrix is:

⎡2 3 4⎤

⎢ ⎥

⎢5 4 6⎥

⎢ ⎥

⎣7 9 8⎦

The echelon form of the matrix is:

⎡2 3 4 ⎤

⎢ ⎥

⎢0 -7/2 -4 ⎥

⎢ ⎥

⎣0 0 -30/7⎦

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 31/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [8]: 

1 import numpy as np
2 from sympy import Matrix, pprint
3 A= np.mat(input("Enter a matrix: "))
4 (r,c)= A.shape
5 A=Matrix(A)
6 print("The given matrix is: ")
7 pprint(A)
8 b=0
9 for j in range(c):
10 for i in range(b+1, r):
11 x= 0
12 Flag= False
13 if (A[b,j]==0):
14 for k in range(b+1, r):
15 if (A[k,j] !=0):
16 A= A.elementary_row_op(op="n<->m", row1=k, row2=b )
17 x=1
18 break
19 if (A[b,j]==0 and x==0):
20 Flag= True
21 break
22 A= A.elementary_row_op(op= "n->n+km", row=i, k=-(A[i,j]/A[b,j]), row1=i, row2=b
23 if Flag:
24 continue
25 else:
26 b+=1
27 print("The echelon form of the matrix is: ")
28 pprint(A)

Enter a matrix: 2 1 2; 3 4 5; 6 7 8

The given matrix is:

⎡2 1 2⎤

⎢ ⎥

⎢3 4 5⎥

⎢ ⎥

⎣6 7 8⎦

The echelon form of the matrix is:

⎡2 1 2 ⎤

⎢ ⎥

⎢0 5/2 2 ⎥

⎢ ⎥

⎣0 0 -6/5⎦

Conclusion: learnt and understand about row echelon form

LAB 18
2 November 2022

Topic: Complex Arithmetic Functions in Python

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 32/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

Aim: to understand and learn about complex arithemetic functions in


python

A complex number is the sum of a real and imaginary number. A complex number is of the form a+ib and is
usually represented by z. Heere both a and b are real numbers. The value is called the real part denoted by
Re(z) and b is called the imaginary part im(z). Also, ib is the imaginary number.

cmath: This module provides access to mathematical functions for complex numbers.

Operations on complex numbers :

1. exp() :- This function returns the exponent of the complex number mentioned in its
argument.
2. log(x,b) :- This function returns the logarithmic value of x with the base b, both mentioned in its
arguments.
If base is not specified, natural log of x is returned.
3. log10() :- This function returns the log base 10 of a complex number.
4. sqrt() :- This computes the square root of a complex number.

In [1]: 

1 v= 2+3j
2 v

Out[1]:

(2+3j)

In [2]: 

1 u= 3j+6
2 u

Out[2]:

(6+3j)

In [3]: 

1 p=3
2 q=5
3 r=p+1j*q
4 r

Out[3]:

(3+5j)

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 33/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [4]: 

1 #Python converts the real numbers x and y into complex numbers using function complex(x
2 x=2
3 y=6
4 z= complex(x,y)
5 z

Out[4]:

(2+6j)

Addition and Substraction of Complex Numbers

In [5]: 

1 m=1+2j
2 n=5+6j
3 w= m+n
4 w

Out[5]:

(6+8j)

In [6]: 

1 a= 2+3j
2 b= 5+6j
3 a+b

Out[6]:

(7+9j)

In [7]: 

1 a-b

Out[7]:

(-3-3j)

Multiplication and Division of Complex Numbers

In [8]: 

1 a*b

Out[8]:

(-8+27j)

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 34/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [9]: 

1 u*v

Out[9]:

(3+24j)

In [10]: 

1 a/b

Out[10]:

(0.4590163934426229+0.04918032786885245j)

Extracting Real and Imaginary part from a complex number

In [12]: 

1 h=9-5j
2 h.real

Out[12]:

9.0

In [13]: 

1 h.imag

Out[13]:

-5.0

Complex Conjugate

In [14]: 

1 h.conjugate()

Out[14]:

(9+5j)

In [16]: 

1 w=-6-8j
2 w.conjugate()

Out[16]:

(-6+8j)

Write a program to enter two real number and convert them into complex number. Also find the real and
imaginary part of the complex number.
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 35/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [21]: 

1 x=int(input("Enter a real number: "))


2
3 y=int(input("Enter a real number: "))
4
5 z=complex(x,y)
6
7 print("The complex number is:",z)
8
9 real=z.real
10
11 print("The real part of",z,"is:",real)
12
13 imag=z.imag
14
15 print("The imaginary part of",z,"is:",imag)

Enter a real number: 2

Enter a real number: 3

The complex number is: (2+3j)

The real part of (2+3j) is: 2.0

The imaginary part of (2+3j) is: 3.0

In [20]: 

1 z=complex(input("Enter a complex number: "))


2 w=complex(input("Enter a complex number: "))
3 x=z+w
4 x

Enter a complex number: 3

Enter a complex number: 6

Out[20]:

(9+0j)

Conclusion: understood and learnt about complex arithemetic functions in


python

LAB 19
8 November 2022

Topic: Complex Arithmetic Functions in Python

Aim: to understand and learn about complex arithemetic functions in


python

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 36/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [1]: 

1 #to perform arithemetic functions on complex numbers


2 x=int(input("Enter a real number: "))
3 y=int(input("Enter a real number: "))
4 z=complex(x,y)
5 print("The first complex number is:",z)
6 a=int(input("Enter a real number: "))
7 b=int(input("Enter a real number: "))
8 c=complex(a,b)
9 print("The second complex number is:",c)
10 #addition
11 print("The sum of two complex numbers is: ", z+c)
12 #substraction
13 print("The difference of two complex numbers is: ", z-c)
14 #multiplication
15 print("The product of two complex numbers is: ", z*c)
16 #division
17 print("The quotient of two complex numbers is: ", z/c)

Enter a real number: 6

Enter a real number: 7

The first complex number is: (6+7j)

Enter a real number: 3

Enter a real number: 14

The second complex number is: (3+14j)

The sum of two complex numbers is: (9+21j)

The difference of two complex numbers is: (3-7j)

The product of two complex numbers is: (-80+105j)

The quotient of two complex numbers is: (0.5658536585365853-0.3073170731707


317j)

Trignometric functions
sin(): This function returns the sine of complex number passed in argument
cos(): This fuction returns the cosine function of complex number passed in argument
tan(): This function returns the tangent function of complex number passed in argument

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 37/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [2]: 

1 #Python code to demonstrate the working of sin(), cos() and tan()


2
3 #Importing "cmath" for complex number operations
4 import cmath
5
6 #Initializing real numbers
7 x= 1.0
8 y=1.0
9
10 #Converting x and y into complex number z
11 z= complex(x,y)
12
13 #Printing sine of complex numbers
14 print("The sine value of complex number is: ", end=" ")
15 print(cmath.sin(z))
16
17 #Printing cosine of complex numbers
18 print("The cosine value of complex number is: ", end=" ")
19 print(cmath.cos(z))
20
21 #Printing tangent of complex numbers
22 print("The tangent value of complex number is: ", end=" ")
23 print(cmath.tan(z))

The sine value of complex number is: (1.2984575814159773+0.6349639147847361


j)

The cosine value of complex number is: (0.8337300251311491-0.98889770576286


51j)

The tangent value of complex number is: (0.2717525853195118+1.0839233273386


946j)

Conclusion: understood and learnt about complex arithemetic functions in


python

LAB 20
9 November 2022

Topic: Complex Arithmetic Functions in Python

Aim: to understand and learn the working of isnan(), isinif() and isfinite() in
python

isfinite() :- Returns true if both real and imaginary part of complex number are finite, else returns false.
isinf() :- Returns true if either real or imaginary part of complex number is/are infinite, else returns false.
isnan() :- Returns true if either real or imaginary part of complex number is NaN , else returns false.
math.inf- A floating-point positive infinity. (For negative infinity, use -math.inf.) Equivalent to the output of
float('inf').
math.nan- A floating-point “not a number” (NaN) value. Equivalent to the output of float('nan')

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 38/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [5]: 

1 #Python code to demonstrate the working of isnan(), isinif() and isfinite()


2 import cmath
3 import math
4
5 x= 1.0
6 y= 1.0
7 a= math.inf
8 b= math.nan
9
10 z= complex(x,y)
11 w= complex(x,a)
12
13 #coverting to complex number
14 v= complex(x,b)
15
16 # checking if both numberare finite
17 if cmath.isfinite(z):
18 print("Complex number is finite")
19 else:
20 print("Complex number is infinite")
21
22 # checking if either number is/are infinite
23 if cmath.isinf(w):
24 print("Complex number is infinite")
25 else:
26 print("Complex number is not infinite")
27
28 # checking if either number is/are finite
29 if cmath.isnan(v):
30 print("Complex number is NaN")
31 else:
32 print("Complex number is not NaN")

Complex number is finite

Complex number is infinite

Complex number is NaN

In [3]: 

1 a= math.inf
2 b= math.nan
3 c= -math.inf
4 print("a:", a)
5 print("b:", b)
6 print("c:", c)

a: inf

b: nan

c: -inf

Conclusion: understood and learnt about complex arithemetic functions in


python

LAB 21
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 39/41
LAB 21
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

15 November 2022

Topic: Trigonometric Complex Functions

Aim: to understand and learn about Trigonometric Complex functions in


python

In [1]: 

1 from cmath import *

In [2]: 

1 r1=sin(1+8j)
2 r1

Out[2]:

(1254.1949676545178+805.3091464217314j)

In [3]: 

1 r2=cos(2+4j)
2 r2

Out[3]:

(-11.36423470640106-24.814651485634187j)

In [4]: 

1 #finding root -1
2 s=sqrt(-1)
3 s

Out[4]:

1j

Note:
𝑒𝑖𝜃 = 𝑐𝑜𝑠𝜃 + 𝑖𝑠𝑖𝑛𝜃
In [5]: 

1 #i=1j
2 f1=3
3 exp(1j*f1)

Out[5]:

(-0.9899924966004454+0.1411200080598672j)

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 40/41


22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook

In [6]: 

1 cos(f1)+1j*sin(f1)

Out[6]:

(-0.9899924966004454+0.1411200080598672j)

Calculation of the length of a planar curve arc


If the curve is expressed in the form and is continuous and

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
2 ⎯
derivable, then the formula for calculating the length of the arc of the curve between and is as follows:
𝑏 𝑑𝑦
∫𝑎 √1 + ( 𝑑𝑥 ) 𝑑𝑥
In [7]: 

1 import scipy.integrate as spi


2 import numpy as np # Thinly-wrapped version of Numpy
3 from scipy.misc import derivative
4 # defining the function
5 def function(x):
6 return np.exp(-x**2)
7
8 # calculating its derivative
9 def deriv(x):
10 return derivative(function, x)
11 integrand =lambda x:np.sqrt(1 + deriv(x) ** 2)
12 a = -1.
13 b = 1.
14 result, error = spi.quad(integrand, a, b)
15 print('Result is ', result, 'with error ', error)

Result is 2.1114895588938722 with error 1.4244827870287161e-11

Conclusion: understood and learnt about complex arithemetic functions in


python

In [ ]: 

localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 41/41

You might also like