Numerical Analysis/Differentiation/Examples

From testwiki
Jump to navigation Jump to search

When deriving a finite difference approximation of the jth derivative of a function f:, we wish to find a1,a2,...,an and b1,b2,...,bn such that

f(j)(x0)=hji=1naif(x0+bih)+O(hk) as h0

or, equivalently,

hji=1naif(x0+bih)=f(j)(x0)+O(hk) as h0

where O(hk) is the error, the difference between the correct answer and the approximation, expressed using Big-O notation. Because h may be presumed to be small, a larger value for k is better than a smaller value.

A general method for finding the coefficients is to generate the Taylor expansion of hji=1naif(x0+bih) and choose a1,a2,...,an and b1,b2,...,bn such that f(j)(x0) and the remainder term are the only non-zero terms. If there are no such coefficients, a smaller value for k must be chosen.

For a function of m variables g:m, the procedure is similar, except x0,b1,b2,...,bn are replaced by points in m and the multivariate extension of Taylor's theorem is used.


Single-Variable

In all single-variable examples, x0 and f: are unknown, and h is small. Additionally, let f be 5 times continuously differentiable on .

First Derivative

Find a,b,c such that af(x0+h)+bf(x0+ch)h best approximates f(x0).

Let f: be 42 times continuously differentiable on . Find the largest n such that

dfdx(x0)=f(x0+2h)+8f(x0+h,y0)8f(x0h)+f(x02h)12h+O(hn) as h0

In other words, find the order of the error of the method.

Second Derivative

Find a,b,c such that af(x0h)+bf(x0)+cf(x0+h)h2 best approximates f(x0).

Multivariate

In all two-variable examples, x0,y0 and f:2 are unknown, and h is small.

Non-Mixed Derivatives

Because of the nature of partial derivatives, some of them may be calculated using single-variable methods. This is done by holding constant all but one variable to form a new function of one variable. For example if gy(y)=f(x0,y), then dfdy(x0,y0)=dgdy(y0).

Find an approximation of ddyf(x0,y0)

Mixed Derivatives

Mixed derivatives may require the multivariate extension of Taylor's theorem.

Let f:2 be 42 times continuously differentiable on 2 and let g:3 be defined as

g(x0,y0,h)=f(x0+h,y0+h)+f(x0h,y0h)f(x0h,y0+h)f(x0+h,y0h)4h2.

Find the largest n such that

d2fdxdy(x0,y0)=g(x0,y0,h)+O(hn) as h0.

In other words, find the order of the error of the approximation.

Example Code

Implementing these methods is reasonably simple in programming languages that support higher-order functions. For example, the method from the first example may be implemented in C++ using function pointers, as follows:

//  Returns an approximation of the derivative of f at x.
double derivative (double (*f)(double), double x, double h =0.01) {
  return (f(x + h) - f(x - h)) / (2 * h);
}

Template:CourseCat