0% found this document useful (0 votes)
121 views31 pages

Lab Manual

Uploaded by

aafiyainamdar77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views31 pages

Lab Manual

Uploaded by

aafiyainamdar77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Lab Component

of
First Semester Engineering Mathematics
as prescribed by Visvesvaraya Technological University, Belagavi

Compiled by:
Dr. Ramananda H. S. Dr. K. Sushan Bairy
St Joseph Engineering College, SOAS, REVA University,
Mangaluru, INDIA. Bengaluru, INDIA.

Dr. Smita S. Nagouda Dr. Madhukar Krishnamurthy


CHRIST(Deemed to be University), BMS College of Engineering,
Central Campus, Bengaluru, INDIA. Bull Temple Road, Bengaluru, INDIA.

Dr. Chandra Shekara G. Mr. Sonam Kumar


BMS College of Engineering, AMC Engineering college,
Bull Temple Road, Bengaluru, INDIA. Bannerghatta Road, Bengaluru, INDIA.
Contents
1. Introduction

I Basics of Python
II Programming Structure

Lab 1. 2D-Plots of Cartesian and Polar Curves

Lab 2. Finding Angle Between Two Polar Curves, Curvature and Radius of Curvature

Lab 3. Finding Partial Derivatives and Jacobian

Lab 4. Taylor Series Expansion and L’Hospital’s Rule

Lab 5. Solution of First Order Differential Equations and Plotting the Solution Curve

Lab 8. Numerical Solution of System of Equations, Test for Consistency and Graphical
Representation of the Solution.

Lab 9. Solution of Linear Equations by Gauss-Seidel Method

Lab 10. Compute Eigen Value and Corresponding Eigen Vectors, Find the Dominant Eigen
Value and Corresponding Eigen Vector by Rayleigh Power Method.

Computer Science and Engineering Stream


Lab 6. Finding GCD Using Euclid’s Algorithm

Lab 7. Solve Linear Congruence of the Form ax ≡ b(modn)

Electrical & Electronics Engineering Stream


Lab 6. Progamme to Compute Area, Volume and Center of Gravity

Lab 7. Evaluation of Improper Integrals

Mechanical & Civil Engineering Stream


Lab 6. Solution of Second Order Ordinary Differential Equation and Plotting the Solution
Curve

Lab 7. Solution of Differential Equation of Oscillations of Spring with Various Load

1
Instructions and method of evaluation
1. In each Lab student have to show the record of previous Lab.

2. Each Lab will be evaluated for 15 marks and finally average will be taken for 15
marks.

3. Viva questions shall be asked in labs and attendance also can be considered for
everyday Lab evaluation.

4. Tests shall be considered for 5 marks and final Lab assessment is for 20 marks.

5. Student has to score minimum 8 marks out of 20 to pass Lab component.

2
I. Introduction to PYTHON
https://drive.google.com/file/d/1gVG2IJ8BIjhYDwDx6jWJns59h9dGOGVi/view?usp=
share_link

II. Programming Structures


Conditional structure
What is conditioning in Python?
• Based on certain conditions, the flow of execution of the program is determined
using proper syntax.

• Often called decision-making statements in Python.

How to use if conditions?


- if statement — for implementing one-way branching

- if..else statements —for implementing two-way branching

- nested if statements —for implementing multiple branching

- if-elif ladder — for implementing multiple branching

# Syntax :

if condition :
statements

# Check if the given number is positive


a = int ( input ( " Enter an integer : " ) )
if a > 0 :
print ( " Entered value is positive " )

Enter an integer: 5
Entered value is positive

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

3
print ( " Number entered is positive " )
else :
print ( " Number entered is negative " )

Enter an integer: -5
Number entered is negative

# Syntax :
# if condition 1 :
# statements 1
# elif condition 2 :
# statements 2
# elif condition 3 :
# statements 3
# else :
# statements 4

# If condition 1 is True - Statements 1 will be executed .


