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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2303
|
18 |
3368
|
19 ## -*- texinfo -*- |
4491
|
20 ## @deftypefn {Function File} {[@var{p}, @var{s}] =} polyfit (@var{x}, @var{y}, @var{n}) |
3368
|
21 ## Return the coefficients of a polynomial @var{p}(@var{x}) of degree |
3426
|
22 ## @var{n} that minimizes |
3368
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $$ |
|
26 ## \sum_{i=1}^N (p(x_i) - y_i)^2 |
|
27 ## $$ |
|
28 ## @end tex |
|
29 ## @end iftex |
|
30 ## @ifinfo |
|
31 ## @code{sumsq (p(x(i)) - y(i))}, |
|
32 ## @end ifinfo |
|
33 ## to best fit the data in the least squares sense. |
3418
|
34 ## |
4491
|
35 ## The polynomial coefficients are returned in a row vector. |
|
36 ## |
|
37 ## If two output arguments are requested, the second is a structure |
|
38 ## containing the following fields: |
3426
|
39 ## |
4491
|
40 ## @table @code |
|
41 ## @item R |
|
42 ## The Cholesky factor of the Vandermonde matrix used to compute the |
|
43 ## polynomial coefficients. |
|
44 ## @item X |
|
45 ## The Vandermonde matrix used to compute the polynomial coefficients. |
|
46 ## @item df |
|
47 ## The degrees of freedom. |
|
48 ## @item normr |
|
49 ## The norm of the residuals. |
|
50 ## @item yf |
|
51 ## The values of the polynomial for each value of @var{x}. |
|
52 ## @end table |
3368
|
53 ## @end deftypefn |
2311
|
54 |
5428
|
55 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312
|
56 ## Created: 13 December 1994 |
|
57 ## Adapted-By: jwe |
|
58 |
4491
|
59 function [p, s, mu] = polyfit (x, y, n) |
2325
|
60 |
|
61 |
2261
|
62 if (nargin != 3) |
6046
|
63 print_usage (); |
2261
|
64 endif |
2325
|
65 |
6157
|
66 if (! (isvector (x) && isvector (y) && size_equal (x, y))) |
2261
|
67 error ("polyfit: x and y must be vectors of the same size"); |
|
68 endif |
2325
|
69 |
4030
|
70 if (! (isscalar (n) && n >= 0 && ! isinf (n) && n == round (n))) |
2261
|
71 error ("polyfit: n must be a nonnegative integer"); |
|
72 endif |
2325
|
73 |
3091
|
74 y_is_row_vector = (rows (y) == 1); |
|
75 |
2261
|
76 l = length (x); |
|
77 x = reshape (x, l, 1); |
|
78 y = reshape (y, l, 1); |
2325
|
79 |
4491
|
80 X = (x * ones (1, n+1)) .^ (ones (l, 1) * (n : -1 : 0)); |
2261
|
81 |
3190
|
82 p = X \ y; |
3091
|
83 |
4491
|
84 if (nargout > 1) |
|
85 |
|
86 yf = X*p; |
3105
|
87 |
|
88 if (y_is_row_vector) |
4491
|
89 s.yf = yf.'; |
|
90 else |
|
91 s.yf = yf; |
3105
|
92 endif |
3091
|
93 |
4491
|
94 [s.R, dummy] = chol (X'*X); |
|
95 s.X = X; |
|
96 s.df = l - n - 1; |
|
97 s.normr = norm (yf - y); |
2261
|
98 |
|
99 endif |
|
100 |
5395
|
101 ## Return value should be a row vector. |
|
102 |
|
103 p = p.'; |
|
104 |
2261
|
105 endfunction |