81 void solveLinear(Double_t eps = 1.e-12)
83 cout <<
"Perform the fit y = c0 + c1 * x in four different ways" << endl;
85 const Int_t nrVar = 2;
86 const Int_t nrPnts = 4;
88 Double_t ax[] = {0.0,1.0,2.0,3.0};
89 Double_t ay[] = {1.4,1.5,3.7,4.1};
90 Double_t ae[] = {0.5,0.2,1.0,0.5};
95 TVectorD x; x.Use(nrPnts,ax);
96 TVectorD y; y.Use(nrPnts,ay);
97 TVectorD e; e.Use(nrPnts,ae);
99 TMatrixD A(nrPnts,nrVar);
100 TMatrixDColumn(A,0) = 1.0;
101 TMatrixDColumn(A,1) = x;
103 cout <<
" - 1. solve through Normal Equations" << endl;
105 const TVectorD c_norm = NormalEqn(A,y,e);
107 cout <<
" - 2. solve through SVD" << endl;
113 for (Int_t irow = 0; irow < A.GetNrows(); irow++) {
114 TMatrixDRow(Aw,irow) *= 1/e(irow);
120 const TVectorD c_svd = svd.Solve(yw,ok);
122 cout <<
" - 3. solve with pseudo inverse" << endl;
124 const TMatrixD pseudo1 = svd.Invert();
125 TVectorD c_pseudo1 = yw;
126 c_pseudo1 *= pseudo1;
128 cout <<
" - 4. solve with pseudo inverse, calculated brute force" << endl;
130 TMatrixDSym AtA(TMatrixDSym::kAtA,Aw);
131 const TMatrixD pseudo2 = AtA.Invert() * Aw.T();
132 TVectorD c_pseudo2 = yw;
133 c_pseudo2 *= pseudo2;
135 cout <<
" - 5. Minuit through TGraph" << endl;
137 TGraphErrors *gr =
new TGraphErrors(nrPnts,ax,ay,0,ae);
138 TF1 *f1 =
new TF1(
"f1",
"pol1",0,5);
140 TVectorD c_graph(nrVar);
141 c_graph(0) = f1->GetParameter(0);
142 c_graph(1) = f1->GetParameter(1);
149 same &= VerifyVectorIdentity(c_norm,c_svd,0,eps);
150 same &= VerifyVectorIdentity(c_norm,c_pseudo1,0,eps);
151 same &= VerifyVectorIdentity(c_norm,c_pseudo2,0,eps);
152 same &= VerifyVectorIdentity(c_norm,c_graph,0,eps);
154 cout <<
" All solutions are the same within tolerance of " << eps << endl;
156 cout <<
" Some solutions differ more than the allowed tolerance of " << eps << endl;