Skip to main content

Tutorial 2

Binder Open In Colab

Interactive Notebooks

Click the badges above to run this tutorial interactively in your browser without installing anything!

  • Binder: Free cloud-based Jupyter environment
  • Colab: Google's free Jupyter notebook environment

Topics Covered​

This tutorial covers the following topics using Python and SymPy:

  • Partial Derivatives: Using limit definition and rules.
  • Chain Rule: For composite functions.
  • Implicit Differentiation: For multivariable functions.
  • Jacobians: For transformations.

Introduction​

Sympy: SymPy is a Python library for symbolic mathematics. We will use this library to solve all the tutorials.

import sympy as sy
from sympy.abc import x, y, z, s, t, r, theta, u, v, alpha # variables
from sympy.solvers import solve # Solving equations
from sympy import sqrt,tan,sin,cos,sec,pi,root,ln, E
from sympy import log,exp,atan,asinh,atanh,asin # Import all required math function
from sympy import diff, idiff, Matrix # Solving differentiation
from sympy import simplify, Rational # Simplifying expressions
sy.init_printing(use_latex=True) # Show it in natural display

Question 1​

Find the partial derivative (∂f∂y)\displaystyle\left(\frac{\partial f}{\partial y}\right) and (∂f∂x)\displaystyle\left(\frac{\partial f}{\partial x}\right) of these functions using the limit definition

a. f(x,y)=x2y+2x+y3f(x, y)=x^{2} y+2 x+y^{3}

f = x**2*y + 2*x + y**3
display(diff(f, x))
display(diff(f, y))

b. f(x,y)=x2−4xy+y2f(x, y)=x^{2}-4 x y+y^{2}

f = x**2 - 4*x*y + y**2
display(diff(f, x))
display(diff(f, y))

c. f(x,y)=2x3+3xy−y2f(x, y)=2 x^{3}+3 x y-y^{2}

f = 2*x**3 + 3*x*y - y**2
display(diff(f, x))
display(diff(f, y))

Question 2​

Determine all the first and second order partial derivatives of the function

a. f(x,y)=x2y3+3y+xf(x, y)=x^{2} y^{3}+3 y+x

f = x**2*y**3 + 3*y + x
display(diff(f, x))
display(diff(f, y))
display(diff(f, x, x))
display(diff(f, x, y))
display(diff(f, y, x))
display(diff(f, y, y))

b. f(x,y)=x4sin⁡3yf(x, y)=x^{4} \sin 3 y

f = x**4*sin(3*y)
display(diff(f, x))
display(diff(f, y))
display(diff(f, x, x))
display(diff(f, x, y))
display(diff(f, y, x))
display(diff(f, y, y))

c. f(x,y)=x2y+ln⁡(y2−x)f(x, y)=x^{2} y+\ln \left(y^{2}-x\right)

f = x**2*y + ln(y**2 - x)
display(diff(f, x))
display(diff(f, y))
display(diff(f, x, x))
display(diff(f, x, y))
display(diff(f, y, x))
display(diff(f, y, y))

d. f(x,y)=exy(2x−y)f(x, y)=e^{x y}(2 x-y)

f = exp(x*y)*(2*x - y)
display(diff(f, x))
display(diff(f, y))
display(diff(f, x, x))
display(diff(f, x, y))
display(diff(f, y, x))
display(diff(f, y, y))

Question 3​

Find both partial derivatives for each of the following two variables functions

a. g(x,y)=yex+yg(x, y)=y e^{x+y}

g = y*exp(x+y)
display(diff(g, x))
display(diff(g, y))

b. h(x,y)=xsin⁡y−ycos⁡xh(x, y)=x \sin y-y \cos x

h = x*sin(y) - y*cos(x)
display(diff(h, x))
display(diff(h, y))

c. p(x,y)=xy+y2p(x, y)=x^{y}+y^{2}

p = x**y + y**2
display(diff(p, x))
display(diff(p, y))

