double lin(double* x, double* p) { return p[0] + x[0] * p[1]; } void upf_cv11_fit(const char* fileName) { FILE* file = fopen(fileName,"r"); int N = 0; double x, y, sigma; TGraphErrors* gr = new TGraphErrors(); // objekt grafu s chybovymi useckami // dokud prectes 3 cisla, prochazej radky... while(fscanf(file,"%lf %lf %lf", &x, &y, &sigma) == 3) { gr->SetPoint(N, x, y); // nastav x a y N-teho bodu gr->SetPointError(N, 0, sigma); // nastav jeho sigma_x a sigma_y N++; // pocitadlo radku } printf("Precten soubor '%s' s %i radky.\n",fileName,N); fclose(file); // zavri soubor gr->SetMarkerStyle(8); gr->SetMarkerColor(1); gr->Draw("PA"); // definuj funkci: /* TF1* func = new TF1("func","[0] + [1]*x", // [i] je i-ty parametr gr->GetX()[0], gr->GetX()[N-1]); // gr->GetX() je pole x func->SetParameters(0,0); // nastav pocatecni hodnoty */ // konstrukce s ukazatelem na C++ funkci: TF1* func = new TF1("func",&lin,gr->GetX()[0], gr->GetX()[N-1],2); /* TF1* func = new TF1("func","[0] + [1]*sin(x) + [2]*cos(x) + [3]*sin(2*x) + [4]*cos(2*x)", // [i] je i-ty parametr gr->GetX()[0], gr->GetX()[N-1]); // gr->GetX() je pole x func->SetParameters(0,0,0,0,0); // nastav pocatecni hodnoty */ TFitResultPtr res = gr->Fit(func,"S"); printf("\nNafitovane parametry: %.4f +/- %.4f %.4f +/- %.4f\n", func->GetParameter(0),func->GetParError(0), func->GetParameter(1),func->GetParError(1)); printf("\n Matice kovariance:\n"); for (int i = 0; i < func->GetNpar(); i++) { for (int j = 0; j < func->GetNpar(); j++) printf(" %7.3f", res->CovMatrix(i,j)); printf("\n"); } printf("\n Matice korelace:\n"); for (int i = 0; i < func->GetNpar(); i++) { for (int j = 0; j < func->GetNpar(); j++) printf(" %7.3f", res->CovMatrix(i,j) / sqrt(res->CovMatrix(i,i)) / sqrt(res->CovMatrix(j,j))); printf("\n"); } }