1 ## Copyright (C) 2000-2012 Kai Habel
2 ## Copyright (C) 2006 David Bateman
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {@var{pp} =} spline (@var{x}, @var{y})
22 ## @deftypefnx {Function File} {@var{yi} =} spline (@var{x}, @var{y}, @var{xi})
23 ## Return the cubic spline interpolant of points @var{x} and @var{y}.
25 ## When called with two arguments, return the piecewise polynomial @var{pp}
26 ## that may be used with @code{ppval} to evaluate the polynomial at specific
27 ## points. When called with a third input argument, @code{spline} evaluates
28 ## the spline at the points @var{xi}. The third calling form @code{spline
29 ## (@var{x}, @var{y}, @var{xi})} is equivalent to @code{ppval (spline
30 ## (@var{x}, @var{y}), @var{xi})}.
32 ## The variable @var{x} must be a vector of length @var{n}. @var{y} can be
33 ## either a vector or array. If @var{y} is a vector it must have a length of
34 ## either @var{n} or @code{@var{n} + 2}. If the length of @var{y} is
35 ## @var{n}, then the "not-a-knot" end condition is used. If the length of
36 ## @var{y} is @code{@var{n} + 2}, then the first and last values of the
37 ## vector @var{y} are the values of the first derivative of the cubic spline
40 ## If @var{y} is an array, then the size of @var{y} must have the form
42 ## $$[s_1, s_2, \cdots, s_k, n]$$
45 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n}]}
49 ## $$[s_1, s_2, \cdots, s_k, n + 2].$$
52 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n} + 2]}.
54 ## The array is reshaped internally to a matrix where the leading
55 ## dimension is given by
57 ## $$s_1 s_2 \cdots s_k$$
60 ## @code{@var{s1} * @var{s2} * @dots{} * @var{sk}}
62 ## and each row of this matrix is then treated separately. Note that this
63 ## is exactly opposite to @code{interp1} but is done for @sc{matlab}
66 ## @seealso{pchip, ppval, mkpp, unmkpp}
69 ## This code is based on csape.m from octave-forge, but has been
70 ## modified to use the sparse solver code in octave that itself allows
71 ## special casing of tri-diagonal matrices, modified for NDArrays and
72 ## for the treatment of vectors y 2 elements longer than x as complete
75 function ret = spline (x, y, xi)
80 error ("spline: requires at least 2 points");
83 ## Check the size and shape of y
86 if (ndy == 2 && (szy(1) == n || szy(2) == n))
94 a = shiftdim (reshape (y, [prod(szy(1:end-1)), szy(end)]), 1);
97 for k = (1:columns (a))(any (isnan (a)))
98 ok = ! isnan (a(:,k));
99 a(!ok,k) = spline (x(ok), a(ok,k), x(!ok));
103 if (size (a, 1) == n + 2)
115 b = c = zeros (size (a));
117 idx = ones (columns (a), 1);
122 d = (dfs + dfe) / (x(2) - x(1)) ^ 2 + ...
123 2 * (a(1,:) - a(2,:)) / (x(2) - x(1)) ^ 3;
124 c = (-2 * dfs - dfe) / (x(2) - x(1)) - ...
125 3 * (a(1,:) - a(2,:)) / (x(2) - x(1)) ^ 2;
135 dg = 1.5 * h(1) - 0.5 * h(2);
136 c(2:n-1,:) = 1/dg(1);
138 dg = 2 * (h(1:n-2) .+ h(2:n-1));
139 dg(1) = dg(1) - 0.5 * h(1);
140 dg(n-2) = dg(n-2) - 0.5 * h(n-1);
144 g = 3 * diff (a(2:n,:)) ./ h(2:n-1,idx) ...
145 - 3 * diff (a(1:n-1,:)) ./ h(1:n-2,idx);
146 g(1,:) = 3 * (a(3,:) - a(2,:)) / h(2) ...
147 - 3 / 2 * (3 * (a(2,:) - a(1,:)) / h(1) - dfs);
148 g(n-2,:) = 3 / 2 * (3 * (a(n,:) - a(n-1,:)) / h(n-1) - dfe) ...
149 - 3 * (a(n-1,:) - a(n-2,:)) / h(n-2);
151 c(2:n-1,:) = spdiags ([[e(:); 0], dg, [0; e(:)]],
152 [-1, 0, 1], n-2, n-2) \ g;
155 c(1,:) = (3 / h(1) * (a(2,:) - a(1,:)) - 3 * dfs
156 - c(2,:) * h(1)) / (2 * h(1));
157 c(n,:) = - (3 / h(n-1) * (a(n,:) - a(n-1,:)) - 3 * dfe
158 + c(n-1,:) * h(n-1)) / (2 * h(n-1));
159 b(1:n-1,:) = diff (a) ./ h(1:n-1, idx) ...
160 - h(1:n-1,idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:));
161 d = diff (c) ./ (3 * h(1:n-1, idx));
171 b = (a(2,:) - a(1,:)) / (x(2) - x(1));
180 c = (a(1,:) - a(3,:)) / ((x(3) - x(1)) * (x(2) - x(3))) ...
181 + (a(2,:) - a(1,:)) / ((x(2) - x(1)) * (x(2) - x(3)));
182 b = (a(2,:) - a(1,:)) * (x(3) - x(1)) ...
183 / ((x(2) - x(1)) * (x(3) - x(2))) ...
184 + (a(1,:) - a(3,:)) * (x(2) - x(1)) ...
185 / ((x(3) - x(1)) * (x(3) - x(2)));
188 x = [min(x), max(x)];
195 g = zeros (n-2, columns (a));
196 g(1,:) = 3 / (h(1) + h(2)) ...
197 * (a(3,:) - a(2,:) - h(2) / h(1) * (a(2,:) - a(1,:)));
198 g(n-2,:) = 3 / (h(n-1) + h(n-2)) ...
199 * (h(n-2) / h(n-1) * (a(n,:) - a(n-1,:)) - (a(n-1,:) - a(n-2,:)));
203 g(2:n - 3,:) = 3 * diff (a(3:n-1,:)) ./ h(3:n-2,idx) ...
204 - 3 * diff (a(2:n-2,:)) ./ h(2:n - 3,idx);
206 dg = 2 * (h(1:n-2) .+ h(2:n-1));
207 dg(1) = dg(1) - h(1);
208 dg(n-2) = dg(n-2) - h(n-1);
210 ldg = udg = h(2:n-2);
211 udg(1) = udg(1) - h(1);
212 ldg(n - 3) = ldg(n-3) - h(n-1);
213 c(2:n-1,:) = spdiags ([[ldg(:); 0], dg, [0; udg(:)]],
214 [-1, 0, 1], n-2, n-2) \ g;
218 dg = [h(1) + 2 * h(2); 2 * h(2) + h(3)];
221 c(2:n-1,:) = spdiags ([[ldg(:);0], dg, [0; udg(:)]],
222 [-1, 0, 1], n-2, n-2) \ g;
226 c(1,:) = c(2,:) + h(1) / h(2) * (c(2,:) - c(3,:));
227 c(n,:) = c(n-1,:) + h(n-1) / h(n-2) * (c(n-1,:) - c(n-2,:));
228 b = diff (a) ./ h(1:n-1, idx) ...
229 - h(1:n-1, idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:));
230 d = diff (c) ./ (3 * h(1:n-1, idx));
232 d = d(1:n-1,:);d = d.'(:);
233 c = c(1:n-1,:);c = c.'(:);
234 b = b(1:n-1,:);b = b.'(:);
235 a = a(1:n-1,:);a = a.'(:);
239 ret = mkpp (x, cat (2, d, c, b, a), szy(1:end-1));
242 ret = ppval (ret, xi);
248 %! x = 0:10; y = sin(x);
249 %! xspline = 0:0.1:10; yspline = spline(x,y,xspline);
250 %! title("spline fit to points from sin(x)");
251 %! plot(xspline,sin(xspline),"r",xspline,yspline,"g-",x,y,"b+");
252 %! legend("original","interpolation","interpolation points");
253 %! %--------------------------------------------------------
254 %! % confirm that interpolated function matches the original
257 %! x = [0:10]; y = sin(x); abserr = 1e-14;
258 %!assert (spline(x,y,x), y, abserr);
259 %!assert (spline(x,y,x'), y', abserr);
260 %!assert (spline(x',y',x'), y', abserr);
261 %!assert (spline(x',y',x), y, abserr);
262 %!assert (isempty(spline(x',y',[])));
263 %!assert (isempty(spline(x,y,[])));
264 %!assert (spline(x,[y;y],x), [spline(x,y,x);spline(x,y,x)],abserr)
265 %!assert (spline(x,[y;y],x'), [spline(x,y,x);spline(x,y,x)],abserr)
266 %!assert (spline(x',[y;y],x), [spline(x,y,x);spline(x,y,x)],abserr)
267 %!assert (spline(x',[y;y],x'), [spline(x,y,x);spline(x,y,x)],abserr)
268 %! y = cos(x) + i*sin(x);
269 %!assert (spline(x,y,x), y, abserr)
270 %!assert (real(spline(x,y,x)), real(y), abserr);
271 %!assert (real(spline(x,y,x.')), real(y).', abserr);
272 %!assert (real(spline(x.',y.',x.')), real(y).', abserr);
273 %!assert (real(spline(x.',y,x)), real(y), abserr);
274 %!assert (imag(spline(x,y,x)), imag(y), abserr);
275 %!assert (imag(spline(x,y,x.')), imag(y).', abserr);
276 %!assert (imag(spline(x.',y.',x.')), imag(y).', abserr);
277 %!assert (imag(spline(x.',y,x)), imag(y), abserr);
282 %! assert (spline (x, y, x(ok)), y(ok), abserr);
285 %! assert (! isnan (spline (x, y, x(!ok))));
289 %! assert (spline (x,y,x), [1,4], abserr);
293 %! assert (spline (x,y,x), [1,4], abserr);
297 %! pp = spline (x,y);
298 %! [x,P] = unmkpp (pp);
299 %! assert (norm (P-[3,-3,1,2]), 0, abserr);
303 %! pp = spline (x,y);
304 %! [x,P] = unmkpp (pp);
305 %! assert (norm (P-[7,-9,1,3]), 0, abserr);