5824
|
1 ## Copyright (C) 2000,2001 Kai Habel |
|
2 ## Copyright (C) 2006 David Bateman |
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
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 2, or (at your option) |
|
9 ## any later version. |
|
10 ## |
|
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. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
|
17 ## along with Octave; see the file COPYING. If not, write to the Free |
|
18 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 ## 02110-1301, USA. |
|
20 |
|
21 ## -*- texinfo -*- |
|
22 ## @deftypefn {Function File} {@var{pp} = } spline (@var{x}, @var{y}) |
|
23 ## @deftypefnx {Function File} {@var{yi} = } spline (@var{x}, @var{y}, @var{xi}) |
|
24 ## |
|
25 ## Returns the cubic spline interpolation of @var{y} at the point |
5837
|
26 ## @var{x}. Called with two arguments the piece-wise polynomial @var{pp} |
5824
|
27 ## that may later be used with @code{ppval} to evaluate the polynomial |
|
28 ## at specific points. |
|
29 ## |
|
30 ## The variable @var{x} must be a vector of length @var{n}, and @var{y} |
5837
|
31 ## can be either a vector or array. In the case where @var{y} is a |
5824
|
32 ## vector, it can have a length of either @var{n} or @code{@var{n} + 2}. |
|
33 ## If the length of @var{y} is @var{n}, then the 'not-a-knot' end |
|
34 ## condition is used. If the length of @var{y} is @code{@var{n} + 2}, |
|
35 ## then the first and last values of the vector @var{y} are the first |
|
36 ## derivative of the cubic spline at the end-points. |
|
37 ## |
|
38 ## If @var{y} is an array, then the size of @var{y} must have the form |
|
39 ## @iftex |
|
40 ## @tex |
|
41 ## $$[s_1, s_2, \cdots, s_k, n]$$ |
|
42 ## @end tex |
|
43 ## @end iftex |
|
44 ## @ifinfo |
|
45 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n}]} |
|
46 ## @end ifinfo |
|
47 ## or |
|
48 ## @iftex |
|
49 ## @tex |
|
50 ## $$[s_1, s_2, \cdots, s_k, n + 2]$$. |
|
51 ## @end tex |
|
52 ## @end iftex |
|
53 ## @ifinfo |
|
54 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n} + 2]}. |
|
55 ## @end ifinfo |
|
56 ## The array is then reshaped internally to a matrix where to leading |
|
57 ## dimension is given by |
|
58 ## @iftex |
|
59 ## @tex |
|
60 ## $$s_1 s_2 \cdots s_k$$ |
|
61 ## @end tex |
|
62 ## @end iftex |
|
63 ## @ifinfo |
|
64 ## @code{@var{s1} * @var{s2} * @dots{} * @var{sk}} |
|
65 ## @end ifinfo |
|
66 ## and each row this matrix is then treated seperately. Note that this |
|
67 ## is exactly the opposite treatment than @code{interp1} and is done |
|
68 ## for compatiability. |
|
69 ## |
|
70 ## Called with a third input argument, @code{spline} evaluates the |
|
71 ## piece-wise spline at the points @var{xi}. There is an equivalence |
|
72 ## between @code{ppval (spline (@var{x}, @var{y}), @var{xi})} and |
|
73 ## @code{spline (@var{x}, @var{y}, @var{xi})}. |
|
74 ## |
|
75 ## @seealso{ppval, mkpp, unmkpp} |
|
76 ## @end deftypefn |
|
77 |
|
78 ## This code is based on csape.m from octave-forge, but has been |
|
79 ## modified to use the sparse solver code in octave that itself allows |
|
80 ## special casing of tri-diagonal matrices, modified for NDArrays and |
|
81 ## for the treatment of vectors y 2 elements longer than x as complete |
|
82 ## splines. |
|
83 |
|
84 function ret = spline (x, y, xi) |
|
85 |
|
86 x = x(:); |
|
87 n = length (x); |
|
88 if (n < 3) |
|
89 error ("spline: requires at least 3 points"); |
|
90 endif |
|
91 |
|
92 ## Check the size and shape of y |
|
93 ndy = ndims (y); |
|
94 szy = size (y); |
|
95 if (ndy == 2 && (szy(1) == 1 || szy(2) == 1)) |
|
96 if (szy(1) == 1) |
|
97 a = y'; |
|
98 else |
|
99 a = y; |
|
100 szy = fliplr (szy); |
|
101 endif |
|
102 else |
|
103 a = reshape (y, [prod(szy(1:end-1)), szy(end)])'; |
|
104 endif |
|
105 complete = false; |
|
106 if (size (a, 1) == n + 2) |
|
107 complete = true; |
|
108 dfs = a(1,:); |
|
109 dfe = a(end,:); |
|
110 a = a(2:end-1,:); |
|
111 endif |
|
112 |
|
113 b = c = zeros (size (a)); |
|
114 h = diff (x); |
|
115 idx = ones (columns (a),1); |
|
116 |
|
117 if (complete) |
|
118 |
|
119 if (n == 3) |
|
120 dg = 1.5 * h(1) - 0.5 * h(2); |
|
121 c(2:n - 1,:) = 1/dg(1); |
|
122 else |
|
123 dg = 2 * (h(1:n - 2) .+ h(2:n - 1)); |
|
124 dg(1) = dg(1) - 0.5 * h(1); |
|
125 dg(n - 2) = dg(n-2) - 0.5 * h(n - 1); |
|
126 |
|
127 e = h(2:n - 2); |
|
128 |
|
129 size(a) |
|
130 size(h) |
|
131 n |
|
132 |
|
133 g = 3 * diff (a(2:n,:)) ./ h(2:n - 1,idx)\ |
|
134 - 3 * diff (a(1:n - 1,:)) ./ h(1:n - 2,idx); |
|
135 g(1,:) = 3 * (a(3,:) - a(2,:)) / h(2) \ |
|
136 - 3 / 2 * (3 * (a(2,:) - a(1,:)) / h(1) - dfs); |
|
137 g(n - 2,:) = 3 / 2 * (3 * (a(n,:) - a(n - 1,:)) / h(n - 1) - dfe)\ |
|
138 - 3 * (a(n - 1,:) - a(n - 2,:)) / h(n - 2); |
|
139 |
|
140 c(2:n - 1,:) = spdiags([[e(:);0],dg,[0;e(:)]],[-1,0,1],n-2,n-2) \ g; |
|
141 endif |
|
142 |
|
143 c(1,:) = (3 / h(1) * (a(2,:) - a(1,:)) - 3 * dfs |
|
144 - c(2,:) * h(1)) / (2 * h(1)); |
|
145 c(n,:) = - (3 / h(n - 1) * (a(n,:) - a(n - 1,:)) - 3 * dfe |
|
146 + c(n - 1,:) * h(n - 1)) / (2 * h(n - 1)); |
|
147 b(1:n - 1,:) = diff (a) ./ h(1:n - 1, idx)\ |
|
148 - h(1:n - 1,idx) / 3 .* (c(2:n,:) + 2 * c(1:n - 1,:)); |
|
149 d = diff (c) ./ (3 * h(1:n - 1, idx)); |
|
150 |
|
151 else |
|
152 |
|
153 g = zeros(n - 2,columns(a)); |
|
154 g(1,:) = 3 / (h(1) + h(2)) * (a(3,:) - a(2,:)\ |
|
155 - h(2) / h(1) * (a(2,:) - a(1,:))); |
|
156 g(n - 2,:) = 3 / (h(n - 1) + h(n - 2)) *\ |
|
157 (h(n - 2) / h(n - 1) * (a(n,:) - a(n - 1,:)) -\ |
|
158 (a(n - 1,:) - a(n - 2,:))); |
|
159 |
|
160 if (n > 4) |
|
161 |
|
162 g(2:n - 3,:) = 3 * diff (a(3:n - 1,:)) ./ h(3:n - 2,idx)\ |
|
163 - 3 * diff (a(2:n - 2,:)) ./ h(2:n - 3,idx); |
|
164 |
|
165 dg = 2 * (h(1:n - 2) .+ h(2:n - 1)); |
|
166 dg(1) = dg(1) - h(1); |
|
167 dg(n - 2) = dg(n-2) - h(n - 1); |
|
168 |
|
169 ldg = udg = h(2:n - 2); |
|
170 udg(1) = udg(1) - h(1); |
|
171 ldg(n - 3) = ldg(n-3) - h(n - 1); |
|
172 c(2:n - 1,:) = spdiags([[ldg(:);0],dg,[0;udg(:)]],[-1,0,1],n-2,n-2) \ g; |
|
173 |
|
174 elseif (n == 4) |
|
175 |
|
176 dg = [h(1) + 2 * h(2), 2 * h(2) + h(3)]; |
|
177 ldg = h(2) - h(3); |
|
178 udg = h(2) - h(1); |
|
179 c(2:n - 1,:) = spdiags([[ldg(:);0],dg,[0;udg(:)]],[-1,0,1],n-2,n-2) \ g; |
|
180 |
|
181 else # n == 3 |
|
182 |
|
183 dg= [h(1) + 2 * h(2)]; |
|
184 c(2:n - 1,:) = g/dg(1); |
|
185 |
|
186 endif |
|
187 |
|
188 c(1,:) = c(2,:) + h(1) / h(2) * (c(2,:) - c(3,:)); |
|
189 c(n,:) = c(n - 1,:) + h(n - 1) / h(n - 2) * (c(n - 1,:) - c(n - 2,:)); |
|
190 b = diff (a) ./ h(1:n - 1, idx)\ |
|
191 - h(1:n - 1, idx) / 3 .* (c(2:n,:) + 2 * c(1:n - 1,:)); |
|
192 d = diff (c) ./ (3 * h(1:n - 1, idx)); |
|
193 |
|
194 endif |
|
195 |
|
196 d = d(1:n-1,:); c=c(1:n-1,:); b=b(1:n-1,:); a=a(1:n-1,:); |
|
197 coeffs = [d(:), c(:), b(:), a(:)]; |
|
198 ret = mkpp (x, coeffs, szy(1:end-1)); |
|
199 |
|
200 if (nargin == 3) |
|
201 ret = ppval (ret, xi); |
|
202 endif |
|
203 |
|
204 endfunction |
|
205 |
|
206 %!demo |
|
207 %! x = 0:10; y = sin(x); |
|
208 %! xspline = 0:0.1:10; yspline = spline(x,y,xspline); |
|
209 %! title("spline fit to points from sin(x)"); |
|
210 %! plot(xspline,sin(xspline),";original;",... |
|
211 %! xspline,yspline,"-;interpolation;",... |
|
212 %! x,y,"+;interpolation points;"); |
|
213 %! %-------------------------------------------------------- |
|
214 %! % confirm that interpolated function matches the original |
|
215 |
|
216 %!shared x,y |
|
217 %! x = [0:10]; y = sin(x); |
|
218 %!assert (spline(x,y,x), y); |
|
219 %!assert (spline(x,y,x'), y'); |
|
220 %!assert (spline(x',y',x'), y'); |
|
221 %!assert (spline(x',y',x), y); |
|
222 %!assert (isempty(spline(x',y',[]))); |
|
223 %!assert (isempty(spline(x,y,[]))); |
|
224 %!assert (spline(x,[y;y],x), [spline(x,y,x);spline(x,y,x)]) |