1 | #include "machdefs.h"
|
---|
2 | #include "peida.h"
|
---|
3 | #include "linfit.h"
|
---|
4 | #include "matrix.h"
|
---|
5 | #include "cvector.h"
|
---|
6 |
|
---|
7 | double LinFit(const Vector& x, const Vector& y, int nf, double (*f)(int, double),
|
---|
8 | Vector& c)
|
---|
9 | {
|
---|
10 | int n = x.NElts();
|
---|
11 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
12 |
|
---|
13 | Matrix fx(nf, n);
|
---|
14 | for (int i=0; i<nf; i++)
|
---|
15 | for (int j=0; j<n; j++)
|
---|
16 | fx(i,j) = f(i,x(j));
|
---|
17 |
|
---|
18 | return LinFit(fx,y,c);
|
---|
19 | }
|
---|
20 |
|
---|
21 |
|
---|
22 |
|
---|
23 | double LinFit(const Matrix& fx, const Vector& y, Vector& c)
|
---|
24 | {
|
---|
25 | int n = y.NElts();
|
---|
26 | if ( n != fx.NCol()) THROW(sizeMismatchErr);
|
---|
27 |
|
---|
28 | int nf = fx.NRows();
|
---|
29 |
|
---|
30 | Matrix a(nf,nf);
|
---|
31 |
|
---|
32 | for (int j=0; j<nf; j++)
|
---|
33 | for (int k=j; k<nf; k++)
|
---|
34 | a(j,k) = a(k,j) = fx.Row(j) * fx.Row(k);
|
---|
35 |
|
---|
36 | c = fx * y;
|
---|
37 |
|
---|
38 | if (Matrix::GausPiv(a,c) == 0) THROW(singMatxErr);
|
---|
39 |
|
---|
40 | double xi2 = 0;
|
---|
41 |
|
---|
42 | for (int k=0; k<n; k++) {
|
---|
43 | double x = 0;
|
---|
44 | for (int i=0; i<nf; i++)
|
---|
45 | x += c(i) * fx(i,k);
|
---|
46 | x -= y(k);
|
---|
47 | xi2 += x*x;
|
---|
48 | }
|
---|
49 | return xi2;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | double LinFit(const Vector& x, const Vector& y, const Vector& errY2, int nf,
|
---|
55 | double (*f)(int, double), Vector& c, Vector& errC)
|
---|
56 | {
|
---|
57 | int n = x.NElts();
|
---|
58 | if (n != y.NElts()) THROW(sizeMismatchErr);
|
---|
59 |
|
---|
60 | Matrix fx(nf, n);
|
---|
61 | for (int i=0; i<nf; i++)
|
---|
62 | for (int j=0; j<n; j++)
|
---|
63 | fx(i,j) = f(i,x(j));
|
---|
64 |
|
---|
65 | return LinFit(fx,y,errY2,c,errC);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | double LinFit(const Matrix& fx, const Vector& y, const Vector& errY2,
|
---|
70 | Vector& c, Vector& errC)
|
---|
71 | {
|
---|
72 | int n = y.NElts();
|
---|
73 | if ( n != errY2.NElts()) THROW(sizeMismatchErr);
|
---|
74 | if ( n != fx.NCol()) THROW(sizeMismatchErr);
|
---|
75 |
|
---|
76 | int nf = fx.NRows();
|
---|
77 |
|
---|
78 | Matrix a(nf,nf);
|
---|
79 |
|
---|
80 | c.Realloc(nf);
|
---|
81 | errC.Realloc(nf);
|
---|
82 |
|
---|
83 | for (int j=0; j<nf; j++)
|
---|
84 | for (int k=j; k<nf; k++) {
|
---|
85 | double x=0;
|
---|
86 | for (int l=0; l<n; l++)
|
---|
87 | x += fx(j,l) * fx(k,l) / errY2(l); // Matrice a inverser
|
---|
88 | a(j,k) = a(k,j) = x;
|
---|
89 | }
|
---|
90 |
|
---|
91 | Matrix d(nf,nf+1);
|
---|
92 | for (int k=0; k<nf; k++) {
|
---|
93 | double x=0;
|
---|
94 | for (int l=0; l<n; l++)
|
---|
95 | x += fx(k,l) * y(l) / errY2(l); // Second membre 1ere colonne
|
---|
96 | d(k,0) = x;
|
---|
97 | for (int m=1; m<=nf; m++)
|
---|
98 | d(k,m) = (k==m) ? 1 : 0; // Reste second membre = Id.
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (Matrix::GausPiv(a,d) == 0) THROW(singMatxErr);
|
---|
102 |
|
---|
103 | for (int l=0; l<nf; l++) {
|
---|
104 | c(l) = d(l,0); // Parametres = 1ere colonne
|
---|
105 | errC(l) = d(l,l+1); // Erreurs = diag inverse.
|
---|
106 | }
|
---|
107 |
|
---|
108 | double xi2 = 0;
|
---|
109 |
|
---|
110 | for (int jj=0; jj<n; jj++) {
|
---|
111 | double x = 0;
|
---|
112 | for (int ii=0; ii<nf; ii++)
|
---|
113 | x += c(ii) * fx(ii,jj);
|
---|
114 | x -= y(jj);
|
---|
115 | xi2 += x*x/errY2(jj);
|
---|
116 | }
|
---|
117 | return xi2;
|
---|
118 | }
|
---|