Python Slip Solution
Python Slip Solution
Python Slip Solution
In [1]: # Write a Python program to plot 2D graph of the functions f(x) = x2 and g
(x) = x3 in [−1,1]
import matplotlib.pyplot as plt
import numpy as np
def f(x):
return x**2
def g(x):
return x**3
x=np.linspace(-1,1,100)
y_f=f(x)
y_g=g(x)
fig,ax=plt.subplots()
ax.plot(x,y_f,label='f(x)=x^2')
ax.plot(x,y_g,label='g(x)=x^3')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
ax.set_title('2D graph of f(x)=x^2 and g(x)=x^3')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip1.html?t=1713114641201 1/6
4/14/24, 10:41 PM slip1
In [8]: # Using python, represent the following information using a bar graph (in g
reen color )
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip1.html?t=1713114641201 2/6
4/14/24, 10:41 PM slip1
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip1.html?t=1713114641201 3/6
4/14/24, 10:41 PM slip1
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip1.html?t=1713114641201 4/6
4/14/24, 10:41 PM slip1
In [13]: # Write a Python program to find the area and perimeter of the ∆ABC, where
A[0,0],B[5,0],C[3,3].
import numpy as np
A = np.array([0, 0])
B = np.array([5, 0])
C = np.array([3, 3])
# Calculate the side lengths of the triangle
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
# Calculate the semiperimeter
s = (AB + BC + CA) / 2
# Calculate the area using Heron's formula
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter
perimeter = AB + BC + CA
# Print the results
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 5.0
Side BC: 3.605551275463989
Side CA: 4.242640687119285
Area: 7.5000000000000036
Perimeter: 12.848191962583275
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip1.html?t=1713114641201 5/6
4/14/24, 10:41 PM slip1
Status: Optimal
Optimal x = 3.0
Optimal y = 0.0
Optimal Z = 450.0
In [3]: # Apply Python program in each of the following transformations on the poin
t 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.
import numpy as np
P = np.array([3,-1])
reflection_x_axis = np.array([1, -1]) * P
scaling_x = np.array([2, 1]) * P
scaling_y = np.array([1, 1.5]) * P
reflection_line = np.array([P[1],P[0]])
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip1.html?t=1713114641201 6/6
4/14/24, 10:41 PM slip2
In [1]: # Write a Python program to plot 2D graph of the functions f(x) = log10(x)
and in [0,10]
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1,10)
# Compute y values using the function f(x) = log10(x)
y = np.log10(x)
# Plot the graph
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x) = log10(x)')
plt.title('2D Graph of f(x) = log10(x)')
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip2.html?t=1713114694182 1/5
4/14/24, 10:41 PM slip2
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip2.html?t=1713114694182 2/5
4/14/24, 10:41 PM slip2
In [4]: # Using sympy declare the points A(0,2),B(5,2),C(3,0) check whether these p
oints are collinear.
# Declare the line passing through the points A and B, find the distance of
this line from point C.
In [1]: # Write a Python program to find the area and perimeter of the ABC, where
# A[0, 0] B[6, 0], C[4,4].
import numpy as np
# Define the vertices of the triangle
A = np.array([0, 0])
B = np.array([6, 0])
C = np.array([4, 4])
# Calculate the side lengths of the triangle
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
# Calculate the semiperimeter
s = (AB + BC + CA) / 2
# Calculate the area using Heron's formula
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter
perimeter = AB + BC + CA
# Print the results
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 6.0
Side BC: 4.47213595499958
Side CA: 5.656854249492381
Area: 11.999999999999998
Perimeter: 16.12899020449196
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip2.html?t=1713114694182 3/5
4/14/24, 10:41 PM slip2
problem += Z
problem += x + y <= 7
problem += 2*x +5* y <= 1
# Solve the LP problem
problem.solve()
# Print the status of the solution
print("Status:", LpStatus[problem.status])
# Print the optimal values of x and y
print("Optimal x =", value(x))
print("Optimal y =", value(y))
# Print the optimal value of the objective function
print("Optimal Z =", value(problem.objective))
Status: Optimal
Optimal x = 0.5
Optimal y = 0.0
Optimal Z = 2.5
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip2.html?t=1713114694182 4/5
4/14/24, 10:41 PM slip2
In [17]: # Apply Python program in each of the following transformations on the poin
t 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
P = np.array([4, -2])
# Reflection through y-axis
reflection_y_axis = np.array([-1, 1]) * P
# Scaling in X-coordinates by factor 3
scaling_x = np.array([3, 1]) * P
# Scaling in Y-coordinates by factor 2.5
scaling_y = np.array([1, 2.5]) * P
# Reflection through the line y = -x
reflection_line = np.array([-P[1],-P[0]])
print("Original point P:", P)
print("Reflection through y-axis:", reflection_y_axis)
print("Scaling in X-coordinates by factor 3:", scaling_x)
print("Scaling in Y-coordinates by factor 2.5:", scaling_y)
print("Reflection through the line y = -x:", reflection_line)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip2.html?t=1713114694182 5/5
4/14/24, 10:42 PM slip3
In [1]: # Using Python plot the graph of function f(x) = cos(x) on the interval [0,
2π].
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,2*np.pi)
y = np.cos(x)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x) = log10(x)')
plt.title('2D Graph of f(x) = log10(x)')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip3.html?t=1713114742434 1/5
4/14/24, 10:42 PM slip3
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip3.html?t=1713114742434 2/5
4/14/24, 10:42 PM slip3
import numpy as np
A = np.array([2, 1])
B = np.array([4, -1])
T = np.array([[1, 2], [2, 1]])
A_transformed = np.dot(T, A)
B_transformed = np.dot(T, B)
# Extract coordinates of transformed points
x1_transformed, y1_transformed = A_transformed
x2_transformed, y2_transformed = B_transformed
In [7]: # Generate line segment having endpoints (0,0) and (10,10) find midpoint of
line segment
x1, y1 = 0, 0
x2, y2 = 10, 10
# Calculate midpoint
midpoint_x = (x1 + x2) / 2
midpoint_y = (y1 + y2) / 2
# Print midpoint
print("Midpoint: ({}, {})".format(midpoint_x, midpoint_y))
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip3.html?t=1713114742434 3/5
4/14/24, 10:42 PM slip3
Z = 3.5 * x + 2 * y
problem+= Z
problem+= x + y >= 5
problem+= x >= 4
problem+= y <= 2
problem.solve()
print("Optimal solution:")
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Optimal solution:
x = 4.0
y = 1.0
Optimal value of Z = 16.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip3.html?t=1713114742434 4/5
4/14/24, 10:42 PM slip3
In [11]: # Apply Python program in each of the following transformations on the poin
t 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
P = np.array([4, -2])
# Reflection through y-axis
reflection_y_axis = np.array([-1, 1]) * P
# Scaling in X-coordinates by factor 3
scaling_x = np.array([3, 1]) * P
# Scaling in Y-coordinates by factor 2.5
scaling_y = np.array([1, 2.5]) * P
# Reflection through the line y = -x
reflection_line = np.array([-P[1],-P[0]])
print("Original point P:", P)
print("Reflection through y-axis:", reflection_y_axis)
print("Scaling in X-coordinates by factor 3:", scaling_x)
print("Scaling in Y-coordinates by factor 2.5:", scaling_y)
print("Reflection through the line y = -x:", reflection_line)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip3.html?t=1713114742434 5/5
4/14/24, 10:43 PM slip4
In [1]: # Write a Python program to plot 2D graph of the functions f(x) = log10(x)
in the [0,10]
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1,10)
# Compute y values using the function f(x) = log10(x)
y = np.log10(x)
# Plot the graph
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x) = log10(x)')
plt.title('2D Graph of f(x) = log10(x)')
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip4.html?t=1713114796841 1/6
4/14/24, 10:43 PM slip4
In [2]: # 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
x = np.linspace(-1,1)
y = 1/np.sin(x)
# Plot the graph
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x) = sin-1(X)')
plt.title('2D Graph of f(x) = log10(x)')
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip4.html?t=1713114796841 2/6
4/14/24, 10:43 PM slip4
import numpy as np
A = np.array([3, 1])
B = np.array([5, -1])
T = np.array([[3, -2], [2, 1]])
A_transformed = np.dot(T, A)
B_transformed = np.dot(T, B)
# Extract coordinates of transformed points
x1_transformed, y1_transformed = A_transformed
x2_transformed, y2_transformed = B_transformed
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip4.html?t=1713114796841 3/6
4/14/24, 10:43 PM slip4
In [2]: # Using python, generate line passing through points (2,3) and (4,3) and fi
nd equation of the line
import matplotlib.pyplot as plt
import numpy as np
# Define the points
x = np.array([2, 4])
y = np.array([3, 3])
# Calculate the slope (m) and y-intercept (b) of the line
m = (y[1] - y[0]) / (x[1] - x[0])
b = y[0] - m * x[0]
# Print the equation of the line
print(f"The equation of the line is: y = {m : .2f}x + {b:.2f}")
# Plot the points and the line
plt.scatter(x, y, c='blue', label='Points')
plt.plot(x, m * x + b, c='red', label='Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Line Passing through Points')
plt.legend()
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip4.html?t=1713114796841 4/6
4/14/24, 10:43 PM slip4
Status: Optimal
Optimal x = 3.0
Optimal y = 0.0
Optimal Z = 450.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip4.html?t=1713114796841 5/6
4/14/24, 10:43 PM slip4
In [4]: # 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
# Input points A and B
A = np.array([4, -1])
B = np.array([3, 0])
# Transformation 1: Shearing in X-Direction by 9 units
shear_matrix = np.array([[1, 9],
[0, 1]])
A_sheared = np.dot(shear_matrix, A)
B_sheared = np.dot(shear_matrix, B)
print("Transformed Point A after Shearing:", A_sheared)
print("Transformed Point B after Shearing:", B_sheared)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip4.html?t=1713114796841 6/6
4/14/24, 10:44 PM slip5
In [1]: # Using Python plot the surface plot of function z = cos (x2 + y2 −0.5)in t
he interval from −1 < x,y < 1
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip5.html?t=1713114852511 1/6
4/14/24, 10:44 PM slip5
In [2]: # Generate 3D surface Plot for the function f(x) = sin (x2 + y2)in the inte
rval [0,10]
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the function
def func(x, y):
return np.sin(x**2 + y**2)
# Generate x, y values in the interval from 0 to 10
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y) # Create a grid of x, y values
Z = func(X, Y) # Compute z values using the function
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis') # Plot the surface
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() # Show the plo
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip5.html?t=1713114852511 2/6
4/14/24, 10:44 PM slip5
In [9]: # Using python, generate triangle with vertices (0,0),(4,0),(4,3) check whe
ther the triangle is Right angle triangle
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip5.html?t=1713114852511 3/6
4/14/24, 10:44 PM slip5
In [10]: # Write a Python program to find the area and perimeter of the ∆ABC, where
A[0,0],B[6,0],C[4,4]
import numpy as np
# Define the vertices of the triangle
A = np.array([0, 0])
B = np.array([6, 0])
C = np.array([4, 4])
# Calculate the side lengths of the triangle
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
# Calculate the semiperimeter
s = (AB + BC + CA) / 2
# Calculate the area using Heron's formula
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter
perimeter = AB + BC + CA
# Print the results
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 6.0
Side BC: 4.47213595499958
Side CA: 5.656854249492381
Area: 11.999999999999998
Perimeter: 16.12899020449196
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip5.html?t=1713114852511 4/6
4/14/24, 10:44 PM slip5
Status: Optimal
Optimal x = 0.5
Optimal y = 0.0
Optimal Z = 2.5
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip5.html?t=1713114852511 5/6
4/14/24, 10:44 PM slip5
In [14]: # Apply Python program in each of the following transformations on the poin
t 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.
import numpy as np
P=np.array([3,8])
# (I) Refection through X−axis.
reflection_x_axis = np.array([1, -1]) * P
# Scaling in X−coordinate by factor 6.
scaling_x = np.array([6, 1]) * P
# Rotation about origin through an angle 30◦.
rotation_matrix = np.array([[np.cos(np.pi/6), -np.sin(np.pi/6)],
[np.sin(np.pi/6), np.cos(np.pi/6)]])
P_rotation = np.dot(rotation_matrix, P)
# Reflection through the line y = -x
reflection_line = np.array([-P[1],-P[0]])
Original point P: [3 8]
Reflection through y-axis: [ 3 -8]
Scaling in X-coordinates by factor 6: [18 8]
Rotation about origin through an angle of 30 degree: [-1.40192379 8.428203
23]
Reflection through the line y = -x: [-8 -3]
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip5.html?t=1713114852511 6/6
4/14/24, 10:45 PM slip6
In [1]: # Using python, generate 3D surface Plot for the function f(x) = sin (x2 +
y2)in the interval [0,10]
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the function
def func(x, y):
return np.sin(x**2 + y**2)
# Generate x, y values in the interval from 0 to 10
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y) # Create a grid of x, y values
Z = func(X, Y) # Compute z values using the function
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z) # Plot the surface
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()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip6.html?t=1713114923764 1/7
4/14/24, 10:45 PM slip6
In [4]: # Draw the horizontal bar graph for the following data in Maroon colour.
# City Pune Mumbai Nasik Nagpur Thane
# Air Quality Index 168 190 170 178 195
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip6.html?t=1713114923764 2/7
4/14/24, 10:45 PM slip6
In [5]: # 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
# Define the original line segment
x1, y1 = 1, 0
x2, y2 = 2, -1
# Plot the original line segment
plt.plot([x1, x2], [y1, y2], 'bo-', label='Original Line')
# Compute the rotated coordinates
x1_rot, y1_rot = -x1, -y1
x2_rot, y2_rot = -x2, -y2
# Plot the rotated line segment
plt.plot([x1_rot, x2_rot], [y1_rot, y2_rot], 'ro-', label='Rotated Line')
# Set axis limits
plt.xlim(-3, 3)
plt.ylim(-3, 3)
# Add labels and title
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Rotation of Line Segment by 180 degrees')
# Add legend
plt.legend()
# Show the plot
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip6.html?t=1713114923764 3/7
4/14/24, 10:45 PM slip6
import numpy as np
import matplotlib.pyplot as plt
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip6.html?t=1713114923764 4/7
4/14/24, 10:45 PM slip6
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip6.html?t=1713114923764 5/7
4/14/24, 10:45 PM slip6
Status: Unbounded
Optimal x = 0.0
Optimal y = 0.0
Optimal Z = 0.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip6.html?t=1713114923764 6/7
4/14/24, 10:45 PM slip6
In [1]: # Apply Python program in each of the following transformations on the poin
t P[4,−2]
# (I) Refection through Y−axis.
# (II) Scaling in X−coordinate by factor 7.
# (III) Shearing in Y direction by 3 units
# (IV) Reflection through the line y = −x.
import numpy as np
P = [4, -2]
# Reflection through y-axis
reflection_y_axis = np.array([-1, 1]) * P
# Scaling in X-coordinates by factor 7
scaling_x = np.array([7, 1]) * P
# Shearing in Y-direction by 3 units
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip6.html?t=1713114923764 7/7
4/14/24, 10:47 PM slip7
In [4]: # Plot the graph of f(x) = x4 in [0,5] with red dashed line with circle mar
kers.
import numpy as np
import matplotlib.pyplot as plt
# Define the function f(x) = x**4
def f(x):
return x**4
# Generate x values in the interval [0, 5]
x = np.linspace(0, 5,100)
# Generate y values using the function f(x)
y = f(x)
# Plot the graph with red dashed line and circle markers
plt.plot(x, y, 'r--o', markersize=6)
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = x**4')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip7.html?t=1713115006962 1/6
4/14/24, 10:47 PM slip7
In [6]: # Using python, generate 3D surface Plot for the function f(x) = sin (x2 +
y2)in the interval [0,10].
# also in slip 6 q1
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the function
def func(x, y):
return np.sin(x**2 + y**2)
# Generate x, y values in the interval from 0 to 10
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y) # Create a grid of x, y values
Z = func(X, Y) # Compute z values using the function
# Create a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z) # Plot the surface
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()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip7.html?t=1713115006962 2/6
4/14/24, 10:47 PM slip7
In [9]: # Write a Python program to reflect the line segment joining the points A
[5,3] & B[1,4] through the line y = x + 1
A = np.array([5, 3])
B = np.array([1, 4])
# Define the equation of the reflection line
reflection_line = lambda x: x + 1
# Plot the original line segment AB
plt.plot([A[0], B[0]], [A[1], B[1]], '-o', label='Original Line Segment A
B')
# Plot the reflection line
x_vals = np.linspace(-5, 5, 100) # Generate x values for the plot
plt.plot(x_vals, reflection_line(x_vals), '-r', label='Reflection Line y =
x + 1')
# Calculate the reflected points
reflected_A = np.array([reflection_line(A[0]), A[0]])
reflected_B = np.array([reflection_line(B[0]), B[0]])
# Plot the reflected line segment A'B'
plt.plot([reflected_A[0], reflected_B[0]], [reflected_A[1], reflected_B
[1]], '-o', label='Reflected Line Segment A\'B\'')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Line Segment Reflection')
plt.legend()
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip7.html?t=1713115006962 3/6
4/14/24, 10:47 PM slip7
In [10]: # 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 th
is ray between P and Q.Also find slope of this ray.
P = Point(5, 2)
Q = Point(5, -2)
R = Point(5, 0)
# Check if points P, Q, and R are collinear
line_PQ = Line(P, Q)
line_PR = Line(P, R)
collinear = line_PQ.is_parallel(line_PR)
# Print the result
if collinear:
print("Points P, Q, and R are collinear")
else:
print("Points P, Q, and R are not collinear")
# Calculate the length of the ray PQ
length_PQ = P.distance(Q)
# Calculate the slope of the ray PQ
slope_PQ = (Q.y - P.y) / (Q.x - P.x)
# Print the length and slope of the ray PQ
print("Length of the ray PQ:", length_PQ)
print("Slope of the ray PQ:", slope_PQ)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip7.html?t=1713115006962 4/6
4/14/24, 10:47 PM slip7
Z = 3.5 * x + 2 * y
problem+= Z
problem+= x + y >= 5
problem+= x >= 4
problem+= y <= 2
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Optimal
x = 4.0
y = 1.0
Optimal value of Z = 16.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip7.html?t=1713115006962 5/6
4/14/24, 10:47 PM slip7
In [1]: # Apply Python program in each of the following transformations on the poin
t 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 7 / 2 units
import numpy as np
P=np.array([4,-2])
# (I) Refection through Y−axis.
reflection_y_axis = np.array([-1, 1]) * P
# (II) Scaling in X−coordinate by factor 5.
scaling_x = np.array([5, 1]) * P
# (III) Rotation about origin through an angle π/2 .
rotation_matrix = np.array([[np.cos(np.pi/2), -np.sin(np.pi/2)],
[np.sin(np.pi/2), np.cos(np.pi/2)]])
P_rotation = np.dot(rotation_matrix, P)
# (IV) Shearing in X direction by 7 / 2 units
shearing_x = [P[0]+((7/2)*P[1]),P[1]]
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip7.html?t=1713115006962 6/6
4/14/24, 10:47 PM slip8
In [1]: # 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
# Generate x values
x = np.linspace(0, 2 * np.pi, 500)
y = np.cos(x)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x) = cos(x)')
plt.title('Graph of f(x) = cos(x)')
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip8.html?t=1713115049842 1/6
4/14/24, 10:47 PM slip8
X, Y = np.meshgrid(x, y)
# Compute z values for the function z = sin(x) + cos(y)
Z = np.sin(X) + np.cos(Y)
# Create a 3D plot
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('3D Plot of z = sin(x) + cos(y)')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip8.html?t=1713115049842 2/6
4/14/24, 10:47 PM slip8
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip8.html?t=1713115049842 3/6
4/14/24, 10:47 PM slip8
In [10]: # Write a Python program to find the area and perimeter of the ∆ABC, where
A[0,0],B[6,0],C[4,4]
import numpy as np
# Define the vertices of the triangle
A = np.array([0, 0])
B = np.array([6, 0])
C = np.array([4, 4])
# Calculate the side lengths of the triangle
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
# Calculate the semiperimeter
s = (AB + BC + CA) / 2
# Calculate the area using Heron's formula
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter
perimeter = AB + BC + CA
# Print the results
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 6.0
Side BC: 4.47213595499958
Side CA: 5.656854249492381
Area: 11.999999999999998
Perimeter: 16.12899020449196
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip8.html?t=1713115049842 4/6
4/14/24, 10:47 PM slip8
Status: Optimal
Optimal x = 3.0
Optimal y = 0.0
Optimal Z = 450.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip8.html?t=1713115049842 5/6
4/14/24, 10:47 PM slip8
In [16]: # Apply Python program in each of the following transformations on the poin
t P[4,−2]
# (I) Refection through Y−axis.
# (II) Scaling in X−coordinate by factor 3.
# (III) Rotation about origin through an angle π.
# (IV) Shearing in both X and Y direction by −2 and 4 units respectively
import numpy as np
P=np.array([4,-2])
# (I) Refection through Y−axis.
reflection_y_axis = np.array([-1, 1]) * P
# (II) Scaling in X−coordinate by factor 3.
scaling_x = np.array([3, 1]) * P
# (III) Rotation about origin through an angle π .
rotation_matrix = np.array([[np.cos(np.pi), -np.sin(np.pi)],
[np.sin(np.pi), np.cos(np.pi)]])
P_rotation = np.dot(rotation_matrix, P)
# (IV) Shearing in both X and Y direction by −2 and 4 units
P_sheared = np.array([P[0] + (-2 * P[1]), P[1] + (4 * P[0])])
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip8.html?t=1713115049842 6/6
4/14/24, 10:48 PM slip9
In [1]: # Write a python program to Plot 2D X-axis and Y-axis black color and in th
e same diagram plot green triangle with vertices [5,4],[7,4],[6,6].
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip9.html?t=1713115110640 1/5
4/14/24, 10:48 PM slip9
In [5]: # Using Python plot the graph of function f(x) = cos(x) on the interval [0,
2π].
import numpy as np
import matplotlib.pyplot as plt
# Generate x values
x = np.linspace(0, 2 * np.pi, 500)
y = np.cos(x)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('f(x) = cos(x)')
plt.title('Graph of f(x) = cos(x)')
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip9.html?t=1713115110640 2/5
4/14/24, 10:48 PM slip9
In [6]: # Using sympy, declare the points A(0,7),B(5,2). Declare the line segment p
assing through them.
# Find the length and midpoint of the line segment passing through points A
and B
A = Point(0, 7)
B = Point(5, 2)
# Declare the line passing through points A and B
line_AB = Line(A, B)
# Calculate the length of the line segment AB
length_AB = A.distance(B)
# Calculate the midpoint of the line segment AB
midpoint_AB = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
# Print the results
print("Point A: {}".format(A))
print("Point B: {}".format(B))
print("Line segment AB: {}".format(line_AB))
print("Length of line segment AB: {}".format(length_AB))
print("Midpoint of line segment AB: {}".format(midpoint_AB))
Point A: Point2D(0, 7)
Point B: Point2D(5, 2)
Line segment AB: Line2D(Point2D(0, 7), Point2D(5, 2))
Length of line segment AB: 5*sqrt(2)
Midpoint of line segment AB: (5/2, 9/2)
In [7]: # Write a python program to find the area and perimeter of ∆ABC where A(0,
0),B(5,0),C(3,3)
import numpy as np
A = np.array([0, 0])
B = np.array([5, 0])
C = np.array([3, 3])
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
s = (AB + BC + CA) / 2
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
perimeter = AB + BC + CA
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 5.0
Side BC: 3.605551275463989
Side CA: 4.242640687119285
Area: 7.5000000000000036
Perimeter: 12.848191962583275
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip9.html?t=1713115110640 3/5
4/14/24, 10:48 PM slip9
Status: Optimal
Optimal x = 3.0
Optimal y = 0.0
Optimal Z = 450.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip9.html?t=1713115110640 4/5
4/14/24, 10:48 PM slip9
In [10]: # Write a python program to apply the following transformations on the poin
t (−2,4) :
# (I) Shearing in Y direction by 7 units.
# (II) Scaling in X and Y direction by 7/2 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 numpy as np
P=np.array([-2,4])
# (I) Shearing in Y direction by 7 units.
shearing_y1 = [P[0], P[1] + (7 * P[0])]
# (II) Scaling in X and Y direction by 7/2 and 7
scaling_x = np.array([7/2, 1]) * P
scaling_y = np.array([1, 7]) * P
# (III) Shearing in X and Y direction by 4 and 7 units respectively.
shearing_x = [P[0] + (4 * P[1]), P[1]]
shearing_y = [P[0], P[1] + (7 * P[0])]
# (IV) Rotation about origin by an angle 60◦.
rotation_matrix = np.array([[np.cos(np.pi/3), -np.sin(np.pi/3)],
[np.sin(np.pi/3), np.cos(np.pi/3)]])
P_rotation = np.dot(rotation_matrix, P)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip9.html?t=1713115110640 5/5
4/14/24, 10:49 PM slip10
In [3]: # Represent the following information using a bar graph (in green color )
# Item clothing Food rent Petrol Misc.
# expenditure in Rs 600 4000 2000 1500 700
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip10.html?t=1713115161889 1/5
4/14/24, 10:49 PM slip10
In [4]: # Write a python program to plot the 3D line graph whose parametric equatio
n 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
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip10.html?t=1713115161889 2/5
4/14/24, 10:49 PM slip10
In [5]: # Write a python program to rotate the ∆ABC by 90◦ where A(1,1),B(2,−2),C
(1,2)
import numpy as np
# Define the original points
A = np.array([1, 1])
B = np.array([2, -2])
C = np.array([1, 2])
In [6]: # Find the area and perimeter of the ∆ABC, where A[0,0],B[5,0],C[3,3]
import numpy as np
A = np.array([0, 0])
B = np.array([5, 0])
C = np.array([3, 3])
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
s = (AB + BC + CA) / 2
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
perimeter = AB + BC + CA
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 5.0
Side BC: 3.605551275463989
Side CA: 4.242640687119285
Area: 7.5000000000000036
Perimeter: 12.848191962583275
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip10.html?t=1713115161889 3/5
4/14/24, 10:49 PM slip10
Status: Unbounded
Optimal x = 0.0
Optimal y = 0.0
Optimal Z = 0.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip10.html?t=1713115161889 4/5
4/14/24, 10:49 PM slip10
In [8]: # Write a python program to apply the following transformations on the poin
t (−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 numpy as np
P=np.array([-2,4])
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip10.html?t=1713115161889 5/5
4/14/24, 10:50 PM slip11
In [1]: # Plot the graph of y = e−x2 in [−5,5] with red dashed-points line with Upw
ard Pointing triangle
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip11.html?t=1713115212528 1/5
4/14/24, 10:50 PM slip11
In [3]: # 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
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip11.html?t=1713115212528 2/5
4/14/24, 10:50 PM slip11
In [4]: # Write a python program to rotate the ∆ABC by 90◦ where A(1,2),B(2,−2),C
(−1,2)
import numpy as np
A = np.array([1, 2])
B = np.array([2, -2])
C = np.array([-1, 2])
In [5]: #Write a python program to reflect the ∆ABC through the line y = 3 where A
(1,0),B(2,−1),C(−1,3)
import numpy as np
# Define the reflection line y = 3
reflection_line = 3
# Define the points A, B, and C
A = np.array([1, 0])
B = np.array([2, -2])
C = np.array([-1, 2])
# Compute the reflected points A', B', and C'
Ap = np.array([A[0], 2 * reflection_line - A[1]])
Bp = np.array([B[0], 2 * reflection_line - B[1]])
Cp = np.array([C[0], 2 * reflection_line - C[1]])
# Print the original points and reflected points
print("Original Points:")
print("A: ", A)
print("B: ", B)
print("C: ", C)
print("Reflected Points:")
print("A':", Ap)
print("B':", Bp)
print("C':", Cp)
Original Points:
A: [1 0]
B: [ 2 -2]
C: [-1 2]
Reflected Points:
A': [1 6]
B': [2 8]
C': [-1 4]
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip11.html?t=1713115212528 3/5
4/14/24, 10:50 PM slip11
Z = x + y
problem+= Z
problem+= x >= 6
problem+= y >= 6
problem+= x + y >=11
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Optimal
x = 6.0
y = 6.0
Optimal value of Z = 12.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip11.html?t=1713115212528 4/5
4/14/24, 10:50 PM slip11
In [1]: # Write a python program to apply the following transformations on the poin
t (−2,4) :
# (I) Shearing in Y direction by 7 units.
# (II) Scaling in X and Y direction by 3/2 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 numpy as np
P=np.array([-2,4])
# (I) Shearing in Y direction by 7 units
shearing_y1 = [P[0], P[1] + (7 * P[0])]
# (II) Scaling in X and Y direction by 3/2 and 4
P_scaled = np.array([P[0] * 3/2, P[1] * 4])
# (III) Shearing in X and Y direction by 2 and 4 units respectively.
P_sheared = np.array([P[0] + (2 * P[1]), P[1] + (4 * P[0])])
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip11.html?t=1713115212528 5/5
4/14/24, 10:52 PM slip12
In [3]: # 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
plt.plot(x, y, color='red')
# Set the plot title and axis labels
plt.title("Graph of y = x**3 + 10*x - 5")
plt.xlabel("x")
plt.ylabel("y")
# Show the plot
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip12.html?t=1713115263347 1/5
4/14/24, 10:52 PM slip12
In [5]: # Using Python plot the graph of function f(x) = x2 on the interval [−2,2]
import numpy as np
import matplotlib.pyplot as plt
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip12.html?t=1713115263347 2/5
4/14/24, 10:52 PM slip12
In [6]: # Write a python program to rotate the segment by 180◦ having end points
(1,0) and (2,−1).
import math
# Define the endpoints of the line segment
x1, y1 = 1, 0
x2, y2 = 2, -1
# Perform the rotation
x1_rotated = -x1
y1_rotated = -y1
x2_rotated = -x2
y2_rotated = -y2
# Print the original and rotated endpoints
print("Original Endpoint 1: ({}, {})".format(x1, y1))
print("Original Endpoint 2: ({}, {})".format(x2, y2))
print("Rotated Endpoint 1: ({}, {})".format(x1_rotated, y1_rotated))
print("Rotated Endpoint 2: ({}, {})".format(x2_rotated, y2_rotated))
In [8]: # 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
# Input coordinates
X = [1, 2]
Y = [2, -2]
Z = [-1, 2]
# Calculate distances between points
def distance(p1, p2):
return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
# Calculate lengths of sides
XY = distance(X, Y)
YZ = distance(Y, Z)
XZ = distance(X, Z)
# Calculate perimeter
perimeter = XY + YZ + XZ
# Calculate area using Heron's formula
s = perimeter / 2
area = math.sqrt(s * (s - XY) * (s - YZ) * (s - XZ))
# Print results
print("Length of XY: ", XY)
print("Length of YZ: ", YZ)
print("Length of XZ: ", XZ)
print("Perimeter: ", perimeter)
print("Area: ", area)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip12.html?t=1713115263347 3/5
4/14/24, 10:52 PM slip12
Z = 3.5 * x + 2 * y
problem+= Z
problem+= x + y >= 5
problem+= x >= 4
problem+= y <= 2
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Optimal
x = 4.0
y = 1.0
Optimal value of Z = 16.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip12.html?t=1713115263347 4/5
4/14/24, 10:52 PM slip12
In [12]: # Write a python program to apply the following transformations on the poin
t [−2,4]
# (I) Refection through Y− axis.
# (II) Scaling in X−coordinate by factor 6.
# (III) Scaling in Y−coordinate by factor 4.1.
# (IV) Shearing in X direction by 7/2 units.
import numpy as np
P=np.array([-2,4])
# (I) Refection through Y− axis.
reflection_y_axis = np.array([-1, 1]) * P
# (II) Scaling in X−coordinate by factor 6
scaling_x = np.array([6, 1]) * P
# (III) Scaling in Y−coordinate by factor 4.1
scaling_y = np.array([1, 4.1]) * P
# (IV) Shearing in X direction by 7/2 units.
shearing_x = [P[0] + ((7/2) * P[1]), P[1]]
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip12.html?t=1713115263347 5/5
4/14/24, 10:53 PM slip13
In [1]: # Write a Python program to plot 2D graph of the functions f(x) = x2 and g
(x) = x3 in [−1,1]
def f(x):
return x**2
def g(x):
return x**3
x=np.linspace(-1,1,100)
y_f=f(x)
y_g=g(x)
fig,ax=plt.subplots()
ax.plot(x,y_f,label='f(x)=x^2')
ax.plot(x,y_g,label='g(x)=x^3')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
ax.set_title('2D graph of f(x)=x^2 and g(x)=x^3')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip13.html?t=1713115362297 1/5
4/14/24, 10:53 PM slip13
In [2]: # 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
# Generate values for x and y
x = np.linspace(-6, 6, 100)
y = np.linspace(-6, 6, 100)
X, Y = np.meshgrid(x, y)
# Calculate values for z based on the parabola equation
Z = X**2 + Y**2
# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the surface plot
surf = 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 = x**2 + y**2')
# Show the plot
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip13.html?t=1713115362297 2/5
4/14/24, 10:53 PM slip13
In [4]: # 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 th
is ray between P and Q. Also find slope of this ray
In [5]: # Find the area and perimeter of the ∆ABC, where A[0,0],B[5,0],C[3,3].
import numpy as np
A = np.array([0, 0])
B = np.array([5, 0])
C = np.array([3, 3])
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
# Calculate the semiperimeter
s = (AB + BC + CA) / 2
# Calculate the area using Heron's formula
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter
perimeter = AB + BC + CA
# Print the results
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 5.0
Side BC: 3.605551275463989
Side CA: 4.242640687119285
Area: 7.5000000000000036
Perimeter: 12.848191962583275
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip13.html?t=1713115362297 3/5
4/14/24, 10:53 PM slip13
Status: Optimal
Optimal x = 0.5
Optimal y = 0.0
Optimal Z = 2.5
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip13.html?t=1713115362297 4/5
4/14/24, 10:53 PM slip13
In [4]: # Write a python program to apply the following transformations on the poin
t (−2,4) :
# (I) Shearing in Y direction by 7 units.
# (II) Scaling in X and Y direction by 7/2 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 numpy as np
P=np.array([-2,4])
# (I) Shearing in Y direction by 7 units.
shearing_y1 = [P[0], P[1] + (7 * P[0])]
# (II) Scaling in X and Y direction by 7/2 and 7
P_scaled = np.array([P[0] * 7/2, P[1] * 7])
# (III) Shearing in X and Y direction by 4 and 7 units respectively.
P_sheared = np.array([P[0] + (4 * P[1]), P[1] + (7 * P[0])])
# (IV) Rotation about origin by an angle 60◦.
rotation_matrix = np.array([[np.cos(np.pi/3), -np.sin(np.pi/3)],
[np.sin(np.pi/3), np.cos(np.pi/3)]])
P_rotation = np.dot(rotation_matrix, P)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip13.html?t=1713115362297 5/5
4/14/24, 10:54 PM slip14
In [1]: # Write a Python program to plot 2D graph of the functions f(x) = x2 and g
(x) = x3 in [−1,1]
import matplotlib.pyplot as plt
import numpy as np
def f(x):
return x**2
def g(x):
return x**3
x=np.linspace(-1,1,100)
y_f=f(x)
y_g=g(x)
fig,ax=plt.subplots()
ax.plot(x,y_f,label='f(x)=x^2')
ax.plot(x,y_g,label='g(x)=x^3')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()
ax.set_title('2D graph of f(x)=x^2 and g(x)=x^3')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip14.html?t=1713115417322 1/6
4/14/24, 10:54 PM slip14
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)
# Create 3D plot
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 Plot of z = sin(x) + cos(y)')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip14.html?t=1713115417322 2/6
4/14/24, 10:54 PM slip14
plt.figure()
plt.plot(vertices[:, 0], vertices[:, 1], 'bo-')
plt.title('Original Polygon')
plt.xlabel('X')
plt.ylabel('Y')
# Define the rotation matrix for 180 degrees
theta = np.pi # 180 degrees
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
# Apply rotation to the vertices
vertices_rotated = np.dot(vertices, rotation_matrix)
# Plot the rotated polygon
plt.figure()
plt.plot(vertices_rotated[:, 0], vertices_rotated[:, 1], 'ro-')
plt.title('Rotated Polygon (180 degrees)')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip14.html?t=1713115417322 3/6
4/14/24, 10:54 PM slip14
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip14.html?t=1713115417322 4/6
4/14/24, 10:54 PM slip14
In [6]: # Write a Python program to find the area and perimeter of the triangle AB
C, where A[0,0],B[5,0] and C[3,3].
import numpy as np
A = np.array([0, 0])
B = np.array([5, 0])
C = np.array([3, 3])
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
# Calculate the semiperimeter
s = (AB + BC + CA) / 2
# Calculate the area using Heron's formula
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
# Calculate the perimeter
perimeter = AB + BC + CA
# Print the results
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 5.0
Side BC: 3.605551275463989
Side CA: 4.242640687119285
Area: 7.5000000000000036
Perimeter: 12.848191962583275
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip14.html?t=1713115417322 5/6
4/14/24, 10:54 PM slip14
Status: Optimal
Optimal x = 3.0
Optimal y = 0.0
Optimal Z = 450.0
import numpy as np
P = np.array([2,-3])
reflection_x_axis = np.array([1, -1]) * P
scaling_x = np.array([2, 1]) * P
scaling_y = np.array([1, 1.5]) * P
reflection_line = np.array([P[1],P[0]])
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip14.html?t=1713115417322 6/6
4/14/24, 10:55 PM slip15
In [1]: # Write the Python program to find area of the triangle ABC, where A[0,0],B
[5,0],C[3,3]
import math
def calculate_area(x1, y1, x2, y2, x3, y3):
area = abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2)
return area
# Coordinates of vertices A, B, and C
Ax, Ay = 0, 0
Bx, By = 5, 0
Cx, Cy = 3, 3
# Call the function to calculate the area
area = calculate_area(Ax, Ay, Bx, By, Cx, Cy)
# Print the result
print("Area of triangle ABC is:", area)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip15.html?t=1713115479465 1/6
4/14/24, 10:55 PM slip15
In [2]: # Write the Python program to plot the graph of the function, using def()
# f(x) = x2 + 4 if −10 ≤x < 5
# 3x + 9 if 5 ≤x < 10
import numpy as np
import matplotlib.pyplot as plt
def f(x):
if -10 < x < 5:
return x**2 + 4
elif 5 <= x:
return 3*x + 9
else:
return None
# Generate x values
# Generate 500 points between -11 and 11
x = np.linspace(-11, 11, 500)
# Calculate y values using f(x)
y = np.array([f(xi) for xi in x])
# Create the plot
plt.plot(x, y, label='f(x)')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x)')
plt.legend()
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip15.html?t=1713115479465 2/6
4/14/24, 10:55 PM slip15
In [1]: # Write the Python program to rotate the triangle ABC by 180 degree, where
A[1,2],B[2,−2] & C[−1,2]
import numpy as np
# Define the original triangle vertices
A = np.array([1,2])
B = np.array([2, -2])
C = np.array([-1, 2])
# Define the rotation matrix for 180 degrees
angle = np.radians(180)
rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
# Rotate the triangle vertices using the rotation matrix
A_rotated = np.dot(rotation_matrix, A)
B_rotated = np.dot(rotation_matrix, B)
C_rotated = np.dot(rotation_matrix, C)
# Print the rotated triangle vertices
print("Original Triangle Vertices:")
print("A:", A)
print("B:", B)
print("C:", C)
print("Rotated Triangle Vertices:")
print("A Rotated:", A_rotated)
print("B Rotated:", B_rotated)
print("C Rotated:", C_rotated)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip15.html?t=1713115479465 3/6
4/14/24, 10:55 PM slip15
In [4]: # Write the Python program to plot the graph of function f(x) = ex in the i
nterval [−10,10].
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.exp(x)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip15.html?t=1713115479465 4/6
4/14/24, 10:55 PM slip15
Z = 3.5 * x + 2 * y
problem+= Z
problem+= x + y >= 5
problem+= x >= 4
problem+= y <= 2
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Optimal
x = 4.0
y = 1.0
Optimal value of Z = 16.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip15.html?t=1713115479465 5/6
4/14/24, 10:55 PM slip15
import numpy as np
P=np.array([-2,4])
print("Original Point:")
print("Point P: ({}, {})".format(P[0], P[1]))
# Transformation I: Reflection through the line y = x + 1
reflection_matrix = np.array([[0, 1], [1,0]])
P_reflection = np.dot(reflection_matrix, P)
print("\nPoint after Reflection:")
print("Point P: ({}, {})".format(P_reflection[0], P_reflection[1]))
Original Point:
Point P: (-2, 4)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip15.html?t=1713115479465 6/6
4/14/24, 10:56 PM slip16
In [1]: # Write a Python program to plot graph of the function f(x,y) = –x2 −y2 whe
n −10 ≤x,y ≤10.
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip16.html?t=1713115563802 1/5
4/14/24, 10:56 PM slip16
In [2]: # Write a Python program to generate plot of the function f(x) = x2, in the
interval [−5,5], in figure of size 6 ×6 inches
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip16.html?t=1713115563802 2/5
4/14/24, 10:56 PM slip16
vertices = np.array([[0, 0], [2, 0], [2, 3], [1, 6], [0, 0]])
# Plot the original polygon
plt.plot(vertices[:, 0], vertices[:, 1], label='Original Polygon')
# Define the rotation angle in degrees
rotation_angle = 90
# Convert the rotation angle to radians
theta = np.radians(rotation_angle)
# Create the rotation matrix
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta),
np.cos(theta)]])
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip16.html?t=1713115563802 3/5
4/14/24, 10:56 PM slip16
In [4]: # Write a Python program to Generate vector x in the interval [0,15] using
numpy package with 100 subintervals
import numpy as np
start = 0
end = 15
# Define the number of subintervals
num_subintervals = 100
# Generate the vector x with equally spaced values in the interval [0, 15]
x = np.linspace(start, end, num=num_subintervals+1)
# Print the generated vector x
print("Generated vector x:")
print(x)
Generated vector x:
[ 0. 0.15 0.3 0.45 0.6 0.75 0.9 1.05 1.2 1.35 1.5 1.65
1.8 1.95 2.1 2.25 2.4 2.55 2.7 2.85 3. 3.15 3.3 3.45
3.6 3.75 3.9 4.05 4.2 4.35 4.5 4.65 4.8 4.95 5.1 5.25
5.4 5.55 5.7 5.85 6. 6.15 6.3 6.45 6.6 6.75 6.9 7.05
7.2 7.35 7.5 7.65 7.8 7.95 8.1 8.25 8.4 8.55 8.7 8.85
9. 9.15 9.3 9.45 9.6 9.75 9.9 10.05 10.2 10.35 10.5 10.65
10.8 10.95 11.1 11.25 11.4 11.55 11.7 11.85 12. 12.15 12.3 12.45
12.6 12.75 12.9 13.05 13.2 13.35 13.5 13.65 13.8 13.95 14.1 14.25
14.4 14.55 14.7 14.85 15. ]
Z = 3.5 * x + 2 * y
problem+= Z
problem+= x + y >= 5
problem+= x >= 4
problem+= y <= 2
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Optimal
x = 4.0
y = 1.0
Optimal value of Z = 16.0
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip16.html?t=1713115563802 4/5
4/14/24, 10:56 PM slip16
In [7]: # Write a python program to plot the Triangle with vertices at [4,3],[6,3],
[6,5]. and its reflec-tions through, 1) x-axis, 2) y-axis. All the figures
must be in different colors, also plot the two axes
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip16.html?t=1713115563802 5/5
4/14/24, 10:57 PM slip17
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip17.html?t=1713115606968 1/6
4/14/24, 10:57 PM slip17
In [2]: # Write a python program to plot 3D contours for the function f(x,y) = log
(x2y2) when −5 ≤x,y ≤5, with greens color map.
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
# Compute the values of f(x, y)
Z = np.log(X**2 * Y**2)
# Create a 3D contour plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.contour(X, Y, Z, cmap='Greens')
# Set labels and title
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('f(x, y)')
ax.set_title('3D Contour Plot of f(x, y) = log(x^2 * y^2)')
# Show the plot
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip17.html?t=1713115606968 2/6
4/14/24, 10:57 PM slip17
In [3]: # Write a python program to rotate the line segment by 180 degrees having e
nd points (1,0) and (2,−1).
A = np.array([1, 0])
B = np.array([2, -1])
# Find the midpoint of the line segment
midpoint = (A + B) / 2
# Define the rotation matrix for 180 degrees
rotation_matrix = np.array([[-1, 0],[0, -1]])
# Rotate the end points of the line segment around the midpoint
A_rotated = np.dot(rotation_matrix, A - midpoint) + midpoint
B_rotated = np.dot(rotation_matrix, B - midpoint) + midpoint
# Plot the original line segment and its rotated version
fig, ax = plt.subplots()
ax.legend()
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_title('Rotation of Line Segment AB by 180 Degrees around Midpoint')
plt.axis('equal')
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip17.html?t=1713115606968 3/6
4/14/24, 10:57 PM slip17
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip17.html?t=1713115606968 4/6
4/14/24, 10:57 PM slip17
Status: Unbounded
Optimal Values:
x = 0.83333333
y = 0.0
z = 0.0
w = 5.8333333
Optimal Objective Function Value = 32.49999982
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip17.html?t=1713115606968 5/6
4/14/24, 10:57 PM slip17
import numpy as np
P = np.array([2, -3])
# (I) Reflection through X-axis
reflection_X = np.array([[1, 0],[0, -1]])
P_reflection_X = np.dot(reflection_X, P)
# (II) Scaling in Y-coordinate by factor 1.5
scaling_Y = np.array([[1, 0],[0, 1.5]])
P_scaling_Y = np.dot(scaling_Y, P)
# (III) Shearing in both X and Y direction by -2 and 4 units respectively
P_sheared = np.array([P[0] + (-2 * P[1]), P[1] + (4 * P[0])])
# (IV) Rotation about origin by an angle of 30 degrees
angle = np.radians(30)
rotation = np.array([[np.cos(angle), -np.sin(angle)],[np.sin(angle),
np.cos(angle)]])
P_rotation = np.dot(rotation, P)
# Print the results
print("Original Point P:", P)
print("Result after reflection through X-axis:", P_reflection_X)
print("Result after scaling in Y-coordinate by factor 1.5:", P_scaling_Y)
print("Result after shearing in both X and Y direction by -2 and
4 units respectively:", P_sheared)
print("Result after rotation about origin by an angle of 30 degrees:", P_ro
tation)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/82edebd97348fac70cd1030cd2a9b7c7/slip17.html?t=1713115606968 6/6
4/14/24, 11:01 PM slip18
In [2]: # Write a Python program to plot the graph 2x2 −4x + 5 in [–10,10] in magen
ta colored dashed pattern
import numpy as np
import matplotlib.pyplot as plt
# Generate x values
x = np.linspace(-10, 10, 400)
# Generate y values
y = f(x)
plt.legend()
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip18.html?t=1713115836845 1/5
4/14/24, 11:01 PM slip18
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
# Create a grid of x, y values
X, Y = np.meshgrid(x, y)
# Compute the corresponding z values using the function z = x^2 + y^2
Z = X**2 + Y**2
# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the surface
ax.plot_surface(X, Y, Z, cmap='viridis')
# Set labels for x, y, and z axes
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
# Set title
ax.set_title('3D Plot of z = x^2 + y^2')
# Show the plot
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip18.html?t=1713115836845 2/5
4/14/24, 11:01 PM slip18
In [9]: # Write a Python program to generate vector x in the interval [−22,22] usin
g numpy package with 80 subintervals
import numpy as np
start = -22
end = 22
num_subintervals = 80
[-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 -0.55
0. 0.55 1.1 1.65 2.2 2.75 3.3 3.85 4.4 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. ]
In [11]: # 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
A = np.array([1, 2])
B = np.array([2, -2])
C = np.array([-1, 2])
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip18.html?t=1713115836845 3/5
4/14/24, 11:01 PM slip18
Z = x + y
problem+= Z
problem+= x >= 6
problem+= y >= 6
problem+= x + y <=11
problem.solve()
print("Status:", LpStatus[problem.status])
if problem.status==LpStatusOptimal:
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Infeasible
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip18.html?t=1713115836845 4/5
4/14/24, 11:01 PM slip18
import numpy as np
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip18.html?t=1713115836845 5/5
4/14/24, 11:02 PM slip19
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 1/8
4/14/24, 11:02 PM slip19
In [9]: # 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
# Create subplots
plt.figure(figsize=(10, 8))
plt.subplot(2, 2, 1)
plt.plot(x, y1, 'r')
plt.title('sin(x)')
plt.subplot(2, 2, 2)
plt.plot(x, y2, 'g')
plt.title('exp(x)')
plt.subplot(2, 2, 3)
plt.plot(x, y3, 'b')
plt.title('x^3')
# Adjustbspacing between subplots
plt.tight_layout()
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 2/8
4/14/24, 11:02 PM slip19
In [10]: # 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 differ
ent colors to the curve
import numpy as np
import matplotlib.pyplot as plt
# Define the functions
def f(x):
return np.log(x) + 5
def g(x):
return np.log(x) - 5
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 3/8
4/14/24, 11:02 PM slip19
In [11]: # Write a python program to rotate the ray by 90o in clockwise direction ha
ving starting point (0,0) and end point (4,4).
import math
# Define the endpoints of the line segment
x1, y1 = 0, 0
x2, y2 = 4, 4
# Perform the rotation
x1_rotated = -x1
y1_rotated = -y1
x2_rotated = -x2
y2_rotated = -y2
# Print the original and rotated endpoints
print("Original Endpoint 1: ({}, {})".format(x1, y1))
print("Original Endpoint 2: ({}, {})".format(x2, y2))
print("Rotated Endpoint 1: ({}, {})".format(x1_rotated, y1_rotated))
print("Rotated Endpoint 2: ({}, {})".format(x2_rotated, y2_rotated))
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 4/8
4/14/24, 11:02 PM slip19
In [13]: # 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]
import numpy as np
import matplotlib.pyplot as plt
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Reflection of Triangle ABC through the line y=3')
plt.legend()
plt.grid(True)
plt.show()
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 5/8
4/14/24, 11:02 PM slip19
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 6/8
4/14/24, 11:02 PM slip19
Status: Optimal
Optimal Values:
x = 2.1707317
y = 1.2195122
z = 1.5121951
Optimal Objective Function Value = 18.658536500000004
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 7/8
4/14/24, 11:02 PM slip19
In [19]: # 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.
A = np.array([4, -1])
B = np.array([3, 0])
# Transformation 1: Rotation about origin through an angle pi (180 degrees)
angle = np.pi
rotation_matrix = np.array([[np.cos(angle), -np.si(angle)], [np.sin(angle),
np.cos(angle)]])
rotated_A = np.dot(rotation_matrix, A)
rotated_B = np.dot(rotation_matrix, B)
# Transformation 2: Scaling in x-coordinate by 3 units
scaling_factor = np.array([[3, 0],
[0, 1]])
scaled_A = np.dot(scaling_factor, rotated_A)
scaled_B = np.dot(scaling_factor, rotated_B)
# Transformation 3: Reflection through the line y = x
reflection_matrix = np.array([[0, 1],
[1, 0]])
reflected_A = np.dot(reflection_matrix, scaled_A)
reflected_B = np.dot(reflection_matrix, scaled_B)
# Print the results
print("Initial Points A and B:")
print("A =", A)
print("B =", B)
print("Combined Transformed Points A and B:")
print("A' =", reflected_A)
print("B' =", reflected_B)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/495099c9ac217b4c29128029f1abe805/slip19.html?t=1713115914521 8/8
slip20
[2]: # 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
# Define the functions
def f(x):
return np.sin(x)
def g(x):
return np.cos(x)
1
[7]: # 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
def f(x):
return np.exp(x) * np.sin(x)
y = f(x)
2
plt.show()
[8]: # Write a python program to reflect the line segment joining the points␣
↪A[−5,2],B[3,−4] through the line y = 2x −1
# Points A and B
A = (-5, 2)
B = (3, -4)
# Reflection line y = 2x - 1
3
def reflect_point(point):
x, y = point
x_reflect = (m * (y - b) + x) / (m**2 + 1)
y_reflect = m * x_reflect + b
return (x_reflect, y_reflect)
# Output
print("Reflection of point A:", A_reflected)
print("Reflection of point B:", B_reflected)
[9]: # 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
4
[11]: # Write a Python program to solve the following LPP:
# Max Z = x + y
# subject to x −y �1
# x + y �2
# x �0,y �0
Z = x + y
problem+= Z
problem+= x - y >= 1
problem+= x + y >= 2
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
5
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Unbounded
x = 0.0
y = 0.0
Optimal value of Z = 0.0
import numpy as np
P=np.array([3,-2])
# I. Shearing in x direction by -2 units.
shearing_x = [P[0] + ((-2) * P[1]), P[1]]
# II. Scaling in x and y direction by -3 and 2 units respectively.
scaling_x = np.array([-3, 1]) * P
scaling_y = np.array([1, 2]) * P
# III. Reflection through x axis.
reflection_x_axis = np.array([1, -1]) * P
# IV. Reflection through the line y = −x.
reflection_line = np.array([-P[1],-P[0]])
6
slip21
[2]: # Write a Python program to plot 2D graph of the function f(x) = x4 in [0,5]␣
↪with red dashed line with circle markers
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x**4
x = np.linspace(0, 5, 100)
y = f(x)
# Plot the graph with red dashed line and circle markers
plt.plot(x, y, 'r--o')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x) = x**4')
plt.show()
1
[6]: # 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
2
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('3D Wireframe Plot of f(x, y) = exp(x**2+y**2)')
# Show the plot
plt.show()
[7]: # 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
A = [2, 5]
B = [4, -13]
# Transformation matrix
T = [[2, 3],
[4, 1]]
3
for j in range(len(vector)):
result[i] += matrix[i][j] * vector[j]
return result
# Calculate slope
slope=(B_trans[1] - A_trans[1]) / (B_trans[0] - A_trans[0])
# Calculate midpoint
midpoint = [(A_trans[0] + B_trans[0]) / 2, (A_trans[1] + B_trans[1]) / 2]
import numpy as np
import matplotlib.pyplot as plt
vertices = np.array([[4, 4], [2, 4], [2, 2], [4, 2], [4, 4]])
fig, ax = plt.subplots()
# Plot the original square
ax.plot(vertices[:, 0], vertices[:, 1], 'b-o', label='Original Square')
# Define the uniform expansion and reduction factors
expans_fac = 3
reduct_fac = 0.4
# Perform uniform expansion
expanded_ver= vertices * expans_fac
# Perform uniform reduction
reduced_ver= vertices * reduct_fac
# Plot the expanded and reduced squares
ax.plot(expanded_ver[:, 0],expanded_ver[:, 1], 'r-o',label='Uniform␣
↪Expansion')
4
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Uniform Expansion and Reduction of a Square')
ax.legend()
plt.grid()
# Set aspect ratio to 'equal' for a square plot
ax.set_aspect('equal')
plt.show()
5
Z = 2*x + 4*y
problem+= Z
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Infeasible
x = 3.0
y = 12.0
Optimal value of Z = 54.0
import numpy as np
P=np.array([-2,4])
# I. Shearing in Y direction by 7 units
shearing_y = [P[0], P[1] + (7 * P[0])]
# II. Scaling in both X and Y direction by 4 and 7 units respectively.
scaling_both = np.array([[4, 0],[0, 7]])
P_scaling_both = np.dot(scaling_both, P)
# III. Rotation about origin by an angle 48 degrees
angle = np.radians(48)
rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.
↪cos(angle)]])
P_rotated = np.dot(rotation_matrix, P)
# IV. Reflection through line y = x.
reflection_line = np.array([P[1],P[0]])
6
print("Reflection through the line y = x:", reflection_line)
7
slip22
[1]: # Write a python program to draw 2D plot y = log(x2) + sin(x) with suitable␣
↪label in the x axis , y
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-5 * np.pi, 5 * np.pi, 500)
y = np.log(x**2) + np.sin(x)
plt.plot(x, y)
plt.xlabel('x-axis Label')
plt.ylabel('y-axis Label')
plt.title('2D Plot of y = log(x^2) + sin(x)')
plt.show()
1
[5]: # Write a python program to draw 2D plot y = xsin( 1 / x2) 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
x = np.linspace(-5, 5, 500)
y = x * np.sin(1 / x**2)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('2D Plot of y = x * sin(1/x^2)')
plt.show()
[6]: # 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].
2
import numpy as np
A = np.array([0, 0])
B = np.array([2, 2])
C = np.array([0, 2])
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
AC = np.linalg.norm(C - A)
angle_A_deg = np.degrees(angle_A)
angle_B_deg = np.degrees(angle_B)
angle_C_deg = np.degrees(angle_C)
[7]: # Write a Python program to find area and perimeter of the triangle ABC where␣
↪A[0,0],B[5,0],C[3,3].
import numpy as np
A = np.array([0, 0])
B = np.array([5, 0])
C = np.array([3, 3])
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
s = (AB + BC + CA) / 2
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
perimeter = AB + BC + CA
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
3
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 5.0
Side BC: 3.605551275463989
Side CA: 4.242640687119285
Area: 7.5000000000000036
Perimeter: 12.848191962583275
Z = x + y
problem+= Z
problem+= x + y <=11
problem+= x >= 6
problem+= y >= 6
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Infeasible
x = 6.0
y = 6.0
Optimal value of Z = 12.0
4
def rotate_point(x, y, cx, cy, theta):
x_new = cx + (x - cx) * math.cos(theta) - (y - cy) * math.sin(theta)
y_new = cy + (x - cx) * math.sin(theta) + (y - cy) * math.cos(theta)
return x_new, y_new
# Task I: Rotate the point (1,1) about (1,4) through angle �/2
rotated_point = rotate_point(1, 1, 1, 4, math.pi/2)
print("Rotated point:", rotated_point)
# Task II: Find distance between two points (0,0) and (1,0)
distance = distance_between_points(0, 0, 1, 0)
print("Distance between points:", distance)
# Task III: Find the shearing of the point (3,4) in X direction by 3 units
sheared_point = shear_point_x(3, 4, 3)
print("Sheared point:", sheared_point)
5
slip23
[1]: # 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
# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1)
# Plot sin(x)
ax1.plot(x, y_sin, label='sin(x)', color='blue')
ax1.set_title('sin(x)')
ax1.set_xlabel('x')
ax1.set_ylabel('sin(x)')
ax1.legend()
# Plot cos(x)
ax2.plot(x, y_cos, label='cos(x)', color='red')
ax2.set_title('cos(x)')
ax2.set_xlabel('x')
ax2.set_ylabel('cos(x)')
ax2.legend()
# Show plot
plt.show()
1
[2]: # 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
# Define the functions
def f1(x):
return x**3
def f2(x):
return x**2
x1 = np.linspace(0, 5, 100)
x2 = np.linspace(-2, 2, 100)
# Calculate y values for the functions
y1 = f1(x1)
y2 = f2(x2)
# Create a figure with two subplots side by side
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
2
# Plot f(x) = x^3 in the first subplot
axs[0].plot(x1, y1, label='f(x) = x^3')
axs[0].set_xlabel('x')
axs[0].set_ylabel('f(x)')
axs[0].set_title('f(x) = x^3 in [0, 5]')
axs[0].legend()
# Plot f(x) = x^2 in the second subplot
axs[1].plot(x2, y2, label='f(x) = x^2')
axs[1].set_xlabel('x')
axs[1].set_ylabel('f(x)')
axs[1].set_title('f(x) = x^2 in [-2, 2]')
axs[1].legend()
# Add overall title to the figure
fig.suptitle('Graphs of f(x) = x^3 and f(x) = x^2')
# Adjust spacing between subplots
plt.tight_layout()
# Show the plot
plt.show()
[3]: # Write a Python program to draw regular polygon with 20 sides and radius 1␣
↪centered at (0,0)
import numpy as np
import matplotlib.pyplot as plt
# Number of sides of the polygon
n = 20
# Radius of the polygon
radius = 1
3
# Generate angles for the vertices of the polygon
angles = np.linspace(0, 2 * np.pi, n + 1)[:-1]
# Calculate x and y coordinates for the vertices of the polygon
x = radius * np.cos(angles)
y = radius * np.sin(angles)
# Create a figure
fig, ax = plt.subplots()
# Plot the regular polygon
ax.plot(x, y, 'b-o', linewidth=2, markersize=8)
ax.set_aspect('equal', 'box')
ax.set_title(f'Regular Polygon with {n} sides')
ax.set_xlabel('x')
ax.set_ylabel('y')
# Show the plot
plt.show()
[6]: # 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
import numpy as np
4
A = np.array([0, 1])
B = np.array([-5, 0])
C = np.array([-3, 3])
AB = np.linalg.norm(B - A)
BC = np.linalg.norm(C - B)
CA = np.linalg.norm(A - C)
s = (AB + BC + CA) / 2
area = np.sqrt(s * (s - AB) * (s - BC) * (s - CA))
perimeter = AB + BC + CA
print("Triangle ABC:")
print("Side AB:", AB)
print("Side BC:", BC)
print("Side CA:", CA)
print("Area:", area)
print("Perimeter:", perimeter)
Triangle ABC:
Side AB: 5.0990195135927845
Side BC: 3.605551275463989
Side CA: 3.605551275463989
Area: 6.500000000000002
Perimeter: 12.310122064520764
5
prob += 2*y + 5*z <= 10
prob += 3*x + 2*y + 4*z <= 15
# Solve the LP prob
prob.solve()
# Print the status of the LP prob
print("Status: ", LpStatus[prob.status])
# Print the optimal values of the decision variables
print("Optimal Values:")
print("x = ", x.varValue)
print("y = ", y.varValue)
print("z = ", z.varValue)
# Print the optimal value of the objective function
print("Optimal Objective Function Value = ", value(prob.objective))
Status: Optimal
Optimal Values:
x = 2.1707317
y = 1.2195122
z = 1.5121951
Optimal Objective Function Value = 18.658536500000004
[11]: # Write the Python program to apply each of the following transformation on the␣
↪point P =
# [3,−1].
# I. Reflection through X axis.
# II. Rotation about origin by an angle 30 degrees.
# III. Scaling in Y coordinate by factor 8.
# IV. Shearing in X direction by 2 units
import numpy as np
P=np.array([3,-1])
# I. Reflection through X axis.
reflection_x_axis = np.array([1, -1]) * P
# II. Rotation about origin by an angle 30 degrees.
rotation_matrix = np.array([[np.cos(np.pi/6), -np.sin(np.pi/6)],
[np.sin(np.pi/6), np.cos(np.pi/6)]])
P_rotation = np.dot(rotation_matrix, P)
# III. Scaling in Y coordinate by factor 8
scaling_y = np.array([1, 8]) * P
# IV. Shearing in X direction by 2 units
shearing_x = [P[0] + (2 * P[1]), P[1]]
print("Original point P:", P)
print("Reflection through x-axis:", reflection_x_axis)
print("Rotation about origin through an angle of 30 degree:",P_rotation)
print("Scaling in Y-coordinates by factor 8:", scaling_y)
print("Shearing in X direction:", shearing_x)
6
Original point P: [ 3 -1]
Reflection through y-axis: [3 1]
Rotation about origin through an angle of 30 degree: [3.09807621 0.6339746 ]
Scaling in Y-coordinates by factor 8: [ 3 -8]
Shearing in X direction: [1, -1]
7
slip24
[1]: # write the Python program to plot the graph of the function, using def()
#
#f(x) = x2 + 4 if −10 �x < 5
# 3x + 9 if 5 �x < 10
import numpy as np
import matplotlib.pyplot as plt
def f(x):
if -10 < x < 5:
return x**2 + 4
elif 5 <= x:
return 3*x + 9
else:
return None
# Generate x values
# Generate 500 points between -11 and 11
x = np.linspace(-11, 11, 500)
# Calculate y values using f(x)
y = np.array([f(xi) for xi in x])
# Create the plot
plt.plot(x, y, label='f(x)')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Graph of f(x)')
plt.legend()
plt.grid(True)
plt.show()
1
[4]: # Write a Python program to plot graph of the function f(x) = log(3x2), 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)
2
[5]: # Write a Python program to generate vector x in the interval [−22,22] using␣
↪numpy package with 80 subintervals
import numpy as np
start = -22
end = 22
num_subintervals = 80
[-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 -0.55
0. 0.55 1.1 1.65 2.2 2.75 3.3 3.85 4.4 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
3
16.5 17.05 17.6 18.15 18.7 19.25 19.8 20.35 20.9 21.45
22. ]
[6]: # 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
v1 = np.array([3, 3])
v2 = np.array([5, 6])
v3 = np.array([5, 2])
theta = -np.pi
R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
plt.figure()
plt.
↪plot([v1_rotated[0],v2_rotated[0],v3_rotated[0],v1_rotated[0]],[v1_rotated[1],␣
4
[8]: # Write a Python program to solve the following LPP:
# Min Z = 3.5x + 2y
# subject to x + y �5
# x �4
# y �2
# x �0,y �0
Z = 3.5 * x + 2 * y
problem+= Z
problem+= x + y >= 5
problem+= x >= 4
problem+= y <= 2
5
problem.solve()
print("Status:", LpStatus[problem.status])
print("x =", value(x))
print("y =", value(y))
print("Optimal value of Z =",value(problem.objective))
Status: Optimal
x = 4.0
y = 1.0
Optimal value of Z = 16.0
[9]: # Apply Python program in each of the following transformations on the point␣
↪P[3,−1]
6
slip25
[1]: # Using Python plot the surface plot of function z = cos (x2 + 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
1
[4]: # 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
x = np.linspace(-1, 1, 1000)
# Calculate f(x) values
f_x = np.arcsin(x)
# Create the plot
plt.plot(x, f_x)
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Plot of f(x) = sin^-1(x)')
plt.grid(True)
plt.axhline(0, color='black') # Add horizontal grid line at y=0
plt.show()
2
[5]: # Rotate the line segment by 180◦ having end points (1,0) and (2,−1)
import numpy as np
# Define the endpoints of the line segment
point1 = np.array([1, 0])
point2 = np.array([2, -1])
# Define the rotation matrix for 180 degrees
R = np.array([[-1, 0],[0, -1]])
# Rotate the endpoints of the line segment
rotated_point1 = np.dot(R, point1)
rotated_point2 = np.dot(R, point2)
print("Rotated endpoint 1: ", rotated_point1)
print("Rotated endpoint 2: ", rotated_point2)
[6]: # Using sympy, declare the points P(5,2),Q(5,−2),R(5,0), check whether these␣
↪points are collinear.
3
# Declare the ray passing through the points P and Q, find the length of this␣
↪ray between P and Q.
P = Point(5, 2)
Q = Point(5, -2)
R = Point(5, 0)
# Check if points P, Q, and R are collinear
line_PQ = Line(P, Q)
line_PR = Line(P, R)
collinear = line_PQ.is_parallel(line_PR)
# Print the result
if collinear:
print("Points P, Q, and R are collinear")
else:
print("Points P, Q, and R are not collinear")
# Calculate the length of the ray PQ
length_PQ = P.distance(Q)
# Calculate the slope of the ray PQ
slope_PQ = (Q.y - P.y) / (Q.x - P.x)
# Print the length and slope of the ray PQ
print("Length of the ray PQ:", length_PQ)
print("Slope of the ray PQ:", slope_PQ)
4
problem += 5*x + 3*y <=15
# Solve the LP problem
problem.solve()
# Print the status of the solution
print("Status:", LpStatus[problem.status])
# Print the optimal values of x and y
print("Optimal x =", value(x))
print("Optimal y =", value(y))
# Print the optimal value of the objective function
print("Optimal Z =", value(problem.objective))
Status: Optimal
Optimal x = 3.0
Optimal y = 0.0
Optimal Z = 450.0
[8]: # Write a python program to apply the following transformations on the point␣
↪(−2,4) :
import numpy as np
P=np.array([-2,4])
5
Rotation about origin through an angle of 30 degree: [-3.73205081 2.46410162]