17.11.2012 Views

Numerical recipes

Numerical recipes

Numerical recipes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

124 Chapter 3. Interpolation and Extrapolation<br />

d2<br />

x1 = x1l<br />

pt. 4<br />

⊗<br />

pt. 1<br />

desired pt.<br />

(x1,x2)<br />

d1<br />

x2 = x2u<br />

x2 = x2l<br />

x1 = x1u<br />

pt. 3<br />

pt. 2<br />

(a) (b)<br />

y<br />

∂y/∂x1<br />

∂y/∂x2<br />

∂ 2 y/∂x1∂x2<br />

pt. number<br />

1 2 3 4<br />

user supplies<br />

these values<br />

Figure 3.6.1. (a) Labeling of points used in the two-dimensional interpolation routines bcuint and<br />

bcucof. (b) For each of the four points in (a), the user supplies one function value, two first derivatives,<br />

and one cross-derivative, a total of 16 numbers.<br />

function value changes continuously. However, the gradient of the interpolated<br />

function changes discontinuously at the boundaries of each grid square.<br />

There are two distinctly different directions that one can take in going beyond<br />

bilinear interpolation to higher-order methods: One can use higher order to obtain<br />

increased accuracy for the interpolated function (for sufficiently smooth functions!),<br />

without necessarily trying to fix up the continuity of the gradient and higher<br />

derivatives. Or, one can make use of higher order to enforce smoothness of some of<br />

these derivatives as the interpolating point crosses grid-square boundaries. We will<br />

now consider each of these two directions in turn.<br />

Higher Order for Accuracy<br />

The basic idea is to break up the problem into a succession of one-dimensional<br />

interpolations. If we want to do m-1 order interpolation in the x 1 direction, and n-1<br />

order in the x2 direction, we first locate an m × n sub-block of the tabulated function<br />

matrix that contains our desired point (x1,x2). We then do m one-dimensional<br />

interpolations in the x2 direction, i.e., on the rows of the sub-block, to get function<br />

values at the points (x1a[j],x2), j =1,...,m. Finally, we do a last interpolation<br />

in the x1 direction to get the answer. If we use the polynomial interpolation routine<br />

polint of §3.1, and a sub-block which is presumed to be already located (and<br />

addressed through the pointer float **ya, see §1.2), the procedure looks like this:<br />

#include "nrutil.h"<br />

void polin2(float x1a[], float x2a[], float **ya, int m, int n, float x1,<br />

float x2, float *y, float *dy)<br />

Given arrays x1a[1..m] and x2a[1..n] of independent variables, and a submatrix of function<br />

values ya[1..m][1..n], tabulated at the grid points defined by x1a and x2a; and given values<br />

x1 and x2 of the independent variables; this routine returns an interpolated function value y,<br />

and an accuracy indication dy (based only on the interpolation in the x1 direction, however).<br />

{<br />

void polint(float xa[], float ya[], int n, float x, float *y, float *dy);<br />

Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)<br />

Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by <strong>Numerical</strong> Recipes Software.<br />

Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable<br />

files (including this one) to any server computer, is strictly prohibited. To order <strong>Numerical</strong> Recipes books or CDROMs, visit website<br />

http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!