// program to calculate the mass on a spring problem. // m=k=1; therfore a=-x // with the simple euler method // ak 9/7/00 // all in double precision #include #include #include #include #include #include "TROOT.h" #include "TApplication.h" #include "TCanvas.h" #include "TLine.h" #include "TFile.h" #include "TGraph.h" TROOT root("hello","Spring Problem"); // initalize root int main(int argc, char **argv) { double v_new[100]; // the forward value calculated double v_old=5.; // the previous value; for the first calculation // the boundary condition double x_new[100]; // the forward x-value double x_old=0.;// the x value from previous double energy[100]; // the energy from mv**2/2+ kx**2/2 double ti[100]; //array to hold the time double step =0.1; // the step size for the euler method double time=0. ;// the time int loop = 0; // the loop counter // first the root stuff TApplication theApp("App", &argc, argv); TCanvas *c = new TCanvas("c", "Hyperbola", 600, 600); //* create Canvas TFile *out_file = new TFile("spring.root","RECREATE","sping"); // create root //file // create the graphs // while (loop <100) { v_new[loop]=v_old-step*x_old; // calculate the forward velocity x_new[loop]=x_old+step*v_old; // calculate the forward position energy[loop]=pow(x_new[loop],2)/2+pow(v_new[loop],2)/2.; // energy conservation energy[loop]=energy[loop]/5. ;// rescaled for plotting purposes time=time+step; // move forward in time by the step v_old=v_new[loop]; // the new value becomes now the old x_old=x_new[loop]; ti[loop]=time; time=time+step; // move forward in time by the step loop=loop+1; } TGraph *root1 = new TGraph(100,ti,x_new); TGraph *root2 = new TGraph(100,ti,v_new); TGraph *root3 = new TGraph(100,ti,energy); root1->SetTitle("Spring with simple Euler Method"); root1->SetLineColor(1); root1->Draw("AC"); root1->Write(); root2->SetLineWidth(2); root2->SetLineColor(2); root2->Draw("C"); root2->Write(); root2->SetLineWidth(2); root3->SetLineColor(4); root3->Draw("C"); root3->Write(); // put everyhting into the file out_file->Write(); out_file->Close(); theApp.Run(); } // compile g++ -o spring spring.cxx wit root libraries