Mercurial > hg > octave-nkf
annotate scripts/polynomial/spline.m @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | f0c3d3fc4903 |
children | 31900e17b5f5 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2006, 2007, 2008, 2009 Kai Habel |
5824 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5824 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5824 | 19 |
20 ## -*- texinfo -*- | |
7650 | 21 ## @deftypefn {Function File} {@var{pp} =} spline (@var{x}, @var{y}) |
22 ## @deftypefnx {Function File} {@var{yi} =} spline (@var{x}, @var{y}, @var{xi}) | |
5824 | 23 ## |
8602 | 24 ## Return the cubic spline interpolant of @var{y} at points @var{x}. |
25 ## If called with two arguments, @code{spline} returns the piece-wise | |
26 ## polynomial @var{pp} that may later be used with @code{ppval} to | |
27 ## evaluate the polynomial at specific points. | |
28 ## If called with a third input argument, @code{spline} evaluates the | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## spline at the points @var{xi}. There is an equivalence |
8602 | 30 ## between @code{ppval (spline (@var{x}, @var{y}), @var{xi})} and |
31 ## @code{spline (@var{x}, @var{y}, @var{xi})}. | |
5824 | 32 ## |
33 ## The variable @var{x} must be a vector of length @var{n}, and @var{y} | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## can be either a vector or array. In the case where @var{y} is a |
5824 | 35 ## vector, it can have a length of either @var{n} or @code{@var{n} + 2}. |
36 ## If the length of @var{y} is @var{n}, then the 'not-a-knot' end | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
37 ## condition is used. If the length of @var{y} is @code{@var{n} + 2}, |
8602 | 38 ## then the first and last values of the vector @var{y} are the values |
39 ## of the first derivative of the cubic spline at the end-points. | |
5824 | 40 ## |
41 ## If @var{y} is an array, then the size of @var{y} must have the form | |
42 ## @tex | |
43 ## $$[s_1, s_2, \cdots, s_k, n]$$ | |
44 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
45 ## @ifnottex |
5824 | 46 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n}]} |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
47 ## @end ifnottex |
5824 | 48 ## or |
49 ## @tex | |
8828 | 50 ## $$[s_1, s_2, \cdots, s_k, n + 2].$$ |
5824 | 51 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
52 ## @ifnottex |
5824 | 53 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n} + 2]}. |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
54 ## @end ifnottex |
8602 | 55 ## The array is then reshaped internally to a matrix where the leading |
5824 | 56 ## dimension is given by |
57 ## @tex | |
58 ## $$s_1 s_2 \cdots s_k$$ | |
59 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
60 ## @ifnottex |
5824 | 61 ## @code{@var{s1} * @var{s2} * @dots{} * @var{sk}} |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
62 ## @end ifnottex |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
63 ## and each row of this matrix is then treated separately. Note that this |
5824 | 64 ## is exactly the opposite treatment than @code{interp1} and is done |
7001 | 65 ## for compatibility. |
5824 | 66 ## @seealso{ppval, mkpp, unmkpp} |
67 ## @end deftypefn | |
68 | |
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 | |
73 ## splines. | |
74 | |
75 function ret = spline (x, y, xi) | |
76 | |
77 x = x(:); | |
78 n = length (x); | |
79 if (n < 3) | |
80 error ("spline: requires at least 3 points"); | |
81 endif | |
82 | |
83 ## Check the size and shape of y | |
84 ndy = ndims (y); | |
85 szy = size (y); | |
86 if (ndy == 2 && (szy(1) == 1 || szy(2) == 1)) | |
87 if (szy(1) == 1) | |
6014 | 88 a = y.'; |
5824 | 89 else |
90 a = y; | |
91 szy = fliplr (szy); | |
92 endif | |
93 else | |
6014 | 94 a = reshape (y, [prod(szy(1:end-1)), szy(end)]).'; |
5824 | 95 endif |
96 complete = false; | |
97 if (size (a, 1) == n + 2) | |
98 complete = true; | |
99 dfs = a(1,:); | |
100 dfe = a(end,:); | |
101 a = a(2:end-1,:); | |
102 endif | |
103 | |
104 b = c = zeros (size (a)); | |
105 h = diff (x); | |
5838 | 106 idx = ones (columns (a), 1); |
5824 | 107 |
108 if (complete) | |
109 | |
110 if (n == 3) | |
111 dg = 1.5 * h(1) - 0.5 * h(2); | |
5838 | 112 c(2:n-1,:) = 1/dg(1); |
5824 | 113 else |
5838 | 114 dg = 2 * (h(1:n-2) .+ h(2:n-1)); |
5824 | 115 dg(1) = dg(1) - 0.5 * h(1); |
5838 | 116 dg(n-2) = dg(n-2) - 0.5 * h(n-1); |
5824 | 117 |
5838 | 118 e = h(2:n-2); |
5824 | 119 |
5838 | 120 g = 3 * diff (a(2:n,:)) ./ h(2:n-1,idx) ... |
121 - 3 * diff (a(1:n-1,:)) ./ h(1:n-2,idx); | |
122 g(1,:) = 3 * (a(3,:) - a(2,:)) / h(2) ... | |
5824 | 123 - 3 / 2 * (3 * (a(2,:) - a(1,:)) / h(1) - dfs); |
5838 | 124 g(n-2,:) = 3 / 2 * (3 * (a(n,:) - a(n-1,:)) / h(n-1) - dfe) ... |
125 - 3 * (a(n-1,:) - a(n-2,:)) / h(n-2); | |
5824 | 126 |
5838 | 127 c(2:n-1,:) = spdiags ([[e(:); 0], dg, [0; e(:)]], |
128 [-1, 0, 1], n-2, n-2) \ g; | |
5824 | 129 endif |
130 | |
131 c(1,:) = (3 / h(1) * (a(2,:) - a(1,:)) - 3 * dfs | |
5838 | 132 - c(2,:) * h(1)) / (2 * h(1)); |
133 c(n,:) = - (3 / h(n-1) * (a(n,:) - a(n-1,:)) - 3 * dfe | |
134 + c(n-1,:) * h(n-1)) / (2 * h(n-1)); | |
135 b(1:n-1,:) = diff (a) ./ h(1:n-1, idx) ... | |
136 - h(1:n-1,idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:)); | |
137 d = diff (c) ./ (3 * h(1:n-1, idx)); | |
5824 | 138 |
139 else | |
140 | |
5838 | 141 g = zeros (n-2, columns (a)); |
142 g(1,:) = 3 / (h(1) + h(2)) ... | |
143 * (a(3,:) - a(2,:) - h(2) / h(1) * (a(2,:) - a(1,:))); | |
144 g(n-2,:) = 3 / (h(n-1) + h(n-2)) ... | |
145 * (h(n-2) / h(n-1) * (a(n,:) - a(n-1,:)) - (a(n-1,:) - a(n-2,:))); | |
5824 | 146 |
147 if (n > 4) | |
148 | |
5838 | 149 g(2:n - 3,:) = 3 * diff (a(3:n-1,:)) ./ h(3:n-2,idx) ... |
150 - 3 * diff (a(2:n-2,:)) ./ h(2:n - 3,idx); | |
5824 | 151 |
5838 | 152 dg = 2 * (h(1:n-2) .+ h(2:n-1)); |
5824 | 153 dg(1) = dg(1) - h(1); |
5838 | 154 dg(n-2) = dg(n-2) - h(n-1); |
5824 | 155 |
5838 | 156 ldg = udg = h(2:n-2); |
5824 | 157 udg(1) = udg(1) - h(1); |
5838 | 158 ldg(n - 3) = ldg(n-3) - h(n-1); |
159 c(2:n-1,:) = spdiags ([[ldg(:); 0], dg, [0; udg(:)]], | |
160 [-1, 0, 1], n-2, n-2) \ g; | |
5824 | 161 |
162 elseif (n == 4) | |
163 | |
6248 | 164 dg = [h(1) + 2 * h(2); 2 * h(2) + h(3)]; |
5824 | 165 ldg = h(2) - h(3); |
166 udg = h(2) - h(1); | |
5838 | 167 c(2:n-1,:) = spdiags ([[ldg(:);0], dg, [0; udg(:)]], |
168 [-1, 0, 1], n-2, n-2) \ g; | |
5824 | 169 |
170 else # n == 3 | |
171 | |
5838 | 172 dg = h(1) + 2 * h(2); |
173 c(2:n-1,:) = g/dg(1); | |
5824 | 174 |
175 endif | |
176 | |
177 c(1,:) = c(2,:) + h(1) / h(2) * (c(2,:) - c(3,:)); | |
5838 | 178 c(n,:) = c(n-1,:) + h(n-1) / h(n-2) * (c(n-1,:) - c(n-2,:)); |
179 b = diff (a) ./ h(1:n-1, idx) ... | |
180 - h(1:n-1, idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:)); | |
181 d = diff (c) ./ (3 * h(1:n-1, idx)); | |
5824 | 182 |
183 endif | |
184 | |
5838 | 185 d = d(1:n-1,:); |
186 c = c(1:n-1,:); | |
187 b = b(1:n-1,:); | |
188 a = a(1:n-1,:); | |
5824 | 189 coeffs = [d(:), c(:), b(:), a(:)]; |
190 ret = mkpp (x, coeffs, szy(1:end-1)); | |
191 | |
192 if (nargin == 3) | |
193 ret = ppval (ret, xi); | |
194 endif | |
195 | |
196 endfunction | |
197 | |
198 %!demo | |
199 %! x = 0:10; y = sin(x); | |
200 %! xspline = 0:0.1:10; yspline = spline(x,y,xspline); | |
201 %! title("spline fit to points from sin(x)"); | |
6702 | 202 %! plot(xspline,sin(xspline),"r",xspline,yspline,"g-",x,y,"b+"); |
203 %! legend("original","interpolation","interpolation points"); | |
5824 | 204 %! %-------------------------------------------------------- |
205 %! % confirm that interpolated function matches the original | |
206 | |
6686 | 207 %!shared x,y,abserr |
208 %! x = [0:10]; y = sin(x); abserr = 1e-14; | |
209 %!assert (spline(x,y,x), y, abserr); | |
210 %!assert (spline(x,y,x'), y', abserr); | |
211 %!assert (spline(x',y',x'), y', abserr); | |
212 %!assert (spline(x',y',x), y, abserr); | |
5824 | 213 %!assert (isempty(spline(x',y',[]))); |
214 %!assert (isempty(spline(x,y,[]))); | |
6686 | 215 %!assert (spline(x,[y;y],x), [spline(x,y,x);spline(x,y,x)],abserr) |
6014 | 216 %! y = cos(x) + i*sin(x); |
6686 | 217 %!assert (spline(x,y,x), y, abserr) |
218 %!assert (real(spline(x,y,x)), real(y), abserr); | |
219 %!assert (real(spline(x,y,x.')), real(y).', abserr); | |
220 %!assert (real(spline(x.',y.',x.')), real(y).', abserr); | |
221 %!assert (real(spline(x.',y,x)), real(y), abserr); | |
222 %!assert (imag(spline(x,y,x)), imag(y), abserr); | |
223 %!assert (imag(spline(x,y,x.')), imag(y).', abserr); | |
224 %!assert (imag(spline(x.',y.',x.')), imag(y).', abserr); | |
225 %!assert (imag(spline(x.',y,x)), imag(y), abserr); |