annotate scripts/polynomial/polyfit.m @ 30564:796f54d4ddbf stable

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2021. In all .txi and .texi files except gpl.txi and gpl.texi in the doc/liboctave and doc/interpreter directories, change the copyright to "Octave Project Developers", the same as used for other source files. Update copyright notices for 2022 (not done since 2019). For gpl.txi and gpl.texi, change the copyright notice to be "Free Software Foundation, Inc." and leave the date at 2007 only because this file only contains the text of the GPL, not anything created by the Octave Project Developers. Add Paul Thomas to contributors.in.
author John W. Eaton <jwe@octave.org>
date Tue, 28 Dec 2021 18:22:40 -0500
parents 7854d5752dd2
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 1996-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
8 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
2313
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 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
19 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
20 ## 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
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 2261
diff changeset
25
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3246
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20165
diff changeset
27 ## @deftypefn {} {@var{p} =} polyfit (@var{x}, @var{y}, @var{n})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20165
diff changeset
28 ## @deftypefnx {} {[@var{p}, @var{s}] =} polyfit (@var{x}, @var{y}, @var{n})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20165
diff changeset
29 ## @deftypefnx {} {[@var{p}, @var{s}, @var{mu}] =} polyfit (@var{x}, @var{y}, @var{n})
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
30 ## Return the coefficients of a polynomial @var{p}(@var{x}) of degree @var{n}
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
31 ## that minimizes the least-squares-error of the fit to the points
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
32 ## @code{[@var{x}(:), @var{y}(:)]}.
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
33 ##
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
34 ## @var{n} is typically an integer @geq{} 0 specifying the degree of the
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
35 ## approximating polynomial. If @var{n} is a logical vector, it is used as a
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
36 ## mask to selectively force the corresponding polynomial coefficients to be
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
37 ## used or ignored.
3418
ca92c9d3f882 [project @ 2000-01-12 03:07:47 by jwe]
jwe
parents: 3368
diff changeset
38 ##
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
39 ## The polynomial coefficients are returned in the row vector @var{p}. The
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
40 ## output @var{p} may be directly used with @code{polyval} to estimate values
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
41 ## using the fitted polynomial.
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
42 ##
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
43 ## 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
44 ##
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
45 ## @table @samp
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
46 ##
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
47 ## @item yf
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
48 ## The values of the polynomial for each value of @var{x}.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
49 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
50 ## @item X
25003
2365c2661b3c doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24534
diff changeset
51 ## The @nospell{Vandermonde} matrix used to compute the polynomial
2365c2661b3c doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24534
diff changeset
52 ## coefficients.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
53 ##
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
54 ## @item R
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
55 ## Triangular factor R from the QR@tie{}decomposition.
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
56 ##
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
57 ## @item C
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
58 ## 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
59 ## @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
60 ## propagation.
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
61 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
62 ## @item df
7501
83cce070104f style fixes
John W. Eaton <jwe@octave.org>
parents: 7500
diff changeset
63 ## The degrees of freedom.
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
64 ##
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
65 ## @item normr
7501
83cce070104f style fixes
John W. Eaton <jwe@octave.org>
parents: 7500
diff changeset
66 ## The norm of the residuals.
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
67 ## @end table
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
68 ##
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
69 ## The second output may be used by @code{polyval} to calculate the statistical
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
70 ## error limits of the predicted values. In particular, the standard deviation
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
71 ## of @var{p} coefficients is given by
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
72 ##
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
73 ## @code{sqrt (diag (@var{s.C})/@var{s.df}) * @var{s.normr}}.
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
74 ##
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
75 ## When the third output, @var{mu}, is present the original data is centered
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
76 ## and scaled which can improve the numerical stability of the fit. The
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
77 ## coefficients @var{p} are associated with a polynomial in
20165
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
78 ##
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
79 ## @code{@var{xhat} = (@var{x} - @var{mu}(1)) / @var{mu}(2)} @*
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
80 ## where @var{mu}(1) = mean (@var{x}), and @var{mu}(2) = std (@var{x}).
f1d0f506ee78 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
81 ##
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
82 ## Example 1 : logical @var{n} and integer @var{n}
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
83 ##
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
84 ## @example
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
85 ## @group
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
86 ## f = @@(x) x.^2 + 5; # data-generating function
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
87 ## x = 0:5;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
88 ## y = f (x);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
89 ## ## Fit data to polynomial A*x^3 + B*x^1
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
90 ## p = polyfit (x, y, logical ([1, 0, 1, 0]))
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
91 ## @result{} p = [ 0.0680, 0, 4.2444, 0 ]
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
92 ## ## Fit data to polynomial using all terms up to x^3
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
93 ## p = polyfit (x, y, 3)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
94 ## @result{} p = [ -4.9608e-17, 1.0000e+00, -3.3813e-15, 5.0000e+00 ]
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
95 ## @end group
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
96 ## @end example
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
97 ##
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 14062
diff changeset
98 ## @seealso{polyval, polyaffine, roots, vander, zscore}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3246
diff changeset
99 ## @end deftypefn
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
100
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
101 function [p, s, mu] = polyfit (x, y, n)
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
102
28789
28de41192f3c Eliminate unneeded verification of nargin, nargout in m-files.
Rik <rik@octave.org>
parents: 28693
diff changeset
103 if (nargin < 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
104 print_usage ();
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
105 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
106
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
107 y_is_row_vector = isrow (y);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
108
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
109 ## Reshape x & y into column vectors.
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
110 x = x(:);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
111 y = y(:);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
112
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
113 nx = numel (x);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
114 ny = numel (y);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
115 if (nx != ny)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
116 error ("polyfit: X and Y must have the same number of points");
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
117 endif
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
118
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
119 if (nargout > 2)
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
120 ## Center and scale the x values.
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
121 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
122 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
123 endif
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
124
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
125 ## n is the polynomial degree (an input, or deduced from the polymask size)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
126 ## m is the effective number of coefficients.
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
127 if (islogical (n))
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
128 polymask = n(:).'; # force to row vector
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
129 n = numel (polymask) - 1;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
130 m = sum (polymask) - 1;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
131 pad_output = true;
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
132 else
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
133 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
134 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
135 endif
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
136 polymask = true (1, n+1);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
137 m = n;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
138 pad_output = false;
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
139 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
140
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
141 if (m >= nx)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
142 warning ("polyfit: degree of polynomial N is >= number of data points; solution is not unique");
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
143 m = nx;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
144 pad_output = true;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
145 ## Keep the lowest m entries in polymask
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
146 idx = find (polymask);
28693
2bb050267d74 maint: Remove trailing spaces from code base.
Rik <rik@octave.org>
parents: 28502
diff changeset
147 idx((end-m+1):end) = [];
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
148 polymask(idx) = false;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
149 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
150
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
151 ## Construct the Vandermonde matrix.
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
152 X = vander (x, n+1);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
153 v = X(:, polymask);
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
154
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
155 ## Solve by QR decomposition.
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
156 [q, r, k] = qr (v, 0);
9138
436b498b0506 simplify polyfit
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
157 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
158 p(k) = p;
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
159
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
160 if (isargout (2))
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
161 yf = v*p;
14110
b8d9530e940e undo non-documentation changes in changeset 614505385171
John W. Eaton <jwe@octave.org>
parents: 14104
diff changeset
162 if (y_is_row_vector)
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
163 s.yf = yf.';
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
164 else
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
165 s.yf = yf;
3105
f936c7f5074f [project @ 1997-11-19 04:36:17 by jwe]
jwe
parents: 3092
diff changeset
166 endif
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
167
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
168 s.X = X;
3091
b06dcbb6b3b1 [project @ 1997-10-10 16:26:47 by jwe]
jwe
parents: 3069
diff changeset
169
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
170 ## r.'*r is positive definite if matrix v is of full rank. Invert it by
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
171 ## cholinv to avoid taking the square root of squared quantities.
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
172 ## If cholinv fails, then v is rank deficient and not invertible.
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
173 try
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
174 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
175 catch
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
176 C = NaN (m, m);
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
177 end_try_catch
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
178
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
179 if (pad_output)
20955
77f5591878bf maint: Use '! expr' rather than '!expr' to conform to coding guidelines.
Rik <rik@octave.org>
parents: 20852
diff changeset
180 s.X(:, ! polymask) = 0;
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
181 s.R = zeros (rows (r), n+1); s.R(:, polymask) = r;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
182 s.C = zeros (rows (C), n+1); s.C(:, polymask) = C;
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
183 else
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
184 s.R = r;
14377
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
185 s.C = C;
c87f927d047d polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents: 14363
diff changeset
186 endif
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
187
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
188 s.df = max (0, nx - m - 1);
4491
96a25f032846 [project @ 2003-08-28 21:01:21 by jwe]
jwe
parents: 4030
diff changeset
189 s.normr = norm (yf - y);
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
190 endif
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
191
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
192 if (pad_output)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
193 ## Zero pad output
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
194 q = p;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
195 p = zeros (n+1, 1);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
196 p(polymask) = q;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
197 endif
28693
2bb050267d74 maint: Remove trailing spaces from code base.
Rik <rik@octave.org>
parents: 28502
diff changeset
198 p = p.'; # Return a row vector.
5395
8db4eb48f546 [project @ 2005-06-16 20:19:18 by jwe]
jwe
parents: 5307
diff changeset
199
2261
1b6e1629fb91 [project @ 1996-05-23 00:52:07 by jwe]
jwe
parents:
diff changeset
200 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
201
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
202
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
203 %!shared x
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
204 %! x = [-2, -1, 0, 1, 2];
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
205
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
206 %!assert (polyfit (x, 3*x.^2 + 2*x + 1, 2), [3, 2, 1], 10*eps)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
207 %!assert (polyfit (x, 3*x.^2 + 2*x + 1, logical ([1 1 1])), [3, 2, 1], 10*eps)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
208 %!assert (polyfit (x, x.^2 + 2*x + 3, 3), [0, 1, 2, 3], 10*eps)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
209 %!assert (polyfit (x, x.^2 + 2*x + 3, logical ([0 1 1 1])), [0 1 2 3], 10*eps)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
210
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
211 ## Test logical input N
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
212 %!test
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
213 %! x = [0:5];
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
214 %! y = 3*x.^3 + 2*x.^2 + 4;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
215 %! [p, s] = polyfit (x, y, logical ([1, 0, 1, 1]));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
216 %! assert (p(2), 0);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
217 %! assert (all (p([1, 3, 4])));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
218 %! assert (s.df, 3);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
219
21751
b571fc85953f maint: Use two spaces after period to indicate sentence break.
Rik <rik@octave.org>
parents: 20955
diff changeset
220 ## Test difficult case where scaling is really needed. This example
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
221 ## 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
222 ## 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
223 ## 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
224 %!test
19670
e5facc6eec13 Silence warning messages in %!test code.
Rik <rik@octave.org>
parents: 19593
diff changeset
225 %! warning ("off", "Octave:nearly-singular-matrix", "local");
17245
7babcdb9bc13 Use ... instead of \ for line continuation marker.
Stefan Mahr <dac922@gmx.de>
parents: 14620
diff changeset
226 %! x = [ -1196.4, -1195.2, -1194, -1192.8, -1191.6, -1190.4, -1189.2, -1188, ...
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
227 %! -1186.8, -1185.6, -1184.4, -1183.2, -1182];
17245
7babcdb9bc13 Use ... instead of \ for line continuation marker.
Stefan Mahr <dac922@gmx.de>
parents: 14620
diff changeset
228 %! y = [ 315571.7086, 315575.9618, 315579.4195, 315582.6206, 315585.4966, ...
7babcdb9bc13 Use ... instead of \ for line continuation marker.
Stefan Mahr <dac922@gmx.de>
parents: 14620
diff changeset
229 %! 315588.3172, 315590.9326, 315593.5934, 315596.0455, 315598.4201, ...
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
230 %! 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
231 %! [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
232 %! [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
233 %! 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
234
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
235 %!test
19670
e5facc6eec13 Silence warning messages in %!test code.
Rik <rik@octave.org>
parents: 19593
diff changeset
236 %! warning ("off", "Octave:nearly-singular-matrix", "local");
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
237 %! x = 1000 + (-5:5);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
238 %! 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
239 %! pn = ones (1,5);
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
240 %! y = polyval (pn, xn);
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
241 %! n = numel (pn) - 1;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
242 %! [p, s, mu] = polyfit (x, y, n);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
243 %! [p2, s2] = polyfit (x, y, n);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
244 %! 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
245 %! 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
246 %! 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
247 %! 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
248
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
249 ## Complex polynomials
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
250 %!test
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
251 %! x = 1:4;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
252 %! p0 = [1i, 0, 2i, 4];
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
253 %! y = polyval (p0, x);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
254 %! n = numel (p0) - 1;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
255 %! p = polyfit (x, y, n);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
256 %! assert (p, p0, 1000*eps);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
257
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
258 ## Matrix input
7500
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
259 %!test
2df882e69f13 use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents: 7411
diff changeset
260 %! 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
261 %! 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
262 %! 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
263 %! 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
264 %! 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
265
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
266 ## Orientation of output
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
267 %!test
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
268 %! x = 0:5;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
269 %! y = x.^4 + 2*x + 5;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
270 %! [p, s] = polyfit (x, y, 3);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
271 %! assert (isrow (s.yf));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
272 %! [p, s] = polyfit (x, y.', 3);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
273 %! assert (iscolumn (s.yf));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
274
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
275 ## Insufficient data for fit
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
276 %!test
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
277 %! x = [1, 2];
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
278 %! y = [3, 4];
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
279 %! ## Disable warnings entirely because there is not a specific ID to disable.
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
280 %! wstate = warning ();
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
281 %! unwind_protect
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
282 %! warning ("off", "all");
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
283 %! p0 = polyfit (x, y, 4);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
284 %! [p1, s, mu] = polyfit (x, y, 4);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
285 %! unwind_protect_cleanup
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
286 %! warning (wstate);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
287 %! end_unwind_protect
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
288 %! assert (p0, [0, 0, 0, 1, 2], 10*eps);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
289 %! assert (p1, [0, 0, 0, sqrt(2)/2, 3.5], 10*eps);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
290 %! assert (size (s.X), [2, 5]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
291 %! assert (s.X(:,1:3), zeros (2,3));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
292 %! assert (size (s.R), [2, 5]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
293 %! assert (s.R(:,1:3), zeros (2,3));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
294 %! assert (size (s.C), [2, 5]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
295 %! assert (s.C(:,1:3), zeros (2,3));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
296 %! assert (s.df, 0);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
297 %! assert (mu, [1.5, sqrt(2)/2]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
298
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
299 %!test
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
300 %! x = [1, 2, 3];
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
301 %! y = 2*x + 1;
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
302 %! ## Disable warnings entirely because there is not a specific ID to disable.
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
303 %! wstate = warning ();
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
304 %! unwind_protect
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
305 %! warning ("off", "all");
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
306 %! p0 = polyfit (x, y, logical ([1, 1, 1, 0 1]));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
307 %! [p1, s, mu] = polyfit (x, y, logical ([1, 1, 1, 0 1]));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
308 %! unwind_protect_cleanup
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
309 %! warning (wstate);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
310 %! end_unwind_protect
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
311 %! assert (p0, [0, -2/11, 12/11, 0, 23/11], 10*eps);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
312 %! assert (p1, [0, 2, 0, 0, 5], 10*eps);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
313 %! assert (size (s.X), [3, 5]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
314 %! assert (s.X(:,[1,4]), zeros (3,2));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
315 %! assert (size (s.R), [3, 5]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
316 %! assert (s.R(:,[1,4]), zeros (3,2));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
317 %! assert (size (s.C), [3, 5]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
318 %! assert (s.C(:,[1,4]), zeros (3,2));
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
319 %! assert (s.df, 0);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
320 %! assert (mu, [2, 1]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
321
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
322 %!test <*57964>
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
323 %! ## Disable warnings entirely because there is not a specific ID to disable.
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
324 %! wstate = warning ();
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
325 %! unwind_protect
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
326 %! warning ("off", "all");
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
327 %! [p, s] = polyfit ([1,2], [3,4], 2);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
328 %! unwind_protect_cleanup
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
329 %! warning (wstate);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
330 %! end_unwind_protect
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
331 %! assert (size (p), [1, 3]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
332 %! assert (size (s.X), [2, 3]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
333 %! assert (s.X(:,1), [0; 0]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
334 %! assert (size (s.R), [2, 3]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
335 %! assert (s.R(:,1), [0; 0]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
336 %! assert (size (s.C), [2, 3]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
337 %! assert (s.C(:,1), [0; 0]);
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
338
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
339 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28789
diff changeset
340 %!error <Invalid call> polyfit ()
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28789
diff changeset
341 %!error <Invalid call> polyfit (1)
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28789
diff changeset
342 %!error <Invalid call> polyfit (1,2)
28502
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
343 %!error <X and Y must have the same number of points> polyfit ([1, 2], 1, 1)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
344 %!error <X and Y must have the same number of points> polyfit (1, [1, 2], 1)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
345 %!error <N must be a non-negative integer> polyfit (1, 2, [1,2])
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
346 %!error <N must be a non-negative integer> polyfit (1, 2, -1)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
347 %!error <N must be a non-negative integer> polyfit (1, 2, Inf)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
348 %!error <N must be a non-negative integer> polyfit (1, 2, 1.5)
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
349 %!test <*57964>
39e6308e4251 polyfit.m: Overhaul function (bug #57964).
Rik <rik@octave.org>
parents: 27985
diff changeset
350 %! fail ("p = polyfit ([1,2], [3,4], 4)", "warning", "solution is not unique");