keisoku

Runge-Kutta Method (RK4, 2nd-Order ODE)

Solve y''=f(x,y,y') numerically with the 4th-order Runge-Kutta method. Enter the initial conditions and step size to get a table of x, y, y' at each step plus a graph of the solution curve.

Input

y''=

Examples (tap to fill the RHS and initial conditions)

Simple harmonic y''=-y
Damped oscillation y''=-0.2v-y
Free fall y''=-9.8
Forced oscillation y''=-y+sin(x)

Variables: x (independent), y (solution), v (=y'; you can also write dy / yp). 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.

Result

Solution y at x = 10

y ≒ -0.839075

Here y' ≈ 0.544014

-1-0.500.51012345678910

Steps

100 steps

101 points

Step size h

0.1

Δx = h

Initial conditions

y(0) = 1

y'(0) = 0

Range of y

-0.9997 〜 1

across all steps


Numerical solution (x, y, y')

Stepxyy' (=v)
0010
70.70.764843-0.644217
141.40.169968-0.985449
212.1-0.504845-0.86321
292.9-0.970957-0.239252
363.6-0.896760.442518
434.3-0.4008020.916164
5050.2836580.958925
575.70.834710.550689
646.40.993185-0.116544
717.10.684551-0.728965
797.9-0.045996-0.998941
868.6-0.678714-0.734402
939.3-0.992224-0.124462
10010-0.8390750.544014

Result of rewriting y' = -y as a system with v = y' and integrating with the 4th-order Runge-Kutta method.

How it works

  • Converts the second-order ODE y''=f(x,y,y') into the first-order system y'=v, v'=f(x,y,v) and solves it numerically with the classic 4th-order Runge-Kutta method (RK4).
  • Enter the expression for f(x,y,v) (where v denotes the first derivative y'), the initial values x0, y0, y'0 (=v0), the step size h, and the end point xend.
  • At each step, four slopes k1 through k4 are computed for both y and v, and a weighted average gives the next values. This is more accurate than second-order methods or Euler's method.
  • The endpoint values of y (and y') are shown as the main result, the per-step (x, y, y') values appear in a table, and the solution y is drawn as an SVG graph.
  • Smaller step sizes h improve accuracy but require more steps. The expression is parsed with a custom parser that does not use eval.
  • Useful for simulating phenomena described by second-order ODEs such as oscillation, damping, and spring motion.