0 ratings 0% found this document useful (0 votes) 83 views 48 pages Python Practices
The document is a practical certificate for students at Padmashree Manibhai Desai College, certifying the completion of Mathematics practicals during the academic year 2023-2024. It includes various Python scripts for creating bar graphs, pie charts, and 2D plots using matplotlib, covering topics like expenditures, student marks, air quality indices, and mathematical functions. Each practical demonstrates the use of Python for data visualization and analysis.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Carousel Previous Carousel Next
Save python practices For Later Mahatma Gandhi Sarvodaya Sangh’s
PADMASHREE MANIBHAI DESAI COLLEGE
Uruli Kanchan, Pune - 412202
‘T OF ™,
en They
re Dy
& &
[PYTHON PRACTICAL]
CERTIFICATE
This is to certify that
Mr/ Miss...
Exam Seat No. of SYBCS class has completed the course of
practical in Mathematics satisfactorily during the year 2023-2024
All practicals are assessed and found to be correct.
Date: /03/2024
Head Head
Department of Department of
Mathematics Computer Science Principal
Internal Examiner External ExaminerINDEX
Sr.No.
Practical Name
Date
SignIn [2]:
In [4]:
PRACTICAL:-01
#practical.191.py
# Using python, represent the following information using a bar graph(in gre
# item clothing food rent petrol misc
#exp. in. 15/600 4000 2000 1500 700
from matplotlib import pyplot as plt
x=['clothing', "food", ‘rent’, ‘petrol* , ‘misc']
y=1600, 4002, 2000, 1500, 700]
plt.bar(x,y,color='g")
plt.xlabel("iten")
plt.ylabel("expendicturemin Rs ")
plt.title(" BAR GRAPH")
plt.show()
BAR GRAPH
g
ges
spendin s
88
3000
#practical.1q2.py
# Using python, represent the following information using a bar graph(in gre
#subject math science english marathi hindi
#no.of.sub 68 90 70 85 91
from matplotlib import pyplot as plt
x=['math', ‘science’, ‘english’, ‘marathi', ‘hindi']
y=[68, 90, 70,85, 91]
plt.bar(x,y,color="g")
plt.ylabel("Number of Subject")
plt.xlabel("subject")
plt.title(" BAR GRAPH FOR MARKS")
plt.show()In [11]:
BAR GRAPH FOR MARKS:
Number of Subject
& 8 B
8
science english marathi hindi
subject
#practical1q3.py
Following is the information of students participating in varoious games 1
#represet it by a bar graph with bar width 0.7 inches
games cricket football hockey chess tennis
dpercent 65 30 541820
from matplotlib import pyplot as plt
‘cricket’, 'football', *hockey', ‘chess’, 'tennis']
y=165,30, 54, 10,20]
pit .bar(x,y,width=0.7)
plt.xlabel ("games")
plt.ylabel("passing in percentage")
plt.title("BAR GRAPH FOR GAMES")
plt..show()
BAR GRAPH FOR GAMES
8 8 8 8 B
7
i
e
i
5In [15]:
In [18]
#practicalig4.py
#0raw the horizontal the graph for the following data in MAROON colour
# city pune mumbai nashik nagpur thane
# Air Quality index 168 190 170 178 195
from matplotlib import pyplot as plt
‘pune’, ‘mumbai , ‘nashik' , ‘nagpur’ , ‘thane']
168, 190,17, 178, 195]
plt.barh(x,y, color='maroon")
plt.xlabel( city")
plt.ylabel("Air Quality index")
plt.title( "HORIZONTAL BAR GRAPH ")
plt.show()
HORIZONTAL BAR GRAPH
thane
sir ott index
g
#practicallq5.py
#Draw the pie chart for the following data
# grain wheat bajra rice maize jawar others
#% of total production 23 25 22 15 11 4
from matplotlib import pyplot as plt
‘wheat ',bajra', ‘rice’, 'maize','jawar', ‘others')
23,25,22 ,15,11,4]
plt.pie(y, labels=x)
plt.title(" PIE CHART ")
plt.show()
PIE CHART
bajra
wheat
ethers
tee ewerIn [38]:
In [37]:
a#practicallq6. py
write a pypthon to plot 20 x-axis and y-axis in black colour. in the same
#a) Green triangle with vertices[5,4], [7,4], [6,6]
#b)blue rectangle with vertices[2,2], [10,2], [10,8], [2,8]
#c) red polygon with vertices[6,2], [10,4], [8,7], [4,8], [2,4]
#d) isosceles triangle with vertices[0, 0), [4,0], [2,4]
from matplotlib import pyplot as plt
ig, ax=plt.subplots()
ax.axhline(, color='black')
ax.axvline(®,color='black")
green_triangle=plt.Polygon( [15,4], [7.4], [6,6]] ,edgecolor=
ax.add_patch(green_triangle)
blue_rectangle=plt .Polygon([{2,2], [10,2], [1¢,8] , [2,8]] ,edgecolor="blue" , fac
ax.add_patch(blue_rectangle)
zed_polygon=plt .Polygon({[6,2], [10,4], [8.7], [4,8], [2,8] ,edgecolor=
ax.add_patch(red_polygon)
isosceles_triangle=plt.Polygon([(0,0], [4,0], [2,4]] ,edgecolor=' orange’ ,facec
ax.add_patch(isosceles_triengle)
ax.set_xlim([-1,11])
ax.set_ylim([-1,11])
ax. set_xlabel("X-Axis”)
ax. set_ylabel("Y-Axis")
ax.set_title("2D Shapes")
green’ , facecolor=
plt..show()
2D Shapes
0
8
a 6
| IN
2
°
° 2 4 6 8 10
Kexis
#practical1q7py
# To generate vector in x in the interval [-22,22]
# with 80 subintervals using numpy package in python
import numpy as np
xenp. Linspace(-22,22,num=81)
print(x)
[-22. -21.45 -20.9 -20.35 -19.8 -19.25 -18.7 -18.15 -17.6 -17.05
16.5 -15.95 -15.4 -14.85 -14.3 -13.75 -13.2 -12.65 -12.1 -11.55
11. -10.45 -9.9 -9.35 -8.8 -8.25 -7.7 -7.15 -6.6 -6.05
“5.5 -4.95 -4.4 -3.85 -3.3 -2.75 -2.2 -1.65 -1.1 -@.55
0. O55 1.1 1.65 2.2 2.75 3.3 3.85 44 4.95
5.5 6.05 6.6 7.15 7.7 8.25 8.8 9.35 9.9 10.45
11. «11.55 12.1 12.65 13.2 13.75 14.3 14.85 15.4 15.95
16.5 17.05 17.6 18.15 18.7 19.25 19.8 20.35 20.9 21.45
22. 4In [2]:
In [3]:
PRACTICAL:-02
apractical2ql.py
# write a python program to plot 20 graph of
#function f(x)=x**2 & g(x)=x**3 in range[-1,1]
from matplotlib import pyplot as plt
import numpy as np
p. Linspace(-1,1, 100)
ylex**2
yaex**3
plt.plot (x, y1,1abel="f(x)=x**2')
plt.plot (x, y2, label="q(x)=x**3")
plt.xlabel('x-Axis')
plt.ylabel (‘y-Axis')
plt.legend()
plt.title("20 graph of f(x) and g(x)")
plt.show()
2D graph of f(x) and g(x)
1.00
0.75
0.50
0.25
0.00
y-axis
-0.25
-0.50
-0.75
— fix)=xe2
-1.00 — g{x)=x3
-1,00 -0.75 -0.50 -0.25 0.00 025 050 075 1.00
xAxis
#practical2q2.py
# write a python program to plot 2D graph of
#function F(x)=10g10(x) in range[0, 10]
from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(0, 10,100)
y=np.10g10(x)
plt.plot(x.y)
plt.xlabel("x')
plt.ylabel('f(x)=10g10(x)")
plt.title("2D greph of f(x)=10g10(x)")
plt..show()In [4]
/tmp/ipykernel_5214/360823099.py:7: Runtimeliarning: divide by zero encount
ered in logi¢
yenp.10g10(x)
2D graph of f(x:
1.00
0.75
0.50
gz (0.25
3S
g
3 0.00
7
¥ -0.25
-0.50
-0.75
-1.00
° 2 4 6 8 10
x
#practical2q3.py
# write a python program to plot 2D graph of
#function f(x)=1og(x)+5 & g(x)=log(x)-5 in range[0, 10] by
#setting different line width & different colours to the curve
from matplotlib import pyplot as plt
import numpy as np
x=np. linspace(-1,1, 100)
p-109(x)+5
ip 10g(x) -S
plt.plot(x,y2, label="g(x
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.legend()
plt.title("20 graph of F(x) and g(x)")
plt..show( }
/tmp /ipykernel_5214/2525585836.py:8: RuntimeWarning: invalid value encount
ered in log
yl=np.1og(x)45
/tmp/ipykernel_5214/2525585836. py
ered in log
y2=np.1og(x)-5
RuntimeWarning: invalid value encountIn [5]:
2D graph of f(x) and g(x)
¥Axis
— f{x)=log(x)+5.
— 9{x)=l09(x)-5
-10 -
0.0 02 04 06 08 1.0
XAXiS
#practical2q4.py
# WAPP plot the 2D graph of the function
# f(x)=sin(x) & g(x)=cos(x) in(-2pi, 2pi)
from matplotlib import pyplot as plt
import numpy as np
from math import*
x=np.linspace(-2*pi,2*pi)
yl=np.sin(x)
y2=np.cos(x)
plt. plot (x, y1,label='F(x)=sin(x)')
plt. plot (x, y2,label="g(x)=cos(x) ')
plt.xlabel("X-Axis')
plt.ylabel(‘Y-Axis')
plt.legend()
plt.title("20 graph of f(x) and g(x)")
plt..show()In [6]
2D graph of f(x) and g(x)
1.00
0.75
0.50
0.25
0.00
¥axis
-0.25
-0.50
-0.75
-1.00
— fx)=sin(x)
— {x)=cos(x)
#practical2q5.py
# WAPP plot the 20 graph of the function f(x)=e%x (sin(x))in
#(-5pi,5pi) with blue points Line with upword pointing triangle
from matplotlib import pyplot as plt
import numpy as np
from math import*
se
ip. Linspace(-5*pi,5*pi)
yenp.exp(x) *np.sin(x)
plt.plot(x,y,marker='A',color='b' , linestyle="-' ,markersize=5)
plt-xlabel('x')
plt.ylabel (f(x) =exp(sin(x))")
plt.title("
plt-show()
20 graph of f(x)=exp(x)*(sin(x))")In [5]:
126 2D graph of f(x)=exp(x)*(sin(x))
2.0
15
0.0
#practical2q6. py
# WAPP plot the 2D graph of the function f(x)=x**4 in(0,5)
# with red dashed Line with circle markers
from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(@,5)
yext4
plt.plot(x,y,marker='o' ,colors'r' ,linestyle='--',markersizes5)
plt.xlabel("x')
plt.ylabel('f(x)=x**4"')
plt.title("2D graph of f(x)=x**4")
plt.show()2D graph of f(x)=x**4
600
500
400
xed
300
fox):
200
100
In [6]: #practical2q7. py
# WAPP draw the 2D plot y=log(x**2)+sin(x) with suitable
# label in the x and y Axis & title in(-5pi, 5pi)
from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(®, 12,100)
p- log (x**2) #np. sin(x)
plt.plot(y)
plt.xlabel('x')
plt .ylabel('f(x)=1og(x**2)+sin(x) ")
plt.title("2D graph of f(x)=log(x*#2)+sin(x)")
plt.show()
/tmp/ipykernel_3819/2617708@15.py:7: RuntimeWarning: divide by zero encoun
tered in log
y=np. Jog (x**2)+np. sin(x)In [7]
2D graph of f(x)=log(x**2)+sin(x)
‘
es
= 2
z
S
& oO
ge |
:
x
=
2
-4
—
0 20 40 60 80 100
*
#practical2q8.py
# WAPP to draw 2D plot y=x sin(1/x*2)in (-5,5)with suitable label
#in the x axis,y axis a title and location of legend to lower right corne.
from matplotlib import pyplot as plt
import numpy as np
xenp. Linspace(-5,5,100)
y=x*np.sin(1/(x**2))
plt.plot(y)
plt.xlabel('x")
plt.ylabel( 'f(x)=x*sin(1/(x**2))')
plt.title("2D graph of f(x)=x*sin(1/(x**2))")
plt.show()In [10]:
2D graph of f(x)=x*sin(1/(x**2))
0.75
0.50
0.25
0.00
xt sin(1/(x**2))
-0.25
fx):
-0.50
-0.75
#practical2q9. py
#plot the graph of sin(x),cos(x),ex,x**2in
#(0,5)in 1 figure with 2*2 subplots
from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(@,5,100)
yl=np.sin(x)
y2=np.cos(x)
y3=np.exp(x)
yaex**2,
plt .subplot(2,2,1)
plt.plot(x,y1)
plt.title(#' (x)=sin(x)')
plt.subplot (2,2,2)
plt.plot(x,y2)
plt.title(#'(x)=cos(x)')
pit. subplot (2,2,3)
plt.plot(x,y3)
plt.title(#' (x)=exp(x)')
pit. subplot (2,2, 4)
plt.plot(x,y4)
Plt.title(f'(x)=x**2")
plt.xlabel ( *X-Axis')
plt.ylabel('Y-Axis')
plt.show()
100(x)=sin(x) (x)=cos(x)
10 10
OS Os
0.0 0.0
-0.5 |-0.5
-1.0 1.0
° (exp(x) 4 ° OG=x2 4
150 25
20
100 5
E 10
50
5
0 0 —-
oO 2 4 0 z 4
XAxis,
In [11]: #practical2q10.py
#HAPP to plot the graphs of sin(x),eax and x**3 in (8,5)in one figure wit!
from matplotlib import pyplot as plt
import numpy as np
xenp. linspace(0,5, 100)
yasnp.sin(x)
y2=np-exp(x)
yaex*#3
pit. subplot (2,2,1)
plt.plot(x,y1)
plt.title(f' (x)=sin(x)")
plt.subplot (2,2,2)
plt.plot(x,y2)
plt.title(f'(x)=exp(x)')
pit. subplot (2,2,3)
plt.plot(x,y3)
plt.title(f'(x)=x*#3")
plt.show()10
05
0.0
0.5
125
100
75
50
25
(x)=sin(x) (x)=exp(x)
150
100
504
. _, 0
0 (Q=x"3 4 ° 2 4
In [12]: #practical2q11.py
#WAPP to plot the graph of sin(x),cos(x)in (0,pi)in one figure with 2*1 si
from matplotlib import pyplot as plt
from math import*
import numpy as np
x=np.linspace(@, pi, 100)
yl=np.sin(x)
y2=np.cos(x)
plt. subplot (2,2, 1)
plt.plot(x,y1)
plt.title(#' (x)=sin(x)")
plt.subplot(2,2,2)
plt.plot(x,y2)
plt.title(#" (x)=cos(x)")
plt.show()
(x)=sin(x) {x)=cos(x)
10 10
08 05
06
0.0
04
0.2 os
0.0 1.0In [5]:
PRACTICAL:-03
#practical3q1.py
#UAPP to plot 30 graph of the function F(x)=eA-x2 in (-5,5)with
#green dashed points line with upword pointing triangle from matplotlib it
from mpl_toolkits import mplot3d
from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(-5,5,5)
y=np.exp(-x**2)
plt.plot (y,marker='4* , color:
plt.plot(x,y,z)
pit.xlabel("X")
plt.ylabel(‘Y")
plt.title("3D graph of f(x)=exp(-x*#2)")
plt.show()
,linestyle='-' ,markersize=5)
3D graph of f(x)=exp(-x**2)
10
0.8
0.6
0.4
0.2
0.0In [2]: #practical3q2. py
#WAPP to generate 30 plot of the function
from mpl_toolkits import mplot3d
from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(-10,10,100)
y=np. linspace(-10, 10,100)
X, Y=np.meshgrid(x,y)
Z=np.sin(X)+np.cos(Y)
fig=plt.figure()
ax=plt .axes (projectior
ax.plot_surface(X,Y,2)
ax. set_xlabel('X")
ax. set_xlabel('Y")
ax.set_xlabel('Z")
plt..show()
inxtcosy in -10exy 000
-025
-050
“075
1.00
as 0 05 00 05 10 15
#Practical n0.5
#06.Using python, generate triangle with vertices (0,0), (4,0)
#,(1,4), check whether the triangle is Scalene triangle.
from sympy import*
from math import*
A=Triangle(Point (9,6), (4,6), (1,4))
A is cralane()
TrueIn [23]:
Out [23]:
In [37]:
Out [37]:
In [22]:
#Practical no.5
#07.Write a Python program to find the area and perimeter
#of the triangle ABC, where A[9, 0],B[5, 9] and C[3, 3].
from sympy import*
from math import*
B=Triangle(Point (0,6), (5,0), (3,3))
R area
15
2
R_nerimatar
VI3+3V245
#Practical no.5
#08.Write the Python program to rotate the triangle
#ABC by 180 degree, where A[1, 2],B[2, -2] & C[-1, 2].
from sympy import*
from math import*
B=Triangle(Point (1,2), (2,-2),(-1,2))
RorntatelnilIn [21]:
In [18]:
import matplotlib.pyplot as plt
vertices = [{4, 3], [6, 3], [6, 5]]
reflected_x = [[x, -y] for x, y in vertices]
reflected_y = [[-x, y] for x, y in vertices
triangle = plt.Polygon(vertices, color="blue’)
plt.gca() .add_patch(triangle
reflection_x = plt.Polygon(reflected_x, color="red‘)
plt.gca() .add_patch(reflection_x)
reflection_y = plt.Polygon(reflected_y,color="green')
plt.gca() .add_patch(reflection_y
plt.axhline()
plt .axvline()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Triangle and its Reflections’)
plt.axis('equal')
ALE chow!) _
‘Triangle and its Reflections
#Practical n0.5
#hrite a python program to plot triangle with vertices [3,3],
#[5,6],[5,2],and its rotation about the origin by angle -n radiansimpor
import numpy as np
A = np.array([3,3])
B = np.array([5,6])
C = np.array([5,2])
theta = -np.pi
rotation_matrix = np.array({{np.cos(theta), -np.sin(theta)], {np.sin(the
np. cos(theta)]])
A_rotated = np.dot(rotation_matrix, A)
B rotated = np.dot(rotation_matrix, B)
C_rotated = np.dot(rotation_matrix, C)
fig, ax = plt.subplots()
ax.plot({A[9], B(O], CLO], AlO]], [ACL], Bf1], Cf1], ALL], ‘b’y label=
ax.plot([A_rotated[0], B_rotated[0], C_rotated[0], A_rotated(0]], [A_ro
B_rotated[1], C_rotated{1], A rotated[1]], 'r', label='Rotated Triangle
ax.scatter(A[0], A[1], color="g', marker='o', label="A')
ax.scatter(B[0], B[1], color='g', marker='o', label="B")
ax.scatter(C[0], C[1], color='g', marker='o', label="C')
ax.scatter(, 0, color='g', marker='0', label='Origin")ax. legend()
ax, set_xlabel( 'X-axis')
ax, set_ylabel( 'Y-axis')
ax.set_title('Rotation of Triangle ABC by -pi Radians around Origin’)
plt.axis (‘equal’)
plt.grid(True)
Alt chow!)
Rotation of Triangle ABC by -pi Radians around Origin
6 | — original Tiangte ABC .
— Rotated Tangle A''C"
ate a sy
8B
2 c UU
© Origin
-100 -75 -50 25 00 25 50 75 100In [23]:
In [ 1]:
In [34]:
PRACTICAL:-06
#practicaléql.py
#write a python program to reflect the line segnent
#joining the points A[5,3]and B[1,4]through the line y=x+1
import numpy as np
import matplotlib.pyplot as plt
‘A=np.array([5,3])
Benp.array({1,4])
reflection_line=lambda x: x+1
plt.plot([A(0],8[0]],(A{1],B(1]],'-0', label
x_vals=np. Linspace(-5,5,100)
pit. plot(x_vals, reflection_line(x_vals),'-r',label='reflection line y=x+1")
reflect_A=np.array([reflection_line(A[0]) ,A{0]])
reflect_B=np.array([reflection_line(B[0]),B[0]])
plt.xlabel('x')
plt.ylabel('y')
plt,.title('line segment reflection’)
plt.legend()
plt.show()
original line segment AB')
line segment reflection
8] -e original fine segment AB
— reflection ine y=x+1
#practicalég2.py
#Generte Line segment having endpoint
#(0,0)and) (10,10) find midpoint of line segment
import numpy as np
inport matplotlib.pyplot as plt
x1,y1=6,0
x2,y2=10,10
midpoint _x=(x1+x2) /2
midpoint_y=(y1+y2) /2
print (midpoint: ({},{})"-format (midpoint_x,midpoint_y))
plt. show()
midpoint: (5.0,5.0)In [ 1:
In [38]:
In (1):
In [2]:
#practical6q3. py
#Using sympy,declare the point A(0,7),B(5,2)
#declare the Line segment passing through then. find the length and
#midpoint of the Line segment passing through points aand B
from sympy import *
A=Point(0,7)
int (5,2)
Line_AB=Line(A,B)
length_AB=A.distance(B)
midpoint_AB=((A[0]+B[0])/2, (A(1]+B(1] )/2
print("Point A :{}", format (A)
print("Point B :{}", format (B)
print("line segment AB: . format (1ine_AB))
print(“length of line segment AB: {}". format (Length_AB) )
print("midpoint of line segment AB:{}".format(midpoint_AB))
plt.show()
Point A :{} Point2D(0, 7)
Point B :{} Point2D(5, 2
Line segment AB:Line2D(Point20(0, 7), Point2D(5, 2))
length of Line segment AB:5*sqrt (2
midpoint of line segment AB: (5/2, 9/2
#practical6q4.py
#wAPP 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
x2,y2=2,-1
plt. plot ( {x1,x2], [y1,y2], ‘bo-', label
x1_rot,yl_rot=-x1,-y
x2_rot ,y2_rot=-x2, -y2
plot ([x1_rot,x2_rot] , [y1_rot,y2_rot], ‘ro-',label=' rotate line")
-xlim(-3,3)
plt.ylim(-3,3)
plt.xlabel(‘x')
plt.ylabel (‘y')
plt.title( ‘rotation Line segment by 180 degrees’
plt.Legend()
plt.show()
original Line’)rotation line segment by 180 degrees
-® original tine
-e rotate line
11131! gpracticalégs. py
#WAPP to reflect the line segment joining
#the points A[-5,2jand B(1,3]through the Line y=x
In [9]:
from sympy import*
import matplotlib.pyplot as plt
‘AsPoint([-5,2])
B=Point({1,3])
plt. plot ({A(0] ,B(0]], [A[1] ,B[1]],‘-0',label='original Line segment AB’
x_vals=np. Linspace(-5,5,50)
plt. plot (x_vals, reflection_line(x_vals),'-r',label="reflection Line y=x'
reflect_A=np.array({reflection_line(A[0]),At0]]
reflect_B=np.array([reflection_line(B[@]) ,B[0]]
plt.xlabel('x')
plt.ylabel('y')
plt.title(‘line segment reflection’
plt.Legend()
plt. show()
line segment reflection
6 | -e- original line segment AB
— reflection tine y=xIn [4]: goractical6gs.py
#find combined transformation of the Line segment betweenthe points
#A[4,-1]and 8[3,0] for the following sequence of transformation
#first rotation about origin through an angle pie c;
#followed scaling in x coordinate by 3 units;
#followed by reflection through the Line y=x.
1 [22]: trom synpy import*
x, yssymbols('x y')
A=Point (4, -1)
BzPoint (3,0)
AsA. rotate (pi)
B=8. rotate(pi)
A=A. transform (Matrix([{[1,0,0]
B=B.transforn(Matrix([[1,0,0]
A=A. scale(3,0)
B=B. scale(3,0)
AzA. reflect (Line(y-x))
B=B. reflect (Line(y-x) }
print(*The transformation coordinates of Point A are
print ("The transformation coordinates of Point B are:",B)
The transformation coordinates of Point A are: Point2D(0, 3/2)
The transformation coordinates of Point B are: Point2D(0, -9)
In (151: gpractical6q?. py
#WAPP to reflect the Line segment joining the
#points A[-5,2],B[3,-4] through the Line y=2x-1
In (171: trom sympy import*
import matplotlib.pyplot as plt
A=Point([-5,2])
B=Point ([3,-4])
plt.plot([A[0],B6[0]],[Al1],8[1]],'-0', label='original line segment AB’)
x_vals=np. Linspace(-5,5,50)
pit. plot (x_vals, reflection_Line(x_vals), '-r',label='reflection Line y=2x-1")
reflect_A=np. array ([reflection_line(A[0]) ,Al6]])
reflect_B=np.array([reflection_line(B[@]),B[0]])
plt.xlabel('x")
plt.ylabel('y")
plt,title('Line segnent reflection")
plt.legend()
plt.show()In [19]:
In [27]:
line segment reflection
6] —e- original line segment AB
— reflection line y=2x-1
#practical6q8.py
#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], [4,1],
#then using python find the slope and midpoint of the transformed Line
import numpy as np
Tenp.array(({(2,3],4,1]])
A=np.array([2,5])
Benp.array((4,-13])
A_primesnp.dot(T, A)
B_prime=np. dot (T,B)
slope=(B_prime[(1]-A_prime[1}) /(B_prime[@]-A_prime[®])
midpoint=(A_prime+8_prime)/2
print(*slope of the transformed Lint
print(*midpoint of the transformed
slope)
ine: " midpoint)
slope of the transformed line: 0.2
midpoint of the transformed line: [-6. 8.]PRACTICAL:-07
jit Format Run Options Window Help
File
Practical No.7 Qi Write a Python program to solve the following LPP:
Max Z = 150x + 75y
subject to 4x + 6y < 24 ,5x + 3y $ 15.x20, y2 0.
from pulp import*
model=LpProblem(nam
x=LpVariable(name:
y=LpVariable (name:
50*x+75*y)
model+=(4*x+6*y<=24)
*x+3*y<=15)
print (model. solve())
print(model objective. values())
print(x.value())
print(y.value())
‘smal1_problem” ,sense=LpMaximize)
" , lowBound=@)
JowBound=0)
OUTPUT:
1
odict_values([150, 75])
3.0
0.0
>>>
Practical No.7 Q2 Write a Python program to solve the following LPP
Max Z = 3x + Sy + 4z
subject to 2x + 3y < 8 ,2y + Sz < 10 ,3x + 2y + az < 15.
x20, y20
from pulp import*
model=LpProblem(name="smal1_problem” ,sense=LpMaximize)
pVariable(name="x" , lowBound=0)
pVariable(name="y"
pVariable(name:
model+=(3*x+5*y+4*z)
=(2*y+5*z<=10)
model+=(3*x+2*y+4*z<=15)
print(model.solve())
print(model.objective.values())
print(x.value())
print(y.value())
print(z.value())
OUTPUT: -
1
odict_values([3, 5, 4])
2.1707317
1,2195122
1.5121951
>>>File Edit Format Run Options Window Help
Practical No.7 Q3
Write a Python program to solve the following LPP:
Max Z = 5x + By
subject tox ty s7
2x + Sys, x20, y20
from pulp inport*
model=LpProblem(name="small_problem", sens
x=LpVariable(name="x", lowBound=0)
y=LpVariable(name="y", lowBound=0)
model*=(5*xt3*y)
model*=(1*x+1*y<=7)
model*=(2*x+5*y<=1)
print(model. solve(})
print{model. objective. values{})
print(x.value(}}
print(y.value(}}
pMaximize)
OUTPUT: -
1
odict_values([5, 31)
@.5
8.0
bo
Practical No.7 Q4
Write a Python program to solve the following LPP:
Max Z = 4x + y + 3z + Sw
subject to 4x + 6y - 5z - 4w > -20
-8x - By + 32 + w < 20,
x20, y2Q.
from pulp import*
model=LpProbl em(nam:
x=LpVariable(name="x", lowBound=0)
y=LpVariable(name="y", lowBound=2)
z=LpVariable(name="z" , lowBound=0)
w=LpVariable(name="w", lowBoun
model#=(4*xt1*y#3*z#5*w)
model +=(4*x+6*y-5*2-A*w>=-20)
model+=( -8*x-3*yt3*zt2*w<=20)
print(model. solve())
print{model. objective. values{})
print(x.value(), y.value(}, z.value(), w.value())
="small_problem" , sense=LpMaximize)
OUTPUT: -
-2
odict_values([4, 1, 3, 51)
0.00.0 0.05.0
pppFile Edit Format Run Options Window Help
Practical No.7 Q5
Write a Python program to solve the following LPP:
Min Z = 3.5x + 2y
subject tox t+ y25
x24, ys2,
x20, y2Q.
from pulp import*
model=LpProblem(name="small_problem" , sense=LpMinimize)
x=LpVariable(name="x", lowBound=2)
y=LpVariable(name="y", lowBound=0)
model*=(3.5*xt2*y)
model+=(4*x+6*y>=5)
model*=(1*x>=4)
model+=(14y<=2)
print(model. solve())
print{model. objective. values{})
print(x.value(}}
print(y.value(}}
OUTPUT: -
1
odict_values([3.5, 2])
40
8.0
>>
Practical No.7 Q6
Write a Python program to solve the following LPP:
Min Z=x + 2y +z
subject tox + 1/2 y+ 1/221
B/2x+ 4+t72E8B
x20, y2@.
from pulp import*
model=LpProblem(name="small_problem”, sense=LpMaximize)
x=LpVariable(name="x", lowBoun:
y=Lpvariable(name="y", lowound=2)
z=LpVariable(name="z", lowBound=0)
model#=(1*x#2*y+1*2)
modelt=(1*xt1/2*yt1s2*z<=1)
model+=(3/2*xt2*yt1*z>=8)
print(model. solve())
print(model. objective. values(})
print(x.value(), y.value(},z.value())
OUTPUT: -
1
odict_values([1, 2, 11)
0.00.4 0.0lit Format Run Options Window Help
Practical No.7 Q7
Write a Python program to solve the following LPP:
Min Z = 3x + Sy + 4z
subject to 2x + 2y < 12
2x + 2y <1
Sx + 2y < 10
x20, y2Q.
from pulp import*
model=LpProblem(name="small_problem" , sense=LpMaximize)
x=LpVariable(name="x" , lowBound=2)
y=LpVariable(name="y", lowBound=2)
z=LpVariable(name="z", lowBound=0)
model+=(3*x+5*y+4*z)
model+=(2*x+2*y<=12)
model#=(2*x+2+y<=10)
model+=(5*x+2*y<=10)
print{model. solve())
print(model. objective. values(})
print(x.value()}
printty.value(}}
print(z.value()}
OUTPUT: -
-2
odict_values([3, 5, 41)
a.0
a0
2.0
>>>PRACTICAL:-08
File Edit Format Run Options Window Help
Practical No. 8
Ql)Write a python program to display the following LPP by using pulp module
and |simplexmethod. Find its optimal solution if exist.
MinZ=x+y
subject to x 2 6
y26
x+y= 6
Ip_problem += y >= 6
Ip_problem += x + y <= 11
1p_problem.solve()
print("Status:", LpStatus[1p_problem. status] )
if LpStatus[1p_problem.status] == "Optimal":
print("Optimal Solution:")
print("x =", value(x))
print("y =", value(y))
print("Optimal z =", value(1p_problem. objective) )
else:
print("No optimal solution found.")
Status: Infeasible
No optimal solution found.
>>>File Edit Format Run Options Window Help
Practical No. 8
Q2)Write a python program to display the following LPP by using pulp module
and simplex method. Find its optimal solution if exist.
Max Z = 3x1 + 5x2 + 4x3
subject to 2x1 + 3x2 <8
2x2 + 5x3 < 10
3x1 + 2x2 + 4x3 ¢ 15
x1 > 0, x2 > 0, x3 > 0.
from pulp import *
lp_problem = LpProblem( "Maximize 2", LpMaximize)
x1 = LpVariable(‘x1", lowBound=a)
x2 = LpVariable('x2", lowBoun:
x3 = LpVariable('x3", lowBound=8)
Ip_problem += 3*x1 # 5*x2 + 4*x3, "Z"
lp_problem t= 2*x1 + 3*x2 <= 8
lp_problem t= 2*x2 + 5*x3 <= 10
lp_problem += 3*x1 + 2*x2 + 4*x3 <= 15
lp_problem += x1 + x2 <= 11
1p_problem. solve()
print("Status:", LpStatus[1p_problem. status])
if LpStatus[1p_problem.status] == "Optimal":
print¢"Optimal Solution:"}
print¢"x1 =", value(x1))
print("x2 =", value(x2))
print("x3 =", value(x3))
print("Optimal Z =", value(1p_problem. objective})
else:
print("No optimal solution found.)
OUTPUT: -
Status: Optimal
Optimal Solution:
x1 = 2,1707317
x2 = 12195122
x3 = 1,5121951
Optimal Z = 18.658536500000004
>>>File Edit Format Run Options Window Help
Practical No. &
Q3)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 + Sw
subject to 4x + Gy - 52 - 4w > 20
-3x - 2y + 42 + ws 10
-8x - By + 3z + 2w < 20
x2Q,y20,z20,we@.
from pulp import*
1p_problem = LpProblem("Maximize_2", LpMaximize)
LpVariable(*x', LowBoun:
LpVariable("y', LowBoun
LpVariable(*z', lowBouni
LpVariable(*w', LowBount
Ip_problem t= 4*x + y + 3¢z + 54w, "Z"
Ip_problem += 4*x + 6ty - 5*z - 4*w >= 20
lp_problem += -3*x - 2*y + 442 + w <= 10
Ip_problem += -8*x - a*y + 34z + 24w <= 20
1p_problem. solve()
print("Status:", LpStatus[{1p_problem.status])
if LpStatus[1p_problem.status] == "Optimal":
print("Optimal solution:")
print("x value(x))
print("y =", value(y))
print("z value{z))
print("w =", value(w))
print("Optimal z =", value(1p_problem.objective))
else:
print("No optimal solution found."}
OUTPUT: -
Status: Unbounded
No optimal solution found.File Edit Format Run Options Window Help
Practical Wo. B Qa}
from pulp import*
Ap_problem = LpProblem( “Minimize Transportation Costs", LpMinimize)
x_s1_d1 = LpVariable('x_si_d1', lowBound=8)
x_s1_d2 = LpVariable('x_s1_d?", lowBound=8)
x_s1_d3 = LpVariable("x_©1_d3", lowBound=@)
x s1_d4 = LpVariable(’x_s1_d4°, lowZound=9)
x_s2_d1 = LpVariable('x_s2_di', lowBound=8)
x_s2_d2 = LpVariable('x_s?_d2", lowBound=8)
x_s2_d3 = LpVariable(*x_©2d3", lowBound=@)
x s2_d4 = LpVariable(’x_s2_d4", lowBound=0)
x_s3_d1 = LpVariable('x_s3_di', lowBound=8)
x_53_d2 = LpVariable('x_s3_d2", lowBound=8)
x_s3_03 = LpVariable(’x_s2_d3", lowBound=8)
x_s3_di = LpVariable('x_s3_di", lowBound=8)
Ap_problem r= (19*_s¥_dl + 30*«_sl_d2 + 58*«_s1_d3 + W*x_sl_dé +
Jotx_s2_d1 + 20x s2_d2 + A0*%x s2_d3 + GO%x 5? dd +
AO"x 53d] + B*x_53_d2 + 705342 + 20"_52 44), “Total_Transportation Costs”
Ap_problem += (x_s1_d1 + x_s1_d2 + «_s1_43 + x sI_d4 <= 7, “Supply _Constraint_st"}
Ap_problem += (x_s?_d1 + x_s2_d2 + x_s2_d3 + x_s2_dd c= 9, “Supply_Constraint_52")
Ap_problem += (x 53 1 + x 53 d2 + x_s3_d3 + x 53 dd <= 18, “Supply Constraint_s3")
Ap_problem += (x s1_dl + x_s2.d1 + x_s2 dl >= §, “Demand Constraint_D1")
Ap_problem += (x_sl_d2 + x_s2_d2 + x_s3d2 2= 6, "Demand Constraint_02")
Ap_problem r= (x_sl_d3 + x_s2_d3 + x_s3_d3 >= 7, "Demand_Constraint_D3")
Ap_problom r= (x sI_da + x 52 d4 + x53 d4 >= 1, “Domand_Constraint_Od")
Ap_problem.solvet}
print(*Status:", LpStetus{1p_problem.status]}
if LpStatus(Ip_problen. status] == "Optimal*
print("Optimal Solution=")
print("Transportation from Si to
print(*Transportation from S1 to
print(*Transportation from S1 to
print("Transportation from Si to
print(*Transportation from S2 to
print ("Transportation from 52 to
print("Transportation from s2 to
print("Transportation from $2 to
print("Transportation from 53 to
print(*Transportation from 53 to walue(x_s3_d2}}
print(*Transportation from 53 to value(x_s3_03)}
print("Transportation from S2 to D4:", value(x_s3_d4))
print(*Total Cost of Transportation:", valuetIp_problem.objective))
else
print("Mo optimal solution found.~)
value(x_s1_d1})
value(x_s1_d7}}
walue(x_s1_d3))
value(x_s1_049)
value(x_s2_d1}}
value(x_s2_d?}}
value(x_s2_43)}
value(x_s2_44})
value(x_s3_d1})
OUTPUT
Status: Optimal
Optimal Solution:
Transportation from 1 to D1: 5.4
Transportation fron $1 te D2: 8.0
Transportation from $1 to D2: 0.8
Transportation from $1 to D4: 2.9
Transportation fron $2 to D1: 8.4
Transportation from 52 to D2: 6.0
Transportation from $2 te D3: 7.8
Transportation from $2 to D4: 0.9
Transportation from $3 to D1: 8.4
Transportation fron 53 te D2: 6.0
Transportation from $3 to D3: 0.0
Transportation from 52 to D4: 12.9
Total Cost of Transportation: 683.8