수치 계산 방법
Running example
Every worked table on this page uses the same IVP:
\[ y' = y,\qquad y(0) = 1,\qquad h = 0.5. \]
The exact solution is \( y(x) = e^{x} \). After two steps: \( y(0.5) \approx 1.648721 \), \( y(1) \approx 2.718282 \). Because \( f(x,y) = y \) ignores \( x \), the slopes are easy by hand, yet the methods still diverge clearly.
Open the same setup on the ODE-1 calculator to compare curves visually.
Euler’s method
Forward Euler is order 1. From \( (x_n, y_n) \) take slope \( k_1 = f(x_n, y_n) \) and step length \( h \):
\[ y_{n+1} = y_n + h\,k_1,\qquad x_{n+1} = x_n + h. \]
Geometrically it follows the direction field tangent; with a large \( h \) the polyline drifts from the true integral curve — useful for teaching.
/**
* One explicit Euler step for y′ = f(x, y).
* @param {(x: number, y: number) => number} f right-hand side
* @param {number} x current x
* @param {number} y current y
* @param {number} h step size
* @returns {{ x: number, y: number, k1: number }}
*/
function eulerStep(f, x, y, h) {
const k1 = f(x, y)
return { x: x + h, y: y + h * k1, k1 }
}
Two steps. Step 0: \( k_1 = f(0,1) = 1 \), so \( y_1 = 1 + 0.5\cdot 1 = 1.5 \). Step 1: \( k_1 = 1.5 \), \( y_2 = 1.5 + 0.5\cdot 1.5 = 2.25 \).
| i | x | y | k₁ | x′ | y′ |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 0.5 | 1.5 |
| 1 | 0.5 | 1.5 | 1.5 | 1 | 2.25 |
At \( x = 1 \) Euler gives \( 2.25 \) vs exact \( \approx 2.718 \) — about 17% low.
Heun’s method (RK2)
Heun (improved Euler, explicit RK2) is order 2. Predict with Euler, evaluate the slope at the end of the step, then average:
\[ k_1 = f(x_n,y_n),\qquad k_2 = f(x_n+h,\, y_n + h\,k_1), \]
\[ y_{n+1} = y_n + \frac{h}{2}(k_1 + k_2). \]
In ODEPLOT the UI label is RK2 — the same integrator as on the ODE-1 page.
/**
* One Heun (explicit RK2) step for y′ = f(x, y).
* @param {(x: number, y: number) => number} f right-hand side
* @param {number} x current x
* @param {number} y current y
* @param {number} h step size
* @returns {{ x: number, y: number, k1: number, k2: number }}
*/
function heunStep(f, x, y, h) {
const k1 = f(x, y)
const k2 = f(x + h, y + h * k1)
return { x: x + h, y: y + (h * (k1 + k2)) / 2, k1, k2 }
}
Step 0: \( k_1 = 1 \), \( k_2 = f(0.5, 1.5) = 1.5 \), \( y_1 = 1 + 0.5\cdot(1+1.5)/2 = 1.625 \). Step 1: \( k_1 = 1.625 \), \( k_2 = 2.4375 \), \( y_2 = 2.640625 \).
| i | x | y | k₁ | k₂ | x′ | y′ |
|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1.5 | 0.5 | 1.625 |
| 1 | 0.5 | 1.625 | 1.625 | 2.4375 | 1 | 2.640625 |
Error at \( x = 1 \) is about 3% — much better than Euler for one extra \( f \) call per step.
Classical Runge–Kutta (RK4)
Explicit RK4 is order 4. Four slope samples — start, two midpoints, end — then a weighted average:
\[ \begin{aligned} k_1 &= f(x_n, y_n),\\ k_2 &= f\!\left(x_n+\tfrac{h}{2},\, y_n+\tfrac{h}{2}k_1\right),\\ k_3 &= f\!\left(x_n+\tfrac{h}{2},\, y_n+\tfrac{h}{2}k_2\right),\\ k_4 &= f(x_n+h,\, y_n+h\,k_3),\\ y_{n+1} &= y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4). \end{aligned} \]
On the ODE-1 calculator RK4 is the default: smooth curves at a moderate \( h \).
/**
* One classical 4th-order Runge–Kutta step for y′ = f(x, y).
* @param {(x: number, y: number) => number} f right-hand side
* @param {number} x current x
* @param {number} y current y
* @param {number} h step size
* @returns {{ x: number, y: number, k1: number, k2: number, k3: number, k4: number }}
*/
function rk4Step(f, x, y, h) {
const k1 = f(x, y)
const k2 = f(x + h / 2, y + (h * k1) / 2)
const k3 = f(x + h / 2, y + (h * k2) / 2)
const k4 = f(x + h, y + h * k3)
return {
x: x + h,
y: y + (h * (k1 + 2 * k2 + 2 * k3 + k4)) / 6,
k1, k2, k3, k4,
}
}
Step 0 yields \( y_1 = 1.6484375 \) (exact \( \approx 1.648721 \)). After step 1, \( y_2 \approx 2.717346 \) vs exact \( \approx 2.718282 \).
| i | x | y | k₁ | k₂ | k₃ | k₄ | x′ | y′ |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 1.25 | 1.3125 | 1.65625 | 0.5 | 1.6484375 |
| 1 | 0.5 | 1.6484375 | 1.6484375 | 2.060546875 | 2.16357421875 | 2.730224609375 | 1 | 2.71734619140625 |
Compare at \( x = 1 \)
| Method | y(1) | Error ≈ |
|---|---|---|
| Exact \(e^{1}\) | 2.718282 | — |
| Euler | 2.25 | 0.468 |
| RK2 (Heun) | 2.640625 | 0.078 |
| RK4 | 2.717346 | 0.00094 |
Even two coarse steps show the orders: Euler undershoots, RK2 recovers, RK4 nearly matches the exponential. On the direction-field page you can enable all three and inspect the step table / CSV.