# HG changeset patch # User jwe # Date 867263850 0 # Node ID 1a5fe3010f09efcf57f2e34fe5f019e208d790b3 # Parent 9c6cd52f3f5a9089d58f0764281b4d6db176dbe3 [project @ 1997-06-25 18:36:19 by jwe] diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,12 @@ +Wed Jun 25 13:34:06 1997 John W. Eaton + + * polynomial/polyfit.m: Return fit y values as second output. + Don't use QR factorization to solve least squares problem. + +Wed Jun 18 10:24:00 1997 John W. Eaton + + * control/dlqr.m: Use ao, not a, to compute k. + Tue Jun 3 12:16:00 1997 John W. Eaton * miscellaneous/path.m: New file. diff --git a/scripts/control/dlqr.m b/scripts/control/dlqr.m --- a/scripts/control/dlqr.m +++ b/scripts/control/dlqr.m @@ -92,7 +92,7 @@ if (is_symmetric (q) && is_symmetric (r) ... && all (eig (q) >= 0) && all (eig (r) > 0)) p = dare (ao, b, qo, r); - k = (r+b'*p*b)\b'*p*a + r\zz'; + k = (r+b'*p*b)\b'*p*ao + r\zz'; e = eig (a - b*k); else error ("dlqr: q (r) must be symmetric positive (semi) definite"); diff --git a/scripts/polynomial/polyfit.m b/scripts/polynomial/polyfit.m --- a/scripts/polynomial/polyfit.m +++ b/scripts/polynomial/polyfit.m @@ -17,17 +17,20 @@ ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. -## usage: polyfit (x, y, n) +## usage: [p, yf] = polyfit (x, y, n) ## ## Returns the coefficients of a polynomial p(x) of degree n that ## minimizes sumsq (p(x(i)) - y(i)), i.e., that best fits the data ## in the least squares sense. +## +## If two outputs are requested, also return the values of the +## polynomial for each value of x. ## Author: KH ## Created: 13 December 1994 ## Adapted-By: jwe -function p = polyfit (x, y, n) +function [p, yf] = polyfit (x, y, n) if (nargin != 3) @@ -46,22 +49,26 @@ x = reshape (x, l, 1); y = reshape (y, l, 1); - X = ones (l, 1); + ## Unfortunately, the economy QR factorization doesn't really save + ## memory doing the computation -- the returned values are just + ## smaller. - if (n > 0) - tmp = (x * ones (1, n)) .^ (ones (l, 1) * (1 : n)); - X = [X, tmp]; - endif + ## [Q, R] = qr (X, 0); + ## p = flipud (R \ (Q' * y)); - ## Compute polynomial coeffients, making returned value compatible - ## with Matlab. + ## XXX FIXME XXX -- this is probably not so good for extreme values of + ## N or X... - [Q, R] = qr (X, 0); + X = (x * ones (1, n+1)) .^ (ones (l, 1) * (0 : n)); - p = flipud (R \ (Q' * y)); + p = (X' * X) \ (X' * y); if (! prefer_column_vectors) p = p'; endif + if (nargout == 2) + yf = X * p; + endif + endfunction