Lesson 10 of 18

Laplacian

The Laplacian

The Laplacian of f(x,y)f(x, y) is the sum of its second-order partial derivatives:

2f=2fx2+2fy2\nabla^2 f = \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2}

It measures how much ff differs from its average in the neighborhood of a point.

Computing Second Derivatives Numerically

Using central differences twice:

2fx2f(x+h,y)2f(x,y)+f(xh,y)h2\frac{\partial^2 f}{\partial x^2} \approx \frac{f(x+h,y) - 2f(x,y) + f(x-h,y)}{h^2}

2fy2f(x,y+h)2f(x,y)+f(x,yh)h2\frac{\partial^2 f}{\partial y^2} \approx \frac{f(x,y+h) - 2f(x,y) + f(x,y-h)}{h^2}

Applications

The Laplacian appears throughout physics and engineering:

  • Heat equation: T/t=α2T\partial T / \partial t = \alpha \nabla^2 T (how temperature diffuses)
  • Wave equation: 2u/t2=c22u\partial^2 u / \partial t^2 = c^2 \nabla^2 u
  • Electrostatics: 2φ=0\nabla^2 \varphi = 0 in free space (Laplace's equation)
  • Image processing: edge detection (Laplacian filter)

Interpretation

  • 2f>0\nabla^2 f > 0 at a point: ff is below its local average (like a bowl)
  • 2f<0\nabla^2 f < 0 at a point: ff is above its local average (like a hill)
  • 2f=0\nabla^2 f = 0: harmonic function — a steady-state distribution

Example

For f(x,y)=x2+y2f(x,y) = x^2 + y^2:

  • 2f/x2=2\partial^2 f / \partial x^2 = 2
  • 2f/y2=2\partial^2 f / \partial y^2 = 2
  • 2f=4\nabla^2 f = 4 everywhere

For f(x,y)=x2y2f(x,y) = x^2 - y^2:

  • 2f=2+(2)=0\nabla^2 f = 2 + (-2) = 0 (harmonic!)

Your Task

Implement double laplacian_2d(double (*f)(double, double), double x, double y, double h) using the central difference formulas for second derivatives.

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