keisoku

Runge-Kutta Method (RK4, 1st-Order ODE)

Enter the right-hand side of dy/dx=f(x,y) plus the initial values x0 and y0, the step size h, and the end point xEnd to solve the ODE numerically with the classic 4th-order Runge-Kutta method (RK4). Shows the approximate solution y at the end point, a table of k1–k4 and y at each step, and a graph of the solution curve.

Input

dy/dx =

Example right-hand sides (tap to insert)

y
x + y
x - y
-2*y
x*y
cos(x) - y

Operators: + - * / ^ (power). Functions: sin cos tan asin acos atan sinh cosh tanh exp log (natural log) ln log10 sqrt cbrt abs pow(a,b). Constants: pi (π), e. Variables x and y (and y'=v) are available. Trigonometric angles are in radians.

Result

Approximate solution y at x = 1

y ≒ 2.71828

dy/dx = y

11.522.5300.10.20.30.40.50.60.70.80.91

Steps

10

Number of times the interval was divided

Step size h

0.1

Interval 0 → 1

Initial condition

y(0) = 1

Value of y at x0


Solution at each step (k1–k4 are the slopes used to find the next point)

nxyk1k2k3k4
00111.051.05251.1053
10.11.1051711.10521.16041.16321.2215
20.21.2214031.22141.28251.28551.35
30.31.3498581.34991.41741.42071.4919
40.41.4918241.49181.56641.57011.6488
50.51.6487211.64871.73121.73531.8222
60.61.8221181.82211.91321.91782.0139
70.72.0137522.01382.11442.11952.2257
80.82.225542.22552.33682.34242.4598
90.92.4596012.45962.58262.58872.7185
1012.71828

How it works

  • This tool numerically solves the first-order ordinary differential equation dy/dx=f(x,y) with the classic 4th-order Runge-Kutta method (RK4). Enter the right-hand side f(x,y), the initial values x0 and y0, the step size h, and the end point xEnd to obtain the approximate value of y at the end point.
  • RK4 uses four slopes at each step. It computes k1=f(x,y), k2=f(x+h/2, y+h·k1/2), k3=f(x+h/2, y+h·k2/2), and k4=f(x+h, y+h·k3), then advances to the next point with y(next)=y+(h/6)·(k1+2·k2+2·k3+k4).
  • The smaller the step size h, the smaller the error: RK4 is a high-accuracy method whose error decreases in proportion to h to the fourth power. Because a smaller h means more steps, choose h with the computational cost in mind.
  • When the end point xEnd is smaller than the initial x0, the integration runs backward (with h taken in the negative direction). Any remainder in the interval is adjusted automatically in the final step so the calculation reaches exactly xEnd.
  • The right-hand side may use the variables x and y, the operators + - * / and the power ^, functions such as sin, cos, tan, exp, log, ln, sqrt, abs, and pow(a,b), and the constants pi and e (angles in radians). For example, dy/dx=y with the initial value y(0)=1 has the solution e^x, giving about 2.71828 at x=1.
  • The x and y values and the slopes k1–k4 at each step are shown in a table, and the curve connecting the solution points is drawn on a graph. If the value diverges or leaves the domain partway through, review the step size or the equation. Your input and results are never sent anywhere and are processed entirely in your browser.