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 # 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
sy.init_printing(use_latex=True) # Show it in natural display

Question 1

Find the partial derivative (fy)\displaystyle\left(\frac{\partial f}{\partial y}\right) and (fx)\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)=x24xy+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+3xyy2f(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)=x4sin3yf(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(y2x)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(2xy)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)=xsinyycosxh(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)=9y3xyU(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 Fs\frac{\partial F}{\partial s} and Ft\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=scostx=s \cos t and y=ssinty=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=etsintx=\mathrm{e}^{t}, y=\mathrm{e}^{t} \sin t and z=etcost\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=x2ys=2 x+y, t=x-2 y and determine the inverse of the transformation J1J_{1}. Confirm that J1=J1J_{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+y1xyu=\frac{x+y}{1-x y} and v=tan1x+tan1yv=\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+vuvx=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)]])
simplify(jacobian.det()) # ∂(x,y)/∂(u,v)