Tutorial 12
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:
- Line integrals along parametric curves
- Path dependence: the same field gives different answers along different paths
- Green's theorem:
- Work done on a particle in a force field
Introduction
The line integral of a field along a curve is
so we can build a reusable helper that takes the field and the parametrisation:
import sympy as sy
from sympy.abc import x, y, z, t, r, theta
from sympy import (sqrt, sin, cos, tan, pi, ln, exp, atan, integrate, diff, simplify,
Rational, N, Matrix, Symbol)
sy.init_printing(use_latex=True)
def line_integral(F, rx, ry, rz, t, t0, t1):
"""Integral of F . dr along (x,y,z) = (rx, ry, rz) for t in [t0, t1]."""
Fx = F[0].subs({x: rx, y: ry, z: rz})
Fy = F[1].subs({x: rx, y: ry, z: rz})
Fz = F[2].subs({x: rx, y: ry, z: rz})
return integrate(Fx*diff(rx, t) + Fy*diff(ry, t) + Fz*diff(rz, t), (t, t0, t1))
Question 1
from to .
a. Along , , .
A = Matrix([3*x**2 + 6*y, -14*y*z, 20*x*z**2])
display(line_integral(A, t, t**2, t**3, t, 0, 1)) # 5
b. Along the straight segments .
seg1 = line_integral(A, t, 0, 0, t, 0, 1) # (0,0,0) -> (1,0,0)
seg2 = line_integral(A, 1, t, 0, t, 0, 1) # (1,0,0) -> (1,1,0)
seg3 = line_integral(A, 1, 1, t, t, 0, 1) # (1,1,0) -> (1,1,1)
display(seg1, seg2, seg3, seg1 + seg2 + seg3) # 1, 0, 20/3, 23/3
c. Along the straight line joining and .
display(line_integral(A, t, t, t, t, 0, 1)) # 13/3
The three answers , and all differ, which shows the field is path dependent (not conservative).
Question 2
Work done by along , , from to .
F = Matrix([3*x*y, -5*z, 10*x])
W = line_integral(F, t**2 + 1, 2*t**2, t**3, t, 1, 2)
display(W) # 303
Question 3
Work done counterclockwise once around the circle of radius centred at the origin for .
On the circle , and the counterclockwise parametrisation is , , .
F = Matrix([2*x - y + z, x + y - z**2, 3*x - 2*y + 4*z])
display(line_integral(F, 3*cos(t), 3*sin(t), 0, t, 0, 2*pi)) # 18*pi
The positive (counterclockwise) direction gives ; traversing the circle clockwise would give .
Question 4
around the region bounded by and , traversed positively.
M, N = x*y + y**2, x**2
display(simplify(diff(N, x) - diff(M, y))) # x - 2y
I = integrate(x - 2*y, (y, x**2, x), (x, 0, 1))
display(I) # -1/20
By Green's theorem the line integral equals , which matches the direct computation in the tutorial ().
Question 5
around the triangle with vertices , and .
The hypotenuse joins to , so .
M, N = y - sin(x), cos(x)
display(simplify(diff(N, x) - diff(M, y))) # -sin(x) - 1
I = integrate(-sin(x) - 1, (y, 0, 2*x/pi), (x, 0, pi/2))
display(simplify(I)) # -pi/4 - 2/pi
Question 6
around the circle of radius centred on the origin.
M, N = -x**2*y, x*y**2
display(simplify(diff(N, x) - diff(M, y))) # x**2 + y**2
I = integrate((r**2)*r, (r, 0, 2), (theta, 0, 2*pi))
display(I) # 8*pi
The integrand is exactly the reason polar coordinates are the natural choice here.
Question 7
Line integral of along the closed path .
Green's theorem gives . The region enclosed by plus the closing segment has an area of unit squares, and .
P, Q = x**3, 4*x
display(simplify(diff(Q, x) - diff(P, y))) # 4
area = 4
integral_D = 4*area
integral_L = -4
display(integral_D, integral_D - integral_L) # 16, 20
So .
Question 8
A particle starts at , moves along the -axis to , then back along the upper half of . Find the work done by .
The path is closed and traversed counterclockwise, so Green's theorem applies with and .
P, Q = x, x**3 + 3*x*y**2
display(simplify(diff(Q, x) - diff(P, y))) # 3*x**2 + 3*y**2
# over the upper half-disc of radius 2, using x^2 + y^2 = r^2
W = integrate(3*r**2 * r, (r, 0, 2), (theta, 0, pi))
display(W, sy.N(W, 6)) # 12*pi = 37.6991
The downloadable solution PDF still works from and , which is not the field in the question and leads to a nonsensical . The field is , so and , giving .
Question 9
along the top half of traversed counterclockwise from to , followed by the line segment from to .
F = Matrix([sin(x), cos(y), 0])
# C1: the upper semicircle, x = cos t, y = sin t, 0 <= t <= pi
C1 = line_integral(F, cos(t), sin(t), 0, t, 0, pi)
display(simplify(C1)) # 0
# C2: the segment, x = -1 - t, y = 3t, 0 <= t <= 1
C2 = line_integral(F, -1 - t, 3*t, 0, t, 0, 1)
display(simplify(C2)) # -cos(2) + cos(1) + sin(3)
display(simplify(C1 + C2)) # cos(1) - cos(2) + sin(3)
Extra Learning Resources
Key Concepts to Master
- Parametrise, then substitute: replace , , and , , in terms of
- Path dependence: if the answers differ for different paths, the field is not conservative
- Green's theorem: , for a positively oriented simple closed curve
- Positive orientation: counterclockwise for the outer boundary
- Closing a path: to use Green's theorem on an open curve, add a straight segment and subtract its integral
SymPy Resources
- SymPy
integrate— definite integrals in - SymPy
subs— substituting a parametrisation into the field - SymPy
diff— partial derivatives for the Green's theorem integrand
Common Pitfalls
- A line integral is a scalar, not a vector — check you have a dot product, not a cross product
- In a
subsdictionary, substitute and and in one call, otherwise SymPy substitutes sequentially and you get the wrong field - Watch the orientation: reversing the direction of travel flips the sign of the integral
- For Green's theorem, the curve must be closed and positively oriented — add a closing segment if it is not