Lab Manual For 1 Sem
Lab Manual For 1 Sem
Contents
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 1
1. INTRODUCTION TO PYTHON, DOWNLOAD AND INSTALL PYTHON AND
RELATED PACKAGES
2. COMMANDS IN PYTHON
3. STATEMENTS IN PYTHON
TWO CURVES
6. RADIUS OF CURVATURE
7. MACLAURIN,S SERIES
8. INDETERMINATE FORMS
9. JACOBIANS
10. PLOTTING
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 2
Python is a widely used general purpose ,high level programming
Python is a programming language that lets you work quickly and integrate
There are two major Python versions – Python 2 and Python 3. Both are
quite different.
1.1.INSTALLING PYTHON
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 3
1. Go to => https://www.python.org/downloads
3. Double click on downloaded file (check THE BOX add python to path )
4. Install Now
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 4
For Example:
>>> 2+3
Output: 5
Explanation :2+3
>>> 2-3
Output: -1
Explanation : 2-3
>>> 2*3
Output: 6
Explanation : 2*3
>>> 2/3
Output :0.6666666666666666
Explanation : 2/3
>>> 2**3
Output: 8
Explanation : 23
>>> 2//3
Output: 0
Explanation :quotient obtained in dividing 2 by 3
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 5
>>> int(3/2)
>>> round(2.51)
>>> round(2.49)
>>> round(2.5)
>>> round(3.51)
>>> round(3.49)
>>> round(3.5)
>>> x=23145621.456872194
>>> x
>>> round(x)
>>> round(x,1)
>>> round(x,2)
>>> round(x,4)
>>> round(x,6)
>>> round(x,-1)
>>> round(x,-2)
>>> round(x,-4)
>>> round(x,-7)
>>> round(x,-8)
0.0
>>> x,y,z=1,2,3
>>> x
>>> x+y
>>> x+y+z
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 6
>>> print(x)
>>> print(x+y)
>>> print(x+y+z)
>>> x,x+y,x+y+z
(1, 3, 6)
>>> print(x,x+y,x+y+z)
>>> x=5
>>> x+=2
>>> x
>>> x-=2
>>> x
>>> a=[10,20,30,40]
>>> a
>>> a[0],a[1],a[2]
>>> a[3]
>>> a[-1]
>>> a[-2]
>>> a[-3]
>>> a[-4]
>>> a.append(50)
>>> a
>>> b=[60,70,80]
>>> a.append(b[0])
>>> a
>>> a.append(b)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 7
>>> a
>>> a.insert(6,90)
>>> a
>>> a.insert(0,100)
>>> a
>>> a.pop()
>>> a
>>> a.pop()
>>> a
>>> a.pop(1)
>>> a
>>> a.pop(0)
>>> a
>>> a.pop(-1)
>>> a
>>> a.remove(40)
>>> a
>>> a.remove(a[1])
>>> a
>>> a=[1,5,9,7,1,0,6,9]
>>> a
>>> a.sort()
>>> a
>>> b=[12,36,0,15,78,9,3,4,6,7]
>>> b
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 8
>>> b.sort()
>>> b
>>> import math
>>> math.sin(1)
>>> math.cos(1)
>>> math.e
>>> math.pi
>>> import math as m
>>> m.sin(1)
>>> m.cos(1)
>>> m.e
>>> m.pi
>>> from math import *
>>> sin(1)
>>> cos(1)
>>> e
>>> pi
>>> sin(pi/2)
>>> cos(pi/2)
>>> tan(pi/2)
>>> log(e)
>>> log(100)
>>> log10(100)
>>> log2(16)
>>> asin(.5)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 9
>>> acos(.6)
>>> atan(35)
>>> asin(1)
>>> acos(1)
>>> asin(-1)
>>> acos(-1)
>>> atan(-1)
>>> atan(1.1)
>>> asinh(2)
>>> acosh(2)
>>> ceil(2.12354)
>>> ceil(6.99999)
>>> ceil(6.000001)
>>> floor(2.2315)
>>> floor(2.99999)
>>> sinh(2)
>>> cosh(2)
>>> tanh(2)
>>> degrees(pi)
>>> degrees(pi/2)
>>> radians(90)
>>> radians(pi/4)
>>> radians(pi/4)
>>> radians(180)
>>> exp(2)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 10
>>> e**2
>>> abs(x)
>>> x
>>> x=-23.125
>>> abs(x)
>>> fabs(x)
>>> x=-123
>>> abs(x)
>>> x
>>> fabs(x)
>> factorial(5)
>>> factorial(10)
>>> factorial(0)
>>> 3//2
>>> 5//2
>>> 5%2
>>> 13%5
>>> fmod(5,2)
>>> fmod(13,5)
>>> gcd(15,25)
>>> gcd(7,13)
>>> gcd(-20,4)
>>> hypot(0,1)
>>> hypot(1,1)
>>> hypot(3,5)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 11
>>> modf(2.5)
>>> modf(35.26)
>>> pow(2,3)
>>> pow(3,4)
>>> x=range(10)
>>> x
range(0, 10)
>>> x[0]
>>> x[5]
>>> x[9]
>>> len(x)
>>> min(x)
>>> max(x)
>>> sum(x)
>>> x=range(5,10)
>>> len(x)
>>> x[0]
>>> x[3]
>>> x[4]
>>> x=range(2,6)
>>> list(x)
>>> x
range(2, 6)
>>> list(_)
>>> a={1,2,3,4,5}
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 12
>>> a
>>> a={1,2,'b','i',6}
>>> a
{'b', 1, 2, 6, 'i'}
>>> a={1,1,3,4,6,5,3}
>>> a
{1, 3, 4, 5, 6}
{range(0, 10)}
>>> a={1,2,3,4,5,6,7,8,9}
>>> a
>>> b={2,4,6,8,10}
>>> a|b
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
>>> a&b
{8, 2, 4, 6}
>>> a-b
{1, 3, 5, 7, 9}
>>> b-a
{10}
>>> x=3+4j
>>> x
>>> y=4+3j
>>> x+y
>>> x-y
(-1+1j)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 13
>>> x*y
>>> x/y
>>> abs(x)
>>> bin(20)
>>> oct(20)
>>> hex(20)
>>> int(0b101101)
>>> int(0o120367)
>>> int(0x12abc534)
>>> int(0o234)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 14
123
A,b,c
3.2. print statement
a=2
b=3
The print statement is a function its general form is
Print(‘this is’,a,’that is’,b)
Where a and b are pre assigned variable names. If the above statement is
executed the output will be as , this is 2 that is 3, anything written between two
single quotes or between two double quotes are considered as string.
3.3. if statement
if condition:
Statement1
Statement2
Statement3
Statement4
Statement5
Statement6
Statement7
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 15
The syntax of if else statement is
if condition:
Statement1
Statement2
Statement3
Statement4
Statement5
Statement6
else:
Statement7
Statement8
Stayement9
Statement10
the “condition” is true then all the statements in the block ( Statement1 to
statement6) are executed and then the control moves to execute statement10. If
may be observed that statement1 to statement7 are placed with same indent
which is called the true block and Statement7 to Statement9 is called false block.
if condition1:
BLOCK1
elif condition2:
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 16
BLOCK2
else:
BLOCK3
If condition1 is true BLOCK1 is executed else if condition2 is true BLOCK2 is
executed else BLOCK3 is executed.
3.6. for loop
First time is initialized to zero and all the statements in the body of for
loop(i.e. S1,S2,S3) are executed once, then i increased by 1, again all the
statements in the body are executed, again i increased by 1 and the process
continues ,when i reaches n then the control comes out of for loop and the next
statement (in this case it is the print statement) is executed.
for i in range(1,n):
S1
S2
S3
print()
In this case i start with 1
for i in range(1,n,2):
S1
S2
S3
print()
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 17
The syntax of while loop is
while condition:
S1
S2
S3
print()
The while loop is executed as long as condition is true if the condition is
false then the control comes out of the loop and the next statement is executed.
EXECUTION
#print statement
print('Mathematics')
print("physics")
a=1
b=2
c=3
print(a)
print(b)
print('a=',a)
print('b=',b)
print(a+b+c)
print('a+b+c=',a+b+c)
*******************************************************************
Mathematics
physics
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 18
1
2
a= 1
b= 2
6
a+b+c= 6
___________________________________________________________________
#if statement
a=1
b=2
c=3
if a<b:
print(a)
if a>b:
print(a)
# if else statement
if a<b:
print(a)
else:
print(b)
if a>b:
print(a)
else:
print(b)
#nested if statements
if a<b:
print(a)
elif a<c:
print(b)
else:
print(c)
if a>b:
print(a)
elif a>c:
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 19
print(b)
else:
print(c)
*******************************************************************
1
1
2
1
3
>>>
___________________________________________________________________
#for loop
for i in range(10):
print(i)
for i in range(10):
print(i,end='')
for i in range(10):
print(i,end=' ')
for i in range(10):
print(i,end=',')
*******************************************************************
0
1
2
3
4
5
6
7
8
9
01234567890 1 2 3 4 5 6 7 8 9 0,1,2,3,4,5,6,7,8,9,
>>
___________________________________________________________________
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 20
Exercise :
1.Write a program to find the factorial of a natural number n
2. Write a program to find the sum of first n natural numbers
3. Write a program to find the roots of a quadratic equayion
4. Write a program to reverse a number
5. Write a program to check the given number is palindrome or not
6. Write a program to check the given number is prime or not
7. write a program to print all odd numbers between 0 and 20
8. write a program to print even numbers between 25 and 45
9. Write a program to print all numbers divisible by 3 between 55 and 75
10. Write a program to print the first n Fibonacci numbers
6.Radius of curvature
x,y,a=symbols('x,y,a')
y=2*sqrt(a*x)
y1=diff(y,x)
y2=diff(y1,x)
roh=((1+y1**2)**(3/2))/y2
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 24
print('Radius of curvature=',roh)
x,y,a,t=symbols('x,y,a,t')
x=a*(t+sin(t))
y=a*(1-cos(t))
x1=diff(x,t)
x2=diff(x1,t)
y1=diff(y,t)
y2=diff(y1,t)
roh=simplify((x1**2+y1**2)**(3/2)/(x1*y2-y1*x2))
roh=roh.subs(t,0)
x,y,a,t=symbols('x,y,a,t')
a=2
r=a*(1+cos(t))
r1=diff(r,t)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 25
r2=diff(r1,t)
roh=(r**2+r1**2)**(3/2)/(r**2+2*r1**2-r*r2)
roh=simplify(roh)
roh=roh.subs(t,0)
p,a=symbols('p,a')
r=(2*a*p**2)**(1/3)
r1=diff(r,p)
roh=r*r1
print('Radius of curvature=',roh)
roh=roh.subs({p:2,a:2})
>>>
= RESTART: C:\Users\user\Documents\radofcur.py =
>>>
7. Maclaurin’s series
x=Symbol('x')
y=e**x
Sum=0
for i in range(0,6):
y1=diff(y,x,i)
y1=y1.subs(x,0)
fact=round(1/factorial(i),2)
term=y1*x**i*fact
Sum=Sum+term
print(Sum)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 27
#Expand sinx in powers of x by Maclaurin's seris
x=Symbol('x')
y=sin(x)
Sum=0
for i in range(0,6):
y1=diff(y,x,i)
y1=y1.subs(x,0)
term=y1*x**i/factorial(i)
Sum=Sum+term
print(Sum)
x=Symbol('x')
y=cos(x)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 28
Sum=0
for i in range(0,6):
y1=diff(y,x,i)
y1=y1.subs(x,0)
term=y1*x**i/factorial(i)
Sum=Sum+term
print(Sum)
x=Symbol('x')
y=tan(x)
Sum=0
for i in range(0,6):
y1=diff(y,x,i)
y1=y1.subs(x,0)
term=y1*x**i/factorial(i)
Sum=Sum+term
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 29
print(Sum)
x=Symbol('x')
y=log(1+x)
Sum=0
for i in range(0,6):
y1=diff(y,x,i)
y1=y1.subs(x,0)
term=y1*x**i/factorial(i)
Sum=Sum+term
print(Sum)
x=Symbol('x')
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 30
y=log(1-x)
Sum=0
for i in range(0,6):
y1=diff(y,x,i)
y1=y1.subs(x,0)
term=y1*x**i/factorial(i)
Sum=Sum+term
print(Sum)
RESTART: C:\Users\user\Documents\maclaurin.py
x**5/120 - x**3/6 + x
x**4/24 - x**2/2 + 1
2*x**5/15 + x**3/3 + x
>>>
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 31
>>> x=Symbol('x')
>>> limit(sin(x)/x,x,0)
1
>>> limit(tan(x),x,0)
0
>>> limit(tan(x)/x,x,0)
1
>>> limit((x**3+3*x-4)/(2*x**2+x-3),x,1)
6/5
>>> limit((x**2-x-2)/(x-2),x,2)
3
>>> from math import *
>>> limit((e**x-1)/x,x,0)
1.00000000000000
Lab 9. Jacobians
#Finding jacobian
from sympy import *
r,t=symbols('r,t')
x=r*cos(t)
y=r*sin(t)
A=Matrix([x,y])
jacA=A.jacobian([r,t])
jacobian=simplify(det(jacA))
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 32
print('jacobian(A)=',jacobian)
#Finding jacobian
from sympy import *
x,y,z=symbols('x,y,z')
u=z-x
v=y-z
w=x+y+z
A=Matrix([u,v,w])
jacA=A.jacobian([x,y,z])
jacobian=simplify(det(jacA))
print('jacobian(A)=',jacobian)
#Finding jacobian
from sympy import *
x,y=symbols('x,y')
u=x*(1-y)
v=x*y
A=Matrix([u,v])
jacA=A.jacobian([x,y])
jacobian=simplify(det(jacA))
print('jacobian(A)=',jacobian)
#Finding jacobian
from sympy import *
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 33
x,y=symbols('x,y')
u=exp(x)*(cos(y)+sin(y))/2
v=exp(x)*(cos(y)-sin(y))/2
A=Matrix([u,v])
jacA=A.jacobian([x,y])
jacobian=simplify(det(jacA))
print('jacobian(A)=',jacobian)
#Finding jacobian
from sympy import *
x,y=symbols('x,y')
u=atan(x)+atan(y)
v=(x+y)/(1-x*y)
A=Matrix([u,v])
jacA=A.jacobian([x,y])
jacobian=simplify(det(jacA))
print('jacobian(A)=',jacobian)
#Finding jacobian
from sympy import *
r,t,p=symbols('r,t,p')
x=r*sin(t)*cos(p)
y=r*sin(t)*sin(p)
z=r*cos(t)
A=Matrix([x,y,z])
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 34
jacA=A.jacobian([r,t,p])
jacobian=simplify(det(jacA))
print('jacobian(A)=',jacobian)
= RESTART: C:/Users/user/Documents/jacobian.py =
jacobian(A)= r
jacobian(A)= -3
jacobian(A)= x
jacobian(A)= -exp(2*x)/2
jacobian(A)= 0
jacobian(A)= r**2*sin(t)
>>>
10. Plotting
'''#Plotting
from pylab import *
x=[1,2,3,4,5,6]
y=[2,4,6,8,10,12]
plot(x,y)
show()
#Plotting
from pylab import *
x=[1,2,3,4,5,6]
y=[2,4,6,8,10,12]
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 35
plot(x,y,marker='+')
show()
#Plotting
from pylab import *
x=[1,2,3,4,5,6]
y=[2,4,6,8,10,12]
plot(x,y,marker='X')
show()
#Plotting
from pylab import *
x=[1,2,3,4,5,6]
y=[2,4,6,8,10,12]
plot(x,y,marker='*')
show()
#Plotting
from pylab import *
import numpy as np
import math as m
x=np.arange(-2*m.pi,2*m.pi,.01)
y=sin(x)
plot(x,y)
show()
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 36
#plotting bar graph
import matplotlib.pyplot as plt
left=[1,2,3,4,5]
height=[10,24,36,40,5]
tick_label1=['one','two','three','four','five']
plt.bar(left,height,tick_label=tick_label1,width=0.2,color=['red','green'])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('BAR GRAPH')
plt.show()
#Histogram Plotting
import matplotlib.pyplot as plt
ages=[2,5,70,40,30,50,6,10,13,15,22,46,90,86,35,44,56,31,42,5,3,2,1]
range=(0,50)
bins=5
plt.hist(ages,bins,range,color='green',histtype='bar',rwidth=.8)
plt.xlabel('age')
plt.ylabel('no. of people')
plt.title('My histogram')
plt.show()
#Pie chart
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 37
import matplotlib.pyplot as plt
activities=['Eat','Sleep','Work','Play']
slices=[3,7,8,6]
color=['r','y','g','b']
plt.pie(slices,labels=activities,colors=color,startangle=90,shadow=True,explode=(0
,0,0.1,0),radius=1.2,autopct='%1.1f%%')
plt.legend()
plt.show()
#Circle
import numpy as np
import matplotlib.pyplot as plt
plt.axes(projection='polar')
plt.title('Circle in polar form=R')
rad=np.arange(0,2*np.pi,.01)
for radian in rad:
plt.polar(radian,2,'o')
plt.show()
#Cardioide
from numpy import *
import matplotlib.pyplot as plt
fig=plt.figure()
fig.add_subplot(121,projection='polar')
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 38
plt.title('Cardioide in polar form: radius=a+(b*cos(k*radian))')
r=arange(0,2*pi,.01)
a,b,k=-1,1,1
for radian in r:
radius=a+(b*cos(k*radian))
plt.polar(radian,radius,'o')
plt.show()
'''
>>> integrate(cos(x),x)
>>> integrate(x**5,x,x)
>>> integrate(integrate(x**5,x))
>>> integrate(integrate(integrate(integrate(x**5,x))))
>>> integrate(integrate(integrate(x**5,x,x)))
>>> integrate(integrate(x**5,x,x,x))
>>> integrate(x**5,x,x,x,x)
>>> integrate(cos(x)**3,[x,0,pi/2])
>>> integrate(cos(x)**8,[x,0,pi/2])
>>> integrate(sin(x),[x,0,pi/2])
>>> integrate(sin(x)**5,[x,0,pi/2])
>>> integrate(x*cos(x)**4,[x,0,pi])
>>> integrate(sin(x)**6*cos(x)**8,[x,0,pi/2])
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 39
>>> integrate(integrate(x**2,[x,0,1]),[y,0,1])
1/3
>>> integrate(integrate(x**2,[y,0,1]),[x,0,1])
1/3
>>> integrate(integrate(x**2,[y,0,1]),[x,0,2])
8/3
>>> integrate(integrate(x**2,[x,0,1]),[y,0,2])
2/3
>>> integrate(x*sin(x)**4*cos(x)**6,[x,0,pi])
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 40
if r==n:
print('The system has only trivial solution',s)
else:
print('The system has a nontrivial solution')
print('The solution is',s)
*******************************************************************
rank(A)= 3
The system has only trivial solution {z: 0, y: 0, x: 0}
___________________________________________________________________
#Solving homogeneous system of equations
from sympy import *
from numpy import *
a=Matrix(([1,3,-2],[2,-1,4],[1,-11,14]))
(m,n)=a.shape
x,y,z=symbols('x,y,z')
X=Matrix(([x],[y],[z]))
aX=a*X
r=a.rank()
print('rank(A)=',r)
s=solve(aX,[x,y,z])
if r==n:
print('The system has only trivial solution',s)
else:
print('The system has a nontrivial solution')
print('The solution is',s)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 41
*******************************************************************
rank(A)= 2
The system has a nontrivial solution
The solution is {y: 8*z/7, x: -10*z/7}
___________________________________________________________________
#Solving homogeneous system of equations
from sympy import *
from numpy import *
a=Matrix(([2,-2,5,3],[4,-1,1,1],[3,-2,3,4],[1,-3,7,6]))
(m,n)=a.shape
x,y,z,w=symbols('x,y,z,w')
X=Matrix(([x],[y],[z],[w]))
aX=a*X
r=a.rank()
print('rank(A)=',r)
s=solve(aX,[x,y,z,w])
if r==n:
print('The system has only trivial solution',s)
else:
print('The system has a nontrivial solution')
print('The solution is',s)
*******************************************************************
rank(A)= 3
The system has a nontrivial solution
The solution is {z: 7*w/9, y: 4*w, x: 5*w/9}
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 42
___________________________________________________________________
if r1==r2:
print('The system is consistent')
if r1==n:
print(' The system has unique solution and the solution is')
s=solve(aX-b,[x,y,z])
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 43
print(s)
else:
print('The system has infinitely many solutions and the solution is')
s=solve(aX-b,[x,y,z])
print(s)
else:
print('The sysyem is inconsistent')
*******************************************************************
a= Matrix([[1, 1, 1], [3, 1, -2], [2, 4, 7]])
ab= Matrix([[1, 1, 1, -3], [3, 1, -2, -2], [2, 4, 7, 7]])
rank(a)= 2
rank(ab)= 3
The sysyem is inconsistent
___________________________________________________________________
#Solving non - homogeneous system of equations
from sympy import *
from numpy import *
a=Matrix(([1,1,1],[1,2,3],[1,4,7]))
(m,n)=a.shape
x,y,z=symbols('x,y,z')
b=Matrix(([6],[14],[30]))
X=Matrix(([x],[y],[z]))
aX=a*X
ab=a.col_insert(n,b)
r1=a.rank()
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 44
r2=ab.rank()
print('a=',a)
print('ab=',ab)
print('rank(a)=',r1)
print('rank(ab)=',r2)
if r1==r2:
print('The system is consistent')
if r1==n:
print(' The system has unique solution and the solution is')
s=solve(aX-b,[x,y,z])
print(s)
else:
print('The system has infinitely many solutions and the solution is')
s=solve(aX-b,[x,y,z])
print(s)
else:
print('The sysyem is inconsistent')
*******************************************************************
a= Matrix([[1, 1, 1], [1, 2, 3], [1, 4, 7]])
ab= Matrix([[1, 1, 1, 6], [1, 2, 3, 14], [1, 4, 7, 30]])
rank(a)= 2
rank(ab)= 2
The system is consistent
The system has infinitely many solutions and the solution is
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 45
{y: -2*z + 8, x: z - 2}
___________________________________________________________________
#Solving non - homogeneous system of equations
from sympy import *
from numpy import *
a=Matrix(([3,1,1],[-1,1,-2],[2,2,2],[-2,2,-3]))
(m,n)=a.shape
x,y,z=symbols('x,y,z')
b=Matrix(([8],[-5],[12],[-7]))
X=Matrix(([x],[y],[z]))
aX=a*X
ab=a.col_insert(n,b)
r1=a.rank()
r2=ab.rank()
print('a=',a)
print('ab=',ab)
print('rank(a)=',r1)
print('rank(ab)=',r2)
if r1==r2:
print('The system is consistent')
if r1==n:
print(' The system has unique solution and the solution is')
s=solve(aX-b,[x,y,z])
print(s)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 46
else:
print('The system has infinitely many solutions and the solution is')
s=solve(aX-b,[x,y,z])
print(s)
else:
print('The sysyem is inconsistent')
*******************************************************************
a= Matrix([[3, 1, 1], [-1, 1, -2], [2, 2, 2], [-2, 2, -3]])
ab= Matrix([[3, 1, 1, 8], [-1, 1, -2, -5], [2, 2, 2, 12], [-2, 2, -3, -7]])
rank(a)= 3
rank(ab)= 3
The system is consistent
The system has unique solution and the solution is
{z: 3, y: 2, x: 1}
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 47