Runge-Kutta C ++ Algorithm

Below my 4th order Runge-Kutta algorithm solves first order ODEs. I test it with wikipedia as an example here to solve:

\frac{dx}{dt} = tan(x) + 1

Unfortunately, this is a bit. I walked around for a long time, but I can not find a mistake. The answer should be t = 1.1 and x = 1.33786352224364362. The code below gives t = 1.1 and x = 1.42223.

/*

This code is a 1D classical Runge-Kutta method. Compare to the Wikipedia page.

*/

#include <math.h> 
#include <iostream>
#include <iomanip>

double x,t,K,K1,K2,K3,K4;

const double sixth = 1.0 / 6.0;

static double dx_dt(double t, double x){
    return tan(x) + 1;
}

int main(int argc, const char * argv[]) {

/*======================================================================*/
/*===================== Runge-Kutta Method for ODE =====================*/
/*======================================================================*/

double t_initial = 1.0;// initial time
double x_initial = 1.0;// initial x position

double t_final = 1.1;// value of t wish to know x 
double dt = 0.025;// time interval for updates
double halfdt = 0.5*dt;

/*======================================================================*/

while(t_initial < t_final){

    /*============================ Runge-Kutta increments =================================*/ 

    double K1 = dt*dx_dt( t_initial, x_initial );
    double K2 = dt*dx_dt( t_initial + halfdt, x_initial + halfdt*K1 );
    double K3 = dt*dx_dt( t_initial + halfdt, x_initial + halfdt*K2 );
    double K4 = dt*dx_dt( t_initial + dt, x_initial + dt*K3 );

    x_initial += sixth*(K1 + 2*(K2 + K3) + K4);

    /*============================ prints =================================*/

    std::cout << t_initial << std::setw(16) << x_initial << "\n";

    /*============================ re-setting update conditions =================================*/

    t_initial += dt;

    /*======================================================================*/
}

std::cout<<"----------------------------------------------\n";
std::cout << "t =  "<< t_initial << ",  x =  "<< x_initial << std::endl; 


}/* main */
+4
source share
3 answers

The problem is that the table used for your code is different from the table you quoted on Wikipedia. What you use is:

0   |
1/2 |   1/2
1/2 |   0       1/2
1   |   0       0       1   
-------------------------------------
    |   1/6     1/3     1/3     1/6

And in wikipedia is used

0   |
2/3 |   2/3     
---------------------
    |   1/4     3/4

, , . , dt -> 0, .

, RK4. , 0.5*dt:

double K1 = dt*dx_dt( t_initial, x_initial );
double K2 = dt*dx_dt( t_initial + halfdt, x_initial + 0.5*K1 );
double K3 = dt*dx_dt( t_initial + halfdt, x_initial + 0.5*K2 );
double K4 = dt*dx_dt( t_initial + dt, x_initial + K3 );
+5

, .

k2 = dt*f(t+0.5*dt, x+0.5*k1)

k2 = f(t+0.5*dt, x+0.5*dt*k1)

k .

, f dt .

+4

, , . :

#include <math.h> 
#include <iostream>
#include <iomanip>

static double dx_dt(double t, double x)
{
    return tan(x) + 1;
}

int main(int argc, const char * argv[])
{
    double t = 1.0;
    double t_end = 1.1;

    double y = 1.0;
    double h = 0.025;

    std::cout << std::setprecision(16);

    int n = static_cast<int>((t_end - t) / h);

    for (int i = 0; i < n; i++)
    {
        double k1 = dx_dt(t, y);
        double k2 = dx_dt(t + h / 2.0, y + h*k1 / 2.0);
        double k3 = dx_dt(t + h / 2.0, y + h*k2 / 2.0);
        double k4 = dx_dt(t + h, y + h*k3);

        y += (k1 + 2 * k2 + 2 * k3 + k4) * h / 6.0;

        std::cout << t << ": " << y << std::endl;

        t += h;
    }

    std::cout << "----------------------------------------------\n";
    std::cout << "t =  " << t << ",  x =  " << y << std::endl;

    std::getchar();
}

, , . , , .

, . , .

+3

Source: https://habr.com/ru/post/1672551/


All Articles