How to get the covariance matrix from a fit


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;




}

See the “S” fit option in the TGraph::Fit method description.

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…

${ROOTSYS}/tutorials/fit/ErrorIntegral.C

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();