Lesson 8 of 18

Directional Derivative

The Directional Derivative

The directional derivative measures the rate of change of ff in a specific direction u\mathbf{u}:

Duf(x,y)=fu^D_{\mathbf{u}} f(x, y) = \nabla f \cdot \hat{\mathbf{u}}

Where u^=(ux,uy)\hat{\mathbf{u}} = (u_x, u_y) is a unit vector (u^=1|\hat{\mathbf{u}}| = 1).

This is the dot product of the gradient with the direction vector.

Expanding the Formula

Duf=fxux+fyuyD_{\mathbf{u}} f = \frac{\partial f}{\partial x} u_x + \frac{\partial f}{\partial y} u_y

Why Unit Vectors?

Using a unit vector normalizes the measurement. If we doubled the direction vector, we'd get twice the rate — but we want a pure measure of slope in that direction, independent of how long the direction vector is.

Special Cases

  • u=(1,0)\mathbf{u} = (1, 0): directional derivative =f/x= \partial f / \partial x
  • u=(0,1)\mathbf{u} = (0, 1): directional derivative =f/y= \partial f / \partial y
  • u=f/f\mathbf{u} = \nabla f / |\nabla f|: maximum directional derivative =f= |\nabla f|
  • u=f/f\mathbf{u} = -\nabla f / |\nabla f|: minimum (most negative) directional derivative =f= -|\nabla f|

Example

For f(x,y)=x2+y2f(x,y) = x^2 + y^2 at (1,1)(1, 1), direction u=(12,12)\mathbf{u} = (\frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}}):

  • f=(2,2)\nabla f = (2, 2)
  • Duf=212+212=42=222.8284D_{\mathbf{u}} f = 2 \cdot \frac{1}{\sqrt{2}} + 2 \cdot \frac{1}{\sqrt{2}} = \frac{4}{\sqrt{2}} = 2\sqrt{2} \approx 2.8284

Your Task

Implement double directional_deriv(double (*f)(double, double), double x, double y, double ux, double uy, double h) that computes the directional derivative using central differences and the dot product formula.

Note: assume (ux,uy)(u_x, u_y) is already a unit vector.

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