Lesson 15 of 18

Divergence

Divergence of a 2D Vector Field

A vector field F(x,y)=(Fx(x,y),  Fy(x,y))\mathbf{F}(x,y) = (F_x(x,y),\; F_y(x,y)) assigns a vector to every point in the plane.

The divergence measures how much the field "spreads out" at a point:

F=Fxx+Fyy\nabla \cdot \mathbf{F} = \frac{\partial F_x}{\partial x} + \frac{\partial F_y}{\partial y}

Interpretation

  • F>0\nabla \cdot \mathbf{F} > 0: the field is a source — fluid flows outward
  • F<0\nabla \cdot \mathbf{F} < 0: the field is a sink — fluid flows inward
  • F=0\nabla \cdot \mathbf{F} = 0: incompressible flow — no net gain or loss

The Divergence Theorem

In 2D, the divergence theorem relates the integral of divergence over a region to flow across the boundary:

RFdA=RFnds\iint_R \nabla \cdot \mathbf{F}\, dA = \oint_{\partial R} \mathbf{F} \cdot \mathbf{n}\, ds

This is the 2D version of Gauss's law.

Examples

F=(x,y)\mathbf{F} = (x, y) (radially outward):

  • Fx/x=1\partial F_x / \partial x = 1, Fy/y=1\partial F_y / \partial y = 1
  • F=2\nabla \cdot \mathbf{F} = 2 (constant — uniform spreading)

F=(y,x)\mathbf{F} = (-y, x) (rotation):

  • Fx/x=0\partial F_x / \partial x = 0, Fy/y=0\partial F_y / \partial y = 0
  • F=0\nabla \cdot \mathbf{F} = 0 (incompressible rotation — no sources or sinks)

F=(x2,y2)\mathbf{F} = (x^2, y^2):

  • Fx/x=2x\partial F_x / \partial x = 2x, Fy/y=2y\partial F_y / \partial y = 2y
  • F=2(x+y)\nabla \cdot \mathbf{F} = 2(x+y)

Your Task

Implement double divergence_2d(double (*Fx)(double, double), double (*Fy)(double, double), double x, double y, double h) that computes the divergence using central differences.

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