Mercurial > hg > octave-nkf
annotate scripts/polynomial/polyfit.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | f1d0f506ee78 |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19871
diff
changeset
|
1 ## Copyright (C) 1996-2015 John W. Eaton |
2313 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
2303 | 18 |
3368 | 19 ## -*- texinfo -*- |
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 | 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 |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
25 ## @code{[@var{x}, @var{y}]}. |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
26 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
27 ## If @var{n} is a logical vector, it is used as a mask to selectively force |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
28 ## the corresponding polynomial coefficients to be used or ignored. |
3418 | 29 ## |
4491 | 30 ## The polynomial coefficients are returned in a row vector. |
31 ## | |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
14062
diff
changeset
|
32 ## The optional output @var{s} is a structure containing the following fields: |
3426 | 33 ## |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
34 ## @table @samp |
4491 | 35 ## @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
|
36 ## Triangular factor R from the QR@tie{}decomposition. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
37 ## |
4491 | 38 ## @item X |
7501 | 39 ## 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
|
40 ## |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
41 ## @item C |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
42 ## 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
|
43 ## @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
|
44 ## propagation. |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
45 ## |
4491 | 46 ## @item df |
7501 | 47 ## The degrees of freedom. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
48 ## |
4491 | 49 ## @item normr |
7501 | 50 ## The norm of the residuals. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
51 ## |
4491 | 52 ## @item yf |
7501 | 53 ## The values of the polynomial for each value of @var{x}. |
4491 | 54 ## @end table |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
55 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
56 ## 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
|
57 ## statistical error limits of the predicted values. In particular, the |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
58 ## standard deviation of @var{p} coefficients is given by |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
59 ## |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
60 ## @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
|
61 ## |
20375
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
62 ## When the third output, @var{mu}, is present the coefficients, @var{p}, are |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
63 ## associated with a polynomial in |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
64 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
65 ## @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:
19898
diff
changeset
|
66 ## 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:
19898
diff
changeset
|
67 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
68 ## This linear transformation of @var{x} improves the numerical stability of |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
69 ## the fit. |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
14062
diff
changeset
|
70 ## @seealso{polyval, polyaffine, roots, vander, zscore} |
3368 | 71 ## @end deftypefn |
2311 | 72 |
5428 | 73 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 74 ## Created: 13 December 1994 |
75 ## 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
|
76 ## 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
|
77 ## polynomial mask instead of a polynomial degree. |
2312 | 78 |
4491 | 79 function [p, s, mu] = polyfit (x, y, n) |
2325 | 80 |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
81 if (nargin < 3 || nargin > 4) |
6046 | 82 print_usage (); |
2261 | 83 endif |
2325 | 84 |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
85 if (nargout > 2) |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
86 ## Normalized the x values. |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
87 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
|
88 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
|
89 endif |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
90 |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
91 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
|
92 error ("polyfit: X and Y must be vectors of the same size"); |
2261 | 93 endif |
2325 | 94 |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
95 if (islogical (n)) |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
96 polymask = n; |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
97 ## 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
|
98 ## 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
|
99 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
|
100 else |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
101 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
|
102 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
|
103 endif |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
104 polymask = logical (ones (1, n+1)); m = n; |
2261 | 105 endif |
2325 | 106 |
14110
b8d9530e940e
undo non-documentation changes in changeset 614505385171
John W. Eaton <jwe@octave.org>
parents:
14104
diff
changeset
|
107 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
|
108 |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
109 ## 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
|
110 l = numel (x); |
9138 | 111 x = x(:); |
112 y = y(:); | |
2325 | 113 |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
114 ## Construct the Vandermonde matrix. |
9138 | 115 v = vander (x, n+1); |
2261 | 116 |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
117 ## 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
|
118 [q, r, k] = qr (v(:, polymask), 0); |
9138 | 119 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
|
120 p(k) = p; |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
121 |
17307
4448cc742880
Use '!' not operator rather than '~' in core m-files.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
122 if (n != m) |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
123 q = p; p = zeros (n+1, 1); |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
124 p(polymask) = q; |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
125 endif |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
126 |
4491 | 127 if (nargout > 1) |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
128 yf = v*p; |
3105 | 129 |
14110
b8d9530e940e
undo non-documentation changes in changeset 614505385171
John W. Eaton <jwe@octave.org>
parents:
14104
diff
changeset
|
130 if (y_is_row_vector) |
4491 | 131 s.yf = yf.'; |
132 else | |
133 s.yf = yf; | |
3105 | 134 endif |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
135 s.X = v; |
3091 | 136 |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
137 ## 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
|
138 ## 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
|
139 ## 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
|
140 ## 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
|
141 try |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
142 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
|
143 catch |
17310
b327c19b6b16
polyfit.m: Replace NaN*ones(...) with direct construction NaN(...).
Rik <rik@octave.org>
parents:
17307
diff
changeset
|
144 C = NaN (m+1, m+1); |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
145 end_try_catch |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
146 |
17307
4448cc742880
Use '!' not operator rather than '~' in core m-files.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
147 if (n != m) |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
148 ## fill matrices if required |
17307
4448cc742880
Use '!' not operator rather than '~' in core m-files.
Rik <rik@octave.org>
parents:
17245
diff
changeset
|
149 s.X(:, !polymask) = 0; |
14377
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
150 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
|
151 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
|
152 else |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
153 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
|
154 s.C = C; |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
155 endif |
c87f927d047d
polyfit.m: add the ability to specify the polynomial template.
Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
parents:
14363
diff
changeset
|
156 s.df = l - m - 1; |
4491 | 157 s.normr = norm (yf - y); |
2261 | 158 endif |
159 | |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
160 ## Return a row vector. |
5395 | 161 p = p.'; |
162 | |
2261 | 163 endfunction |
7411 | 164 |
165 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
166 %!shared x |
7411 | 167 %! 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
|
168 %!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
|
169 %!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
|
170 %!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
|
171 %!fail ("polyfit (x, x.^2+x+1, [])") |
7411 | 172 |
7500
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
173 ## 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
|
174 ## 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
|
175 ## 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
|
176 ## 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
|
177 %!test |
19871
e5facc6eec13
Silence warning messages in %!test code.
Rik <rik@octave.org>
parents:
19790
diff
changeset
|
178 %! 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
|
179 %! 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
|
180 %! -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
|
181 %! 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
|
182 %! 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
|
183 %! 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
|
184 %! [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
|
185 %! [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
|
186 %! 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
|
187 |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
188 %!test |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
189 %! x = 1:4; |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
190 %! 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
|
191 %! 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
|
192 %! 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
|
193 %! 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
|
194 |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
195 %!test |
19871
e5facc6eec13
Silence warning messages in %!test code.
Rik <rik@octave.org>
parents:
19790
diff
changeset
|
196 %! 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
|
197 %! x = 1000 + (-5:5); |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
198 %! 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
|
199 %! pn = ones (1,5); |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
200 %! 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
|
201 %! [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
|
202 %! [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
|
203 %! 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
|
204 %! 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
|
205 %! 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
|
206 %! 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
|
207 |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
208 %!test |
2df882e69f13
use QR decomposition and normalization for polyfit; normalization for polyval
Ben Abbott <bpabbott@mac.com>
parents:
7411
diff
changeset
|
209 %! 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
|
210 %! 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
|
211 %! 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
|
212 %! 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
|
213 %! 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
|
214 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
215 %!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
|
216 |