Difference Compared to Octave

Thomas D. Dean tomdean at speakeasy.org
Sun Aug 24 23:08:19 CEST 2008


I tried to implement the Octave polyfit function in scilab.

Coding was no problem, some minor syntax changes and a couple
manipulation function changes.

I get different results when comparing Scilab to Octave.
The results from Octave seem to be closer to the input data.

The difference seems to be in X\y, returning a different value in
Scilab.  The function and data are at the bottom.

n = 2;
x = [1:7]*1000;
l = length (x);
x = matrix(x, l, 1);
y = matrix(y, l, 1);

X = (x * ones (1, n+1)) .^ (ones (l, 1) * (n : -1 : 0));

p = X\y;

Octave:   [3.9283e-05 -1.3290e-01  1.1586e+02]
Scilab:   [ 0.0000325 - 0.0715604  0.  ]

Also, Scilab produces a message, "rank defficient. rank =  2"

Any ideas?

tomdean

function [p, s] = polyfit (x, y, n)
  if (argn(2) <> 3) then
    error("Usage: polyfit(x,y,n)");
  end

  if (~ (isvector(x) & isvector(y) & (max(size(x)) == max(size(y))) ) )
then
    error ("polyfit: x and y must be vectors of the same size");
  end

  if (~ (prod(size(n)==1) & n >= 0 & ~isinf (n) & n == round (n))) then
    error ("polyfit: n must be a nonnegative integer");
  end

  y_is_row_vector = (size(y,1) == 1);

  l = length (x);
  x = matrix(x, l, 1);
  y = matrix(y, l, 1);

  X = (x * ones (1, n+1)) .^ (ones (l, 1) * (n : -1 : 0));

  p = X \ y;

  if (argn(1) > 1) then
    yf = X*p;
    if (y_is_row_vector) then
      s.yf = yf.';
    else
      s.yf = yf;
    end
    s.R = chol (X'*X);
    s.X = X;
    s.df = l - n - 1;
    s.normr = norm (yf - y);
  end

  // Return value should be a row vector.
  p = p.';

endfunction;

s = [1:7]*1000;
t = [ 3.3598, 26.4377, 89.6760, 211.5766, 414.1391, 716.1501,1128.1406];

polyfit(s,t,2)





More information about the users mailing list