d. U(x,y)=9y3x−yU(x, y)=\frac{9 y^{3}}{x-y}

U = (9*y**3)/(x-y)
display(diff(U, x))
display(diff(U, y))

Question 4​

For f(x,y,z)f(x, y, z), use the implicit function theorem to find dy/dxd y / d x and dy/dzd y / d z

a. f(x,y,z)=x2y3+z2+xyzf(x, y, z)=x^{2} y^{3}+z^{2}+x y z

f = x**2*y**3 + z**2 + x*y*z
dydx = -diff(f, x) / diff(f, y)
dydz = -diff(f, z) / diff(f, y)
display(dydx)
display(dydz)

b. f(x,y,z)=x3z2+y3+4xyzf(x, y, z)=x^{3} z^{2}+y^{3}+4 x y z

f = x**3*z**2 + y**3 + 4*x*y*z
dydx = -diff(f, x) / diff(f, y)
dydz = -diff(f, z) / diff(f, y)
display(dydx)
display(dydz)

c. f(x,y,z)=3x2y3+xz2y2+y3zx4+y2zf(x, y, z)=3 x^{2} y^{3}+x z^{2} y^{2}+y^{3} z x^{4}+y^{2} z

f = 3*x**2*y**3 + x*z**2*y**2 + y**3*z*x**4 + y**2*z
dydx = -diff(f, x) / diff(f, y)
dydz = -diff(f, z) / diff(f, y)
display(dydx)
display(dydz)

Question 5​

Find ∂F∂s\frac{\partial F}{\partial s} and ∂F∂t\frac{\partial F}{\partial t}, if applicable, for the following composite functions

a. F=sin⁡(x+y)F=\sin (x+y) where x=2stx=2st and y=s2+t2y=s^2+t^2

F = sin(x+y)
x_s = 2*s*t
y_s = s**2 + t**2
F_s = F.subs([(x, x_s), (y, y_s)])
display(diff(F_s, s))
display(diff(F_s, t))

b. F=ln⁡(x2+y)F=\ln \left(x^{2}+y\right) where x=e(s+t2)x=\mathrm{e}^{(s+t 2)} and y=s2+ty=s^{2}+t

F = ln(x**2 + y)
x_st = exp(s+t**2)
y_st = s**2 + t
F_st = F.subs([(x, x_st), (y, y_st)])
display(diff(F_st, s))
display(diff(F_st, t))

c. F=x2y2F=x^{2} y^{2} where x=scos⁡tx=s \cos t and y=ssin⁡ty=s \sin t

F = x**2*y**2
x_st = s*cos(t)
y_st = s*sin(t)
F_st = F.subs([(x, x_st), (y, y_st)])
display(diff(F_st, s))
display(diff(F_st, t))

d. F=xy+yz2F=xy+y \mathrm{z}^{2} where x=et,y=etsin⁡tx=\mathrm{e}^{t}, y=\mathrm{e}^{t} \sin t and z=etcos⁡t\mathrm{z}=\mathrm{e}^{t} \cos t

F = x*y + y*z**2
x_t = E**t
y_t = E**t*sin(t)
z_t = E**t*cos(t)
F_t = F.subs([(x, x_t), (y, y_t), (z, z_t)])
display(diff(F_t, t))

Question 6​

Find dy/dxd y / d x and dy/dzd y / d z (if applicable) for each of the following

a. 7x2+2xy2+9y4=07 x^{2}+2 x y^{2}+9 y^{4}=0

f = 7*x**2 + 2*x*y**2 + 9*y**4
dydx = idiff(f, y, x)
display(dydx)

b. x3z2+y3+4xyz=0x^{3} z^{2}+y^{3}+4 x y z=0

f = x**3*z**2 + y**3 + 4*x*y*z
dydx = idiff(f, y, x)
dydz = idiff(f, y, z)
display(dydx)
display(dydz)

