Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
205 views

1st Sem Lab Manual Python

Uploaded by

lakhbhat2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views

1st Sem Lab Manual Python

Uploaded by

lakhbhat2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lab Manual for

B.Sc. Mathematics
Practical using
FOSS(Python) for

I semester B.Sc.
Effective from the academic Year 2020-21
(Draft)
Contents
Lab Description
No.
1 Introduction to Python :Basic syntax,variable types,basic
operators,numbers,strings,lists,tuples,functions and input/output
statements.
2 Some simple programs to understand the relational,conditional and logical
operators.
i)Compare two numbers (less than,greater than) using if statement
ii) Sum of natural numbers using while loop
iii) Finding the factors of a number using for loop
iv) To check the given number is prime or not (use if …else statement)
v)Find the factorial of a number (use if…if…else).
vi) Simple programs to illustrate logical operators (and, or , not)

3 Python commands to reduce given matrix to echelon form and normal form
with examples.
4 Python program/command to establish the constituency or otherwise and
solving system of linear equations.
5 Python command to find the nth derivatives.

6 Python program to find nth derivative with and without Leibnitz’s theorem.

7 Obtaining partial derivatives of some standard functions.

8 Verification of Euler’s theorem ,its extension and Jacobians.

9 Python program for reduction formula with or without limits.

10 Python program to find equation and plot sphere,cone,cylinder.


LAB 1.Introduction to Python

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

1.3. TOKENS IN PYTHON


The smallest unit in a program is known as a Token. Following are the python tokens.
i) Key words ii) Identifiers(Names) iii) Literals iv) Operators v) Punctuations
i) Key words: Key words are pre defined words with special meaning and these are reserved words
must not be used as normal identifier names. These key words consists of False, True, None, and,
or, not, as, is, in, if, elif, else, for, while, assert, del, break, class, continue, def, except, global,
finally, from ,import, lambda, non local, pass, return, try, with, yield.
ii) Identifiers: Identifiers are the names given to variables,objects,classes,functions,lists and so
on.Variable names must not be key words and must not contain blank spaces. Variable names are
made up of (A – Z, a – z, 0 – 9, underscore _ ) and must not start with a digit.Valied variable names
are ABCD, abcd, AbCd, A345, a_bG78 ,_product. Invalid variable names are 6ab, av fd, for, and.
iii) Literals:
a) String literals: String literals are sequence of characters surrounded by quotes( both single, both
double, or both triple). Single line literals are like ‘hello world’, “my function”,
“ Mera Bharath Mahan”, . Multiline strings are like
’’’ program to find the sum of
the first natural numbers’’’.
Invalid string literals are ‘ hello world”, “ Hello world’
b) Numeric literals: Numeric literals are integers, float,complex numbers (in python complex
numbers are identified by a+bJ).
c) Boolean literals: Boolean literals in python are True, False.
d) Special literal : The special literal is None (Absence of a value )
iv ) operators :
a) arithmetic operators’ +’ (addition), ‘-‘ (substraction), ‘*’ (multiplication), ‘/’ (division), ‘a**b’
or pow(a,b) (for ab), a//b (integer part of a/b), a%b (remainder obtained by dividing a by b)
b) Logical operators and relational : and, or, not, <, >, <=, >=, ==
c) Punctuations : ‘, “, #, \, ( ), [ ], { }, :

Input statement : input() is used to input string,int(input()) is used to input integer and
float(input()) is used to input floating value.
Output statement:
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.

Lists: Lists are defined as a= [ 1,2,3,4], indexing start with 0 i.e. a[0]=1,a[1]=2,a[2[=3, a[3]=4.
The elements of the list may be any data type and are mutable.
Tuple : Tuples are the group of any data types separated by commas and are enclosed in
parenthesis.The elements of tuple are immutable. Examples are a=(1,2,3), b=( ‘a’,2,”C”).
Functions : Functions are defined as
def name(arguments):
return expression
for example def f(x,y):
return x+y+2

1.4.COMMANDS IN PYTHON
Execute the following commands,record the output and explain their meaning :
For Example:
>>> 2+3
Output: 5
Explanation :2+3=5
>>> 2-3
Output: -1
Explanation : 2-3=-1
>>> 2*3
Output: 6
Explanation : 2*3=6
>>> 2/3
Output :0.6666666666666666
Explanation : 2/3=.66666

>>> 2**3
Output: 8
Explanation : 23 =8
>>> 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.

>>> 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)
>>> x,y,z=1,2,3
>>> x
>>> x+y
>>> x+y+z
>>> print(x)
>>> print(x+y)
>>> print(x+y+z)
>>> x,x+y,x+y+z
>>> 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)
>>> 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
>>> 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)
>>> 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)
>>> 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)
>>> modf(2.5)
>>> modf(35.26)
>>> pow(2,3)
>>> pow(3,4)
>>> x=range(10)
>>> x
>>> 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
>>> list(_)
>>> a={1,2,3,4,5}
>>> a
>>> a={1,2,'b','i',6}
>>> a
{'b', 1, 2, 6, 'i'}
>>> a={1,1,3,4,6,5,3}
>>> a
>>> x=3+4j
>>> x
>>> y=4+3j
>>> x+y
>>> x-y
>>> x*y
>>> x/y
>>> abs(x)
>>> bin(20)
>>> oct(20)
>>> hex(20)
>>> int(0b101101)
>>> int(0o120367)
>>> int(0x12abc534)
>>> int(0o234)

LAB 2.Simple Programs

2.1. 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.
2.2. if else statement
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.
2.3. nested if statement
The syntax of if else statement is
if condition1:
BLOCK1
elif condition2:
BLOCK2
else:
BLOCK3
If condition1 is true BLOCK1 is executed else if condition2 is true BLOCK2 is executed
else BLOCK3 is executed.
2.4. for loop
The syntax of for loop is
for i in range(n):
S1
S2
S3
print()

First time i 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 ( I takes the values from 0
to n-1).

for i in range(1,n):
S1
S2
S3
print()
In this case i start with 1and takes the values up to n-1.
for i in range(1,n,2):
S1
S2
S3
print()

In this case i start with 1 and takes the values less than n with increment 2.

2.5. while loop


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.

2.6. some simple programs


#2.1.Compare two numbers using if statement
def compare(a,b):
if a>b:
print(a,'>',b)
elif a==b:
print(a,'=',b)
else:
print(b,'>',a)

OUT PUT
>>> compare(2,3)
3>2
>>> compare(20,5)
20 > 5
#2.2.Sum of natural numbers using while loop
def sonn(n):
i,s=1,0
while i<=n:
s=s+i
i=i+1
print('sum of first',n,'natural numbers =',s)
OUT PUT
>>> sonn(5)
sum of first 5 natural numbers = 15
>>> sonn(10)
sum of first 10 natural numbers = 55

#2.3.Finding the factors of a number using for loop


def factors(n):
print('The factors of',n,' are')
for i in range(1,n+1):
if n%i==0:
print(i,end=' ')

OUT PUT
>>> factors(10)
The factors of 10 are
1 2 5 10
>>> factors(100)
The factors of 100 are
1 2 4 5 10 20 25 50 100

#2.4.To check the given number is prime or not


def pri(n):
for i in range(2,n):
if n%i==0:
print(n,'is not a prime')
break
else:
print(n,'is a prime')

OUT PUT
>>> pri(7)
7 is a prime
>>> pri(10)
10 is not a prime
>>> pri(23)
23 is a prime
>>> pri(37)
37 is a prime

#2.5.Find the factorial of a number


def fact(n):
p=1
for i in range(1,n+1):
p=p*i
print('factorial(',n,')=',p)
OUT PUT
>>> fact(3)
factorial( 3 )= 6
>>> fact(5)
factorial( 5 )= 120
>>> fact(6)
factorial( 6 )= 720

#2.6.Simple programs to illustrate logical operators and or not

a,b=False,False
print('a and b is',a and b)
print('a or b is',a or b)
print('not a is',not a)
print('not b is',not b)

OUT PUT
a and b is False
a or b is False
not a is True
not b is True

a,b=2,3
p=a>1
q=b>1
print(a>1 and b>1)
print(a>1 or b>1)
print(not p,not q)

OUT PUT
True
True
False False

def big(a,b,c):
if(a>=b and a>=c):
print(a,'is bigger')
elif (b>=a and b>=c):
print(b,'is bigger')
else:
print(c,'is bigger')

OUT PUT
big(10,20,30)
30 is bigger
>>> big(12,67,24)
67 is bigger
>>> big(35,63,85)
85 is bigger
___________________________________________________________________

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 3. Python commands to reduce given matrix to echelon form


