Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 47

_______________

Lab Manual for


B.E. Mathematics
Practical using
FOSS(Python) for
I semester B.E.
______________
Efective from the academic Year 2020-21
________________________
Department of Mathematics
MVJ college of Engineering(Autonomous)
_______________________

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

4. SIMPLE PROGRAMS WRITING AND EXECUTING

5. ANGLE BETWEEN RADIUS VECTOR AND THE TANGENT, ANGLE BETWEEN

TWO CURVES

6. RADIUS OF CURVATURE

7. MACLAURIN,S SERIES

8. INDETERMINATE FORMS

9. JACOBIANS

10. PLOTTING

11. EVALUATION OF INTEGRALS

12.SOLVING LINEAR SYSTEM OF EQUATIONS

LAB 1.Introduction to Python

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

language.it was initially designed by Guido van Rossum in 1991 and

developed by Python Software Foundation. It was mainly developed

emphasis on code readability , and its syntax allows programmers to

express concepts in fewer lines of code.

Python is a programming language that lets you work quickly and integrate

systems more efficiently

There are two major Python versions – Python 2 and Python 3. Both are

quite different.

Beginning with Python programming :

Before we start Python programming , we need to have an interpreter and

run our programs. There are certain online interpreters like

http://ide.geekforgeeks.org/, http://ideone.com/ or http://codepad.org/

that can be used to start Python without installing an interpreter.

Windows: There are many interpreters available freely to run Python

scripts like IDLE (Integrated Development Environment) which is installed

when you install the python software from http://python.org/

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

2. Download the file

3. Double click on downloaded file (check THE BOX add python to path )

4. Install Now

5. Open python IDLE in search or in all program

6. Start working on IDLE

1.2.INSTALLING PACKAGES FOR PYTHON

1. Go to search and type: cmd command prompt is opened

2. Type: python -m pip install --upgrade pip

3. pip install numpy

4. pip install sympy

5. pip install matplotlib

LAB 2.COMMANDS IN PYTHON

2.1.Execute the following commands,record the output and

explain their meaning :

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

2.2.Execute the following commands in console , record the

output and explain the nature of the command.

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 3. STATEMENTS IN PYTHON

3.1. Variable names


The variable name is a combination of alphabets, digits and underscore
without spaces , must start with an alphabet or an underscore. Variable names
are used to assign some value or expression. Python is case sensitive,’A’ is treated
different from’ a’. Some examples

Valied variable names


A=5
B=6
c=10
d=15
ramesh12=13
suresh=50
my_function=30

invalied variable names


2a
My function

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

The syntax of if statement is

if condition:
Statement1
Statement2
Statement3
Statement4
Statement5
Statement6
Statement7

Here the “condition” is a logical expression which is either true or false if


the “condition” is true then all the statements in the block ( Statement1 to
statement6) are executed and then the control moves to execute statement7. If
the ‘condition ” is false then Statement1 to statement6 are skipped and the
control moves to execute statement7.It may be observed that statement1 to
statement7 are placed with same indent which is called the block of if statement.
3.4. if else statement

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

Here the “condition” is a logical expression which is either true or false if

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

the ‘condition ” is false then Statement1 to statement6 are skipped ,Statement7

to Statement9 are executed and the control moves to execute statement10.It

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.

3.5. nested if statement

The syntax of if else statement is

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

The syntax of for loop is


for i in range(n):
S1
S2
S3
print()

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()

In this case i start with 1 and the increment is 2.

3.7. while loop

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.

LAB 4. SIMPLE PROGRAMS: WRITING AND

EXECUTION

4.1. some simple programs

#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

Lab 5. Angle between radius vector and


the tangent, angle between two curves
#angle between the radius vector and the tangent
#to the curve r=a*(1-cos(t)) at t=pi/2
from sympy import *
a,m,r,t=symbols('a,m,r,t')
R=a*(1-cos(t))
R=R.subs(t,pi/2)
u=r-a*(1-cos(t))
nr=diff(u,t)
dr=diff(u,r)
drdt=-nr/dr
drdt=drdt.subs({t:pi/2,r:R})
PHI=atan(R/drdt)
print('The angle between the radius vector and the tangent =',PHI)
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 21
#angle between the radius vector and the tangent
#to the curve r^m=a^m(cosmt-sinmt) at t=0
from sympy import *
a,m,r,t=symbols('a,m,r,t')
R=a*((cos(m*t)-sin(m*t))**(1/m))
R=R.subs(t,0)
u=r**m-a**m*(cos(m*t)-sin(m*t))
nr=diff(u,t)
dr=diff(u,r)
drdt=-nr/dr
drdt=drdt.subs({t:0,r:R})
PHI=atan(R/drdt)
print('The angle between the radius vector and the tangent =',PHI)