c. 3x2y3+xz2y2+y3zx4+y2z=03 x^{2} y^{3}+x z^{2} y^{2}+y^{3} z x^{4}+y^{2} z=0

f = 3*x**2*y**3 + x*z**2*y**2 + y**3*z*x**4 + y**2*z
dydx = idiff(f, y, x)
dydz = idiff(f, y, z)
display(dydx)
display(dydz)

d. y5+x2y3=1+yexp⁡(x2)y^{5}+x^{2} y^{3}=1+y \exp \left(x^{2}\right)

f = y**5 + x**2*y**3 - (1 + y*exp(x**2))
dydx = idiff(f, y, x)
display(dydx)

Question 7​

a. In polar coordinates, x=rcos⁡θ,y=rsin⁡θx=r \cos \theta, y=r \sin \theta, show that ∂(x,y)∂(r,θ)=r\frac{\partial(x, y)}{\partial(r, \theta)}=r

X = Matrix([r*cos(theta), r*sin(theta)])
Y = Matrix([r, theta])
J = X.jacobian(Y)
display(J)
display(J.det())

b. Obtain the Jacobian JJ of the transformation s=2x+y,t=x−2ys=2 x+y, t=x-2 y and determine the inverse of the transformation J1J_{1}. Confirm that J1=J−1J_{1}=J^{-1}.

S = Matrix([2*x+y, x-2*y])
X = Matrix([x, y])
J = S.jacobian(X)
display(J)
display(J.det())

# Inverse
sol = solve([s - (2*x+y), t - (x-2*y)], (x, y))
x_st = sol[x]
y_st = sol[y]
X_st = Matrix([x_st, y_st])
S_st = Matrix([s, t])
J1 = X_st.jacobian(S_st)
display(J1)
display(J1.det())
display(J.inv())

c. Show that if x+y=ux+y=\mathrm{u} and y=uvy=\mathrm{uv}, then ∂(x,y)∂(u,v)=u\frac{\partial(x, y)}{\partial(u, v)}=u.

# x = u - y => x = u - uv
x_uv = u - u*v
y_uv = u*v
X = Matrix([x_uv, y_uv])
Y = Matrix([u, v])
J = X.jacobian(Y)
display(J)
display(J.det())

d. Verify whether the functions u=x+y1−xyu=\frac{x+y}{1-x y} and v=tan⁡−1x+tan⁡−1yv=\tan ^{-1} x+\tan ^{-1} y are functionally dependent.

u_xy = (x+y)/(1-x*y)
v_xy = atan(x) + atan(y)
U = Matrix([u_xy, v_xy])
X = Matrix([x, y])
J = U.jacobian(X)
display(J)
display(J.det().simplify())

The Jacobian is 0, so the functions are functionally dependent.

e. If x=uv,y=u+vu−vx=uv, y=\frac{u+v}{u-v}, find ∂(u,v)∂(x,y)\frac{\partial(u, v)}{\partial(x, y)}.

jacobian = Matrix([[diff(u*v, u), diff(u*v, v)],
[diff((u+v)/(u-v), u), diff((u+v)/(u-v), v)]])
display(simplify(jacobian.det())) # ∂(x,y)/∂(u,v) = 4uv/(u-v)²
display(simplify(1 / jacobian.det())) # ∂(u,v)/∂(x,y) = (u-v)²/(4uv)

The question asks for ∂(u,v)∂(x,y)\frac{\partial(u,v)}{\partial(x,y)}, which is the reciprocal of the Jacobian we just computed:

∂(u,v)∂(x,y)=1∂(x,y)∂(u,v)=(u−v)24uv\frac{\partial(u, v)}{\partial(x, y)}=\frac{1}{\frac{\partial(x, y)}{\partial(u, v)}}=\frac{(u-v)^{2}}{4 u v}

note

