[go: up one dir, main page]

0% found this document useful (0 votes)
35 views8 pages

Slip NO - 7

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

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...

In [5]: #Q3)Write a python program to draw rectangle with vertices [1,0],[2,1],[1,2


# [0,1] its rotation about the origin by 𝝅/2 radians

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)

The points P, Q, and R are collinear.


The length of the ray between P and Q is 4.0
The slope of the ray is inf

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

point = np.array([1, 0, 0])


theta = np.radians(90)
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]])
new_point = np.dot(rotation_matrix, point)
print("The original point was:", point)
print("The rotated point is:", new_point)

The original point was: [1 0 0]


The rotated point is: [6.123234e-17 1.000000e+00 0.000000e+00]

5 of 8 26/03/24, 11:46
PRAJAPATI SANJAY PRACTICAL NO_7 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [13]: #Q7) Write a python program to solve the following LPP :


Min Z=3.5x+2y
Subject to x+y≥5
x≥4
y≤2
x,y≥0

from pulp import *


prob = LpProblem("LP Problem", LpMinimize)
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)
prob += 3.5*x + 2*y
prob += x + y >= 5
prob += x >= 4
prob += y <= 2
prob.solve()
print("Status: ", LpStatus[prob.status])
print("Optimal values:")
for v in prob.variables():
print(v.name, "=", v.varValue)
print("Optimal objective value: ", value(prob.objective))

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

from pulp import *


prob = LpProblem("LP Problem", LpMaximize)
x = LpVariable("x", lowBound=0)
y = LpVariable("y", lowBound=0)
z = LpVariable("z", lowBound=0)
prob += x + 2*y + z
prob += x + 2*y + 2*z <= 1
prob += 3*x + 2*y + z >= 8
prob.solve()
print("Status: ", LpStatus[prob.status])
print("Optimal values:")
for v in prob.variables():
print(v.name, "=", v.varValue)
print("Optimal objective value: ", value(prob.objective))

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:"

Reflection through Y-axis: [-4 2]


Scaling in X-coordinate by factor 3: [12 -2]
Rotation about origin through an angle π: [-4. 2.]
Shearing in both X and Y direction by -2 and 4 units respectively:
[ 8 14]

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)

Transformed point A: [-17.67766953 16.26345597]


Transformed point B: [-3.53553391 6.36396103]

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

You might also like