Solve first‑order ODEs dy/dx = f(x, y) using classical fixed-step Runge‑Kutta 4 (RK4). Visualize direction fields, compare with exact solutions, and explore real‑world applications: RC circuits, Newton’s law of cooling, logistic growth, and more.
An ordinary differential equation (ODE) relates a function to its derivatives. First‑order ODEs of the form dy/dx = f(x, y) describe countless phenomena: population growth, radioactive decay, cooling, circuit dynamics, and more. The direction field (slope field) visually represents the slope of the solution at every point, while numerical methods like Runge‑Kutta 4 (RK4) produce approximate solutions with high accuracy.
Runge‑Kutta 4th order (RK4) scheme:
k₁ = f(xₙ, yₙ)
k₂ = f(xₙ + h/2, yₙ + h·k₁/2)
k₃ = f(xₙ + h/2, yₙ + h·k₂/2)
k₄ = f(xₙ + h, yₙ + h·k₃)
yₙ₊₁ = yₙ + h·(k₁ + 2k₂ + 2k₃ + k₄)/6
This method achieves local truncation error O(h⁵) and global error O(h⁴), making it a standard choice for non‑stiff ODEs.
RK4 is derived from Taylor series expansion, combining four slope evaluations to cancel lower‑order error terms. Given the initial value problem y' = f(x,y), y(x₀)=y₀, the method approximates the next step with a weighted average of slopes at the start, midpoint (twice), and end. This provides a balance of accuracy and computational cost. The direction field is generated by evaluating f(x,y) at grid points and drawing short line segments with slope corresponding to f(x,y). The solution curve must be tangent to every segment it crosses, offering geometric insight into the ODE's behavior.
x - y, y*(1-y), Math.sin(x)).
Verified examples with known analytical solutions.
| ODE | Initial Condition | Solution Type | Behavior |
|---|---|---|---|
| dy/dx = x - y | y(0)=1 | Linear, exact: y = x - 1 + 2e⁻ˣ | Approaches line y = x - 1 as x → ∞ |
| dy/dx = 2y | y(0)=1 | Exponential growth | y = e²ˣ, unbounded |
| dy/dx = y(1-y) | y(0)=0.2 | Logistic growth | Approaches carrying capacity y=1 |
| dy/dx = cos(x) | y(0)=0 | Periodic | y = sin(x) |
| dy/dx = -0.5y | y(0)=3 | Exponential decay | y = 3e⁻⁰·⁵ˣ, tends to 0 |
The logistic equation dP/dt = rP(1 - P/K) is fundamental in ecology. Using our solver with r=1, K=1, initial P(0)=0.2, the solution curve shows the classic S‑shape, stabilizing at carrying capacity K. This matches empirical observations in microbiology and fisheries management. The direction field reveals two equilibrium solutions: P=0 (unstable) and P=1 (stable). Such analysis helps predict population limits under resource constraints.
RK4's global error scales with h⁴, meaning halving the step size reduces error by a factor of 16. For stiff ODEs (e.g., rapidly decaying components), explicit RK4 may require impractically small steps; in such cases, implicit methods are preferred. Our tool is ideal for non‑stiff, well‑behaved equations commonly encountered in introductory courses and engineering.
Global error ≈ C·h⁴. Halving h reduces error ~16×. For stiff ODEs (e.g., y' = -100y), RK4 may require extremely small h; consider using smaller step sizes and narrower domains. If you observe oscillations or NaN, reduce h immediately.
Math.sin(x), Math.exp(y) are allowed. If you see "Invalid function syntax", ensure you’re using Math.sin not sin, and check parentheses.