Mercurial > hg > octave-nkf
annotate scripts/polynomial/spline.m @ 10793:be55736a0783
Grammarcheck the documentation from m-files.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 18 Jul 2010 20:35:16 -0700 |
parents | c66a4657d764 |
children | d17cb8a1271d |
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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10658
diff
changeset
|
21 ## @deftypefn {Function File} {@var{pp} =} spline (@var{x}, @var{y}) |
7650 | 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 |
10658
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
96 |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
97 for k = (1:columns (a))(any (isnan (a))) |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
98 ok = ! isnan (a(:,k)); |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
99 a(!ok,k) = spline (x(ok), a(ok,k), x(!ok)); |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
100 endfor |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
101 |
5824 | 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(:)]], |
10549 | 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 | |
10549 | 138 - c(2,:) * h(1)) / (2 * h(1)); |
5838 | 139 c(n,:) = - (3 / h(n-1) * (a(n,:) - a(n-1,:)) - 3 * dfe |
10549 | 140 + c(n-1,:) * h(n-1)) / (2 * h(n-1)); |
5838 | 141 b(1:n-1,:) = diff (a) ./ h(1:n-1, idx) ... |
10549 | 142 - h(1:n-1,idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:)); |
5838 | 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)) ... | |
10549 | 149 * (a(3,:) - a(2,:) - h(2) / h(1) * (a(2,:) - a(1,:))); |
5838 | 150 g(n-2,:) = 3 / (h(n-1) + h(n-2)) ... |
10549 | 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(:)]], | |
10549 | 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(:)]], |
10549 | 174 [-1, 0, 1], n-2, n-2) \ g; |
5824 | 175 |
176 else # n == 3 | |
10549 | 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) ... | |
10549 | 186 - h(1:n-1, idx) / 3 .* (c(2:n,:) + 2 * c(1:n-1,:)); |
5838 | 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,:); | |
9768
31900e17b5f5
improve Matlab compatibility & performance of ppval/mkpp and some associated funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
9211
diff
changeset
|
195 coeffs = cat (3, d.', c.', b.', a.'); |
5824 | 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); | |
10658
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
232 %!test |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
233 %! xnan = 5; |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
234 %! y(x==xnan) = NaN; |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
235 %! ok = ! isnan (y); |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
236 %! assert (spline (x, y, x(ok)), y(ok), abserr); |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
237 %!test |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
238 %! ok = ! isnan (y); |
c66a4657d764
spline.m: Ignore NaNs within input vectors.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
239 %! assert (! isnan (spline (x, y, x(!ok)))); |