# else if condition 2 is True - Statements 2 will be executed and so on
.
# If any of the conditions is not True then statements in else block is
executed .

# Example :

perc = float ( input ( " Enter the percentage of marks obtained by a student : "
))
if perc > = 75 :
print ( perc , ' % - Grade : Distinction ')
elif perc > = 60 :
print ( perc , ' % - Grade : First class ')
elif perc > = 50 :
print ( perc , ' % - Grade : Second class ')
else :
print ( perc , ' % - Grade : Fail ')

Enter the percentage of marks obtained by a student:65


65.0 % - Grade: First class

# To check if a number is divisble by 7


num1 = int ( input ( " Enter a number : " ) )
if ( num1 % 7 = = 0 ) :
print ( " Divisible by 7 " )
else :
print ( " The given number is not divisible by 7 " )

Enter a number:45
The given number is not divisible by 7

4
# Conversion Celsius to Fahrenheit and vice - versa :
def print_menu () :
print ( " 1 . Celsius to Fahrenheit " )
print ( " 2 . Fahrenheit to Celsius " )

def Far () :
c = float ( input ( " Enter Temperature in Celsius : " ) )
f = c * ( 9 / 5 ) + 32
print ( " Temperature in Fahrenheit : { 0 : 0 . 2f } " . format ( f ) )

def Cel () :
f = float ( input ( " Enter Temperature in Fahrenheit : " ) )
c = ( f - 32 ) * ( 5 / 9 )
print ( " Temperature in Celsius : { 0 : 0 . 2f } " . format ( c ) )

print_menu ()
choice = input ( " Which conversion would you like : " )
if ( choice = = '1 ') :
Far ()
elif ( choice = = '2 ') :
Cel ()
else : print ( " INVALID " )

1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Which conversion would you like: 1
Enter Temperature in Celsius: 34
Temperature in Fahrenheit: 93.20

Control flow (Loops)


Loop types:
while loop
- Repeats a statement or group of statements while a given condition is TRUE. It tests
the condition before executing the loop body.

for loop
- Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.

nested loops
- You can use one or more loop inside any another while, for or do..while loop.

5
1. While loop
• Is used to execute a block of statements repeatedly until a given condition is satis-
fied.

• When the condition becomes false, the line immediately after the loop in the pro-
gram is executed

• Syntax:
while expression :
statement ( s )

# Fibonacci series :
# the sum of two elements defines the next
a, b = 0, 1 # First step : a = 0 ; b = 1 second step : a = 1 ; b = 1 + 0
while a < 10 :
a , b =b , a + b
print ( a )

1
1
2
3
5
8
13

# Print multiplication table


n = int ( input ( " Enter the number : " ) )
i=1
while ( i < 11 ) :
print (n , 'x ' ,i , '= ' ,n * i )
i=i+1

Enter the number: 45


45 x 1 = 45
45 x 2 = 90
45 x 3 = 135
45 x 4 = 180
45 x 5 = 225
45 x 6 = 270
45 x 7 = 315
45 x 8 = 360
45 x 9 = 405
45 x 10 = 450

6
break statement
• It terminates the current loop and resumes execution at the next statement.

• The most common use for break is when some external condition is triggered re-
quiring a hasty exit from a loop.

• The break statement can be used in both while and for loops.

• If you are using nested loops, the break statement stops the execution of the inner-
most loop and start executing the next line of code after the block.

# Use of break ststement


i=1
while i < 6 :
print ( i )
if i = = 3 :
break
i+=1

1
2
3

Continue statement
• The continue statement rejects all the remaining statements in the current iteration
of the loop and moves the control back to the top of the loop.

• The continue statement can be used in both while and for loops.

i=0
while i < 6 :
i+=1
if i = = 3 :
continue
print ( i )

1
2
4
5
6

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

The range() function


Syntax:
• range(a) : Generates a sequence of numbers from 0 to a, excluding a, incrementing
by 1.

• range(a,b): Generates a sequence of numbers from a to b excluding b, increment-


ing by 1.

