Mercurial > hg > octave-lyh
comparison scripts/polynomial/polyfit.m @ 2303:5cffc4b8de57
[project @ 1996-06-24 09:15:24 by jwe]
author | jwe |
---|---|
date | Mon, 24 Jun 1996 09:15:24 +0000 |
parents | 1b6e1629fb91 |
children | 2b5788792cad |
comparison
equal
deleted
inserted
replaced
2302:470c856bf55a | 2303:5cffc4b8de57 |
---|---|
1 ### Copyright (C) 1996 John W. Eaton | |
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. | |
19 | |
1 function p = polyfit (x, y, n) | 20 function p = polyfit (x, y, n) |
2 | 21 |
3 # usage: polyfit (x, y, n) | 22 ## usage: polyfit (x, y, n) |
4 # | 23 ## |
5 # Returns the coefficients of a polynomial p(x) of degree n that | 24 ## Returns the coefficients of a polynomial p(x) of degree n that |
6 # minimizes sumsq (p(x(i)) - y(i)), i.e., that best fits the data | 25 ## minimizes sumsq (p(x(i)) - y(i)), i.e., that best fits the data |
7 # in the least squares sense. | 26 ## in the least squares sense. |
8 | 27 |
9 # Written by KH (Kurt.Hornik@ci.tuwien.ac.at) on Dec 13, 1994 | 28 ## Written by KH (Kurt.Hornik@ci.tuwien.ac.at) on Dec 13, 1994 |
10 # Copyright Dept of Statistics and Probability Theory TU Wien | 29 ## Copyright Dept of Statistics and Probability Theory TU Wien |
11 | 30 |
12 if (nargin != 3) | 31 if (nargin != 3) |
13 usage ("polyfit (x, y, n)"); | 32 usage ("polyfit (x, y, n)"); |
14 endif | 33 endif |
15 | 34 |
30 if (n > 0) | 49 if (n > 0) |
31 tmp = (x * ones (1, n)) .^ (ones (l, 1) * (1 : n)); | 50 tmp = (x * ones (1, n)) .^ (ones (l, 1) * (1 : n)); |
32 X = [X, tmp]; | 51 X = [X, tmp]; |
33 endif | 52 endif |
34 | 53 |
35 # Compute polynomial coeffients, making returned value compatible | 54 ## Compute polynomial coeffients, making returned value compatible |
36 # with Matlab. | 55 ## with Matlab. |
37 | 56 |
38 [Q, R] = qr (X, 0); | 57 [Q, R] = qr (X, 0); |
39 | 58 |
40 p = flipud (R \ (Q' * y)); | 59 p = flipud (R \ (Q' * y)); |
41 | 60 |