How this calculation works
A two-point boundary problem prescribes solution values at both ends of an interval. This page solves the linear equation y″+p(x)y′+q(x)y=f(x) with polynomial coefficient functions. It replaces the first and second derivatives at interior nodes by central differences, assembles a linear system and solves it with row-scaled pivoting. A second solve at twice the interval count supplies a refinement comparison at the original nodes.
y″+p(x)y′+q(x)y=f(x); (1−p_i h/2)y_(i−1)+(−2+q_i h²)y_i+(1+p_i h/2)y_(i+1)=h²f_i.A worked example
For y″=−2, y(0)=y(1)=0, the exact solution is y=x(1−x). With ten intervals, h=0.1 and the first interior point is x=0.1, where y=0.09. At x=0.5 the solution is 0.25. Central second differences differentiate this quadratic exactly up to floating-point arithmetic, so the mesh comparison is nearly zero for this example. That special agreement does not make finite differences exact for a general ODE.
Good to know
Supports linear second-order equations with polynomial p,q,f of degree at most four and coefficients within ±10⁶. Requires ordered endpoints within ±100 separated by at least 10⁻⁶, fixed endpoint values within ±10⁶ and 4–100 coarse intervals. The refined solve doubles that mesh. Singular or numerically ill-conditioned discrete systems are rejected. Derivative boundary conditions, nonlinear equations, adaptive meshes and an initial-slope mode are not supported.
Reference: MIT — finite-difference treatment of boundary-value problems (opens in a new tab) ↗
The two boundary values enter one coupled interior system
An initial-value solver takes data at one starting location and advances outward. Here the right endpoint is known before any interior values are solved. The first interior equation includes the left boundary, and the last includes the right boundary. Moving those known terms to the right-hand side leaves the unknown interior values coupled together. Guessing a starting slope and calling it the requested right-end value would not preserve the stated boundary problem.
For the default zero-boundary example, the scaled interior equation is y_(i−1)−2y_i+y_(i+1)=−2h². Substituting the quadratic x(1−x) satisfies each row. If instead the endpoints are y(0)=1 and y(1)=2 while the forcing stays −2, the exact solution becomes 1+2x−x²; at x=1/2 it is 1.75. This second example checks that both boundary contributions actually enter the solve rather than being overwritten after computing an unrelated curve.
The Fourier series coefficients calculator for a piecewise function studies a periodic harmonic expansion rather than a two-endpoint differential boundary problem.
Residual, mesh difference and continuum error answer different questions
The algebraic residual measures how closely the returned vector satisfies the finite linear equations. A tiny residual can accompany a poor approximation to the differential equation if the grid is too coarse. The refinement table compares the coarse result with values from the doubled grid at matching locations. That comparison can reveal change under refinement, but two meshes agreeing is not a proof that both resolve a narrow feature or that the original continuous problem has a unique solution.
For a nonquadratic check, y″−y=0 with y(0)=0 and y(1)=1 has solution sinh(x)/sinh(1). Its second derivative is not represented exactly by the stencil, so increasing the interval count normally changes the approximation. Near singularity, the discrete solve may become sensitive or be rejected by the pivot checks. Report the equation, boundary conditions and mesh count when sharing a result; a curve without those assumptions is not a reproducible boundary-value calculation.
A few questions, answered
Can I prescribe y′ at one endpoint?
This page accepts Dirichlet data: a value of y at each endpoint. Neumann, Robin and mixed boundary conditions need different boundary equations and are not silently converted.
Why is the first result not always the value at the midpoint?
The primary result is explicitly the first interior coarse node. The full node table includes the remaining locations, and an even interval count includes the midpoint as a table row.