• range(a,b,c): Generates a sequence of numbers from a to b excluding b, incre-


menting by c.

# Print numbers from 101 to 130 with a step length 2 excluding 130 .
for i in range ( 101 , 130 , 2 ) :
print ( i )

101
103
105
107
109
111
113
115
117
119
121
123
125
127
129

8
One can type the following examples and observe the outputs.
# Sum of first n natural numbers
sum = 0
n = int ( input ( " Enter n : " ) )
for i in range (1 , n + 1 ) : # i =1 , sum = 1 ; i =2 , sum = 3 ; i =4 , sum =7 , ....
sum = sum + i
print ( " Sum of first " ,n , " natural numbers = " , sum )

# Multiplication table
n = int ( input ( " Enter the number " ) )
for i in range (1 , 11 ) :
print (n , 'x ' ,i , '= ' ,n * i )

# printing the elements of a list


fruits = [ ' apple ' , ' banana ' , ' cherry ' , ' orange ']
for x in fruits :
print ( x )

apple
banana
cherry
orange

Exercise:
1. Finding the factors of a number using for loop.

2. Check the given number is prime or not.

3. Find largest of three numbers.

4. Write a program to print even numbers between 25 and 45.

5. Write a program to print all numbers divisible by 3 between 55 and 75.

9
LAB 1: 2D plots of Cartesian and polar curves.
1.1 Objectives:
Use python
1. to plot Cartesian curves.
2. to plot polar curves.
3. to plot implicit functions.

Syntax for the commands used:


1. 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 )

2. A scatter plot of y versus x with varying marker size and/or color.


scatter ( x_axis_data , y_axis_data , s = None , c = None , marker = None , cmap
= None , vmin = None , vmax = None ,
alpha = None , linewidths = None ,
edgecolors = None )

3. Return num evenly spaced numbers over a specified interval [start, stop]. The
endpoint of the interval can optionally be excluded.
numpy . linspace ( start , stop , num = 50 , endpoint = True , retstep = False ,
dtype = None , axis = 0 )

4. 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 )

https://matplotlib.org/stable/api/pyplot_summary.html#module-matplotlib.pyplot

1.2 Example: Plotting points(Scattered plot)


# 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 . scatter (x , y ) # plotting the points
plt . xlabel ( 'x - axis ') # naming the x axis
plt . ylabel ( 'y - axis ') # naming the y axis
plt . title ( ' Scatter points ') # giving a title to my graph
plt . show () # function to show the plot

10
1.3 Example: Plotting a line(Line plot)
# 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

11
1.4 Functions
1. Exponential curve, y = ex

# importing the required modules


import numpy as np
import matplotlib . pyplot as plt

x = np . arange ( - 10 , 10 , 0 . 001 ) # x takes the values between - 10 and 10


with a step length of 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

2. Sine and Cosine curves

import numpy as np
import matplotlib . pyplot as plt

x = np . arange ( - 10 , 10 , 0 . 001 )
y1 = np . sin ( x )
y2 = np . cos ( x )
plt . plot (x , y1 ,x , y2 ) # plotting sine and cosine function together with
same values of x
plt . title ( " sine curve and cosine curve " )
plt . xlabel ( " Values of x " )
plt . ylabel ( " Values of sin ( x ) and cos ( x ) " )
plt . grid ()
plt . show ()

12
# A simple graph
import matplotlib . pyplot as plt
import numpy as np

x = np . linspace (0 , 2 , 100 )

plt . plot (x , x , label = ' linear ') # Plot of y = x a linear curve


plt . plot (x , x ** 2 , label = ' quadratic ') # Plot of y = x ^ 2 a quadric curve
plt . plot (x , x ** 3 , label = ' cubic ') # Plot of y = x ^ 3 a cubic curve

plt . xlabel ( 'x label ') # Add an x - label to the axes .


plt . ylabel ( 'y label ') # Add a y - label to the axes .

plt . title ( " Simple Plot " ) # Add a title to the axes .
plt . legend () # Add a legend
plt . show () # to show the complete graph

