[go: up one dir, main page]

0% found this document useful (0 votes)
11 views33 pages

Maths Ans

The document contains programs to generate various plots in Python: 1. Line plots, contour plots, wireframe plots and surface plots of functions such as y=log(x^2)+sin(x), y=e^-x^2, y=sin(x)+cos(y) between different intervals. 2. Subplots to plot multiple functions like y1=exp(-x)*sin(x), y2=exp(-2x)*cos(x) on the same figure. 3. 3D plots including contour plots, wireframe plots and surface plots of functions involving x, y and z. 4. Programs to generate points, lines, segments, rays and

Uploaded by

rohan bawadkar
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)
11 views33 pages

Maths Ans

The document contains programs to generate various plots in Python: 1. Line plots, contour plots, wireframe plots and surface plots of functions such as y=log(x^2)+sin(x), y=e^-x^2, y=sin(x)+cos(y) between different intervals. 2. Subplots to plot multiple functions like y1=exp(-x)*sin(x), y2=exp(-2x)*cos(x) on the same figure. 3. 3D plots including contour plots, wireframe plots and surface plots of functions involving x, y and z. 4. Programs to generate points, lines, segments, rays and

Uploaded by

rohan bawadkar
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/ 33

Practical No 1 to 10

Name - Rutuja Sunil Wagaj.

Roll No- 30 (Akurdi)


1. Write a Python program to draw a line 𝑦 = log 𝑥 2 + sin 𝑥 with suitable label in 𝑥 axis, 𝑦 axis and a title in
[−5𝜋, 5𝜋] .
Program:-
from pylab import*
import numpy as np
x=np.linspace(-5*pi,5*pi,100)
y=np.log(x**2)+np.sin(x)
plot(x,y)
xlabel('x-axis')
ylabel('y-axis')
title('Line of Y is')
show()

Output

Q2:- Generate a 2D plot for the following functions using the Python. i. 𝑦 = −𝑥 sin , −5𝜋 ≤ 𝑥 ≤ 5𝜋 ii. 𝑦 = 1 1+𝑥 2 log
, −5 ≤ 𝑥 ≤ 5

Program:- I)

from pylab import*

import numpy as np

x=np.linspace(-5*pi,5*pi)

y=x*sin(x)
plot(x,y)

show()

Output:-

II)Program:-

from pylab import*

import numpy as np

x=np.linspace(-5,5)

y=(1/(1+x**2))*log(x)

plot(x,y)

show()

Output:-
3.Plot the graph of 𝑦 = 𝑒 −𝑥 2 in [−5,5] with green dashed-points line with Upward- Pointing triangleProgram:-

from pylab import *

import numpy as np

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

y=np.exp(-x**2)

plot(x,y,"-.^k",label="$y=e^{-x^2}$")

xlabel=("x-axis")

ylabel=("y-axis")

title("Graph of f(x)")

legend()

show()Output:-

Q4:- 4. Plot the graph of (𝑥) = sin 𝑥 and (𝑥) = cos 𝑥 in [−2𝜋, 2𝜋] with legend at upper left corner.

Program:-from pylab import*

import numpy as np

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

f1=np.sin(x)

f2=np.cos(x)

plot(x,f1,label="$\sinx$")

plot(x,f2,label="$\cosx$")

legend(loc=2)
show()

Output:-

5. Generate a subplot for the following functions using the Python. i. 𝑦 = 𝑒 −𝑥 sin , = 𝑒 −2𝑥 cos 𝑥 ; 0 ≤ 𝑥 ≤ 4𝜋 (2 × 1
subplots ) ii. 𝑦 = 1 , = 1 𝑥 2 , 𝑦 = 1 𝑥 3 ; −5 ≤ 𝑥 ≤ 5 (1 × 3) subplots

Program:-

from pylab import*

import numpy as np

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

y1=exp(-x)*sin(x)

y2=exp(-2*x)*cos(x)

subplot(2,1,1)

plot(x,y1,label="$\exp(-x)*sin(x)$")

legend()

subplot(2,1,2)

plot(x,y2,label="$\exp(-2*x)*cos(x)$")

