0% found this document useful (0 votes)
14 views221 pages

Python codes sem 5

The document contains a series of Python programming exercises focused on plotting graphs, performing geometric transformations, and solving linear programming problems. It includes code examples for 2D and 3D graph plotting, reflecting points, calculating areas and perimeters of shapes, and using libraries like NumPy, Matplotlib, and SciPy. The exercises aim to enhance understanding of mathematical concepts through practical coding applications.

Uploaded by

rutv403
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
14 views221 pages

Python codes sem 5

The document contains a series of Python programming exercises focused on plotting graphs, performing geometric transformations, and solving linear programming problems. It includes code examples for 2D and 3D graph plotting, reflecting points, calculating areas and perimeters of shapes, and using libraries like NumPy, Matplotlib, and SciPy. The exercises aim to enhance understanding of mathematical concepts through practical coding applications.

Uploaded by

rutv403
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 221

PYTHON PROGRAMMING LANGUAGE= ll

SLIP NO :1

Q 1) Attempt any two of the following .

1) Write a python program to plot 2D graph of the functions f(x)= x^2 and g(x) in [-1,1].

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 100)

plt.plot(x, x**2, label="f(x) = x^2")

plt.plot(x, x, label="g(x) = x")

plt.axhline(0, color='black', linestyle="--", linewidth=0.8)

plt.axvline(0, color='black', linestyle="--", linewidth=0.8)

plt.legend()

plt.grid()

plt.show()

2) Write a python program to plot 3D graph of the functions f(x)=e^(-x^2) in [-5,5] with green dashed
points with upward pointing triangle.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 100)

z = np.exp(-x**2)
fig = plt.figure()

ax = fig.add_subplot(projection='3d')

ax.plot(x, np.zeros_like(x), z, 'g^--') # Green dashed line with triangles

plt.show()

3) Using python,represent the following information using bar graph (colour=green).

Item =[clothing ,food ,rent, petrol,misc]

Expenditure in Rs=[600,4000,2000,1500,700]

Ans:

import matplotlib.pyplot as plt

items = ["Clothing", "Food", "Rent", "Petrol", "Misc"]

expenditure = [600, 4000, 2000, 1500, 700]

# Plotting the bar graph

plt.bar(items, expenditure, color='green')

plt.title("Expenditure Chart", fontsize=14)

plt.xlabel("Items", fontsize=12)

plt.ylabel("Expenditure in Rs", fontsize=12)

plt.show()

Q 2) Attempt any two of the following .

1) Write a python program to reflect the line segment joining the points A[5,3] and B [1,4] through the
line y=x+1.

Ans:

import numpy as np

def reflect_point(point, line_slope, line_intercept):


x, y = point

d = (x + (y - line_intercept) * line_slope) / (1 + line_slope**2)

x_reflected = 2 * d - x

y_reflected = 2 * d * line_slope - y + 2 * line_intercept

return x_reflected, y_reflected

A = (5, 3)

B = (1, 4)

line_slope = 1

line_intercept = 1

A_reflected = reflect_point(A, line_slope, line_intercept)

B_reflected = reflect_point(B, line_slope, line_intercept)

print(f"Reflected Point A: {A_reflected}")

print(f"Reflected Point B: {B_reflected}")

2) Write a python program draw polygon with vertices(0,0),(2,0),(2,3) and (1,6) and rotate by 180°.

Ans:

import matplotlib.pyplot as plt

import numpy as np

vertices = np.array([[0, 0], [2, 0], [2, 3], [1, 6], [0, 0]])

rotated = -vertices
plt.plot(vertices[:, 0], vertices[:, 1], 'b-o', label="Original")

plt.plot(rotated[:, 0], rotated[:, 1], 'r--o', label="Rotated 180°")

plt.axhline(0, color='black', linewidth=0.8)

plt.axvline(0, color='black', linewidth=0.8)

plt.legend()

plt.axis('equal')

plt.show()

3) Write a python program to find the area and perimeter of the ΔABC A[0,0],B[5,0],c[3,3].

Ans:

import math

A = (0, 0)

B = (5, 0)

C = (3, 3)

def distance(p1, p2):

return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)

AB = distance(A, B)

BC = distance(B, C)

CA = distance(C, A)

perimeter = AB + BC + CA

s = perimeter / 2

area = math.sqrt(s * (s - AB) * (s - BC) * (s - CA))


print(f"Area of the triangle: {area:.2f}")

print(f"Perimeter of the triangle: {perimeter:.2f}")

Q 3) Attempt the following.

A)Attempt Any one of the following .

1) Write the python program to solve following LPP

I) Max Z=150x+75y

Subject to 4x + 6y ≤ 24.

5x + 3y ≤ 15.

x ≥ 0,y ≥0.

Ans:

from scipy.optimize import linprog

c = [-150, -75]

A = [[4, 6], [5, 3]]

b = [24, 15]

bounds = [(0, None), (0, None)]

result = linprog(c, A_ub=A, b_ub=b, bounds=bounds, method='highs')

if result.success:

print(f"x = {result.x[0]:.2f}, y = {result.x[1]:.2f}, Z = {-result.fun:.2f}")

else:

print("No solution")

Il)Write a python program to display the following LPP using pulp modulo and simplex method . Find the
optimal solution if exits .
Min Z=x + y

Subject to x ≥ 6

Y ≥6

x + y ≤ 11

x ≥0 , y ≥0.

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

B) Attempt any of the following .

1) Apply python program of each of the following transformation on point P[3,-1].

I) Refection through x-axis .

II) Scalling on x-coordinate by factor 2.

III) Scalling on y-coordinate by factor 1.5 .


IV) Refection through line y = x.

Ans:

P = [3, -1]

reflection_x_axis = [P[0], -P[1]]

scaling_x = [2 * P[0], P[1]]

scaling_y = [P[0], 1.5 * P[1]]

reflection_y_equals_x = [P[1], P[0]]

print("Original Point P:", P)

print("1. Reflection through x-axis:", reflection_x_axis)

print("2. Scaling on x-coordinate by factor 2:", scaling_x)

print("3. Scaling on y-coordinate by factor 1.5:", scaling_y)

print("4. Reflection through line y = x:", reflection_y_equals_x)

2) Find the combine transformation of the line segment between the points A[5,-2] & B[4,3] by using
python program for the following sequence of transformation :-

I) Rotation about origin through an angle π

II) Scaling x- coordinate by 2 units

III) Reflection through line y= -x.

IV) Shearing X direction by 4 units

Ans:

A, B = [5, -2], [4, 3]

def transform(point):

x, y = point

x, y = -x, -y

x, y = 2 * x, y

x, y = -y, -x
x, y = x + 4 * y, y

return [x, y]

A_transformed = transform(A)

B_transformed = transform(B)

print("Point A after transformations:", A_transformed)

print("Point B after transformations:", B_transformed)


PYTHON PROGRAMMING LANGUAGE=I

SLIP NO:2

Q 1) Attempt any two of the following .

1) Write a Python program to 2D graphs to the functions f(x)=log₁₀(x)in interval [0,10].

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0.1, 10, 400)

y = np.log10(x)

plt.plot(x, y)

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Plot of f(x) = log10(x)')

plt.grid(True)

plt.show()

2) Using python generate 3D surface plot for the function f(x)= sin(x² + y² ) in the interval [0,10].

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 10, 400)

y = np.linspace(0, 10, 400)

x, y = np.meshgrid(x, y)
z = np.sin(x**2 + y**2)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

3) Using python ,draw a bar graph in GREEN to represent the data below:

Subject =[Math, Scinece,English ,Marathi ,Hindi].

Percentage of passing =[68,90,70,85,91].

Ans:

import matplotlib.pyplot as plt

subjects = ['Math', 'Science', 'English', 'Marathi', 'Hindi']

percentages = [68, 90, 70, 85, 91]

plt.bar(subjects, percentages, color='green')

plt.title("Percentage of Passing per Subject")

plt.xlabel("Subjects")

plt.ylabel("Percentage")

plt.show()

Q 2) Attempt any two of the following .

1) Using sympy declare the points A(0,2), B(5,2),C(3,0) cheak whether these points are collinear .Declare
the line passing through points A & B find the distance of these line from point C.

Ans:

import sympy as sp

A = sp.Point2D(0, 2)
B = sp.Point2D(5, 2)

C = sp.Point2D(3, 0)

are_collinear = sp.Point.is_collinear(A, B, C)

print(f"Are the points collinear? {are_collinear}")

line_AB = sp.Line(A, B)

print(f"Equation of line passing through A and B: {line_AB.equation()}")

distance_C_to_AB = line_AB.distance(C)

print(f"Distance from point C to line AB: {distance_C_to_AB}")

2) Using python ,draw a regular polygon with 6 sides and radius 1 centered at (1,2) and find its area and
perimeter.

Ans:

import numpy as np

import matplotlib.pyplot as plt

sides = 6

radius = 1

center = (1, 2)

angles = np.linspace(0, 2 * np.pi, sides, endpoint=False)

x = center[0] + radius * np.cos(angles)

y = center[1] + radius * np.sin(angles)

area = (3 * np.sqrt(3) / 2) * radius**2 # Area of hexagon

perimeter = sides * (2 * radius * np.sin(np.pi / sides)) # Perimeter

plt.fill(x, y, 'b', alpha=0.5)

plt.plot(x, y, 'r-')

plt.xlim(0, 3)

plt.ylim(1, 4)

plt.gca().set_aspect('equal')
plt.title(f'Hexagon\nArea: {area:.2f}, Perimeter: {perimeter:.2f}')

plt.grid()

plt.show()

print(f"Area: {area:.2f}, Perimeter: {perimeter:.2f}")

3) Write a python program to find area and perimeter of the ΔABC where A[0,0], B[6,0] and C[4,4].

Ans:

import math

A = (0, 0)

B = (6, 0)

C = (4, 4)

AB = math.sqrt((B[0] - A[0])**2 + (B[1] - A[1])**2)

BC = math.sqrt((C[0] - B[0])**2 + (C[1] - B[1])**2)

CA = math.sqrt((A[0] - C[0])**2 + (A[1] - C[1])**2)

perimeter = AB + BC + CA

area = abs(A[0]*B[1] + B[0]*C[1] + C[0]*A[1] - A[1]*B[0] - B[1]*C[0] - C[1]*A[0]) / 2

print(f"Perimeter of ΔABC: {perimeter}")

print(f"Area of ΔABC: {area}")

Q 3) Attempt the following .


A) Attempt any one of the following .

I) Write a python program to solve LPP:

Max Z=5x + 3y

Subject to x + y ≤ 7.

2x + 5y ≤ 1.

X ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 5 * x + 3 * y

prob += x + y <= 7

prob += 2 * x + 5 * y <= 1

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")
II) Write a python program to display the following LPP using pulp modulo and simplex method .Find its
optimal solution if exits.

Max Z= 3x + 2y + 5z

Subject to x + 2y +z ≤ 430

3x + 2z ≤ 460

x + 4y ≤ 120

x ≥ 0,y ≥ 0,z ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

prob += 3 * x + 2 * y + 5 * z

prob += x + 2 * y + z <= 430

prob += 3 * x + 2 * z <= 460

prob += x + 4 * y <= 120

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")
print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal z: {pulp.value(z)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

B) Attempt any one of the following .

1) Apply python program in each of the following transformations on the points P[4,-2].

I) Refection through Y-axis .

II) Scaling on X- coordinate by factor 3.

III) Scaling in Y- coordinate by factor 2.5

IV) Reflection through the line y=-x

Ans:

import numpy as np

P = np.array([4, -2])

P_reflect_y_axis = np.array([-P[0], P[1]])

print(f"Reflection through Y-axis: {P_reflect_y_axis}")

P_scale_x = np.array([3 * P[0], P[1]])

print(f"Scaling on X-coordinate by factor 3: {P_scale_x}")

P_scale_y = np.array([P[0], 2.5 * P[1]])

print(f"Scaling on Y-coordinate by factor 2.5: {P_scale_y}")

P_reflect_line_y_neg_x = np.array([-P[1], -P[0]])

print(f"Reflection through the line y = -x: {P_reflect_line_y_neg_x}")


2) Find the combined transformation of each line segment between the points A[4,-1] & B[3,0] by using
python program for the following sequence of transformation.

I) Rotaion about origin through an angle π

II) Shearing in Y direfction by 4.5 units.

III) Scaling in X -coordinate by 3 units.

IV) Reflection through lines y=x

Ans:

import numpy as np

A = np.array([4, -1])

B = np.array([3, 0])

R = np.array([[-1, 0], [0, -1]])

S_y = np.array([[1, 0], [4.5, 1]])

S_x = np.array([[3, 0], [0, 1]])

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

T = R_yx @ S_x @ S_y @ R

A_transformed = T @ A

B_transformed = T @ B
print(f"Transformed A: {A_transformed}")

print(f"Transformed B: {B_transformed}")

PYTHON PROGRAMMING LANGUAGE=III

SLIP NO: 3

Q 1) Attempt any two of the following .

1) Using python plot the graph of function f(x)= cos (x) on the interval [0,2π].

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 2 * np.pi, 400)

y = np.cos(x)

plt.plot(x, y)

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Plot of f(x) = cos(x)')

plt.grid(True)

plt.show()

2) Following is the information of students participating in various game in a school.Represent it a by Bar


graph with bar width of 0.7 inches .
Game =[Cricket ,Football ,Hockey ,Chess ,Tennis].

Number of students=[65,30 54,10,20].

Ans:

import matplotlib.pyplot as plt

games = ['Cricket', 'Football', 'Hockey', 'Chess', 'Tennis']

students = [65, 30, 54, 10, 20]

plt.bar(games, students, width=0.7, color='skyblue')

plt.xlabel('Games')

plt.ylabel('Number of Students')

plt.title('Number of Students Participating in Various Games')

plt.grid(True)

plt.show()

3) Write a python program to generate 3D plot of the function z= sin x+ cos x in -10 < x, y < 10.

Ans:

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import numpy as np

x = np.linspace(-10, 10, 400)

y = np.linspace(-10, 10, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x) + np.cos(y)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

ax.set_title('3D Plot of z = sin(x) + cos(y)')

plt.show()

Q 2) Attemprt any two of the following .

1) Write a python program to reflect line segment joining the points A[5,3] and B[1,4] through line
y=x+1.

Ans:

import numpy as np

A = np.array([5, 3])

B = np.array([1, 4])

def reflect_point(point):

x, y = point

x_reflected = (x - y + 2) / 2

y_reflected = (y - x + 2) / 2

return np.array([x_reflected, y_reflected])

A_reflected = reflect_point(A)

B_reflected = reflect_point(B)

print(f"Reflected A: {A_reflected}")
print(f"Reflected B: {B_reflected}")

2) If the line with points A[2,1],B[4,-1] is transformed by the transformation matrix [T]=[ 1 2

2 1],the using python find the equation of transform line .

Ans:

import numpy as np

T = np.array([[1, 2], [2, 1]])

A, B = np.array([2, 1]), np.array([4, -1])

A_prime, B_prime = T @ A, T @ B

m = (B_prime[1] - A_prime[1]) / (B_prime[0] - A_prime[0])

c = A_prime[1] - m * A_prime[0]

print(f"A' = {A_prime}, B' = {B_prime}, Line: y = {m:.2f}x + {c:.2f}")

3) Generate the line segment having endpoints (0,0) and (10,10) . Find the mid points of line segment .

Ans:

import numpy as np

A = np.array([0, 0])

B = np.array([10, 10])

midpoint = (A + B) / 2

print(f"Midpoint of the line segment: {midpoint}")


Q 3) Attempt the following .

A) Attempt any one of the following.

1) Write a program to solve the following LPP :

Min Z = 3.5 x +2y

Subject to x + y ≥ 5

x≥4

y≤2

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 3.5 * x + 2 * y

prob += x + y >= 5

prob += x >= 4

prob += y <= 2

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")
print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2) Write a program to display the following LPP using pulp modulo and simplex method . Find its optimal
solution if exits.

Max Z = 3x1 + 5x2 + 4x3

Subject to 2x1 + 3x2 ≤ 8

2x2 + 5x3 ≤ 10

3x1 + 2x2+ 4x3 ≤ 15

x1 ≥0, x2 ≥ 0, x3 ≥0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x1 = pulp.LpVariable('x1', lowBound=0)

x2 = pulp.LpVariable('x2', lowBound=0)

x3 = pulp.LpVariable('x3', lowBound=0)

prob += 3 * x1 + 5 * x2 + 4 * x3

prob += 2 * x1 + 3 * x2 <= 8

prob += 2 * x2 + 5 * x3 <= 10

prob += 3 * x1 + 2 * x2 + 4 * x3 <= 15
prob.solve(pulp.PULP_CBC_CMD(msg=1))

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x1: {pulp.value(x1)}")

print(f"Optimal x2: {pulp.value(x2)}")

print(f"Optimal x3: {pulp.value(x3)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

B) Attempt any one of the following .

1) Apply pythonnprogram in each of the following transformation on the point P[4,-2]

I) Refection through Y -axis .

II) Scaling in X-coordinate by factor 3.

III) Scaling in Y-coordinate by factor 2.5

IV) Reflection through the line y=x.

Ans:

import numpy as np

P = np.array([4, -2])

P_reflect_y_axis = np.array([-P[0], P[1]])

print(f"Reflection through Y-axis: {P_reflect_y_axis}")

P_scale_x = np.array([3 * P[0], P[1]])

print(f"Scaling on X-coordinate by factor 3: {P_scale_x}")

P_scale_y = np.array([P[0], 2.5 * P[1]])


print(f"Scaling on Y-coordinate by factor 2.5: {P_scale_y}")

P_reflect_y_eq_x = np.array([P[1], P[0]])

print(f"Reflection through the line y = x: {P_reflect_y_eq_x}")

2) Find the combine transformation ofthe line segment between points A[2,-1] & B[5,4] by using python
program for each following sequence of transformations:-

I) Rotation about origin through an angle π.

II) Scaling in X- coordinate by 3 units.

III) Shearing in Xdirection by 6 units.

IV) Reflection through the line y=x.

Ans:

import numpy as np

A, B = np.array([2, -1]), np.array([5, 4])

rotation = np.array([[-1, 0], [0, -1]])

scaling = np.array([[3, 0], [0, 1]])

shearing = np.array([[1, 6], [0, 1]])

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

T = reflection @ shearing @ scaling @ rotation

A_transformed, B_transformed = T @ A, T @ B

print(f"Transformed Points: A' = {A_transformed}, B' = {B_transformed}")


PYTHON PROGRAMMING LANGUAGE =II

SLIP NO =4

Q 1) Attempt any two of the following .

1)Write a Python program to 2D graphs to the functions f(x)=log₁₀(x)in interval [0,10].

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0.1, 10, 400)

y = np.log10(x)

plt.plot(x, y)
plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Plot of f(x) = log10(x)')

plt.grid(True)

plt.show()

2)Using python plot the graph of function f(x)=sin⁻¹ (x) interval [-1,1].

Ans:

import matplotlib.pyplot as plt

import numpy as np

# Define the interval [-1, 1]

x = np.linspace(-1, 1, 400)

y = np.arcsin(x)

# Plot the function

plt.plot(x, y)

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Plot of f(x) = sin⁻¹(x)')

plt.grid(True)

plt.show()

3)Using python plot the surface plot of parabola x= x^2 +y^2 in -6< x,y < 6.

Ans:

import matplotlib.pyplot as plt

import numpy as np
x = np.linspace(-6, 6, 400)

y = np.linspace(-6, 6, 400)

x, y = np.meshgrid(x, y)

z = x**2 + y**2

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

Q 2) Attempt any two of the following .

1) If the line with points A[3,1] , B [ 5,-1] is transformed by the transformation matrix [T]=[3 -2

2 1] then using python, find the equation of transformed line.

Ans:

import numpy as np

T = np.array([[3, -2], [2, 1]])

A, B = np.array([3, 1]), np.array([5, -1])

A_prime, B_prime = T @ A, T @ B

m = (B_prime[1] - A_prime[1]) / (B_prime[0] - A_prime[0])

c = A_prime[1] - m * A_prime[0]
print(f"A' = {A_prime}, B' = {B_prime}, Line: y = {m:.2f}x + {c:.2f}")

2) Write a python program to draw a polygon with vertices (0,0),(2,0),(2,3) & (1,6 ) and rotate it by 180∘

Ans:

import matplotlib.pyplot as plt

import numpy as np

vertices = np.array([[0, 0], [2, 0], [2, 3], [1, 6]])

rotation_matrix = np.array([[-1, 0], [0, -1]])

vertices_rotated = vertices.dot(rotation_matrix)

plt.plot(*zip(*np.append(vertices, [vertices[0]], axis=0)), label="Original")

plt.plot(*zip(*np.append(vertices_rotated, [vertices_rotated[0]], axis=0)), label="Rotated")

plt.legend()

plt.show()

3) Using python generate line passing through points (2,3) & (4,3) and find equation of the line.

Ans:

import matplotlib.pyplot as plt

import numpy as np

A = np.array([2, 3])

B = np.array([4, 3])

plt.plot([A[0], B[0]], [A[1], B[1]], marker='o')

plt.xlabel('X')

plt.ylabel('Y')
plt.title('Line passing through points (2, 3) and (4, 3)')

plt.grid(True)

plt.show()

slope = (B[1] - A[1]) / (B[0] - A[0])

intercept = A[1] - slope * A[0]

print(f"Equation of the line: y = {slope}x + {intercept}")

Q 3) Attempt the following.

A) Attempt any one of the following .

1) Write the python program to solve the following LPP.

Max Z= 150x + 75y

Subject to 4x + 6y ≤ 24

5x + 3y ≤ 15

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 150 * x + 75 * y
prob += 4 * x + 6 * y <= 24

prob += 5 * x + 3 * y <= 15

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2) Write a python program to display the following LPP by using pulp modulo and simplex method . Find
its optimal solution if exits .

Max Z = 4x + y + 5z + 5w

Subject to 4x + 6y - 5z - 4w ≥ -20

-8x - 3y +3z + 2w ≤ 20

x + y ≤11

x ≥ 0, y ≥ 0, z ≥ 0, w≥ 0

Ans:

from pulp import *

prob = LpProblem("Maximize_Z", LpMaximize)

x, y, z, w = [LpVariable(var, 0) for var in "xyzw"]

prob += 4*x + y + 5*z + 5*w

prob += 4*x + 6*y - 5*z - 4*w >= -20

prob += -8*x - 3*y + 3*z + 2*w <= 20

prob += x + y <= 11

prob.solve()
print("x=", x.varValue, "y=", y.varValue, "z=", z.varValue, "w=", w.varValue, "Max Z=",
value(prob.objective))

B) Attempt any one of the following .

1) Plot 3D axes with label as x-axis and z- axis and also plot following with given coordinates in one
graph.

I) (70,-25,15) as a diamond in black colour

II) (50,71,-45) as a * in green colour

III) (58,-82 ,65 ) as a dot in green colour

IV) (20 ,72 ,-45)as a * in red colour

Ans:

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

# Coordinates

points = [

(70, -25, 15, 'D', 'black'),

(50, 71, -45, '*', 'green'),

(58, -82, 65, 'o', 'green'),

(20, 72, -45, '*', 'red')

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

# Plot each point with specified marker and color

for x, y, z, marker, color in points:


ax.scatter(x, y, z, c=color, marker=marker)

# Label the axes

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

plt.show()

2) Find the combine transformation of the line segment between the points A[4,-1] &B[3,0] by
using python program for the following sequence of transformation.

I) Shearing in X-direction by 9 units.

II) Rotaion about origin through an angle π .

III) Scaling in X-coordinate by 2 units.

IV) Reflecton through the line y=x.

Ans:

import numpy as np

A, B = np.array([4, -1]), np.array([3, 0])

shear = np.array([[1, 9], [0, 1]])

rotate = np.array([[-1, 0], [0, -1]])

scale = np.array([[2, 0], [0, 1]])

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

A = reflect @ scale @ rotate @ shear @ A

B = reflect @ scale @ rotate @ shear @ B

print("Transformed A:", A)

print("Transformed B:", B)
PYTHON PROGRAMMING LANGUAGE=II

SLIP NO =5

Q 1) Attempt any two of the following .

1) Using python plot the surface plot of function z=cos(x^2-y^2-0.5) in the interval from -1 < x, y
<1.

Ans:

import matplotlib.pyplot as plt


import numpy as np

x = np.linspace(-1, 1, 400)

y = np.linspace(-1, 1, 400)

x, y = np.meshgrid(x, y)

z = np.cos(x**2 - y**2 - 0.5)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

2) Generate 3D surface plot for the fuctions f(x)=sin(x^2 + y^2) in the interval [0,10].

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 10, 400)

y = np.linspace(0, 10, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x**2 + y**2)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')
plt.show()

3) Write a program to generate 3D plot of the fuctions z= sin x + cos y in the interval -10 < x,y < 10.

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-10, 10, 400)

y = np.linspace(-10, 10, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x) + np.cos(y)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

Q 2) Attempt any two of the following .

1) Using python to generate triangle with vertices(0,0),(4,0),(4,3) cheak wheather triangle is Right angle
triangle

Ans:

import math

A, B, C = (0, 0), (4, 0), (4, 3)

AB, BC, AC = math.dist(A, B), math.dist(B, C), math.dist(A, C)

print("Right-angled" if AB**2 + BC**2 == AC**2 else "Not Right-angled")


2) Generate the vector x in the interval [-7,7] using numpy package with subintervals.

Ans:

import numpy as np

x = np.linspace(-7, 7, num=100)

print(x)

3)Write a python program to find the area and perimeter ΔABC where, A[0,0],B[6,0],C[4,4].

Ans:

import math

A, B, C = (0, 0), (6, 0), (4, 4)

AB, BC, CA = math.dist(A, B), math.dist(B, C), math.dist(C, A)

perimeter = AB + BC + CA

area = 0.5 * abs(A[0] * (B[1] - C[1]) + B[0] * (C[1] - A[1]) + C[0] * (A[1] - B[1]))

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

Q 3) Attempt the following.

A) Attempt any one of the following.

I) Write a python program solvethe following LPP:

Max Z=5x + 3y

Subject to x + y ≤ 7

2x + 5y ≤ 1

x ≥ 0,y ≥0.

Ans:
import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 5 * x + 3 * y

prob += x + y <= 7

prob += 2 * x + 5 * y <= 1

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

3) Write a python program to display the following LPP using pulp modulo and simplex method .Find its
optimal solution if exits .

Max Z= 4x + y + 3z +5w

Subject to 4x +6y -5z -4w ≥ 20

-3x - 2y +4z + w ≤ 10

-8x - 3y + 3z + 2w ≤ 20

x + y ≤ 11

x,y,z,w ≥ 0
Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

w = pulp.LpVariable('w', lowBound=0)

prob += 4 * x + y + 3 * z + 5 * w

prob += 4 * x + 6 * y - 5 * z - 4 * w >= 20

prob += -3 * x - 2 * y + 4 * z + w <= 10

prob += -8 * x - 3 * y + 3 * z + 2 * w <= 20

prob += x + y <= 11

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal z: {pulp.value(z)}")

print(f"Optimal w: {pulp.value(w)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")
B) Attempt any one of the following.

1) Apply python program in each of the following transformation on the point P[3,8]

I) refection through X-axis

II) Scaling in x coordinate by factor 6.

III) Rotation about oringin through an angle 30°.

IV) Reflection through the line y= -x.

Ans:

import numpy as np

P = np.array([3, 8])

P = np.array([P[0], -P[1]])

P = np.array([6 * P[0], P[1]])

theta = np.radians(30)

rotation = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

P = rotation @ P

P = np.array([-P[1], -P[0]])

print(f"Transformed P: {P}")
2) Write a program to plot 2D X-axis and Y-axis in black colour . In the same direction plot :-

I) Green triangle with vertices [5,4],[7,4],[6,6].

II) Blue rectangle with vertices[2,2],[10,2],[10,8],[2,8].

III) Red polygon with vertices[[6,2],[10,4],[10,8],[2,8].

IV) Isoscales triangle with of vertices[0,0][4,0][2,4].

Ans:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.axhline(0, color='black')

ax.axvline(0, color='black')

ax.add_patch(plt.Polygon([[5, 4], [7, 4], [6, 6]], color='green'))

ax.add_patch(plt.Polygon([[2, 2], [10, 2], [10, 8], [2, 8]], color='blue'))

ax.add_patch(plt.Polygon([[6, 2], [10, 4], [10, 8], [2, 8]], color='red'))

ax.add_patch(plt.Polygon([[0, 0], [4, 0], [2, 4]], color='orange'))

ax.set_aspect('equal')

plt.xlim(-1, 12)

plt.ylim(-1, 10)

plt.show()
PYTHON PROGRAMMING LANGUGAE=II

SLIP NO=6

Q 1) Attempt any two of the following .

1)Draw the horizontal bar graph for the following datain Martoon colour .

City=[Pune, Mumbai ,Nashik,Nagpur ,Thane].

Air Quality Index=[168,190,170,178,195].

Ans:

import matplotlib.pyplot as plt

cities = ['Pune', 'Mumbai', 'Nashik', 'Nagpur', 'Thane']

aqi = [168, 190, 170, 178, 195]

plt.barh(cities, aqi, color='maroon')

plt.xlabel('Air Quality Index')

plt.ylabel('City')

plt.title('Air Quality Index by City')

plt.show()

2)Using python 3D surface plot for the function f(x)=sin(x^2+y^2) in the interval [0,10].

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 10, 400)


y = np.linspace(0, 10, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x**2 + y**2)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.xlabel('X')

plt.ylabel('Y')

ax.set_zlabel('f(x, y)')

plt.title('3D Surface Plot of f(x, y) = sin(x^2 + y^2)')

plt.show()

3) Using python plot the graph of function f(x)= sin x-e^x +3x^2 - log10(x) on the inverval [0,2π].

Ans:

import numpy as np

import matplotlib.pyplot as plt

import math

def f(x):

return np.sin(x) - np.exp(x) + 3 * x**2 - np.log10(x)

x = np.linspace(0.1, 2 * np.pi, 400) # Start from 0.1 to avoid log10(0)

y = f(x)
plt.plot(x, y, label='f(x) = sin(x) - e^x + 3x^2 - log10(x)')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Plot of f(x) = sin(x) - e^x + 3x^2 - log10(x)')

plt.legend()

plt.grid(True)

plt.show()

Q 2) Attempt any two of the following.

1) Using python rotate the line segment by 180° having end points (1,0) and (2,-1).

Ans:

import numpy as np

import matplotlib.pyplot as plt

P1, P2 = np.array([1, 0]), np.array([2, -1])

rotation_matrix = np.array([[-1, 0], [0, -1]])

P1_rotated, P2_rotated = rotation_matrix @ P1, rotation_matrix @ P2

plt.plot([P1[0], P2[0]], [P1[1], P2[1]], 'bo-', label='Original')

plt.plot([P1_rotated[0], P2_rotated[0]], [P1_rotated[1], P2_rotated[1]], 'ro-', label='Rotated')

plt.axhline(0, color='black', linewidth=0.5)

plt.axvline(0, color='black', linewidth=0.5)

plt.legend()

plt.gca().set_aspect('equal', adjustable='box')

plt.show()
2)Write a python program to draw a polygon with vertices (0,0),(2,0),(2,3) and (1,6) and rotate it by
180°.

Ans:

import numpy as np

import matplotlib.pyplot as plt

vertices = np.array([[0, 0], [2, 0], [2, 3], [1, 6]])

rotation_matrix = np.array([[-1, 0], [0, -1]])

vertices_rotated = np.dot(vertices, rotation_matrix.T)

plt.plot(*vertices.T, 'bo-', label='Original')

plt.fill(*vertices.T, 'b', alpha=0.3)

plt.plot(*vertices_rotated.T, 'ro-', label='Rotated')

plt.fill(*vertices_rotated.T, 'r', alpha=0.3)

plt.axhline(0, color='black', linewidth=0.5)

plt.axvline(0, color='black', linewidth=0.5)

plt.legend()

plt.gca().set_aspect('equal', adjustable='box')

plt.show()

2) Using python generate triangle with vertices (0,0),(4,0),(2,4) cheak wheather thetriangle isoscale
triangle .

Ans:

import numpy as np
A, B, C = np.array([0, 0]), np.array([4, 0]), np.array([2, 4])

distance = lambda p1, p2: np.sqrt(np.sum((p1 - p2)**2))

AB, BC, CA = distance(A, B), distance(B, C), distance(C, A)

isosceles = AB == BC or BC == CA or CA == AB

print("The triangle is isosceles." if isosceles else "The triangle is not isosceles.")

Q 3)Attempt any following.

A) Attempt any one of the folloiwng.

1) Write a pyhon program to solve following LPP :

Maz Z=x + y

Subject to 2x -2y ≥ 1

x + y ≥ 2.

x ≥ 0,y ≥ 0

Ans:

def remove_comments(code):

"""

Removes single-line comments from Python code.

Args:

code: The Python code as a string.

Returns:

The code with comments removed.


"""

lines = code.splitlines()

new_lines = []

for line in lines:

# Find the index of the first '#' character (if it exists)

comment_start = line.find('#')

# If no '#' found, keep the entire line

if comment_start == -1:

new_lines.append(line)

else:

# Keep the part of the line before the comment

new_lines.append(line[:comment_start].strip())

return '\n'.join(new_lines)

# Example usage:

code_with_comments = """

# This is a comment

x = 5 # Another comment

# This is a multiline comment

# that spans multiple lines


y = 10

"""

code_without_comments = remove_comments(code_with_comments)

print(code_without_comments)

2) Write a python program to display the the following LPP using pulp modulo and simplex method
. Find the optimal solution if exits.

Min Z= x + y

Subject to x ≥ 6

y≥6

x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11
prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}")

B) Attempt any one of the following

1) Apply python program in each ofthe following transformation on the point P[ 4,-2]

I) Refection through Y-axis .

II) Scaling in X-coordinate by factor 7

III) Shearing in Y direction by 3 units

IV) Reflection through the line y=-x.

Ans:

import numpy as np

P = np.array([4, -2])

P = np.array([-P[0], P[1]])

print(f"After Reflection through Y-axis: {P}")

P = np.array([7 * P[0], P[1]])

print(f"After Scaling in X-coordinate by factor 7: {P}")

P = np.array([P[0], P[1] + 3 * P[0]])


print(f"After Shearing in Y direction by 3 units: {P}")

P = np.array([-P[1], -P[0]])

print(f"After Reflection through the line y = -x: {P}")

PYTHON PROGRAMMING LANGUAGE=II

SLIP NO:7

Q 1) Attempt any two of the following.

1) Plot the graph of f(x)= x^ 4 in [0,5] with red dashed line with circle markers.

Ans:

import numpy as np

import matplotlib.pyplot as plt


def f(x):

return x**4

x = np.linspace(0, 5, 100)

y = f(x)

plt.plot(x, y, 'r--o', label='$f(x) = x^4$')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Graph of $f(x) = x^4$')

plt.legend()

plt.grid(True)

plt.show()

2) Using python generate 3D surface plot for function f(x)=sin(x^2 + y^2) on the interval [0,10].

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 10, 400)

y = np.linspace(0, 10, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x**2 + y**2)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, cmap='viridis')

plt.xlabel('X')

plt.ylabel('Y')

ax.set_zlabel('f(x, y)')

plt.title('3D Surface Plot of f(x, y) = sin(x^2 + y^2)')

plt.show()

3) Write a python program to rectangle with vertices [1,0],[2,1],[1,2] and [0,1] its rotation about oringin
by π/2 radians .

Ans:

import matplotlib.pyplot as plt

vertices = [(1, 0), (2, 1), (1, 2), (0, 1)]

def rotate_90_degrees(x, y):

return -y, x

rotated_vertices = [rotate_90_degrees(x, y) for x, y in vertices]

vertices.append(vertices[0])

rotated_vertices.append(rotated_vertices[0])

plt.figure(figsize=(8, 8))

plt.plot(*zip(*vertices), marker='o', label="Original Rectangle", color='blue')

plt.plot(*zip(*rotated_vertices), marker='o', label="Rotated Rectangle (π/2)", color='red')


plt.axhline(0, color='black', linewidth=0.5)

plt.axvline(0, color='black', linewidth=0.5)

plt.legend()

plt.grid(True)

plt.gca().set_aspect('equal', adjustable='box')

plt.title("Rectangle Rotation by π/2 Radians")

plt.show()

Q 2) Attempt any two of the following.