13
1.5 Implicit Function
Syntax:

plot_implicit ( expr , x_var = None , y_var = None , adaptive = True , depth =0 ,


points = 300 , line_color = ' blue ' , show
= True , ** kwargs )

• expr : The equation / inequality that is to be plotted.


• x var (optional) : symbol to plot on x-axis or tuple giving symbol and range as
(symbol, xmin, xmax)
• y var (optional) : symbol to plot on y-axis or tuple giving symbol and range as
(symbol, ymin, ymax)
• If neither x var nor y var are given then the free symbols in the expression will be
assigned in the order they are sorted.
• The following keyword arguments can also be used:
– adaptive: Boolean. The default value is set to True. It has to beset to False
if you want to use a mesh grid.
– depth : integer. The depth of recursion for adaptive mesh grid. Default value
is 0. Takes value in the range (0, 4).
– points: integer. The number of points if adaptive mesh grid is not used.
Default value is 300.
– show: Boolean. Default value is True. If set to False, the plot will not be
shown. See Plot for further information.
• title string. The title for the plot.
• xlabel string. The label for the x-axis
• ylabel string. The label for the y-axis
Aesthetics options:
• line color: float or string. Specifies the color for the plot

1.5.1 Plot the following


1. Circle: x2 + y 2 = 5

# importing Sympy package with plot_implicit , symbols and Eq functions


only
# symbols : used to declare variable as symbolic expression
# Eq : sets up an equation . Ex : 2x - y = 0 is written as Eq ( 2 *x -y , 0 )

from sympy import plot_implicit , symbols , Eq


x , y = symbols ( 'x y ')
p1 = plot_implicit ( Eq ( x ** 2 + y ** 2 , 4 ) ,(x , -4 , 4 ) ,(y , -4 , 4 ) ,
title = ' Circle : $x ^ 2 + y ^ 2 = 4$ ') # r = 2

14
2. Strophoid: y 2 (a − x) = x2 (a + x), a > 0

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

3. Cissiod: y 2 (a − x) = x3 , a > 0

15
p4 = plot_implicit (
Eq (( y ** 2 ) * ( 3 - x ) ,x ** 3 ) ,(x , -2 , 5 ) ,(y , -5 , 5 ) ) # a = 3

4. Lemniscate: a2 y 2 = x2 (a2 − x2 )

p5 = plot_implicit (
Eq ( 4 * ( y ** 2 ) ,( x ** 2 ) * ( 4 - x ** 2 ) ) ,(x , -5 , 5 ) ,(y , -5 , 5 ) ) # a = 2

16
5. Folium of De-Cartes: x3 + y 3 = 3axy

p6 = plot_implicit (
Eq ( x ** 3 + y ** 3 , 3 * 2 * x * y ) ,(x , -5 , 5 ) ,(y , -5 , 5 ) ) # a = 2

1.6 Polar Curves


The matplotlib.pyplot.polar() function in pyplot module of matplotlib python
library is used to plot the curves in polar coordinates.
Syntax:
matplotlib . pyplot . polar ( theta , r , ** kwargs )

• Theta: This is the angle at which we want to draw the curve.

• r: It is the distance.

1. Circle: r = p,Where p is the radius of the circle

import numpy as np
import matplotlib . pyplot as plt

plt . axes ( projection = ' polar ')


r = 3
rads = np . arange (0 , ( 2 * np . pi ) , 0 . 01 )

17
# plotting the circle
for i in rads :
plt . polar (i , r , 'g . ')
plt . show ()

3. Cardioid: r = 5(1 + cosθ)

# Plot cardioid r = 5 ( 1 + cos theta )


from pylab import *
theta = linspace (0 , 2 * np . pi , 1000 )
r1 = 5 + 5 * cos ( theta )

polar ( theta , r1 , 'r ')


show ()

18
4. Four leaved Rose: r = 2|cos2x|

# Plot Four Leaved Rose r = 2 | cos2x |