legend()

show()

Output:-
II)Program:-

import matplotlib.pyplot as plt

import numpy as np

from math import *

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

y1=1/x

y2=1/x**2

y3=1/x**3

plt.subplot(1,3,1)

plt.plot(x,y1,label='$y1=\dfrac{1}{x}$')

plt.legend()

plt.subplot(1,3,2)

plt.plot(x,y2,label="$y2=\dfrac{1}{x^2}$")

plt.legend()

plt.subplot(1,3,3)

plt.plot(x,y3,label='$y3=\dfrac{1}{x^3}$')

plt.legend()

plt.show()

output:-
Q.1) 1. Generate 3D Contour Plots for the following functions using the Python.𝑦 = sin(𝑥 + 𝑦) , −2𝜋 ≤ 𝑥 ≤ 2 𝜋

Program:-

from mpl_toolkits import mplot3d

from pylab import*

import numpy as np

def f(x,y):

return np.sin(x+y)

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

y=np.linspace(-2*pi,2*pi,100)

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

z=f(X,Y)

ax=axes(projection='3d')

ax.contour3D(x,y,z,50)

xlabel('X-axis')

ylabel('Y-axis')

title('3D contour')

show()

Output:-
Q 1.)ii. 𝑦 = 𝑥 3 + 𝑦 2 , −5 ≤ 𝑥 ≤ 5

Program:-

from mpl_toolkits import mplot3d

from pylab import*

import numpy as np

from sympy import *

x,y=symbols('x y')

def f(x,y):

return x**3+y**3

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

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

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

z=f(X,Y)

ax=axes(projection='3d')

ax.contour3D(x,y,z,50)
xlabel('X-axis')

ylabel('Y-axis')

title('3D contour')

show()

output:-

Q .2)Generate 3D wireframe Plot for the function (𝑥) = 𝑒𝑥 2+𝑦 2 in the interval [0,2𝜋].

Program:-

from mpl_toolkits import mplot3d

from pylab import*

import numpy as np

from sympy import *

from math import*

x,y=symbols('x y')
def f(x,y):

return np.exp(x**2+y**2)

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

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

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

Z=f(X,Y)

ax=axes(projection='3d')

ax.plot_wireframe(X,Y,Z,rstride=3,cstride=3)

xlabel('X-axis')

ylabel('Y-axis')

title('3D wireframe')

show()

Output:-

Q.3)Generate 3D wireframe Plot for the function (𝑥) = sin(𝑥) + cos(𝑦) in the interval [−5,5]

Program:-
from mpl_toolkits import mplot3d

from pylab import*

import numpy as np

from sympy import *

x,y=symbols('x y')

def f(x,y):

return np.sin(x)+np.cos(y)

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

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

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

Z=f(X,Y)

ax=axes(projection='3d')

ax.plot_wireframe(X,Y,Z,rstride=5,cstride=5)

xlabel('X-axis')

ylabel('Y-axis')

title('3D wireframe')

show()

Output:-

Q.4)Generate 3D Surface Plot for the function (𝑥) = 𝑥𝑒−𝑥 2−𝑦 2 in the interval [−6,6].
Program:-

from mpl_toolkits import mplot3d

from pylab import*

import numpy as np

from sympy import *

x,y=symbols('x y')

def f(x,y):

return x*np.exp(-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)

ax=axes(projection='3d')

ax.plot_surface(X,Y,Z)

xlabel('X-axis')

ylabel('Y-axis')

title('3D Surface')

show() output:-
Q.5)Generate 3D Surface Plot for the function (𝑥) = 𝑥 3 + 𝑦 3 + 𝑥 2 + 𝑦 2 + 𝑥 + 𝑦 + 5 in the interval [−2,2]

Program:-

from mpl_toolkits import mplot3d

from pylab import*

import numpy as np

from sympy import *

x,y=symbols('x y')

def f(x,y):

return x**3+y**3+x**2+y**2+x+y+5

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

y=np.linspace(-2,2,100)

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

Z=f(X,Y)

ax=axes(projection='3d')

