Skip to main content

Tutorial 12

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:

  • Line integrals ∫CA⋅dr\int_{C}\mathbf{A}\cdot d\mathbf{r} along parametric curves
  • Path dependence: the same field gives different answers along different paths
  • Green's theorem: ∮CM dx+N dy=∬R(Nx−My)dA\oint_{C}M\,dx+N\,dy=\iint_{R}\left(N_{x}-M_{y}\right)dA
  • Work done on a particle in a force field

Introduction​

The line integral of a field F=(F1,F2,F3)\mathbf{F}=(F_{1},F_{2},F_{3}) along a curve r(t)\mathbf{r}(t) is

∫CF⋅dr=∫t0t1(F1dxdt+F2dydt+F3dzdt)dt\int_{C}\mathbf{F}\cdot d\mathbf{r}=\int_{t_{0}}^{t_{1}} \left(F_{1}\frac{dx}{dt}+F_{2}\frac{dy}{dt}+F_{3}\frac{dz}{dt}\right)dt

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​

A=(3x2+6y)i^−14yz j^+20xz2 k^\mathbf{A}=\left(3x^{2}+6y\right)\hat{i}-14yz\,\hat{j}+20xz^{2}\,\hat{k} from (0,0,0)(0,0,0) to (1,1,1)(1,1,1).

a. Along x=tx=t, y=t2y=t^{2}, z=t3z=t^{3}.

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 (0,0,0)→(1,0,0)→(1,1,0)→(1,1,1)(0,0,0)\to(1,0,0)\to(1,1,0)\to(1,1,1).

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 (0,0,0)(0,0,0) and (1,1,1)(1,1,1).

display(line_integral(A, t, t, t, t, 0, 1))              # 13/3

The three answers 55, 233\frac{23}{3} and 133\frac{13}{3} all differ, which shows the field is path dependent (not conservative).

Question 2​

Work done by F=3xy i^−5z j^+10x k^\mathbf{F}=3xy\,\hat{i}-5z\,\hat{j}+10x\,\hat{k} along x=t2+1x=t^{2}+1, y=2t2y=2t^{2}, z=t3z=t^{3} from t=1t=1 to t=2t=2.

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 33 centred at the origin for F=(2x−y+z)i^+(x+y−z2)j^+(3x−2y+4z)k^\mathbf{F}=(2x-y+z)\hat{i}+\left(x+y-z^{2}\right)\hat{j}+(3x-2y+4z)\hat{k}.

On the circle z=0z=0, and the counterclockwise parametrisation is x=3cos⁡tx=3\cos t, y=3sin⁡ty=3\sin t, 0≤t≤2π0\le t\le2\pi.

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 18π18\pi; traversing the circle clockwise would give −18π-18\pi.

Question 4​

∮C(xy+y2)dx+x2 dy\displaystyle\oint_{C}\left(xy+y^{2}\right)dx+x^{2}\,dy around the region bounded by y=xy=x and y=x2y=x^{2}, 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 ∬R(x−2y) dA=−120\iint_{R}(x-2y)\,dA=-\dfrac{1}{20}, which matches the direct computation in the tutorial (1920−1\frac{19}{20}-1).

Question 5​

∮C(y−sin⁡x) dx+cos⁡x dy\displaystyle\oint_{C}(y-\sin x)\,dx+\cos x\,dy around the triangle with vertices (0,0)(0,0), (π2,0)\left(\frac{\pi}{2},0\right) and (π2,1)\left(\frac{\pi}{2},1\right).

The hypotenuse joins (0,0)(0,0) to (π2,1)\left(\frac{\pi}{2},1\right), so y=2xπy=\frac{2x}{\pi}.

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​

∮C−x2y dx+xy2 dy\displaystyle\oint_{C}-x^{2}y\,dx+xy^{2}\,dy around the circle of radius 22 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 x2+y2=r2x^{2}+y^{2}=r^{2} is exactly the reason polar coordinates are the natural choice here.

Question 7​

Line integral of F(x,y)=⟨x3,4x⟩\mathbf{F}(x,y)=\left\langle x^{3},4x\right\rangle along the closed path CC.

Green's theorem gives ∬D(Qx−Py)dA=∬D4 dA=4×(area of D)\iint_{D}\left(Q_{x}-P_{y}\right)dA=\iint_{D}4\,dA=4\times(\text{area of }D). The region enclosed by CC plus the closing segment LL has an area of 44 unit squares, and ∫LF⋅dr=−4\int_{L}\mathbf{F}\cdot d\mathbf{r}=-4.

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 ∫CF⋅dr=16−(−4)=20\displaystyle\int_{C}\mathbf{F}\cdot d\mathbf{r}=16-(-4)=20.

Question 8​

A particle starts at (−2,0)(-2,0), moves along the xx-axis to (2,0)(2,0), then back along the upper half of x2+y2=4x^{2}+y^{2}=4. Find the work done by F(x,y)=⟨x, x3+3xy2⟩\mathbf{F}(x,y)=\left\langle x,\ x^{3}+3xy^{2}\right\rangle.

The path is closed and traversed counterclockwise, so Green's theorem applies with P=xP=x and Q=x3+3xy2Q=x^{3}+3xy^{2}.

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
W=∬3(x2+y2)dA=∫0π∫023r3 dr dθ=12π≈37.70W=\iint 3\left(x^{2}+y^{2}\right)dA =\int_{0}^{\pi}\int_{0}^{2}3r^{3}\,dr\,d\theta=12\pi\approx37.70
note

The downloadable solution PDF still works from P=sin⁡(x3)P=\sin(x^{3}) and Q=2yex2Q=2y e^{x^{2}}, which is not the field in the question and leads to a nonsensical e4−5e^{4}-5. The field is F=⟨x,  x3+3xy2⟩\mathbf F=\langle x,\;x^{3}+3xy^{2}\rangle, so Qx=3x2+3y2Q_x=3x^{2}+3y^{2} and Py=0P_y=0, giving 12π≈37.7012\pi\approx37.70.

Question 9​

F(x,y)=⟨sin⁡x, cos⁡y⟩\mathbf{F}(x,y)=\langle\sin x,\ \cos y\rangle along the top half of x2+y2=1x^{2}+y^{2}=1 traversed counterclockwise from (1,0)(1,0) to (−1,0)(-1,0), followed by the line segment from (−1,0)(-1,0) to (−2,3)(-2,3).

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)

∫Csin⁡x dx+cos⁡y dy=cos⁡1−cos⁡2+sin⁡3\int_{C}\sin x\,dx+\cos y\,dy=\cos 1-\cos 2+\sin 3


Extra Learning Resources​

Key Concepts to Master​

  1. Parametrise, then substitute: replace xx, yy, zz and dxdx, dydy, dzdz in terms of tt
  2. Path dependence: if the answers differ for different paths, the field is not conservative
  3. Green's theorem: ∮M dx+N dy=∬(Nx−My)dA\oint M\,dx+N\,dy=\iint\left(N_{x}-M_{y}\right)dA, for a positively oriented simple closed curve
  4. Positive orientation: counterclockwise for the outer boundary
  5. Closing a path: to use Green's theorem on an open curve, add a straight segment and subtract its integral

SymPy Resources​

Common Pitfalls​

  • A line integral is a scalar, not a vector — check you have a dot product, not a cross product
  • In a subs dictionary, substitute xx and yy and zz 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