Maths Python Lab
Lab Set 6: Finding partial derivatives and Jacobian of functions
of several variables.
1.Prove that if u=𝑒 𝑥 (𝑥𝑐𝑜𝑠 𝑦 − 𝑦𝑠𝑖𝑛 𝑦 ) then 𝑢𝑥𝑥 + 𝑢𝑦𝑦 = 0.
Program Code:
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)
display(w1)
Output:
(𝑥cos(𝑦)−𝑦sin(𝑦))
0
2.If 𝑢 = 𝑥 + 3𝑦 2 − 𝑧 3 , 𝑣 = 4𝑥 2 𝑦𝑧, 𝑤 = 2𝑧 2 − 𝑥𝑦 then prove that at (1,-1,0),J=20.
Program Code:
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**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\nJ=\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)
Output:
The Jacobian matrix is
1 6𝑦 −3𝑧 2
8𝑥𝑦𝑧 4𝑥 2 𝑧 4𝑥 2 𝑦
−𝑦 −𝑥 4𝑧
J=
4𝑥 3 𝑦 − 24𝑥 2 𝑦 3 + 12𝑥 2 𝑦𝑧 3 + 16𝑥 2 𝑧 2 − 192𝑥𝑦 2 𝑧 2
J at (1,-1,0):
20
Maths Python Lab
Lab Set 7: Applications of taylor's series expansion and
L'Hospital's Rule.
1.Expand sin(x) as Taylor series about x=pi/2 upto 3rd degree term.
Also find 𝑠𝑖𝑛(1000 )
Program Code:
import numpy as np
from matplotlib import pyplot as plt
from sympy import*
x=Symbol('x')
y=sin(1*x)
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()
Output:
-2.55134749822365e-18*x**3 - 0.166666666666667*x**2 + 0.523598775598299*x + 0.588766483287943
0.995
2.Find the Maclaurin series expansion of sin(x)+cos(x) upto 3rd degree
term. Calculate sin(10)+cos(10).
Program Code:
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(pi/2+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()
Output:
-0.0416666666666667*x**3 - 0.166666666666667*x**2 + 0.5*x + 1.0
1.143
sin
(𝑥)
3.lim𝑥→0
𝑥
Program Code:
from sympy import Limit,Symbol,exp,sin
x=Symbol('x')
l=Limit((sin(x))/x,x,0).doit()
print(l)
Output:
1
(5x 4 −4x 2 −1)
4.𝐸𝑣𝑎𝑙𝑢𝑎𝑡𝑒 lim𝑥→1
(10−𝑥−9𝑥 3 )
Program Code:
from sympy import*
x=Symbol('x')
l=Limit((5*x**4-4*x**2-1)/(10-x-9*x**3),x,1).doit()
print(l)
Output:
-3/7
1 𝑥
5.𝑃𝑟𝑜𝑣𝑒 𝑡ℎ𝑎𝑡 lim𝑥→∞ 1 + =𝑒
𝑥
Program Code:
from sympy import *
import numpy as np
x=Symbol('x')
l=Limit((1+1/x)**x,x,np.inf).doit()
display(l)
Output:
e