3092
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2303
|
19 |
3368
|
20 ## -*- texinfo -*- |
4491
|
21 ## @deftypefn {Function File} {[@var{p}, @var{s}] =} polyfit (@var{x}, @var{y}, @var{n}) |
3368
|
22 ## Return the coefficients of a polynomial @var{p}(@var{x}) of degree |
3426
|
23 ## @var{n} that minimizes |
3368
|
24 ## @iftex |
|
25 ## @tex |
|
26 ## $$ |
|
27 ## \sum_{i=1}^N (p(x_i) - y_i)^2 |
|
28 ## $$ |
|
29 ## @end tex |
|
30 ## @end iftex |
|
31 ## @ifinfo |
|
32 ## @code{sumsq (p(x(i)) - y(i))}, |
|
33 ## @end ifinfo |
|
34 ## to best fit the data in the least squares sense. |
3418
|
35 ## |
4491
|
36 ## The polynomial coefficients are returned in a row vector. |
|
37 ## |
|
38 ## If two output arguments are requested, the second is a structure |
|
39 ## containing the following fields: |
3426
|
40 ## |
4491
|
41 ## @table @code |
|
42 ## @item R |
|
43 ## The Cholesky factor of the Vandermonde matrix used to compute the |
|
44 ## polynomial coefficients. |
|
45 ## @item X |
|
46 ## The Vandermonde matrix used to compute the polynomial coefficients. |
|
47 ## @item df |
|
48 ## The degrees of freedom. |
|
49 ## @item normr |
|
50 ## The norm of the residuals. |
|
51 ## @item yf |
|
52 ## The values of the polynomial for each value of @var{x}. |
|
53 ## @end table |
3368
|
54 ## @end deftypefn |
2311
|
55 |
5428
|
56 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312
|
57 ## Created: 13 December 1994 |
|
58 ## Adapted-By: jwe |
|
59 |
4491
|
60 function [p, s, mu] = polyfit (x, y, n) |
2325
|
61 |
|
62 |
2261
|
63 if (nargin != 3) |
6046
|
64 print_usage (); |
2261
|
65 endif |
2325
|
66 |
4030
|
67 if (! (isvector (x) && isvector (y) && size (x) == size (y))) |
2261
|
68 error ("polyfit: x and y must be vectors of the same size"); |
|
69 endif |
2325
|
70 |
4030
|
71 if (! (isscalar (n) && n >= 0 && ! isinf (n) && n == round (n))) |
2261
|
72 error ("polyfit: n must be a nonnegative integer"); |
|
73 endif |
2325
|
74 |
3091
|
75 y_is_row_vector = (rows (y) == 1); |
|
76 |
2261
|
77 l = length (x); |
|
78 x = reshape (x, l, 1); |
|
79 y = reshape (y, l, 1); |
2325
|
80 |
4491
|
81 X = (x * ones (1, n+1)) .^ (ones (l, 1) * (n : -1 : 0)); |
2261
|
82 |
3190
|
83 p = X \ y; |
3091
|
84 |
4491
|
85 if (nargout > 1) |
|
86 |
|
87 yf = X*p; |
3105
|
88 |
|
89 if (y_is_row_vector) |
4491
|
90 s.yf = yf.'; |
|
91 else |
|
92 s.yf = yf; |
3105
|
93 endif |
3091
|
94 |
4491
|
95 [s.R, dummy] = chol (X'*X); |
|
96 s.X = X; |
|
97 s.df = l - n - 1; |
|
98 s.normr = norm (yf - y); |
2261
|
99 |
|
100 endif |
|
101 |
5395
|
102 ## Return value should be a row vector. |
|
103 |
|
104 p = p.'; |
|
105 |
2261
|
106 endfunction |