Lesson 17 of 18

Line Integral of a Scalar Field

Line Integrals

A line integral (or path integral) integrates a scalar field f(x,y)f(x,y) along a curve CC:

Cf(x,y)ds\int_C f(x,y)\, ds

where dsds is the arc-length element — each tiny piece of the curve weighted by the function value at that point.

Parameterization

If the curve is given by r(t)=(x(t),y(t))\mathbf{r}(t) = (x(t), y(t)) for t[a,b]t \in [a, b], then:

ds=(dxdt)2+(dydt)2dtds = \sqrt{\left(\frac{dx}{dt}\right)^2 + \left(\frac{dy}{dt}\right)^2}\, dt

So:

Cfds=abf(x(t),y(t))x(t)2+y(t)2dt\int_C f\, ds = \int_a^b f(x(t), y(t)) \cdot \sqrt{x'(t)^2 + y'(t)^2}\, dt

Examples

Arc length (set f=1f = 1): C1ds=length of C\int_C 1\, ds = \text{length of } C

For a unit circle (x=costx = \cos t, y=sinty = \sin t, t[0,2π]t \in [0, 2\pi]): C1ds=02π11dt=2π6.2832\int_C 1\, ds = \int_0^{2\pi} 1 \cdot 1\, dt = 2\pi \approx 6.2832

Integral of f=xf = x along xx-axis from 0 to 2 (x=t,y=0,ds=dtx=t, y=0, ds=dt): 02tdt=2.0000\int_0^2 t\, dt = 2.0000

Numerical Computation

double line_integral(double (*f)(double, double),
                     double (*xt)(double), double (*yt)(double),
                     double (*dxt)(double), double (*dyt)(double),
                     double a, double b, int n) {
    double h = (b - a) / n, sum = 0.0;
    for (int i = 0; i < n; i++) {
        double t = a + i * h;
        double dx = dxt(t), dy = dyt(t);
        sum += f(xt(t), yt(t)) * sqrt(dx*dx + dy*dy) * h;
    }
    return sum;
}

Your Task

Implement double line_integral(f, xt, yt, dxt, dyt, a, b, n) using a left Riemann sum over the parameterization.

TCC compiler loading...
Loading...
Click "Run" to execute your code.