programming
rung-kutta

・Runge-Kutta法

微分方程式が

dx/dt = -x + tsin(t)

のとき


/* Differential Equation : F(t,x) */
double f(double t,double x){
double y;
y = -x + t*sin(t);
return y;
}

————————————————————————————————

/* Runge-Kutta Function */

double runge_kutta(double x0){      // x0:Initial Value
double h = 0.01;    // Step Size
double k1,k2,k3,k4,x,nx,T;

// Initial Value
x = x0;

// Runge-Kutta Method
for(T = 0.0;T < 50;T += h){
k1 = f(T,x);
k2 = f(T+h/2,x+(h/2)*k1);
k3 = f(T+h/2,x+(h/2)*k2);
k4 = f(T+h,x+h*k3);
nx = x + h*(k1+2*k2+2*k3+k4)/6;
x = nx;
}
return x;
}