Two sign/typo defects in the printed solution are avoided automatically by SymPy:

  • Q2d — the printed mixed partial fxy=exy(2x3y−xy2+4x−2y)f_{xy}=e^{xy}(2x^{3}y-xy^{2}+4x-2y) should be exy(2x2y−xy2+4x−2y)e^{xy}(2x^{2}y-xy^{2}+4x-2y). diff(f, x, y) returns the correct expression, and it agrees with diff(f, y, x) as Clairaut's theorem requires.
  • Q7e — ∂y∂v=+2u(u−v)2\frac{\partial y}{\partial v}=\frac{+2u}{(u-v)^{2}}, not −2u(u−v)2\frac{-2u}{(u-v)^{2}}. With the printed (wrong) sign the Jacobian determinant would be 00 instead of 4uv(u−v)2\frac{4uv}{(u-v)^{2}}.

Question 8​

Error and sensitivity analysis, using the increment (total differential) approximation ΔV≈Vr Δr+Vh Δh\Delta V\approx V_{r}\,\Delta r+V_{h}\,\Delta h.

a. How sensitive is V=πr2hV=\pi r^{2}h to small changes in rr and hh near (r0,h0)=(1,3)(r_{0},h_{0})=(1,3)?

# Q3 above already used `g` and `h` as function names, so rebind them to plain symbols
# here — otherwise `V = pi*r**2*h` would silently contain that earlier expression.
h, l, g = sy.symbols('h l g')

V = pi*r**2*h
V_r, V_h = diff(V, r), diff(V, h)
display(V_r, V_h) # 2*pi*h*r, pi*r**2

display(V_r.subs({r: 1, h: 3}), V_h.subs({r: 1, h: 3})) # 6*pi, pi

ΔV≈Vr(1,3) Δr+Vh(1,3) Δh=6π Δr+π Δh\Delta V\approx V_{r}(1,3)\,\Delta r+V_{h}(1,3)\,\Delta h=6\pi\,\Delta r+\pi\,\Delta h

A one-unit change in rr shifts VV by about 6π6\pi; a one-unit change in hh shifts it by about π\pi. Near (1,3)(1,3) the volume is therefore roughly 6 times as sensitive to rr as it is to hh.

Now reverse the pair, so that r=3r=3 and h=1h=1:

display(V_r.subs({r: 3, h: 1}), V_h.subs({r: 3, h: 1}))  # 6*pi, 9*pi

ΔV≈6π Δr+9π Δh\Delta V\approx 6\pi\,\Delta r+9\pi\,\Delta h

note

The rr-sensitivity is unchanged at 6π6\pi when the pair is swapped, because it depends only on the product rh=3rh=3; only πr2\pi r^{2} moves, and that is what makes the volume sensitive to hh instead. So the sensitivity to a change depends not only on the increment but on the relative size of rr and hh.

b. If rr is measured to ±1%\pm1\% and hh to ±0.5%\pm0.5\%, how accurately can VV be found?

Taking logarithms, log⁡V=log⁡π+2log⁡r+log⁡h\log V=\log\pi+2\log r+\log h, so ΔVV≈2Δrr+Δhh\dfrac{\Delta V}{V}\approx 2\dfrac{\Delta r}{r}+\dfrac{\Delta h}{h}.

display(Rational(2, 100) + Rational(1, 200))            # 1/40
display((Rational(2, 100) + Rational(1, 200))*100) # 5/2 -> 2.5 per cent

∣ΔVV∣≤2∣Δrr∣+∣Δhh∣=2100+1200=0.025\left|\frac{\Delta V}{V}\right|\le 2\left|\frac{\Delta r}{r}\right|+\left|\frac{\Delta h}{h}\right|=\frac{2}{100}+\frac{1}{200}=0.025

so the maximum percentage error in VV is about 2.5%2.5\%.

c. T=2πlgT=2\pi\sqrt{\dfrac{l}{g}}, with errors up to 1%1\% in ll and 2%2\% in gg.

