Mercurial > hg > octave-lyh
annotate scripts/polynomial/spline.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | f0c3d3fc4903 |
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 ## @iftex | |
43 ## @tex | |
44 ## $$[s_1, s_2, \cdots, s_k, n]$$ | |
45 ## @end tex | |
46 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
47 ## @ifnottex |
5824 | 48 ## @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
|
49 ## @end ifnottex |
5824 | 50 ## or |
51 ## @iftex | |
52 ## @tex | |
8828 | 53 ## $$[s_1, s_2, \cdots, s_k, n + 2].$$ |
5824 | 54 ## @end tex |
55 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
56 ## @ifnottex |
5824 | 57 ## @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
|
58 ## @end ifnottex |
8602 | 59 ## The array is then reshaped internally to a matrix where the leading |
5824 | 60 ## dimension is given by |
61 ## @iftex | |
62 ## @tex | |
63 ## $$s_1 s_2 \cdots s_k$$ | |
64 ## @end tex | |
65 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
8310
diff
changeset
|
66 ## @ifnottex |
5824 | 67 ## @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
|
68 ## @end ifnottex |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
69 ## and each row of this matrix is then treated separately. Note that this |
5824 | 70 ## is exactly the opposite treatment than @code{interp1} and is done |
7001 | 71 ## for compatibility. |
5824 | 72 ## @seealso{ppval, mkpp, unmkpp} |
73 ## @end deftypefn | |
74 | |
75 ## This code is based on csape.m from octave-forge, but has been | |
76 ## modified to use the sparse solver code in octave that itself allows | |
77 ## special casing of tri-diagonal matrices, modified for NDArrays and | |
78 ## for the treatment of vectors y 2 elements longer than x as complete | |
79 ## splines. | |
80 | |
81 function ret = spline (x, y, xi) | |
82 | |
83 x = x(:); | |
84 n = length (x); | |
85 if (n < 3) | |
86 error ("spline: requires at least 3 points"); | |
87 endif | |
88 | |
89 ## Check the size and shape of y | |
90 ndy = ndims (y); | |
91 szy = size (y); | |
92 if (ndy == 2 && (szy(1) == 1 || szy(2) == 1)) | |
93 if (szy(1) == 1) | |
6014 | 94 a = y.'; |
5824 | 95 else |
96 a = y; | |
97 szy = fliplr (szy); | |
98 endif | |
99 else | |
6014 | 100 a = reshape (y, [prod(szy(1:end-1)), szy(end)]).'; |
5824 | 101 endif |
102 complete = false; | |
103 if (size (a, 1) == n + 2) | |
104 complete = true; | |
105 dfs = a(1,:); | |
106 dfe = a(end,:); | |
107 a = a(2:end-1,:); | |
108 endif | |
109 | |
110 b = c = zeros (size (a)); | |
111 h = diff (x); | |
5838 | 112 idx = ones (columns (a), 1); |
5824 | 113 |
114 if (complete) | |
115 | |
116 if (n == 3) | |
117 dg = 1.5 * h(1) - 0.5 * h(2); | |
5838 | 118 c(2:n-1,:) = 1/dg(1); |
5824 | 119 else |
5838 | 120 dg = 2 * (h(1:n-2) .+ h(2:n-1)); |
5824 | 121 dg(1) = dg(1) - 0.5 * h(1); |
5838 | 122 dg(n-2) = dg(n-2) - 0.5 * h(n-1); |
5824 | 123 |
5838 | 124 e = h(2:n-2); |
5824 | 125 |
5838 | 126 g = 3 * diff (a(2:n,:)) ./ h(2:n-1,idx) ... |
127 - 3 * diff (a(1:n-1,:)) ./ h(1:n-2,idx); | |
128 g(1,:) = 3 * (a(3,:) - a(2,:)) / h(2) ... | |
5824 | 129 - 3 / 2 * (3 * (a(2,:) - a(1,:)) / h(1) - dfs); |
5838 | 130 g(n-2,:) = 3 / 2 * (3 * (a(n,:) - a(n-1,:)) / h(n-1) - dfe) ... |
131 - 3 * (a(n-1,:) - a(n-2,:)) / h(n-2); | |
5824 | 132 |
5838 | 133 c(2:n-1,:) = spdiags ([[e(:); 0], dg, [0; e(:)]], |
134 [-1, 0, 1], n-2, n-2) \ g; | |
5824 | 135 endif |
136 | |
137 c(1,:) = (3 / h(1) * (a(2,:) - a(1,:)) - 3 * dfs | |
5838 | 138 - c(2,:) * h(1)) / (2 * h(1)); |
139 c(n,:) = - (3 / h(n-1) * (a(n,:) - a(n-1,:)) - 3 * dfe | |
140 + c(n-1,:) * h(n-1)) / (2 * h(n-1)); | |
141 b(1:n-1,:) = diff (a) ./ h(1:n-1, idx) ... | |
142 - h(1:n-1,idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:)); | |
143 d = diff (c) ./ (3 * h(1:n-1, idx)); | |
5824 | 144 |
145 else | |
146 | |
5838 | 147 g = zeros (n-2, columns (a)); |
148 g(1,:) = 3 / (h(1) + h(2)) ... | |
149 * (a(3,:) - a(2,:) - h(2) / h(1) * (a(2,:) - a(1,:))); | |
150 g(n-2,:) = 3 / (h(n-1) + h(n-2)) ... | |
151 * (h(n-2) / h(n-1) * (a(n,:) - a(n-1,:)) - (a(n-1,:) - a(n-2,:))); | |
5824 | 152 |
153 if (n > 4) | |
154 | |
5838 | 155 g(2:n - 3,:) = 3 * diff (a(3:n-1,:)) ./ h(3:n-2,idx) ... |
156 - 3 * diff (a(2:n-2,:)) ./ h(2:n - 3,idx); | |
5824 | 157 |
5838 | 158 dg = 2 * (h(1:n-2) .+ h(2:n-1)); |
5824 | 159 dg(1) = dg(1) - h(1); |
5838 | 160 dg(n-2) = dg(n-2) - h(n-1); |
5824 | 161 |
5838 | 162 ldg = udg = h(2:n-2); |
5824 | 163 udg(1) = udg(1) - h(1); |
5838 | 164 ldg(n - 3) = ldg(n-3) - h(n-1); |
165 c(2:n-1,:) = spdiags ([[ldg(:); 0], dg, [0; udg(:)]], | |
166 [-1, 0, 1], n-2, n-2) \ g; | |
5824 | 167 |
168 elseif (n == 4) | |
169 | |
6248 | 170 dg = [h(1) + 2 * h(2); 2 * h(2) + h(3)]; |
5824 | 171 ldg = h(2) - h(3); |
172 udg = h(2) - h(1); | |
5838 | 173 c(2:n-1,:) = spdiags ([[ldg(:);0], dg, [0; udg(:)]], |
174 [-1, 0, 1], n-2, n-2) \ g; | |
5824 | 175 |
176 else # n == 3 | |
177 | |
5838 | 178 dg = h(1) + 2 * h(2); |
179 c(2:n-1,:) = g/dg(1); | |
5824 | 180 |
181 endif | |
182 | |
183 c(1,:) = c(2,:) + h(1) / h(2) * (c(2,:) - c(3,:)); | |
5838 | 184 c(n,:) = c(n-1,:) + h(n-1) / h(n-2) * (c(n-1,:) - c(n-2,:)); |
185 b = diff (a) ./ h(1:n-1, idx) ... | |
186 - h(1:n-1, idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:)); | |
187 d = diff (c) ./ (3 * h(1:n-1, idx)); | |
5824 | 188 |
189 endif | |
190 | |
5838 | 191 d = d(1:n-1,:); |
192 c = c(1:n-1,:); | |
193 b = b(1:n-1,:); | |
194 a = a(1:n-1,:); | |
5824 | 195 coeffs = [d(:), c(:), b(:), a(:)]; |
196 ret = mkpp (x, coeffs, szy(1:end-1)); | |
197 | |
198 if (nargin == 3) | |
199 ret = ppval (ret, xi); | |
200 endif | |
201 | |
202 endfunction | |
203 | |
204 %!demo | |
205 %! x = 0:10; y = sin(x); | |
206 %! xspline = 0:0.1:10; yspline = spline(x,y,xspline); | |
207 %! title("spline fit to points from sin(x)"); | |
6702 | 208 %! plot(xspline,sin(xspline),"r",xspline,yspline,"g-",x,y,"b+"); |
209 %! legend("original","interpolation","interpolation points"); | |
5824 | 210 %! %-------------------------------------------------------- |
211 %! % confirm that interpolated function matches the original | |
212 | |
6686 | 213 %!shared x,y,abserr |
214 %! x = [0:10]; y = sin(x); abserr = 1e-14; | |
215 %!assert (spline(x,y,x), y, abserr); | |
216 %!assert (spline(x,y,x'), y', abserr); | |
217 %!assert (spline(x',y',x'), y', abserr); | |
218 %!assert (spline(x',y',x), y, abserr); | |
5824 | 219 %!assert (isempty(spline(x',y',[]))); |
220 %!assert (isempty(spline(x,y,[]))); | |
6686 | 221 %!assert (spline(x,[y;y],x), [spline(x,y,x);spline(x,y,x)],abserr) |
6014 | 222 %! y = cos(x) + i*sin(x); |
6686 | 223 %!assert (spline(x,y,x), y, abserr) |
224 %!assert (real(spline(x,y,x)), real(y), abserr); | |
225 %!assert (real(spline(x,y,x.')), real(y).', abserr); | |
226 %!assert (real(spline(x.',y.',x.')), real(y).', abserr); | |
227 %!assert (real(spline(x.',y,x)), real(y), abserr); | |
228 %!assert (imag(spline(x,y,x)), imag(y), abserr); | |
229 %!assert (imag(spline(x,y,x.')), imag(y).', abserr); | |
230 %!assert (imag(spline(x.',y.',x.')), imag(y).', abserr); | |
231 %!assert (imag(spline(x.',y,x)), imag(y), abserr); |