annotate scripts/polynomial/polyfit.m @ 15063:36cbcc37fdb8

Refactor configure.ac to make it more understandable. Use common syntax for messages in config.h Correct typos, refer to libraries in all caps, use two spaces after period. Follow Autoconf guidelines and place general tests before specific tests. * configure.ac, m4/acinclude.m4: Use common syntax for messages in config.h Correct typos, refer to libraries in all caps, use two spaces after period. Follow Autoconf guidelines and place general tests before specific tests.
author Rik <rik@octave.org>
date Tue, 31 Jul 2012 10:28:51 -0700
parents cd375519eab0
children 7babcdb9bc13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 14110
diff changeset
1 ## Copyright (C) 1996-2012 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6157
diff changeset
17 ## <http://www.gnu.org/licenses/>.
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 2261
diff changeset
18
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3246
diff changeset
19 ## -*- texinfo -*-
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9290
diff changeset
20 ## @deftypefn {Function File} {@var{p} =} polyfit (@var{x}, @var{y}, @var{n})
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9290
diff changeset
21 ## @deftypefnx {Function File} {[@var{p}, @var{s}] =} polyfit (@var{x}, @var{y}, @var{n})
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9290
diff changeset
22 ## @deftypefnx {Function File} {[@var{p}, @var{s}, @var{mu}] =} polyfit (@var{x}, @var{y}, @var{n})
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3246
diff changeset
23 ## Return the coefficients of a polynomial @var{p}(@var{x}) of degree
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
24 ## @var{n} that minimizes the least-squares-error of the fit to the points
14620
cd375519eab0 doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents: 14401
diff changeset
25 ## @code{[@var{x}, @var{y}]}. If @var{n} is a logical vector, it is used
14401
2bce551c85b6 doc: Correct typo in polyfit docstring.
Rik <octave@nomad.inbox5.com>
parents: 14377
diff changeset
26 ## as a mask to selectively force the corresponding polynomial
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
27 ## coefficients to be used or ignored.
3418
ca92c9d3f882 [project @ 2000-01-12 03:07:47 by jwe]
jwe
parents: 3368
diff changeset
28 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
29 ## The polynomial coefficients are returned in a row vector.
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
30 ##
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
31 ## The optional output @var{s} is a structure containing the following fields:
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3418
diff changeset
32 ##
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
33 ## @table @samp
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
34 ## @item R
11593
1577c6f80926 Use non-breaking spaces between certain adjectives and their nouns in docstrings.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
35 ## Triangular factor R from the QR@tie{}decomposition.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
36 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
37 ## @item X
7501
83cce070104f style fixes
John W. Eaton <jwe@octave.org>
parents: 7500
diff changeset
38 ## The Vandermonde matrix used to compute the polynomial coefficients.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
39 ##
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
40 ## @item C
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
41 ## The unscaled covariance matrix, formally equal to the inverse of
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
42 ## @var{x'}*@var{x}, but computed in a way minimizing roundoff error
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
43 ## propagation.
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
44 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
45 ## @item df
7501
83cce070104f style fixes
John W. Eaton <jwe@octave.org>
parents: 7500
diff changeset
46 ## The degrees of freedom.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
47 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
48 ## @item normr
7501
83cce070104f style fixes
John W. Eaton <jwe@octave.org>
parents: 7500
diff changeset
49 ## The norm of the residuals.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
50 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
51 ## @item yf
7501
83cce070104f style fixes
John W. Eaton <jwe@octave.org>
parents: 7500
diff changeset
52 ## The values of the polynomial for each value of @var{x}.
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
53 ## @end table
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
54 ##
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
55 ## The second output may be used by @code{polyval} to calculate the
14620
cd375519eab0 doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents: 14401
diff changeset
56 ## statistical error limits of the predicted values. In particular, the
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
57 ## standard deviation of @var{p} coefficients is given by @*
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
58 ## @code{sqrt (diag (s.C)/s.df)*s.normr}.
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
59 ##
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
60 ## When the third output, @var{mu}, is present the
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
61 ## coefficients, @var{p}, are associated with a polynomial in
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
62 ## @var{xhat} = (@var{x}-@var{mu}(1))/@var{mu}(2).
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
63 ## Where @var{mu}(1) = mean (@var{x}), and @var{mu}(2) = std (@var{x}).
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
64 ## This linear transformation of @var{x} improves the numerical
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
65 ## stability of the fit.
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
66 ## @seealso{polyval, polyaffine, roots, vander, zscore}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3246
diff changeset
67 ## @end deftypefn
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
68
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5395
diff changeset
69 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
70 ## Created: 13 December 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
71 ## Adapted-By: jwe
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
72 ## Modified on 20120204 by P. Dupuis; added the ability to specify a
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
73 ## polynomial mask instead of a polynomial degree.
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
74
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
75 function [p, s, mu] = polyfit (x, y, n)
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
76
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
77 if (nargin < 3 || nargin > 4)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
78 print_usage ();
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
79 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
80
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
81 if (nargout > 2)
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
82 ## Normalized the x values.
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
83 mu = [mean(x), std(x)];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
84 x = (x - mu(1)) / mu(2);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
85 endif
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
86
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
87 if (! size_equal (x, y))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
88 error ("polyfit: X and Y must be vectors of the same size");
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
89 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
90
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
91 if (islogical (n))
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
92 polymask = n;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
93 ## n is the polynomial degree as given the polymask size; m is the
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
94 ## effective number of used coefficients.
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
95 n = length (polymask) - 1; m = sum (polymask) - 1;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
96 else
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
97 if (! (isscalar (n) && n >= 0 && ! isinf (n) && n == fix (n)))
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
98 error ("polyfit: N must be a non-negative integer");
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
99 endif
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
100 polymask = logical (ones (1, n+1)); m = n;
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
101 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
102
14110
b8d9530e940e undo non-documentation changes in changeset 614505385171
John W. Eaton <jwe@octave.org>
parents: 14104
diff changeset
103 y_is_row_vector = (rows (y) == 1);
b8d9530e940e undo non-documentation changes in changeset 614505385171
John W. Eaton <jwe@octave.org>
parents: 14104
diff changeset
104
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
105 ## Reshape x & y into column vectors.
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
106 l = numel (x);
9138
436b498b0506 simplify polyfit
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
107 x = x(:);
436b498b0506 simplify polyfit
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
108 y = y(:);
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
109
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
110 ## Construct the Vandermonde matrix.
9138
436b498b0506 simplify polyfit
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
111 v = vander (x, n+1);
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
112
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
113 ## Solve by QR decomposition.
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
114 [q, r, k] = qr (v(:, polymask), 0);
9138
436b498b0506 simplify polyfit
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
115 p = r \ (q' * y);
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
116 p(k) = p;
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
117
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
118 if (n ~= m)
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
119 q = p; p = zeros (n+1, 1);
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
120 p(polymask) = q;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
121 endif
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
122
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
123 if (nargout > 1)
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
124 yf = v*p;
3105
f936c7f5074f [project @ 1997-11-19 04:36:17 by jwe]
jwe
parents: 3092
diff changeset
125
14110
b8d9530e940e undo non-documentation changes in changeset 614505385171
John W. Eaton <jwe@octave.org>
parents: 14104
diff changeset
126 if (y_is_row_vector)
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
127 s.yf = yf.';
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
128 else
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
129 s.yf = yf;
3105
f936c7f5074f [project @ 1997-11-19 04:36:17 by jwe]
jwe
parents: 3092
diff changeset
130 endif
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
131 s.X = v;
3091
b06dcbb6b3b1 [project @ 1997-10-10 16:26:47 by jwe]
jwe
parents: 3069
diff changeset
132
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
133 ## r.'*r is positive definite if X(:, polymask) is of full rank.
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
134 ## Invert it by cholinv to avoid taking the square root of squared
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
135 ## quantities. If cholinv fails, then X(:, polymask) is rank
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
136 ## deficient and not invertible.
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
137 try
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
138 C = cholinv (r.'*r)(k, k);
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
139 catch
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
140 C = NaN * ones (m+1, m+1);
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
141 end_try_catch
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
142
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
143 if (n ~= m)
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
144 ## fill matrices if required
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
145 s.X(:, ~polymask) = 0;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
146 s.R = zeros (n+1, n+1); s.R(polymask, polymask) = r;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
147 s.C = zeros (n+1, n+1); s.C(polymask, polymask) = C;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
148 else
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
149 s.R = r;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
150 s.C = C;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
151 endif
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
152 s.df = l - m - 1;
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
153 s.normr = norm (yf - y);
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
154 endif
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
155
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
156 ## Return a row vector.
5395
8db4eb48f546 [project @ 2005-06-16 20:19:18 by jwe]
jwe
parents: 5307
diff changeset
157 p = p.';
8db4eb48f546 [project @ 2005-06-16 20:19:18 by jwe]
jwe
parents: 5307
diff changeset
158
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
159 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
160
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
161
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
162 %!shared x
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
163 %! x = [-2, -1, 0, 1, 2];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
164 %!assert (polyfit (x, x.^2+x+1, 2), [1, 1, 1], sqrt (eps))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
165 %!assert (polyfit (x, x.^2+x+1, 3), [0, 1, 1, 1], sqrt (eps))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
166 %!fail ("polyfit (x, x.^2+x+1)")
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
167 %!fail ("polyfit (x, x.^2+x+1, [])")
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
168
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
169 ## Test difficult case where scaling is really needed. This example
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
170 ## demonstrates the rather poor result which occurs when the dependent
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
171 ## variable is not normalized properly.
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
172 ## Also check the usage of 2nd & 3rd output arguments.
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
173 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
174 %! x = [ -1196.4, -1195.2, -1194, -1192.8, -1191.6, -1190.4, -1189.2, -1188, \
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
175 %! -1186.8, -1185.6, -1184.4, -1183.2, -1182];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
176 %! y = [ 315571.7086, 315575.9618, 315579.4195, 315582.6206, 315585.4966, \
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
177 %! 315588.3172, 315590.9326, 315593.5934, 315596.0455, 315598.4201, \
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
178 %! 315600.7143, 315602.9508, 315605.1765 ];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
179 %! [p1, s1] = polyfit (x, y, 10);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
180 %! [p2, s2, mu] = polyfit (x, y, 10);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
181 %! assert (s2.normr < s1.normr);
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
182
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
183 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
184 %! x = 1:4;
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
185 %! p0 = [1i, 0, 2i, 4];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
186 %! y0 = polyval (p0, x);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
187 %! p = polyfit (x, y0, numel (p0) - 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
188 %! assert (p, p0, 1000*eps);
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
189
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
190 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
191 %! x = 1000 + (-5:5);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
192 %! xn = (x - mean (x)) / std (x);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
193 %! pn = ones (1,5);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
194 %! y = polyval (pn, xn);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
195 %! [p, s, mu] = polyfit (x, y, numel (pn) - 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
196 %! [p2, s2] = polyfit (x, y, numel (pn) - 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
197 %! assert (p, pn, s.normr);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
198 %! assert (s.yf, y, s.normr);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
199 %! assert (mu, [mean(x), std(x)]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
200 %! assert (s.normr/s2.normr < sqrt (eps));
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
201
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
202 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
203 %! x = [1, 2, 3; 4, 5, 6];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
204 %! y = [0, 0, 1; 1, 0, 0];
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
205 %! p = polyfit (x, y, 5);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
206 %! expected = [0, 1, -14, 65, -112, 60] / 12;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
207 %! assert (p, expected, sqrt (eps));
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
208
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
209 %!error <vectors of the same size> polyfit ([1, 2; 3, 4], [1, 2, 3, 4], 2)
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
210