'''Angle of intersection of two curves


r1=a/(1-cost) and r2=b/(1-cost) at t = pi/3'''
from sympy import *
a,b,r1,r2,t=symbols('a,b,r1,r2,t')
R1=a/(1+cos(t))
R1=R1.subs(t,pi/3)
u1=r1-a/(1+cos(t))
nr1=diff(u1,t)
dr1=diff(u1,r1)
drdt1=-nr1/dr1
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 22
drdt1=drdt1.subs({t:pi/3,r1:R1})
PHI1=atan(R1/drdt1)
print('The angle between the radius vector R1 and the tangent=',PHI1)
R2=b/(1-cos(t))
R2=R2.subs(t,pi/3)
u2=r2-b/(1-cos(t))
nr2=diff(u2,t)
dr2=diff(u2,r2)
drdt2=-nr2/dr2
drdt2=drdt2.subs({t:pi/3,r2:R2})
PHI2=atan(R2/drdt2)
print('The angle between the radius vector R2 and the tangent=',PHI1)
print('The angle of intersection =',abs(PHI1-PHI2))

'''Angle of intersection of two curves


r1^2=16sin2t and r2^2sin2t=4 t = pi/12'''
from sympy import *
r1,r2,t=symbols('r1,r2,t')
R1=4*sqrt(sin(2*t))
R1=R1.subs(t,pi/12)
u1=r1-4*sqrt(sin(2*t))
nr1=diff(u1,t)
dr1=diff(u1,r1)
drdt1=-nr1/dr1
drdt1=drdt1.subs({t:pi/12,r1:R1})
Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 23
PHI1=atan(R1/drdt1)
print('The angle between the radius vector R1 and the tangent=',PHI1)
R2=2/sqrt(sin(2*t))
R2=R2.subs(t,pi/12)
u2=r2-2/sqrt(sin(2*t))
nr2=diff(u2,t)
dr2=diff(u2,r2)
drdt2=-nr2/dr2
drdt2=drdt2.subs({t:pi/12,r2:R2})
PHI2=atan(R2/drdt2)
print('The angle between the radius vector R2 and the tangent=',PHI1)
print('The angle of intersection =',abs(PHI1-PHI2))

6.Radius of curvature

'''Radius of curvature for the curve

y^2=4ax cartesian form'''

from sympy import *

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)

''' Radius of curvature for the curve

x=a(t+sint) , y=a(1-cost) at t=0 parametric form'''

from sympy import *

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)

print('Radius of curvature -',roh)

''' Radius of curvature for the curve

r=a(1+cost) at t=0 polar form'''

from sympy import *

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)

print('Radius of curvature at is',roh)

roh=roh.subs(t,0)

print('Radius of curvature at t=0 is',round(roh,2))

''' Radius of curvature for the curve

r^3=2ap**2 pedal form'''

from sympy import *

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})

print('Radius of curvature at p=2,a=2 is ',round(roh,2))

>>>

= RESTART: C:\Users\user\Documents\radofcur.py =

Radius of curvature= -2*x**2*(a/x + 1)**1.5/sqrt(a*x)

Radius of curvature - 4.0*(a**2)**0.5

Radius of curvature at is 1.88561808316413*(cos(t) + 1)**0.5


Lab Manual for B.E.I semester Mathematics FOSS(Python) MVJ College of Engineering effective from 2020-21
Page 26
Radius of curvature at t=0 is 2.67

Radius of curvature= 1.0582673679788*(a*p**2)**0.666666666666667/p

Radius of curvature at p=2,a=2 is 2.12

>>>

7. Maclaurin’s series

#Expand e^x in powers of x by Maclaurin's seris

#upto 5th powers of x

from sympy import *

from math import *

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

#upto 5th powers of x

from sympy import *

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)

#Expand cosx in powers of x by Maclaurin's seris

#upto 5th powers of x

from sympy import *

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)

#Expand tanx in powers of x by Maclaurin's seris

#upto 5th powers of x

from sympy import *

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)

#Expand lo(1+x) in powers of x by Maclaurin's seris

#upto 5th powers of x

from sympy import *

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)

#Expand lo(1+x) in powers of x by Maclaurin's seris

#upto 5th powers of x

from sympy import *

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

0.01*x**5 + 0.04*x**4 + 0.17*x**3 + 0.5*x**2 + 1.0*x + 1.0

x**5/120 - x**3/6 + x

x**4/24 - x**2/2 + 1

2*x**5/15 + x**3/3 + x

x**5/5 - x**4/4 + x**3/3 - x**2/2 + x

-x**5/5 - x**4/4 - x**3/3 - x**2/2 - x

>>>

Lab 8. Indeterminate forms

>>> from sympy import *

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()

'''

11. Evaluation of integrals


>>> from sympy import *
>>> x,y,z,m,n=symbols('x,y,z,m,n')
>>> integrate(x**n,x)
>>> integrate(x**5,x)
>>> integrate(log(x),x)
>>> integrate(exp(x),x)
>>> integrate(sin(x),x)

>>> 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 12.Solving homogeneous and non


homogeneous system of equations
12.1.homogeneous system of equations
#Solving homogeneous system of equations
from sympy import *
from numpy import *
a=Matrix(([1,2,3],[3,4,4],[7,10,12]))
(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])

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
___________________________________________________________________

12.2.non-homogeneous system of equations


#Solving non - homogeneous system of equations
from sympy import *
from numpy import *
a=Matrix(([1,1,1],[3,1,-2],[2,4,7]))
(m,n)=a.shape
x,y,z=symbols('x,y,z')
b=Matrix(([-3],[-2],[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])

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

You might also like