Overview of interpolation in Scilab
In this page, we present an overview of interpolation functions in Scilab
Contents
Introduction
The creation of a spline-based interpolation is generally made of two steps :
- the creation of the spline,
- the evaluation of the spline at some points xp, yp, zp.
The creation of the spline may require to compute the coefficients of the spline by solving a system of linear equations. As the matrix is tridiagonal, the cost of this resolution is reduced.
In the following functions, these two steps are provided as two separate functions:
- splin and interp: 1D spline creation and evaluation to approximate y=f(x)
- splin2d and interp2d: 2D spline creation and evaluation to approximate z=f(x,y) (interpolation points must be on a grid)
- splin3d, interp3d (and bsplin3val): 3D spline creation and evaluation to approximate r=f(x,y,z) (or its derivatives)
- cshep2d and eval_cshep2d: 2D interpolation creation and evaluation to approximate z=f(x,y) (squattered interpolation points)
- lsq_splin and interp: 1D spline creation and evaluation to approximate y=f(x) (least squares approximation)
In the following functions, one single function performs both the creation and the evaluation of the spline at given points:
- interp1: 1D spline creation and evaluation to approximate y=f(x)
- interpln: 1D linear creation and evaluation to approximate y=f(x)
- linear_interpn: multi-dimensionnal creation and evaluation of linear interpolation to approximate y=f(x1,x2,...,xd)
There are other functions which make use of interpolation tools to process datas:
- smooth: 1D smoothing of data by spline functions.
- intsplin: 1D spline interpolation to approximate an integral.
Features:
- The splin* functions take a splin_type input argument to define the kind of spline or sub-spline to compute.
- The interp* functions take a out_mode input argument to define the rule when evaluating outside the interpolation domain
Most of this module is based on the work by Bruno Pincon.
A 1D spline interpolation
The following example is from the help page of the interp function.
a = -8; b = 8; x = linspace(a,b,20)'; y = sinc(x); dk = splin(x,y); // not_a_knot df = splin(x,y, "fast"); xx = linspace(a,b,800)'; [yyk, yy1k, yy2k] = interp(xx, x, y, dk); [yyf, yy1f, yy2f] = interp(xx, x, y, df); clf() subplot(3,1,1) plot2d(xx, [yyk yyf]) plot2d(x, y, style=-9) legends(["not_a_knot spline","fast sub-spline","interpolation points"],... [1 2 -9], "ur",%f) xtitle("spline interpolation") subplot(3,1,2) plot2d(xx, [yy1k yy1f]) legends(["not_a_knot spline","fast sub-spline"], [1 2], "ur",%f) xtitle("spline interpolation (derivatives)") subplot(3,1,3) plot2d(xx, [yy2k yy2f]) legends(["not_a_knot spline","fast sub-spline"], [1 2], "lr",%f) xtitle("spline interpolation (second derivatives)")
The previous script produces the following figure.
A 2D spline interpolation
The following example shows how to combine splin2d and interp2d to perform a 2D spline interpolation. It performs the interpolation of the cos(x)cos(y) function.
// // 1. Create the spline nx = 7; ny = 15; x = linspace(0,2*%pi,nx); y = linspace(%pi/2,4*%pi,ny); z = cos(x')*cos(y); C = splin2d(x, y, z, "periodic"); // // 2. Evaluate the spline on a grid mx = 50; my = 20; xx = linspace(%pi,1.5*%pi,mx); yy = linspace(%pi,2*%pi,my); [XX,YY] = ndgrid(xx,yy); zz = interp2d(XX,YY, x, y, C); // // 3. Plot the interpolated values scf(); plot3d(xx, yy, zz)
The previous script produces the following figure.
Acknowledgments
We thank Bruno Pincon for contributing to this module and to the content of this page.