logo

Complex Step Derivative Approximation 📂Numerical Analysis

Complex Step Derivative Approximation

Formula

The derivative f(x)f ' (x) of the function f:RRf : \mathbb{R} \to \mathbb{R} can be approximated using complex numbers as follows. f(x)Im(f(x+ih))h f ' (x) \approx \frac{\im \left( f \left( x + i h \right) \right)}{h} Here, Im\im represents the imaginary part of the complex number, and hh is a sufficiently small real number.

Explanation

Complex analysis continues to be fascinating the more you learn about it. Unlike the finite difference method, which approximates derivatives through multiple function values, this method approximates the derivative of ff by formally treating it like a complex function, even if it wasn’t a complex function.

Derivation 1

There are two methods, both remarkably intriguing. Let’s represent the real number x,yRx, y \in \mathbb{R} as the complex number z=x+iyz = x + i y, and the real function f:RRf : \mathbb{R} \to \mathbb{R} as f(z)=u(z)+iv(z)f(z) = u(z) + i v(z) in terms of u,v:CRu, v : \mathbb{C} \to \mathbb{R}.

Cauchy-Riemann Equations

Cauchy-Riemann Equations: Suppose the function f:ACCf: A \subseteq \mathbb{C} \to \mathbb{C} is analytic at R\mathscr{R}. If the real function u,vu,v satisfies f(z)=f(x+iy)=u(x,y)+iv(x,y) f(z) = f(x+iy) = u(x,y) + iv(x,y) then the partial derivatives of u,vu,v with respect to x,yx,y exist and satisfy the following system of differential equations at all points on R\mathscr{R}. {ux(x,y)=vy(x,y)uy(x,y)=vx(x,y) \begin{cases} u_{x} (x,y) = v_{y} (x,y) \\ u_{y} (x,y) = -v_{x} (x,y) \end{cases}

If ff is analytic, then f=u+ivf = u + i v must satisfy the Cauchy-Riemann equations. ux=vyuy=vx \begin{align*} {\frac{ \partial u }{ \partial x }} =& {\frac{ \partial v }{ \partial y }} \\ {\frac{ \partial u }{ \partial y }} =& - {\frac{ \partial v }{ \partial x }} \end{align*} Rewriting the right-hand side of uxu_{x}, which is vyv_{y}, in limit form gives the following. ux=limh0v(x+i(y+h))v(x+iy)h {\frac{ \partial u }{ \partial x }} = \lim_{h \to 0} {\frac{ v \left( x + i \left( y + h \right) \right) - v \left( x + i y \right) }{ h }} Though ff is formally treated like a complex function, it is inherently a real function. Thus, y=0y = 0 and v(x)=0v(x) = 0 are valid, and u(x)u(x) is indeed u(x)=f(x)u(x) = f(x). Substituting them one by one, with y=0y = 0, we have ux=limh0v(x+ih)v(x)h {\frac{ \partial u }{ \partial x }} = \lim_{h \to 0} {\frac{ v \left( x + i h \right) - v \left( x \right) }{ h }} and with v(x)=0v(x) = 0, we have ux=limh0v(x+ih)h {\frac{ \partial u }{ \partial x }} = \lim_{h \to 0} {\frac{ v \left( x + i h \right) }{ h }} Finally, f=u+ivf = u + i v leads to Imf=v\im f = v, and for a sufficiently small hh, we obtain the following formula. fx=limh0Imf(x+ih)hImf(x+ih)h {\frac{ \partial f }{ \partial x }} = \lim_{h \to 0} {\frac{ \im f \left( x + i h \right) }{ h }} \approx {\frac{ \im f \left( x + i h \right) }{ h }}

Taylor Expansion

Taylor Expansion: If the function f(x)f(x) is continuous at [a,b][a,b] and nn differentiable at (a,b)(a,b), then there exists ξ(a,b)\xi \in (a,b) satisfying f(b)=k=0n1(ba)kk!f(k)(a)+(ba)nn!f(n)(ξ)=f(a)+(ba)f(a)++(ba)n1(n1)!f(n1)(a)+(ba)n(n)!f(n)(ξ) \begin{align*} f(b) =& \sum_{k=0}^{n-1} {{(b-a)^{k}\over{k!}}{f^{(k)}( a )}} + {(b-a)^{n}\over{n!}}{f^{(n)}(\xi)} \\ =& {f(a)} + {(b-a)f ' (a)} + \cdots + {(b-a)^{n-1}\over{(n-1)!}}{f^{(n-1)}(a)} + {(b-a)^{n}\over{(n)!}}{f^{(n)}(\xi)} \end{align*}

Performing a Taylor expansion from f(x)f(x) to ihi h yields the following. f(x+ih)=f(x)+ihf(x)h2f(x)2!ih3f(x)3!s+ f \left( x + i h \right) = f (x) + i h f ' (x) - h^{2} {\frac{ f '' (x) }{ 2! }} - i h^{3} {\frac{ f ''' (x) }{ 3!s }} + \cdots Since we are interested in f(x)f ' (x), focusing on the imaginary part gives Imf(x+ih)=hf(x)h3f(x)3!s+ \im f \left( x + i h \right) = h f ' (x) - h^{3} {\frac{ f ''' (x) }{ 3!s }} + \cdots and canceling hh results in Imf(x+ih)h=f(x)+O(h2) {\frac{ \im f \left( x + i h \right) }{ h }} = f ' (x) + O \left( h^{2} \right) Similarly, if hh is sufficiently small, h2h^{2} becomes negligible, providing the following approximation. Imf(x+ih)hf(x) {\frac{ \im f \left( x + i h \right) }{ h }} \approx f ' (x)


  1. Martins, J. R., Sturdza, P., & Alonso, J. J. (2003). The complex-step derivative approximation. ACM Transactions on Mathematical Software (TOMS), 29(3), 245-262. https://dx.doi.org/10.1145/838250.838251 ↩︎