ODE Solver (Euler & RK4)

Numerically integrate first-order differential equations from initial conditions

Parameters

ⓘ
ⓘ
sⓘ
sⓘ
sⓘ
1/sⓘ
m/s²ⓘ
Show Trail

Controls

xⓘ

Calculated Values

Euler y(tf):
21.81;21.81;
RK4 y(tf):
22.31;22.31;
Exact y(tf):
22.31;22.31;
Euler error:
0.51;0.51;
RK4 error:
0.00;0.00;

Examples

Decay N₀=100, λ=0.3, t=5 s

Compare Euler and RK4 to N₀e^(−λt).

  • Exact: 22.3122.31
  • RK4: 22.3122.31

Velocity v₀=0, a=2 m/s², t=3 s

Analytic v = at.

  • v: 6.006.00

Visualization

Solving Physics ODEs Numerically

Differential equations describe how quantities change: radioactive decay dN/dt = −λN, cooling dT/dt = −k(T−T₀), and velocity dv/dt = a under constant acceleration. When analytic solutions are inconvenient, numerical methods step forward in small time increments Δt.

Euler’s method is the simplest: yₙ₊₁ = yₙ + h·f(tₙ, yₙ), where h is the step size and f is the right-hand side of dy/dt = f. Local truncation error is O(h²); global error is O(h) — accuracy improves by shrinking h.

The fourth-order Runge–Kutta (RK4) method evaluates the slope at four points per step and combines them: yₙ₊₁ = yₙ + (h/6)(k₁ + 2k₂ + 2k₃ + k₄). It is the workhorse of computational physics for smooth ODEs.

Second-order equations (e.g. SHM d²x/dt² = −ω²x) are written as two first-order equations: v = dx/dt and dv/dt = −ω²x. Compare numerical output to Simple Harmonic Motion or Radioactive Decay analytic formulas.

Stability matters: Euler can diverge if h is too large for oscillatory systems. RK4 allows larger steps but is not a substitute for understanding the physics.

Key Concepts

  • Initial value problem: y(t₀) = y₀ given
  • Euler: first-order accurate globally
  • RK4: fourth-order accurate, much smaller error for same h
  • Step size h trades accuracy vs speed
  • Second-order ODE → system of two first-order ODEs

Real-World Applications

  • Exponential decay and radioactivity
  • Constant-acceleration kinematics
  • RC circuit charging curves
  • Population and cooling models

Explore Further

More computational physics tools

  • Gradient Descent

    Iteratively minimize f(x) by following the negative gradient with animated path visualization.

  • 1D Heat Equation

    Finite-difference FTCS solution to the diffusion equation with animated temperature profiles.

  • 1D Wave Equation

    Leapfrog finite-difference solution to the wave equation with animated wave propagation.

  • Numerical Integration

    Trapezoidal and Simpson rules to approximate definite integrals with error vs exact solutions.

  • Monte Carlo Intro

    Estimate π and integrals by random sampling — introduction to stochastic computational physics.

  • Newton-Raphson

    Solve nonlinear equations f(x) = 0 with tangent-line iterations — fast when the guess is good.

Physics Equations

Euler:
yn+1=yn+hf(tn,yn)y_{n+1} = y_n + h f(t_n, y_n)
RK4:
yn+1=yn+h6(k1+2k2+2k3+k4)y_{n+1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)
Decay exact:
y(t)=y0e−kty(t) = y_0 e^{-kt}

Step-by-Step Solution

See how the main results are calculated.

1

Step 1: Write the ODE and initial condition

Exponential decay: rate proportional to current amount.

Equation:

dydt=−ky,y(t0)=y0\frac{dy}{dt} = -k y, \quad y(t_0) = y_0

Result:

y0=100att0=0s,k=0.3s−1y_0 = 100 at t_0 = 0 s, k = 0.3 s⁻¹
2

Step 2: Analytic solution

Separate variables and integrate.

Equation:

y(t)=y0e−k(t−t0)y(t) = y_0 e^{-k(t - t_0)}

Calculation:

y(5)=100×e−0.3×5=22.313016y(5) = 100 \times e^{-0.3 \times 5} = 22.313016

Result:

yexact(5)=22.313016y_{\text{exact}}(5) = 22.313016
3

Step 3: Choose numerical step size

Divide interval into N equal steps of size h.

Equation:

N=⌊(tf−t0)/h⌋N = \lfloor (t_f - t_0) / h \rfloor

Calculation:

N=⌊(5−0)/0.1⌋=50N = \lfloor (5 - 0) / 0.1 \rfloor = 50

Result:

50stepsfromt=0tot≈5.00s50 steps from t = 0 to t ≈ 5.00 s
4

Step 4: Euler iteration

Update: y_{n+1} = y_n + h(−k y_n).

Equation:

yn+1=yn+hf(tn,yn)y_{n+1} = y_n + h f(t_n, y_n)

Calculation:

After 50 steps: yEuler=21.806538\text{After 50 steps: } y_{\text{Euler}} = 21.806538

Result:

Euler: 21.806538 (error 0.5065)

Explanation:

First-order method; error decreases when h is reduced.

5

Step 5: RK4 iteration

Weighted average of four slope estimates per step.

Equation:

yn+1=yn+h6(k1+2k2+2k3+k4)y_{n+1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)

Calculation:

yRK4=22.313016y_{\text{RK4}} = 22.313016

Result:

RK4: 22.313016 (error 0.0000)

Explanation:

RK4 is usually much closer to exact for the same h.

6

Step 6: Compare methods

Result:

Exact=22.313016,Euler=21.806538,RK4=22.313016Exact = 22.313016, Euler = 21.806538, RK4 = 22.313016

Explanation:

Halve h and repeat until Euler and RK4 agree with each other and with exact.

Frequently Asked Questions (FAQ)

When should I use RK4 over Euler?

RK4 is preferred for smooth ODEs when accuracy matters; Euler is simpler but needs smaller h.

What step size h should I choose?

Start with h = (tf−t0)/100, halve h until Euler and RK4 agree with each other and the analytic solution.

Practice MCQs

  1. Smaller step size h generally:
  2. For dN/dt = −λN, the exact solution is:
  3. RK4 is preferred over Euler because:
  4. Euler’s method is equivalent to:
  5. Constant acceleration dv/dt = a gives: