# How to get the covariance matrix from a fit

**URL:** <https://root-forum.cern.ch/t/how-to-get-the-covariance-matrix-from-a-fit/50133>\
**Category:** Newbie\
**Created:** [May 26, 2022, 9:08am UTC](https://root-forum.cern.ch/t/how-to-get-the-covariance-matrix-from-a-fit/50133 "2022-05-26T09:08:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rrapo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/da6949/32.png) [@rrapo](https://root-forum.cern.ch/u/rrapo)\
**Post date:** [May 26, 2022, 9:08am UTC](https://root-forum.cern.ch/t/how-to-get-the-covariance-matrix-from-a-fit/50133/1 "2022-05-26T09:08:07Z")

</div>

* * *

Hi, i have an issue with che covariance matrix: I did an experiment on the band-pass filter (using a RCL-series circuit ) and i am trying to find the resonance frequency from a fit.  
the program should be ok, but i copied and pasted it here in order to help you helping me!  
I need to calculate some quantities which are linked with more parameters of the fit, but I don’t know how to extract the covariance matrix (or the elements needed). I hope you can help me somehow! Have a good day! thanks for your help!

_ROOT Version:_ Not Provided  
\_Platform: Mac  
\_Compiler: Xcode  
#include // ci serve per stampare a video il testo (uso di cout e endl, per esempio)  
#include \<TGraphErrors.h\> // ci serve per istanziare grafici  
#include \<TAxis.h\> // ci serve per manipolare gli assi dei grafici  
#include \<TCanvas.h\> // ci serve per disegnare i grafici  
#include \<TF1.h\> // ci serve per scrivere le funzioni con cui fittare i grafici  
#include   
#include \<TMatrixD.h\>  
#define \_USE\_MATH\_DEFINES

using namespace std;

// Corpo del programma. La funzione qui sotto deve avere lo stesso nome del file .C  
void filtriRCL1buono()  
{  
// numero misure prese  
const int nmisure = 36;

// Dati presi in laboratorio: V = tensione, sV = incertezza tensione, f = frequenza, sf = incertezza frequenza  
// consiglio di non mettere il valore per V = 0 V altrimenti si avranno problemi nella parte in cui si calcolano i logaritmi

```
float frequ[] = {0.5,1,2,3,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.2,9.4,9.6,9.8,10,10.2,10.5,11,11.5,12,12.5,13,14,15,16,17,18,19,20,25,30}; // kHz

float Vout[] = {0.16,0.24,0.46,0.66,0.96,1.1,1.24,1.4,1.58,1.76,1.92,2.1,2.24,2.32,2.38,2.38,2.38,2.38,2.36,2.32,2.3,2.24,2.16,2.08,2,1.92,1.86,1.7,1.58,1.46,1.36,1.28,1.2,1.14,0.88,0.74}; // V

float Vin[] = {3.92,3.92,3.9,3.86,3.82,3.8,3.72,3.62,3.54,3.4,3.26,3.1,2.92,2.82,2.8,2.8,2.8,2.8, 2.82, 2.82,2.88,2.9,3,3.08,3.18,3.24,3.3,3.4,3.5,3.56,3.6,3.66,3.68,3.72,3.82,3.86}; // V

```

// ---------------------------------------------------------------- //

// ---------------------- Quantità derivate ----------------------- //

// Array che conterà incertezze su i e V  
float sVin[nmisure];  
float sfrequ[nmisure];  
float sVout[nmisure];

// Array che conterrà i valori calcolati del guadagno = Vout/Vin  
float G[nmisure];  
float sG[nmisure];

```
float f[nmisure];
float sf[nmisure];

```

// ciclo for (loop) sulle misure  
for(int j=0; j\<nmisure;++j){  
sVin[j] = 0.1 ; // Volt, trovato come 500mA su 5 divisioni  
sfrequ[j] = 0.0001 ; // kHz, frequenza data dal generatore  
sVout[j] = 0.1; // Volt, trovato come 500mA su 5 divisioni

```
  G[j] = (Vout[j]/Vin[j]);

  sG[j] = (1/Vin[j]) * sqrt( sVout[j]*sVout[j] + ((Vout[j]*Vout[j]*sVin[j]*sVin[j])/(Vin[j]*Vin[j])) );
  
  f[j]=frequ[j]*pow(10,3);
  sf[j]= sfrequ[j]*pow(10,3);

```

// qua c’è direttamente la definizione di G come rapporto del CH2 e del CH1  
// il calcolo degli errori segue lo stesso ragionamento di quello del filtro RC

```
cout << "Measurement number " << j << ":\t V_in = (" << Vin[j] << " +- " << sVin[j] << ") V, \t frequ = (" << frequ[j] << " +- " << sfrequ[j] << ") Hertz,\t G = ("
<< G[j] << " +- " << sG[j] << "), \t V_out = (" << Vout[j] << " +- " << sVout[j] << ") V." << endl;

```

}  
// ----------------------------------------------------------------- //

```
// --------------------- Grafico G(f) con la scala logaritmica ------------------------------ //
TCanvas *cGf = new TCanvas("cGf","G(f)",200,10,600,400);
cGf->SetFillColor(0);
cGf->cd();
TGraphErrors *gGf = new TGraphErrors(nmisure,frequ,G,sfrequ,sG);
gGf->SetMarkerSize(0.6);
gGf->SetMarkerStyle(21);
gGf->SetTitle("G(f) con scala logaritmica");
gGf->GetXaxis()->SetTitle("f [kHz]");
gGf->GetYaxis()->SetTitle("G ");
cGf->SetLogx(1);
gGf->Draw("AP");
cout << "--------------------------------------------------------------------------------" << endl;
// ----------------------------------------------------------------- //

// --------------------- Grafico G(f) senza la scala logaritmica ------------------------------ //
TCanvas *cGf2 = new TCanvas("cGf2","G2(f)",200,10,600,400);
cGf2->SetFillColor(0);
cGf2->cd();
TGraphErrors *gGf2 = new TGraphErrors(nmisure,frequ,G,sfrequ,sG);
gGf2->SetMarkerSize(0.6);
gGf2->SetMarkerStyle(21);
gGf2->SetTitle("G(f) senza scala logaritmica");
gGf2->GetXaxis()->SetTitle("f [kHz]");
gGf2->GetYaxis()->SetTitle("G ");

```

// cGf2-\>SetLogx(1);  
gGf2-\>Draw(“AP”);  
cout \<\< “------------------------------------------------------------------------------” \<\< endl;  
// ----------------------------------------------------------------------------------------- //

// --------------------- Fit G(f) senza la scala logaritmica ------------------------------ //

```
TCanvas *cAf = new TCanvas("cAf","G(f) fit",200,10,600,400);
cAf->SetFillColor(0);
cAf->cd();
TGraphErrors *gAf = new TGraphErrors(nmisure,frequ,G,sfrequ,sG);
gAf->SetMarkerSize(0.6);
gAf->SetMarkerStyle(21);
gAf->SetTitle("G(f) fit");
gAf->GetXaxis()->SetTitle("f [kHz]");
gAf->GetYaxis()->SetTitle("G");
gAf->Draw("AP");

```

// inseriamo il fit con la sua funzione  
TF1 \*funz1 = new TF1(“funz1”,"x/pow( ( [0]\*pow(x,4) + [1]\*pow(x,2) +[2] ), 0.5 ) ",0,50000);  
funz1-\>SetParameter(0,100);  
funz1-\>SetParameter(1,0);  
funz1-\>SetParameter(2,0.02);

// funz1-\>SetParLimits(0,0,500);  
// funz1-\>SetParLimits(0,0,500);  
// funz1-\>SetParLimits(2,0,500);

```
funz1->SetLineColor(4); // Blu
gAf->Fit(funz1,"RM+");
gStyle->SetOptFit(1);
cout << "Chi^2:" << funz1->GetChisquare() << ", number of DoF: " << funz1->GetNDF() << " (Probability: " << funz1->GetProb() << ")." << endl;
cout << "--------------------------------------------------------------------------------------------------------" << endl;

}
```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 26, 2022, 9:12am UTC](https://root-forum.cern.ch/t/how-to-get-the-covariance-matrix-from-a-fit/50133/2 "2022-05-26T09:12:05Z")

</div>

See the “`S`” fit option in the [TGraph::Fit](https://root.cern/doc/master/classTGraph.html#aa978c8ee0162e661eae795f6f3a35589) method description.

---

<div class="post-metadata">

**Author:** ![rrapo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/da6949/32.png) [@rrapo](https://root-forum.cern.ch/u/rrapo)\
**Post date:** [May 26, 2022, 9:18am UTC](https://root-forum.cern.ch/t/how-to-get-the-covariance-matrix-from-a-fit/50133/3 "2022-05-26T09:18:29Z")

</div>

hi.  
first of all, thank you for the reply.  
excuse me, but what do you mean with “S”?  
PS: I am sorry, but I am not really used to do data analysis with root and with exploring the forum/descriptions on this website…

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 26, 2022, 9:26am UTC](https://root-forum.cern.ch/t/how-to-get-the-covariance-matrix-from-a-fit/50133/4 "2022-05-26T09:26:15Z")

</div>

[`${ROOTSYS}/tutorials/fit/ErrorIntegral.C`](https://root.cern/doc/master/ErrorIntegral_8C.html)

---

<div class="post-metadata">

**Author:** ![rrapo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/da6949/32.png) [@rrapo](https://root-forum.cern.ch/u/rrapo)\
**Post date:** [May 26, 2022, 9:54am UTC](https://root-forum.cern.ch/t/how-to-get-the-covariance-matrix-from-a-fit/50133/5 "2022-05-26T09:54:45Z")

</div>

oh, I read the code and copied some parts and it WORKS (it’s CRAZY)!!  
thank you! you’ve made my day! have a nice day!  
goodbye!

PS: i am writing down here the code added for the covariance matrix for those who will read this forum, I hope this helps them a little (it works with the fit function of my code, which is in the first question I made)

```
auto fitResult = gAf->Fit(funz1,"S");
auto covMatrix = fitResult->GetCovarianceMatrix();
std::cout << "Covariance matrix from the fit ";
covMatrix.Print();
```
