MTC-243 Python Programing Language II Slips Semester IV-1
MTC-243 Python Programing Language II Slips Semester IV-1
Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 01
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot 2D graph of the functions f(x) = x 2 and g(x) = x3 in [−1, 1]
import numpy as np
import matplotlib.pyplot as plt
# Add a legend
plt.legend()
# Add grid
plt.grid(True)
(b) Write a Python program to plot 3D graph of the function f(x) = e−x2 in [−5, 5] with green dashed
points line with upward pointing triangle.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create an array of x values from -5 to 5 with 100 points
x = np.linspace(-5, 5, 100)
# Create an array of y values from -5 to 5 with 100 points
y = np.linspace(-5, 5, 100)
# Create a meshgrid of x and y values
X, Y = np.meshgrid(x, y)
# Calculate the corresponding z values using the function f(x, y) = e^(-x^2 - y^2)
Z = np.exp(-X**2 - Y**2)
# Create the 3D plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot the surface
ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.5)
# Plot the green dashed points line with upward pointing triangles
ax.plot(x, np.zeros_like(x), np.exp(-x**2), 'go', markersize=8, linestyle='--', marker='^', label='f(x)
= e^(-x^2)')
# Add a legend
ax.legend()
# Set the limits of the plot’s axes
ax.set_xlim(-5.1, 5.1)
ax.set_ylim(-0.1, 5.1)
ax.set_zlim(0, 1.1)
# Add axis labels and title
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x, y)')
ax.set_title('Graph of f(x, y) = e^(-x^2 - y^2) in [-5, 5] with green dashed points line with upward
pointing triangles')
# Show the plot
plt.show()
(c) Using python, represent the following information using a bar graph (in green color )
Item clothing Food rent Petrol Misc.
# Data
expenditure = {'clothing': 600, 'Food': 4000,
'rent': 2000, 'Petrol': 1500, 'Misc.': 700}
[10]
Q.2 Attempt any TWO of the following.
(a) 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.
(c) Write a Python program to find the area and perimeter of the ∆ABC, where A[0, 0], B[5, 0], C[3, 3].
import math
# Define the vertices of the triangle
A = (0, 0)
B = (5, 0)
C = (3, 3)
# Calculate the length of the sides using the distance formula
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)
# Calculate the semi-perimeter of the triangle
s = (AB + BC + CA) / 2
# Calculate the area of the triangle using Heron's formula
area = math.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter of the triangle
perimeter = AB + BC + CA
# Print the results
print("The area of the triangle is:", area)
print("The perimeter of the triangle is:", perimeter)
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Min Z = x + y
subject to x ≥ 6
y≥6
x + y ≤ 11
x ≥ 0, y ≥ 0.
import pulp
# Create a new LP problem
prob = pulp.LpProblem("Minimize_x_plus_y", pulp.LpMinimize)
# Define the variables
x = pulp.LpVariable("x", lowBound=0)
y = pulp.LpVariable("y", lowBound=0)
# Define the objective function
prob += x + y
# Define the constraints
prob += x >= 6
prob += y >= 6
prob += x + y <= 11
# Solve the problem using the simplex method
prob.solve()
# Print the results
print("Status:", pulp.LpStatus[prob.status])
print("Objective value:", prob.objective.value())
print("x:", x.varValue)
print("y:", y.varValue)
1
(b) Attempt any ONE of the following. [8]
(i) Apply Python program in each of the following transformations on the point 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) Reflection through the line y = x.
(ii) Find the combined transformation of the line segment between the points A[5, −2] & B[4, 3]
by using Python program for the following sequence of transformations:-
(I) Rotation about origin through an angle π.
(II) Scaling in X− coordinate by 2 units.
(III) Reflection through the line y = −x.
(IV) Shearing in X direction by 4 units.
import numpy as np
Slip No. : 02
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot 2D graph of the functions f(x) = log10(x) in the interval [0, 10].
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(a) Using sympy declare the points A(0, 2), B(5, 2), C(3, 0) check whether these points are collinear.
Declare the line passing through the points A and B, find the distance of this line from point C.
import sympy as sp
import math
Max Z = 5x + 3y
subject to x + y ≤ 7
2x + 5y ≤ 1
x ≥ 0, y ≥ 0.
from scipy.optimize import linprog
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Max Z = 3x + 2y + 5z
subject to x + 2y + z ≤ 430
3x + 2z ≤ 460
x + 4y ≤ 120
x ≥ 0, y ≥ 0, z ≥ 0.
import pulp as pp
(i) Apply Python program in each of the following transformations 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.
import numpy as np
# Initial point P
P = np.array([4, -2])
import numpy as np
import matplotlib.pyplot as plt
A_scaled = scale_x(A_shear, 3)
B_scaled = scale_x(B_shear, 3)
A_refl = reflect_xy(A_scaled)
B_refl = reflect_xy(B_scaled)
Slip No. : 03
Time: 3 Hours Max. Marks: 35
import numpy as np
import matplotlib.pyplot as plt
Number of students 65 30 54 10 20
Write a Python program to generate 3D plot of the functions z = sin x + cos y in −10 < x, y < 10.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
(a) Write a Python program to reflect the line segment joining the points A[5, 3] & B[1, 4] through
the line y = x + 1
import math
import numpy as np
(c) Generate line segment having endpoints (0, 0) and (10, 10) find midpoint of line
segment.
import matplotlib.pyplot as plt
Min Z = 3.5x + 2y
subject to x + y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
5
(b) Attempt any ONE of the following. [8]
(i) Apply Python program in each of the following transformations 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.
P_reflected =
reflect_y_minus_x(P_scaled)
print("Point after reflection through the line
y = -x:", P_reflected)
(ii) Find the combined transformation of the line segment between the points A[2, −1] & B[5, 4]
by using Python program for the following sequence of transformations:-
(I) Rotation about origin through an angle π.
(II) Scaling in X−coordinate by 3 units.
(III) Shearing in X direction by 6 units.
(IV) Reflection through the line y = x.
A_sheared = shear_x(A_scaled, 6)
B_sheared = shear_x(B_scaled, 6)
A_reflected = reflect_y_equals_x(A_sheared)
B_reflected = reflect_y_equals_x(B_sheared)
Slip No. : 04
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot 2D graph of the functions f(x) = log10(x) in the interval [0, 10].
import numpy as np
import matplotlib.pyplot as plt
# Generate y values
y = f(x)
import numpy as np
import matplotlib.pyplot as plt
# Generate x values
x = np.linspace(-1, 1, 1000)
# Generate y values
y = f(x)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate x, y values
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
# Generate z values
Z = X**2 + Y**2
(a) If the line with points A[3, 1], B[5, −1] is transformed by the transformation matrix [T ] =
then using python, find the equation of transformed line.
import numpy as np
import turtle
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Max Z = 4x + y + 3z + 5w
subject to 4x + 6y − 5z − 4w ≥ −20
−8x − 3y + 3z + 2w ≤ 20
x + y ≤ 11
x ≥ 0, y ≥ 0, z ≥ 0, w ≥ 0.
import pulp
7
(b) Attempt any ONE of the following. [8]
(i) Plot 3D axes with labels as x−axis and z−axis and also plot following points with given
coordinates in one graph.
(I) (70, −25, 15) as a diamond in black colour,
(II) (50, 72, −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.
# Plot point I
ax.scatter(70, -25, 15, marker='D',
color='black', label='Point I')
# Plot point II
ax.scatter(50, 72, -45, marker='*',
color='green', label='Point II')
# Plot point IV
ax.scatter(20, 72, -45, marker='*',
color='red', label='Point IV')
# Add legend
ax.legend()
# Show plot
plt.show()
(ii) 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) Shearing in X direction by 9 units.
(II) Rotation about origin through an angle π.
(III) Scaling in X− coordinate by 2 units.
(IV) Reflection through the line y = x.
import numpy as np
8
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 05
Time: 3 Hours Max. Marks: 35
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x_values, y_values, z_values, cmap='viridis')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Surface Plot of f(x) = sin(x^2 + y^2)')
plt.show()
(c) Write a Python program to generate 3D plot of the functions z = sin x + cos y in the interval −10
< x, y < 10.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
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('Surface Plot of z = sin x + cos y')
plt.show()
Q.2 Attempt any TWO of the following. [10]
(a) Using python, generate triangle with vertices (0, 0), (4, 0), (4, 3) check whether the triangle is
Right angle triangle.
import math
import numpy as np
x = np.linspace(-7, 7, 50)
print(x)
(c) Write a Python program to find the area and perimeter of the ∆ABC, where A[0, 0], B[6, 0], C[4, 4].
import math
# Define the vertices of the triangle
A = (0, 0)
B = (6, 0)
C = (4, 4)
# Calculate the length of the sides
a = math.sqrt((B[0] - A[0])**2 + (B[1] - A[1])**2)
b = math.sqrt((C[0] - B[0])**2 + (C[1] - B[1])**2)
c = math.sqrt((A[0] - C[0])**2 + (A[1] - C[1])**2)
# Calculate the semi-perimeter
s = (a + b + c) / 2
# Calculate the area using Heron's formula
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
# Calculate the perimeter
perimeter = a + b + c
# Print the results
print("Area of the triangle: {:.2f}".format(area))
print("Perimeter of the triangle: {:.2f}".format(perimeter))
Max Z = 5x + 3y
subject to x + y ≤ 7
2x + 5y ≤ 1
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
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 ≥ 0, y ≥ 0, z ≥ 0, w ≥ 0.
import pulp
9
(b) Attempt any ONE of the following. [8]
(i) Apply Python program in each of the following transformations on the point P [3, 8]
(I) Refection through X−axis.
(II) Scaling in X−coordinate by factor 6.
(III) Rotation about origin through an angle 30◦.
(IV) Reflection through the line y = −x.
(ii) Write a python program to Plot 2D X−axis and Y−axis in black colour. In the same diagram
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], [8, 7], [4, 8], [2,
4]. (IV) Isosceles triangle with vertices [0, 0], [4, 0], [2, 4].
10
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 06
Time: 3 Hours Max. Marks: 35
(b) Using python, generate 3D surface Plot for the function f(x) = sin x 2 + y2 in the interval [0, 10].
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_title('3D Surface Plot of f(x) = sin(x^2 + y^2)')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
(c) Using Python, plot the graph of function f(x) = sin(x) −ex + 3x2 −log10(x) on the Interval [0, 2π].
import numpy as np
import matplotlib.pyplot as plt
(a) Using python, rotate the line segment by 180◦ having end points (1, 0) and (2, −1).
import numpy as np
import matplotlib.pyplot as plt
# Add a legend
plt.legend()
(b) Write a Python program to draw a polygon with vertices (0, 0), (2, 0), (2, 3) and (1, 6) and rotate
it by 180◦.
import turtle
# Create a new turtle object
t = turtle.Turtle()
# Draw a polygon with vertices (0, 0), (2, 0), (2, 3), and (1, 6)
t.penup()
t.goto(0, 0)
t.pendown()
t.goto(2, 0)
t.goto(2, 3)
t.goto(1, 6)
t.goto(0, 0)
(c) Using python, generate triangle with vertices (0, 0), (4, 0), (2, 4), check whether the triangle is
isosceles triangle.
import math
Max Z = x + y
subject to 2x − 2y ≥ 1
x+y≥2
x ≥ 0, y ≥ 0.
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Min Z = x + y
subject to x ≥ 6
y≥6
x + y ≤ 11
x ≥ 0, y ≥ 0.
11
(b) Attempt any ONE of the following. [8]
(i) Apply Python program in each of the following transformations on the point P [4, −2]
(I) Refection through Y−axis.
P = [4, -2]
reflection_matrix = [[-1, 0], [0, 1]]
P_reflected = [reflection_matrix[0][0]*P[0] + reflection_matrix[0][1]*P[1],
reflection_matrix[1][0]*P[0] + reflection_matrix[1][1]*P[1]]
print("Reflected point:", P_reflected)
P = [4, -2]
scaling_matrix = [[7, 0], [0, 1]]
P_scaled = [scaling_matrix[0][0]*P[0] + scaling_matrix[0][1]*P[1],
scaling_matrix[1][0]*P[0] + scaling_matrix[1][1]*P[1]]
print("Scaled point:", P_scaled)
P = [4, -2]
shearing_matrix = [[1, 0], [3, 1]]
P_sheared = [shearing_matrix[0][0]*P[0]
+ shearing_matrix[0][1]*P[1],
shearing_matrix[1][0]*P[0] +
shearing_matrix[1][1]*P[1]]
print("Sheared point:", P_sheared)
P = [4, -2]
reflection_matrix = [[0, -1], [1, 0]]
P_reflected =
[reflection_matrix[0][0]*P[0] +
reflection_matrix[0][1]*P[1],
reflection_matrix[1][0]*P[0] +
reflection_matrix[1][1]*P[1]]
print("Reflected point:", P_reflected)
(ii) Find the combined transformation by using Python program for the following sequence of
transformations:-
(I) Rotation about origin through an angle 60◦.
(II) Scaling in X−coordinate by 7 units.
(III) Uniform scaling by 4 units.
(IV) Reflection through the line y = x..
import math
Slip No. : 07
Time: 3 Hours Max. Marks: 35
(a) Plot the graph of f(x) = x4 in [0, 5] with red dashed line with circle markers.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(c) Write a python program to draw rectangle with vertices [1, 0], [2, 1], [1, 2] and [0, 1], its rotation
about the origin by π2 radians.
import pygame
import math
# Initialize Pygame
pygame.init()
(a) Write a Python program to reflect the line segment joining the points A[5, 3] & B[1, 4] through
the line y = x + 1.
import math
(b) Using sympy declare the points P (5, 2), Q(5, −2), R(5, 0), check whether these points are
collinear. Declare the ray passing through the points P and Q, find the length of this ray between
P and Q. Also find slope of this ray.
import sympy as sp
(c) Write a Python program in 3D to rotate the point (1, 0, 0) through X Plane in anticlockwise
direction (Rotation through Z axis) by an angle of 90◦.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Min Z = 3.5x + 2y
subject to x + y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Max Z = x + 2y + z
subject to x + 2y + 2z ≤ 1
3x + 2y + z ≥ 8
x ≥ 0, y ≥ 0, z ≥ 0.
import pulp
(i) Apply Python program in each of the following transformations on the point P [4, −2]
(I) Refection through Y−axis.
(II) Scaling in X−coordinate by factor 5.
(III) Rotation about origin through an angle π2
. (IV) Shearing in X direction by 72 units.
import math
(ii) Find the combined transformation of the line segment between the points A[7, −2] & B[6, 2]
by using Python program for the following sequence of transformations:-
(I) Rotation about origin through an angle π3 .
(II) Scaling in X− coordinate by 7 units.
(III) Uniform scaling by −4 units.
(IV) Reflection through the line X− axis.
import math
14
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 08
Time: 3 Hours Max. Marks: 35
(a) Plot the graphs of sin x, cos x, ex and x2 in [0, 5] in one figure with (2 × 2) subplots.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 100)
plt.show()
(b) Using Python plot the graph of function f(x) = cos(x) in the interval [0, 2π].
import numpy as np
import matplotlib.pyplot as plt
# Define the function
def f(x):
return np.cos(x)
# Define the interval
x = np.linspace(0, 2 * np.pi, 100)
# Plot the graph
plt.plot(x, f(x))
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = cos(x) in [0, 2π]')
plt.grid(True)
plt.show()
(c) Write a Python program to generate 3D plot of the functions z = sin x + cos y in −10 < x, y < 10.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
(a) Write a Python program in 3D to rotate the point (1, 0, 0) through XZ Plane in anticlockwise
direction (Rotation through Y axis) by an angle of 90◦.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial.transform import Rotation as R
(b) Using python, generate triangle with vertices (0, 0), (4, 0), (1, 4), check whether the triangle is
Scalene triangle.
(c) Write a Python program to find the area and perimeter of the ∆ABC, where A[0, 0], B[6, 0], C[4, 4].
import math
import pulp as p
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Max Z = 3x + 5y + 4z
subject to 2x + 3y ≤ 8
2y + 5z ≤ 10
3x + 2y + 4z ≤ 15
x ≥ 0, y ≥ 0, z ≥ 0.
import pulp as p
15
(b) Attempt any ONE of the following. [8]
(i) Apply Python program in each of the following transformations on the point P [4, −2]
(I) Refection through Y−axis.
P = [4, -2]
P_reflected = [-P[0], P[1]]
print("Reflected point:", P_reflected)
P = [4, -2]
P_scaled = [3*P[0], P[1]]
print("Scaled point:", P_scaled)
import math
P = [4, -2]
P_rotated = [-P[0], -P[1]]
print("Rotated point:", P_rotated)
P = [4, -2]
P_sheared = [P[0] - 2*P[1], P[1] + 4*P[0]]
print("Sheared point:", P_sheared)
(ii) Find the combined transformation of the line segment between the points A[4, −1] & B[3, 2]
by using Python program for the following sequence of transformations:-
(I) Rotation about origin through an angle π4 .
(II) Shearing in Y direction by 4 units.
(III) Scaling in X− coordinate by 5 units.
(IV) Reflection through y− axis.
import numpy as np
scaling_matrix = np.array([
[5, 0],
[0, 1]
])
reflection_matrix = np.array([
[-1, 0],
[0, 1]
])
Slip No. : 09
Time: 3 Hours Max. Marks: 35
(a) Write a python program to Plot 2DX-axis and Y-axis black color and in the same diagram plot
green triangle with vertices [5, 4], [7, 4], [6, 6].
import numpy as np
(c) Using Python plot the graph of function f(x) = cos(x) on the interval [0, 2π].
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
y = np.cos(x)
plt.plot(x, y)
plt.show()
Q.2 Attempt any TWO of the following. [10]
(a) Write a python program to rotate the ray by 90◦ having starting point (1, 0) and (2, −1).
# Rotate by 90°
new_x, new_y = rotate_ray(x, y, 90)
# Rotate by 90°
new_x, new_y = rotate_ray(x, y, 90)
(b) Using sympy, declare the points A(0, 7), B(5, 2). Declare the line segment passing through
them. Find the length and midpoint of the line segment passing through points A and B.
import sympy as sp
(c) Write a python program to find the area and perimeter of ∆ABC where A(0, 0), B(5, 0), C(3, 3).
import math
import numpy as np
from scipy.optimize import linprog
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Max Z = 4x + y + 3z + 5w
subject to 4x + 6y − 5z − 4w ≥ 20
−3x − 2y + 4z + w ≤ 10
−8x − 3y + 3z + 2w ≤ 20
x ≥ 0, y ≥ 0, z ≥ 0, w ≥ 0.
import pulp
17
(b) Attempt any ONE of the following. [8]
(i) Write a python program to apply the following transformations on the point (−2, 4) :
(I) Shearing in Y direction by 7 units.
(II) Scaling in X and Y direction by 72 and 7 units respectively.
(III) Shearing in X and Y direction by 4 and 7 units respectively.
(IV) Rotation about origin by an angle 60◦.
import math
(ii) Write a python program to find the combined transformation of the line segment between
the points A[5, 3] & 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 units respectively.
import math
# Define the points A and B
A = [5, 3]
B = [1, 4]
# Transformation I: Rotate about origin through an angle π/2
A = [-3, -5]
B = [4, -1]
# Transformation II: Uniform scaling by -3.5 units
A = [-10.5, -17.5]
B = [-3.5, -3.5]
# Transformation III: Scaling in Y-axis by 5 units
A = [-10.5, -87.5]
B = [-3.5, -17.5]
# Transformation IV: Shearing in X and Y direction by 3 and 4 units respectively
A = [(-10.5*3) + (-87.5), (-10.5*4) + (-87.5)]
B = [(-3.5*3) + (-17.5), (-3.5*4) + (-17.5)]
A = [A[0] + 3*A[1], A[1]]
B = [B[0] + 3*B[1], B[1]]
# Print the transformed points A and B
print("Transformed points A and B:", A, B)
18
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 10
Time: 3 Hours Max. Marks: 35
import numpy as np
b)Represent the following information using a bar graph (in green color)
import matplotlib.pyplot as plt
# Define the item labels and expenditure values
items = ['clothing', 'Food', 'rent', 'Petrol', 'Misc.']
expenditures = [600, 4000, 2000, 1500, 700]
# Define the color of the bars
color = 'green'
# Create the bar graph
plt.bar(items, expenditures, color=color)
# Set the title and labels of the graph
plt.title('Expenditure in Rs')
plt.xlabel('Item')
plt.ylabel('Expenditure')
# Show the graph
plt.show()
(c) 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 color ), with title to the graph.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
(a) Write a python program to rotate the ∆ABC by 90◦ where A(1, 1), B(2, −2), C(1, 2).
import numpy as np
(b) Draw a polygon with vertices (0, 0), (2, 0), (2, 3), (1, 6). Write a python program to rotate the
polygon by 180◦.
import matplotlib.pyplot as plt
import numpy as np
(c) Find the area and perimeter of the ∆ABC, where A[0, 0], B[5, 0], C[3, 3].
import math
# Define the coordinates of the vertices
A = (0, 0)
B = (5, 0)
C = (3, 3)
# Calculate the lengths of the sides
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)
# Calculate the semi-perimeter
s = (AB + BC + CA) / 2
# Calculate the area using Heron's formula
area = math.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter
perimeter = AB + BC + CA
print("The area of the triangle is", area)
print("The perimeter of the triangle is", perimeter)
Max Z = x + y
subject to x − y ≥ 1
x+y≥2
x ≥ 0, y ≥ 0.
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Max Z = 3x + 2y + 5z
subject to x + 2y + z ≤ 430
3x + 4z ≤ 460
x + 4y ≤ 120
x ≥ 0, y ≥ 0, z ≥ 0.
import pulp
import math
(ii) Write a python 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 coordinate by 3 & 5 units respectively.
(IV) Shearing in X direction by 6 units.
import math
20
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 11
Time: 3 Hours Max. Marks: 35
(a) Write a python program to plot 3D axes with labels as X−axis, Y−axis and Z−axis and also plot
following point with given coordinates in the same graph: (70, −25, 15) as a diamond in black
colour.
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(b) Plot the graph of y = e−x2 in [−5, 5] with red dashed-points line with Upward Pointing triangle.
# Plot the graph with red dashed-points line with upward pointing triangles
ax.plot(x, y, linestyle='--', marker='^', markersize=5, color='red', label='y = e^(-x^2)')
# Add a legend
ax.legend()
(c) Draw a bar graph in GREEN colour to represent the data below:
Subject Maths Science English Marathi Hindi
Percentage of passing 68 90 70 85 91
# Data
subjects = ['Maths', 'Science', 'English',
'Marathi', 'Hindi']
percentages = [68, 90, 70, 85, 91]
(a) Write a python program to reflect the ∆ABC through the line y = 3 where A(1, 0), B(2, −1), C(−1, 3).
(b) Write a python program to rotate the ∆ABC by 90◦ where A(1, 2), B(2, −2), C(−1, 2).
(c) Write a python program to draw a polygon with 6 sides and radius 1 centered at (1, 2) and find
its area and perimeter.
import math
import matplotlib.pyplot as plt
# Given parameters
n = 6 # number of sides
r = 1 # radius
x, y = 1, 2 # center of the polygon
# Calculate the vertices of the polygon
vertices = [(x + r * math.cos(2 * math.pi * (i - 1) / n),
y + r * math.sin(2 * math.pi * (i - 1) / n))
for i in range(1, n + 1)]
# Calculate the area of the polygon
area = 0.5 * r * r * n * math.sin(2 * math.pi / n)
# Calculate the perimeter of the polygon
perimeter = n * r * 2
# Plot the polygon
plt.plot(*zip(*vertices), 'b-')
plt.scatter(*vertices, color='r', marker='o')
# Set the axis labels
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Regular Polygon with {} Sides and Radius {}'.format(n, r))
plt.text(x, y, 'Center', color='b', fontsize=12, fontweight='bold')
# Show the plot
plt.show()
# Print the area and perimeter of the polygon
print('Area: {:.2f}'.format(area))
print('Perimeter: {:.2f}'.format(perimeter))
Q.3 Attempt the following.
Min Z = x + y
subject to x ≥ 6
y≥6
x + y ≥ 11
x ≥ 0, y ≥ 0.
import pulp
(ii) Write a program in python to solve the following LPP and to find optimal solution if exists:
Max Z = 3x + 5y + 4z
subject to 2x + 3y ≤ 8
2y + 5z ≤ 10
3x + 2y + 4z ≤ 15
x ≥ 0, y ≥ 0, z ≥ 0.
import pulp as p
# Define the problem
prob = p.LpProblem('Maximize_3x_5y_4z', p.LpMaximize)
21
(b) Attempt any ONE of the following. [8]
(i) Write a python program to apply the following transformations on the point (−2, 4) :
(I) Shearing in Y direction by 7 units.
(II) Scaling in X and Y direction by 32 and 4 units respectively.
(III) Shearing in X and Y direction by 2 and 4 units respectively.
(IV) Rotation about origin by an angle 45◦.
import math
point = (-2, 4)
import math
def uniform_scaling(point,
scale_factor):
x, y = point
return (scale_factor * x, scale_factor
* y)
point_A = (3, 2)
point_B = (2, -3)
22
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 12
Time: 3 Hours Max. Marks: 35
(a) Write a python program to plot the graph of y = x3 + 10x − 5, for x ∈ [−10, 10] in red colour.
import numpy as np
import matplotlib.pyplot as plt
(b) Write a python program in 3D to rotate the point (1, 0, 0) through XZ− plane in clockwise
direction (rotation through Y− axis by an angle of 90◦).
import numpy as np
(c) Using Python plot the graph of function f(x) = x2 on the interval [−2, 2].
import numpy as np
import matplotlib.pyplot as plt
(a) Write a python program to rotate the segment by 180◦ having end points (1, 0) and (2, −1).
import math
(b) Write a python program to draw a polygon with 8 sides having radius 5 centered at origin and
find its area and perimeter.
(c) Write a python program to find the area and perimeter of the ∆XY Z, where X(1, 2), Y (2, −2),
Z(−1, 2).
import math
Min Z = 3.5x + 2y
subject to x + y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
(ii) Write a python program to solve LPP and find optimal solution if exists.
Max Z = 3x + 5y + 4z
subject to 2x + 3y ≤ 8
2y + 5z ≤ 10
3x + 2y + 4z ≤ 15
x ≥ 0, y ≥ 0, z ≥ 0.
import numpy as np
from scipy.optimize import linprog
import numpy as np
(ii) 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 Transformations:
(I) Rotation about origin through an angle π4 .
(II) Uniform scaling by 7.3 units.
(III) Scaling in X− coordinate by 3 units.
(IV) Shearing in X direction by 12 units.
import numpy as np
24
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 13
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot 2D graph of the functions f(x) = x 2 and g(x) = x3 in [−1, 1].
x = np.linspace(-1, 1, 100)
(b) Using Python, plot the surface plot of parabola z = x2 + y2 in −6 < x, y < 6.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(c) 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 color ), with title to the graph.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(a) Write a python program to reflect the ∆ABC through the line y = 3 where A(1, 0), B(2, −1), C(−1, 3).
(b) Find the area and perimeter of the ∆ABC, where A[0, 0], B[5, 0], C[3, 3].
# Define the coordinates of A, B, and C
A = (0, 0)
B = (5, 0)
C = (3, 3)
# Calculate the distance between A and B
AB = ((B[0] - A[0])**2 + (B[1] - A[1])**2)**0.5
# Calculate the distance between B and C
BC = ((C[0] - B[0])**2 + (C[1] - B[1])**2)**0.5
# Calculate the distance between C and A
CA = ((A[0] - C[0])**2 + (A[1] - C[1])**2)**0.5
# Calculate the semi-perimeter of the triangle
s = (AB + BC + CA) / 2
# Calculate the area of the triangle using Heron's formula
area = (s*(s-AB)*(s-BC)*(s-CA))**0.5
# Print the area and perimeter of the triangle
print("Area =", area)
print("Perimeter =", AB + BC + CA)
(c) Using sympy declare the points P(5, 2), Q(5, −2), R(5, 0), check whether these points are
collinear. Declare the ray passing through the points P and Q, find the length of this ray between
P and Q. Also find slope of this ray.
Max Z = 5x + 3y
subject to x + y ≤ 7
2x + 5y ≤ 1
x ≥ 0, y ≥ 0.
import pulp as pp
(ii) Write a python program to display the following LPP by using pulp module and simplex
method. Find its optimal solution if exist.
Max Z = 3x + 2y + 5z
subject to x + 2y + z ≤ 430
3x + 4z ≤ 460
x + 4y ≤ 120
x ≥ 0, y ≥ 0, z ≥ 0.
import pulp as pp
25
(b) Attempt any ONE of the following. [8]
(i) Write a python program to apply the following transformations on the point (−2, 4) :
(I) Shearing in Y direction by 7 units.
(II) Scaling in X and Y direction by 72 and 7 units respectively.
(III) Shearing in X and Y direction by 4 and 7 units respectively.
(IV) Rotation about origin by an angle 60◦.
import math
(ii) Write a python program to Plot 2D x-axis and y-axis in black colour. In the same diagram
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], [8, 7], [4, 8], [2,
4]. (IV) Isosceles triangle with vertices [0, 0], [4, 0], [2, 4].
verts_rectangle = [(2, 2), (10, 2), (10, 8), (2, 8), (2, 2)]
ax.fill(verts_rectangle, color='blue', alpha=0.5)
verts_polygon = [(6, 2), (10, 4), (8, 7), (4, 8), (2, 4), (6, 2)]
ax.fill(verts_polygon, color='red', alpha=0.5)
Slip No. : 14
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot 2D graph of the functions f(x) = x 2 and g(x) = x3 in [−1, 1].
# Add a legend
ax.legend()
(b) Write a Python program to plot 3D graph of the function f(x) = e−x2 in [−5, 5] with green dashed
points line with upward pointing triangle.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a range of x values from -5 to 5
x = np.linspace(-5, 5, 100)
# Calculate the corresponding y values using the function f(x) = e^-x^2
y = np.exp(-x**2)
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the function with a green dashed line and an upward pointing triangle marker
ax.plot(x, y, np.zeros_like(x), color='green', linestyle='--', marker='^', markersize=5)
# Set the limits of the x, y, and z axes
ax.set_xlim(-5, 5)
ax.set_ylim(0, 1.5)
ax.set_zlim(0, 1.5)
# Set the labels of the x, y, and z axes
ax.set_xlabel('x')
ax.set_ylabel('y = e^-x^2')
ax.set_zlabel('z')
# Show the plot
plt.show()
(c) Write a Python program to generate 3D plot of the function z = sin(x) + cos(y) in −5 < x, y < 5.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(a) 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.
(b) Write a Python program to draw a polygon with vertices (0, 0), (2, 0), (2, 3) and (1, 6) and rotate
it by 180o.
import turtle
(c) 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].
import math
Min Z = x + y
subject to x ≥ 6
y≥6
x + y ≤ 11
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
27
(b) Attempt any ONE of the following. [8]
i. Apply each of the following transformations on the point P [2, −3].
I. Reflection through X-axis.
II. Scaling in X-coordinate by factor 2.
III. Scaling in Y-coordinate by factor 1.5.
IV. Reflection through the line y = x.
# Initial point P
P = [2, -3]
ii. Apply each of the following transformations on the point P [3, −1].
I. Shearing in Y direction by 2 units.
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 30 degrees.
# Initial point P
P = [3, -1]
P_rotated = [
P_sheared_xy[0] * math.cos(angle_in_radians) - P_sheared_xy[1] *
math.sin(angle_in_radians),
P_sheared_xy[0] * math.sin(angle_in_radians) + P_sheared_xy[1] *
math.cos(angle_in_radians)
]
28
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 15
Time: 3 Hours Max. Marks: 35
import math
(b) Write the Python program to plot the graph of the function, using def()
(c) Write the python program to plot the graphs of sin x, cos x, ex and x2 in [0, 5] in one figure with
2 × 2 subplots.
(a) Write the Python program to rotate the triangle ABC by 180 degree, where A[1, 2], B[2, −2] & C[−1, 2].
def rotate_point(point):
return (-point[0], -point[1])
A = [1, 2]
B = [2, -2]
C = [-1, 2]
A_rotated = rotate_point(A)
B_rotated = rotate_point(B)
C_rotated = rotate_point(C)
print("Rotated points:")
print("A:", A_rotated)
print("B:", B_rotated)
print("C:", C_rotated)
(b) Write the Python program to plot the graph of function f(x) = ex in the interval [−10, 10].
import numpy as np
import matplotlib.pyplot as plt
(c) Write a Python program to plot the 3D graph whose parametric equation is (cos(2x), sin(2x), x)
for 10 ≤ x ≤ 20 (in red color ), with title to the graph.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.show()
Min Z = 3.5x + 2y
subject to x + y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
# Bounds on variables
bounds = ((0, None), (0, None))
Min Z = x + y
subject to x ≥ 6
y≥6
x + y ≤ 11
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
# Bounds on variables
bounds = ((0, None), (0, None))
29
(b) Attempt any ONE of the following. [8]
i. Write a Python program to find the combined transformation of the line segment between
the points A[5, 3] and B[1, 4] for the following sequence of transformations:
I. First rotation about origin through an angle π/2.
II. Followed by scaling in x co-ordinate by 5 units.
III. Followed by reflection through the line y = −x.
import numpy as np
ii. Write a Python program to apply each of the following transformations on the point P [−2, 4].
I. Reflection through the line y = x + 1.
II. Scaling in Y -coordinate by factor 1.5.
III. Shearing in X direction by 2 units.
IV. Rotation about origin by an angle 45 degrees.
Slip No. : 16
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot graph of the function f(x, y) = –x2 − y2 when −10 ≤ x, y ≤ 10.
import numpy as np
import matplotlib.pyplot as plt
(b) Write a Python program to plot graph of the function f(x) = log(3x 2), in [1, 10] with black dashed
points.
import numpy as np
import matplotlib.pyplot as plt
# Define the function f(x)
f = lambda x: np.log(3*x**2)
# Create an array of x values in the interval [1, 10]
x = np.linspace(1, 10, 100)
# Evaluate the function at each x value
y = f(x)
# Create a plot of the function with black dashed points
plt.plot(x, y, 'k--')
# Set the x and y axis labels and title
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = log(3x^2) in [1, 10]')
# Show the plot
plt.show()
(c) Write a Python program to generate plot of the function f(x) = x 2, in the interval [−5, 5], in figure
of size 6 × 6 inches.
import numpy as np
import matplotlib.pyplot as plt
(a) Write a Python program to declare the line segment passing through the points A(0, 7), B(5, 2).
Also find the length and midpoint of the line segment passing through points A and B.
(b) Write a Python program to draw a polygon with vertices (0, 0), (2, 0), (2, 3) and (1, 6) and rotate
it by 90o.
import turtle
# Create a new turtle screen and set its background color
screen = turtle.Screen()
screen.bgcolor("white")
(c) Write a Python program to Generate vector x in the interval [0, 15] using numpy package with
100 subintervals.
import numpy as np
# Generate the vector x with 100 subintervals in the interval [0, 15]
x = np.linspace(0, 15, 100)
Max Z = 5x + 3y
subject to 3x + 5y ≤ 15
6x + 2y ≥ 24
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
Min Z = 3.5x + 2y
subject to x + y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
# Define the colors for the original triangle and its reflections
colors = ['b', 'g', 'r', 'y', 'c']
# Add a legend
plt.legend()
ii. Write a python program to plot the Triangle with vertices at [3, 3], [3, 6], [0, 6] and its
reflections through, line y = x and y-axis. Also plot the mirror lines.
# Define the colors for the original triangle and its reflections
colors = ['b', 'g', 'r', 'y', 'c']
# Add a legend
plt.legend()
Slip No. : 17
Time: 3 Hours Max. Marks: 35
(a) Write a python program to plot the 3D graph of the function z = x 2 + y2 in −6 < x, y < 6 using
surface plot.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a grid of x and y values
x = np.linspace(-6, 6, 100)
y = np.linspace(-6, 6, 100)
X, Y = np.meshgrid(x, y)
# Calculate the z values for each point on the grid
Z = X**2 + Y**2
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the surface
ax.plot_surface(X, Y, Z, cmap='viridis')
# Set the axis labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# Set the title
ax.set_title('3D Surface Plot of z = x^2 + y^2')
# Show the plot
plt.show()
(b) Write a python program to plot 3D contours for the function f(x, y) = log(x 2y2) when −5 ≤ x, y ≤
5, with greens color map.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the function
def f(x, y):
return np.log(x**2 * y**2)
# Create a grid of x and y values
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the contours
contours = ax.contour(X, Y, f(X, Y), levels=np.linspace(-10, 10, 20), cmap='Greens')
# Set the axis labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# Set the title
ax.set_title('3D Contours of f(x, y) = log(x^2y^2)')
# Show the plot
plt.show()
(c) Write a Python program to reflect the line segment joining the points A[−5, 2] and B[1, 3]
through the line y = x.
import numpy as np
import matplotlib.pyplot as plt
(a) Write a python program to rotate the line segment by 180 degrees having end points (1, 0) and
(2, −1).
import numpy as np
import matplotlib.pyplot as plt
(b) Write a python program to plot triangle with vertices [3, 3], [5, 6], [5, 2], and its rotation about the
origin by angle −π radians.
import numpy as np
import matplotlib.pyplot as plt
(c) Write a python program to drawn a polygon with vertices (0, 0), (1, 0), (2, 2), (1, 4) and find its
area and perimeter.
Max Z = x + y
subject to x ≤ 6
y≤6
x + y ≤ 11
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
import math
def reflection_x_axis(point):
return [point[0], -point[1]]
ii. 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 factors -2 and 1 respectively.
import turtle
33
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 18
Time: 3 Hours Max. Marks: 35
(a) 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 factors 3, 5 respectively.
(b) Write a Python program to plot the graph 2x2 − 4x + 5 in [–10, 10] in magenta colored dashed
pattern.
import matplotlib.pyplot as plt
import numpy as np
# Define the function
def f(x):
return 2*x**2 - 4*x + 5
# Generate x values from -10 to 10 with 0.1 spacing
x = np.arange(-10, 10, 0.1)
# Generate y values from the function
y = f(x)
# Create a figure and a set of axes
fig, ax = plt.subplots()
# Plot the function with a magenta colored dashed pattern
ax.plot(x, y, color='magenta', linestyle='--')
# Set the limits of the axes
ax.set_xlim([-10, 10])
ax.set_ylim([-5, 35])
# Show the plot
plt.show()
(c) Write a Python program to generate 3D plot of the functions z = x2 + y2 in −5 < x, y < 5.
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(a) Write a Python program to generate vector x in the interval [−22, 22] using numpy package with
80 subintervals.
import numpy as np
(b) Write a Python program to rotate the triangle ABC by 90 degree, where A[1, 2], B[2, −2]andC[−1, 2].
import numpy as np
import matplotlib.pyplot as plt
(c) Write a Python program to plot the rectangle with vertices at [2, 1], [2, 4], [5, 4], [5, 1], and its
uniform expansion by factor 4.
import matplotlib.pyplot as plt
Min Z = x + y
subject to x ≥ 6
y≥6
x + y ≤ 11
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
Max Z = 2x + 3y
subject to 5x − y ≥ 0
x+y≥6
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
import numpy as np
S = np.array([[1, 0],
[0, 4]])
F = np.array([[-1, 0],
[0, -1]])
T3 = S @ T1
T4 = S @ T2
T5 = F @ T3
T6 = F @ T4
ii. 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 degrees.
import numpy as np
R = np.array([[np.cos(np.pi/3), -np.sin(np.pi/3)],
[np.sin(np.pi/3), np.cos(np.pi/3)]])
Slip No. : 19
Time: 3 Hours Max. Marks: 35
(a) Write a python program to plot the graphs of sin(x), ex and x3 in [0, 5] in one figure with 2 × 2
subplots.
import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2, figsize=(10, 10))
x = np.linspace(0, 5, 100)
axs[0, 0].plot(x, np.sin(x))
axs[0, 0].set_title('sin(x)')
axs[0, 1].plot(x, np.exp(x))
axs[0, 1].set_title('e^x')
axs[1, 0].plot(x, x**3)
axs[1, 0].set_title('x^3')
axs[1, 1].plot(x, x**3, label='x^3')
axs[1, 1].plot(x, np.exp(x), label='e^x')
axs[1, 1].plot(x, np.sin(x), label='sin(x)')
axs[1, 1].set_title('x^3, e^x, sin(x)')
axs[1, 1].legend()
plt.show()
(b) Write a python program to plot 3D Surface Plot of the function z = cos(|x|+ |y|) in −1 < x, y < 1.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-1, 1, 20)
y = np.linspace(-1, 1, 20)
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')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('3D Surface Plot of z = cos(|x|+ |y|) in -1 < x, y < 1')
plt.show()
(c) 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 colors to the curve.
import numpy as np
import matplotlib.pyplot as plt
(a) Write a python program to rotate the ray by 90o in clockwise direction having starting point (0, 0)
and end point (4, 4).
import matplotlib.pyplot as plt
# starting point
x1, y1 = 0, 0
# end point
x2, y2 = 4, 4
# rotate the ray by 90 degrees in clockwise direction
x3 = y1
y3 = -x1
x4 = y2
y4 = -x2
# plot the original ray
plt.plot([x1, x2], [y1, y2], color='blue', linewidth=2)
# plot the rotated ray
plt.plot([x3, x4], [y3, y4], color='red', linewidth=2)
# add labels and title
plt.xlabel('X', fontsize=12)
plt.ylabel('Y', fontsize=12)
plt.title('Ray rotation by 90 degrees in clockwise direction', fontsize=14)
# show the plot
plt.show()
(b) Write a Python program to Reflect the triangle ABC through the line y=3, where A[1, 0], B[2, −1]
and C[−1, 3]
(c) Write a Python program to draw a polygon with vertices (0, 0), (1, 0), (2, 2), (1, 4). Also find area
and perimeter of the polygon.
Max Z = 3x + 5y + 4z
subject to 2x + 3y ≤ 8
2y + 5z ≤ 10
3x + 2y + 4z ≤ 15
x ≥ 0, y ≥ 0.
import pulp
Min Z = x + 2y + z
1 1
subject to x +
3
2y+ 2z≤1
2 x + 2y + z ≥ 8
x ≥ 0, y ≥ 0.
import pulp
36
(b) Attempt any ONE of the following. [8]
i. Write the Python program to apply each of the following transformation on the point P = [−2,
4].
I. Rotation about origin through an angle 48
degree II. Scaling in X-coordinate by factor 2
III. Reflection through the line y = 2x − 3
IV. Shearing in X direction by 7 units
import numpy as np
ii. Find combined transformation of the line segment between the points A[4, −1] and B[3, 0]
for the following sequence of transformations:
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.
import numpy as np
# Define the points A and B
A = np.array([4, -1])
B = np.array([3, 0])
A_rotated = rotate(A)
B_rotated = rotate(B)
A_reflected = reflect(A_scaled)
B_reflected = reflect(B_scaled)
Slip No. : 20
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot 2D graph of the function f(x) = sin x and g(x) = cos x in [−2π, 2π].
import numpy as np
import matplotlib.pyplot as plt
# Add legend
plt.legend()
(b) Write a Python program to plot the 2D graph of the function f(x) = ex sin x in [−5π, 5π] with blue
points line with upward pointing triangle.
import numpy as np
import matplotlib.pyplot as plt
# Define the x-axis range
x = np.linspace(-5*np.pi, 5*np.pi, 400)
# Calculate the y-values for f(x) = e^(sin x)
f_x = np.exp(np.sin(x))
# Create the plot
plt.plot(x, f_x, 'b^', markersize=5, label='f(x) = e^(sin x)')
# Add title and labels
plt.title('2D Graph of f(x) = e^(sin x)')
plt.xlabel('x')
plt.ylabel('y')
# Add legend
plt.legend()
# Show the plot
plt.show()
(c) Write a Python program to plot the 3D graph of the function f(x) = sin(x 2 + y2), −6 < x, y < 6.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(a) Write a python program to reflect the line segment joining the points A[−5, 2], B[3, −4] through
the line y = 2x − 1.
import numpy as np
# Calculate the point C where the line segment AB intersects the line y = 2x - 1
C = (A[1] - m*A[0] + m*b) / (m**2 + 1) * np.array([1, m]) + b * np.array([0, -1])
(b) Write a Python program to find the area and perimeter of a polygon with vertices (0, 0), (−2, 0), (5, 5), (1, −6).
import math
(c) Write a Python program to plot the 3D graph of the function f(x, y) = sin x+cos y, x, y ∈ [−2π, 2π]
using wireframe plot.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Max Z = x + y
subject to x − y ≥ 1
x+y≥2
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
Min Z = 3.5x + 2y
subject to x − y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0.
import pulp
# Add constraints
prob += x - y >= 5
prob += x >= 4
prob += y <= 2
38
(b) Attempt any ONE of the following. [8]
i. Apply the following transformations on the point P [3, −2]
I. Scaling in y direction by 4 units.
II. Reflection through y axis.
III. Rotation about origin by an angle 45o.
IV. Reflection through the line y = x.
import math
p = [3, -2]
p = scale_y(p[0], p[1], 4)
p = reflect_y(p[0], p[1])
p = rotate_origin(p[0], p[1], 45)
p = reflect_y_x(p[0], p[1])
p = [3, -2]
39
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 21
Time: 3 Hours Max. Marks: 35
(a) 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.
import numpy as np
import matplotlib.pyplot as plt
# Define the x range
x = np.linspace(0, 5, 100)
# Calculate the y values
y = x ** 4
# Create the plot
plt.plot(x, y, linestyle='--', color='red', marker='o', markersize=3, label='f(x) = x^4')
# Set the plot title, x and y labels
plt.title('2D Graph of f(x) = x^4 in [0, 5]')
plt.xlabel('x')
plt.ylabel('y')
# Enable grid
plt.grid(True)
# Enable legend
plt.legend()
# Show the plot
plt.show()
(b) Write a Python program to generate 3D plot of the function z = x 2 + y2 in −6 < x, y < 6.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
(c) Write a Python program to plot the 3D graph of the function f(x) = ex2+y2 for x, y ∈ [0, 2π] using
wireframe.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
(a) If the line segment joining the points A[2, 5] and B[4, − 13] is transformed to the line segment A B
′ ′
2 3
by the transformation matrix [T ] = 4 1 , then using python find the slope and midpoint of the
transformed line.
# transformation matrix
T = [[2, 3], [4, 1]]
(b) 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.
(c) 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.
# shearing factors
m_x = 2
m_y = -3
Min Z = 4x + 2y
subject to x + y ≤ 3
x−y≥2
x ≥ 0, y ≥ 0.
import numpy as np
# constraint coefficients
A = np.array([[1, 1], [1, -1], [1, 0], [0, 1]])
b = np.array([3, 2, 0, 0])
Max Z = 2x + 4y
subject to 2x + y ≤ 18
2x + 2y ≥ 30
x + 2y = 26
x ≥ 0, y ≥ 0.
import numpy as np
# constraint coefficients
A = np.array([[2, 1], [2, 2], [1, 2], [1, 0], [0, 1]])
b = np.array([18, 30, 26, 0, 0])
40
(b) Attempt any ONE of the following. [8]
i. Apply the following transformations on the point P [−2, 4].
I. Reflection through line 3x + 4y = 5.
II. Scaling in X coordinate by factor 6.
III. Scaling in Y coordinate by factor 4.1.
IV. Reflection through line y = 2x + 3.
# Initial point P
x, y = -2, 4
# Initial point P
x, y = -2, 4
Slip No. : 22
Time: 3 Hours Max. Marks: 35
(a) Write a python program to draw 2D plot y = log(x2) + sin(x) with suitable label in the x axis , y
axis and a title in [−5π, 5π].
import numpy as np
import matplotlib.pyplot as plt
(b) Write a python program to Plot 3D dimensional Contour plot of parabola z = x 2 + y2, −6 < x, y <
6.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the range of x and y values
x = np.linspace(-6, 6, 100)
y = np.linspace(-6, 6, 100)
# Create a meshgrid of x and y values
X, Y = np.meshgrid(x, y)
# Calculate the z values for each x and y value
Z = X**2 + Y**2
# Create the 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the contour plot
ax.contour(X, Y, Z)
# Set the labels for the x, y, and z axes
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
# Set the title of the plot
ax.set_title('3D Contour Plot of Parabola z = x^2 + y^2, -6 < x, y < 6')
# Display the plot
plt.show()
(c) Write a python program to draw 2D plot y = x sin( x12 ) in [−5, 5] with suitable label in the x axis,
y axis, a title and location of legend to lower right corner.
import numpy as np
import matplotlib.pyplot as plt
# Define the range of x values
x = np.linspace(-5, 5, 1000)
# Calculate the y values for each x value
y = x * np.sin(x**(1/2))
# Create the plot
plt.plot(x, y)
# Set the labels for the x and y axes
plt.xlabel('x-axis')
plt.ylabel('y-axis')
# Set the title of the plot
plt.title('Plot of y = x sin(x^(1/2)) in the range of [-5, 5]')
# Add a legend in the lower right corner
plt.legend(loc='lower right')
# Display the plot
plt.show()
(a) Write a python program to find the angle at each vertices of the triangle ABC where A[0, 0], B[2,
2] and C[0, 2].
import math
point_p = [3, 6]
print("Reflection of point P", point_p, "through the line x - 2y + 4 = 0 is", reflect_point(point_p,
None))
(c) Write a Python program to find area and perimeter of the triangle ABC where A[0, 0], B[5, 0], C[3, 3].
# No original code provided, so I'll write a basic implementation
def calculate_distance(point1, point2):
return ((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2) ** 0.5
def calculate_perimeter(point1, point2, point3):
side1 = calculate_distance(point1, point2)
side2 = calculate_distance(point2, point3)
side3 = calculate_distance(point3, point1)
return side1 + side2 + side3
def calculate_area(point1, point2, point3):
side1 = calculate_distance(point1, point2)
side2 = calculate_distance(point2, point3)
side3 = calculate_distance(point3, point1)
# Calculate semi-perimeter
s = (side1 + side2 + side3) / 2
# Calculate area using Heron's formula
return (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5
point_a = [0, 0]
point_b = [5, 0]
point_c = [3, 3]
perimeter = calculate_perimeter(point_a, point_b, point_c)
area = calculate_area(point_a, point_b, point_c)
print("Perimeter of triangle ABC:", perimeter)
print("Area of triangle ABC:", area)
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, z, w ≥ 0.
import pulp
Min Z = x + y
subject to x + y ≤ 11
x≥6
y≥6
x ≥ 0, y ≥ 0.
import pulp
42
(b) Attempt any ONE of the following. [8]
i. Write the Python program for each of the following.
I. Rotate the point (1, 1) about (1, 4) through angle π/2.
import numpy as np
IV. Represent two dimensional points using point function (−2, 5).
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"({self.x}, {self.y})"
point = Point(-2, 5)
print("Point:", point)
ii. A Company has 3 production facilities S1, S2 and S3 with production capacity of 7, 9 and
18 units (in 100’s) per week of a product, respectively. These units are to be shipped to 4
warehouses D1, D2, D3 and D4 with requirement of 5, 6, 7 and 14 units (in 100’s) per week,
respectively. The transportation costs (in rupees) per unit between factories to warehouses
are given in the table below.
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
Write a Python programme to solve transportation problem for minimize the costs of whole
operation.
import pulp
demand = {
'D1': 5,
'D2': 8,
'D3': 7,
'D4': 14,
}
cost_matrix = {
('S1', 'D1'): 19, ('S1', 'D2'): 30, ('S1', 'D3'): 50, ('S1', 'D4'): 10,
('S2', 'D1'): 70, ('S2', 'D2'): 30, ('S2', 'D3'): 40, ('S2', 'D4'): 60,
('S3', 'D1'): 40, ('S3', 'D2'): 8, ('S3', 'D3'): 70, ('S3', 'D4'): 20,
}
43
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 23
Time: 3 Hours Max. Marks: 35
(a) Write a python program plot the graphs of sin x,and cos x in [0, π] in one figure with 2×1 subplots.
import numpy as np
import matplotlib.pyplot as plt
# Plot sin(x)
ax[0].plot(x, np.sin(x))
ax[0].set_title('sin(x)')
ax[0].grid(True)
# Plot cos(x)
ax[1].plot(x, np.cos(x))
ax[1].set_title('cos(x)')
ax[1].grid(True)
(b) Write a python program to Plot the graph of the following functions in the given interval.
i. f(x) = x3 in [0, 5].
ii. f(x) = x2 in [−2, 2].
import numpy as np
import matplotlib.pyplot as plt
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graphs of f(x) = x^3 and f(x) = x^2')
plt.legend()
plt.show()
(c) Write a python program to plot 3D Surface Plot of the function z = cos(|x|+ |y|) in −1 < x, y < 1.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(a) Write a Python program to draw regular polygon with 20 sides and radius 1 centered at (0, 0).
(c) Write a Python program to find area and perimeter of triangle ABC where A[0, 1], B[−5, 0] and
C[−3, 3].
import math
Max Z = 3x + 5y + 4z
subject to 2x + 3y ≤ 8
2x + 5y ≤ 10
3x + 2y + 4z ≤ 15
x, y, z ≥ 0.
import numpy as np
from scipy.optimize import linprog
Min Z = 3x + 5y + 4z
subject to 2x + 2y ≤ 12
2x + 2y ≤ 10
5x + 2y ≤ 10
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
44
(b) Attempt any ONE of the following. [8]
i. Write the Python program to apply each of the following transformation on the point P = [3,
−1].
I. Reflection through X axis.
P = [3, -1]
P_reflected = [P[0], -P[1]]
print(P_reflected) # Output: [3, 1]
import math
P = [3, -1]
angle = math.radians(30) # Convert degree to radians
rotation_matrix = [
[math.cos(angle), -math.sin(angle)],
[math.sin(angle), math.cos(angle)]
]
P_rotated = [
sum(x * y for x, y in zip(rotation_matrix[i], P))
for i in range(2)
]
print(P_rotated) # Output: [2.598076211353316, -2.3431457059272746]
P = [3, -1]
scale_factor = 8
P_scaled = [P[0], P[1] * scale_factor]
print(P_scaled) # Output: [3, -8]
P = [3, -1]
shear_factor = 2
shearing_matrix = [
[1, shear_factor],
[0, 1]
]
P_sheared = [
sum(x * y for x, y in zip(shearing_matrix[i], P))
for i in range(2)
]
print(P_sheared) # Output: [5, -1]
ii. Write a Python program to apply each of the following transformations on the point P [−2, 4].
I. Reflection through the line y = x + 2.
P = [-2, 4]
m=1
x0 = -2
P_reflected = [P[0], P[1] - 2 * m * (P[0] - x0)]
print(P_reflected) # Output: [-2, 6]
P = [-2, 4]
scale_factor = 2
P_scaled = [P[0], P[1] * scale_factor]
print(P_scaled) # Output: [-2, 8]
P = [-2, 4]
shear_factor = 4
P_sheared = [P[0] + shear_factor * P[1], P[1]]
print(P_sheared) # Output: [18, 4]
import math
P = [-2, 4]
angle = math.radians(60) # Convert degree to radians
rotation_matrix = [
[math.cos(angle), -math.sin(angle)],
[math.sin(angle), math.cos(angle)]
]
P_rotated = [
sum(x * y for x, y in zip(rotation_matrix[i], P))
for i in range(2)
]
print(P_rotated) # Output: [-1.7320508075688772, 3.4641016151377544]
45
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 24
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to plot 3D graph of the function f(x) = e−x2 in [−5, 5] with green dashed
points line with upward pointing triangle.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
import numpy as np
import matplotlib.pyplot as plt
def f(x):
if x < 5:
return x**2 + 4
else:
return 3*x + 9
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x)')
plt.grid(True)
plt.show()
(c) Write a Python program to plot graph of the function f(x) = log(3x 2), in [1, 10] with black dashed
points.
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.log(3*x**2)
x = np.linspace(1, 10, 400)
y = f(x)
plt.plot(x, y, 'k--')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = log(3x2)')
plt.grid(True)
plt.show()
Q.2 Attempt any TWO of the following. [10]
(a) Write a python program to plot triangle with vertices [3, 3], [5, 6], [5, 2], and its rotation about the
origin by angle −π radians.
(b) Write a Python program to generate vector x in the interval [−22, 22] using numpy package with
80 subintervals.
import numpy as np
(c) Write a Python program to draw a polygon with vertices (0, 0), (1, 0), (2, 2), (1, 4). Also find area
and perimeter of the polygon.
Min Z = 3.5x + 2y
subject to x + y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
Min Z = x + y
subject to x ≥ 6
y≥6
x + y ≤ 11
x ≥ 0, y ≥ 0.
import numpy as np
from scipy.optimize import linprog
# Define the objective function
c = np.array([1, 1])
# Define the constraints
A_ub = np.array([[1, 1], [1, 0], [0, 1]])
b_ub = np.array([11, 6, 6])
A_eq = np.array([[1, 1]])
b_eq = np.array([6])
46
(b) Attempt any ONE of the following. [8]
i. Apply Python program in each of the following transformations on the point 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. Reflection through the line y = x.
class Point:
""" Point class for representing and manipulating x,y coordinates. """
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def __str__(self):
return str(self.x)+","+str(self.y)
# method reflect_x to Point which returns a new Point, one which is the reflection of the
point about the x-axis
def reflect_x(self):
x1 = self.x
y1 = (-1)*self.y
return Point(x1, y1)
# method scale_x to Point which returns a new Point, one which is the scaling of the
point in x-coordinate by factor 2
def scale_x(self, factor):
x1 = self.x * factor
y1 = self.y
return Point(x1, y1)
# method scale_y to Point which returns a new Point, one which is the scaling of the
point in y-coordinate by factor 1.5
def scale_y(self, factor):
x1 = self.x
y1 = self.y * factor
return Point(x1, y1)
# method reflect_yx to Point which returns a new Point, one which is the reflection of the
point through the line y = x
def reflect_yx(self):
x1 = self.y
y1 = self.x
return Point(x1, y1)
# create a Point object with coordinates [3, -1]
P = Point(3, -1)
# apply the transformations
P_reflected_x = P.reflect_x()
P_scaled_x = P.scale_x(2)
P_scaled_y = P.scale_y(1.5)
P_reflected_yx = P.reflect_yx()
# print the results
print("I. Reflection through X-axis: ", P_reflected_x)
print("II. Scaling in X-coordinate by factor 2: ", P_scaled_x)
print("III. Scaling in Y-coordinate by factor 1.5: ", P_scaled_y)
print("IV. Reflection through the line y = x: ", P_reflected_yx)
ii. 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.
import math
class Point:
""" Point class for representing and manipulating x,y coordinates. """
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def __str__(self):
return str(self.x)+","+str(self.y)
# method rotate to Point which returns a new Point, one which is the rotation of the point
about the origin through an angle pi
def rotate(self):
x1 = self.x * math.cos(math.pi) - self.y * math.sin(math.pi)
y1 = self.x * math.sin(math.pi) + self.y * math.cos(math.pi)
return Point(x1, y1)
# method shear_y to Point which returns a new Point, one which is the shearing of the
point in y-direction by 4.5 units
def shear_y(self):
x1 = self.x
y1 = self.y + 4.5 * self.x
return Point(x1, y1)
# method scale_x to Point which returns a new Point, one which is the scaling of the
point in x-coordinate by factor 3
def scale_x(self, factor):
x1 = self.x * factor
y1 = self.y
return Point(x1, y1)
# method reflect_yx to Point which returns a new Point, one which is the reflection of the
point through the line y = x
def reflect_yx(self):
x1 = self.y
y1 = self.x
return Point(x1, y1)
# create Point objects for A and B
A = Point(4, -1)
B = Point(3, 0)
# apply the transformations
A_rotated = A.rotate()
B_rotated = B.rotate()
A_sheared = A_rotated.shear_y()
B_sheared = B_rotated.shear_y()
A_scaled = A_sheared.scale_x(3)
B_scaled = B_sheared.scale_x(3)
A_reflected = A_scaled.reflect_yx()
B_reflected = B_scaled.reflect_yx()
# print the results
print("A after I. Rotation about origin through an angle pi: ", A_rotated)
print("B after I. Rotation about origin through an angle pi: ", B_rotated)
print("A after II. Shearing in Y direction by 4.5 units: ", A_sheared)
print("B after II. Shearing in Y direction by 4.5 units: ", B_sheared)
print("A after III. Scaling in X-coordinate by factor 3: ", A_scaled)
print("B after III. Scaling in X-coordinate by factor 3: ", B_scaled)
print("A after IV. Reflection through the line y = x: ", A_reflected)
print("B after IV. Reflection through the line y = x: ", B_reflected)
47
SAVITRIBAI PHULE PUNE UNIVERSITY, PUNE Board of Studies in Mathematics S.Y.B.Sc (Computer
Science) Practical Examination in Mathematics MTC-243: Python Programming Language-II (CBCS 2019
Pattern)(Semester-IV)
Slip No. : 25
Time: 3 Hours Max. Marks: 35
(a) Write a Python program to generate 3D plot of the functions z = sin x + cos y in −10 < x, y < 10.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
(b) Using Python plot the graph of function f(x) = sin−1(x) on the interval [−1, 1].
import numpy as np
import matplotlib.pyplot as plt
# create a plot
plt.plot(x, y)
(c) Using Python plot the surface plot of function z = cos x 2 + y2 − 0.5 in the interval from −1 <
x, y < 1.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# create a grid of x and y values
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(x, y)
# calculate the z values
Z = np.cos(X**2) + Y**2 - 0.5
# create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# plot the surface
ax.plot_surface(X, Y, Z, cmap='viridis')
# set the limits of the plot
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
# show the plot
plt.show()
(a) Rotate the line segment by 180◦ having end points (1, 0) and (2, −1).
:param point1: the first point of the line segment (x1, y1)
:param point2: the second point of the line segment (x2, y2)
:return: the rotated line segment as a list of two points
"""
# Rotate the points
rotated_point1 = (-point1[0], -point1[1])
rotated_point2 = (-point2[0], -point2[1])
import sympy as sp
(c) Generate triangle with vertices (0, 0), (4, 0), (1, 4), check whether the triangle is Scalene triangle.
import sympy as sp
Max Z = 4x + y + 3z + 5w
subject to 4x + 6y − 5z − 4w ≥ 20
−3x − 2y + 4z + w ≤ 10
−8x − 3y + 3z + 2w ≤ 20
x ≥ 0, y ≥ 0, z ≥ 0, w ≥ 0.
import numpy as np
from scipy.optimize import linprog
# Define the objective function
c = np.array([4, 1, 3, 5])
# Define the constraints
A_eq = np.array([[4, 6, -5, -4],
[-3, -2, 4, 1],
[-8, -3, 3, 2]])
b_eq = np.array([20, 10, 20])
# Define the bounds
bounds = [(0, None), (0, None), (0, None), (0, None)]
# Define the options
options = {'disp': True}
# Solve the LPP
res = linprog(c, A_eq=A_eq, b_eq=b_eq, bounds=bounds, options=options)
# Print the solution
print("Optimal solution found:")
print("x =", res.x[0])
print("y =", res.x[1])
print("z =", res.x[2])
print("w =", res.x[3])
print("Maximum value of Z =", res.fun)
48
(b) Attempt any ONE of the following. [8]
i. Write a python program to apply the following transformations on the point (−2, 4) :
I. Refection through X−axis.
II. Scaling in X−coordinate by factor 6.
III. Shearing in X direction by 4 units.
IV. Rotate about origin through an angle 30◦.
import math
ii. Write a python program to find the combined transformation of 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.
import math
import numpy as np
49