1) Write a python program to reflecte the line segment joining the points A[5,3] & B[1,4] through
the line y=x+1`

Ans:

def reflect_point(x, y, m, c):

d = (x + (y - c) * m) / (1 + m**2)

x_reflect = 2 * d - x

y_reflect = 2 * d * m - y + 2 * c

return x_reflect, y_reflect

A = [5, 3]

B = [1, 4]

m, c = 1, 1

A_reflect = reflect_point(A[0], A[1], m, c)

B_reflect = reflect_point(B[0], B[1], m, c)

print(f"Original point A: {A}, Reflected point A: {A_reflect}")


print(f"Original point B: {B}, Reflected point B: {B_reflect}")

2) Using sympy declare the points P(5,2),Q(5,-2),R(5,0),cheak whether these points are
collinear.Declare the ray passing through the points P and Q find the length of the this ray between P
and Q .Also find slop of this ray

Ans:

import sympy as sp

P = sp.Point(5, 2)

Q = sp.Point(5, -2)

R = sp.Point(5, 0)

collinear = sp.Point.is_collinear(P, Q, R)

print(f"The points P, Q, and R are collinear: {collinear}")

ray_PQ = sp.Ray(P, Q)

length_PQ = P.distance(Q)

print(f"The length of the ray between P and Q is: {length_PQ}")

slope_PQ = (Q.y - P.y) / (Q.x - P.x) if Q.x != P.x else None

print(f"The slope of the ray between P and Q is: {slope_PQ if slope_PQ is not None else 'undefined
(vertical line)'}")

3)Write a pyhton program in 3D to rotate the point A(1,0,0) through X plane anticlockwise
direction(Rotation through Z axis) by angle of 90°

Ans:

import numpy as np
A = np.array([1, 0, 0])

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta), 0],

[np.sin(theta), np.cos(theta), 0],

[0, 0, 1]

])

A_rotated = rotation_matrix @ A

print(f"Original point A: {A}")

print(f"Rotated point A: {A_rotated}")

Q 3) Attempt the following

A) Attempt any one of the following.

3) Write a program to solve the following LPP :

Min Z = 3.5 x +2y

Subject to x + y ≥ 5

x≥4

y≤2

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 3.5 * x + 2 * y

prob += x + y >= 5

prob += x >= 4

prob += y <= 2

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to display following LPP using pulp modulo and simplex method. Findt its
optimal solution if exits.

Max Z= x+2y+1z

Subject to x+2y+2z ≤ 1

3x+2y+z ≥8

x ≥ 0,y ≥ 0, z ≥ 0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.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(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}, z: {pulp.value(z)}")

A) Attempt any one of the following.

1) Apply python program on each of the following transformation of the point P[4,-2]

I) Refection throughX-axis.

II) Scaling in X- coordinate by factor 5.

III) Rotaion about origin through an angle π/2.

IV) Shearing in X direction by 7/2 units.

Ans:

import numpy as np

P = np.array([4, -2])
P_reflect_x = np.array([P[0], -P[1]])

print(f"After Reflection through X-axis: {P_reflect_x}")

P_scale_x = np.array([5 * P[0], P[1]])

print(f"After Scaling in X-coordinate by factor 5: {P_scale_x}")

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

P_rotate_origin = rotation_matrix @ P

print(f"After Rotation about origin through an angle π/2: {P_rotate_origin}")

shear_matrix = np.array([

[1, 7/2],

[0, 1]

])

P_shear_x = shear_matrix @ P

print(f"After Shearing in X direction by 7/2 units: {P_shear_x}")

2)Find the combine transformation of the line segment between the points A[7,-2] and B[6,2] by using
python program for each following sequence of transformation:-

I) Rotation about origin through angle π/3 .

II) Scaling in X -coordinate by 7 units.


III) Uniform Scaling by -4 units.

IV) Reflection through the line X-axis.

Ans:

import numpy as np

A, B = np.array([7, -2]), np.array([6, 2])

theta = np.pi / 3

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

scale_x_matrix = np.array([

[7, 0],

[0, 1]

])

uniform_scale_matrix = np.array([

[-4, 0],

[0, -4]

])

reflection_x_matrix = np.array([

[1, 0],

[0, -1]

])
transform = reflection_x_matrix @ uniform_scale_matrix @ scale_x_matrix @ rotation_matrix

A_transformed = transform @ A

B_transformed = transform @ B

print(f"Original A: {A}, Transformed A: {A_transformed}")

print(f"Original B: {B}, Transformed B: {B_transformed}")

PYTHON PROGRAMMING LANGUAGE=II

SLIP NO:8

Q 1) Attempt any two of the following.

1) Plot the graphs of sin x, cos x,e^x, and x^2 in[0,5] in one figure with(2x2) subplots.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, 5, 100)

fig, axs = plt.subplots(2, 2)

axs[0, 0].plot(x, np.sin(x), 'b-')

axs[0, 1].plot(x, np.cos(x), 'r-')

axs[1, 0].plot(x, np.exp(x), 'g-')

axs[1, 1].plot(x, x**2, 'm-')


axs[0, 0].set_title('sin(x)')

axs[0, 1].set_title('cos(x)')

axs[1, 0].set_title('e^x')

axs[1, 1].set_title('x^2')

plt.show()

2) Using python plot the graph of function f(x)= cos x with interval [0,2π ].

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)

y = np.cos(x)

plt.plot(x, y, 'b-', label='cos(x)')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Graph of $f(x) = \cos(x)$')

plt.legend()

plt.grid(True)

plt.show()

3) Write a python program to generate 3D plot of the functions z= sin x+ cos y in -10<x, y < 10.

Ans:

import numpy as np

import matplotlib.pyplot as plt


from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-10, 10, 400)

y = np.linspace(-10, 10, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x) + np.cos(y)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

ax.set_title('3D plot of $z = \sin(x) + \cos(y)$')

plt.show()

Q 2) Attempt any two of the following.

1) Write a python program in 3D to rotate the point (1,0,0) through xz plane in anticlosewise
direction(Rotation through Y-axis) by angle of 90°

Ans:

import numpy as np

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

theta = np.pi / 2
rotation_matrix = np.array([

[np.cos(theta), 0, np.sin(theta)],

[0, 1, 0],

[-np.sin(theta), 0, np.cos(theta)]

])

A_rotated = rotation_matrix @ A

print(f"Original point A: {A}")

print(f"Rotated point A: {A_rotated}")

2)Using python generate triangle with vertices (0,0),(4,0),(1,4) cheak whether the triangle is Scalene
triangle.

Ans:

import numpy as np

A = np.array([0, 0])

B = np.array([4, 0])

C = np.array([1, 4])

AB = np.linalg.norm(B - A)

BC = np.linalg.norm(C - B)

CA = np.linalg.norm(A - C)

is_scalene = (AB != BC) and (BC != CA) and (CA != AB)

print(f"The triangle with vertices A{A}, B{B}, and C{C} is a scalene triangle: {is_scalene}")

2) Write a python program to find the area and perimeter of the ΔABC where A[0,0],B[6,0],C[4,4].
Ans:

import numpy as np

A, B, C = np.array([0, 0]), np.array([6, 0]), np.array([4, 4])

AB, BC, CA = np.linalg.norm(B - A), np.linalg.norm(C - B), np.linalg.norm(A - C)

perimeter = AB + BC + CA

area = 0.5 * np.abs(A[0]*(B[1]-C[1]) + B[0]*(C[1]-A[1]) + C[0]*(A[1]-B[1]))

print(f"Perimeter: {perimeter}, Area: {area}")

Q 3)Attempt the following.

B) Attempt any one of the following.

1)Write the python program to solve the following LPP.

Max Z= 150x + 75y

Subject to 4x + 6y ≤ 24

5x + 3y ≤ 15

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)
prob += 150 * x + 75 * y

prob += 4 * x + 6 * y <= 24

prob += 5 * x + 3 * y <= 15

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

3) Write a program to display the following LPP using pulp modulo and simplex method. Find its
opmtimal solution if exits.

Max Z=3x + 2y + 4z

Subject to 2x + 3y ≤ 8

2x + 5y ≤ 10

3x + 2y 4z ≤ 15

x ≥ 0,y ≥ 0,z ≥ 0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)
prob += 3*x + 2*y + 4*z

prob += 2*x + 3*y <= 8

prob += 2*x + 5*y <= 10

prob += 3*x + 2*y + 4*z <= 15

prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}, z: {pulp.value(z)}")

B) Attempt any one of the following.

1) Apply python program in each of the following transformation of the point [4-2]

I) Refection through Y-axis .

II) Scaling in X-coordinate by factor 3

III) Rotaion about oringin through an angle π

IV) Shearing in both X and Y direction by -2 and 4 units respectively

Ans:

import numpy as np

P = np.array([4, -2])

P_reflect_y = np.array([-P[0], P[1]])

print(f"After Reflection through Y-axis: {P_reflect_y}")


P_scale_x = np.array([3 * P[0], P[1]])

print(f"After Scaling in X-coordinate by factor 3: {P_scale_x}")

theta = np.pi

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

P_rotate_origin = rotation_matrix @ P

print(f"After Rotation about origin through an angle π: {P_rotate_origin}")

shear_matrix = np.array([

[1, -2],

[4, 1]

])

P_shear = shear_matrix @ P

print(f"After Shearing in both X and Y direction by -2 and 4 units respectively: {P_shear}")

2) Find the combine transformation of the line segment between the pointsA[4,-1] and & B[3,2]by
using python program for the following sequence of transformation:-

I) Rotaion about origin through an angle π /4 .

II) Shearing in Y direction by 4 units.

III) Scaling in X coordinateby 5 units.

IV) Refection through Y-axis

Ans:
import numpy as np

A = np.array([4, -1])

B = np.array([3, 2])

transform = np.array([

[-5, 20],

[-5, 1]

]) @ np.array([

[np.cos(np.pi / 4), -np.sin(np.pi / 4)],

[np.sin(np.pi / 4), np.cos(np.pi / 4)]

])

A_transformed = transform @ A

B_transformed = transform @ B

print(f"Original A: {A}, Transformed A: {A_transformed}")

print(f"Original B: {B}, Transformed B: {B_transformed}")


PYTHON PROGRAMMING LANGUAGE=II

SLIP NO:9

Q 1) Attempt any two of the following.

1) Write a python program to plot 2D X-axis and Y-axis black clolour and in the same diagram in plot
green triangle with vertices [5,4[,[7,4],[6,6].

Ans:

import matplotlib.pyplot as plt

triangle_vertices = [[5, 4], [7, 4], [6, 6]]

fig, ax = plt.subplots()

ax.axhline(0, color='black', linewidth=1)

ax.axvline(0, color='black', linewidth=1)

triangle = plt.Polygon(triangle_vertices, color='green', edgecolor='black')

ax.add_patch(triangle)

ax.set_xlim(0, 10)

ax.set_ylim(0, 10)
ax.grid(True)

ax.set_aspect('equal', adjustable='box')

plt.show()

2) Write a python program to rotate the point through yz plane in anticlockewise direction(Rotation
through Y-axis) by angle of 90°

Ans:

import numpy as np

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

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), 0, np.sin(theta)],

[0, 1, 0],

[-np.sin(theta), 0, np.cos(theta)]

])

P_rotated = rotation_matrix @ P

print(f"Original point P: {P}")

print(f"Rotated point P: {P_rotated}")

3)Using python plot the graph of function f(x)= cos x with interval [0,2π ].

Ans:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)

y = np.cos(x)

plt.plot(x, y, 'b-', label='cos(x)')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Graph of $f(x) = \cos(x)$')

plt.legend()

plt.grid(True)

plt.show()

Q 2) Attempt any two of the following .

1) Write apython program to rotate the rays by 90° having starting point(1,0) and (2,-1).

Ans:

import numpy as np

P1, P2 = np.array([1, 0]), np.array([2, -1])

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])
P1_rotated = rotation_matrix @ P1

P2_rotated = rotation_matrix @ P2

print(f"Original point P1: {P1}, Rotated point P1: {P1_rotated}")

print(f"Original point P2: {P2}, Rotated point P2: {P2_rotated}")

2)Using sympy declare the points A(0,7),B(5,-2).Declare the line segment passing through them.Find the
length and mid point o fthe line segment passing through points A and B.

Ans:

import sympy as sp

A, B = sp.Point(0, 7), sp.Point(5, -2)

line_segment = sp.Segment(A, B)

length = line_segment.length

midpoint = line_segment.midpoint

print(f"Length of the line segment: {length}")

print(f"Midpoint of the line segment: {midpoint}")

2) Write a python program to find the area and perimeter ΔABC where A(0,0),B(5,0),C(3,3).

Ans:

import numpy as np

A, B, C = np.array([0, 0]), np.array([5, 0]), np.array([3, 3])

AB = np.linalg.norm(B - A)

BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)

perimeter = AB + BC + CA

area = 0.5 * np.abs(A[0] * (B[1] - C[1]) + B[0] * (C[1] - A[1]) + C[0] * (A[1] - B[1]))

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

Q 3)Attempt the following.

A) Attempt any one of the following.

1)Write the python program to solve the following LPP.

Max Z= 150x + 75y

Subject to 4x + 6y ≤ 24

5x + 3y ≤ 15

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 150 * x + 75 * y
prob += 4 * x + 6 * y <= 24

prob += 5 * x + 3 * y <= 15

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to display the following LPP using pulp modulo and simplex method ,Find it's
optimal solution if exits .

Max Z= 4x + y + 3z +5w

Subject to 4x +6y -5z -4w ≥ 20

-3x - 2y +4z + w ≤ 10

-8x - 3y + 3z + 2w ≤ 20

x + y ≤ 11

x,y,z,w ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

w = pulp.LpVariable('w', lowBound=0)
prob += 4 * x + y + 3 * z + 5 * w

prob += 4 * x + 6 * y - 5 * z - 4 * w >= 20

prob += -3 * x - 2 * y + 4 * z + w <= 10

prob += -8 * x - 3 * y + 3 * z + 2 * w <= 20

prob += x + y <= 11

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal z: {pulp.value(z)}")

print(f"Optimal w: {pulp.value(w)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

B) Attempt any one of the following.

1) Write a python program to apply the following transformations on the following point (2,-4) :

I) Shearing in Y direction by 7 units.

II) Scaling in X and Y direction by 7/2 and 7 units repsectively.

III) Scaling in X and Y direction by 4and 7 units repsectively.

IV) Rotation about origin by angle 60°

Ans:

import numpy as np
P = np.array([2, -4])

P_shear_y = np.array([P[0], P[1] + 7 * P[0]])

P_scale_1 = np.array([7/2 * P[0], 7 * P[1]])

P_scale_2 = np.array([4 * P[0], 7 * P[1]])

theta = np.pi / 3

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

P_rotate = rotation_matrix @ P

print(f"Original point: {P}")

print(f"After Shearing in Y direction by 7 units: {P_shear_y}")

print(f"After Scaling in X and Y direction by 7/2 and 7 units respectively: {P_scale_1}")

print(f"After Scaling in X and Y direction by 4 and 7 units respectively: {P_scale_2}")

print(f"After Rotation about origin by angle 60°: {P_rotate}")

2) Write a python program to find the combined transformation of the line segemnt between the
points A[5,3] and B[1,4] for the following sequence of transformations:

I) Rotate about origin through an angle π/2.

II) Uniform scaling by -3.5 units.

III) Scaling in Y axis by 5 units.

IV) Shearing in X and Y direction by 3 and 4 unit respectively.

Ans:

import numpy as np
A, B = np.array([5, 3]), np.array([1, 4])

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

uniform_scale_matrix = np.array([

[-3.5, 0],

[0, -3.5]

])

scale_y_matrix = np.array([

[1, 0],

[0, 5]

])

shear_matrix = np.array([

[1, 3],

[4, 1]

])

combined_transform = shear_matrix @ scale_y_matrix @ uniform_scale_matrix @ rotation_matrix

A_transformed = combined_transform @ A

B_transformed = combined_transform @ B

print(f"Original point A: {A}, Transformed point A: {A_transformed}")


print(f"Original point B: {B}, Transformed point B: {B_transformed}")

PYTHON PROGRAMMING LANGUAGE=II

SLIP NO:10

Q 1) Attempt any two of the following.

1)Write a python program in 3D to rotate the point (1,0,0) through xz plane in anticlosewise
direction(Rotation through Y-axis) by angle of 90°

Ans:

import numpy as np

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

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), 0, np.sin(theta)],

[0, 1, 0],
[-np.sin(theta), 0, np.cos(theta)]

])

A_rotated = rotation_matrix @ A

print(f"Original point A: {A}")

print(f"Rotated point A: {A_rotated}")

2)Using python,represent the following information using bar graph (colour=green).

Item =[clothing ,food ,rent, petrol,misc]

Expenditure in Rs=[600,4000,2000,1500,700]

Ans:

import matplotlib.pyplot as plt

items = ["Clothing", "Food", "Rent", "Petrol", "Misc"]

expenditure = [600, 4000, 2000, 1500, 700]

# Plotting the bar graph

plt.bar(items, expenditure, color='green')

plt.title("Expenditure Chart", fontsize=14)

plt.xlabel("Items", fontsize=12)

plt.ylabel("Expenditure in Rs", fontsize=12)

plt.show()

3)Write a python program to plot the 3D line graph whose parametric equation is (cos (2x),sin(2x),x) for
10 ≤ x ≤ 20 (in red colour ) with title to the graph.

Ans:

import numpy as np

import matplotlib.pyplot as plt


from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(10, 20, 400)

X = np.cos(2 * x)

Y = np.sin(2 * x)

Z=x

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot(X, Y, Z, color='red')

ax.set_title('3D Line Graph of the Parametric Equation (cos(2x), sin(2x), x)')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

plt.show()

Q 2)Attempt any two of the following.

1)Write a python program to rotate the ΔABC by 90° where A(1,1),B(2,-2),C(1,2).

Ans:

import numpy as np

A, B, C = np.array([1, 1]), np.array([2, -2]), np.array([1, 2])

theta = np.pi / 2
rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

A_rotated = rotation_matrix @ A

B_rotated = rotation_matrix @ B

C_rotated = rotation_matrix @ C

print(f"Original A: {A}, Rotated A: {A_rotated}")

print(f"Original B: {B}, Rotated B: {B_rotated}")

print(f"Original C: {C}, Rotated C: {C_rotated}")

3) Draw apolygon with vertices (0,0),(2,0),(2,3),(1,6).Write a python program to rotate the python
program by 180°.

Ans:

import numpy as np

import matplotlib.pyplot as plt

vertices = np.array([[0, 0], [2, 0], [2, 3], [1, 6]])

plt.fill(*zip(*vertices), color='lightblue', alpha=0.5, label='Original Polygon')

theta = np.pi

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])
vertices_rotated = np.dot(vertices, rotation_matrix)

plt.fill(*zip(*vertices_rotated), color='lightgreen', alpha=0.5, label='Rotated Polygon')

plt.legend()

plt.gca().set_aspect('equal', adjustable='box')

plt.title('Polygon Rotation by 180°')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.show()

4) Find the area and perimeter of the ΔABC,where A[0,0],B[5,0],C[3,3].

Ans:

import numpy as np

A, B, C = np.array([0, 0]), np.array([5, 0]), np.array([3, 3])

# Calculate the lengths of the sides

AB = np.linalg.norm(B - A)

BC = np.linalg.norm(C - B)

CA = np.linalg.norm(A - C)

# Calculate the perimeter

perimeter = AB + BC + CA
# Calculate the area using the determinant method

area = 0.5 * np.abs(A[0] * (B[1] - C[1]) + B[0] * (C[1] - A[1]) + C[0] * (A[1] - B[1]))

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

Q 3)Attempt the following.

A) Attempt any one of the following.

1) Solve following LPP using python:

Max Z=x+y

Subject to x-y ≥ 1

x+y ≥ 2

x ≥ 0,y ≥ 0

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 5, 400)

y1 = x - 1

y2 = 2 - x

plt.figure(figsize=(8, 8))

plt.plot(x, y1, label=r'$x - y \geq 1$', color='blue')

plt.plot(x, y2, label=r'$x + y \geq 2$', color='red')

plt.fill_between(x, np.maximum(y1, 0), np.maximum(y2, 0), where=(y2 >= y1), color='lightgreen',


alpha=0.5)

plt.xlim(0, 5)

plt.ylim(0, 5)

plt.axhline(0, color='black', lw=1)


plt.axvline(0, color='black', lw=1)

plt.xlabel('x')

plt.ylabel('y')

plt.title('Feasible Region for the LPP')

plt.legend()

plt.grid(True)

plt.show()

2)Write a python program to display the following LPP using pulp modulo and simplex method .Find its
optimal solution if exits.

Max Z= 3x + 2y + 5z

Subject to x + 2y +z ≤ 430

3x + 2z ≤ 460

x + 4y ≤ 120

x ≥ 0,y ≥ 0,z ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

prob += 3 * x + 2 * y + 5 * z

prob += x + 2 * y + z <= 430


prob += 3 * x + 2 * z <= 460

prob += x + 4 * y <= 120

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal z: {pulp.value(z)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

A) Attempt any one of the following.

1) Write a python program to apply following transformation on the point(-2,4):

I) Reflection through X axis.

II) Scaling in X coordinate by factor 6.

III) Shearing in X direction by 4 units.

IV) Rotation about origin through an angle 30°.

Ans:

import numpy as np

P = np.array([-2, 4])

P_reflect_x = np.array([P[0], -P[1]])

P_scale_x = np.array([6 * P[0], P[1]])

P_shear_x = np.array([P[0] + 4 * P[1], P[1]])

theta = np.pi / 6

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]

])

P_rotate = rotation_matrix @ P

print(f"Original point: {P}")

print(f"After Reflection through X axis: {P_reflect_x}")

print(f"After Scaling in X coordinate by factor 6: {P_scale_x}")

print(f"After Shearing in X direction by 4 units: {P_shear_x}")

print(f"After Rotation about origin through angle 30°: {P_rotate}")

2) Write apython program to find the combined transformation between the points for the
following sequence of transformations:

I) Rotation about origin through an angle π / 2.

II) Uniform scaling by 3.5 units.

III) Scaling in X & Y coordinte by 3 & 5 unit respectively.

IV) Shreaing in X direction by 6 unit.

Ans:

import numpy as np

A, B = np.array([1, 2]), np.array([3, 4])

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

uniform_scale_matrix = np.array([
[3.5, 0],

[0, 3.5]

])

scale_matrix = np.array([

[3, 0],

[0, 5]

])

shear_matrix = np.array([

[1, 6],

[0, 1]

])

combined_transform = shear_matrix @ scale_matrix @ uniform_scale_matrix @ rotation_matrix

A_transformed = combined_transform @ A

B_transformed = combined_transform @ B

print(f"Original A: {A}, Transformed A: {A_transformed}")

print(f"Original B: {B}, Transformed B: {B_transformed}")

PYTHON PROGRAMMIMG LANGUAGE=II

SLIP NO:11
Q 1)Attempt any two of the following.

1) Write a python program to plot 3Daxes with labels as X-axis,Y-axis and Z-axis and also
plotfollowing point with coordinates in the same graph :(70,-25,15) as adiamond in black colour.

Ans:

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

x, y, z = 70, -25, 15

ax.scatter(x, y, z, color='black', marker='D')

plt.show()

2) Plot the graph of y=e^x in[-5,5] with red dashed points line with upward pointing triangle.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 400)

y = np.exp(x)

plt.plot(x, y, 'r^--', label='$y=e^x$')


plt.title('Graph of $y=e^x$')

plt.xlabel('x')

plt.ylabel('y')

plt.legend()

plt.show()

3)Using python ,draw a bar graph in GREEN to represent the data below:

Subject =[Math, Scinece,English ,Marathi ,Hindi].

Percentage of passing =[68,90,70,85,91].

Ans:

import matplotlib.pyplot as plt

subjects = ['Math', 'Science', 'English', 'Marathi', 'Hindi']

percentages = [68, 90, 70, 85, 91]

plt.bar(subjects, percentages, color='green')

plt.title("Percentage of Passing per Subject")

plt.xlabel("Subjects")

plt.ylabel("Percentage")

plt.show()

Q 2)Attempt any two of the following.

1) Write a python program to reflect the ΔABC through the line y=3 where A(1,0),B(2,-1),C(-1,3).

Ans:import numpy as np
A, B, C = np.array([1, 0]), np.array([2, -1]), np.array([-1, 3])

reflection_matrix = np.array([[1, 0], [0, -1]])

translation_matrix = np.array([0, 6])

A_reflected = reflection_matrix @ A + translation_matrix

B_reflected = reflection_matrix @ B + translation_matrix

C_reflected = reflection_matrix @ C + translation_matrix

print(f"Original A: {A}, Reflected A: {A_reflected}")

print(f"Original B: {B}, Reflected B: {B_reflected}")

print(f"Original C: {C}, Reflected C: {C_reflected}")

2) Write a python program to rotate the ΔABC by 90° where A(1,2),B(2,-2),C(-1,2).

Ans:

import numpy as np

A, B, C = np.array([1, 2]), np.array([2, -2]), np.array([-1, 2])

theta = np.pi / 2

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

A_rotated = rotation_matrix @ A

B_rotated = rotation_matrix @ B
C_rotated = rotation_matrix @ C

print(f"Original A: {A}, Rotated A: {A_rotated}")

print(f"Original B: {B}, Rotated B: {B_rotated}")

print(f"Original C: {C}, Rotated C: {C_rotated}")

3) Write a python program to draw a polygon with 6 side and radius 1 centered at(1,2) find and its
area and perimeter .

Ans:

import numpy as np

import matplotlib.pyplot as plt

center = [1, 2]

radius = 1

angles = np.linspace(0, 2 * np.pi, 7)

vertices = np.array([center[0] + radius * np.cos(angles), center[1] + radius * np.sin(angles)]).T

plt.fill(vertices[:, 0], vertices[:, 1], 'lightblue', alpha=0.5)

plt.plot(vertices[:, 0], vertices[:, 1], 'b-')

plt.scatter(center[0], center[1], color='red')

plt.gca().set_aspect('equal', adjustable='box')

plt.title('Hexagon')

plt.show()

area = 3 * np.sqrt(3) / 2 * radius**2

perimeter = 6 * radius
print(f"Area: {area}")

print(f"Perimeter: {perimeter}")

Q 3) Attempt any following.

B) Attempt any one of the following.

1)Write a python program to display the the following LPP using pulp modulo and simplex method . Find
the optimal solution if exits.

Min Z= x + y

Subject to x ≥ 6

y≥6

x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11

prob.solve(pulp.PULP_CBC_CMD())
print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}")

2)Write a program to display the following LPP using pulp modulo and simplex method. Find its
opmtimal solution if exits.

Max Z=3x + 2y + 4z

Subject to 2x + 3y ≤ 8

2x + 5y ≤ 10

3x + 2y 4z ≤ 15

x ≥ 0,y ≥ 0,z ≥ 0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

prob += 3*x + 2*y + 4*z

prob += 2*x + 3*y <= 8

prob += 2*x + 5*y <= 10

prob += 3*x + 2*y + 4*z <= 15


prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}, z: {pulp.value(z)}")

B) Attempt any one of the following.

1) Write a python program to apply the following transformations of the following(2,-4):

I) Shearing in Y direction by 7 units.

II) Scaling in x and y direction by 3/2 and 4 units respectively.

III) Shearing x and y direction by 2 and 4 units respectively

IV) Rotation about origin by an angle 45°

Ans:

import numpy as np

P = np.array([2, -4])

P_shear_y = np.array([P[0], P[1] + 7 * P[0]])

P_scale_1 = np.array([3/2 * P[0], 4 * P[1]])

P_shear_2 = np.array([P[0] + 2 * P[1], 4 * P[0] + P[1]])

theta = np.pi / 4

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

P_rotate = rotation_matrix @ P
print(f"Original point: {P}")

print(f"After Shearing in Y direction by 7 units: {P_shear_y}")

print(f"After Scaling in X and Y direction by 3/2 and 4 units respectively: {P_scale_1}")

print(f"After Shearing in X and Y direction by 2 and 4 units respectively: {P_shear_2}")

print(f"After Rotation about origin by angle 45°: {P_rotate}")

2) Write a python to find the combined transformation of the line segment between the points
A[3,2] and B[2,-3] for the following sequence of transformation:

I) Rotation about origin through an angle π/6.

II) Scaling in Y-coordinate by -4 units.

III) Uniform scaling by-6.4 units.

IV) Shearing in Y direction by 5 units.

Ans:

import numpy as np

A, B = np.array([3, 2]), np.array([2, -3])

theta = np.pi / 6

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

scale_y_matrix = np.array([

[1, 0],

[0, -4]

])
uniform_scale_matrix = np.array([

[-6.4, 0],

[0, -6.4]

])

shear_y_matrix = np.array([

[1, 0],

[5, 1]

])

combined_transform = shear_y_matrix @ uniform_scale_matrix @ scale_y_matrix @ rotation_matrix

A_transformed = combined_transform @ A

B_transformed = combined_transform @ B

print(f"Original point A: {A}, Transformed point A: {A_transformed}")

print(f"Original point B: {B}, Transformed point B: {B_transformed}")

PYTHON PROGRAMMING LANGUAGE = II

SLIP NO: 12
Q 1) Attempt any two of the following.

1) Write a python program to plot the graph of y= x^3 +10x-5, for x∈[-10,10] in red colour .

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-10, 10, 400)

y = x**3 + 10*x - 5

plt.plot(x, y, 'r', label='$y=x^3 + 10x - 5$')

plt.title('Graph of $y=x^3 + 10x - 5$')

plt.xlabel('x')

plt.ylabel('y')

plt.legend()

plt.grid(True)

plt.show()

2)Write a python program in 3D to rotate the point (1,0,0) through xz plane in anticlosewise
direction(Rotation through Y-axis) by angle of 90°

Ans:

import numpy as np

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

theta = np.pi / 2

rotation_matrix = np.array([
[np.cos(theta), 0, np.sin(theta)],

[0, 1, 0],

[-np.sin(theta), 0, np.cos(theta)]

])

A_rotated = rotation_matrix @ A

print(f"Original point A: {A}")

print(f"Rotated point A: {A_rotated}")

3)Using python plot the graph of function f(x)=x^2 on the interval [2,-2].

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(2, -2, 400)

y = x**2

plt.plot(x, y, 'b', label='$f(x)=x^2$')

plt.title('Graph of $f(x)=x^2$')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.legend()

plt.grid(True)

plt.show()
Q 2)Attempt any two of the following.

1) Write a python program to rotate the segment by 180° having end points(1,0) and(2,-1).

Ans:

import numpy as np

A, B = np.array([1, 0]), np.array([2, -1])

theta = np.pi # 180 degrees in radians

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

A_rotated = rotation_matrix @ A

B_rotated = rotation_matrix @ B

print(f"Original A: {A}, Rotated A: {A_rotated}")

print(f"Original B: {B}, Rotated B: {B_rotated}")

2)Write a python program to draw polygon with 8 sides having radius 5 centered and origin and findits
area and perimeter.

Ans:

import numpy as np

import matplotlib.pyplot as plt

sides, radius = 8, 5

angles = np.linspace(0, 2 * np.pi, sides + 1)


vertices = np.array([radius * np.cos(angles), radius * np.sin(angles)]).T

plt.fill(vertices[:, 0], vertices[:, 1], 'lightblue', alpha=0.5)

plt.plot(vertices[:, 0], vertices[:, 1], 'b-')

plt.gca().set_aspect('equal', adjustable='box')

plt.title('Polygon with 8 sides and radius 5')

plt.show()

area = 0.5 * sides * radius**2 * np.sin(2 * np.pi / sides)

perimeter = sides * 2 * radius * np.sin(np.pi / sides)

print(f"Area: {area}")

print(f"Perimeter: {perimeter}")

2) Write apython program to find area and perimeter of ΔXYZ where X(1,2),Y(2,-2),Z(-1,2)

Ans:

import numpy as np

X, Y, Z = np.array([1, 2]), np.array([2, -2]), np.array([-1, 2])

a = np.linalg.norm(Y - Z)

b = np.linalg.norm(X - Z)

c = np.linalg.norm(X - Y)

perimeter = a + b + c
s = perimeter / 2

area = np.sqrt(s * (s - a) * (s - b) * (s - c))

print(f"Length of sides: a = {a}, b = {b}, c = {c}")

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

Q 3) Attempt the following.

A) Attempt any one of the following.

4) Write a program to solve the following LPP :

Min Z = 3.5 x +2y

Subject to x + y ≥ 5

x≥4

y≤2

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 3.5 * x + 2 * y

prob += x + y >= 5
prob += x >= 4

prob += y <= 2

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a program to display the following LPP using pulp modulo and simplex method. Find its
opmtimal solution if exits.

Max Z=3x + 2y + 4z

Subject to 2x + 3y ≤ 8

2x + 5y ≤ 10

3x + 2y 4z ≤ 15

x ≥ 0,y ≥ 0,z ≥ 0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

prob += 3*x + 2*y + 4*z


prob += 2*x + 3*y <= 8

prob += 2*x + 5*y <= 10

prob += 3*x + 2*y + 4*z <= 15

prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}, z: {pulp.value(z)}")

B)Attempt any one of the following.

1)Write a python program to apply following transformation on the point(-2,4):

I)Reflection through Y axis.

II)Scaling in X coordinate by factor 6.

III)Shearing in X direction by 4.1 units.

IV)Scaling in X coordinate by factor 7/2.

Ans:

x, y = -2, 4

# I) Reflection through X axis

x_reflected = -x

y_reflected = -y

# II) Scaling in X coordinate by factor 6


x_scaled_6 = x_reflected * 6

# III) Shearing in X direction by 4 units

x_sheared = x_scaled_6 + 4 * y_reflected

# IV) Scaling by 7/2

x_scaled_7_2 = x_sheared * (7 / 2)

y_scaled_7_2 = y_reflected * (7 / 2)

print(f"Final Transformed Point: ({x_scaled_7_2}, {y_scaled_7_2})")

3) Write a python program to findthe combined transformation of the line between the A[4,1] &
B[-3,0] for the following sequence to transformations:

I) Rotaion about origin through angle π / 4.

II) Uniform scaling by 7.3 units.

III) Scaling in X-coordinate by 3 units.

IV) Shearing in direction by 1/2 units.

Ans:

import numpy as np

A, B = np.array([4, 1]), np.array([-3, 0])

theta = np.pi / 4

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])
uniform_scale_matrix = np.array([

[7.3, 0],

[0, 7.3]

])

scale_x_matrix = np.array([

[3, 0],

[0, 1]

])

shear_matrix = np.array([

[1, 0.5],

[0, 1]

])

combined_transform = shear_matrix @ scale_x_matrix @ uniform_scale_matrix @ rotation_matrix

A_transformed = combined_transform @ A

B_transformed = combined_transform @ B

print(f"Original A: {A}, Transformed A: {A_transformed}")

print(f"Original B: {B}, Transformed B: {B_transformed}")


PYTHON PROGRAMMING LANGUAGE=II

SLIP NO: 13

Q 1)Atteempt any two of the following.

1)Write a python program to plot 2D graph of the functions f(x)= x^2 and g(x) in [-1,1].

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 100)

plt.plot(x, x**2, label="f(x) = x^2")

plt.plot(x, x, label="g(x) = x")

plt.axhline(0, color='black', linestyle="--", linewidth=0.8)

plt.axvline(0, color='black', linestyle="--", linewidth=0.8)

plt.legend()

plt.grid()

plt.show()

2) Using python plot the surface plot the parabola z= x^2+y^2 in -6<x,y<6.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-6, 6, 400)
y = np.linspace(-6, 6, 400)

x, y = np.meshgrid(x, y)

z = x**2 + y**2

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

ax.set_title('Surface plot of $z = x^2 + y^2$')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

plt.show()

3)Write a python program to plot the 3D line graph whose parametric equation is (cos (2x),sin(2x),x) for
10 ≤ x ≤ 20 (in red colour ) with title to the graph.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(10, 20, 400)

X = np.cos(2 * x)

Y = np.sin(2 * x)

Z=x
fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot(X, Y, Z, color='red')

ax.set_title('3D Line Graph of the Parametric Equation (cos(2x), sin(2x), x)')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

plt.show()

Q 2)Attempt any two of the following.

1)Write a python program to reflect the ΔABC through the line y=3 where A(1,0),B(2,-1),C(-1,3).

Ans:import numpy as np

A, B, C = np.array([1, 0]), np.array([2, -1]), np.array([-1, 3])

reflection_matrix = np.array([[1, 0], [0, -1]])

translation_matrix = np.array([0, 6])

A_reflected = reflection_matrix @ A + translation_matrix

B_reflected = reflection_matrix @ B + translation_matrix

C_reflected = reflection_matrix @ C + translation_matrix

print(f"Original A: {A}, Reflected A: {A_reflected}")

print(f"Original B: {B}, Reflected B: {B_reflected}")

print(f"Original C: {C}, Reflected C: {C_reflected}")


2)Find the area and perimeter of the ΔABC,where A[0,0],B[5,0],C[3,3].

Ans:

import numpy as np

A, B, C = np.array([0, 0]), np.array([5, 0]), np.array([3, 3])

# Calculate the lengths of the sides

AB = np.linalg.norm(B - A)

BC = np.linalg.norm(C - B)

CA = np.linalg.norm(A - C)

# Calculate the perimeter

perimeter = AB + BC + CA

# Calculate the area using the determinant method

area = 0.5 * np.abs(A[0] * (B[1] - C[1]) + B[0] * (C[1] - A[1]) + C[0] * (A[1] - B[1]))

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

3) Using sympy declare the points P(5,2),(5,-2),R(5,0) cheak whether these points are
collinear.Declare the ray passing through the points and P and Q. find the length of this ray between P
and Q .Also find slop of this ray .

Ans:

import sympy as sp

P = sp.Point(5, 2)
Q = sp.Point(5, -2)

R = sp.Point(5, 0)

collinear = sp.Point.is_collinear(P, Q, R)

print(f"Are points P, Q, and R collinear? {collinear}")

ray = sp.Ray(P, Q)

length_PQ = P.distance(Q)

print(f"Length of the ray between P and Q: {length_PQ}")

slope_PQ = sp.Rational(Q[1] - P[1], Q[0] - P[0]) if Q[0] != P[0] else 'undefined'

print(f"Slope of the ray: {slope_PQ}")

1)Write a python program solvethe following LPP:

Max Z=5x + 3y

Subject to x + y ≤ 7

2x + 5y ≤ 1

x ≥ 0,y ≥0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)
prob += 5 * x + 3 * y

prob += x + y <= 7

prob += 2 * x + 5 * y <= 1

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to display the following LPP using pulp modulo and simplex method .Find its
optimal solution if exits.

Max Z= 3x + 2y + 5z

Subject to x + 2y +z ≤ 430

3x + 2z ≤ 460

x + 4y ≤ 120

x ≥ 0,y ≥ 0,z ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)
prob += 3 * x + 2 * y + 5 * z

prob += x + 2 * y + z <= 430

prob += 3 * x + 2 * z <= 460

prob += x + 4 * y <= 120

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal z: {pulp.value(z)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

B) Attempt any one of the following.

1) Write a python program to apply the following transformation on the points[-2,4].

I) Refection through Y-axis.

II) Scaling in X-coordinante by factor 6.

III) Scaling in Y-coordinate by factor 4.1

IV) Shrearing in X direction 7/2 units.

Ans:

import numpy as np

P = np.array([-2, 4])

P_reflect_y = np.array([-P[0], P[1]])


P_scale_x = np.array([6 * P_reflect_y[0], P_reflect_y[1]])

P_scale_y = np.array([P_scale_x[0], 4.1 * P_scale_x[1]])

P_shear_x = np.array([P_scale_y[0] + (7 / 2) * P_scale_y[1], P_scale_y[1]])

print(f"Original point: {P}")

print(f"After Reflection through Y-axis: {P_reflect_y}")

print(f"After Scaling in X-coordinate by factor 6: {P_scale_x}")

print(f"After Scaling in Y-coordinate by factor 4.1: {P_scale_y}")

print(f"After Shearing in X direction by 7/2 units: {P_shear_x}")

2) Write a python program to find the combined transformation on the line segment between the
points A[4,1] & B[-3,0] for the following sequence of transformation.

I) Rotation about orinin through an angle π/4.

II) Uniform Scaling by 7.09 units.

III) Scaling in Coordinate by 3units.

IV) Shearing in Xdirection by 1/2 units.

Ans:

import numpy as np

A, B = np.array([4, 1]), np.array([-3, 0])

theta = np.pi / 4

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]

])

uniform_scale_matrix = np.array([
[7.09, 0],

[0, 7.09]

])

scale_matrix = np.array([

[3, 0],

[0, 3]

])

shear_matrix = np.array([

[1, 0.5],

[0, 1]

])

combined_transform = shear_matrix @ scale_matrix @ uniform_scale_matrix @ rotation_matrix

A_transformed = combined_transform @ A

B_transformed = combined_transform @ B

print(f"Original point A: {A}, Transformed point A: {A_transformed}")

print(f"Original point B: {B}, Transformed point B: {B_transformed}")

PYHTON PROGRAMMING LANGUAGE=II

SLIP NO:14
Q 1)Attempt any two of the following.

1)Write a python program to plot 2D graph of the functions f(x)= x^2 and g(x) in [-1,1].

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 100)

plt.plot(x, x**2, label="f(x) = x^2")

plt.plot(x, x, label="g(x) = x")

plt.axhline(0, color='black', linestyle="--", linewidth=0.8)

plt.axvline(0, color='black', linestyle="--", linewidth=0.8)

plt.legend()

plt.grid()

4) Write a python program to plot 3D graph of the functions f(x)= e^(-x^2) in [-5,5] with green dashed
points line the upward pointing triangle.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 400)

y = np.exp(-x**2)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot(x, y, 'g^--')
ax.set_title('3D graph of $f(x) = e^{-x^2}$')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

plt.show()

4) Write a python program to generate 3D plot of the functions z= sin x+ cos y in -5<x, y < 5.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-5, 5, 400)

y = np.linspace(-5, 5, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x) + np.cos(y)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

ax.set_title('3D plot of $z = \sin(x) + \cos(y)$')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')
plt.show()

Q 2)Attempt any two of the following.

1) Write a python program to reflect the line segment joining the points A[5,3] and B[1,4] through the
line y=x+1.

Ans:

import numpy as np

A, B = np.array([5, 3]), np.array([1, 4])

def reflect_through_line(point, a, b, c):

x, y = point

d = (x + (y - c) / b - a / b * y) / (1 + a / b * a / b)

x_reflected = 2 * d - x

y_reflected = a / b * x_reflected - c / b

return np.array([x_reflected, y_reflected])

A_reflected = reflect_through_line(A, 1, -1, -1)

B_reflected = reflect_through_line(B, 1, -1, -1)

print(f"Original point A: {A}, Reflected point A: {A_reflected}")

print(f"Original point B: {B}, Reflected point B: {B_reflected}")

2)Write a python program to draw polygon with vertices(0,0),(2,0),(2,3) and (1,6) and rotate it by 180°.

Ans:

import matplotlib.pyplot as plt

import numpy as np
v = np.array([[0, 0], [2, 0], [2, 3], [1, 6], [0, 0]])

r = np.array([[-1, 0], [0, -1]])

plt.plot(v[:, 0], v[:, 1], 'bo-', label='Original')

plt.plot((v @ r.T)[:, 0], (v @ r.T)[:, 1], 'ro-', label='Rotated 180°')

plt.axhline(0, color='black', lw=0.5)

plt.axvline(0, color='black', lw=0.5)

plt.gca().set_aspect('equal')

plt.legend()

plt.show()

3)Write a python program to find the area and perimeter of the triangle ABC where A[0,0],B[5,0] and
C[3,3].

Ans:

import numpy as np

A = np.array([0, 0])

B = np.array([5, 0])

C = np.array([3, 3])

a = np.linalg.norm(B - C)

b = np.linalg.norm(A - C)

c = np.linalg.norm(A - B)

perimeter = a + b + c
area = 0.5 * abs(A[0]*(B[1] - C[1]) + B[0]*(C[1] - A[1]) + C[0]*(A[1] - B[1]))

print(f"Length of sides: a = {a}, b = {b}, c = {c}")

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

Q 3) Attempt the following.

A) Attempt any one of the following.

1)Write the python program to solve the following LPP.

Max Z= 150x + 75y

Subject to 4x + 6y ≤ 24

5x + 3y ≤ 15

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 150 * x + 75 * y

prob += 4 * x + 6 * y <= 24

prob += 5 * x + 3 * y <= 15
prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to solve the following LPP:

Min Z= x + y

Subject to x ≥ 6

y≥6

x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11
prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}")

B)Attempt any one of following.

1)Write a python program to apply the following transformations of the following(2,-4):

V) Shearing in Y direction by 7 units.

VI) Scaling in x and y direction by 3/2 and 4 units respectively.

VII) Shearing x and y direction by 2 and 4 units respectively

VIII) Rotation about origin by an angle 45°

Ans:

import numpy as np

P = np.array([2, -4])

P_shear_y = np.array([P[0], P[1] + 7 * P[0]])

P_scale_1 = np.array([3/2 * P[0], 4 * P[1]])

P_shear_2 = np.array([P[0] + 2 * P[1], 4 * P[0] + P[1]])

theta = np.pi / 4

rotation_matrix = np.array([

[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]
])

P_rotate = rotation_matrix @ P

print(f"Original point: {P}")

print(f"After Shearing in Y direction by 7 units: {P_shear_y}")

print(f"After Scaling in X and Y direction by 3/2 and 4 units respectively: {P_scale_1}")

print(f"After Shearing in X and Y direction by 2 and 4 units respectively: {P_shear_2}")

print(f"After Rotation about origin by angle 45°: {P_rotate}")

2)Write a program to plot 2D X-axis and Y-axis in black colour . In the same direction plot :-

V) Green triange with vertices [5,4],[7,4],[6,6].

VI) Blue rectangle with vertices[2,2],[10,2],[10,8],[2,8].

VII) Red polygon with vertices[[6,2],[10,4],[10,8],[2,8].

VIII) Isosacales triangle with of vertices[0,0][4,0][2,4].

Ans:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.axhline(0, color='black')

ax.axvline(0, color='black')

ax.add_patch(plt.Polygon([[5, 4], [7, 4], [6, 6]], color='green'))

ax.add_patch(plt.Polygon([[2, 2], [10, 2], [10, 8], [2, 8]], color='blue'))

ax.add_patch(plt.Polygon([[6, 2], [10, 4], [10, 8], [2, 8]], color='red'))

ax.add_patch(plt.Polygon([[0, 0], [4, 0], [2, 4]], color='orange'))


ax.set_aspect('equal')

plt.xlim(-1, 12)

plt.ylim(-1, 10)

plt.show()

PYTHON PROGRAMMING LANGUAGES=II

SLIP NO:15

Q 1) Attempt any one of the following.

1)Write a python program to find the area and perimeter of the triangle ABC where A[0,0],B[5,0] and
C[3,3].

Ans:

import numpy as np
A = np.array([0, 0])

B = np.array([5, 0])

C = np.array([3, 3])

a = np.linalg.norm(B - C)

b = np.linalg.norm(A - C)

c = np.linalg.norm(A - B)

perimeter = a + b + c

area = 0.5 * abs(A[0]*(B[1] - C[1]) + B[0]*(C[1] - A[1]) + C[0]*(A[1] - B[1]))

print(f"Length of sides: a = {a}, b = {b}, c = {c}")

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

3) Write a python program to plot the graph of the function ,using def()

f(x)={ x^2 +4 if -10< x <5

3x +9 if 5 ≤ x < 10

Ans:

import numpy as np

import matplotlib.pyplot as plt

def f(x):
if -10 < x < 5:

return x**2 + 4

elif 5 <= x < 10:

return 3 * x + 9

x1 = np.linspace(-10, 5, 500)

x2 = np.linspace(5, 10, 500)

y1 = [f(xi) for xi in x1]

y2 = [f(xi) for xi in x2]

plt.plot(x1, y1, label='f(x) = x^2 + 4')

plt.plot(x2, y2, label='f(x) = 3x + 9')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Graph of the piecewise function f(x)')

plt.legend()

plt.grid(True)

plt.show()

3)Plot the graphs of sin x, cos x,e^x, and x^2 in[0,5] in one figure with(2x2) subplots.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, 5, 100)
fig, axs = plt.subplots(2, 2)

axs[0, 0].plot(x, np.sin(x), 'b-')

axs[0, 1].plot(x, np.cos(x), 'r-')

axs[1, 0].plot(x, np.exp(x), 'g-')

axs[1, 1].plot(x, x**2, 'm-')

axs[0, 0].set_title('sin(x)')

axs[0, 1].set_title('cos(x)')

axs[1, 0].set_title('e^x')

axs[1, 1].set_title('x^2')

plt.show()

3)Plot the graphs of sin x, cos x,e^x, and x^2 in[0,5] in one figure with(2x2) subplots.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, 5, 100)

fig, axs = plt.subplots(2, 2)

axs[0, 0].plot(x, np.sin(x), 'b-')

axs[0, 1].plot(x, np.cos(x), 'r-')

axs[1, 0].plot(x, np.exp(x), 'g-')


axs[1, 1].plot(x, x**2, 'm-')

axs[0, 0].set_title('sin(x)')

axs[0, 1].set_title('cos(x)')

axs[1, 0].set_title('e^x')

axs[1, 1].set_title('x^2')

plt.show()

Q 2) Attempt any two of the following.

1)Write a python program to rotate the triangle ABC through the line by 180°where A[1,2],B[2,-2],C[-
1,2].

Ans:

import numpy as np

import matplotlib.pyplot as plt

A = np.array([1, 2])

B = np.array([2, -2])

C = np.array([-1, 2])

vertices = np.array([A, B, C])

rotation_matrix = np.array([[-1, 0], [0, -1]])

vertices_rotated = vertices @ rotation_matrix

plt.plot(*np.append(vertices, [A], axis=0).T, 'b-', label='Original')

plt.plot(*np.append(vertices_rotated, [vertices_rotated[0]], axis=0).T, 'r--', label='Rotated 180°')


plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

2)Write a python program to plot the graph of the function f(x)=e^x in the interval [-10,10].

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-10, 10, 400)

y = np.exp(x)

plt.plot(x, y, label='$f(x) = e^x$')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Graph of $f(x) = e^x$')

plt.legend()

plt.grid(True)

plt.show()

3)Write a python program to plot the 3D line graph whose parametric equation is (cos (2x),sin(2x),x) for
10 ≤ x ≤ 20 (in red colour ) with title to the graph.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(10, 20, 400)


X = np.cos(2 * x)

Y = np.sin(2 * x)

Z=x

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot(X, Y, Z, color='red')

ax.set_title('3D Line Graph of the Parametric Equation (cos(2x), sin(2x), x)')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

plt.show()

Q 3) Attempt the following.

A)Attempt any one of the following.

1)Write a program to solve the following LPP :

Min Z = 3.5 x +2y

Subject to x + y ≥ 5

x≥4

y≤2

x ≥ 0, y ≥ 0

Ans:

Ans:

import pulp
prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 3.5 * x + 2 * y

prob += x + y >= 5

prob += x >= 4

prob += y <= 2

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to solve the following LPP:

Min Z= x + y

Subject to x ≥ 6

y≥6

x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:
import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11

prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}")

B) Attempt any one of the following.

1) Write a python program to find the combined transformation of the line segment between the
pointsA[5,3],B[1,4] for the following sequence of transformations:

I) First rotation about origin through an angle π/2

II) Followed by scaling in x-coordinate by 5 unit.

III) Followed by reflection through the line y= -x.

Ans:
import numpy as np

A, B = np.array([5, 3]), np.array([1, 4])

theta = np.pi / 2

rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])

scaling_matrix = np.array([[5, 0], [0, 1]])

reflection_matrix = np.array([[0, -1], [-1, 0]])

A_transformed = reflection_matrix @ scaling_matrix @ rotation_matrix @ A

B_transformed = reflection_matrix @ scaling_matrix @ rotation_matrix @ B

print(f"Original point A: {A}, Transformed point A: {A_transformed}")

print(f"Original point B: {B}, Transformed point B: {B_transformed}")

2) Write a python program to apply each of the following transformations on the points P[-2,4].

I) Reflection through the line y=x + 1.

II) Scaling in Y-coordinate by factor 1.5

III) Shering in X direction by 2 unit.

IV) Rotation about origin by an angle 45°

Ans:

import numpy as np

P = np.array([-2, 4])

reflect = lambda p: [(p[0] + p[1] - 1) / 2, (p[0] + p[1] + 1) / 2]

scale_y = lambda p, f: [p[0], p[1] * f]

shear_x = lambda p, f: [p[0] + f * p[1], p[1]]


rotate = lambda p, a: np.dot([[np.cos(a), -np.sin(a)], [np.sin(a), np.cos(a)]], p)

P_reflected = reflect(P)

P_scaled = scale_y(P_reflected, 1.5)

P_sheared = shear_x(P_scaled, 2)

P_rotated = rotate(P_sheared, np.radians(45))

print(f"Original point: {P}")

print(f"After Reflection through y = x + 1: {P_reflected}")

print(f"After Scaling in Y-coordinate by factor 1.5: {P_scaled}")

print(f"After Shearing in X direction by 2 units: {P_sheared}")

print(f"After Rotation about origin by an angle of 45°: {P_rotated}")

PYTHON PROGRAMMING LANGUAGE = II

SLIP NO :16

Q 1)Attempt any two of the following.

1) Using python plot the surface plot the graph of the function z= -x^2-y^2 in -10<x,y<10.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-10, 10, 400)

y = np.linspace(-10, 10, 400)


x, y = np.meshgrid(x, y)

z = -x**2 - y**2

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

ax.set_title('Surface plot of $z = -x^2 - y^2$')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

plt.show()

2)Write a python program to plot of the function f(x)= log(3x^2),interval [1,10] with the black dashed
points.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(1, 10, 400)

y = np.log(3 * x**2)

plt.plot(x, y, 'k^--', label='$f(x) = \log(3x^2)$')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Plot of $f(x) = \log(3x^2)$')


plt.legend()

plt.grid(True)

plt.show()

2) Write apython program to generate plot of the function f(x)=x^2 , in the interval [-5,5] in figure
of size 6x6 inches.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 400)

y = x**2

plt.figure(figsize=(6, 6)) # Set figure size to 6x6 inches

plt.plot(x, y, label='$f(x) = x^2$')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Plot of $f(x) = x^2$')

plt.legend()

plt.grid(True)

plt.show()

Q 2)Attempt any two of the following.

1)Write a python program to declare the line segment passing through the points A(0,7), B(5,2).Also
find the lengh and mid point of the segment passing through points A and B.

Ans:

import numpy as np
A = np.array([0, 7])

B = np.array([5, 2])

length = np.linalg.norm(B - A)

midpoint = (A + B) / 2

print(f"Point A: {A}")

print(f"Point B: {B}")

print(f"Length of the line segment AB: {length}")

print(f"Midpoint of the line segment AB: {midpoint}")

3) Write a python program to draw polygon with vertices(0,0),(2,0),(2,3) and (1,6) and rotate it by
90°.

Ans:

import numpy as np

import matplotlib.pyplot as plt

vertices = np.array([[0, 0], [2, 0], [2, 3], [1, 6]])

rotation_matrix = np.array([[0, -1], [1, 0]])

vertices_rotated = vertices @ rotation_matrix

plt.plot(*np.append(vertices, [vertices[0]], axis=0).T, 'b-', label='Original')

plt.plot(*np.append(vertices_rotated, [vertices_rotated[0]], axis=0).T, 'r--', label='Rotated 90°')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()
3)Write a python program to generate vector x in the interval [0,15] using numpy package with 100 sub
intevals .

Ans:

import numpy as np

x = np.linspace(0, 15, 100)

print(x)

Q 3)Attempt the following.

A) Attempt any one of the following.

1)Write a python program solve the following LPP:

Max Z=5x + 3y

Subject to 3x + 5y ≤ 15

6x + 2y ≤ 24

x ≥ 0,y ≥0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 5 * x + 3 * y

prob += 3 * x + 5 * y <= 15

prob += 6 * x + 2 * y <= 24
prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values: x = {pulp.value(x)}, y = {pulp.value(y)}")

2)Write a program to solve the following LPP :

Min Z = 3.5 x +2y

Subject to x + y ≥ 5

x≥4

y≤2

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 3.5 * x + 2 * y

prob += x + y >= 5

prob += x >= 4

prob += y <= 2
prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

B) Attempt any one of the following.

1) Write apython program to plot the triangle with vertices [4,3],[6,3],[6,5] and its reflection
through 1)x-axis ,2)y-axis .All figures must be different colour also plot the two axes.

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[4, 3], [6, 3], [6, 5], [4, 3]])

rx, ry = [[1, 0], [0, -1]], [[-1, 0], [0, 1]]

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*(v @ rx).T, 'r--', label='Reflected x-axis')

plt.plot(*(v @ ry).T, 'g-.', label='Reflected y-axis')

plt.axhline(0, color='black', linewidth=1)

plt.axvline(0, color='black', linewidth=1)

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

2)Write apython program to plot the triangle with vertices [3,3],[3,6],[0,6] and its reflection through line
y=x and y-axis .All plot mirror lines.
Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[3, 3], [3, 6], [0, 6], [3, 3]])

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*(v @ [[-1, 0], [0, 1]]).T, 'r--', label='Reflected y-axis')

plt.plot(*(v @ [[0, 1], [1, 0]]).T, 'g-.', label='Reflected y=x')

plt.plot([-7, 7], [-7, 7], 'k:', label='Line y=x')

plt.axvline(0, color='black', linestyle=':', label='y-axis')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()
PYHTON PROGRAMMING LANGUAGE=II

SLIP NO:17

Q 1)Attempt any two of the following.

1) Write the python program to plot the 3D graph of the function z=x^2+y^2 in -6<x,y<6 unsing
surface plot .

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x, y = np.meshgrid(np.linspace(-6, 6, 100), np.linspace(-6, 6, 100))

z = x**2 + y**2

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

2) Write a python program to plot 3D counter for the function f(x,y) log(x^2 y^2) when -5 ≤ x,y ≤ 5
with green colour map.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 100)

y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)

z = np.log(x**2 * y**2)

plt.contourf(x, y, z, cmap='Greens')

plt.colorbar(label='$\log(x^2 y^2)$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('3D Contour plot of $f(x,y) = \log(x^2 y^2)$')

plt.show()

3) Write a python program to reflect the line segment joining the points A[-5,2] and B[1,4] through the
line y=x.

Ans:

import numpy as np

import matplotlib.pyplot as plt

A, B = np.array([-5, 2]), np.array([1, 4])

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

A_reflected, B_reflected = A @ reflection, B @ reflection

plt.plot([A[0], B[0]], [A[1], B[1]], 'b-', label='Original')

plt.plot([A_reflected[0], B_reflected[0]], [A_reflected[1], B_reflected[1]], 'r--', label='Reflected')

plt.plot([-6, 6], [-6, 6], 'k:', label='Line y=x')


plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

Q 2) Attempt any two of the following.

1)Write a python program to rotate the line segment by 180° having end points(1,0) and (2,-1).

Ans:

import numpy as np

import matplotlib.pyplot as plt

A, B = np.array([1, 0]), np.array([2, -1])

rotation = np.array([[-1, 0], [0, -1]])

A_rotated, B_rotated = A @ rotation, B @ rotation

plt.plot([A[0], B[0]], [A[1], B[1]], 'b-', label='Original')

plt.plot([A_rotated[0], B_rotated[0]], [A_rotated[1], B_rotated[1]], 'r--', label='Rotated 180°')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

2)Write a python program to plot triangle with vertices[3,3][5,6],[5,2] and its rotation about the origin
by angle -π radian .

Ans:

import numpy as np

import matplotlib.pyplot as plt


v = np.array([[3, 3], [5, 6], [5, 2], [3, 3]])

rotation = [[-1, 0], [0, -1]]

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*(v @ rotation).T, 'r--', label='Rotated by -π')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

3) Write a python program to draw a polygon with vertices (0,0),(1,0),(2,2)(1,4) and find its area
and perimeter

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[0, 0], [1, 0], [2, 2], [1, 4], [0, 0]])

area = 0.5 * np.abs(np.dot(v[:-1, 0], v[1:, 1]) - np.dot(v[:-1, 1], v[1:, 0]))

perimeter = np.sum(np.sqrt(np.sum(np.diff(v, axis=0)**2, axis=1)))

plt.plot(*v.T, 'b-', label='Polygon')

plt.fill(*v.T, 'b', alpha=0.3)

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

print(f"Area: {area}")
print(f"Perimeter: {perimeter}")

Q 3)Attempt the following.

A)Attempt any one of the following.

1Write a python program to solve the following LPP:

Max Z= 4x + y + 3z +5w

Subject to 4x +6y -5z -4w ≥ -20

-3x - 2y +4z + w ≤ 20

-8x - 3y + 3z + 2w ≤ 20

x,y, ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z')

w = pulp.LpVariable('w')

prob += 4 * x + 1 * y + 3 * z + 5 * w

prob += 4 * x + 6 * y - 5 * z - 4 * w >= -20

prob += -3 * x - 2 * y + 4 * z + 1 * w <= 20

prob += -8 * x - 3 * y + 3 * z + 2 * w <= 20
prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values: x = {pulp.value(x)}, y = {pulp.value(y)}, z = {pulp.value(z)}, w = {pulp.value(w)}")

2)Write a python program to solve the following LPP:

MaxZ= x + y

Subject to x≤ 6

y≤ 6

x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x <= 6

prob += y <= 6

prob += x + y <= 11
prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values: x = {pulp.value(x)}, y = {pulp.value(y)}")

A) Attempt any one of the following.

1) Apply each of the following transformations on the point [3,-1].

I) Refection through X-axis

II) Scaling Y-coordinate by factor 1.5

III) Shearing in both X and Y direction by-2 and 4 units respectively

IV) Rotation about origin by angle 30°

Ans: import numpy as np

point = np.array([3, -1])

reflection_x = [point[0], -point[1]]

scaling_y = [point[0], point[1] * 1.5]

shear_x = point @ [[1, 1], [0, 1]]

shear_y = point @ [[1, 0], [1, 1]]

print(f"Reflection X-axis: {reflection_x}")

print(f"Scaling Y by 1.5: {scaling_y}")

print(f"Shearing X: {shear_x}")

print(f"Shearing Y: {shear_y}")

2)Write a python program to draw polygon with vertices [3,3],[4,6],[5,4],[4,2] and [2,2] and its
translation in x and y direction by factor -2 and 1 respectively.
Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[3, 3], [4, 6], [5, 4], [4, 2], [2, 2], [3, 3]])

t = np.array([-2, 1])

v_translated = v + t

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*v_translated.T, 'r--', label='Translated')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()
PYTHON PROGRAMMING LANGUAGE=II

SLIP NO :18

Q 1)Attempt any two of the following.

1) Write a python program to draw polygon with vertices[3,3],[4,6],[2,5],[2,2] and its translation in
x and y directions using the fators 3,5 respecively.

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[3, 3], [4, 6], [2, 5], [2, 2], [3, 3]])

t = np.array([3, 5])

v_translated = v + t

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*v_translated.T, 'r--', label='Translated')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

2) Write a python program to plot the graph 2x^2-4x+5 in [-10,10] magenta coloured dashed
pattern.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-10, 10, 400)


y = 2*x**2 - 4*x + 5

plt.plot(x, y, 'm--', label='$2x^2 - 4x + 5$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('Plot of $2x^2 - 4x + 5$')

plt.legend()

plt.grid(True)

plt.show()

3) Write a python program to generate 3D plot of the functions z=x^2 +y^2 in -5<x,y<5.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-5, 5, 100)

y = np.linspace(-5, 5, 100)

x, y = np.meshgrid(x, y)

z = x**2 + y**2

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()
Q 2)Attempt any two of the following.

1) Write a python program to generate vector x in the interval [-22,22] using numpy package with
80 subintervals.

Ans:

import numpy as np

x = np.linspace(-22, 22, 80)

print(x)

2)Write a python program to rotate triange ABC by 90° where A[1,2],B[2,2] and C[-1,2].

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[1, 2], [2, 2], [-1, 2], [1, 2]])

rotation = np.array([[0, -1], [1, 0]])

v_rotated = v @ rotation

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*v_rotated.T, 'r--', label='Rotated 90°')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

2) Write a python program to plot rectangle with vertices at[2,1],[2,4],[5,4],[5,1] and its uniform
expansion by factor 4.

Ans:
import numpy as np

import matplotlib.pyplot as plt

v = np.array([[2, 1], [2, 4], [5, 4], [5, 1], [2, 1]])

center = np.mean(v[:-1], axis=0)

v_expanded = (v - center) * 4 + center

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*v_expanded.T, 'r--', label='Expanded')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

Q 3)Attempt the following.

B) Attempt any one of the following.

1) Write a python program to solve the following LPP:

Min Z= x + y

Subject to x ≥ 6

y≥6

x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11

prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}")

2) Write a python program to solve the following LPP:

Max Z=2x+3y

Subject to 5x-y ≥ 0

x+y≥6

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 2 * x + 3 * y

prob += 5 * x - y >= 0

prob += x + y >= 6

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values: x = {pulp.value(x)}, y = {pulp.value(y)}")

A) Attempt one of the following.

1) Write a python program to find the combined transformation of the line segment betweeen the
points A[3,2] and B[2,-3] for the following sequence of transformations:

I) First rotation about origin through an angle π/6.

II) Followed by scaling in Y coordinates by 4 units respectively.

III) Followed by reflection through the origin .

Ans:

import numpy as np

import matplotlib.pyplot as plt

A = np.array([3, 2])

B = np.array([2, -3])

line = np.array([A, B])


rotation = np.array([[np.cos(np.pi/6), -np.sin(np.pi/6)],

[np.sin(np.pi/6), np.cos(np.pi/6)]])

scaling = np.array([[1, 0], [0, 4]])

reflection = np.array([[-1, 0], [0, -1]])

transformed = line @ rotation.T @ scaling @ reflection

plt.plot(*line.T, 'b-', label='Original')

plt.plot(*transformed.T, 'r--', label='Transformed')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

2) Apply each of the following transformations on the point P[3,-1].

I) Reflection through Y-axis .

II) Scaling in X and Y direction by 1/2 and 3 units respectively .

III) Shearing in both X and Y direction by -2 and 4 units respectively.

IV) Rotation about origin by an angle 60°

Ans:

import numpy as np

P = np.array([3, -1])

reflection_y = [-P[0], P[1]]

scaling = [P[0] * 0.5, P[1] * 3]

shear = P @ [[1, -2], [4, 1]]


rotation = P @ [[np.cos(np.radians(60)), -np.sin(np.radians(60))],

[np.sin(np.radians(60)), np.cos(np.radians(60))]]

print(f"Reflection Y-axis: {reflection_y}")

print(f"Scaling 1/2, 3: {scaling}")

print(f"Shearing -2, 4: {shear}")

print(f"Rotation 60°: {rotation}")


PYTHON PROGRAMMIMG LANGUAGE = II

SLIP NO:19

Q 1)Attempt any two of the following.

1)Write a python program to plot the graphs of sin x, cos x,e^x, and x^2 in[0,5] in one figure with(2x2)
subplots.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, 5, 100)

fig, axs = plt.subplots(2, 2)

axs[0, 0].plot(x, np.sin(x), 'b-')

axs[0, 1].plot(x, np.cos(x), 'r-')

axs[1, 0].plot(x, np.exp(x), 'g-')

axs[1, 1].plot(x, x**2, 'm-')

axs[0, 0].set_title('sin(x)')

axs[0, 1].set_title('cos(x)')

axs[1, 0].set_title('e^x')

axs[1, 1].set_title('x^2')

plt.show()

2) Write a python program to plot 3D surface plot of the function z=cos( ∣x∣+∣y∣) in -1 < x, y< 1.

Ans:
import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-1, 1, 100)

y = np.linspace(-1, 1, 100)

x, y = np.meshgrid(x, y)

z = np.cos(np.abs(x) + np.abs(y))

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

3) Write a python program to plot 2D graph of the functions f(x)=log (x) + 5 and g(x)=log(x)-5 in
[0,10] by setting different line width and different colour to the curve.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0.1, 10, 400) # Start from 0.1 to avoid log(0)

f_x = np.log(x) + 5

g_x = np.log(x) - 5

plt.plot(x, f_x, 'b-', linewidth=2, label='$f(x) = \log(x) + 5$')


plt.plot(x, g_x, 'r--', linewidth=3, label='$g(x) = \log(x) - 5$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('2D Graphs of $f(x) = \log(x) + 5$ and $g(x) = \log(x) - 5$')

plt.legend()

plt.grid(True)

plt.show()

Q 2)Attempt any two of the following.

1)Write a python program to rotate the ray by 90° in clockwise direction having staring point (0,0) and
end point (4,4).

Ans:

import numpy as np

import matplotlib.pyplot as plt

ray = np.array([[0, 0], [4, 4]])

rotation_matrix = np.array([[0, 1], [-1, 0]])

ray_rotated = ray @ rotation_matrix

plt.plot(ray[:, 0], ray[:, 1], 'b-', label='Original')

plt.plot(ray_rotated[:, 0], ray_rotated[:, 1], 'r--', label='Rotated')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

2)Write a python program to reflect the triangle ABC through line y=3 where A[1,0],B[2,-1] and C[-1,3].
Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[1, 0], [2, -1], [-1, 3], [1, 0]])

reflect_y = 3

reflect = lambda p, line: [p[0], 2*line - p[1]]

v_reflected = np.array([reflect(p, reflect_y) for p in v])

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*v_reflected.T, 'r--', label='Reflected')

plt.axhline(y=reflect_y, color='k', linestyle='--', label='y=3')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

3)Write a python program to draw a polygon with vertices (0,0),(1,0),(2,2)(1,4) and find its area and
perimeter

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[0, 0], [1, 0], [2, 2], [1, 4], [0, 0]])

area = 0.5 * np.abs(np.dot(v[:-1, 0], v[1:, 1]) - np.dot(v[:-1, 1], v[1:, 0]))

perimeter = np.sum(np.sqrt(np.sum(np.diff(v, axis=0)**2, axis=1)))


plt.plot(*v.T, 'b-', label='Polygon')

plt.fill(*v.T, 'b', alpha=0.3)

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

print(f"Area: {area}")

print(f"Perimeter: {perimeter}")

Q 3)Attempt the following.

A)Attempt any one of the following.

1) Write a python program to solve the following LPP:

Max Z=3x + 2y + 4z

Subject to 2x + 3y ≤ 8

2x + 5y ≤ 10

3x + 2y +4z ≤ 15

x ≥ 0,y ≥ 0,z ≥ 0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)
prob += 3*x + 2*y + 4*z

prob += 2*x + 3*y <= 8

prob += 2*x + 5*y <= 10

prob += 3*x + 2*y + 4*z <= 15

prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}, z: {pulp.value(z)}")

2) Write a python program to solve the following LPP:

Min Z= x+2y+z

Subject to x+ 1/2 y+1/2+z ≤ 1

3/2 x+ 2y +z ≥ 8

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x, y, z = [pulp.LpVariable(var, lowBound=0) for var in ['x', 'y', 'z']]

prob += x + 2*y + z

prob += x + 0.5*y + 0.5*z <= 1


prob += 1.5*x + 2*y + z >= 8

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values: x = {pulp.value(x)}, y = {pulp.value(y)}, z = {pulp.value(z)}")

B)Attempt any one of the following.

1)Write a python program to apply each of the following transformation on the points P=[-2,4].

I) Rotation about origin through an angle 48°.

II) Scaling in X-coordinate by factor 2.

III) Reflection through the line y=2x-3

IV) Shearing in X direction by 7 units

Ans:

import numpy as np

P = np.array([-2, 4])

rotation = P @ [[np.cos(np.radians(48)), -np.sin(np.radians(48))],

[np.sin(np.radians(48)), np.cos(np.radians(48))]]

scaling = P @ [[2, 0], [0, 1]]

reflection = (lambda p, m, c: np.array([2 * ((p[0] + (p[1] - c) * m) / (1 + m**2)) - p[0], 2 * ((p[0] + (p[1] - c)


* m) / (1 + m**2)) * m - p[1] + 2 * c]))(P, 2, -3)

shearing = P @ [[1, 7], [0, 1]]

print(f"Rotation by 48°: {rotation}")


print(f"Scaling X by 2: {scaling}")

print(f"Reflection through y=2x-3: {reflection}")

print(f"Shearing X by 7: {shearing}")

1) Find the combined transformation of the line segment between the points A[4,-1] and B[3,0] for
the following sequence of transformation :

First rotation about origin through an angle π ^ c ; followed by scaling in x coordinate by 3 units ;
followed by reflection through the line y=x.

Ans:

import numpy as np

import matplotlib.pyplot as plt

A, B = np.array([4, -1]), np.array([3, 0])

line = np.array([A, B])

rotation = np.array([[np.cos(np.pi), -np.sin(np.pi)],

[np.sin(np.pi), np.cos(np.pi)]])

scaling = np.array([[3, 0], [0, 1]])

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

transformed = line @ rotation.T @ scaling @ reflection

plt.plot(*line.T, 'b-', label='Original')

plt.plot(*transformed.T, 'r--', label='Transformed')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()
PYTHON PROGRAMMING LANGUAGE=II

SLIP NO:20

Q 1)Attempt any two of the following.

1) Write a python program to 2D graph of the function f(x)=sin x and g(x)=cos x in [-2π ,2π ].

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-2 * np.pi, 2 * np.pi, 400)

f_x = np.sin(x)

g_x = np.cos(x)

plt.plot(x, f_x, 'b-', linewidth=2, label='$f(x) = \sin(x)$')

plt.plot(x, g_x, 'r--', linewidth=2, label='$g(x) = \cos(x)$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('2D Graphs of $f(x) = \sin(x)$ and $g(x) = \cos(x)$')

plt.legend()

plt.grid(True)

plt.show()

2) Write a python program to plot 2D graph of the function f(x)= e^x sin x in [-5π ,5π] with blue
points line with upward pointing triangle.

Ans:

import numpy as np

import matplotlib.pyplot as plt


x = np.linspace(-5 * np.pi, 5 * np.pi, 400)

f_x = np.exp(x) * np.sin(x)

plt.plot(x, f_x, 'b^-', label='$f(x) = e^x \sin(x)$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('2D Graph of $f(x) = e^x \sin(x)$')

plt.legend()

plt.grid(True)

plt.show()

3)Write a python program to plot the 3D graph of the function f(x)= sin(x² + y² ) -6 < x,y< 6.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-6, 6, 100)

y = np.linspace(-6, 6, 100)

x, y = np.meshgrid(x, y)

z = np.sin(x**2 + y**2)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')
plt.show()

Q 2) Attempt any two of the following.

1)Write apython program to reflect the line segment joining the points A[-5,2],B[3,-4] through the line
y= 2x-1

Ans:

import numpy as np

import matplotlib.pyplot as plt

def reflect_point(p, m, c):

d = (p[0] + (p[1] - c) * m) / (1 + m**2)

return np.array([2 * d - p[0], 2 * d * m - p[1] + 2 * c])

line_slope, line_intercept = 2, -1

A, B = np.array([-5, 2]), np.array([3, -4])

A_reflected, B_reflected = reflect_point(A, line_slope, line_intercept), reflect_point(B, line_slope,


line_intercept)

plt.plot(*np.array([A, B]).T, 'b-', label='Original')

plt.plot(*np.array([A_reflected, B_reflected]).T, 'r--', label='Reflected')

plt.plot(np.linspace(-6, 4, 100), 2 * np.linspace(-6, 4, 100) - 1, 'k--', label='y = 2x - 1')

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

2)Write apython program to find the area and perimeter of a polygon with vertices (0,0),(-2,0),(5,5)(1,-
1).
Ans:

import numpy as np

# Vertices of the polygon

vertices = np.array([[0, 0], [-2, 0], [5, 5], [1, -1], [0, 0]])

# Calculate the area using the Shoelace formula

def polygon_area(verts):

x = verts[:, 0]

y = verts[:, 1]

return 0.5 * np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))

# Calculate the perimeter

def polygon_perimeter(verts):

return np.sum(np.sqrt(np.sum(np.diff(verts, axis=0)**2, axis=1)))

# Find the area and perimeter

area = polygon_area(vertices)

perimeter = polygon_perimeter(vertices)

print(f"Area: {area}")

print(f"Perimeter: {perimeter}")

3)Write a python program to plot the 3D graph of the function f(x,y)=sin x +cos y y,x ∈[-2π ,2π ]using
wireframe plot.

Ans:
import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-2 * np.pi, 2 * np.pi, 100)

y = np.linspace(-2 * np.pi, 2 * np.pi, 100)

x, y = np.meshgrid(x, y)

z = np.sin(x) + np.cos(y)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_wireframe(x, y, z, color='b')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

ax.set_title('$f(x, y) = \sin(x) + \cos(y)$')

plt.show()

Q 3)Attempt the following.

A) Attempt any one of the following.

2) Write a python program to solve the following LPP:

Max Z=x+y

Subject to x-y ≥ 1

x+y ≥ 2
x ≥ 0,y ≥ 0

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0, 5, 400)

y1 = x - 1

y2 = 2 - x

plt.figure(figsize=(8, 8))

plt.plot(x, y1, label=r'$x - y \geq 1$', color='blue')

plt.plot(x, y2, label=r'$x + y \geq 2$', color='red')

plt.fill_between(x, np.maximum(y1, 0), np.maximum(y2, 0), where=(y2 >= y1), color='lightgreen',


alpha=0.5)

plt.xlim(0, 5)

plt.ylim(0, 5)

plt.axhline(0, color='black', lw=1)

plt.axvline(0, color='black', lw=1)

plt.xlabel('x')

plt.ylabel('y')

plt.title('Feasible Region for the LPP')

plt.legend()

plt.grid(True)

plt.show()

2)Write a python program to solve the following LPP:

Min Z = 3.5 x +2y

Subject to x + y ≥ 5

x≥4
y≤2

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 3.5 * x + 2 * y

prob += x + y >= 5

prob += x >= 4

prob += y <= 2

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

A) Attempt any one of the following.

1) Apply the following transformations on the points P[3,-2]

I) Scaling in Y direction by 4 units


II) Reflection through y axis

III) Rotation about origin by an angle 45°

IV) Reflection through the line y=x

Ans:

import numpy as np

P = np.array([3, -2])

scaling_matrix = np.array([[1, 0], [0, 4]])

P_scaled = P @ scaling_matrix

reflection_y_axis = np.array([[-1, 0], [0, 1]])

P_reflected_y_axis = P_scaled @ reflection_y_axis

theta = np.radians(45)

rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]])

P_rotated = P_reflected_y_axis @ rotation_matrix

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

P_final = P_rotated @ reflection_y_eq_x

print(f"Original point: {P}")

print(f"After scaling in Y direction by 4 units: {P_scaled}")

print(f"After reflection through y-axis: {P_reflected_y_axis}")


print(f"After rotation about origin by 45°: {P_rotated}")

print(f"After reflection through the line y=x: {P_final}")

2)Apply the following transformations on the points P[3,-2]

I)Shearing in X direction by -2 unit

II)Scaling in X and Y direction by -3 and 2 units respectively

III)Refelction through X axis

IV)Reflection through the line y= -x

Ans:

import numpy as np

P = np.array([3, -2])

shear_matrix = np.array([[1, -2], [0, 1]])

P_sheared = P @ shear_matrix

scaling_matrix = np.array([[-3, 0], [0, 2]])

P_scaled = P_sheared @ scaling_matrix

reflection_x_axis = np.array([[1, 0], [0, -1]])

P_reflected_x_axis = P_scaled @ reflection_x_axis

reflection_y_eq_neg_x = np.array([[0, -1], [-1, 0]])

P_final = P_reflected_x_axis @ reflection_y_eq_neg_x

print(f"Original point: {P}")


print(f"After shearing in X direction by -2 units: {P_sheared}")

print(f"After scaling in X by -3 and in Y direction by 2 units: {P_scaled}")

print(f"After reflection through X axis: {P_reflected_x_axis}")

print(f"After reflection through the line y = -x: {P_final}")


PYTHON PROGRAMMING LANGUANGE =II

SLIP NO: 21

Q 1) Attempt any two of the following.

1) Write a python program to plot 2D graph of the function f(x)=x^4 in [0,5] with red dashed line
with circle markers.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, 5, 100)

f_x = x**4

plt.plot(x, f_x, 'ro--', label='$f(x) = x^4$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('2D Graph of $f(x) = x^4$')

plt.legend()

plt.grid(True)

plt.show()

2)Write the python program to plot the 3D graph of the function z=x^2+y^2 in -6<x,y<6 .

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D


x = np.linspace(-6, 6, 100)

y = np.linspace(-6, 6, 100)

x, y = np.meshgrid(x, y)

z = x**2 + y**2

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

ax.set_title('$z = x^2 + y^2$')

plt.show()

2) Write a python program to plot the 3D graph of the function f(x)=e^{x^2+y^2} for x,y ∈[0,2π]
using wireframe.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(0, 2 * np.pi, 100)

y = np.linspace(0, 2 * np.pi, 100)

x, y = np.meshgrid(x, y)

z = np.exp(x**2 + y**2)
fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_wireframe(x, y, z, color='b')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

ax.set_title('$f(x, y) = e^{x^2 + y^2}$')

plt.show()

Q 2)Attempt any two of the following.

1) If the line segment joining the points A[2,5] and B[4,-13] is transformed to the line segment A’ B’
,by the transformation matrix [T]=[ 2 3

1 4] then using python find the slop and mid point of the
transformed line .

Ans:

import numpy as np

A = np.array([2, 5])

B = np.array([4, -13])

T = np.array([[2, 3], [1, 4]])

A_transformed = A @ T

B_transformed = B @ T
slope = (B_transformed[1] - A_transformed[1]) / (B_transformed[0] - A_transformed[0])

midpoint = (A_transformed + B_transformed) / 2

print(f"Slope of the transformed line segment: {slope}")

print(f"Midpoint of the transformed line segment: {midpoint}")

2) Write a python program to plot square with vertices at [4,4],[2,4],[2,2],[4,2] and find its uniform
expansion by factor 3 ,uniform reduction by factor 0.4

Ans:

import numpy as np

import matplotlib.pyplot as plt

vertices = np.array([[4, 4], [2, 4], [2, 2], [4, 2], [4, 4]])

expansion_factor, reduction_factor = 3, 0.4

centroid = np.mean(vertices[:-1], axis=0)

expanded_vertices = centroid + expansion_factor * (vertices - centroid)

reduced_vertices = centroid + reduction_factor * (vertices - centroid)

plt.plot(vertices[:, 0], vertices[:, 1], 'b-o', label='Original Square')

plt.plot(expanded_vertices[:, 0], expanded_vertices[:, 1], 'r--o', label='Expanded Square')

plt.plot(reduced_vertices[:, 0], reduced_vertices[:, 1], 'g-.o', label='Reduced Square')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('Uniform Expansion and Reduction of Square')


plt.legend()

plt.gca().set_aspect('equal', adjustable='box')

plt.grid(True)

plt.show()

3)Write a python program to find the equation of the transformed line if shearing is applied on the line
2x+y=3 in x and y direction by 2 and -3 units respectively.

Ans:

import numpy as np

a, b, c = 2, 1, 3

shear_matrix = np.array([[1, 2], [-3, 1]])

original_coefficients = np.array([a, b])

transformed_coefficients = shear_matrix @ original_coefficients

a_prime, b_prime = transformed_coefficients

print(f"Transformed line equation: {a_prime}x + {b_prime}y = {c}")

Q 3)Attempt the following.

B) Attempt any one of the following.

1) Write a python program to solve the following LPP:

Min Z= 4x+2y

Subject to x+y ≤ 3

x-y ≤ 2

x ≥ 0, y ≥ 0

Ans:
import pulp

lp_problem = pulp.LpProblem("Minimize Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

lp_problem += 4 * x + 2 * y, "Z"

lp_problem += x + y <= 3

lp_problem += x - y <= 2

lp_problem.solve()

x_value = pulp.value(x)

y_value = pulp.value(y)

z_value = pulp.value(lp_problem.objective)

print(f"Optimal solution: x = {x_value}, y = {y_value}")

print(f"Minimum value of Z: {z_value}")

2) Write apython program to solve the following LPP

Max Z =2x +4y

2x+y ≤ 18

x+2y≥ 30

x≥ 0,y≥ 0
Ans:

import pulp

lp_problem = pulp.LpProblem("Maximize Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

lp_problem += 2 * x + 4 * y, "Z"

lp_problem += 2 * x + y <= 18

lp_problem += x + 2 * y >= 30

lp_problem.solve()

x_value = pulp.value(x)

y_value = pulp.value(y)

z_value = pulp.value(lp_problem.objective)

print(f"Optimal solution: x = {x_value}, y = {y_value}")

print(f"Maximum value of Z: {z_value}")

A) Attempt any one of the following.

1) Apply the following transformations on the points[-2,4]

I) Reflection through line 3x+4y=5

II) Scaling in X coordinate by factor 6


III) Scaling in X coordinate by factor 4.1

IV) Reflection through line y=2x+3

Ans:

import numpy as np

def reflect_point_through_line(point, line):

a, b, c = line

x, y = point

d = (a * x + b * y + c) / (a**2 + b**2)

x_reflect = x - 2 * a * d

y_reflect = y - 2 * b * d

return np.array([x_reflect, y_reflect])

P = np.array([-2, 4])

line1 = np.array([3, 4, -5])

line2 = np.array([2, -1, -3])

P_reflected_line1 = reflect_point_through_line(P, line1)

P_scaled_x6 = P_reflected_line1 * np.array([6, 1])

P_scaled_x4_1 = P_scaled_x6 * np.array([4.1, 1])

P_final = reflect_point_through_line(P_scaled_x4_1, line2)

print(f"Original point: {P}")

print(f"After reflection through line 3x + 4y = 5: {P_reflected_line1}")

print(f"After scaling in X coordinate by factor 6: {P_scaled_x6}")


print(f"After scaling in X coordinate by factor 4.1: {P_scaled_x4_1}")

print(f"After reflection through line y = 2x + 3: {P_final}")

B)Apply the following transformations on the points[-2,4]

I)Shearing in Y direction by 7 units.

II)Scaling in X and Y coordinatedirection by 4 and 7 units respectively

III)Rotation aboutv origin by an angle 48°

IV)Reflection through line y=x

Ans:

import numpy as np

P = np.array([-2, 4])

shear_matrix = np.array([[1, 0], [7, 1]])

P_sheared = P @ shear_matrix

scaling_matrix = np.array([[4, 0], [0, 7]])

P_scaled = P_sheared @ scaling_matrix

theta = np.radians(48)

rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]])

P_rotated = P_scaled @ rotation_matrix

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

P_final = P_rotated @ reflection_matrix


print(f"Original point: {P}")

print(f"After shearing in Y direction by 7 units: {P_sheared}")

print(f"After scaling in X and Y direction by 4 and 7 units respectively: {P_scaled}")

print(f"After rotation about origin by 48°: {P_rotated}")

print(f"After reflection through the line y=x: {P_final}")


PYTHON PROGRAMMING LANGUAGE =II

SLIP NO: 22

Q 1) Attempt any two of the following.

1) Write a python progrma to draw 2D plot y= log(x^2)+sin(x) with suitable lable in the x axis and y
axis and a title in [-5π ,5π ]

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5 * np.pi, 5 * np.pi, 500)

y = np.log(x**2) + np.sin(x)

plt.plot(x, y, label='$y = \log(x^2) + \sin(x)$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('2D Plot of $y = \log(x^2) + \sin(x)$')

plt.legend()

plt.grid(True)

plt.show()

2)Write the python program to plot the 3D dimentional contour plot of parabola z=x^2+y^2 in -
6<x,y<6 .

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-6, 6, 100)
y = np.linspace(-6, 6, 100)

x, y = np.meshgrid(x, y)

z = x**2 + y**2

plt.contour(x, y, z, 20, cmap='viridis')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('3D Contour Plot of $z = x^2 + y^2$')

plt.colorbar()

plt.show()

2) Write a python program to draw 2D plot y =x sin(1/2) in[-5,5] with suitable label in the x axis ,y
axis a title and location of legend to lower right corner .

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 500)

y = x * np.sin(0.5 * x)

plt.plot(x, y, label='$y = x \sin(0.5x)$')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('2D Plot of $y = x \sin(0.5x)$')

plt.legend(loc='lower right')

plt.grid(True)
plt.show()

Q 2)Attempt any two of the following.

1) Write a python program to find the angle at each vertices of the triangle ABC where
A[0,0],B[2,2] & C[0,2].

Ans:

import numpy as np

def calculate_angle(a, b, c):

ab = b - a

ac = c - a

cosine_angle = np.dot(ab, ac) / (np.linalg.norm(ab) * np.linalg.norm(ac))

angle = np.arccos(cosine_angle)

return np.degrees(angle)

A = np.array([0, 0])

B = np.array([2, 2])

C = np.array([0, 2])

angle_A = calculate_angle(B, A, C)

angle_B = calculate_angle(A, B, C)

angle_C = calculate_angle(A, C, B)

print(f"Angle at vertex A: {angle_A:.2f} degrees")

print(f"Angle at vertex B: {angle_B:.2f} degrees")

print(f"Angle at vertex C: {angle_C:.2f} degrees")

2) Write a python program to reflect the points P[3,6] through the line x-2y+4=0
Ans:

import numpy as np

def reflect_point_through_line(point, line):

a, b, c = line

x, y = point

d = (a * x + b * y + c) / (a**2 + b**2)

x_reflect = x - 2 * a * d

y_reflect = y - 2 * b * d

return np.array([x_reflect, y_reflect])

P = np.array([3, 6])

line = np.array([1, -2, 4])

P_reflected = reflect_point_through_line(P, line)

print(f"Original point: {P}")

print(f"Reflected point: {P_reflected}")

3)Write a python program to find the area and perimeter of the triangle ABC where A[0,0],B[5,0] and
C[3,3].

Ans:

import numpy as np

A = np.array([0, 0])

B = np.array([5, 0])

C = np.array([3, 3])
a = np.linalg.norm(B - C)

b = np.linalg.norm(A - C)

c = np.linalg.norm(A - B)

perimeter = a + b + c

area = 0.5 * abs(A[0]*(B[1] - C[1]) + B[0]*(C[1] - A[1]) + C[0]*(A[1] - B[1]))

print(f"Length of sides: a = {a}, b = {b}, c = {c}")

print(f"Perimeter: {perimeter}")

print(f"Area: {area}")

Q 3)Attempt the following.

A)Attempt any two of the following.

1) Write a python programmto solve the following LPP:

Max Z= 4x + y + 3z +5w

Subject to 4x +6y -5z -4w ≥ 20

-3x - 2y +4z + w ≤ 10

-8x - 3y + 3z + 2w ≤ 20

x + y ≤ 11

x,y,z,w ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

w = pulp.LpVariable('w', lowBound=0)

prob += 4 * x + y + 3 * z + 5 * w

prob += 4 * x + 6 * y - 5 * z - 4 * w >= 20

prob += -3 * x - 2 * y + 4 * z + w <= 10

prob += -8 * x - 3 * y + 3 * z + 2 * w <= 20

prob += x + y <= 11

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal z: {pulp.value(z)}")

print(f"Optimal w: {pulp.value(w)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to solve the following LPP:

Min Z= x + y

Subject to x ≥ 6

y≥6
x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11

prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}")

B)Attempt any one of the following.

1) Write apython program to for each of the following.

I) Rotation the point (1,1) about (1,4) through angle π/2

II) Find the distance between two points (0,0) and (1,0)
III) Find the shearing of point (3,4) and X direction by 3 units

IV) Represent two dimentional points using point function(-2,5)

Ans:

import numpy as np

rotated_point = np.array([1 - 1, 1 - 4]) @ np.array([[0, -1], [1, 0]]) + np.array([1, 4])

distance = np.linalg.norm([1, 0])

sheared_point = np.array([3, 4]) @ np.array([[1, 3], [0, 1]])

two_d_point = np.array([-2, 5])

print(f"Rotated point: {rotated_point}")

print(f"Distance: {distance}")

print(f"Sheared point: {sheared_point}")

print(f"Point: {two_d_point}")

2) A company has 3 production facilites S1,S2 and S3 with production capacity of 7,9 and 18 units
( in 100’s )per week of product ,respectively .These unit are to be shipped to 4 respectively.The
transportation cost (in rupees) per unit between factories to warehouse are given in table below .

Write a python to solve the transportation problem for minimize the costs of whole operation

D1 D2 D3 D4 Supply

S1 19 30 50 10 7

S2 70 30 40 60 9

S3 40 8 70 20 18

Demand 5 8 7 14 34

Ans:

import pulp
supply = [7, 9, 18]

demand = [5, 8, 7, 14]

cost = [[19, 30, 50, 10],

[70, 30, 40, 60],

[40, 8, 70, 20]]

prob = pulp.LpProblem("Transportation_Problem", pulp.LpMinimize)

routes = [(i, j) for i in range(3) for j in range(4)]

route_vars = pulp.LpVariable.dicts("Route", (range(3), range(4)), lowBound=0, cat='Continuous')

prob += pulp.lpSum(route_vars[i][j] * cost[i][j] for i, j in routes)

for i in range(3):

prob += pulp.lpSum(route_vars[i][j] for j in range(4)) <= supply[i]

for j in range(4):

prob += pulp.lpSum(route_vars[i][j] for i in range(3)) >= demand[j]

prob.solve()

print("Optimal Solution:")

for i in range(3):

for j in range(4):

print(f"Route from S{i+1} to D{j+1}: {pulp.value(route_vars[i][j])} units")


print(f"Total Transportation Cost: {pulp.value(prob.objective)
PYTHON PROGRAMMING LANGUAGES=II

SLIP NO:23

Q 1)Attempt any one of the following.

1)Write a python program to plot graphs of sin x and cos x in [0,π] in one figure with2×1 subplots.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0, np.pi, 500)

sin_x = np.sin(x)

cos_x = np.cos(x)

fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)

ax1.plot(x, sin_x, label='$\sin(x)$')

ax1.set_ylabel('sin(x)')

ax1.legend()

ax1.grid(True)

ax2.plot(x, cos_x, label='$\cos(x)$')

ax2.set_xlabel('X-axis')

ax2.set_ylabel('cos(x)')

ax2.legend()

ax2.grid(True)
fig.suptitle('Graphs of $\sin(x)$ and $\cos(x)$ in [0, π]')

plt.show()

1) Write a python program to plot the graph of the following function in the given intervals :

I) f(x)=x^3 in [0,5]

II) f(x)= x^2 in [-2,2]

Ans:

import numpy as np

import matplotlib.pyplot as plt

x1 = np.linspace(0, 5, 100)

x2 = np.linspace(-2, 2, 100)

fig, (ax1, ax2) = plt.subplots(2, 1)

ax1.plot(x1, x1**3)

ax2.plot(x2, x2**2)

plt.show()

3)Write a python program to plot 3D surface plot of the function z=cos( ∣x∣+∣y∣) in -1 < x, y< 1.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)

x, y = np.meshgrid(x, y)

z = np.cos(np.abs(x) + np.abs(y))

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

Q 2)Attempt any two of the following.

1) Write a python program to draw regular polygon with 20 sides and radius 1 centered at (0,0).

Ans:

import numpy as np

import matplotlib.pyplot as plt

num_sides = 20

radius = 1

angles = np.linspace(0, 2 * np.pi, num_sides + 1)

x = radius * np.cos(angles)

y = radius * np.sin(angles)

plt.figure(figsize=(6, 6))

plt.plot(x, y)

plt.gca().set_aspect('equal')

plt.title('Regular Polygon with 20 Sides and Radius 1')


plt.grid(True)

plt.show()

2)Write a python program to draw a polygon with vertices (0,0),(1,0),(2,2)(1,4) and find its area and
perimeter

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[0, 0], [1, 0], [2, 2], [1, 4], [0, 0]])

area = 0.5 * np.abs(np.dot(v[:-1, 0], v[1:, 1]) - np.dot(v[:-1, 1], v[1:, 0]))

perimeter = np.sum(np.sqrt(np.sum(np.diff(v, axis=0)**2, axis=1)))

plt.plot(*v.T, 'b-', label='Polygon')

plt.fill(*v.T, 'b', alpha=0.3)

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

print(f"Area: {area}")

print(f"Perimeter: {perimeter}")

2) Write a python program to find the area and perimeter of the triangle ABC where A[0,0],B[-5,0]
and C[-3,3].

Ans:

import numpy as np

A = np.array([0, 0])
B = np.array([-5, 0])

C = np.array([-3, 3])

AB = np.linalg.norm(B - A)

BC = np.linalg.norm(C - B)

CA = np.linalg.norm(A - C)

perimeter = AB + BC + CA

s = perimeter / 2

area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))

print(f"Perimeter of the triangle: {perimeter}")

print(f"Area of the triangle: {area}")

Q 3)Attempt the following.

A) Write a python program to solve theb following LPP:

Max Z=3x + 2y + 4z

Subject to 2x + 3y ≤ 8

2x + 5y ≤ 10

3x + 2y 4z ≤ 15

x ≥ 0,y ≥ 0,z ≥ 0.

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

prob += 3*x + 2*y + 4*z

prob += 2*x + 3*y <= 8

prob += 2*x + 5*y <= 10

prob += 3*x + 2*y + 4*z <= 15

prob.solve(pulp.PULP_CBC_CMD())

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}, z: {pulp.value(z)}")

2)Write a python program to solve the following LPP:

Max Z=3x + 2y + 4z

Subject to 2x + 2y ≤ 12

2x + 2y ≤ 10

5x+3y≤ 15

x ≥ 0,y ≥ 0,

Ans:

import pulp

lp_problem = pulp.LpProblem("Maximize Z", pulp.LpMaximize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

lp_problem += 3 * x + 2 * y + 4 * z, "Z"

lp_problem += 2 * x + 2 * y <= 12

lp_problem += 2 * x + 2 * y <= 10

lp_problem += 5 * x + 3 * y <= 15

lp_problem.solve()

x_value = pulp.value(x)

y_value = pulp.value(y)

z_value = pulp.value(z)

z_max_value = pulp.value(lp_problem.objective)

print(f"Optimal solution: x = {x_value}, y = {y_value}, z = {z_value}")

print(f"Maximum value of Z: {z_max_value}")

B)Attempt any one of the following.

1)Write a python program to apply each of the following transformation on the point[3,-1]

I)Reflection through X axis.

II) Rotation about origin by an angle 30°.

III)Scaling Y coordinate by factor 8.


IV)Shearing in X direction by 2 units

Ans:

import numpy as np

P = np.array([3, -1])

reflection_matrix = np.array([[1, 0], [0, -1]])

P_reflected = reflection_matrix @ P

theta = np.radians(30)

rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]])

P_rotated = rotation_matrix @ P_reflected

scaling_matrix = np.array([[1, 0], [0, 8]])

P_scaled = scaling_matrix @ P_rotated

shear_matrix = np.array([[1, 2], [0, 1]])

P_sheared = shear_matrix @ P_scaled

print(f"Original point: {P}")

print(f"After reflection through X axis: {P_reflected}")

print(f"After rotation about origin by 30°: {P_rotated}")

print(f"After scaling Y coordinate by factor 8: {P_scaled}")

print(f"After shearing in X direction by 2 units: {P_sheared}")


2)Write a python program to apply each of the following transformation on the point[-2,4]

I) Reflection through the line y=x+2

II) Scaling Y coordinate by factor 2

III) Shearing in X direction by 2 units

IV) Rotation about origin by an angle 30°

Ans:

import numpy as np

P = np.array([-2, 4])

# I) Reflection through the line y = x + 2

def reflect_point(point, m, c):

x, y = point

d = (x + (y - c) * m) / (1 + m**2)

x_reflect = 2*d - x

y_reflect = 2*d*m - y + 2*c

return np.array([x_reflect, y_reflect])

P_reflected = reflect_point(P, 1, 2)

# II) Scaling Y coordinate by factor 2

P_scaled = P_reflected * np.array([1, 2])

# III) Shearing in X direction by 2 units

shear_matrix = np.array([[1, 2], [0, 1]])


P_sheared = P_scaled @ shear_matrix

# IV) Rotation about origin by an angle 30°

theta = np.radians(30)

rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]])

P_rotated = P_sheared @ rotation_matrix

print(f"Original point: {P}")

print(f"After reflection through the line y = x + 2: {P_reflected}")

print(f"After scaling Y coordinate by factor 2: {P_scaled}")

print(f"After shearing in X direction by 2 units: {P_sheared}")

print(f"After rotation about origin by 30°: {P_rotated}")


PYTHON PROGRAMMING LANGUAGES=II

SLIP NO:24

Q1)Attempt any two of the following.

1)Write a python program to plot 3D graph of the functions f(x)= e^(-x^2) in [-5,5] with green dashed
points line the upward pointing triangle.

Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 400)

y = np.exp(-x**2)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot(x, y, 'g^--')

ax.set_title('3D graph of $f(x) = e^{-x^2}$')

ax.set_xlabel('X-axis')

ax.set_ylabel('Y-axis')

ax.set_zlabel('Z-axis')

plt.show()

2)Write a python program to plot the graph of the function ,using def()

f(x)={ x^2 +4 if -10< x <5

3x +9 if 5 ≤ x < 10

}
Ans:

import numpy as np

import matplotlib.pyplot as plt

def f(x):

if -10 < x < 5:

return x**2 + 4

elif 5 <= x < 10:

return 3 * x + 9

x1 = np.linspace(-10, 5, 500)

x2 = np.linspace(5, 10, 500)

y1 = [f(xi) for xi in x1]

y2 = [f(xi) for xi in x2]

plt.plot(x1, y1, label='f(x) = x^2 + 4')

plt.plot(x2, y2, label='f(x) = 3x + 9')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.title('Graph of the piecewise function f(x)')

plt.legend()

plt.grid(True)

plt.show()

3) Write a python program to plot graphof function f(x)=log(3x^2) in [1,10] with black dashed
points.

Ans:
import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(1, 10, 100)

y = np.log(3 * x**2)

plt.plot(x, y, 'k--', marker='o', linestyle='dashed')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('$f(x) = \log(3x^2)$ in [1, 10]')

plt.grid(True)

plt.show()

Q 2)Attempt any two of the following.

1)Write a python program to plot triangle with vertices[3,3][5,6],[5,2] and its rotation about the origin
by angle -π radian .

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[3, 3], [5, 6], [5, 2], [3, 3]])

rotation = [[-1, 0], [0, -1]]

plt.plot(*v.T, 'b-', label='Original')

plt.plot(*(v @ rotation).T, 'r--', label='Rotated by -π')

plt.gca().set_aspect('equal', adjustable='box')
plt.legend()

plt.show()

2)Write a python program to generate vector x in the interval [-22,22] using numpy package with 80
subintervals.

Ans:

import numpy as np

x = np.linspace(-22, 22, 80)

print(x)

3)Write a python program to draw a polygon with vertices (0,0),(1,0),(2,2)(1,4) and find its area and
perimeter

Ans:

import numpy as np

import matplotlib.pyplot as plt

v = np.array([[0, 0], [1, 0], [2, 2], [1, 4], [0, 0]])

area = 0.5 * np.abs(np.dot(v[:-1, 0], v[1:, 1]) - np.dot(v[:-1, 1], v[1:, 0]))

perimeter = np.sum(np.sqrt(np.sum(np.diff(v, axis=0)**2, axis=1)))

plt.plot(*v.T, 'b-', label='Polygon')

plt.fill(*v.T, 'b', alpha=0.3)

plt.gca().set_aspect('equal', adjustable='box')

plt.legend()

plt.show()

print(f"Area: {area}")
print(f"Perimeter: {perimeter}")

Q 3)Attempt the following.

1)Write a python program to solve the following LPP:

Min Z = 3.5 x +2y

Subject to x + y ≥ 5

x≥4

y≤2

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += 3.5 * x + 2 * y

prob += x + y >= 5

prob += x >= 4

prob += y <= 2

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")
print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to solve the following LPP:

Min Z= x + y

Subject to x ≥ 6

y≥6

x + y ≤ 11

x ≥ 0,y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Minimize_Z", pulp.LpMinimize)

x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

prob += x + y

prob += x >= 6

prob += y >= 6

prob += x + y <= 11

prob.solve(pulp.PULP_CBC_CMD())
print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal value of Z: {pulp.value(prob.objective)}")

print(f"Optimal values of x: {pulp.value(x)}, y: {pulp.value(y)}")

B) Attempt any one of the following.

1) Apply python program each of the following transformations on the points P[3,-1]

I) Refection through X-axis

II) Scaling in X- coordinate by factor 2

III) Scaling in Y-coordinate by factor 1.5

IV) Relfection through the line y=x

Ans:

import numpy as np

P = np.array([3, -1])

reflection_x_matrix = np.array([[1, 0], [0, -1]])

P_reflected_x = reflection_x_matrix @ P

P_scaled_x = P_reflected_x * np.array([2, 1])

P_scaled_y = P_scaled_x * np.array([1, 1.5])

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

P_reflected_y_eq_x = reflection_y_eq_x_matrix @ P_scaled_y

print(f"Original point: {P}")

print(f"After reflection through X-axis: {P_reflected_x}")


print(f"After scaling in X-coordinate by factor 2: {P_scaled_x}")

print(f"After scaling in Y-coordinate by factor 1.5: {P_scaled_y}")

print(f"After reflection through the line y = x: {P_reflected_y_eq_x}")

2) Find the combined transformation of the line segment between the points A[4,-1] & B[3,0] by
using python program for the following sequence of transformations:-

I) Rotation about origin through an angle π

II) Shearing in Y direction by 4.5 units

III) Scaling in X-coordinate by 3 units

IV) Reflection through the line y=x

Ans:

import numpy as np

A = np.array([4, -1])

B = np.array([3, 0])

theta = np.pi

rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]])

A_rotated = A @ rotation_matrix

B_rotated = B @ rotation_matrix

shear_matrix = np.array([[1, 0], [4.5, 1]])

A_sheared = A_rotated @ shear_matrix

B_sheared = B_rotated @ shear_matrix

A_scaled = A_sheared * np.array([3, 1])


B_scaled = B_sheared * np.array([3, 1])

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

A_final = A_scaled @ reflection_matrix

B_final = B_scaled @ reflection_matrix

print(f"Original points: A = {A}, B = {B}")

print(f"After rotation about origin through π: A = {A_rotated}, B = {B_rotated}")

print(f"After shearing in Y direction by 4.5 units: A = {A_sheared}, B = {B_sheared}")

print(f"After scaling in X-coordinate by 3 units: A = {A_scaled}, B = {B_scaled}")

print(f"After reflection through the line y = x: A = {A_final}, B = {B_final}")


PYTHON PROGRAMMING LANGUAGE=II

SLIP NO:25

Q 1)Attempt any two of the following.

1)Write a python program to generate 3D plot of the functions z= sin x+ cos y in -10<x, y < 10.

Ans:

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-10, 10, 400)

y = np.linspace(-10, 10, 400)

x, y = np.meshgrid(x, y)

z = np.sin(x) + np.cos(y)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(x, y, z, cmap='viridis')

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

ax.set_title('3D plot of $z = \sin(x) + \cos(y)$')

plt.show()

2)Using python plot the graph of function f(x) = sin^{-1}x on the interval [-1,1]
Ans:

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 100)

y = np.arcsin(x)

plt.plot(x, y, 'b-', marker='o')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.title('$f(x) = \sin^{-1}(x)$ in [-1, 1]')

plt.grid(True)

plt.show()

3)Using python plot the surface plot of function z=cos(x^2-y^2-0.5) in the interval from -1 < x, y < 1.

Ans:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(-1, 1, 400)

y = np.linspace(-1, 1, 400)

x, y = np.meshgrid(x, y)

z = np.cos(x**2 - y**2 - 0.5)

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, cmap='viridis')

plt.show()

Q 2)Attempt any two of the following.

1)Rotate the line segment by 180° having end points (1,0) and (2,-1)

Ans:

import numpy as np

import matplotlib.pyplot as plt

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

point2 = np.array([2, -1])

theta = np.radians(180)

rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],

[np.sin(theta), np.cos(theta)]])

rotated_point1 = point1 @ rotation_matrix

rotated_point2 = point2 @ rotation_matrix

plt.figure()

plt.plot([point1[0], point2[0]], [point1[1], point2[1]], 'bo-', label='Original')

plt.plot([rotated_point1[0], rotated_point2[0]], [rotated_point1[1], rotated_point2[1]], 'ro--',


label='Rotated 180°')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')
plt.legend()

plt.grid(True)

plt.title('Rotation of Line Segment by 180°')

plt.show()

2)Using sympy declare the points P(5,2),Q(5,-2),R(5,0),cheak whether these points are collinear.Declare
the ray passing through the points P and Q find the length ogf the this ray between P and Q .Also find
slop of this ray

Ans:

import sympy as sp

P = sp.Point(5, 2)

Q = sp.Point(5, -2)

R = sp.Point(5, 0)

collinear = sp.Point.is_collinear(P, Q, R)

print(f"The points P, Q, and R are collinear: {collinear}")

ray_PQ = sp.Ray(P, Q)

length_PQ = P.distance(Q)

print(f"The length of the ray between P and Q is: {length_PQ}")

slope_PQ = (Q.y - P.y) / (Q.x - P.x) if Q.x != P.x else None

print(f"The slope of the ray between P and Q is: {slope_PQ if slope_PQ is not None else 'undefined
(vertical line)'}")

3)Generate triangle with vertices (0,0),(4,0),(1,4) cheak whether the triangle is Scalene triangle.

Ans:
import numpy as np

A = np.array([0, 0])

B = np.array([4, 0])

C = np.array([1, 4])

AB = np.linalg.norm(B - A)

BC = np.linalg.norm(C - B)

CA = np.linalg.norm(A - C)

is_scalene = (AB != BC) and (BC != CA) and (CA != AB)

print(f"The triangle with vertices A{A}, B{B}, and C{C} is a scalene triangle: {is_scalene}")

Q 3)Attempt the following.

A) Attempt any one of the following.

1)Write the python program to solve the following LPP.

Max Z= 150x + 75y

Subject to 4x + 6y ≤ 24

5x + 3y ≤ 15

x ≥ 0, y ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)

x = pulp.LpVariable('x', lowBound=0)
y = pulp.LpVariable('y', lowBound=0)

prob += 150 * x + 75 * y

prob += 4 * x + 6 * y <= 24

prob += 5 * x + 3 * y <= 15

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal Z: {pulp.value(prob.objective)}")

2)Write a python program to solve the following LPP:

Max Z= 4x + y + 3z +5w

Subject to 4x +6y -5z -4w ≥ 20

-3x - 2y +4z + w ≤ 10

-8x - 3y + 3z + 2w ≤ 20

x + y ≤ 11

x,y,z,w ≥ 0

Ans:

import pulp

prob = pulp.LpProblem("Maximize_Z", pulp.LpMaximize)


x = pulp.LpVariable('x', lowBound=0)

y = pulp.LpVariable('y', lowBound=0)

z = pulp.LpVariable('z', lowBound=0)

w = pulp.LpVariable('w', lowBound=0)

prob += 4 * x + y + 3 * z + 5 * w

prob += 4 * x + 6 * y - 5 * z - 4 * w >= 20

prob += -3 * x - 2 * y + 4 * z + w <= 10

prob += -8 * x - 3 * y + 3 * z + 2 * w <= 20

prob += x + y <= 11

prob.solve()

print(f"Status: {pulp.LpStatus[prob.status]}")

print(f"Optimal x: {pulp.value(x)}")

print(f"Optimal y: {pulp.value(y)}")

print(f"Optimal z: {pulp.value(z)}")

print(f"Optimal w: {pulp.value(w)}")

B)Attempt any onre of the following.

1)Write a python program to apply the following transformation on the points(2,-4):

I) Refection through X-axis.

II)Scaling in X-coordinate by factor 6

III)Shearing in X direction by 4 unit

IV)Rotation about origin through an angle bu 30 degree


Ans:

import numpy as np

def apply_transformations(point):

matrices = [

np.array([[1, 0], [0, -1]]),

np.array([[6, 0], [0, 1]]),

np.array([[1, 4], [0, 1]]),

np.array([[np.cos(np.radians(30)), -np.sin(np.radians(30))], [np.sin(np.radians(30)),


np.cos(np.radians(30))]])

for matrix in matrices:

point = np.dot(matrix, point)

return point

point = np.array([2, -4])

transformed_point = apply_transformations(point)

print(f"Original Point: {point}")

print(f"Transformed Point: {transformed_point}")

3)Write a python program to combined transformation on the line segment between the points A[3,2] &
B[2,-3] for the following sequence of transformations:-

I) Rotation about origin through an angle π/6

II) Scaling in Y-coordinate by -4 units

III)Uniform scaling by -6.4 units

IV)Shearing in Y direction by 5 units


Ans :

import numpy as np

def apply_transformations(points):

matrices = [

np.array([[np.cos(np.pi/6), -np.sin(np.pi/6)], [np.sin(np.pi/6), np.cos(np.pi/6)]]),

np.array([[1, 0], [0, -4]]),

np.array([[-6.4, 0], [0, -6.4]]),

np.array([[1, 0], [5, 1]])

for matrix in matrices:

points = np.dot(matrix, points)

return points

points = np.array([[3, 2], [2, -3]]).T

transformed_points = apply_transformations(points)

print(f"Original Points: A: {points[:, 0]}, B: {points[:, 1]}")

print(f"Transformed Points: A: {transformed_points[:, 0]}, B: {transformed_points[:, 1]}")

You might also like