Mercurial > hg > octave-nkf
comparison scripts/polynomial/spline.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | 702dbd0c53f5 |
children | 07e102029d2a |
comparison
equal
deleted
inserted
replaced
11586:12df7854fa7c | 11587:c792872f8942 |
---|---|
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {@var{pp} =} spline (@var{x}, @var{y}) | 21 ## @deftypefn {Function File} {@var{pp} =} spline (@var{x}, @var{y}) |
22 ## @deftypefnx {Function File} {@var{yi} =} spline (@var{x}, @var{y}, @var{xi}) | 22 ## @deftypefnx {Function File} {@var{yi} =} spline (@var{x}, @var{y}, @var{xi}) |
23 ## | 23 ## |
24 ## Return the cubic spline interpolant of @var{y} at points @var{x}. | 24 ## Return the cubic spline interpolant of @var{y} at points @var{x}. |
25 ## If called with two arguments, @code{spline} returns the piecewise | 25 ## If called with two arguments, @code{spline} returns the piecewise |
26 ## polynomial @var{pp} that may later be used with @code{ppval} to | 26 ## polynomial @var{pp} that may later be used with @code{ppval} to |
27 ## evaluate the polynomial at specific points. | 27 ## evaluate the polynomial at specific points. |
28 ## If called with a third input argument, @code{spline} evaluates the | 28 ## If called with a third input argument, @code{spline} evaluates the |
29 ## spline at the points @var{xi}. There is an equivalence | 29 ## spline at the points @var{xi}. There is an equivalence |
30 ## between @code{ppval (spline (@var{x}, @var{y}), @var{xi})} and | 30 ## between @code{ppval (spline (@var{x}, @var{y}), @var{xi})} and |
31 ## @code{spline (@var{x}, @var{y}, @var{xi})}. | 31 ## @code{spline (@var{x}, @var{y}, @var{xi})}. |
32 ## | 32 ## |
33 ## The variable @var{x} must be a vector of length @var{n}, and @var{y} | 33 ## The variable @var{x} must be a vector of length @var{n}, and @var{y} |
51 ## @end tex | 51 ## @end tex |
52 ## @ifnottex | 52 ## @ifnottex |
53 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n} + 2]}. | 53 ## @code{[@var{s1}, @var{s2}, @dots{}, @var{sk}, @var{n} + 2]}. |
54 ## @end ifnottex | 54 ## @end ifnottex |
55 ## The array is then reshaped internally to a matrix where the leading | 55 ## The array is then reshaped internally to a matrix where the leading |
56 ## dimension is given by | 56 ## dimension is given by |
57 ## @tex | 57 ## @tex |
58 ## $$s_1 s_2 \cdots s_k$$ | 58 ## $$s_1 s_2 \cdots s_k$$ |
59 ## @end tex | 59 ## @end tex |
60 ## @ifnottex | 60 ## @ifnottex |
61 ## @code{@var{s1} * @var{s2} * @dots{} * @var{sk}} | 61 ## @code{@var{s1} * @var{s2} * @dots{} * @var{sk}} |
75 function ret = spline (x, y, xi) | 75 function ret = spline (x, y, xi) |
76 | 76 |
77 x = x(:); | 77 x = x(:); |
78 n = length (x); | 78 n = length (x); |
79 if (n < 3) | 79 if (n < 3) |
80 error ("spline: requires at least 3 points"); | 80 error ("spline: requires at least 3 points"); |
81 endif | 81 endif |
82 | 82 |
83 ## Check the size and shape of y | 83 ## Check the size and shape of y |
84 ndy = ndims (y); | 84 ndy = ndims (y); |
85 szy = size (y); | 85 szy = size (y); |
91 szy = fliplr (szy); | 91 szy = fliplr (szy); |
92 endif | 92 endif |
93 else | 93 else |
94 a = reshape (y, [prod(szy(1:end-1)), szy(end)]).'; | 94 a = reshape (y, [prod(szy(1:end-1)), szy(end)]).'; |
95 endif | 95 endif |
96 | 96 |
97 for k = (1:columns (a))(any (isnan (a))) | 97 for k = (1:columns (a))(any (isnan (a))) |
98 ok = ! isnan (a(:,k)); | 98 ok = ! isnan (a(:,k)); |
99 a(!ok,k) = spline (x(ok), a(ok,k), x(!ok)); | 99 a(!ok,k) = spline (x(ok), a(ok,k), x(!ok)); |
100 endfor | 100 endfor |
101 | 101 |
102 complete = false; | 102 complete = false; |
103 if (size (a, 1) == n + 2) | 103 if (size (a, 1) == n + 2) |
104 complete = true; | 104 complete = true; |
105 dfs = a(1,:); | 105 dfs = a(1,:); |
106 dfe = a(end,:); | 106 dfe = a(end,:); |