from pylab import *
theta = linspace (0 , 2 * pi , 1000 )
r = 2 * abs ( cos ( 2 * theta ) )
polar ( theta ,r , 'r ')
show ()

5. Cardioids: r = a + acos(θ) and r = a − acos(θ)

import numpy as np
import matplotlib . pyplot as plt
import math

plt . axes ( projection = ' polar ')


a=3

rad = np . arange (0 , ( 2 * np . pi ) , 0 . 01 )
# plotting the cardioid
for i in rad :
r = a + ( a * np . cos ( i ) )
plt . polar (i ,r , 'g . ')
r1 = a - ( a * np . cos ( i ) )
plt . polar (i , r1 , 'r . ')
# display the polar plot
plt . show ()

19
1.7 Parametric Equation
1. Circle: x = acos(θ); y = asin(θ)

import numpy as np
import matplotlib . pyplot as plt
def circle ( r ) :
x = [ ] # create the list of x coordinates
y = [ ] # create the list of y coordinates

for theta in np . linspace ( - 2 * np . pi , 2 * np . pi , 100 ) :


# loop over a list of theta , which ranges from -2 pi to 2 pi
x . append ( r * np . cos ( theta ) )
# add the corresponding expression of x to the x list
y . append ( r * np . sin ( theta ) )
# same for y

plt . plot (x , y ) # plot using matplotlib . piplot


plt . show () # show the plot

circle ( 5 ) # call the function

20
2. Cycloid: x = a(θ − sinθ); y = a(1 − sinθ)

def cycloid ( r ) :
x = [ ] # create the list of x coordinates
y = [ ] # create the list of y coordinates

for theta in np . linspace ( - 2 * np . pi , 2 * np . pi , 100 ) :


# loop over a list of theta , which ranges from -2 pi to 2 pi
x . append ( r * ( theta - np . sin ( theta ) ) )
# add the corresponding expression of x to the x list
y . append ( r * ( 1 - np . cos ( theta ) ) ) # same for y

plt . plot (x , y ) # plot using matplotlib . piplot


plt . show () # show the plot

cycloid ( 2 ) # call the function

1.8 Exercise:
Plot the following:
1. Parabola y 2 = 4ax
x2 y2
2. Hyperbola a2
− b2
=1
3. Lower half of the circle: x2 + 2x = 4 + 4y − y 2
4. cos( πx
2
)
5. 1 + sin(x + π4 )
6. Spiral of Archimedes: r = a + bθ
7. Limacon: r = a + b cosθ

21
LAB 2: Finding angle between two polar curves, cur-
vature and radius of curvature.
2.1 Objectives:
Use python

1. To find angle between two polar curves.

2. To find radius of curvature.

Syntax for the commands used:


1. diff()
diff ( function , variable )

2. Derivative()
Derivative ( expression , reference variable )

• expression – A SymPy expression whose unevaluated derivative is found.


• reference variable – Variable with respect to which derivative is found.
• Returns: Returns an unevaluated derivative of the given expression.

3. doit()
doit ( x )

4. Return : evaluated object

5. simplify()
simplify ( expression )

6. expression – It is the mathematical expression which needs to be simplified.

7. Returns: Returns a simplified mathematical expression corresponding to the input


expression.

8. display()
display ( expression )

9. expression – It is the mathematical expression which needs to be simplified.

10. Returns: Displays the expression.

11. syntax of Substitute : subs()

22
math_expression . subs ( variable , substitute )

12. variable – It is the variable or expression which will be substituted.

13. substitute – It is the variable or expression or value which comes as substitute.

14. Returns: Returns the expression after the substitution.

2.2 1. Angle between two polar curves


Angle between radius vector and tangent is given by tan ϕ = r dθ dr
.
If tan ϕ1 and tan ϕ2 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 r = 4(1 + cos t) and r = 5(1 − cos t).

from sympy import *

r , t = symbols ( 'r , t ') # Define the variables required as symbols