and normal form.
1 2 3
Ex. 3.1. Ex : A=[5 3 6]
6 4 2

#Program to find the echelon form of the matrix A


from sympy import *
A=Matrix(([1,2,3],[5,3,6],[6,4,2]))
a=A.echelon_form()
print('echelonform of')
pprint(A)
print('is')
pprint(a)

OUT PUT
echelonform of
⎡1 2 3⎤
⎢5 3 6⎥
⎣6 4 2⎦
is
⎡1 2 3 ⎤
⎢0 -7 -9⎥
⎣0 0 40⎦

#Program to find the normal form of the matrix A


from sympy import *
A=Matrix(([1,2,4],[-1,-2,-4],[2,4,8],[3,6,9]))
a=A.echelon_form()
a=A.T
a=a.echelon_form()
a=a.T
print('Normal form of')
pprint(A)
print('is')
print(a.rref())

OUT PUT
Normal form of
⎡1 2 4 ⎤
⎢-1 -2 -4⎥
⎢2 4 8 ⎥
⎣3 6 9 ⎦
is
(Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 0],
[0, 0, 0]]), (0, 1))

𝑬𝒙𝒆𝒓𝒄𝒊𝒔𝒆: 𝐹𝑖𝑛𝑑 𝑡ℎ𝑒 𝑒𝑐ℎ𝑒𝑙𝑜𝑛 𝑎𝑛𝑑 𝑛𝑜𝑟𝑚𝑎 𝑓𝑜𝑟𝑚𝑠 𝑜𝑓 𝑡ℎ𝑒 𝑓𝑜𝑙𝑙𝑜𝑤𝑖𝑛𝑔 𝑚𝑎𝑡𝑟𝑖𝑐𝑒𝑠
4 6 5
1 3 5 3 5 6 3 3 5
1 2 3 2 5 4 6 7
𝑖) [ ] 𝑖𝑖) [1 5 7] 𝑖𝑖𝑖) [2 5 7 6] 𝑖𝑣) [ ] 𝑣) [ ] 𝑣𝑖) [4 5]
5 2 6 7 8 2 4 6
3 2 6 8 5 7 8 7 8
8 9 7

Lab 4. non-homogeneous system of equations


'''
python program /commands to establish
the constistency criterionor otherwise
and solving system of linear equations\
Solve 3.x+y-2z=5,x-2y+z=-2,-2x+y+z=4
'''
from sympy import *
x,y,z=symbols('x,y,z')
n=int(input('Enter the number of unknowns'))
a11,a12,a13,b1=1,1,-2,5
a21,a22,a23,b2=1,-2,1,-2
a31,a32,a33,b3=2,1,1,4
a=Matrix(([a11,a12,a13],[a21,a22,a23],[a31,a32,a33]))
b=Matrix(([a11,a12,a13,b1],[a21,a22,a23,b2],[a31,a32,a33,b3]))
ra=a.rank()
print('Rank(A)=',ra)
rb=b.rank()
print('Rank(A|B)=',rb)
if (ra!=rb):
print('The system is inconsistent and has no solution')
elif(ra==rb==n):
print('The system has unique solution and the solution is')
soln=solve([a11*x+a12*y+a13*z-b1,a21*x+a22*y+a23*z-b2,
a31*x+a32*y+a33*z-b3],[x,y,z])
print(soln)
else:
print('The system has infinitely many solutions and the solution is')
print(linsolve([a11*x+a12*y+a13*z-b1,a21*x+a22*y+a23*z-b2,
a31*x+a32*y+a33*z-b3],[x,y,z]))

OUT PUT
Enter the number of unknowns3
Rank(A)= 3
Rank(A|B)= 3
The system has unique solution and the solution is
{x: 7/4, y: 17/12, z: -11/12}

𝐸𝑥𝑒𝑟𝑐𝑖𝑠𝑒 𝑺𝒐𝒍𝒗𝒆 ∶
1. 𝑆𝑜𝑙𝑣𝑒 𝑥 + 𝑦 + 𝑧 = 4, 2𝑥 + 𝑦 − 𝑧 = 1, 𝑥 − 𝑦 + 2𝑧 = 2
OUT PUT
Enter the number of unknowns3
Rank(A)= 3
Rank(A|B)= 3
The system has unique solution and the solution is
{x: 3/7, y: 13/7, z: 12/7}

2. 𝑥 + 2𝑦 + 2𝑧 = 1, 2𝑥 + 𝑦 + 𝑧 = 2, 3𝑥 + 2𝑦 + 2𝑧 = 3
OUT PUT
Enter the number of unknowns3
Rank(A)= 2
Rank(A|B)= 2
The system has infinitely many solutions and the solution is
FiniteSet((1, -z, z))

3. 𝑥 + 𝑦 − 2𝑧 = 5, 𝑥 − 2𝑦 + 𝑧 = −2, −2𝑥 + 𝑦 + 𝑧 = 4
OUT PUT
Enter the number of unknowns3
Rank(A)= 2
Rank(A|B)= 3
The system is inconsistent and has no solution
4. 𝑥 + 2𝑦 − 𝑧 = 3, 3𝑥 − 𝑦 + 2𝑧 = 1, 2𝑥 − 2𝑦 + 3𝑧 = 2,
OUT PUT
Enter the number of unknowns3
Rank(A)= 3
Rank(A|B)= 3
The system has unique solution and the solution is
{x: -1, y: 4, z: 4}

5. 𝑥 − 7𝑦 + 15𝑧 = −14, 2𝑥 + 3𝑦 − 4𝑧 = 6, 3𝑥 − 4𝑦 + 11𝑧 = −8, 5𝑥 − 𝑦 + 7𝑧 = −2


OUT PUT
(In this example add one more row in the program a41,a42,a43,a44=5,-1,7,-2)
Enter the number of unknowns3
Rank(A)= 2
Rank(A|B)= 2
The system has infinitely many solutions and the solution is
FiniteSet((-z, 2*z + 2, z))

Lab 5 Finding the nth derivatives


#Finding the nth derivative of log(sinx)
from sympy import *
x=symbols('x')
def der(n):
y=log(sin(x))
yn=diff(y,x,n)
print(yn)

OUT PUT
>>> der(1)
cos(x)/sin(x)
>>> der(2)
-(1 + cos(x)**2/sin(x)**2)
>>> der(3)
2*(1 + cos(x)**2/sin(x)**2)*cos(x)/sin(x)
>>> der(4)
-2*(1 + 4*cos(x)**2/sin(x)**2 + 3*cos(x)**4/sin(x)**4)

𝑬𝒙𝒆𝒓𝒄𝒊𝒔𝒆 ∶ 𝐹𝑖𝑛𝑑 𝑡ℎ𝑒 𝑛𝑡ℎ 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑣𝑒𝑠 𝑜𝑓 𝑡ℎ𝑒 𝑓𝑜𝑙𝑙𝑜𝑤𝑖𝑛𝑔


𝑖) 𝑥10
OUT PUT
>>> der(1)
10*x**9
>>> der(2)
90*x**8
>>> der(3)
720*x**7
>>> der(4)
5040*x**6
𝑖𝑖) 𝑥 5
OUT PUT
>>> der(1)
5*x**4
>>> der(4)
120*x
>>> der(5)
120

𝑖𝑖𝑖) 𝑒 2𝑥
OUT PUT
>>> der(1)
2*exp(2*x)
>>> der(5)
32*exp(2*x)
>>> der(10)
1024*exp(2*x)
>>> der(20)
1048576*exp(2*x)
>>>
𝑖𝑣) log(𝑐𝑜𝑠𝑥)
OUT PUT
>>> der(1)
-sin(x)/cos(x)
>>> der(3)
-2*(sin(x)**2/cos(x)**2 + 1)*sin(x)/cos(x)
𝑣) (2𝑥 + 3)4
OUT PUT
der(1)
8*(2*x + 3)**3
>>> der(4)
384
𝑣) sin3 𝑥
OUT PUT
der(1)
3*sin(x)**2*cos(x)
>>> der(3)
3*(-7*sin(x)**2 + 2*cos(x)**2)*cos(x)
𝒗𝒊) 𝐜𝐨𝐬 𝟑 𝒙
OUT PUT
der(3)
3*(-2*sin(x)**2 + 7*cos(x)**2)*sin(x)
𝑣𝑖𝑖) tan2 𝑥
OUT PUT
der(1)
(2*tan(x)**2 + 2)*tan(x)
>>> der(3)
8*(tan(x)**2 + 1)*(3*tan(x)**2 + 2)*tan(x)

Lab 6 : finding nth derivative by Leibnitz theorem and without


Leibnitz theorem
#Find the nth derivative of e^x*sinx
from sympy import *
x=symbols('x')
def der(n):
y=exp(x)*sin(x)
yn=diff(y,x,n)
print(yn)