ax.plot_surface(X,Y,Z)

xlabel('X-axis')

ylabel('Y-axis')

title('3D surface')

show()

output:-
0…………………… Q.1)Generate following points using python.

A= (3,4)

B= (5,-7)

C= (0.4,0.9)

Q= (7/2,5/2)

OUTPUT:

from sympy import*

A=Point(3,4)

Point2D(3, 4)

B=Point(5,-7)

Point2D(5, -7)

C=Point(0.4,0.9)

Point2D(2/5, 9/10)

Q=Point(7/2,5/2)

Point2D(7/2, 5/2)

Q.2)Find the distance between points 𝑥, ; 𝑦, 𝑤 and 𝑥, 𝑧 , if 𝑥 = [0,0], 𝑦 = [2,2], 𝑧 = [−1, −1] and 𝑤 = [3,4].

OUTPUT:

x=Point(0,0)

y=Point(2,2)

z=Point(-1,-1)

w=Point(3,4)

x.distance(y)
2*sqrt(2)

y.distance(w)

sqrt(5)

x.distance(z)

sqrt(2)

Q.3) Generate points 𝑃 = (4, −8), = (3, −5.3) and = (7.7,10) , check if they are collinear.

from sympy import*

P=Point(4,-8)

P=Point(4,-8)

P=Point(4,-8)

Q=Point(3,-5.3)

R=Point(7.7,10)

Point.is_collinear(P,Q,R)

OUTPUT:

False

Q.4)Generate points 𝑎 = (3,2), 𝑏 = (5, −5), 𝑐 = (0, −6) and 𝑑 = (10, −10) , check if they are coplanar.

a=Point(3,2)

b=Point(5,-5)

c=Point(0,-6)

d=Point(10,-10)

Point.are_coplanar(a,b,c,d)

OUTPUT:

True
Q.5). Plot point 𝑋 = (−8,8) in python using matplotlib

from pylab import*

x=[-8]

y=[8]

plt.plot(x,y,marker='o')

xlabel('X label')

ylabel('Y label')

title('Graph of X')

plt.show()

GRAPH:

Q.1)

1)=>

L=Line(Point(1,5),Point(9,5.1))

L
Line2D(Point2D(1, 5), Point2D(9, 51/10))

2)=>

M=Line(2*x+3*y-6)

Line2D(Point2D(0, 2), Point2D(1, 4/3))

3)=>

N=Line(Point(2,6),slope=3)

Line2D(Point2D(2, 6), Point2D(3, 9))

4)=>

S=Segment((0,0),(6,6))

Segment2D(Point2D(0, 0), Point2D(6, 6))

5)=>

R=Ray((0,0),(4,4))

Ray2D(Point2D(0, 0), Point2D(4, 4))

Q.2)=>

from sympy import*

S=Segment((1,4),(7,14))

m=S.length

n=S.midpoint

print('lenght of segment is',m)

print('midpoint of segment is',n)

OUTPUT:

lenght of segment is 2*sqrt(34)

midpoint of segment is Point2D(4, 9)


Q.3)

from sympy import*

x,y=symbols('x y')

L=Line(3*x+4*y-5)

a=L.slope

print('Slope of line is',a)

OUTPUT:

Slope of line is -3/4

Q.4)

from sympy import*

x,y=symbols('x y')

L=Line((0,0),(0,3))

R=Ray((0,0),(8,8))

a=L.angle_between(R)

print('angle between line and ray is',a)

OUTPUT:

angle between line and ray is pi/4

Q.5)

from sympy import*

L=Line((0,8),(8,0))

S=Segment((0,0),(10,10))

a=L.intersection(S)

print('Intersection of line and segment is',a)

OUTPUT:
Intersection of line and segment is [Point2D(4, 4)]

Qno.1)

from sympy import *

A=Polygon((0,0),(1,0),(5,1),(0,1),(3,0))

p=A.area

q=A.perimeter

print('Area of polygon is',p)

print('perimeter of polygon is ',q)

Output : Area of polygon is 3/2

perimeter of polygon is sqrt(10) + sqrt(17) + 7

