void uniform(int nSum = 10) // parametr: počet čísel x_i v sumě { int nGen = 1e6; // počet generovaných čísel X = sum x_i // U(0,1): double mu_x = 0.5; double sigma_x = 1./sqrt(12); double mu_y = nSum * mu_x; double sigma_y = sqrt(nSum) * sigma_x; TRandom3* rnd = new TRandom3(); TH1D* histy = new TH1D("histy",";y", 200, mu_y - 4*sigma_y, mu_y + 4*sigma_y); histy->SetLineWidth(2); for (int j = 0; j < nGen; j++) { double y = 0; // = sum x_i, i = 0, 1, ..., nSum-1 for (int i = 0; i < nSum; i++) y += rnd->Rndm(); histy->Fill(y); } TF1* fNorm = new TF1("fNorm","1 / (TMath::Sqrt(2*TMath::Pi())*[2]) * gaus", mu_y - 4*sigma_y, mu_y + 4*sigma_y); fNorm->SetParameter(0,nGen*histy->GetBinWidth(1)); // 1. parametr = normalizace fNorm->SetParameter(1,mu_y); // 2. parametr = mu Gausiánu fNorm->SetParameter(2,sigma_y); // 3. parametr = sigma Gausiánu gStyle->SetOptStat(0); // neukazuj tabulku entries, mean, RMS histy->Draw(); fNorm->Draw("same"); TLegend* leg = new TLegend(0.6,0.75,0.9,0.9); char str[256]; sprintf(str,"Sumy %i #it{x} #in U(0,1)",nSum); leg->AddEntry(histy, str); sprintf(str,"%.2e N(%.2f, %.2f)",nGen*histy->GetBinWidth(1),mu_y,sigma_y); leg->AddEntry(fNorm, str); leg->Draw(); } void exponential(int nSum = 10) { int nGen = 1e6; // počet generovaných čísel y = sum x_i // exponenciální (lambda = 1/mu_x): double mu_x = 2.0; double sigma_x = mu_x; double mu_y = nSum * mu_x; double sigma_y = sqrt(nSum) * sigma_x; TRandom3* rnd = new TRandom3(); TH1D* histy = new TH1D("histy",";y", 200, mu_y - 4*sigma_y, mu_y + 4*sigma_y); histy->SetLineWidth(2); for (int j = 0; j < nGen; j++) { double X = 0; // = sum x_i, i = 0, 1, ..., nSum-1 for (int i = 0; i < nSum; i++) X += rnd->Exp(mu_x); histy->Fill(X); } TF1* fNorm = new TF1("fNorm","1 / (TMath::Sqrt(2*TMath::Pi())*[2]) * gaus", mu_y - 4*sigma_y, mu_y + 4*sigma_y); fNorm->SetParameter(0,nGen*histy->GetBinWidth(1)); // 1. parametr = normalizace fNorm->SetParameter(1,mu_y); // 2. parametr = mu Gausiánu fNorm->SetParameter(2,sigma_y); // 3. parametr = sigma Gausiánu gStyle->SetOptStat(0); // neukazuj tabulku entries, mean, RMS histy->Draw(); fNorm->Draw("same"); TLegend* leg = new TLegend(0.6,0.75,0.9,0.9); char str[256]; sprintf(str,"Sumy %i #it{x} #in exp(#it{#tau} = %.1f)",nSum,mu_x); leg->AddEntry(histy, str); sprintf(str,"%.2e N(%.2f, %.2f)",nGen*histy->GetBinWidth(1),mu_y,sigma_y); leg->AddEntry(fNorm, str); leg->Draw(); } void cauchy(int nSum = 10) { int nGen = 1e6; // počet generovaných čísel y = sum x_i // cauchyho = Breitovo-Wignerovo (střed v 0, pološířka 1): double mu_x = 0.0; double sigma_x = 1.0; // skutečný rozptyl je nekonečný double mu_y = nSum * mu_x; // double sigma_y = sqrt(nSum) * sigma_x; double sigma_y = nSum * sigma_x; TRandom3* rnd = new TRandom3(); TH1D* histy = new TH1D("histy",";y", 200, mu_y - 4*sigma_y, mu_y + 4*sigma_y); histy->SetLineWidth(2); for (int j = 0; j < nGen; j++) { double X = 0; // = sum x_i, i = 0, 1, ..., nSum-1 for (int i = 0; i < nSum; i++) X += rnd->BreitWigner(mu_x,sigma_x); histy->Fill(X); } TF1* fNorm = new TF1("fNorm","1 / (TMath::Sqrt(2*TMath::Pi())*[2]) * gaus", mu_y - 4*sigma_y, mu_y + 4*sigma_y); fNorm->SetParameter(0,nGen*histy->GetBinWidth(1)); // 1. parametr = normalizace fNorm->SetParameter(1,mu_y); // 2. parametr = mu Gausiánu fNorm->SetParameter(2,sigma_y); // 3. parametr = sigma Gausiánu gStyle->SetOptStat(0); // neukazuj tabulku entries, mean, RMS histy->Draw(); fNorm->Draw("same"); TLegend* leg = new TLegend(0.6,0.75,0.9,0.9); char str[256]; sprintf(str,"Sumy %i #it{x} #in Cauchy(#it{l} = %.1f)",nSum,sigma_x); leg->AddEntry(histy, str); sprintf(str,"%.2e N(%.2f, %.2f)",nGen*histy->GetBinWidth(1),mu_y,sigma_y); leg->AddEntry(fNorm, str); leg->Draw(); } void upf_cv7_clt() { printf("Vyberte druh rozdělení:\n"); printf("\tU rovnoměrné U(0,1)\n"); printf("\tE exponenciální, tau = 2\n"); printf("\tC Cauchyho, l = 1\n"); char sel = 'u'; scanf("%1c%*c",&sel); printf("Vyberte počet sečtených čísel: "); int N = 1; scanf("%i%*c",&N); if (sel == 'U' || sel == 'u') uniform(N); else if (sel == 'E' || sel == 'e') exponential(N); else if (sel == 'C' || sel == 'c') cauchy(N); else printf("Neplatná volba '%c'", sel); }