Re: [Scilab-users] Polynomial fitting

2020-04-04 Thread Federico Miyara


Stéphane,

Yeah, but really badly conditionned compared to the above method which 
is based on orthogonal tranformations (X=Q*R factorization). With your 
below method you solve a linear system with X'*X matrix which has a 
condition number which is the square of the condition number of the R 
matrix issued from the Q*R factorization of X.


Thanks for the clarification!

Is it posible to predict in what kind of cases will the bad conditioning 
impact on the result? I've compared the results for several cases and 
they are exactly the same.


However, I've noticed that when taking the inverse of a matrix whose 
components span several orders of magnitude there are important errors, 
but if I manage to normalize the poblem the errors are substantially reduced


Regards,

Federico



S.



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

___
users mailing list
users@lists.scilab.org
https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users

--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Polynomial fitting

2020-04-02 Thread Clément David
Hello Frederico,

AFAIK there is a moc_polyfit function inside the Matlab/Octave Compatibility 
toolbox which might help you porting your code. Stixbox also provides a polyfit 
function which might be more generic toward Scilab datatypes.

Thanks,

--
Clément

> -Original Message-
> From: users  On Behalf Of Stéphane Mottelet
> Sent: Thursday, April 2, 2020 10:48 AM
> To: users@lists.scilab.org
> Subject: Re: [Scilab-users] Polynomial fitting
> 
> Hello Frederico,
> 
> 
> Le 02/04/2020 à 10:27, Federico Miyara a écrit :
> 
> 
> 
>   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
> <https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/mail
> inglists.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.
> 
> 
> Yeah, but really badly conditionned compared to the above method which is
> based on orthogonal tranformations (X=Q*R factorization). With your below
> method you solve a linear system with X'*X matrix which has a condition number
> which is the square of the condition number of the R matrix issued from the 
> Q*R
> factorization of X.
> 
> S.
> 
> 
> 
>   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
> 
> 
>   ___
>   users mailing list
>   users@lists.scilab.org <mailto:users@lists.scilab.org>
>   https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLm
> Zy/lists.scilab.org/mailman/listinfo/users
> 
> --
> Stéphane Mottelet
> Ingénieur de recherche
> EA 4297 Transformations Intégrées de la Matière Renouvelable Département
> Génie des Procédés Industriels Sorbonne Universités - Université de 
> Technologie
> de Compiègne CS 60319, 60203 Compiègne cedex Tel : +33(0)344234688
> http://www.utc.fr/~mottelet
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Polynomial fitting

2020-04-02 Thread Stéphane Mottelet

Hello Frederico,

Le 02/04/2020 à 10:27, Federico Miyara a écrit :


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.


Yeah, but really badly conditionned compared to the above method which 
is based on orthogonal tranformations (X=Q*R factorization). With your 
below method you solve a linear system with X'*X matrix which has a 
condition number which is the square of the condition number of the R 
matrix issued from the Q*R factorization of X.


S.



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

___
users mailing list
users@lists.scilab.org
https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users


--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users