changeset 4491:96a25f032846

[project @ 2003-08-28 21:01:21 by jwe]
author jwe
date Thu, 28 Aug 2003 21:01:21 +0000
parents 1aed172ab84a
children 819e3c246702
files scripts/ChangeLog scripts/polynomial/polyfit.m
diffstat 2 files changed, 37 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Thu Aug 28 19:03:13 2003 +0000
+++ b/scripts/ChangeLog	Thu Aug 28 21:01:21 2003 +0000
@@ -1,3 +1,10 @@
+2003-08-28  John W. Eaton  <jwe@bevo.che.wisc.edu>
+
+	* polynomial/polyfit.m: Avoid calling flipud.
+	From Pascal A. Dupuis <Pascal.Dupuis@esat.kuleuven.ac.be>.
+	Return structure as second output value for improved Matlab
+	compatibility.
+
 2003-07-30  John W. Eaton  <jwe@bevo.che.wisc.edu>
 
 	* linear-algebra/cond.m: Behave as though old built-in variable
--- a/scripts/polynomial/polyfit.m	Thu Aug 28 19:03:13 2003 +0000
+++ b/scripts/polynomial/polyfit.m	Thu Aug 28 21:01:21 2003 +0000
@@ -18,7 +18,7 @@
 ## 02111-1307, USA.
 
 ## -*- texinfo -*-
-## @deftypefn {Function File} {[@var{p}, @var{yf}] =} polyfit (@var{x}, @var{y}, @var{n})
+## @deftypefn {Function File} {[@var{p}, @var{s}] =} polyfit (@var{x}, @var{y}, @var{n})
 ## Return the coefficients of a polynomial @var{p}(@var{x}) of degree
 ## @var{n} that minimizes
 ## @iftex
@@ -33,19 +33,31 @@
 ## @end ifinfo
 ##  to best fit the data in the least squares sense.
 ##
-## The polynomial coefficients are returned in a row vector if @var{x}
-## and @var{y} are both row vectors; otherwise, they are returned in a
-## column vector.
+## The polynomial coefficients are returned in a row vector.
+##
+## If two output arguments are requested, the second is a structure
+## containing the following fields:
 ##
-## If two output arguments are requested, the second contains the values of
-## the polynomial for each value of @var{x}.
+## @table @code
+## @item R
+## The Cholesky factor of the Vandermonde matrix used to compute the
+## polynomial coefficients.
+## @item X
+## The Vandermonde matrix used to compute the polynomial coefficients.
+## @item df
+## The degrees of freedom.
+## @item normr
+## The norm of the residuals.
+## @item yf
+## The values of the polynomial for each value of @var{x}.
+## @end table
 ## @end deftypefn
 
 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
 ## Created: 13 December 1994
 ## Adapted-By: jwe
 
-function [p, yf] = polyfit (x, y, n)
+function [p, s, mu] = polyfit (x, y, n)
 
 
   if (nargin != 3)
@@ -66,22 +78,25 @@
   x = reshape (x, l, 1);
   y = reshape (y, l, 1);
 
-  X = (x * ones (1, n+1)) .^ (ones (l, 1) * (0 : n));
+  X = (x * ones (1, n+1)) .^ (ones (l, 1) * (n : -1 : 0));
 
   p = X \ y;
 
-  if (nargout == 2)
-    yf = X * p;
+  if (nargout > 1)
+
+    yf = X*p;
 
     if (y_is_row_vector)
-      yf = yf.';
+      s.yf = yf.';
+    else
+      s.yf = yf;
     endif
-  endif
 
-  p = flipud (p);
+    [s.R, dummy] = chol (X'*X);
+    s.X = X;
+    s.df = l - n - 1;
+    s.normr = norm (yf - y);
 
-  if (y_is_row_vector && rows (x) == 1)
-    p = p';
   endif
 
 endfunction