0% found this document useful (0 votes)
54 views10 pages

Slip NO - 1

This document contains solutions to 9 questions involving plotting graphs and solving optimization problems in Python. 2D and 3D graphs are plotted for various functions. Bar graphs and scatter plots are used to represent data. Linear programming problems are solved using scipy and pulp modules. Geometric transformations like reflection and scaling are applied to polygons.

Uploaded by

Komal Rathod
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)
54 views10 pages

Slip NO - 1

This document contains solutions to 9 questions involving plotting graphs and solving optimization problems in Python. 2D and 3D graphs are plotted for various functions. Bar graphs and scatter plots are used to represent data. Linear programming problems are solved using scipy and pulp modules. Geometric transformations like reflection and scaling are applied to polygons.

Uploaded by

Komal Rathod
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/ 10

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

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

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 100)
y1 = x**2
y2 = x**3

plt.plot(x, y1, label='f(x) = x^2')


plt.plot(x, y2, label='g(x) = x^3')
plt.xlabel('x')
plt.ylabel('y')
plt.title('2D Graph of f(x)=x^2 and g(x)=x^3')
plt.legend()
plt.grid(True)
plt.show()

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

In [2]: #Q2) Write a Python program to plot 3D graph of the function f(x) = e−x2 i
# points line with upward pointing triangle.

import numpy as np
import matplotlib.pyplot as plt

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

x = np.linspace(-5, 5, 100)
y = np.exp(-x) ** 2
ax.plot(x, y, zs=0, zdir='z', color='g', linestyle='--', marker='^')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('f(x)')

plt.show()

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

In [3]: #Q3 using python represent the following information using a bar graph (in
# Item Clothing food Rent Petrol Misc.
# Expenditure in Rs 600 4000 2000 1500 700

import matplotlib.pyplot as plt

items = ['Clothing', 'Food', 'Rent', 'Petrol', 'Misc.']


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

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


plt.xlabel('Item')
plt.ylabel('Expenditure in Rs')
plt.title('Expenditure on Different Items')
plt.show()

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

In [4]: #Q4)write a python, program to reflect the line segment joining the point A

import numpy as np

# Define the points A and B


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

# Define the line y=x+1


def reflect_point(point):
x, y = point
new_x = (2 * 1 * y - x + 1) / 2
new_y = (2 * 1 * x - y - 1) / 2
return np.array([new_x, new_y])

# Reflect points A and B through the line y=x+1


reflected_A = reflect_point(A)
reflected_B = reflect_point(B)

print("Reflected Point A:", reflected_A)


print("Reflected Point B:", reflected_B)

Reflected Point A: [1. 3.]


Reflected Point B: [ 4. -1.5]

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

In [5]: #Q5)Write a python program to draw a polygon with vertices(0,0),(2,0),(2,3)

import matplotlib.pyplot as plt


import numpy as np

# Define the vertices of the polygon


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

# Plot the original polygon


