At the heart of conventional methods of derivative approximation based on divided differences lies a sum of the form in which the coefficients have different signs and vary (usually considerably) in magnitude. As a result, when these coefficients of different magnitude multiply function values, the absolute errors in the latter are amplified and the error in the outcome becomes quantitatively unpredictable [LynessMoler1967].

As a special case, consider the second-order central difference formula, one of the standard derivative approximation methods for a real valued function,

for which, as the name should suggest, the truncation error is . In theory, the truncation error could be made arbitrarily small by making the step size small enough. In practice, on a finite-precision machine with floating point arithmetic, this cannot be done due to the existence of another type of error, the rounding error. In the particular case of (1), for instance, too small a step size would lead to a catastrophic subtractive cancellation in the numerator, whose culprit is the rounding done when evaluating the exponential. Clearly, a compromise must be made between minimizing truncation and rounding errors. In the case of (1), analyzing the monotonic behavior reveals that when rounding becomes prominent, the approximate value given by (1) oscillates around the true value [SteplemanWinarsky1979]. Although this behavior can be used to select an optimal , only marginal improvements can be made [SquireTrapp1998].

Using complex variables it is possible to derive derivative approximation formulas that do not involve subtractions and hence are free from catastrophic cancellations. Let us derive a complex-step numerical approximation formula for the first derivative with the same order of truncation error as (1). If is an holomorphic function of a complex variable that is real valued on the real axis, then its Taylor expansion, with complex step , about the real point is

Taking imaginary parts of both sides of (2) yields

The first term on the RHS of (3) is an estimate to the LHS which is the first derivative. As promised, does not involve subtractions and, consequently, it will not suffer from the associated cancellation due to rounding.

This approach can be easily implemented to compute the jacobian matrix. (The implementation below is based on Yi Cao’s.)

function [J,z] = jacobianComplexStep(fun,x,varargin) z = fun(x); n = numel(x); m = numel(z); J = zeros(m,n); h = n*eps; optArgs = {h}; emptyArgs = cellfun(@isempty,varargin); [optArgs{~emptyArgs}] = varargin{~emptyArgs}; [h] = optArgs{:}; for k = 1:n x1 = x; x1(k) = x1(k) + h*1i; J(:,k) = imag(fun(x1))/h; end

Here is an equivalent implementation of the jacobian based on the second order central difference formula:

function [J,z] = jacobianCentralDiff(fun,x,varargin) z = fun(x); n = numel(x); m = numel(z); J = zeros(m,n); h = n*eps; optArgs = {h}; emptyArgs = cellfun(@isempty,varargin); [optArgs{~emptyArgs}] = varargin{~emptyArgs}; [h] = optArgs{:}; h_2 = 2*h; for k = 1:n [x_left,x_right] = deal(x); x_right(k) = x(k) + h; x_left( k) = x(k) - h; J(:,k) = (fun(x_right) - fun(x_left))/h_2; end

And here is a sample invocation of the functions `jacobianComplexStep`

and `jocobianCentralDiff`

:

f = @(x) x^(9/2); x = 1.5; le = -20; ri = -2; m = ri - le + 1; h = logspace(ri,le,m).'; [dfdxComplexStep,dfdxCentralDiff] = deal(zeros(m,1)); for iRow=1:m dfdxComplexStep(iRow) = jacobianComplexStep(f,x,h(iRow)); dfdxCentralDiff(iRow) = jacobianCentralDiff(f,x,h(iRow)); end fprintf('%.16e %.16e %.16e\n',[h dfdxComplexStep dfdxCentralDiff]')

The tables below compare the two methods by listing their approximations to the first derivative of the function at for different step sizes, using MATLAB R2009a on a machine running Windows XP with an Intel Core 2 Duo E8400 CPU.

, Complex Step | |

, Central Difference | |

The first table demonstrates that the complex step approximation improves with decreasing . On the other hand, the second table shows that the central difference approximation deteriorates after becomes less than, roughly, and finally succumbs to the catastrophic subtractive cancellation.

There is more to be said about using complex variables in numerical differentiation, so stay tuned….