Runge Kutta 2 Calculator
Solve an ODE with Heun's second order method.
What it does
Runge Kutta methods improve on Euler's method by sampling the right hand side of a differential equation several times within each step. The second order version, often called Heun's method, uses an Euler trial step to estimate the slope at the far end and then averages the two slopes.
Florida State University numerical methods notes explain that each Runge Kutta step uses trial evaluations of the right hand side, reducing local truncation error. A method of order two has global error that shrinks like the step size squared.
Middlebury College differential equations notes give the Heun update and list related second order choices such as midpoint and Ralston, which differ in how the two samples are weighted.
This tool offers three simple equations, an initial condition, a step size and a step count. It reports the final x and y values.
Heun's method is a good middle ground between the simplicity of Euler and the cost of fourth order Runge Kutta.
How to use
- Choose a built in differential equation.
- Enter the initial x and y values.
- Enter a positive step size.
- Enter the number of steps, up to 1000.
- Read the final y value in the main result.
- Compare with Euler's method on the same inputs.
Formula
- f(x,y)
- Right hand side of dy/dx = f(x,y).
- k1
- Slope at the start of the step.
- k2
- Slope estimated at the end using an Euler trial value.
- h
- Step size.
The engine evaluates the chosen right hand side twice per step. It keeps the step size constant and reports the final values to eight decimal places.
Worked example
Example: dy/dx = y
Initial values x = 0, y = 1, step size 0.1, 10 steps.
- At the start, k1 = 1 and the Euler trial gives y trial = 1.1 at x = 0.1.
- The second slope is k2 = 1.1.
- The average slope is 1.05, giving y = 1 + 0.1 x 1.05 = 1.105 after one step.
- Repeating across 10 steps gives y about 2.71408085 at x = 1.
- The exact solution is e, about 2.71828, so the error is smaller than Euler's 2.59374.
- The tool reports final y 2.71408085.
Reference table
| Method | Estimate | Error vs e |
|---|---|---|
| Euler | 2.59374246 | about 0.1245 |
| Heun RK2 | 2.71408085 | about 0.0042 |
| Exact | 2.71828183 | 0 |
Limits
- Only three built in equations are available.
- The method has second order accuracy.
- Very stiff problems may still need tiny steps.
- Results are shown to eight decimal places.
Common mistakes
- Using the Euler slope for the second sample instead of the trial endpoint.
- Forgetting to average the two slopes.
- Expecting fourth order accuracy from a two stage method.
- Using a step size too large for the desired precision.
- Comparing methods with different step sizes.
Try it yourself
Quick checks. Answers sit tucked under each question.
What is another name for RK2?
Heun's method or the improved Euler method.
What is the Heun update?
y next = y + h(k1 + k2)/2.
How does its accuracy compare with Euler?
It is second order rather than first order.
Questions
What is Runge Kutta 2?
A two stage numerical method for ordinary differential equations.
Why sample twice?
To estimate the slope change across the step and reduce error.
Is it the same as midpoint?
No. Midpoint uses a different weighting of the two samples.
What is its order?
Second order.
Can I use my own equation?
Not on this page. Three built in options are provided.
When is RK4 preferable?
When higher accuracy per step is worth the extra evaluations.