/* File: RK45.cpp Christer Karlsson This code implements The Runge-Kutta method to solve Ordinary Differential Equations. Allowed inputs are real numbers (long double). Define f in the function procedure */ #include #include #include // Needed too manipulate the display using namespace std; // the function long double f(long double t,long double x) { long double f; f=sqrt(1+pow(t,3)); return f; } // The Runge-Kutta method of order 4 function void RK45(long double h, long double t, long double x, int n) { int j; long double K1, K2, K3, K4, ta; cout << 0 << '\t' << t << "\t\t" << x << endl; ta=t; for (j=1; j<=n; j++) { K1=h*f(t,x); K2=h*f((t+h/2),(x+K1/2)); K3=h*f((t+h/2),(x+K2/2)); K4=h*f((t+h),(x+K3)); x=x+(K1+2*K2+2*K3+K4)/6; t=ta+j*h; cout << j << '\t' << t << "\t\t" << x << endl; } } int main(int argc, char *argv[]) { int n=36, digits=13; long double a=0, b=1, x=0; long double h, t; h=(b-a)/n; t=a; // Set a fixed amount of digits to show cout.setf(ios::fixed ); cout.setf(ios::showpoint); cout << setprecision(digits); // Call the integrating function RK45(h, t, x, n); // Unset the fixed point cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout << endl; system("PAUSE"); return 0; }