OUT PUT
der(2)
2*exp(x)*cos(x)

𝑬𝒙𝒆𝒓𝒄𝒊𝒔𝒆 ∶ 𝐹𝑖𝑛𝑑 𝑡ℎ𝑒 𝑛𝑡ℎ 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑣𝑒𝑠 𝑜𝑓 𝑡ℎ𝑒 𝑓𝑜𝑙𝑙𝑜𝑤𝑖𝑛𝑔


𝑖) sin2 𝑥 cos 3 𝑥 𝑖𝑖 ) 𝑐𝑜𝑠𝑥 𝑐𝑜𝑠2𝑥 𝑐𝑜𝑠3𝑥 𝑖𝑖𝑖) 𝑠𝑖𝑛𝑥 𝑠𝑖𝑛2𝑥 𝑠𝑖𝑛3𝑥 𝑖𝑣) 𝑒 𝑥 𝑠𝑖𝑛𝑥𝑐𝑜𝑠2𝑥 𝑣) 𝑥 2 𝑒 𝑥 𝑐𝑜𝑠𝑥

Lab 7 : Finding the partial derivatives


#Finding all first and second order partial detivatives of atan(x,y)
from sympy import *
x,y=symbols('x,y')
z=atan(y/x)
print('zx=',simplify(diff(z,x)))
print('zy=',simplify(diff(z,y)))
print('zxy=',simplify(diff(z,x,y)))
print('zyx=',simplify(diff(z,y,x)))
print('zxx=',simplify(diff(z,x,2)))
print('zyy=',simplify(diff(z,y,2)))

OUT PUT
zx= -y/(x**2 + y**2)
zy= x/(x**2 + y**2)
zxy= (-x**2 + y**2)/(x**2 + y**2)**2
zyx= (-x**2 + y**2)/(x**2 + y**2)**2
zxx= 2*x*y/(x**2 + y**2)**2
zyy= -2*x*y/(x**2 + y**2)**2

𝑬𝒙𝒆𝒓𝒄𝒊𝒔𝒆 ∶ 𝐹𝑖𝑛𝑑 𝑎𝑙𝑙 𝑓𝑖𝑟𝑠𝑡 𝑎𝑛𝑑 𝑠𝑒𝑐𝑜𝑛𝑑 𝑜𝑟𝑑𝑒𝑟 𝑝𝑎𝑟𝑡𝑖𝑎𝑙 𝑑𝑒𝑟𝑖𝑣𝑎𝑡𝑖𝑣𝑒𝑠 𝑜𝑓 𝑡ℎ𝑒 𝑓𝑜𝑙𝑜𝑤𝑖𝑛𝑔
𝑦 𝑥 𝜕𝑢 𝜕𝑢 𝜕𝑢
𝑖)𝑥 𝑦 + 𝑦 𝑥 𝑖𝑖 ) 𝑥 2 tan−1 𝑥 − 𝑦 2 tan−1 𝑦 𝑖𝑖𝑖) 𝑖𝑓 𝑢 = 𝑥 2 𝑦 + 𝑦 2 𝑧 + 𝑧 2 𝑥 𝑓𝑖𝑛𝑑 𝜕𝑥 + 𝜕𝑦 + 𝜕𝑧
𝜕𝑢 𝜕𝑢 𝜕𝑢
𝑖𝑣) 𝑖𝑓 𝑢 = 𝑥 3 + 𝑦 3 + 𝑧 3 − 3𝑥𝑦𝑧 𝑓𝑖𝑛𝑑 + +
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝜕𝑢 𝜕𝑢 𝜕𝑢
𝑣) ) 𝑖𝑓 𝑢 = 𝑥 3 + 𝑦 3 + 𝑧 3 + 3𝑥𝑦𝑧 𝑓𝑖𝑛𝑑𝑥 +𝑦 +𝑧
𝜕𝑥 𝜕𝑦 𝜕𝑧
Lab 8 :Verification of Euler’s theorem
#Program to verify Euler's theorem for u=(x**2+y**2)/(x+y)
from sympy import *
x,y=symbols('x,y')
n=int(input('Enter the degree'))
u=(x**2+y**2)/(x+y)
ux=diff(u,x)
uy=diff(u,y)
lhs=simplify(x*ux+y*uy)
rhs=n*u
if lhs==rhs:
print('Eulers theorem is satisfied')
else:
print('Eulers theorem not satisfied')

