[Scilab-users] errors (uncertainties) in non-linear least-squares fitting parameters

CRETE Denis denis.crete at thalesgroup.com
Wed Aug 26 11:57:43 CEST 2020


Hello,
Just to finish my suggestion with this code, taking Y(2,:) as "reference":
// prepare "noisy" data
slope=[0.9;1;1.2];X=1:10;a=4;
Y=slope*(X+a)+0.1*rand(3,10);

// solve problem
Z=matrix(Y'*inv(diag(Y(2,:)*Y')),-1,1);
[p,q,sig]=reglin([X,X,X],Z')

// compare results with a and slope
q/p, p*Y(2,:)*Y'

It should work as long as the same values of X are used. As it is, it assumes that datasets contain the same number of points. This restriction may be suppressed using an average value for the set of Yk taken at equal values of xk and a weight equal to the number of points averaged for this xk.
And "sig" should give you some information on errors...

HTH
Denis


De : users <users-bounces at lists.scilab.org> De la part de CRETE Denis
Envoyé : mardi 25 août 2020 16:38
À : Users mailing list for Scilab <users at lists.scilab.org>; Heinz Nabielek <heinznabielek at me.com>
Objet : Re: [Scilab-users] errors (uncertainties) in non-linear least-squares fitting parameters

Hello,
If the fixed point has to be optimized as well, it is possible to keep a linear treatment, although the solution that I have found is tedious:
First, notice that because of the fixed point and the set of xk is the same for the 3 lines, all Y coordinates are proportional, I mean

-          y2(xk)=P2/P1*y1(xk)

-          y3(xk)=P3/P1*y1(xk)
It is probably easy to fit the datasets y2 and y3 as a function of y1 to find r=P2/P1 and s=P3/P1. It might even be possible to use r=sum(y2)/sum(y1) and s= sum(y3)/sum(y1)... but the exact solution of the least square method is r=sum(y2.*y1)/sum(y1.*y1), s= sum(y3.*y1)/sum(y1.*y1).
Then the full dataset of the 3 functions y1, y2/r and y3/s can be adjusted to the same function p1*x+A (e.g. using reglin)
However, I did not write the code, yet... There might exist a more elegant solution...
I understand it is not in the focus of the initial question, but it may help anyway.

Denis
NB: a more compact algorithm is to fit for i=1...3,  yi/sum(yi.*y1)= f(x)

De : users <users-bounces at lists.scilab.org<mailto:users-bounces at lists.scilab.org>> De la part de Rafael Guerra
Envoyé : mardi 25 août 2020 01:47
À : Heinz Nabielek <heinznabielek at me.com<mailto:heinznabielek at me.com>>; Users mailing list for Scilab <users at lists.scilab.org<mailto:users at lists.scilab.org>>
Objet : Re: [Scilab-users] errors (uncertainties) in non-linear least-squares fitting parameters

In that case, the code can be simplified using backslash left matrix division:

// Fixed point (-4,0) solution:
a = (MW+4)\Y;
b = a*4;
GG= a'.*.xx' + repmat(b',1,size(xx,1));
plot(xx,GG','LineWidth',1);

Regards,
Rafael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20200826/6076b33a/attachment.htm>


More information about the users mailing list