Tutorial 2
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 and of these functions using the limit definition
a.
f = x**2*y + 2*x + y**3
display(diff(f, x))
display(diff(f, y))
b.
f = x**2 - 4*x*y + y**2
display(diff(f, x))
display(diff(f, y))
c.
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**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**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**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 = 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 = y*exp(x+y)
display(diff(g, x))
display(diff(g, y))
b.
h = x*sin(y) - y*cos(x)
display(diff(h, x))
display(diff(h, y))
c.
p = x**y + y**2
display(diff(p, x))
display(diff(p, y))
d.
U = (9*y**3)/(x-y)
display(diff(U, x))
display(diff(U, y))
Question 4
For , use the implicit function theorem to find and
a.
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**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 = 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 and , if applicable, for the following composite functions
a. where and
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. where and
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. where and
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. where and
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 and (if applicable) for each of the following
a.
f = 7*x**2 + 2*x*y**2 + 9*y**4
dydx = idiff(f, y, x)
display(dydx)
b.
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.
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.
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, , show that
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 of the transformation and determine the inverse of the transformation . Confirm that .
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 and , then .
# 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 and 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 , find .
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)