OUT PUT
Enter the degree1
Eulers theorem is satisfied

#Program to verify Euler's theorem extension for u=(x**3+y**3)/(x+y)


from sympy import *
x,y=symbols('x,y')
n=int(input('Enter the degree'))
u=(x**3+y**3)/(x+y)
ux=diff(u,x)
uxx=diff(u,x,2)
uyy=diff(u,y,2)
uxy=diff(u,x,y)
uy=diff(u,y)
lhs=simplify(x**2*uxx+2*x*y*uxy+y**2*uyy)
rhs=simplify((n*(n-1)*u))
if lhs==rhs:
print('Eulers theorem is satisfied')
else:
print('Eulers theorem not satisfied')

OUT PUT
Enter the degree2
Eulers theorem is satisfied

#Finding jacobian u=x*(1-y), v=x*y


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)

OUT PUT
jacobian(A)= x
>>>
#Finding jacobian x=r*cos(t), y=r*sin(t)
from sympy import *
x,y,r,t=symbols('x,y,r,t')
x=r*cos(t)
y=r*sin(t)
A=Matrix([x,y])
jacA=A.jacobian([r,t])
jacobian=simplify(det(jacA))
print('jacobian(A)=',jacobian)

OUT PUT
jacobian(A)= r

𝑬𝒙𝒆𝒓𝒄𝒊𝒔𝒆 ∶ 𝑉𝑒𝑟𝑖𝑓𝑦 𝑒𝑢𝑙𝑒𝑟𝑠 𝑡ℎ𝑒𝑜𝑟𝑒𝑚 𝑎𝑛𝑑 𝑖𝑡𝑠 𝑒𝑥𝑡𝑒𝑛𝑠𝑖𝑜𝑛 𝑓𝑜𝑟 𝑓𝑜𝑙𝑙𝑜𝑤𝑖𝑛𝑔


𝑥3 + 𝑦3 𝑥3 + 𝑦3
𝑖) 𝑖𝑖) (𝑥 2 − 𝑦 2 ) 𝑖𝑖𝑖)
𝑥−𝑦 𝑥
3 3
𝑥 +𝑦 𝑥 + 𝑦 3 + 3𝑥 2 𝑦
3
𝑖𝑣) 𝑣) 𝑥 2 + 𝑦 2 + 𝑥𝑦 𝑣𝑖)
𝑥 𝑥−𝑦

Lab 9 : python program for reduction formula with


or without limits
#Finding the integration of sinx^n
from sympy import *
def red(n):
x=symbols('x')
print(integrate(sin(x)**n,x))

OUT PUT
red(2)
x/2 - sin(x)*cos(x)/2
>>> red(4)
3*x/8 - sin(x)**3*cos(x)/4 - 3*sin(x)*cos(x)/8
#Finding the integration of sinx^m*cosx^n
from sympy import *
def red(m,n):
x=symbols('x')
print(integrate(sin(x)**m*cos(x)**n,x))

OUT PUT
>>> red(1,1)
sin(x)**2/2
>>> red(2,1)
sin(x)**3/3
>>> red(1,2)
-cos(x)**3/3
>>> red(2,2)
x/8 - sin(2*x)*cos(2*x)/16
'
#Finding the integration of sinx^n x: 0 t0 pi/2
from sympy import *
def red(n):
x=symbols('x')
print(integrate(sin(x)**n,[x,0,pi/2]))

OUT PUT
>>> red(1)
1
>>> red(3)
2/3
>>> red(10)
63*pi/512
>>> red(20)
46189*pi/524288

Exercise: Evaluate 𝑖) ∫ sin2 𝑥 cos2 𝑥 𝑑𝑥 𝑖𝑖) ∫ sin4 𝑥 𝑑𝑥 𝑖𝑖𝑖) ∫ cos5 𝑥 𝑑𝑥


𝜋 𝜋 𝜋
2 2 2

𝑖𝑣) ∫ sin5 𝑥 𝑑𝑥 𝑣) ∫ cos 6 𝑥 𝑑𝑥 𝑣𝑖𝑖) ∫ sin3 𝑥 cos 4 𝑥 𝑑𝑥


0 0 0
LAB 10.Sphere,Cone and Cylinder
10.1. Sphere

#Equation of the sphere with centre (a,b,c) and radius r


