- Published on
Engineering Mathematics 4: Vector Calculus - From Green's Theorem to Stokes' Theorem
- Authors

- Name
- Youngju Kim
- @fjvbn20031
Engineering Mathematics 4: Vector Calculus
Vector calculus is the mathematical backbone of virtually every engineering discipline — from classical physics and electromagnetism to fluid dynamics and modern machine learning. This guide builds systematically from vector functions and fields through the three great integral theorems: Green's theorem, the Divergence theorem, and Stokes' theorem.
1. Vector Functions and Vector Fields
Vector-Valued Functions
A vector-valued function maps a scalar parameter to a vector in space.
A classic example is the helix:
Tangent and Normal Vectors
The tangent vector of a curve is obtained by differentiation:
The unit tangent vector is , and the principal unit normal is .
Vector Fields
A vector field assigns a vector to every point in space:
Canonical examples:
- Gravitational field:
- Electric field:
- Fluid velocity field:
import numpy as np
import matplotlib.pyplot as plt
# Visualize a 2D rotational vector field
x = np.linspace(-2, 2, 15)
y = np.linspace(-2, 2, 15)
X, Y = np.meshgrid(x, y)
# F = (-y, x): pure rotation
U = -Y
V = X
fig, ax = plt.subplots(figsize=(6, 6))
ax.quiver(X, Y, U, V, color='steelblue', alpha=0.8)
ax.set_title('Rotational Vector Field F = (-y, x)')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.tight_layout()
plt.show()
2. Scalar Fields and the Gradient
Directional Derivative
For a scalar field , the directional derivative in the direction of unit vector is:
The Gradient Vector
The gradient captures both the direction of steepest ascent and the rate of increase:
Geometric and physical meaning:
- Direction of : the direction of maximum rate of increase of
- Magnitude : the maximum rate of increase
- is always perpendicular to the level surfaces
Example: For , the gradient is .
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
# Symbolic gradient computation
x_s, y_s = sp.symbols('x y')
f = x_s**2 + y_s**2 + x_s * y_s
grad_x = sp.diff(f, x_s)
grad_y = sp.diff(f, y_s)
print("grad_x =", grad_x) # 2x + y
print("grad_y =", grad_y) # x + 2y
# Numerical visualization
x = np.linspace(-2, 2, 20)
y = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x, y)
f_val = X**2 + Y**2
U = 2 * X # df/dx
V = 2 * Y # df/dy
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
c = axes[0].contourf(X, Y, f_val, 20, cmap='viridis')
axes[0].contour(X, Y, f_val, 10, colors='white', alpha=0.5)
plt.colorbar(c, ax=axes[0])
axes[0].set_title('f(x,y) = x² + y² — Level Curves')
axes[1].quiver(X, Y, U, V, color='red', alpha=0.7)
axes[1].set_title('Gradient ∇f = (2x, 2y)')
plt.tight_layout()
plt.show()
3. Divergence
Definition
The divergence of a vector field is a scalar:
Physical Interpretation
Divergence measures the net outward flux per unit volume at a point:
- : the point is a source — fluid flows outward
- : the point is a sink — fluid flows inward
- : incompressible flow — no net creation or destruction of fluid
Example: For :
The rotational field satisfies (incompressible).
import sympy as sp
x, y, z = sp.symbols('x y z')
P = x**2
Q = y**2
R = z**2
divergence = sp.diff(P, x) + sp.diff(Q, y) + sp.diff(R, z)
print("div F =", divergence) # 2*x + 2*y + 2*z
4. Curl
Definition
The curl of is a vector, computed via the symbolic determinant:
Expanding:
Physical Meaning and Conservative Fields
- The curl measures local rotation or vorticity of a fluid element.
- implies the field is irrotational (conservative).
- A conservative field admits a potential function such that .
Curl in Maxwell's Equations
Faraday's law of induction:
Ampere-Maxwell law:
import sympy as sp
x, y, z = sp.symbols('x y z')
# F = (y*z, x*z, x*y) — a conservative field
P = y * z
Q = x * z
R = x * y
curl_x = sp.diff(R, y) - sp.diff(Q, z)
curl_y = sp.diff(P, z) - sp.diff(R, x)
curl_z = sp.diff(Q, x) - sp.diff(P, y)
print("curl F =", (curl_x, curl_y, curl_z))
# Result: (0, 0, 0) => conservative (irrotational)
5. Line Integrals
Scalar Line Integral
For a curve parameterized by , :
Vector Line Integral (Work)
The work done by a force field moving a particle along :
Path Independence
For a conservative field , the line integral depends only on the endpoints:
import numpy as np
from scipy.integrate import quad
# F = (2x, 2y), path: r(t) = (t, t^2), t in [0,1]
def work_integrand(t):
x_t = t
y_t = t**2
Fx = 2 * x_t # 2x
Fy = 2 * y_t # 2y
dx = 1 # dx/dt
dy = 2 * t # dy/dt
return Fx * dx + Fy * dy
work, _ = quad(work_integrand, 0, 1)
print(f"Work = {work:.4f}") # = 2.0; phi = x^2 + y^2, phi(1,1)-phi(0,0) = 2
6. Surface Integrals
Scalar Surface Integral
For a parametric surface :
Flux Integral
The flux of through a surface measures the volume of fluid crossing per unit time:
7. Green's Theorem
Statement
Let be a positively oriented (counterclockwise), piecewise-smooth, simple closed curve bounding a region . If and have continuous first partial derivatives on an open set containing , then:
Area Calculation
Green's theorem yields an elegant formula for the area enclosed by a curve:
This is the basis of the shoelace formula used in computational geometry to find polygon areas from vertex coordinates.
Fluid Mechanics Application
In 2D fluid flow, Green's theorem relates the circulation around a closed path to the net rotation (vorticity) within the enclosed region.
import numpy as np
from scipy.integrate import dblquad
# Green's theorem verification
# P = -y, Q = x => dQ/dx - dP/dy = 1 + 1 = 2
# Double integral over unit disk: 2 * pi * 1^2 = 2*pi
result, _ = dblquad(
lambda y, x: 2.0,
-1, 1,
lambda x: -np.sqrt(max(0.0, 1 - x**2)),
lambda x: np.sqrt(max(0.0, 1 - x**2))
)
print(f"Double integral = {result:.5f}") # ≈ 6.28318 = 2*pi
print(f"2*pi = {2*np.pi:.5f}")
8. Gauss's Divergence Theorem
Statement
Let be a solid region with boundary surface (outward orientation), and let have continuous first partial derivatives throughout . Then:
Physical Interpretation
The total outward flux through the closed surface equals the total amount of source/sink activity inside. This transforms a difficult surface integral into a potentially easier volume integral (or vice versa).
Gauss's Law for Electric Fields
This is a direct application of the divergence theorem combined with . It allows us to compute electric fields for highly symmetric charge distributions (sphere, cylinder, infinite plane).
import sympy as sp
x, y, z = sp.symbols('x y z')
# F = (x^3, y^3, z^3), integrate over unit sphere
P, Q, R = x**3, y**3, z**3
div_F = sp.diff(P, x) + sp.diff(Q, y) + sp.diff(R, z)
print("div F =", div_F) # 3*x**2 + 3*y**2 + 3*z**2
# In spherical coords: integral of 3*r^2 over unit sphere = 3*(4*pi/5)
# Total flux = 12*pi/5
print("Expected total flux =", sp.Rational(12, 1) * sp.pi / 5)
9. Stokes' Theorem
Statement
Let be an oriented piecewise-smooth surface bounded by a simple, closed, piecewise-smooth curve (oriented consistently with ). Then:
- Left side: line integral of around (circulation)
- Right side: flux of through
Green's Theorem as a Special Case
When is a flat region in the -plane with , Stokes' theorem reduces to Green's theorem:
Ampere's Law
Stokes' theorem converts this integral form to the differential form , one of Maxwell's equations.
import numpy as np
# Numerical verification of Stokes' theorem
# F = (-y, x, 0), S: upper hemisphere of unit sphere, C: unit circle
# curl F = (0, 0, 2)
# Surface integral: 2 * projected area of S onto xy-plane = 2 * pi
# Line integral: circulation around unit circle = 2*pi
R = 1.0
t = np.linspace(0, 2 * np.pi, 10000)
x_c = R * np.cos(t)
y_c = R * np.sin(t)
dx = np.gradient(x_c, t)
dy = np.gradient(y_c, t)
F_dot_dr = (-y_c) * dx + x_c * dy
line_integral = np.trapz(F_dot_dr, t)
print(f"Line integral ≈ {line_integral:.5f}")
print(f"2*pi = {2*np.pi:.5f}")
10. Engineering and AI Applications
Computational Fluid Dynamics (CFD)
The incompressible Navier-Stokes equations — the cornerstone of CFD — rely heavily on divergence and curl:
Incompressibility constraint:
Maxwell's Equations
| Law | Differential Form | Physical Meaning |
|---|---|---|
| Gauss's Law (E) | Charges are sources of the electric field | |
| Gauss's Law (B) | No magnetic monopoles exist | |
| Faraday's Law | Changing magnetic flux induces electric field | |
| Ampere-Maxwell | Current and changing electric flux create magnetic field |
Computer Graphics
In 3D rendering, surface normal vectors for lighting calculations (Phong shading, bump mapping) are computed exactly as from surface parameterization — a direct application of vector calculus.
Gradient Descent in Machine Learning
The gradient descent optimizer updates parameters in the direction opposite to the gradient of the loss:
This is gradient vector calculus applied directly to a high-dimensional parameter space. Variants such as Adam and RMSProp adaptively scale the gradient using its history.
Physics-Informed Neural Networks (PINNs)
PINNs embed physical laws (PDEs involving divergence, curl, gradient) directly into the loss function, using automatic differentiation to evaluate differential operators:
where might be enforcing incompressibility.
11. Comprehensive Python Implementation
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import dblquad, quad
import sympy as sp
print("=== Vector Calculus Python Examples ===\n")
# --- 1. Gradient ---
x_s, y_s, z_s = sp.symbols('x y z')
f = x_s**3 + y_s**2 * z_s - x_s * y_s
grad = [sp.diff(f, v) for v in (x_s, y_s, z_s)]
print("1. Gradient")
print(f" f = {f}")
print(f" grad f = {grad}\n")
# --- 2. Divergence ---
P = x_s**2 * y_s
Q = y_s**2 * z_s
R = z_s**2 * x_s
div_F = sp.diff(P, x_s) + sp.diff(Q, y_s) + sp.diff(R, z_s)
print("2. Divergence")
print(f" F = ({P}, {Q}, {R})")
print(f" div F = {div_F}\n")
# --- 3. Curl ---
curl_x = sp.diff(R, y_s) - sp.diff(Q, z_s)
curl_y = sp.diff(P, z_s) - sp.diff(R, x_s)
curl_z = sp.diff(Q, x_s) - sp.diff(P, y_s)
print("3. Curl")
print(f" curl F = ({sp.simplify(curl_x)}, {sp.simplify(curl_y)}, {sp.simplify(curl_z)})\n")
# --- 4. Green's theorem (numerical) ---
result, _ = dblquad(
lambda y, x: 2.0,
-1, 1,
lambda x: -np.sqrt(max(0, 1 - x**2)),
lambda x: np.sqrt(max(0, 1 - x**2))
)
print("4. Green's Theorem verification")
print(f" Double integral ≈ {result:.5f}, 2*pi ≈ {2*np.pi:.5f}\n")
# --- 5. Vector field visualization ---
x_arr = np.linspace(-2, 2, 20)
y_arr = np.linspace(-2, 2, 20)
X, Y = np.meshgrid(x_arr, y_arr)
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].quiver(X, Y, 2*X, 2*Y, color='crimson', alpha=0.7)
axes[0].set_title('Gradient of x²+y²')
axes[1].quiver(X, Y, -Y, X, color='steelblue', alpha=0.7)
axes[1].set_title('Rotational Field (-y, x)')
axes[2].quiver(X, Y, X, Y, color='forestgreen', alpha=0.7)
axes[2].set_title('Source Field (x, y)')
plt.tight_layout()
plt.savefig('vector_fields_en.png', dpi=120)
plt.show()
12. Quiz
Q1. What geometric relationship does the gradient vector ∇f have with level surfaces of f?
Answer: The gradient ∇f is always perpendicular (normal) to the level surface f = c at every point.
Explanation: The gradient points in the direction of steepest ascent of f. For any tangent vector t lying in the level surface, the directional derivative D_t f = ∇f · t = 0 (since f is constant along the surface), proving that ∇f is orthogonal to all tangent directions — hence normal to the surface. This is exploited in computer graphics to compute surface normals and in optimization to identify descent directions.
Q2. A vector field F satisfies div F = 0 everywhere. What does this imply physically and what is F called?
Answer: F is called an incompressible (or solenoidal) vector field. Physically it means no fluid is created or destroyed — sources and sinks are absent.
Explanation: The condition ∇·F = 0 means the net outflow per unit volume at every point is zero, so volume is conserved by the flow. Liquids (at low pressure) and magnetic fields (∇·B = 0, Gauss's law for magnetism, encoding the absence of magnetic monopoles) are canonical examples. For flow simulation, maintaining this constraint is a central challenge in CFD pressure-velocity coupling schemes such as SIMPLE.
Q3. How does Green's theorem allow computation of a region's area from a boundary integral?
Answer: Choose P = -y/2 and Q = x/2, so that ∂Q/∂x - ∂P/∂y = 1. Then ∮C P dx + Q dy = ∬D 1 dA = Area(D), giving A = (1/2) ∮C (x dy - y dx).
Explanation: The key insight is choosing P and Q so the integrand of the double integral equals 1. This transforms the area problem into a boundary traversal, requiring only the curve coordinates — not integration over the interior. Computational applications include the shoelace formula for polygon areas (the discrete version) and planimeter instruments for measuring map areas mechanically.
Q4. Stokes' theorem states ∮C F·dr = ∬S (∇×F)·n dS. If curl F = 0, what must the line integral equal?
Answer: The line integral is 0 for any closed path C bounding a surface over which curl F = 0.
Explanation: With ∇×F = 0 the right-hand side is ∬S 0·n dS = 0. This is equivalent to F being a conservative (irrotational) field, guaranteeing path independence: ∫C F·dr depends only on endpoints, not the path. The potential function φ satisfying F = ∇φ is then found by integrating the components. In electrostatics, E = -∇V (V is voltage) follows from ∇×E = 0 in static fields.
Q5. Explain how the Divergence Theorem connects to Gauss's Law in electrostatics.
Answer: Combining the integral form of Gauss's law ∯S E·dA = Q_enc/ε₀ with the Divergence Theorem ∯S E·dA = ∭V (∇·E) dV yields the differential form ∇·E = ρ/ε₀.
Explanation: The Divergence Theorem converts a surface flux integral to a volume integral over the enclosed charge density ρ. For a sphere of radius r enclosing total charge Q, choosing S as the sphere and using the spherical symmetry of E makes the surface integral trivial: 4πr²E = Q/ε₀, giving E = Q/(4πε₀r²) — Coulomb's law. The theorem thus bridges the local differential law (∇·E = ρ/ε₀) and the global integral law (Gauss's law), and underpins finite-element and finite-volume methods in computational electromagnetics.
References
- Kreyszig, E. — Advanced Engineering Mathematics (10th ed.), Wiley. The standard reference for engineering mathematics.
- MIT OCW 18.02 — Multivariable Calculus (Denis Auroux). ocw.mit.edu
- Stewart, J. — Calculus: Early Transcendentals (9th ed.), Cengage. Chapters on line integrals, surface integrals, and vector theorems.
- Griffiths, D.J. — Introduction to Electrodynamics (4th ed.), Cambridge University Press. Physical applications of all three vector theorems.
- SciPy Documentation —
scipy.integratefor numerical multi-dimensional integration. scipy.org - SymPy Documentation — Symbolic differentiation and integration library. sympy.org
- Raissi, M. et al. — "Physics-informed neural networks," Journal of Computational Physics, 2019. PINNs and vector calculus in deep learning.