with
¨y = ddt˙y = ddtf(t,y) = ft+fy˙yand hence
y(t+h)−y(t) = Δy ≈ h˙y+h22(ft+fyf)with
k1=f(t,y),k2=f(t+h2,y+hk12)and
k3=f(t+h2,y+hk22),k4=f(t+h,y+hk3)
- #include <iostream>
- #include <math.h> // for pow
- #include <fstream> // file streams
- using namespace std;
- const int nVar = 2; // number of dynamical variables
- const int nTimeSteps = 9000; // number of time steps
- const double h = 0.01; // Delta t
- double vars[nVar]; // the dynamical variables
- // ---
- // --- evaluate the i-th differential equation
- // ---
- double evaluate(int i, double t, double x[])
- {
- switch (i)
- {
- case 0: return x[1]; // x' = v
- case 1: return -x[0]+0.2*(1.0-x[0]*x[0])*x[1]; // v' = -x+0.2*(1-x*x)*v
- case 2: return 0.0; // van der Pol oscillator
- case 3: return 0.0;
- default:
- cout << "throw? problem in evaluate" << endl;
- return 0.0;
- }
- } // end of evaluate()
- // ---
- // --- solve via Runge Kutta; t = last time value; h = time increment
- // ---
- void solve(double t, double h)
- {
- int i;
- double inp[nVar];
- double k1[nVar];
- double k2[nVar];
- double k3[nVar];
- double k4[nVar];
- for (i=0; i<nVar; i++)
- k1[i] = evaluate(i,t,vars); // evaluate at time t
- //
- for (i=0; i<nVar; i++)
- inp[i] = vars[i]+k1[i]*h/2; // set up input to diffeqs
- for (i=0; i<nVar; i++)
- k2[i] = evaluate(i,t+h/2,inp); // evaluate at time t+h/2
- //
- for (i=0; i<nVar; i++)
- inp[i] = vars[i]+k2[i]*h/2; // set up input to diffeqs
- for (i=0; i<nVar; i++)
- k3[i] = evaluate(i,t+h/2,inp); // evaluate at time t+h/2
- //
- for (i=0; i<nVar; i++)
- inp[i] = vars[i]+k3[i]*h; // set up input to diffeqs
- for (i=0; i<nVar; i++)
- k4[i] = evaluate(i,t+h,inp); // evaluate at time t+h
- for (i=0; i<nVar; i++)
- vars[i] = vars[i]+(k1[i]+2*k2[i]+2*k3[i]+k4[i])*h/6;
- } //end of solve
- // ---
- // --- main
- // ---
- int main()
- {
- for (int i=0; i<nVar; i++) // initial conditions
- vars[i] = 0.0;
- vars[0] = 0.1;
- vars[1] = 0.1;
- //
- ofstream myOutput;
- myOutput.open("RungeKutta.out",ios::out);
- myOutput.setf(ios::fixed,ios::floatfield); // for formated output
- myOutput.precision(3); // floating point precision
- for (int iTimes=0;iTimes<nTimeSteps;iTimes++)
- {
- double t = h*iTimes;
- solve(t,h);
- myOutput << t << " " << vars[0] << " " << vars[1] << endl;
- }
- myOutput.close();
- return 1;
- } // end of main()
- #include <iostream>
- #include <stdio.h> // for printf
- #include <math.h> // for pow
- #include <fstream> // file streams
- using namespace std;
- const int nVar = 2; // number of dynamical variables
- const int nTimeSteps = 800; // number of time steps
- const double h = 0.01; // Delta t
- double y_0[nVar]; // the dynamical variables
- double y_1[nVar]; // of (n-1)
- double y_2[nVar]; // of (n-2)
- double f_0[nVar]; // the flow
- double f_1[nVar]; // of (n-1)
- double f_2[nVar]; // of (n-2)
- // ---
- // --- standard stuff
- // ---
- void zeroGlobalVariables()
- {
- for (int i=0;i<nVar;i++)
- {
- y_0[nVar] = 0.0;
- f_0[nVar] = 0.0;
- y_1[nVar] = 0.0;
- f_1[nVar] = 0.0;
- y_2[nVar] = 0.0;
- f_2[nVar] = 0.0;
- }
- } // end of zeroVariables()
- void shiftGlobalVariables()
- {
- for (int i=0;i<nVar;i++)
- {
- y_2[i] = y_1[i];
- f_2[i] = f_1[i];
- y_1[i] = y_0[i];
- f_1[i] = f_0[i];
- }
- } // end of shiftVariables()
- // ---
- // --- evaluate the flow : \dot y = f(y)
- // ---
- void evaluate(double y[nVar], double f[nVar])
- {
- f[0] = y[1]; // \dot x = y : for circle
- f[1] = -y[0]; // \dot y = -x : for circle
- //
- f[0] = -y[0]; // \dot x = -x : for exp(-t)
- f[1] = 0.0;
- } // end of evaluate()
- // ---
- // --- main
- // ---
- int main()
- {
- // *** ***************** ***
- // *** Euler integration ***
- // *** ***************** ***
- zeroGlobalVariables();
- y_0[0] = 1.0;
- y_0[1] = 0.0;
- //
- ofstream myOutput;
- myOutput.open("integrate_Euler.dat",ios::out);
- myOutput.setf(ios::fixed,ios::floatfield); // for formated output
- myOutput.precision(3); // floating point precision
- for (int iTimes=0;iTimes<nTimeSteps;iTimes++)
- {
- double t = h*iTimes;
- myOutput << t << " " << y_0[0] << " " << y_0[1] << endl;
- evaluate(y_0,f_0);
- y_0[0] += f_0[0]*h;
- y_0[1] += f_0[1]*h;
- }
- myOutput.close();
- // *** **************** ***
- // *** leap frog scheme ***
- // *** **************** ***
- zeroGlobalVariables();
- y_2[0] = 1.0;
- y_2[1] = 0.0;
- evaluate(y_2,f_2);
- y_1[0] = y_2[0] + f_2[0]*h;
- y_1[1] = y_2[1] + f_2[1]*h;
- //
- myOutput.open("integrate_leapFrog.dat",ios::out);
- myOutput.setf(ios::fixed,ios::floatfield); // for formated output
- myOutput.precision(3); // floating point precision
- for (int iTimes=0;iTimes<nTimeSteps;iTimes++)
- {
- double t = h*iTimes;
- myOutput << t << " " << y_2[0] << " " << y_2[1] << endl;
- evaluate(y_1,f_1);
- y_0[0] = y_2[0] + 2.0*h*f_1[0];
- y_0[1] = y_2[1] + 2.0*h*f_1[1];
- shiftGlobalVariables();
- }
- myOutput.close();
- // *** ********************** ***
- // *** Adams Bashforth scheme ***
- // *** ********************** ***
- zeroGlobalVariables();
- y_2[0] = 1.0;
- y_2[1] = 0.0;
- evaluate(y_2,f_2);
- y_1[0] = y_2[0] + f_2[0]*h;
- y_1[1] = y_2[1] + f_2[1]*h;
- //
- myOutput.open("integrate_Adams.dat",ios::out);
- myOutput.setf(ios::fixed,ios::floatfield); // for formated output
- myOutput.precision(3); // floating point precision
- for (int iTimes=0;iTimes<nTimeSteps;iTimes++)
- {
- double t = h*iTimes;
- myOutput << t << " " << y_2[0] << " " << y_2[1] << endl;
- evaluate(y_1,f_1);
- y_0[0] = y_1[0] + 1.5*h*f_1[0] - 0.5*h*f_2[0];
- y_0[1] = y_1[1] + 1.5*h*f_1[1] - 0.5*h*f_2[1];
- shiftGlobalVariables();
- }
- myOutput.close();
- return 1;
- } // end of main()
Space, Right Arrow or swipe left to move to next slide, click help below for more details