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
Example right-hand sides (tap to insert)
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
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)
| n | x | y | k1 | k2 | k3 | k4 |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1.05 | 1.0525 | 1.1053 |
| 1 | 0.1 | 1.105171 | 1.1052 | 1.1604 | 1.1632 | 1.2215 |
| 2 | 0.2 | 1.221403 | 1.2214 | 1.2825 | 1.2855 | 1.35 |
| 3 | 0.3 | 1.349858 | 1.3499 | 1.4174 | 1.4207 | 1.4919 |
| 4 | 0.4 | 1.491824 | 1.4918 | 1.5664 | 1.5701 | 1.6488 |
| 5 | 0.5 | 1.648721 | 1.6487 | 1.7312 | 1.7353 | 1.8222 |
| 6 | 0.6 | 1.822118 | 1.8221 | 1.9132 | 1.9178 | 2.0139 |
| 7 | 0.7 | 2.013752 | 2.0138 | 2.1144 | 2.1195 | 2.2257 |
| 8 | 0.8 | 2.22554 | 2.2255 | 2.3368 | 2.3424 | 2.4598 |
| 9 | 0.9 | 2.459601 | 2.4596 | 2.5826 | 2.5887 | 2.7185 |
| 10 | 1 | 2.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.
Related calculators
Function Graph Plotter f(x)
NumericalType an expression such as sin(x) or x^2-2 and instantly plot its graph over a chosen x range. Maximum, minimum, zeros, and sample point values are shown alongside.
Open calculator →Graph Two Functions f(x) and g(x)
NumericalPlot two expressions f(x) and g(x) on the same axes in distinct colors, and automatically find their max, min, and intersection points.
Open calculator →Data Point f(x) Graph Plotter
NumericalJust paste your (x, y) data points to instantly draw a line-and-point chart. See the point count plus the minimum, maximum, and average of y at the same time.
Open calculator →Plot Data Points f(x), g(x)
NumericalPaste two sets of (x, y) data points and get f(x) and g(x) drawn as color-coded overlaid line graphs, with each series' point count and min/max.
Open calculator →