/*RLC.cpp -- An application that calculates RLC circuit voltage as function of frequency*/ /*WARNING. This is a root application and cannot be compiled with a standard c++ compiler, as it uses root classes and does not contain a int main() function. It can be executed by loading and executing in the CINT interpreter*/ //included modules #include #include #include #include #include #include //namespaces using namespace std; //prototypes char * MixStringInt(string name,float num); void FillRandom(int N, int spr,float scaling, float * array); void CalculateVoltage(int N,float Res, float L, float C, float Vinn,float *w, float * Varr); void RLC() { const int N = 100; //discretisation steps const int M = 5; //number of random circuits //scaling factors for random generator float Cscaling = 1e-11; float Lscaling = 1e-5; float Rscaling = 1e1; float Vscaling = 1.0; //common spread int spread = 500; //Frequency parameters float wmin = 100000; float wmax = 4000000; float wstep = (wmax - wmin) / float(N); //discrete frequency stepsize //seeding the randomgenerator with the system clock. srand((unsigned)time(0)); //making array of M random elements with physical quanitities float randCs[M]; FillRandom(M, spread, Cscaling,randCs); float randLs[M]; FillRandom(M, spread, Lscaling,randLs); float randRs[M]; FillRandom(M, spread, Rscaling,randRs); float randVs[M]; FillRandom(M, spread, Vscaling, randVs); //defining frequency array float w [N]; //defining output voltage array float Varr [N]; //Some good circuit data float Res = 100.0; float L = 1e-3; float C = 1e-9; float Vinn = 10; //Initializing frequency array w[0] = wmin; for (int i = 0; i < N; i++) w[i] = w[0] + i*wstep; //Calculating good circuit voltage, stored in Varr CalculateVoltage(N,Res,L,C,Vinn,w,Varr); // main cuircuit //histogram declaration for good circuit TCanvas *c1 = new TCanvas("c1","The good circuit",200,10,700,500); c1->SetGrid(); //setting grid //making a plot object TGraph * plot1 = new TGraph(N,w,Varr); //specifing plot properties plot1->SetMarkerColor(kBlue); plot1->SetMarkerStyle(21); plot1->GetXaxis()->SetTitle("Frequency"); plot1->GetYaxis()->SetTitle("Voltage over the resistor"); plot1->SetLineWidth(0); //drawing plot plot1->Draw("ACP"); //adding text with root TPaveText object: TPaveText *pt = new TPaveText(0.6,0.7,0.98,0.98,"brNDC"); //making strings for physical quanitites char * labelstring = MixStringInt("Resistance: ",Res); pt->AddText(labelstring); labelstring = MixStringInt("Capasitance: ", C); pt->AddText(labelstring); labelstring = MixStringInt("Inductance: ", L); pt->AddText(labelstring); float expres = 1. / sqrt(L*C); labelstring = MixStringInt("Expected Resonance: ", expres); pt->AddText(labelstring); //drawing TPavetext object pt->Draw("c1"); //updating canvas for good circuit c1->Update(); //doing some random circuits //making pointers for root objects, each with M elements capacity TCanvas *cs[M]; TGraph * plots[M]; TPaveText *pts[M]; //char variable to hold canvas identifier char name[2]; //looping over M random circuits for (int j = 0; j < M; j++) { //calculating the voltage versus frequency curve for the j-th circuit CalculateVoltage(N,randRs[j],randLs[j],randCs[j],randVs[j],w,Varr); //modifying canvas identifier name[0] = 'c'; name[1] = (char)(j+2); //Constructing canvas object and TPaveText object cs[j] = new TCanvas(name,"Random circuit",200,10,700,500); pts[j] = new TPaveText(0.6,0.7,0.98,0.98,"brNDC"); cs[j]->SetGrid(); //making plot as above for the j-th circuit plots[j] = new TGraph(N,w,Varr); plots[j]->SetMarkerColor(kBlue); plots[j]->SetMarkerStyle(21); plots[j]->GetXaxis()->SetTitle("Frequency"); plots[j]->GetYaxis()->SetTitle("Voltage over the resistor"); plots[j]->SetLineWidth(0); plots[j]->Draw("ACP"); //making plottext as above for the j-th circuit labelstring = MixStringInt("Resistance: ",randRs[j]); pts[j]->AddText(labelstring); labelstring = MixStringInt("Capasitance: ", randCs[j]); pts[j]->AddText(labelstring); labelstring = MixStringInt("Inductance: ", randLs[j]); pts[j]->AddText(labelstring); expres = 1. / sqrt(randLs[j]*randCs[j]); labelstring = MixStringInt("Expected Resonance: ", expres); pts[j]->AddText(labelstring); pts[j]->Draw(name); cs[j]->Update(); } } //Method that combines a string and a float numer char * MixStringInt(string name,float num) { stringstream out; out << num; string intstring = out.str(); string labelstring = name + intstring; return (char*)labelstring.c_str(); } /*Method that calculates an entire voltage array based on the quantities C,L,Res,Vinn. N is the number of discrete points, w is a pointer to the frequency array. Varr is a pointer to the output array. NOTE: This function does not return the array but uses a buffer*/ void CalculateVoltage(int N,float Res, float L, float C, float Vinn, float * w, float * Varr) { float Xl; float Xc; float Im; //Looping over discrete frequency steps for (int i = 0; i < N; i++) { //calculating reactance Xl = w[i] * L; Xc = 1 / (w[i] * C); //Calculating current amplitude Im = Vinn / (sqrt(pow(Res,2) + pow((Xl - Xc),2))); //Calculating Voltage amplitude as a function of wi, where wi //is the discrete frequency step Varr[i] = Im * Res; } } /*Method to make M elements array wih random numbers scaled by scaling. Spread is spr orders of magnitude above scaling */ void FillRandom(int N,int spr,float scaling,float * array) { for (int i = 0; i < N; i++) { array[i] = (((rand() % spr)+1)*scaling); } }