Q.no.2)

from sympy import *

A=Polygon((0,0),5,n=8)

a=A.area

b=A.perimeter

print('Area of polygon is',a)

print('perimeter of polygon is ',b)

Output : Area of polygon is (400 - 200*sqrt(2))/(-4 + 4*sqrt(2))

perimeter of polygon is 40*sqrt(2 - sqrt(2))

Qno.3)

i)

from sympy import *

T1=Triangle(sss=(3,4,5))

T1
Output : Triangle(Point2D(0, 0), Point2D(3, 0), Point2D(3, 4))

ii)

from sympy import *

T2=Triangle(sas=(1,30,2))

T2

Output : Triangle(Point2D(0, 0), Point2D(2, 0), Point2D(sqrt(3)/2, 1/2))

iii)

from sympy import *

v1,v2,v3=[(0,0),(1,0),(5,1)]

T3=Triangle(v1,v2,v3)

T3

Output : Triangle(Point2D(0, 0), Point2D(1, 0), Point2D(5, 1))

Q.no.4)

from sympy import *

p1,p2,p3=[(0,0),(4,0),(2,4)]

T4=Triangle(p1,p2,p3)

T4.is_isosceles()

Output : True

Q.no.5)

from sympy import *

a1,a2,a3=[(0,0),(4,0),(4,3)]

T5=Triangle(a1,a2,a3)

T5.is_right()
Output : True

Q.no.6)

from sympy import *

b1,b2,b3=[(0,0),(4,0),(1,4)]

T6=Triangle(b1,b2,b3)

T6.is_scalene()

Output : True

Q1. Apply each of the following transformation on the point [−2 3.5].

a. Reflection through the line 𝑦 = −𝑥.

b. Rotation about origin through an angle 57° .

c. Uniform scaling by factor 3.

d. Shearing in Y direction by 2 units.

e. Reflection through origin.

Program:-

from sympy import*

from math import*

x=Point(-2,3.5)

A=x.transform(Matrix([[0,-1,0],[-1,0,0],[0,0,1]]))

print('Reflection of the point x through y=-x is',A)

angle=radians(57)

B=x.rotate(angle)

print('Rotation about origin through an angle 57 degrees',B)

C=x.scale(3,3)

print('Uniform scaling by the factor 3 is',C)

D=x.transform(Matrix([[1,0,0],[2,1,0],[0,0,1]]))
print('Shearing in y direction by 2 units is',D)

E=x.transform(Matrix([[-1,0,0],[0,-1,0],[0,0,1]]))

print('Reflection through the origin is',E)

Output:-Reflection of the point x through y=-x is Point2D(-7/2, 2)

Rotation about origin through an angle 57 degrees Point2D(-12576953305747/3125000000000,


228895486661747/1000000000000000)

Uniform scaling by the factor 3 is Point2D(-6, 21/2)

Shearing in y direction by 2 units is Point2D(5, 7/2)

Reflection through the origin is Point2D(2, -7/2)

2. Apply each of the following transformation on the point [3 − 1].

i. Reflection through X-axis.

ii. Scaling in X-coordinate by factor 2.

iii. Scaling in Y-coordinate by factor 1.5.

iv. Rotation about origin through an angle 30°.

v. Shearing in both X and Y direction by -2 and 4 units respectively.

Program:-

from sympy import*

from math import*

x=Point(3,-1)

A=x.transform(Matrix([[1,0,0],[0,1,0],[0,0,1]]))

print('Reflection of the point x through x-axis is',A)

B=x.scale(2,0)

print('Scaling in x coordinate by factor 2',B)

C=x.scale(0,1.5)

print('scaling in y direction by the factor 3 is',C)

angle=radians(30)
D=x.rotate(angle)

print('Rotation about origin through an angle 30 degrees',D)

E=x.transform(Matrix([[1,4,0],[-2,1,0],[0,0,1]]))

print('Shearing in x & y direction by -2 & 4 units is',E)

Output:-

Reflection of the point x through x-axis is Point2D(3, -1)

Scaling in x coordinate by factor 2 Point2D(6, 0)