T = 2*pi*sqrt(l/g)
display(simplify(diff(T, l)*l/T)) # 1/2
display(simplify(diff(T, g)*g/T)) # -1/2

The elasticities are +12+\frac{1}{2} for ll and −12-\frac{1}{2} for gg, so

display(Rational(1, 2)*Rational(1, 100) + Rational(1, 2)*Rational(2, 100))            # 3/200
display((Rational(1, 2)*Rational(1, 100) + Rational(1, 2)*Rational(2, 100))*100) # 3/2

∣dTT∣≤12(0.01)+12(0.02)=0.015\left|\frac{dT}{T}\right|\le\frac{1}{2}(0.01)+\frac{1}{2}(0.02)=0.015

The maximum percentage error in TT is 1.5%1.5\%.

d. R=v2sin⁡2αgR=\dfrac{v^{2}\sin 2\alpha}{g}, with a 1%1\% error in vv and a 0.5%0.5\% error in α\alpha.

R = v**2*sin(2*alpha)/g
display(simplify(diff(R, v)*v/R)) # 2
display(simplify(diff(R, alpha)*alpha/R)) # 2*alpha/tan(2*alpha)

Since log⁡R=2log⁡v+log⁡sin⁡2α−log⁡g\log R=2\log v+\log\sin 2\alpha-\log g,

δRR=2δvv+2cot⁡(2α) δα\frac{\delta R}{R}=2\frac{\delta v}{v}+2\cot(2\alpha)\,\delta\alpha

so the percentage error is 2(1)+2αcot⁡2α(0.5)=(2+αcot⁡2α)%2(1)+2\alpha\cot 2\alpha(0.5)=\left(2+\alpha\cot 2\alpha\right)\%.

Question 9​

Find the tangent plane and the normal line to each surface at the given point.

For a level surface f(x,y,z)=cf(x,y,z)=c, the gradient evaluated at the point is a normal vector, so the tangent plane is fx(x−x0)+fy(y−y0)+fz(z−z0)=0f_{x}(x-x_{0})+f_{y}(y-y_{0})+f_{z}(z-z_{0})=0 and the normal line has direction ratios (fx,fy,fz)\left(f_{x},f_{y},f_{z}\right).

def grad(f):
return Matrix([diff(f, x), diff(f, y), diff(f, z)])

def tangent_plane(f, pt):
"""Normal vector and tangent plane for the level surface f = 0 at pt."""
n = grad(f).subs(pt)
return n, sy.expand(n[0]*(x - pt[x]) + n[1]*(y - pt[y]) + n[2]*(z - pt[z]))

a. x2+2y2+3z2=6x^{2}+2y^{2}+3z^{2}=6 at (1,1,1)(1,1,1)

n, plane = tangent_plane(x**2 + 2*y**2 + 3*z**2 - 6, {x: 1, y: 1, z: 1})
display(n) # <2, 4, 6>
display(plane) # 2*x + 4*y + 6*z - 12
display(simplify(plane/2)) # x + 2*y + 3*z - 6

Tangent plane x+2y+3z=6x+2y+3z=6; normal line x−11=y−12=z−13\dfrac{x-1}{1}=\dfrac{y-1}{2}=\dfrac{z-1}{3}.

b. 2x2+y2−z2=−32x^{2}+y^{2}-z^{2}=-3 at (1,2,3)(1,2,3)

n, plane = tangent_plane(2*x**2 + y**2 - z**2 + 3, {x: 1, y: 2, z: 3})
display(n) # <4, 4, -6>
display(simplify(plane/2)) # 2*x + 2*y - 3*z + 3

Tangent plane 2x+2y−3z=−32x+2y-3z=-3; normal line x−12=y−22=z−3−3\dfrac{x-1}{2}=\dfrac{y-2}{2}=\dfrac{z-3}{-3}.

c. x2+y2−z=1x^{2}+y^{2}-z=1 at (1,2,4)(1,2,4)