r1 = 4 * ( 1 + cos ( t ) ) ; # Input first polar curve


r2 = 5 * ( 1 - cos ( t ) ) ; # Input first polar curve
dr1 = diff ( r1 , t ) # find the derivative of first function
dr2 = diff ( r2 , t ) # find the derivative of secodn function
t1 = r1 / dr1
t2 = r2 / dr2
q = solve ( r1 - r2 , t ) # solve r1 == r2 , to find the point of intersection
between curves
w1 = t1 . subs ( { t : float ( q [ 1 ] ) } ) # substitute the value of " t " in t1
w2 = t2 . subs ( { t : float ( q [ 1 ] ) } ) # substitute the value of " t " in t2
y1 = atan ( w1 ) # to find the inverse tan of w1
y2 = atan ( w2 ) # to find the inverse tan of w2
w = abs ( y1 - y2 ) # angle between two curves is abs ( w1 - w2 )
print ( ' Angle between curves in radians is % 0 . 3f '% ( w ) )

2. Find the angle between the curves r = 4 cos t and r = 5 sin t.

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 )

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

2.3 2. Radius of curvature


(r2 + r12 )3/2
Formula to calculate Radius of curvature in polar form is ρ =
r2 + 2r12 − rr2

1. Find the radius of curvature, r = 4(1 + cos t) at t=π/2.

from sympy import *


t = Symbol ( 't ') # define t as symbol
r = Symbol ( 'r ')
r = 4 * ( 1 + cos ( t ) )
r1 = Derivative (r , t ) . doit () # find the first derivative of r w . r . t " t "
r2 = Derivative ( r1 , t ) . doit () # find the second derivative of r w . r . t " t "
rho = ( r ** 2 + r1 ** 2 ) ** ( 1 . 5 ) / ( r ** 2 + 2 * r1 ** 2 - r * r2 ) ; # Substitute r1 and r2 in
formula
rho1 = rho . subs (t , pi / 2 ) # substitute t in rho
print ( ' The radius of curvature is % 3 . 4f units '% rho1 )

2. Find the radius of curvature for r = asin(nt) at t = pi/2 and n = 1.

from sympy import *


t ,r ,a , n = symbols ( 't r a n ')
r = a * sin ( n * t )
r1 = Derivative (r , t ) . doit ()
r2 = Derivative ( r1 , t ) . doit ()
rho = ( r ** 2 + r1 ** 2 ) ** 1 . 5 / ( r ** 2 + 2 * r1 ** 2 - r * r2 ) ;
rho1 = rho . subs (t , pi / 2 )
rho1 = rho1 . subs (n , 1 )
print ( " The radius of curvature is " )
display ( simplify ( rho1 ) )

2.4 Parametric curves


3
(x′2 +y ′2 ) 2
The formula to calculate Radius of curvature is ρ = y ′′ x′ −x′′ y ′
.
d2 x dy d2 y
x′ = dx
dt
, x′′ = dt2
, y′ = dt
, y ′′ = dt2

1. Find radius of curvature of x = acos(t), y = asin(t).

24
from sympy import *
from sympy . abc import rho , x ,y ,r ,K ,t ,a ,b ,c , alpha # define all symbols
required
y = ( sqrt ( x ) - 4 ) ** 2
y = a * sin ( t ) # input the parametric equation
x = a * cos ( t )
dydx = simplify ( Derivative (y , t ) . doit () ) / simplify ( Derivative (x , t ) . doit () )
# find the derivative of parametric
equation
rho = simplify (( 1 + dydx ** 2 ) ** 1 . 5 / ( Derivative ( dydx , t ) . doit () / ( Derivative (x ,
t ) . doit () ) ) ) # substitute the
derivative in radius of curvature
formula
print ( ' Radius of curvature is ')
display ( ratsimp ( rho ) )
t1 = pi / 2
r1 = 5
rho1 = rho . subs (t , t1 ) ;
rho2 = rho1 . subs (a , r1 ) ;
print ( '\ n \ nRadius of curvature at r = 5 and t = pi / 2 is ' , simplify ( rho2 ) ) ;
curvature = 1 / rho2 ;
print ( '\ n \ n Curvature at (5 , pi / 2 ) is ' , float ( curvature ) )