from sympy import *
from sympy.abc import x,y,z
def sphere(a,b,c,r):
eq=(x-a)**2+(y-b)**2+(z-c)**2-r**2
print('The equation of the sphere with centre ('
,a,b,c,') and radius r=',r,'is)\n', eq,'=',0)

OUT PUT
>>> sphere(0,0,0,2)
The equation of the sphere with centre ( 0 0 0 ) and radius r= 2 is)
x**2 + y**2 + z**2 - 4 = 0
>>> sphere(1,1,2,3)
The equation of the sphere with centre ( 1 1 2 ) and radius r= 3 is)
(x - 1)**2 + (y - 1)**2 + (z - 2)**2 - 9 = 0

#Equation of the sphere with two ends of diameter A(x1,y1,z1) and B(x2,y2,z2)
from sympy import *
from sympy.abc import x,y,z
def sphere(x1,y1,z1,x2,y2,z2):
eq=(x-x1)*(x-x2)+(y-y1)*(y-y2)+(z-z1)*(z-z2)
eq=simplify(eq)
print('The equation of the sphere with two ends of diameter A(',x1,y1,z1,
') and B(',x2,y2,z2,')is \n', expand(eq),'=',0)

OUT PUT
>>> sphere(1,2,3,5,6,7)
The equation of the sphere with two ends of diameter A( 1 2 3 ) and B( 5 6 7 )is
x**2 - 6*x + y**2 - 8*y + z**2 - 10*z + 38 = 0
>>>

'''Equation of the tangent plane to the sphere


x^2+y^2+z^2-2x-4y+2z-3=0 A(-1,4,2)'''
from sympy import *
from sympy.abc import x,y,z
u,v,w,d,x1,y1,z1=-1,-2,1,-3,-1,4,-2
print('Equation of tangent plane is')
print(x*x1+y*y1+z*z1+u*(x+x1)+v*(y+y1)+w*(z+z1)+d)

OUT PUT
Equation of tangent plane is
-2*x + 2*y - z – 12

10.2. Cone

'''Equation of the right circular cone with vertex (a,b,c)and its axis on
the line whose direction ratios are l,m,n and semivertical angle t '''
from sympy import *
from sympy.abc import x,y,z
from math import *
def cone(a,b,c,l,m,n,t):
eq=(l*(x-a)+m*(y-b)+n*(z-c))**2-(l**2+m**2+n**2)*((x-a)**2+(y-b)**2+(z-
c)**2)*(cos(radians(t)))**2
eq=expand(eq)
print('The equation of the cone is \n', eq,'=',0)

OUT PUT
>>> cone(0,0,0,0,0,1,30)
The equation of the cone is
-0.75*x**2 - 0.75*y**2 + 0.25*z**2 = 0
>>> cone(0,0,0,0,1,0,30)
The equation of the cone is
-0.75*x**2 + 0.25*y**2 - 0.75*z**2 = 0

10.3. Cylinder

'''Equation of the right circular cylinder whose axis is the line


(x-a)/l=(y-b)/m=(z-c)n and the radius r '''
from sympy import *
from sympy.abc import x,y,z
from math import *
def cylinder(a,b,c,l,m,n,r):
eq=(x-a)**2+(y-b)**2+(z-c)**2-((l*(x-a)+m*(y-b)+n*(z-c))**2)/(l**2+m**2+n**2)-r**2
eq=expand(eq)
print('The equation of the cylinder is \n', eq,'=',0)

OUT PUT
>>> cylinder(1,2,3,2,-3,6,2)
The equation of the cylinder is
45*x**2/49 + 12*x*y/49 - 24*x*z/49 - 6*x/7 + 40*y**2/49 + 36*y*z/49 - 40*y/7 + 13*z**2/49
- 18*z/7 + 6 = 0
>>> cylinder(1,3,5,2,2,-1,3)
The equation of the cylinder is
5*x**2/9 - 8*x*y/9 + 4*x*z/9 - 2*x/3 + 5*y**2/9 + 4*y*z/9 - 14*y/3 + 8*z**2/9 - 32*z/3 + 25
=0
>>> cylinder(1,0,3,2,3,1,2)
The equation of the cylinder is
5*x**2/7 - 6*x*y/7 - 2*x*z/7 - 4*x/7 + 5*y**2/14 - 3*y*z/7 + 15*y/7 + 13*z**2/14 - 37*z/7 +
59/14 = 0

You might also like