0% found this document useful (0 votes)
13 views11 pages

Practical No 4

The document contains a Jupyter Notebook with various Python programming exercises focused on plotting functions, surface plots, transformations, and solving linear programming problems using libraries like NumPy, Matplotlib, and PuLP. Each exercise includes code snippets for visualizing mathematical concepts and performing calculations such as plotting graphs of functions, transforming geometric shapes, and optimizing linear programming problems. The notebook serves as a practical guide for applying Python in mathematical and graphical contexts.

Uploaded by

magicworld189
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Practical No 4

The document contains a Jupyter Notebook with various Python programming exercises focused on plotting functions, surface plots, transformations, and solving linear programming problems using libraries like NumPy, Matplotlib, and PuLP. Each exercise includes code snippets for visualizing mathematical concepts and performing calculations such as plotting graphs of functions, transforming geometric shapes, and optimizing linear programming problems. The notebook serves as a practical guide for applying Python in mathematical and graphical contexts.

Uploaded by

magicworld189
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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

In [2]: #Q1) Write a Python program to plot graph of the functions f(x) = log10(x)

import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.log10(x)
x = np.linspace(0.1, 10, 100)
y = f(x)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = log10(x)')
plt.show()

1 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [3]: #Q2)Write a Python program to plot graph of the functions f(x) = sin^-1 (x)

import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.arcsin(x)
x = np.linspace(-1, 1, 100)
y = f(x)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = sin⁻¹(x)')
plt.show()

2 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [5]: #Q3) Using Python plot the surface plot of parabola z = x**2 + y**2 in -6 <

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def f(x, y):
return x**2 + y**2
x = np.linspace(-6, 6, 100)
y = np.linspace(-6, 6, 100)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('Surface plot of z = x^2 + y^2')
plt.show()

3 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [17]: #Q4) If the line with points A[3, 1], B[5, -1] is transformed by the transf
matrix [T] = 3 −2
2 1 then using python, find the equation of transformed

import numpy as np

# Define the transformation matrix


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

# Define the points A and B


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

# Define the line equation in matrix form


line_eq = np.array([B - A])

# Apply the transformation to the line equation


transformed_line_eq = np.dot(line_eq, T)

print("Equation of the transformed line:")


print(transformed_line_eq)

Equation of the transformed line:


[[ 2 -6]]

4 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [6]: #Q5)Write a Python program to draw a polygon with vertices (0,0), (2,0), (2
and (1,6) and rotate by 1800

import matplotlib.pyplot as plt


import numpy as np
vertices = np.array([(0,0), (2,0), (2,3), (1,6)])
plt.plot(vertices[:,0], vertices[:,1], color='blue')
theta = np.pi
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
rotated_vertices = vertices.dot(rotation_matrix)
plt.plot(rotated_vertices[:,0], rotated_vertices[:,1], color='red')
plt.xlim([-2, 7])
plt.ylim([-2, 7])
plt.xlabel('x')
plt.ylabel('y')
plt.title('Polygon rotated by 180 degrees')
plt.show()

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

In [7]: #Q6)Using python, generate line passing through points (2,3) and (4,3) and

import matplotlib.pyplot as plt


x1, y1 = 2, 3
x2, y2 = 4, 3
slope = (y2 - y1) / (x2 - x1)
y_intercept = y1 - slope * x1
x = [0, 5]
y = [slope * xi + y_intercept for xi in x]
plt.plot(x, y, label=f"y = {slope:.2f}x + {y_intercept:.2f}")
plt.scatter([x1, x2], [y1, y2], color='red')
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.show()

6 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [8]: #Q7) write a Python program to solve the following LPP


Max Z = 150x + 75y
Subjected to
4x + 6y <= 24
5x + 3y <= 15
x > 0 , y > 0

from pulp import *


problem = LpProblem("LP Problem", LpMaximize)
x = LpVariable('x', lowBound=0, cat='Continuous')
y = LpVariable('y', lowBound=0, cat='Continuous')
problem += 150 * x + 75 * y
problem += 4 * x + 6 * y <= 24
problem += 5 * x + 3 * y <= 15
status = problem.solve()
print(f"Status: {LpStatus[status]}")
print(f"x = {value(x):.2f}")
print(f"y = {value(y):.2f}")
print(f"Z = {value(problem.objective):.2f}")

C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\site-packag
es\pulp\pulp.py:1316: UserWarning: Spaces are not permitted in the
name. Converted to '_'
warnings.warn("Spaces are not permitted in the name. Converted to
'_'")

Status: Optimal
x = 3.00
y = 0.00
Z = 450.00

7 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [14]: #Q8) Write a python program to display the following LPP by using pulp
module and simplex method. Find its optimal solution if exist.
Min Z = 4x+y+3z+5w
subject to
4x+-6y-4w >= -20 -8x-3y+3z+2w <= 20
x + y <= 11
x >= 0,y>= 0,z>= 0,w>= 0

from pulp import LpMaximize, LpProblem, LpVariable

# Create the LP problem


lp_problem = LpProblem(name="LP_Problem", sense=LpMaximize)

# Define the decision variables


x = LpVariable(name="x", lowBound=0)
y = LpVariable(name="y", lowBound=0)
z = LpVariable(name="z", lowBound=0)
w = LpVariable(name="w", lowBound=0)

# Add the objective function


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

# Add the constraints


lp_problem += 4*x + 6*y - 5*z + 2*w <= -20
lp_problem += -8*x - 3*y + 3*z + 2*w <= 20
lp_problem += x + y <= 11

# Solve the problem


lp_problem.solve()

# Print the optimal solution


print("Optimal Solution:")
for var in lp_problem.variables():
print(f"{var.name} = {var.varValue}")
print(f"Optimal Value: {lp_problem.objective.value()}")

Optimal Solution:
w = 21.75
x = 11.0
y = 0.0
z = 21.5
Optimal Value: 217.25

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

In [15]: #Q9) Plot 3D axes with labels X - axis and z –axis and also plot following
with given coordinate in one graph
(I) (70, -25, 15) as a diamond in black color
(II) (50, 72, -45) as a* in green color,
(III) (58, -82, 65) as a dot in green color,
(IV) (20, 72, -45) as a * in Red color.

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_zlabel('Z-axis')
x1, y1, z1 = 70, -25, 15
x2, y2, z2 = 50, 72, -45
x3, y3, z3 = 58, -82, 65
x4, y4, z4 = 20, 72, -45
ax.scatter(x1, y1, z1, marker='D', c='black')
ax.scatter(x2, y2, z2, marker='*', c='green')
ax.scatter(x3, y3, z3, marker='o', c='green')
ax.scatter(x4, y4, z4, marker='*', c='red')
plt.show()

9 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [16]: #Q10)Q.10) Find the combined transformation of the line segment between the
A[4, -1] & B[3, 0] by using Python program for the following sequence
transformation:-
(I) Shearing in X – Direction by 9 unit
(II)
(III)

Rotation about origin through an angle pi.


Scaling in X-Coordinate by 2 units.
(IV) Reflection trough he line y = x

import numpy as np
import matplotlib.pyplot as plt
A = np.array([4, -1])
B = np.array([3, 0])
T1 = np.array([[1/9, 0], [0, 1]])
T2 = np.array([[-1, 0], [0, -1]])
T3 = np.array([[2, 0], [0, 1]])
T4 = np.array([[0, 1], [1, 0]])
AB = B - A
AB_T1 = T1 @ AB
AB_T2 = T2 @ AB_T1
AB_T3 = T3 @ AB_T2
AB_T4 = T4 @ AB_T3
A_T = A + AB_T4
B_T = B + AB_T4
plt.plot([A[0], B[0]], [A[1], B[1]], 'b', label='Original line segment'
plt.plot([A_T[0], B_T[0]], [A_T[1], B_T[1]], 'r', label='Transformed line s
plt.xlim(-10, 10)
plt.ylim(-10, 10)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()

10 of 11 26/03/24, 11:41
PRAJAPATI SANJAY PRACTICAL NO_4 - Jupyter Notebook http://localhost:8888/notebooks/PRAJAPATI%20SAN...

In [ ]:

11 of 11 26/03/24, 11:41

You might also like