2. Find the radius of curvature of y = (asin(t))3/2 ; x = (acos(t))3/2 .

from sympy import *


from sympy . abc import rho , x ,y ,r ,K ,t ,a ,b ,c , alpha
y = ( a * sin ( t ) ) ** ( 3 / 2 )
x = ( a * cos ( t ) ) ** ( 3 / 2 )
dydx = simplify ( Derivative (y , t ) . doit () ) / simplify ( Derivative (x , t ) . doit () )
rho = simplify (( 1 + dydx ** 2 ) ** 1 . 5 / ( Derivative ( dydx , t ) . doit () / ( Derivative (x ,
t ) . doit () ) ) )
print ( ' Radius of curvature is ')
display ( ratsimp ( rho ) )
t1 = pi / 4
r1 = 1 ;
rho1 = rho . subs (t , t1 ) ;
rho2 = rho1 . subs (a , r1 ) ;
display ( ' Radius of curvature at r = 1 and t = pi / 4 is ' , simplify ( rho2 ) ) ;
curvature = 1 / rho2 ;
print ( '\ n \ n Curvature at (1 , pi / 4 ) is ' , float ( curvature ) )

2.5 Exercise:
Plot the following:

1. Find the angle between radius vector and tangent to the folloing polar curves
a) r = aθ and r = aθ
Ans: Angle between curves in radians is 90.000
b) r = 2sin(θ) and r = 2cos(θ)
Ans: Angle between curves in radians is 90.000

25
2. Find the radius of curvature of r = a(1 − cos(t)) at t = π2 .
2 )1.5
Ans: 0.942809041582063(a
a2

3. Find radius of√curvature of x = acos3 (t), y = asin3 (t) at t = 0.


Ans: ρ = 0.75 3 and κ = 0.769800

4. Find the radius of curvature of r = acos(t) at t = π4 .


2 1.5
Ans: (a2a)2

5. Find the radius of curvature of x = a(t − sin(t)) andy = a(1 − cos(t)) at t = π/2.
Ans: ρ = 2.82842712 and κ = 0.353553

26
LAB 3: Finding partial derivatives and Jacobian of
functions of several variables.
3.1 Objectives:
Use python
1. to find partial derivatives of functions of several variables.
2. to find Jacobian of function of two and three variables.

Syntax for the commands used:


1. To create a matrix:
Matrix ( [ [ row1 ] ,[ row2 ] ,[ row3 ] .... [ rown ] ] )

Ex: A 3 × 3 matrix can be defined as


Matrix ( [ [ a11 , a12 , a13 ] ,[ a21 , a22 , a23 ] ,[ a31 , a32 a33 ] ] )

2. Evaluate the determinant of a matrix M .


Determinant ( M )
det ( M )

3. To evaluates derivative of function w.r.t variable.


diff ( function , variable )

4. If function is of two or more than two independent variable then it differentiates


the function partially w.r.t variable.
If u = u(x, y) then,
• ∂u
∂x
= dif f (u, x)
• ∂u
∂y
= dif f (u, y)
∂2u
• ∂x2
= dif f (u, x, x)
∂2u
• ∂x∂y
= dif f (u, x, y)

3.2 I. Partial derivatives


The partial derivative of f (x, y) with respect to x at the point (x0 , y0 ) is
∂f f (x0 + h, y0 ) − f (x0 , y0 )
fx = at(x0 , y0 ) = lim .
∂x h→0 h
The partial derivative of f (x, y) with respect to xy at the point (x0 , y0 ) is
∂f f (x0 , y0 + h) − f (x0 , y0 )
fy = at(x0 , y0 ) = lim .
∂y h→0 h

27
1. Prove that mixed partial derivatives , uxy = uyx for u = exp(x)(xcos(y) −
ysin(y)).

from sympy import *


x , y = symbols ( 'x y ')

u = exp ( x ) * ( x * cos ( y ) - y * sin ( y ) ) # input mutivariable function u = u (x , y )


dux = diff (u , x ) # Differentate u w . r . t x
duy = diff (u , y ) # Differentate u w . r . t . y
duxy = diff ( dux , y ) # or duxy = diff (u ,x , y )
duyx = diff ( duy , x ) # or duyx = diff (u ,y , x )
# Check the condtion uxy = uyx
if duxy = = duyx :
print ( ' Mixed partial derivatives are equal ')
else :
print ( ' Mixed partial derivatives are not equal ')

2. Prove that if u = ex (x cos(y) − y sin(y)) then uxx + uyy = 0.

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 ) # or uxx = diff (u ,x , x ) second derivative of u w . r . t x
uyy = diff ( duy , y ) # or uyy = diff (u ,y , y ) second derivative of u w . r . t y
w = uxx + uyy # Add uxx and uyy
w1 = simplify ( w ) # Simply the w to get actual result
print ( ' Ans : ' , float ( w1 ) )

