Skip to content
Tactiverge

✦ Numerical methods · error analysis

How accurate is your solver?

Compare Euler and fourth-order Runge–Kutta on exponential decay against the exact answer.

  • Level 5 · Research Engineer
  • 7 tests
  • technically checked

Your task

A capacitor discharging through a resistor obeys dy/dt = −k·y, with exact solution y(t) = y₀·e^(−kt). That makes it perfect for testing solvers.

Write, each returning the value after steps steps of size dt:

  • eulerDecay(k, y0, dt, steps): y = y + dt × (−k·y)
  • rk4Decay(k, y0, dt, steps): with f(y) = −k·y, each step computes k1 = f(y), k2 = f(y + dt/2·k1), k3 = f(y + dt/2·k2), k4 = f(y + dt·k3), then y = y + dt/6 × (k1 + 2·k2 + 2·k3 + k4)
  • relativeError(approx, exact): |approx − exact| / |exact|

Hints (0 of 3 shown)

    What gets checked

    • eulerDecay(1, 1, 0.1, 10) → ≈ 0.3486784401 (± 1e-9)
    • rk4Decay(1, 1, 0.1, 10) → ≈ 0.3678797744 (± 1e-9)
    • relativeError(1.1, 1) → ≈ 0.1 (± 1e-12)

    Plus 4 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.

    Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments

    Starting the sandbox…

    Press Run to see what your program prints.

    Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.