scaling in y direction by the factor 3 is Point2D(0, -3/2)

Rotation about origin through an angle 30 degrees Point2D(77451905283833/25000000000000,


633974596215561/1000000000000000)

Shearing in x & y direction by -2 & 4 units is Point2D(5, 11)

Q1.

Program:-

from sympy import *

s1=Segment((1,0),(2,-1))

a1=s1.rotate(pi)

print(a1)

OUTPUT:-

Segment2D(Point2D(-1, 0), Point2D(-2, 1))

Q2.

Program:-

from sympy import *

x,y=symbols('x y')

r=Ray((0,0),(2,4))

l=Line(x-2*y-3)
a=r.reflect(l)

print(a)

OUTPUT:-

Ray2D(Point2D(6/5, -12/5), Point2D(28/5, -16/5))

Q3.

Program:-

from sympy import *

l=Line((0,0),(0,1))

a=l.rotate(pi/2)

b=l.equation()

print(a)

print(b)

OUTPUT:-

Line2D(Point2D(0, 0), Point2D(-1, 0))

Q4.

Program:-

from sympy import*

x,y=symbols('x y')

l=Line(4*x+3*y-5)

m=Line(x+y)

n=l.reflect(m)

a=n.equation()

print("Equation of reflected line is:",a)


OUTPUT:-

Equation of reflected line is: x + 4*y/3 + 5/3

Q5.

Program:-

from sympy import *

x,y=symbols('x y')

A=Point(5,3)

B=Point(1,4)

l=Line(A,B)

l=Line(y-x-1)

a=l.reflect(l)

print(a)

OUTPUT:-

Line2D(Point2D(0, 1), Point2D(1, 2))

Q1:-

from sympy import*

x,y=symbols('x y')

l1=Line(2*x-y-5)

l2=Line(x+3*y+1)

p=l1.intersection(l2)

p=p[0]

q=p.transform(Matrix([[-2,1,0],[3,1,0],[0,0,-1]]))

print("Point of intersection of the transform line is",q)

Output:-

Point of intersection of the transform line is Point2D(-7, 1)


Q2:-

Program:-

from sympy import*

x,y=symbols('x y')

A=Point(-1,8)

B=Point(5,-6)

A1=A.transform(Matrix([[3,5,0],[-1,3,0],[0,0,1]]))

B1=B.transform(Matrix([[3,5,0],[-1,3,0],[0,0,1]]))

S=Segment(A1,B1)

a=S.slope

print("Slope of segment is",a)

Output:-

Slope of segment is -3/8

Q3

Program:-

from sympy import*

x,y=symbols('x y')

l=Line(y-x-4)

T=Matrix([[1,-2,0],[3,2,0],[0,0,1]])

points=l.points

p=points[0]

q=points[1]

s=T.inv()

p1=p.transform(s)

q1=q.transform(s)
l1=Line(p1,q1)

print(l1.equation())

Output:-

-3*x/8 - y/8 - ½

Q4

from sympy import*

x,y=symbols('x y')

l=Line(2*x+y-3)

points=l.points

p=points[0]

q=points[1]

M=Matrix([[1,-3,0],[2,1,0],[0,0,1]])

p1=p.transform(M)

q1=q.transform(M)

l1=Line(p1,q1)

print(l1.equation())

Output:-

5*x - 3*y – 21

Q5

Program:-

from sympy import*

x,y=symbols('x y')

A=Point(-2,5)

B=Point(4,3)
A1=A.transform(Matrix([[2,4,0],[2,-1,0],[0,0,1]]))

B1=B.transform(Matrix([[2,4,0],[2,-1,0],[0,0,1]]))

S=Segment(A1,B1)

a=S.midpoint

print("Midpoint of segment is",a)

Output:-

Midpoint of segment is Point2D(10, 0)

Q1.

Program:-

from sympy import *

p1,p2,p3,p4=map(Point,((0,0),(1,0),(2,2),(1,4)))

p=Polygon(p1,p2,p3,p4)

Area=p.area

print(Area)

perimeter=p.perimeter

print(perimeter)

