Comp Phy Lab
Comp Phy Lab
Submitted by:
NAME: Kartikey Singh
REG No.: 24BCE11116
Bachelor of Technology
In
Computer Science & Engineering
Submitted To:
Dr. Vayunandana Kishore.P
SCHOOL OF COMPUTER SCIENCE And
ENGINEERING
S.No. TOPIC NAME
3. Plot a parabola.
REQUIREMENTS:
Libraries to be used: Matplot Library is used for data
visualization
Python version to be used: Python 3.x or above.
Python Code:
plt.xlabel('X-Label')
plt.ylabel('Y-label')
OUTPUT:
EXPERIMENT 2:
REQUIREMENTS:
Libraries to be used: Matplot Library is used for
data visualization.
Python version to be used: Python 3.x or above.
Python Code:
i. Start
ii. Import matplotlib.pyplot library
iii. Define data and labels for the chart
iv. Use plt.pie() to create a pie chart
v. Label the chart
vi. Display the chart
vii. End
:
OUTPUT:
EXPERIMENT 3:
REQUIREMENTS:
Libraries to be used: Matplot Library is used for
data visualization.
NumPy library for range of X-Values
Python version to be used: Python 3.x or above.
Python Code:
import matplotlib.pyplot as plt
import numpy as np
#Calulate Y- Values
y=x**2
i. Start
ii. Import matplotlib.pyplot library
iii. Define categories and corresponding values
iv. Use plt.bar() to create a bar graph
v. Label the graph
vi. Display the bar graph
vii. End
OUTPUT:
EXPERIMENT 4:
REQUIREMENTS:
Python version to be used: Python 3.x or above
Libraries to be used: Matplot Library is used for
data visualization.
Python Code:
OUTPUT:
EXPERIMENT 5:
REQUIREMENTS:
Libraries to be used: Numpy for array calculations.
Python version to be used: Python 3.x or above.
Python Code:
import numpy as np
def add_vectors(vector1, vector2):
if vector1.shape != vector2.shape:
raise ValueError("Vectors must have the same dimension.")
else:
return vector1 + vector2
# Example usage:
vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])
OUTPUT:
EXPERIMENT 6:
REQUIREMENTS:
Libraries to be used: No external libraries are required for this
experiment as dot product can be done using Python's built-in lists.
Python version to be used: Python 3.x or above.
Python Code:
def dot_product(vector1, vector2):
# Check if the vectors have the same length
if len(vector1) != len(vector2):
raise ValueError("Vectors must have the same length")
# Initialize result to 0
result = 0
return result
# Example usage
vector1 = [1, 2, 3]
vector2 = [4, 5, 6]
try:
result = dot_product(vector1, vector2)
print("Dot Product:", result)
except ValueError as e:
print(e)
ALGORITHM:
OUTPUT:
PRECAUTIONS:
REQUIREMENTS:
Libraries to be used: NumPy library is used for data calculations.
This includes linalg function which contains variety of linear
algebra functions for working with matrix and vectors.
Python version to be used: Python 3.x or above.
Python Code:
import numpy as np
ALGORITHM:
i. Input: Two matrices A and B.
ii. Validate Input:
a. Check if both matrices are square:
i. Ensure rows = columns for A and B.
b. Check if dimensions match:
i. Ensure the size of A equals the size of B.
iii. Matrix Multiplication:
a. If valid, compute C=A⋅B using np.dot.
iv. Output: Return the resultant matrix C if valid otherwise return error.
OUTPUT:
PRECAUTIONS:
REQUIREMENTS:
Libraries to be used: NumPy library is used for data calculations.
This includes linalg function which contains variety of linear algebra
functions for working with matrix and vectors.
Python version to be used: Python 3.x or above.
Python Code:
import numpy as np
if det!= 0:
# Compute the inverse
inverse = np.linalg.inv(matrix)
print("The inverse of the matrix is:”)
print(inverse)
else:
print("The matrix is singular and does not have an
inverse.")
ALGORITHM:
i. Start
ii. Define the matrix:
a. Initialize a predefined 3×3 matrix as a NumPy array.
iii. Calculate the determinant:
a. Use np.linalg.det(matrix) to compute the determinant of
the matrix.
iv. Check if the matrix is invertible:
a. If the determinant is non-zero:
i. Compute the inverse using np.linalg.inv(matrix).
ii. Display the inverse of the matrix.
b. Else, if the determinant is zero:
i. Display a message that the matrix is singular and does
not have an inverse.
v. End
OUTPUT:
PRECAUTIONS:
REQUIREMENTS:
Libraries to be used: NumPy library is used for data calculations.
This includes linalg function which contains variety of linear
algebra functions for working with matrix and vectors.
Python version to be used: Python 3.x or above.
Python Code:
import numpy as np
# Predefined 3x3 matrix
matrix = np.array([[2, 1, 1],
[1, 3, 2],
[1, 0, 0]])
OUTPUT:
PRECAUTIONS:
REQUIREMENTS:
Libraries to be used: NumPy library is used for data calculations.
This includes linalg function which contains variety of linear
algebra functions for working with matrix and vectors.
Python version to be used: Python 3.x or above.
Python Code:
import numpy as np
def solve_linear_equations(coefficients, constants):
determinant = np.linalg.det(A)
if determinant == 0:
return "The system has no unique solution
(determinant is zero)."
# Example Input
coefficients = [
[2, -1, 3],
[1, 2, -1],
[3, -2, 4] ]
constants = [5, 6, 8]
# Solve
solution = solve_linear_equations(coefficients,
constants)
print("Solution:", solution)
Algorithm:
i. Input:
a. A 3x3 coefficient matrix AA.
b. A 1x3 constant vector BB.
ii. Check Determinant:
a. Calculate det(A)\text{det}(A) using np.linalg.det(A).
b. If det(A) = 0, return "No unique solution".
iii. Solve the System:
a. If det(A)≠0, calculate X=A−1B or use np.linalg.solve(A, B).
iv. Output:
a. Return the solution vector X.
OUTPUT:
PRECAUTIONS: