Slip NO - 7
Slip NO - 7
Slip NO - 7
In [3]: #Q1)Plot the graph of f(x) = x^4 in [0,5] with red dashed line with circle
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x**4
x_values = np.linspace(0, 5, num=1000)
y_values = f(x_values)
plt.plot(x_values, y_values, 'r--', marker='o')
plt.title('Graph of f(x) = x^4')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
1 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...
In [4]: #Q2) Using Python program generate 3D surface plot for the function
# f(x)=sin(x^2+y^2) in the interval [0,10]
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def f(x, y):
return np.sin(x**2 + y**2)
x_values = np.linspace(0, 10, 100)
y_values = np.linspace(0, 10, 100)
x_mesh, y_mesh = np.meshgrid(x_values, y_values)
z_values = f(x_mesh, y_mesh)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x_mesh, y_mesh, z_values, cmap='viridis')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('Surface plot of f(x,y) = sin(x^2+y^2)')
plt.show()
2 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...
import numpy as np
import matplotlib.pyplot as plt
vertices = np.array([[1, 0], [2, 1], [1, 2], [0, 1], [1, 0]])
theta = np.pi/2
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
rotated_vertices = vertices @ rotation_matrix
fig, ax = plt.subplots()
ax.plot(vertices[:,0], vertices[:,1], 'k--', label='Original')
ax.plot(rotated_vertices[:,0], rotated_vertices[:,1], 'r-', label='Rotated'
ax.legend()
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.grid()
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Rectangle and its rotation by π/2 radians')
plt.show()
3 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...
In [7]: #Q4) Write a python program to reflect the line segment joining the points
# B[1,4] through the line y=x+1
import numpy as np
import matplotlib.pyplot as plt
A = np.array([5, 3])
B = np.array([1, 4])
def reflection_line(x):
return x - 1
m = 1
b = 1
A_image = np.array([(1-m**2)*A[0] + 2*m*A[1] - 2*m*b, (1-m**2)*A[1] +
2*m*A[0] - 2*b])
B_image = np.array([(1-m**2)*B[0] + 2*m*B[1] - 2*m*b, (1-m**2)*B[1] +
2*m*B[0] - 2*b])
fig, ax = plt.subplots()
ax.plot([A[0], B[0]], [A[1], B[1]], 'b-', label='Original')
ax.plot([A_image[0], B_image[0]], [A_image[1], B_image[1]], 'r-', label
ax.legend()
ax.grid()
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Reflection of line segment through y=x+1')
plt.show()
4 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...
In [9]: #Q5) Using python declare the points P(5,2),Q(5,-2),R(5,0) check whether th
# collinear.Declare the ray passing through the points P and Q find the len
# between P and Q. Also find slope of this ray.
import math
# Define the coordinates of the points P, Q, and R
P = (5, 2)
Q = (5, -2)
R = (5, 0)
# Check if the points P, Q, and R are collinear
if (Q[1]-P[1])*(R[0]-Q[0]) == (R[1]-Q[1])*(Q[0]-P[0]):
print("The points P, Q, and R are collinear.")
else:
print("The points P, Q, and R are not collinear.")
# Define the ray passing through P and Q
ray_direction = (Q[0]-P[0], Q[1]-P[1])
# Find the length of the ray between P and Q
ray_length = math.sqrt(ray_direction[0]**2 + ray_direction[1]**2)
# Find the slope of the ray
if ray_direction[0] != 0:
slope = ray_direction[1] / ray_direction[0]
else:
slope = float('inf')
# Print the length and slope of the ray
print("The length of the ray between P and Q is", ray_length)
print("The slope of the ray is", slope)
In [10]: #Q6) write a python program in 3D to rotate the point (1,0,0) through X pla
# in anticlock wise direction(rotation through Z axis) by angle of 90
5 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...
Status: Optimal
Optimal values:
x = 4.0
y = 1.0
Optimal objective value: 16.0
In [15]: #Q8) Write a python program to display the following LPP by using pulp
module and simplex method.Find its optimal solution if exist
Max Z=x+2y+z
Subject to x+2y+2z ≤ 1
3x+2y+z ≥ 8
X,y,x ≥ 0
Status: Infeasible
Optimal values:
x = 2.6666667
y = 0.0
z = 0.0
Optimal objective value: 2.6666667
6 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...
In [16]: #Q9) Apply Python program in each of the following transformation on the
point P[4,-2]
A ) Reflection through the Y-axis
B) Scaling in X-coordinate by factor 3
C ) Rotation about origin through an angle π
D ) Shearing in both X and Y direction by -2 and 4 units respectively
import numpy as np
P = np.array([4, -2])
P_reflect_y = np.array([-4, 2])
print("Reflection through Y-axis:", P_reflect_y)
P_scale_x = np.array([12, -2])
print("Scaling in X-coordinate by factor 3:", P_scale_x)
theta = np.pi
rot_matrix = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
P_rotate = rot_matrix.dot(P)
print("Rotation about origin through an angle π:", P_rotate)
shear_matrix = np.array([[1, -2], [4, 1]])
P_shear = shear_matrix.dot(P)
print("Shearing in both X and Y direction by -2 and 4 units respectively:"
In [18]: #Q10) Find the combined transformation of the line segment between the poin
A[4,-1] & B[3,2] by using python program for the following sequence of
transformation
i)Rotation about origin through an angle 𝝅/4
ii) Shering in Y direction by 4 units
iii) Scaling in X-coordinate by 5 units
iv) reflection through Y-axis
import numpy as np
A = np.array([4, -1])
B = np.array([3, 2])
theta = np.pi/4
rot_matrix = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
A_rot = rot_matrix.dot(A)
B_rot = rot_matrix.dot(B)
shear_matrix = np.array([[1, 0], [4, 1]])
A_shear = shear_matrix.dot(A_rot)
B_shear = shear_matrix.dot(B_rot)
scale_matrix = np.array([[5, 0], [0, 1]])
A_scale = scale_matrix.dot(A_shear)
B_scale = scale_matrix.dot(B_shear)
A_reflect_y = np.array([-A_scale[0], A_scale[1]])
B_reflect_y = np.array([-B_scale[0], B_scale[1]])
print("Transformed point A:", A_reflect_y)
print("Transformed point B:", B_reflect_y)
7 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...
In [ ]:
8 of 8 26/03/24, 11:46