#ifndef _CINT_ #include #include #include #include #endif #include #include #include #include #include #include "nr.h" using namespace std; #define PI 3.141592653589793 Vec_DP *xp_p; Mat_DP *yp_p; Mat_DP *fp_p; const int n_ord = 3; const double a = 8.0; // global parameter const double ya = 2.0; // initial condition double yex(double t) { // analytical solution of the ODE double a2; a2 = 1.0/(1.0+a*a); return( (ya - a2)*exp(-t) + a2*( cos(a*t) + a*sin(a*t) ) ); } void deriv(const DP t, Vec_I_DP &y, Vec_O_DP &f) // RHS of ODE { f[0] = cos(a*t) - y[0]; } void adamsbash(int k, Vec_IO_DP &y, const DP h, Vec_IO_DP xp, Mat_IO_DP yp, Mat_IO_DP fp, const int n_ord, Vec_O_DP &yout ) { const int NMAXORD = 5; static DP beta[NMAXORD][NMAXORD] = {{1.0, 0.0, 0.0, 0.0, 0.0}, {(3.0/2.0), (-1.0/2.0), 0.0, 0.0, 0.0}, {(23.0/12.0),(-16.0/12.0),(5.0/12.0), 0.0, 0.0}, {(55.0/24.0),(-59.0/24.0),(37.0/24.0),(-9.0/24.0), 0.0}, {(1901.0/720.0),(-2774.0/720.0),(2616.0/720.0),(-1274.0/720.0), (251.0/720.0)}}; int i,j; int nvar=y.size(); Vec_DP sum(nvar); if (n_ord > NMAXORD || n_ord < 1) NR::nrerror("Formula not present"); for (i=0; i end of starting procedure ..." << endl; // multistep method ( Adams-Bashforth, order ) for (j=n_ord; j analytical solution: " << endl; cout << "# j | t | yex(t) | f(t,yex(t)) " << endl; cout << "#======================================================= " << endl; tt = 0.0; for (int j=0;j numerical solution; n_ord = " << n_ord << endl; cout << "# j | t | yp(t) | f(t,yp(t)) " << endl; cout << "# ======================================================= " << endl; for (int j=0;j error: n_ord = " << n_ord << endl; cout << "# j | t | yp(t) | yex(t) | yp(t) - yex(t)"; cout << endl; cout << "# =======================================================" << endl; for (int j=0;jSetLineColor(2); TGraph *grph2=new TGraph(NN,t1,y2); grph2->SetLineColor(3); TGraph *grph3=new TGraph(NN,t1,y3); grph3->SetLineColor(2); TCanvas *c1 = new TCanvas("c1","Exercise 7.3",800, 400); c1->SetGrid(); c1->cd(); grph2->Draw(""); grph1->Draw("AC"); grph2->SetMarkerStyle(22); grph2->Draw("CP"); grph1->SetTitle("Ex. 7.3: y'(t) = cos(a*t) - y, y(0)=2"); c1->Update(); TCanvas *c2 = new TCanvas("c2","Exercise 7.3",800, 400); c2->SetGrid(); c2->cd(); grph3->Draw("AC"); grph3->SetTitle("Ex. 7.3: error [abs(y(t) - y_k)]"); c2->Update(); theApp.Run(); delete xp_p, yp_p, fp_p; return 1; }