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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
2303
|
19 |
3368
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{p}, @var{yf}] =} polyfit (@var{x}, @var{y}, @var{n}) |
|
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 ## |
|
36 ## The polynomial coefficients are returned in a row vector if @var{x} |
|
37 ## and @var{y} are both row vectors; otherwise, they are returned in a |
|
38 ## column vector. |
3426
|
39 ## |
3368
|
40 ## If two output arguments are requested, the second contains the values of |
|
41 ## the polynomial for each value of @var{x}. |
|
42 ## @end deftypefn |
2311
|
43 |
2312
|
44 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
45 ## Created: 13 December 1994 |
|
46 ## Adapted-By: jwe |
|
47 |
3061
|
48 function [p, yf] = polyfit (x, y, n) |
2325
|
49 |
|
50 |
2261
|
51 if (nargin != 3) |
|
52 usage ("polyfit (x, y, n)"); |
|
53 endif |
2325
|
54 |
2261
|
55 if (! (is_vector (x) && is_vector (y) && size (x) == size (y))) |
|
56 error ("polyfit: x and y must be vectors of the same size"); |
|
57 endif |
2325
|
58 |
2261
|
59 if (! (is_scalar (n) && n >= 0 && ! isinf (n) && n == round (n))) |
|
60 error ("polyfit: n must be a nonnegative integer"); |
|
61 endif |
2325
|
62 |
3091
|
63 y_is_row_vector = (rows (y) == 1); |
|
64 |
2261
|
65 l = length (x); |
|
66 x = reshape (x, l, 1); |
|
67 y = reshape (y, l, 1); |
2325
|
68 |
3061
|
69 X = (x * ones (1, n+1)) .^ (ones (l, 1) * (0 : n)); |
2261
|
70 |
3190
|
71 p = X \ y; |
3091
|
72 |
|
73 if (nargout == 2) |
|
74 yf = X * p; |
3105
|
75 |
|
76 if (y_is_row_vector) |
3246
|
77 yf = yf.'; |
3105
|
78 endif |
3091
|
79 endif |
|
80 |
|
81 p = flipud (p); |
2261
|
82 |
3418
|
83 if (y_is_row_vector && rows (x) == 1) |
|
84 p = p'; |
2261
|
85 endif |
|
86 |
|
87 endfunction |