📄 interp.texi
字号:
@cindex interpolation@cindex splineThis chapter describes functions for performing interpolation. Thelibrary provides a variety of interpolation methods, including Cubicsplines and Akima splines. The interpolation types are interchangeable,allowing different methods to be used without recompiling.Interpolations can be defined for both normal and periodic boundaryconditions. Additional functions are available for computingderivatives and integrals of interpolating functions.The functions described in this section are declared in the header files@file{gsl_interp.h} and @file{gsl_spline.h}.@menu* Introduction to Interpolation:: * Interpolation Functions:: * Interpolation Types:: * Index Look-up and Acceleration:: * Evaluation of interpolating functions:: * Higher-level interface:: * Interpolation Example programs:: * Interpolation References and Further Reading:: @end menu@node Introduction to Interpolation@section IntroductionGiven a set of data points @math{(x_1, y_1) \dots (x_n, y_n)} theroutines described in this section compute a continuous interpolatingfunction @math{y(x)} such that @math{y_i = y(x_i)}. The interpolationis piecewise smooth, and its behavior at the points is determined by thetype of interpolation used.@node Interpolation Functions@section Interpolation FunctionsThe interpolation function for a given dataset is stored in a@code{gsl_interp} object. These are created by the following functions.@deftypefun {gsl_interp *} gsl_interp_alloc (const gsl_interp_type * @var{T}, size_t @var{size})This function returns a pointer to a newly allocated interpolationobject of type @var{T} for @var{size} data-points.@end deftypefun@deftypefun int gsl_interp_init (gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], size_t @var{size})This function initializes the interpolation object @var{interp} for thedata (@var{xa},@var{ya}) where @var{xa} and @var{ya} are arrays of size@var{size}. The interpolation object (@code{gsl_interp}) does not savethe data arrays @var{xa} and @var{ya} and only stores the static statecomputed from the data. The @var{xa} data array is always assumed to bestrictly ordered; the behavior for other arrangements is not defined.@end deftypefun@deftypefun void gsl_interp_free (gsl_interp * @var{interp})This function frees the interpolation object @var{interp}.@end deftypefun@node Interpolation Types@section Interpolation TypesThe interpolation library provides five interpolation types:@deffn {Interpolation Type} gsl_interp_linear@cindex linear interpolationLinear interpolation. This interpolation method does not require anyadditional memory.@end deffn@deffn {Interpolation Type} gsl_interp_cspline@cindex cubic splinesCubic spline with natural boundary conditions@end deffn@deffn {Interpolation Type} gsl_interp_cspline_periodicCubic spline with periodic boundary conditions@end deffn@deffn {Interpolation Type} gsl_interp_akima@cindex Akima splinesAkima spline with natural boundary conditions@end deffn@deffn {Interpolation Type} gsl_interp_akima_periodicAkima spline with periodic boundary conditions@end deffnThe following related functions are available,@deftypefun {const char *} gsl_interp_name (const gsl_interp * @var{interp})This function returns the name of the interpolation type used by @var{interp}.For example,@exampleprintf("interp uses '%s' interpolation\n", gsl_interp_name (interp));@end example@noindentwould print something like,@exampleinterp uses 'cspline' interpolation.@end example@end deftypefun@deftypefun {unsigned int} gsl_interp_min_size (const gsl_interp * @var{interp})This function returns the minimum number of points required by theinterpolation type of @var{interp}. For example, cubic interpolationrequires a minimum of 3 points.@end deftypefun@node Index Look-up and Acceleration@section Index Look-up and AccelerationThe state of searches can be stored in a @code{gsl_interp_accel} object,which is a kind of iterator for interpolation lookups. It caches theprevious value of an index lookup. When the subsequent interpolationpoint falls in the same interval its index value can be returnedimmediately.@deftypefun size_t gsl_interp_bsearch (const double x_array[], double @var{x}, size_t @var{index_lo}, size_t @var{index_hi})This function returns the index @math{i} of the array @var{x_array} suchthat @code{x_array[i] <= x < x_array[i+1]}. The index is searched forin the range [@var{index_lo},@var{index_hi}].@end deftypefun@deftypefun {gsl_interp_accel *} gsl_interp_accel_alloc (void)This function returns a pointer to an accelerator object, which is akind of iterator for interpolation lookups. It tracks the state oflookups, thus allowing for application of various accelerationstrategies.@end deftypefun@deftypefun size_t gsl_interp_accel_find (gsl_interp_accel * @var{a}, const double x_array[], size_t @var{size}, double @var{x})This function performs a lookup action on the data array @var{x_array}of size @var{size}, using the given accelerator @var{a}. This is howlookups are performed during evaluation of an interpolation. Thefunction returns an index @math{i} such that @code{xarray[i] <= x <xarray[i+1]}.@end deftypefun@deftypefun void gsl_interp_accel_free (gsl_interp_accel* @var{a})This function frees the accelerator object @var{a}.@end deftypefun@node Evaluation of interpolating functions@section Evaluation of interpolating functions@deftypefun double gsl_interp_eval (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], double @var{x}, gsl_interp_accel * @var{a})@deftypefunx int gsl_interp_eval_e (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], double @var{x}, gsl_interp_accel * @var{a}, double * @var{y})These functions return the interpolated value of @var{y} for a givenpoint @var{x}, using the interpolation object @var{interp}, data arrays@var{xa} and @var{ya} and the accelerator @var{a}.@end deftypefun@deftypefun double gsl_interp_eval_deriv (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], double @var{x}, gsl_interp_accel * @var{a})@deftypefunx int gsl_interp_eval_deriv_e (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], double @var{x}, gsl_interp_accel * @var{a}, double * @var{d})These functions return the derivative @var{d} of an interpolatedfunction for a given point @var{x}, using the interpolation object@var{interp}, data arrays @var{xa} and @var{ya} and the accelerator@var{a}. @end deftypefun@deftypefun double gsl_interp_eval_deriv2 (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], double @var{x}, gsl_interp_accel * @var{a})@deftypefunx int gsl_interp_eval_deriv2_e (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], double @var{x}, gsl_interp_accel * @var{a}, double * @var{d2})These functions return the second derivative @var{d2} of an interpolatedfunction for a given point @var{x}, using the interpolation object@var{interp}, data arrays @var{xa} and @var{ya} and the accelerator@var{a}. @end deftypefun@deftypefun double gsl_interp_eval_integ (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], double @var{a}, double @var{b}double @var{x}, gsl_interp_accel * @var{a})@deftypefunx int gsl_interp_eval_integ_e (const gsl_interp * @var{interp}, const double @var{xa}[], const double @var{ya}[], , double @var{a}, double @var{b}, gsl_interp_accel * @var{a}, double * @var{result})These functions return the numerical integral @var{result} of aninterpolated function over the range [@var{a}, @var{b}], using theinterpolation object @var{interp}, data arrays @var{xa} and @var{ya} andthe accelerator @var{a}.@end deftypefun@node Higher-level interface@section Higher-level interfaceThe functions described in the previous sections required the user tosupply pointers to the @math{x} and @math{y} arrays on each call. Thefollowing functions are equivalent to the corresponding@code{gsl_interp} functions but maintain a copy of this data in the@code{gsl_spline} object. This removes the need to pass both @var{xa}and @var{ya} as arguments on each evaluation. These functions aredefined in the header file @file{gsl_spline.h}.@deftypefun {gsl_spline *} gsl_spline_alloc (const gsl_interp_type * @var{T}, size_t @var{n})@end deftypefun@deftypefun int gsl_spline_init (gsl_spline * @var{spline}, const double xa[], const double @var{ya}[], size_t @var{size})@end deftypefun@deftypefun void gsl_spline_free (gsl_spline * @var{spline})@end deftypefun@deftypefun double gsl_spline_eval (const gsl_spline * @var{spline}, double @var{x}, gsl_interp_accel * @var{a})@deftypefunx int gsl_spline_eval_e (const gsl_spline * @var{spline}, double @var{x}, gsl_interp_accel * @var{a}, double * @var{y})@end deftypefun@deftypefun double gsl_spline_eval_deriv (const gsl_spline * @var{spline}, double @var{x}, gsl_interp_accel * @var{a})@deftypefunx int gsl_spline_eval_deriv_e (const gsl_spline * @var{spline}, double @var{x}, gsl_interp_accel * @var{a}, double * @var{d})@end deftypefun@deftypefun double gsl_spline_eval_deriv2 (const gsl_spline * @var{spline}, double @var{x}, gsl_interp_accel * @var{a})@deftypefunx int gsl_spline_eval_deriv2_e (const gsl_spline * @var{spline}, double @var{x}, gsl_interp_accel * @var{a}, double * @var{d2})@end deftypefun@deftypefun double gsl_spline_eval_integ (const gsl_spline * @var{spline}, double @var{a}, double @var{b}, gsl_interp_accel * @var{acc})@deftypefunx int gsl_spline_eval_integ_e (const gsl_spline * @var{spline}, double @var{a}, double @var{b}, gsl_interp_accel * @var{acc}, double * @var{result})@end deftypefun@node Interpolation Example programs@section ExamplesThe following program demonstrates the use of the interpolation andspline functions. It computes a cubic spline interpolation of the10-point dataset @math{(x_i, y_i)} where @math{x_i = i + \sin(i)/2} and@math{y_i = i + \cos(i^2)} for @math{i = 0 \dots 9}.@example#include <config.h>#include <stdlib.h>#include <stdio.h>#include <math.h>#include <gsl/gsl_errno.h>#include <gsl/gsl_spline.h>intmain (void)@{ int i; double xi, yi, x[10], y[10]; printf ("#m=0,S=2\n"); for (i = 0; i < 10; i++) @{ x[i] = i + 0.5 * sin (i); y[i] = i + cos (i * i); printf ("%g %g\n", x[i], y[i]); @} printf ("#m=1,S=0\n"); @{ gsl_interp_accel *acc = gsl_interp_accel_alloc (); gsl_spline *spline = gsl_spline_alloc (gsl_interp_cspline, 10); gsl_spline_init (spline, x, y, 10); for (xi = x[0]; xi < x[9]; xi += 0.01) @{ double yi = gsl_spline_eval (spline, xi, acc); printf ("%g %g\n", xi, yi); @} gsl_spline_free (spline); gsl_interp_accel_free(acc); @} return 0;@}@end example@noindentThe output is designed to be used with the @sc{gnu} plotutils@code{graph} program,@example$ ./a.out > interp.dat$ graph -T ps < interp.dat > interp.ps@end example@iftex@sp 1@center @image{interp,3in}@end iftex@noindentThe result shows a smooth interpolation of the original points. Theinterpolation method can changed simply by varying the first argument of@code{gsl_spline_alloc}.@node Interpolation References and Further Reading@section References and Further Reading@noindentDescriptions of the interpolation algorithms and further references canbe found in the following book,@itemize @asis@item C.W. Ueberhuber,@cite{Numerical Computation (Volume 1), Chapter 9 "Interpolation"},Springer (1997), ISBN 3-540-62058-3.@end itemize@noindent
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -