comparison scripts/polynomial/polyfit.m @ 2325:b5568c31ee2c

[project @ 1996-07-15 22:20:21 by jwe]
author jwe
date Mon, 15 Jul 1996 22:20:21 +0000
parents 5ca126254d15
children 8b262e771614
comparison
equal deleted inserted replaced
2324:fdc6e2f81333 2325:b5568c31ee2c
18 ## 02111-1307, USA. 18 ## 02111-1307, USA.
19 19
20 ## usage: polyfit (x, y, n) 20 ## usage: polyfit (x, y, n)
21 ## 21 ##
22 ## Returns the coefficients of a polynomial p(x) of degree n that 22 ## Returns the coefficients of a polynomial p(x) of degree n that
23 ## minimizes sumsq (p(x(i)) - y(i)), i.e., that best fits the data 23 ## minimizes sumsq (p(x(i)) - y(i)), i.e., that best fits the data
24 ## in the least squares sense. 24 ## in the least squares sense.
25 25
26 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> 26 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
27 ## Created: 13 December 1994 27 ## Created: 13 December 1994
28 ## Adapted-By: jwe 28 ## Adapted-By: jwe
29 29
30 function p = polyfit (x, y, n) 30 function p = polyfit (x, y, n)
31 31
32 32
33 if (nargin != 3) 33 if (nargin != 3)
34 usage ("polyfit (x, y, n)"); 34 usage ("polyfit (x, y, n)");
35 endif 35 endif
36 36
37 if (! (is_vector (x) && is_vector (y) && size (x) == size (y))) 37 if (! (is_vector (x) && is_vector (y) && size (x) == size (y)))
38 error ("polyfit: x and y must be vectors of the same size"); 38 error ("polyfit: x and y must be vectors of the same size");
39 endif 39 endif
40 40
41 if (! (is_scalar (n) && n >= 0 && ! isinf (n) && n == round (n))) 41 if (! (is_scalar (n) && n >= 0 && ! isinf (n) && n == round (n)))
42 error ("polyfit: n must be a nonnegative integer"); 42 error ("polyfit: n must be a nonnegative integer");
43 endif 43 endif
44 44
45 l = length (x); 45 l = length (x);
46 x = reshape (x, l, 1); 46 x = reshape (x, l, 1);
47 y = reshape (y, l, 1); 47 y = reshape (y, l, 1);
48 48
49 X = ones (l, 1); 49 X = ones (l, 1);
50 50
51 if (n > 0) 51 if (n > 0)
52 tmp = (x * ones (1, n)) .^ (ones (l, 1) * (1 : n)); 52 tmp = (x * ones (1, n)) .^ (ones (l, 1) * (1 : n));
53 X = [X, tmp]; 53 X = [X, tmp];