[Scilab-users] Polynomial fitting

Federico Miyara fmiyara at fceia.unr.edu.ar
Thu Apr 2 10:27:14 CEST 2020


Dear All,

Trying to convert an old Matlab script to Scilab I miss the function 
polyfit, which computes the coefficients of a polynomial that fits x-y 
data using the least square method.

I found the following thread 
http://mailinglists.scilab.org/Polynomic-regression-td4030799.html 
explaining that one can use the backslash, for instance for a 3rd degree 
polynomial, where x and y are column vectors:

X = [ones(x), x, x.^2, x.^3]
A = X\y;

(I changed the order of powers to get the coefficients ready for horner)

I implemented a polyfit function using the basic theory of polynomial 
regression and I find it is faster by a factor of 1.5-2 than the 
previously mentioned method.

The basic algorithm I use is (n = desired degree):

// Initialize matrix X
X = ones(length(x), n+1);
// Compute Vandermonde's matrix
for k =2:n+1
    X(:,k) = X(:,k-1).*x;
end
// Apply the Moore-Penrose pseudoinverse matrix and
// multiply by the dependent data vector to get the
// least squares approximation of the polynomial
// coefficients
A = inv(X'*X)*X'*y;

I've seen some discussion regarding the need for a polyfit function in 
Scilab. The main argument against such a function is that it is 
unnecessary since it is a particular case of the backslash division. 
This is true, but the above example shows that users' implementations 
are not always optimized, and as it is such a frequent problem, it would 
be nice to have a native polyfit (or whatever it may be called) function.

Regards,

Federico Miyara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20200402/b531d6d5/attachment.htm>


More information about the users mailing list