4.C] Write code to compute \nabla \phi for \phi = x^2 y z
Answer:
from sympy.physics.vector import ReferenceFrame, gradient
from sympy import var, display
# Declare variables
x, y, z = var('x y z')
# Define reference frame
v = ReferenceFrame('v')
# Define scalar field F
F = v[0]**2 * v[1] * v[2]
# Compute the gradient of F
G = gradient(F, v)
# Substitute v[0], v[1], v[2] with x, y, z in F
F = F.subs([(v[0], x), (v[1], y), (v[2], z)])
print('Given scalar function F =')
display(F)
# Substitute v[0], v[1], v[2] with x, y, z in the gradient result
G = G.subs([(v[0], x), (v[1], y), (v[2], z)])
print('Gradient of F =')
display(G)