p1=Polygon((2,2),5,n=8)

Area1=p1.area

print(Area)

perimeter1=p1.perimeter

print(perimeter)

OUTPUT:-

1 + sqrt(17) + 2*sqrt(5)

1 + sqrt(17) + 2*sqrt(5)
Q2.

Program:-

from sympy import *

x,y=symbols('x y')

l=Line(y-3)

A,B,C=map(Point,[(1,1),(2,-3),(-1,5)])

p=Polygon(A,B,C)

r=p.reflect(l)

print(r)

OUTPUT:-

Triangle(Point2D(1, 5), Point2D(2, 9), Point2D(-1, 1))

Q3.

Program:-

from sympy import *

A,B,C=map(Point,[(1,1),(1,2),(0,1)])

p=Polygon(A,B,C)

print(p.angles[A])

print(p.angles[B])

print(p.angles[C])

OUTPUT:-

pi/2

pi/4

pi/4

Q4.

Program:-

from sympy import *

A=Point(0,0)
B=Point(-2,0)

C=Point(5,5)

D=Point(1,-6)

p=Polygon(A,B,C,D)

print(p.rotate(pi))

print(p.angles[A])

print(p.angles[B])

print(p.angles[C])

print(p.angles[D])

OUTPUT:-

Polygon(Point2D(0, 0), Point2D(2, 0), Point2D(-5, -5), Point2D(-1, 6))

acos(-sqrt(37)/37)

acos(7*sqrt(74)/74) + 2*pi

acos(83*sqrt(10138)/10138) + 2*pi

acos(62*sqrt(5069)/5069) + 2*pi

Q5.

Program:-

from sympy import *

A=Point(1,-2)

B=Point(4,-6)

C=Point(-1,4)

p=Triangle(A,B,C)

print(p.rotate(pi/2))

print(p.angles[A])

print(p.angles[B])
print(p.angles[C])

OUTPUT:-

Triangle(Point2D(2, 1), Point2D(6, 4), Point2D(-4, -1))

acos(-3*sqrt(10)/10)

acos(11*sqrt(5)/25)

acos(7*sqrt(2)/10)

Q1.

Program:-

from pulp import *

model=LpProblem(name="Small Problem ",sense=LpMaximize)

x=LpVariable(name="x",lowBound=0)

y=LpVariable(name="y",lowBound=0)

model+=(4*x+6*y<=24)

model+=(5*x+3*y<=15)

model+=150*x+75*y

print(model)

print(model.solve())

print("optimal value of objective fun is",model.objective.value())

print("optimal value of x-variable is",x.value())

print("optimal value of y-variable",y.value())

OUTPUT:-

Small_Problem_:

MAXIMIZE

150*x + 75*y + 0

SUBJECT TO
_C1: 4 x + 6 y <= 24

_C2: 5 x + 3 y <= 15

VARIABLES

x Continuous

y Continuous

optimal value of objective fun is 450.0

optimal value of x-variable is 3.0

optimal value of y-variable 0.0

Q2.

Program:-

from pulp import *

model=LpProblem(name="Small Problem ",sense=LpMinimize)

x=LpVariable(name="x",lowBound=0)

y=LpVariable(name="y",lowBound=0)

model+=(x>=6)

model+=(y>=6)

model+=x+y<=11

print(model)

print(model.solve())

OUTPUT:-

Small_Problem_:

MINIMIZE

None

SUBJECT TO

_C1: x >= 6
_C2: y >= 6

_C3: x + y <= 11

VARIABLES

x Continuous

y Continuous

Q3.

Program:-

from pulp import *

model=LpProblem(name="Small Problem ",sense=LpMaximize)

x=LpVariable(name="x",lowBound=0)

y=LpVariable(name="y",lowBound=0)

model+=(x>=1)

model+=(y>=2)

model+=x+y

print(model)

print(model.solve())

OUTPUT:_

Small_Problem_:

MAXIMIZE

1*x + 1*y + 0

SUBJECT TO

_C1: x >= 1

_C2: y >= 2
VARIABLES

x Continuous

y Continuous

-2

You might also like