n, plane = tangent_plane(x**2 + y**2 - z - 1, {x: 1, y: 2, z: 4})
display(n, plane) # <2, 4, -1>, 2*x + 4*y - z - 6

Tangent plane 2x+4y−z=62x+4y-z=6; normal line x−12=y−24=z−4−1\dfrac{x-1}{2}=\dfrac{y-2}{4}=\dfrac{z-4}{-1}.

d. ln⁡(xy)−z2(x−2y)−3z=3\ln\left(\dfrac{x}{y}\right)-z^{2}(x-2y)-3z=3 at (4,2,−1)(4,2,-1)

f = ln(x/y) - z**2*(x - 2*y) - 3*z - 3
display(simplify(f.subs({x: 4, y: 2, z: -1}))) # log(2) -> the point is NOT on f = 0

n, plane = tangent_plane(f, {x: 4, y: 2, z: -1})
display(n) # <-3/4, 3/2, -3>
display(plane) # -3*x/4 + 3*y/2 - 3*z - 3
caution

The given point does not satisfy the printed equation: ln⁡42−(−1)2(4−4)−3(−1)=ln⁡2+3≈3.693≠3\ln\frac{4}{2}-(-1)^{2}(4-4)-3(-1)=\ln 2+3\approx3.693\neq3. So no tangent plane exists at (4,2,−1)(4,2,-1) for that level surface. The gradient and plane above belong to the surface that actually passes through the point (right-hand side 3+ln⁡23+\ln 2) — which is what the printed solution intended to compute.

e. x3z+z3x−2yz=0x^{3}z+z^{3}x-2yz=0 at (1,1,1)(1,1,1)

n, plane = tangent_plane(x**3*z + z**3*x - 2*y*z, {x: 1, y: 1, z: 1})
display(n) # <4, -2, 2>
display(simplify(plane/2)) # 2*x - y + z - 2

Tangent plane 2x−y+z=22x-y+z=2; normal line x−12=y−1−1=z−11\dfrac{x-1}{2}=\dfrac{y-1}{-1}=\dfrac{z-1}{1}.

f. z=5+(x−1)2+(y+2)2z=5+(x-1)^{2}+(y+2)^{2} at (2,0,10)(2,0,10)

n, plane = tangent_plane((x - 1)**2 + (y + 2)**2 - z + 5, {x: 2, y: 0, z: 10})
display(n, plane) # <2, 4, -1>, 2*x + 4*y - z + 6

Tangent plane 2x+4y−z=−62x+4y-z=-6; normal line x−22=y4=z−10−1\dfrac{x-2}{2}=\dfrac{y}{4}=\dfrac{z-10}{-1}.

g. x212+y26+z24=1\dfrac{x^{2}}{12}+\dfrac{y^{2}}{6}+\dfrac{z^{2}}{4}=1 at (1,2,1)(1,2,1)

n, plane = tangent_plane(x**2/12 + y**2/6 + z**2/4 - 1, {x: 1, y: 2, z: 1})
display(n) # <1/6, 2/3, 1/2>
display(sy.expand(6*plane)) # x + 4*y + 3*z - 12

Tangent plane x+4y+3z=12x+4y+3z=12; normal line x−11=y−24=z−13\dfrac{x-1}{1}=\dfrac{y-2}{4}=\dfrac{z-1}{3}.

h. zex+ez+1+xy+y=3ze^{x}+e^{z+1}+xy+y=3 at (0,3,−1)(0,3,-1)

n, plane = tangent_plane(z*exp(x) + exp(z + 1) + x*y + y - 3, {x: 0, y: 3, z: -1})
display(n, plane) # <2, 1, 2>, 2*x + y + 2*z - 1

Tangent plane 2x+y+2z=12x+y+2z=1; normal line x2=y−31=z+12\dfrac{x}{2}=\dfrac{y-3}{1}=\dfrac{z+1}{2}.