1st Sem B.Tech Lab Manual_091723 lal
1st Sem B.Tech Lab Manual_091723 lal
LIST OF PROGRAMS:
Lab 1: Find the rank of a matrix and solution of linear equations by Gauss
Seidel method.
Lab 2: Compute eigenvalues and Eigenvectors, and find the Dominant
Eigenvalues and corresponding Eigenvector by Rayleigh power method.
Lab 3: 2-D plots of cartesian and polar curves.
Lab 4: Finding angle between two polar curves.
Lab 5: Find the partial derivatives and Jacobians.
Lab 6: Evaluation of improper integrals.
Lab 7: Solution of First-order Linear Differential Equation and Exact
Differential Equation
Lab 8: Solution of second-order differential Equation
PYTHON PROGRAMMING LANGUAGE
Introduction: Python is a high-level, general-purpose programming language
known for its simplicity and readability. It was created by Guido van Rossum and
first released in 1991. Python supports multiple programming paradigms, including
procedural, object-oriented, and functional programming. It has become one of the
most popular programming languages, widely used for web development, data
science, artificial intelligence, automation, scripting, and more.
Key features of Python include:
1. Readability: Python's syntax is designed to be clear and readable, making
it easy for developers to write and maintain code.
2. Versatility: Python can be used for various applications, from simple scripts
to complex software development.
Basics of Python:
❖ To run the program → Shift + Enter
❖ Addition → +
❖ Subtraction → -
❖ Multiplication → *
❖ Division → / (Quotient as answer)
❖ Floor division → // (Quotient value without decimal as answer)
❖ Modulus → % (Remainder as answer)
❖ Absolute value → abs() (Returns the absolute value of a number)
❖ Round value → round() (function rounds a number to a specified
number of decimal places)
❖ Power value → ** or pow(base value, power value)
❖ math.sqrt(x) →Returns the square root of x.
❖ math.exp(x) →Returns the e raised to the power x.
❖ math.log(x, base) → Returns the logarithm of x to the given base. If the
base is not specified, it returns the natural logarithm.
❖ math.log10(x) →Returns the base-10 logarithm of x.
❖ math.factorial(x) → Returns the factorial of x.
❖ math.gcd(a, b) →Returns the greatest common divisor of a and b.
Solved examples:
2+7
9
9-2
7
7*7
49
7/2
3.5
7//2
3
7%2
1
abs(-7)
7
round(3.6)
4
pow(2,3)
8
(or)
2**3
8
import math
math.sqrt(25)
5.0
import math
math.exp(2)
7.3890560
import math
math.log(1)
0.0
(or)
import math
math.log(4,2)
2
import math
math.factorial(3)
6
import math
math.gcd(2,3)
1
print(matrix)
[[1 2 3]
[4 5 6]
[7 8 9]]
Trigonometric Functions:
Syntax:
math.sin(x), math.cos(x), math.tan(x):
Returns the sine, cosine, and tangent of x, respectively.
Examples:
math.sin(0)
0
Differentiation:
Syntax:
from sympy import*
x,y=symbols(‘x,y’)
function=diff(function, independent variable)
display(function)
Example:
Differentiate 𝑓 = 𝑥 2 + 2𝑥 w.r.t. x
Code:
from sympy import*
x,y=symbols('x,y')
function=diff(x**2+2*x, x)
display(function)
2x+2
Differential equations:
Syntax:
from sympy import*
init_printing()
x,y=symbols(‘x,y’)
y=Function(“y”)(x)
y1=Derivative(y,x)z1=dsolve(Eq(),y)
display(z1)
𝑑𝑦
Example: Solve 𝑑𝑥
+ 𝑦 tan 𝑥 − 𝑦 3 sec 𝑥 = 0
Code:
from sympy import*
init_printing()
x,y=symbols('x,y')
y=Function("y")(x)
y1=Derivative(y,x)
z1=dsolve(Eq(y1+y*tan(x)-y**3*sec(x),0),y)
display(z1)
1 1
Output: 𝑦(𝑥) = −√𝐶 cos 𝑥 or 𝑦(𝑥) = √𝐶 cos 𝑥
1 −2 sin 𝑥 1 −2 sin 𝑥
Integrals:
Syntax:
from sympy import*
x,y=symbols(‘x,y’)
function=integrate(function, independent variable)
display(function)
Example: Integrate 𝑓 = 𝑥 2 + 2𝑥
Code:
from sympy import*
x,y=symbols('x,y')
f=x**2+3*x
W1=integrate(f,x)
display(W1)
𝑥3 3𝑥 2
Output: 3
+ 2
Code:
from sympy import*
x,y,z=symbols('x,y,z')
f=x**2+y**2
W1=integrate(f, (y,0,x), (x,0,1))
display(W1)
Output: 1/3
Built-in commands for plotting the graphs in Python:
In Python, several libraries provide functionalities for plotting graphs and
visualizations. Two popular ones are Matplotlib and Seaborn. Below are some built-
in commands for plotting graphs using these libraries:
Matplotlib:
Line Plot:
Code:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel('X-axis Label')
plt.ylabel('Y-axis Label')
plt.title('Line Plot')
plt.show()
Output:
#Syntax:
if condition:
statements
# Synatx:
# if condition:
#statements 1
# else:
# statements 2
# If condition is True- statements 1 will be executed
# otherwise- statements 2 will be executed
a=int(input("Enter an integer: "))
if a>0:
print("Number entered is positive")
else:
print("Number entered is negative")
Enter an integer: -5
Number entered is negative
While loop
• Is used to execute a block of statements repeatedly until a given condition is
satisfied.
• When the condition is false, immediately after the loop in the program is
executed
• Syntax:
while expression:
statement(s)
count = 1
while count <= 5:
print(count)
count += 1
1
2
3
4
5
For loop
• are used for sequential traversal
• it falls under the category of definite iteration
• also used to access elements from a container (for example list,
string,tuple)using built-in function range()
• Syntax:
for variable_name in sequence :
statement_1
statement_2
....
Exercise:
0 2 3 4
3. Find the rank of the matrix 𝐴 = [2 3 5 4]
4 8 13 12
4 0 2 1
4 1 3 4
4. Find the rank of the matrix 𝐴 = [ ]
2 3 4 7
2 3 1 4
Gauss-Seidel method:
Count x y z
Exercise:
7. Solve the system of equations using the Gauss-Seidel method:
10𝑥 + 2𝑦 + 𝑧 = 9; 𝑥 + 10𝑦 − 𝑧 = −22; −2𝑥 + 3𝑦 + 10𝑧 = 22.
8. Solve the system of equations using the Gauss-Seidel method:
5𝑥 + 2𝑦 + 𝑧 = 12; 𝑥 + 4𝑦 + 2𝑧 = 15; 𝑥 + 2𝑦 + 5𝑧 = 20.
Lab 2: Compute Eigen values and Eigen vectors, find the
Dominant Eigen values and corresponding Eigen vector by
Rayleigh power method.
Use python
1. To find dominant and corresponding eigenvector by Rayleigh power
method.
Eigenvalue : 2.9998983946352364
Eigenvalue : 5.999977420010387
Eigenvector : [ 1. 0.99999624 -0.99999624]
Exercise:
3. Find the largest eigenvalues and the corresponding eigenvectors of matrix
10 2 1
[ 2 10 1 ] by taking the initial eigen vector as [0, 0, 1]𝑇
2 1 10
4. Find the largest eigenvalues and the corresponding eigenvectors of matrix
2 −1 0
[−1 2 −1] by taking the initial eigen vector as [1, 1, 1]𝑇 .
0 −1 2
Lab 3: 2-D plots of cartesian and polar curves.
Use python
1. to plot Cartesian curves.
2. to plot polar curves.
Syntax for the commands used:
• Plot y versus x as lines and or markers using default line style, color, and
other customizations.
plot(x, y, color='green', marker='o',
linestyle='dashed',linewidth=2,markersize=12)
• Return evenly spaced values within a given interval. arange can be called
with a varying number of positional arguments
numpy.arange([start, ]stop, [step,]dtype=None,
*, like=None)
Output:
Code:
# importing the required module
import matplotlib.pyplot as plt
x = [1,2,3,4,6,7,8] # x axis values
y = [2,7,9,1,5,10,3] # corresponding y axis values
plt.plot(x, y, 'r+--') # plotting the points
plt.xlabel('x- axis') # naming the x axis
plt.ylabel('y- axis') # naming the y axis
plt.title('My first graph!') # giving a title to my graph
plt.show() # function to show the plot
Output:
Functions
3. Exponential curve, 𝑦 = 𝑒 𝑥
Code:
# importing the required modules
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10, 10, 0.001)
y = np.exp(x) # Exponential function
plt.plot(x,y) # plotting the points
plt.title("Exponential curve ") # giving a title to the
graph
plt.grid() # displaying the grid
plt.show() # shows the plot
Output:
Implicit Function:
5. Strophoid: 𝑦 2 (𝑎 − 𝑥) = 𝑥 2 (𝑎 + 𝑥), 𝑎 > 0
Code:
from sympy import *
x, y = symbols('x y')
p3= plot_implicit(Eq((y**2)*(2-x), (x**2)*(2+x)), (x,-5, 5), (y,-5, 5),
title= 'Strophoid: $y^2 (a-x)=x^2 (a+x), a> 0$') # a=2
Exercise:
Syntax:
matplotlib.pyplot.polar(theta, r, **kwargs)
Exercise:
9. Four leaved Rose: 𝑟 = 2|cos(2𝑥)|
10. Limacon: 𝑟 = 𝑎 + 𝑏𝑐𝑜𝑠𝜃
Lab 4: Finding angle between two polar curves.
Use python
1. To find angle between two polar curves.
• solve()
This line solves the equation, finding the points of intersection between the
two curves.
If 𝑡𝑎𝑛𝜑1 and 𝑡𝑎𝑛𝜑1 are angle between radius vector and tangent of two curves then
|𝜑1 − 𝜑2 | is the angle between two curves at the point of intersection.
1. Find the angle between the curves 𝑟 = 4(1 + cos 𝑡) and 𝑟 = 5(1 − cos 𝑡).
Code:
from sympy import *
r,t =symbols('r,t')
r1=4* (1+cos(t));
r2=5* (1-cos(t));
dr1=diff(r1,t)
dr2=diff(r2,t)
t1=r1/dr1
t2=r2/dr2
q=solve(r1-r2,t)
w1=t1.subs({t:float(q[1])})
w2=t2.subs({t:float(q[1])})
y1=atan(w1)
y2=atan(w2)
w=abs(y1-y2)
print('Angle between curves in radians is %0.3f'%(w))
Output
Angle between curves in radians is 1.571
2. Find the angle between the curves 𝑟 = 4 cos 𝑡 and 𝑟 = 5 sin 𝑡.
Code:
from sympy import *
r,t =symbols('r,t')
r1=4*(cos(t));
r2=5*(sin(t));
dr1=diff(r1,t)
dr2=diff(r2,t)
t1=r1/dr1
t2=r2/dr2
q=solve(r1-r2,t)
w1=t1.subs({t:float(q[0])})
w2=t2.subs({t:float(q[0])})
y1=atan(w1)
y2=atan(w2)
w=abs(y1-y2)
print('Angle between curves in radians is
%0.4f'%float(w))
Output
Angle between curves in radians is 1.5708
Exercise:
3. Find the angle between the radius vector and tangent to the following polar
𝑎
curves 𝑟 = 𝑎𝜃 and 𝑟 = 𝜃
4. Find the angle between the radius vector and tangent to the
following polar curves 𝑟 = 2 sin 𝜃 and 𝑟 = 2 cos 𝜃
Lab 5: Find the partial derivatives and Jacobians.
Use python
1. to find partial derivatives of functions of several variables.
2. to find Jacobian of function of two and three variables.
Code:
from sympy import *
x,y =symbols('x y')
u=exp(x)*(x*cos(y)-y*sin(y))
dux=diff(u,x)
duy=diff(u,y)
duxy=diff(dux,y)
duyx=diff(duy,x)
if duxy==duyx:
print('Mixed partial derivatives are equal')
else:
print('Mixed partial derivatives are not equal')
Output:
Mixed partial derivatives are equal.
Code:
from sympy import *
x,y =symbols('x y')
u=exp(x)*(x*cos(y)-y*sin(y))
display(u)
dux=diff(u,x)
duy=diff(u,y)
uxx=diff(dux,x)
uyy=diff(duy,y)
w=uxx+uyy
w1=simplify(w)
print('Ans:',float(w1))
Output:
𝑒 𝑥 ( 𝑥 cos(𝑦) − 𝑦 sin(𝑦)), Ans 0.
𝑥𝑦 𝑦𝑧 𝑧𝑥
3. If 𝑢 = 𝑧
,𝑣 = 𝑥
,𝑤 = 𝑦
then prove that 𝐽 = 4.
Code:
from sympy import *
x,y,z=symbols('x,y,z')
u=x*y/z
v=y*z/x
w=z*x/y
dux=diff(u,x)
duy=diff(u,y)
duz=diff(u,z)
dvx=diff(v,x)
dvy=diff(v,y)
dvz=diff(v,z)
dwx=diff(w,x)
dwy=diff(w,y)
dwz=diff(w,z)
J=Matrix([[dux,duy,duz],[dvx,dvy,dvz],[dwx,dwy,dwz]])
;
Output:
The Jacobian matrix is
𝑦 𝑥 −𝑥𝑦
𝑧 𝑧 𝑧2
−𝑦 2 𝑧 𝑦
𝑥2 𝑥 𝑥
𝑧 −𝑥𝑧 𝑥
𝑦 𝑦2 𝑦
J=4
Code:
from sympy import *
from sympy.abc import rho, phi, theta
X=rho*cos(phi)*sin(theta);
Y=rho*cos(phi)*cos(theta);
Z=rho*sin(phi);
dx=Derivative(X,rho).doit()
dy=Derivative(Y,rho).doit()
dz=Derivative(Z,rho).doit()
dx1=Derivative(X,phi).doit();
dy1=Derivative(Y,phi).doit();
dz1=Derivative(Z,phi).doit()
dx2=Derivative(X,theta).doit()
dy2=Derivative(Y,theta).doit();
dz2=Derivative(Z,theta).doit();
J=Matrix([[dx,dy,dz],[dx1,dy1,dz1],[dx2,dy2,dz2]]);
print('The Jacobian matrix is ')
display(J)
print('\n\n J = \n')
display(simplify(Determinant(J).doit()))
Output:
The Jacobian matrix is
sin (θ)cos (𝜑) cos (𝜑)cos (𝜃) sin (𝜑)
−ρ sin(𝜑) sin (𝜃) −ρsin (φ)cos (𝜃) ρcos (𝜑)
ρcos(𝜑) cos (𝜃) −ρ sin(𝜃) cos (𝜑) 0
𝐽 = 𝜌2 cos (𝜑)
Exercise:
𝑦 𝜕2 𝑢 𝜕2 𝑢
1. If 𝑢 = 𝑡𝑎𝑛−1 (𝑥 ). Verify that 𝜕𝑦 𝜕𝑥 = 𝜕𝑥 𝜕𝑦.
𝑥 2 +𝑦 2
2. If 𝑢 = log ( ). Show that 𝑥𝑢𝑥 + 𝑦𝑢𝑦 = 1.
𝑥+𝑦
3. If 𝑥 = 𝑢 − 𝑣, 𝑦 = 𝑣 − 𝑢𝑣𝑤 and 𝑧 = 𝑢𝑣𝑤 find Jacobian of
𝑥, 𝑦, 𝑧 𝑤. 𝑟. 𝑡 𝑢, 𝑣, 𝑤.
4. If 𝑢 = 𝑥 + 3𝑦 2 − 𝑧 3 , 𝑣 = 4𝑥 2 𝑦𝑧 and 𝑤 = 2𝑧 2 − 𝑥𝑦 find
𝜕(𝑢,𝑣,𝑤)
𝑎𝑡 (−2, −2,1).
𝜕(𝑥,𝑦,𝑧)
• Integrate:
integrate(function,(variable, min_limit, max_limit))
Code:
from sympy import *
x=symbols('x')
w1=integrate(exp(-x),(x,0,float('inf')))
print(simplify(w1))
Output:
1
𝜋
2. Evaluate ∫02 sin7 (𝑥) 𝑑𝑥.
Code:
from sympy import *
x=symbols('x')
w1=integrate(sin(x)**7,(x,0,float(pi/2)))
print(simplify(w1))
Output:
0.457142857142857
𝜋
3. Evaluate ∫02 sin9 (𝑥) 𝑑𝑥.
Code:
from sympy import *
x=symbols('x')
w1=integrate(sin(x)**9,(x,0,float(pi/2)))
print(simplify(w1))
Output:
0.406349206349206
𝜋
4. Evaluate ∫02 sin6 (𝑥) cos 4(𝑥) 𝑑𝑥.
Code:
from sympy import *
x=symbols('x')
w1=integrate(sin(x)**6 *
cos(x)**4,(x,0,float(pi/2)))
print(simplify(w1))
Output:
0.0184077694546277
Exercise:
∞ 𝑥2
5. Evaluate ∫0 (1+𝑥 2 )2
𝑑𝑥.
2𝜋
6. Evaluate ∫0 cos4(𝑥) 𝑑𝑥.
𝜋
7. Evaluate ∫02 cos 5(4𝑥) 𝑑𝑥.
𝜋
8. Evaluate ∫0 sin8(𝑥) cos(𝑥) 𝑑𝑥.
• sympy: This imports all functions and classes from the `sympy` library. This
is generally not recommended for larger projects due to namespace
pollution, but it can be convenient for small scripts or interactive sessions.
• Define the symbols: The symbols function creates symbolic variables `x`
and `y`. These symbols can be used to define mathematical expressions and
equations.
• Function("y")(x): Here, Function ("y") creates a symbolic function y, and
(x) indicates that y is a function of the variable x. Now, y represents 𝑦(𝑥).
• Eq(…., 0): This creates a symbolic equation
• dsolve(..., y): This function attempts to solve the differential equation for y.
• display(): This function displays the result nicely formatted.
𝑑𝑦 2
1. Solve 𝑑𝑥
+ 𝑥 𝑦 = 𝑥3
Code:
from sympy import *
Output:
Differential Equation:
𝑑 2𝑦(𝑥)
𝑑𝑥
𝑦(𝑥) + 𝑥
= 𝑥3
General Solution:
𝑥6
𝐶1 +
6
𝑦(𝑥) = 𝑥2
𝑑𝑦 2
2. Solve + 𝑦 = 𝑥𝑙𝑜𝑔𝑥
𝑑𝑥 𝑥
Code:
from sympy import *
Output:
Differential Equation:
𝑑𝑦 2𝑦(𝑥)
𝑦(𝑥) + = 𝑥𝑙𝑜𝑔𝑥
𝑑𝑥 𝑥
General Solution:
𝑐1 +𝑥 4 (4 log(𝑥)−1)
𝑦(𝑥) = 𝑥2
Exercise:
𝑑𝑦
1. Solve 𝑠𝑖𝑛2 𝑥 𝑑𝑥 − 𝑦 = 𝑐𝑜𝑡𝑥
𝑑𝑦
2. Solve 𝑑𝑥 + 𝑦𝑠𝑒𝑐𝑥 = 𝑡𝑎𝑛𝑥
Code:
from sympy import *
# Define M and N
M = 2*x*y + 3*y
N = x**2 + 3*x
if exact_condition:
print("The given equation is exact.")
# Find the potential function
f_x = integrate(M, x) + integrate(N - diff(integrate(M,
x), y), y)
else:
print("The given equation is not exact.")
Output:
Code:
from sympy import *
# Define M and N
M = (exp(y) + 1) * cos(x)
N = exp(y) * sin(x)
if exact_condition:
print("The given equation is exact.")
# Find the potential function
f_x = integrate(M, x) + integrate(N - diff(integrate(M,
x), y), y)
else:
print("The given equation is not exact.")
Output:
Exercise:
𝑑𝑦 𝑥 2 −𝑎𝑦
1. Solve 𝑑𝑥
= 𝑎𝑥−𝑦2
𝑦
2. Solve (3𝑥 2 𝑦 + ) 𝑑𝑥 + (𝑥 3 + 𝑙𝑜𝑔𝑥)𝑑𝑦 = 0
𝑥
Lab 8: Solution of second-order differential Equation
A second-order differential equation is defined as
𝑑2 𝑦 𝑑𝑦
𝑑𝑥 2
+ 𝑃(𝑥) 𝑑𝑥 + 𝑄(𝑥)𝑦 = 𝑓(𝑥), where 𝑃(𝑋), 𝑄(𝑥), and 𝑓(𝑥) are functions of 𝑥.
Output:
The solution to the differential equation is:
3𝑒 −4𝑥
𝑦(𝑥) = (𝑐1 + 𝑐2 𝑥)𝑒 3𝑥 +
49
Output:
The solution to the differential equation is:
𝑒 3𝑥 + 4
𝑦(𝑥) = 𝑐1 𝑒 −𝑥 + 𝑐2 𝑠𝑖𝑛(𝑥) + 𝑐3 𝑐𝑜𝑠(𝑥) +
40
3. Solve (𝐷 3 − 1)𝑦 = 3 cos 2𝑥
from sympy import *
Output:
The solution to the differential equation is:
Exercise: