Returns the cubic spline interpolation of y at the point x. Called with two arguments the piece-wise polynomial pp that may later be used with
ppval
to evaluate the polynomial at specific points.The variable x must be a vector of length n, and y can be either a vector or array. In the case where y is a vector, it can have a length of either n or n
+ 2
. If the length of y is n, then the 'not-a-knot' end condition is used. If the length of y is n+ 2
, then the first and last values of the vector y are the first derivative of the cubic spline at the end-points.If y is an array, then the size of y must have the form or The array is then reshaped internally to a matrix where to leading dimension is given by and each row this matrix is then treated separately. Note that this is exactly the opposite treatment than
interp1
and is done for compatibility.Called with a third input argument,
spline
evaluates the piece-wise spline at the points xi. There is an equivalence betweenppval (spline (
x,
y),
xi)
andspline (
x,
y,
xi)
.
The following code
x = 0:10; y = sin(x); xspline = 0:0.1:10; yspline = spline(x,y,xspline); title("spline fit to points from sin(x)"); plot(xspline,sin(xspline),"r",xspline,yspline,"g-",x,y,"b+"); legend("original","interpolation","interpolation points"); %-------------------------------------------------------- % confirm that interpolated function matches the original
Produces the following figure
![]() |