Differential Equation Solver

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.

Supports natural math syntax: sin(x), cos(x), exp(x), log(x), sqrt(x)
Examples: x - y | y*(1-y) | sin(x) | -0.5*y | -0.3*(y-20)
Smaller h increases accuracy. Recommended range: 0.01-0.1
Linear: dy/dx = x - y, y(0)=1
Exponential: dy/dx = 2y, y(0)=1
Logistic: dy/dx = y(1-y), y(0)=0.2
Sinusoidal: dy/dx = cos(x), y(0)=0
Decay: dy/dx = -0.5y, y(0)=3
RC Circuit: dy/dx = -2y, y(0)=5
Cooling: dy/dx = -0.3(y-20), y(0)=80
Privacy & safety: All calculations are local. Uses math.js for secure expression parsing.

Understanding Ordinary Differential Equations

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.

Why Use an Interactive ODE Solver?

  • Intuitive Learning: Instantly see how the direction field shapes the solution curve. Adjust parameters and observe behavior changes.
  • Numerical Validation: Compare RK4 with analytical solutions where available (e.g., exponential, logistic).
  • Modeling & Simulation: Prototype dynamic systems in engineering, biology, and economics without coding complex solvers.
  • Pedagogical Tool: Perfect for classroom demonstrations, homework verification, and self‑study.

Mathematical Foundation & Derivation

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.

Step-by-Step Usage Guide

  1. Enter the function f(x, y) using JavaScript syntax (e.g., x - y, y*(1-y), Math.sin(x)).
  2. Set the initial condition (x₀, y₀) and the x‑domain [x_min, x_max].
  3. Choose a step size h (smaller values increase accuracy but slow down rendering).
  4. Click Solve & Visualize to compute the RK4 solution and generate the direction field.
  5. The canvas displays the solution curve (blue) overlaid on the direction field (orange segments). The initial point is marked in green.

Common ODEs & Their Behavior

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
Case Study: Logistic Population Model

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.

Error Analysis & Stability

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.

Common Misconceptions

  • Direction field segments are not solution curves: They only indicate local slopes; the actual solution is a smooth curve that is tangent to them.
  • Smaller step size always improves accuracy: Yes, but extremely small h may lead to round‑off errors and slower performance. Choose a reasonable balance.
  • RK4 gives exact solutions for polynomials: RK4 is exact for linear ODEs with constant coefficients when f is linear, but generally provides approximations.

Applications Across Disciplines

  • Physics: Motion under variable forces, RC circuits, radioactive decay.
  • Biology: Predator‑prey models, spread of infectious diseases (SIR).
  • Economics: Solow growth model, capital accumulation.
  • Chemistry: Reaction kinetics, rate equations.
  • Engineering: Control systems, thermal dynamics, fluid flow.

Error Analysis & Practical Advice

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.

Syntax validation & safety: The tool validates your function to avoid malicious code. Expressions like 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.

Trusted Numerical Methods – This tool implements the classical Runge‑Kutta 4 algorithm as described in Numerical Recipes (Press et al.) and verified against standard ODE benchmarks. The direction field rendering follows best practices for slope field visualization. Reviewed by the GetZenQuery Tech team, last updated March 2026.

Frequently Asked Questions

Euler uses only one slope evaluation per step (error O(h)), while RK4 uses four evaluations to cancel lower‑order terms, achieving much higher accuracy (error O(h⁴)) at modest computational cost.

This tool solves first‑order ODEs directly. Higher‑order equations can be reduced to systems of first‑order equations, which may be added in a future version.

The solver will stop when values become non‑finite. You may need to adjust the domain or step size, or check if the ODE has a singularity.

RK4 typically yields relative errors below 1e-6 for moderate step sizes on smooth problems. Use smaller h for demanding precision.

Stiff equations may require very small step sizes with RK4; consider implicit methods for stiffness. This tool works best for non‑stiff problems.

Explore resources like Wolfram MathWorld, MIT OpenCourseWare, or textbooks like "Elementary Differential Equations" by Boyce & DiPrima.
References: Runge‑Kutta methods (Wikipedia); Butcher, J.C. "Numerical Methods for Ordinary Differential Equations" (2016); Slope Field (MathWorld).