3.3 II Jacobians
Let x = g(u, v) and y = h(u, v) be a transformation of the plane. Then the Jacobian of
this transformation is
∂x ∂x
∂(x, y)
J= = ∂u
∂y
∂v
∂y .
∂(u, v) ∂x ∂v

1. If u = xy/z, v = yz/x, w = zx/y then prove that J = 4.

from sympy import *

x ,y , z = symbols ( 'x ,y , z ')

u=x*y/z
v=y*z/x
w=z*x/y
# find the all first order partial derivates
dux = diff (u , x )

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

# construct the Jacobian matrix


J = Matrix ( [ [ dux , duy , duz ] ,[ dvx , dvy , dvz ] ,[ dwx , dwy , dwz ] ] ) ;

print ( " The Jacobian matrix is \ n " )


display ( J )

# Find the determinat of Jacobian Matrix


Jac = det ( J ) . doit ()
print ( '\ n \ n J = ' , Jac )

2. If u = x + 3y 2 − z 3 , v = 4x2 yz, w = 2z 2 − xy then prove that at (1, −1, 0), J = 20.

from sympy import *

x ,y , z = symbols ( 'x ,y , z ')

u = x + 3 * y ** 2 - z ** 3
v = 4 * x ** 2 * y * z
w = 2 * z * z ** 2 - 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 ] ] ) ;

print ( " The Jacobian matrix is " )


display ( J )

Jac = Determinant ( J ) . doit ()


print ( '\ n \ n J = \ n ')
display ( Jac )

J1 = J . subs ( [ (x , 1 ) , (y , - 1 ) , (z , 0 ) ] )

print ( '\ n \ n J at (1 , -1 , 0 ) :\ n ')

29
Jac1 = Determinant ( J1 ) . doit ()
display ( Jac1 )

∂(X,Y,Z)
3. X = ρ ∗ cos(ϕ) ∗ sin(θ), Y = ρ ∗ cos(ϕ) ∗ cos(θ), Z = ρ ∗ sin(ϕ) then find ∂(ρ,ϕ,θ)
.

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

3.4 Exercise:
Plot the following:
∂2u ∂2u
1. If u = tan−1 (y/x) verify that ∂y∂x
= ∂x∂y
.
Ans:True
2 2
2. If u = log( xx+y
+y
) show that xux + yuy = 1.
Ans: True

3. If x = u − v, y = v − uvw and z = uvw find Jacobian of x, y, z w.r.t u, v, w.


Ans: uv
∂(x,y)
4. If x = rcos(t) and y = rsin(t) then find the ∂(r,t)
.
Ans: J = r
∂(u,v,w)
5. If u = x + 3y 2 − z 3 , v = 4x2 yz and w = 2z 2 − xy find ∂(x,y,z)
at (-2,-1,1).
Ans: 752

30

You might also like