Lab3 - Lab4
Lab3 - Lab4
[2]: #lab 3
#sharankumar maga cse b 90
#partial derivatives
#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 ')
(𝑥cos(𝑦) − 𝑦sin(𝑦))𝑒𝑥
Ans : 0.0
In [5]: #jacobians
#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
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 \n")
display ( J )
Jac =det ( J ) . doit ()
print ('\n\n J = ', Jac )
𝑦𝑧 𝑥𝑧 − 𝑥𝑦𝑧2
− 𝑦𝑧𝑥2 𝑧𝑥 𝑦𝑥
𝑧𝑦 − 𝑥𝑧2 𝑥𝑦
𝑦
J = 4
In [6]: # If u = x + 3y
#2 − z
#3
#, v = 4x
#2
#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')
Jac1 = Determinant ( J1 ) . doit ()
display ( Jac1 )
1 6𝑦 −3𝑧2
8𝑥𝑦𝑧 4𝑥2 𝑧 4𝑥2 𝑦
−𝑦 −𝑥 6𝑧2
J =
20
In [7]: #X = ρ ∗ cos(ϕ) ∗ sin(θ), Y = ρ ∗ cos(ϕ) ∗ cos(θ), Z = ρ ∗ sin(ϕ) then find ∂(X
#∂(ρ,ϕ,θ)
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 () ) )
𝜌2 cos(𝜙)
In [14]: #lab 4
#maxima and minima problem
#find the maxima and minnima of f(x,y) = x^2+y^2+3x-3y+4
import sympy
from sympy import Symbol , solve , Derivative , pprint
x= Symbol ('x')
y= Symbol ('y')
f=x ** 2+x*y+y ** 2+3*x-3*y+4
d1= Derivative (f , x ) . doit ()
d2= Derivative (f , y ) . doit ()
criticalpoints1 = solve ( d1 )
criticalpoints2 = solve ( d2 )
s1= Derivative (f ,x , 2 ) . doit ()
s2= Derivative (f ,y , 2 ) . doit ()
s3= Derivative ( Derivative (f , y ) ,x ) . doit ()
print ('function value is ')
q1=s1 . subs ({y: criticalpoints1 , x: criticalpoints2 }) . evalf ()
q2=s2 . subs ({y: criticalpoints1 , x: criticalpoints2 }) . evalf ()
q3=s3 . subs ({y: criticalpoints1 , x: criticalpoints2 }) . evalf ()
delta =s1*s2-s3 ** 2
print ( delta , q1 )
if( delta >0 and s1<0 ):
print (" f takes maximum ")
elif ( delta >0 and s1>0 ):
print (" f takes minimum ")
if ( delta <0 ):
print ("The point is a saddle point ")
if ( delta ==0 ):
print (" further tests required ")
function value is
3 2.00000000000000
f takes minimum
In [19]: #tylor series expansion
#. Expand sin(x) as Taylor series about x = pi/2 upto 3rd degree term. Alsofind
import numpy as np
from matplotlib import pyplot as plt
from sympy import *
x= Symbol ('x')
y=sin( 1*x )
format
x0= float (pi/2 )
dy= diff (y , x )
d2y = diff (y ,x , 2 )
d3y = diff (y ,x , 3 )
yat = lambdify (x , y )
dyat = lambdify (x , dy )
d2yat = lambdify (x , d2y )
d3yat = lambdify (x , d3y )
y=yat(x0)+((x-x0)/2)*dyat(x0)+((x-x0)**2/6)*d2yat(x0)+((x-x0)**3/24 )*d3yat(x0
print ( simplify ( y ) )
yat=lambdify(x , y )
print ("%.3f" % yat (pi/2+10*(pi/180 ) ) )
def f( x ):
return np .sin ( 1*x )
x = np.linspace (-10, 10 )
plt.plot(x, yat(x), color='red')
plt.plot(x , f(x), color='green')
plt.ylim([-3, 3])
plt.grid()
plt.show()
In [ ]: