0% found this document useful (0 votes)
130 views9 pages

Lab3 - Lab4

The document contains code snippets demonstrating various calculus concepts including: 1) Checking if mixed partial derivatives are equal for a given function u. 2) Proving that the sum of second partial derivatives of a function u is equal to zero. 3) Calculating the Jacobian of different functions and proving the determinant is a specific value. 4) Finding the maxima and minima of a function f by taking its partial derivatives. 5) Expanding trigonometric functions like sin(x) as Taylor series and comparing to the actual function values. 6) Approximating functions like sin(x)+cos(x) using Maclaurin series expansions.

Uploaded by

Sharan maga
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)
130 views9 pages

Lab3 - Lab4

The document contains code snippets demonstrating various calculus concepts including: 1) Checking if mixed partial derivatives are equal for a given function u. 2) Proving that the sum of second partial derivatives of a function u is equal to zero. 3) Calculating the Jacobian of different functions and proving the determinant is a specific value. 4) Finding the maxima and minima of a function f by taking its partial derivatives. 5) Expanding trigonometric functions like sin(x) as Taylor series and comparing to the actual function values. 6) Approximating functions like sin(x)+cos(x) using Maclaurin series expansions.

Uploaded by

Sharan maga
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/ 9

In 

[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 ')

Mixed partial derivatives are equal

In [4]: #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 )
uyy = diff ( duy , y )
w=uxx+uyy
w1= simplify ( w )
print ('Ans :',float ( w1 ) )

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

The Jacobian matrix is

 𝑦𝑧 𝑥𝑧 − 𝑥𝑦𝑧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 )

The Jacobian matrix is

 1 6𝑦 −3𝑧2 
 8𝑥𝑦𝑧 4𝑥2 𝑧 4𝑥2 𝑦 
 −𝑦 −𝑥 6𝑧2 
J =

4𝑥3 𝑦 − 24𝑥2 𝑦3 + 12𝑥2 𝑦𝑧3 + 24𝑥2 𝑧3 − 288𝑥𝑦2 𝑧3


J at (1, -1,0):

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

The Jacobian matrix is

 sin(𝜃)cos(𝜙) cos(𝜙)cos(𝜃) sin(𝜙) 


 −𝜌sin(𝜙)sin(𝜃) −𝜌sin(𝜙)cos(𝜃) 𝜌cos(𝜙) 
 𝜌cos(𝜙)cos(𝜃) −𝜌sin(𝜃)cos(𝜙) 0 
J =

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

-2.55134749822365e-18*x**3 - 0.166666666666667*x**2 + 0.523598775598299*x +


0.588766483287943
0.995
In [23]: #maclurian series
import numpy as np
from matplotlib import pyplot as plt
from sympy import *
x= Symbol('x')
y=sin(x)+cos(x)
format
x0=float ( 0 )
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 ( 10*(pi/180 ) ) )

def f(x):
return np .sin ( 1*x )+np .cos ( 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()

-0.0416666666666667*x**3 - 0.166666666666667*x**2 + 0.5*x + 1.0


1.082
In [ ]: ​

In [ ]: ​

You might also like