plt.figure()
plt.gca().add_patch(plt.Polygon(vertices, closed=True, fill=None, edgecolor
plt.axis('scaled')
plt.title('Original Polygon')

# Rotate the polygon by 180 degrees


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

# Plot the rotated polygon


plt.figure()
plt.gca().add_patch(plt.Polygon(rotated_vertices, closed=True, fill=None
plt.axis('scaled')
plt.title('Rotated Polygon by 180 Degrees')

plt.show()

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

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

In [6]: #Q6)write a python program to find the area and perimeter of Triangle ABC w

import math

# Coordinates of points A, B, and C


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

# Distance between two points


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

# Calculate the sides of the triangle


side_a = distance(B, C)
side_b = distance(A, C)
side_c = distance(A, B)

# Calculate the perimeter of the triangle


perimeter = side_a + side_b + side_c

# Calculate the semi-perimeter


s = perimeter / 2

# Calculate the area of the triangle using Heron's formula


area = math.sqrt(s * (s - side_a) * (s - side_b) * (s - side_c))

print("Perimeter of the triangle ABC:", perimeter)


print("Area of the triangle ABC:", area)

Perimeter of the triangle ABC: 12.848191962583273


Area of the triangle ABC: 7.499999999999997

In [7]: #Q7) write a python program to solve the following LPP.


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

from scipy.optimize import linprog

c = [-150, -75] # Coefficients of the objective function to be minimized

A = [[4, 6], [5, 4]] # Coefficients of the left-hand side of inequalities


b = [24, 15] # Right-hand side of inequalities

x_bounds = (0, None) # Bounds for x variable


y_bounds = (0, None) # Bounds for y variable

res = linprog(c, A_ub=A, b_ub=b, bounds=[x_bounds, y_bounds], method=

print('Optimal values:', res.x)


print('Optimal solution:', res.fun)

Optimal values: [3. 0.]


Optimal solution: -450.0

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

In [7]: #Q8)write a python program to display the following LPP by using pulp modul
# Min Z = x+y
# subject to
# x>= =6
# y>= =6
# x+y<=11
# x>=0, y>=0

import pulp

# Create a problem instance


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

# Define the decision variables


x = pulp.LpVariable("x", lowBound=0)
y = pulp.LpVariable("y", lowBound=0)

# Define the objective function


prob += x + y, "Minimize Z"

# Add the constraints


prob += x >= 6, "Constraint 1"
prob += y >= 6, "Constraint 2"
prob += x + y <= 11, "Constraint 3"

# Solve the problem


prob.solve()

# Display the solution


print("Status:", pulp.LpStatus[prob.status])
print("Optimal solution: Z =", pulp.value(prob.objective))
print("x =", pulp.value(x))
print("y =", pulp.value(y))

Status: Infeasible
Optimal solution: Z = 12.0
x = 6.0
y = 6.0

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

In [6]: #Q9) Apply Python program in each of the following transformations on the
#(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:
def __init__(self, x, y):
self.x = x
self.y = y

def reflect_x_axis(point):
return Point(point.x, -point.y)

def scale_x_coordinate(point, factor):


return Point(point.x * factor, point.y)

def scale_y_coordinate(point, factor):


return Point(point.x, point.y * factor)

def reflect_through_line(point):
return Point(point.y, point.x)

# Initial point P[3, -1]


P = Point(3, -1)

# Transformation (I): Reflection through X-axis


P_reflected_x_axis = reflect_x_axis(P)
print("Reflection through X-axis:", P_reflected_x_axis.x, P_reflected_x_axi

# Transformation (II): Scaling in X-coordinate by factor 2


P_scaled_x = scale_x_coordinate(P, 2)
print("Scaling in X-coordinate by factor 2:", P_scaled_x.x, P_scaled_x

# Transformation (III): Scaling in Y-coordinate by factor 1.5


P_scaled_y = scale_y_coordinate(P, 1.5)
print("Scaling in Y-coordinate by factor 1.5:", P_scaled_y.x, P_scaled_y

# Transformation (IV): Reflection through the line y = x


P_reflected_line = reflect_through_line(P)
print("Reflection through the line y = x:", P_reflected_line.x, P_reflected

Reflection through X-axis: 3 1


Scaling in X-coordinate by factor 2: 6 -1
Scaling in Y-coordinate by factor 1.5: 3 -1.5
Reflection through the line y = x: -1 3

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

In [7]: #Q10) Find the combined transformation of the line segment between the poi
# 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

def rotate_about_origin(point, angle):


rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
return np.dot(rotation_matrix, point)

def scale_x_coordinate(point, scale_factor):


return np.array([point[0] * scale_factor, point[1]])

def reflect_through_line(point):
return np.array([-point[1], -point[0]])

def shear_x_direction(point, shear_factor):


return np.array([point[0] + shear_factor * point[1], point[1]])

# Initial points A and B


point_A = np.array([5, -2])
point_B = np.array([4, 3])

# Applying transformations sequentially


rotated_point_A = rotate_about_origin(point_A, np.pi)
rotated_point_B = rotate_about_origin(point_B, np.pi)

scaled_point_A = scale_x_coordinate(rotated_point_A, 2)
scaled_point_B = scale_x_coordinate(rotated_point_B, 2)

reflected_point_A = reflect_through_line(scaled_point_A)
reflected_point_B = reflect_through_line(scaled_point_B)

sheared_point_A = shear_x_direction(reflected_point_A, 4)
sheared_point_B = shear_x_direction(reflected_point_B, 4)

print("Transformed Point A:", sheared_point_A)


print("Transformed Point B:", sheared_point_B)

Transformed Point A: [38. 10.]


Transformed Point B: [35. 8.]

In [ ]:

10 of 10 26/03/24, 11:33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy