Skip to content
QFT.org

Search QFT.org

Enter a word or phrase.

    You can also browse the main navigation and chapter contents.

    Finite Differences and Sparse Linear Systems

    A field equation can have a beautifully small numerical residual and still describe the wrong continuum field. The residual tests the equations on a chosen grid; it does not test the grid itself. We will solve a sourced massive scalar problem, refine its grid, and then deliberately change the linear-solver tolerance to separate these two questions.

    Preparation: Taylor expansion, matrix eigenvalues and the static limit of the Klein–Gordon equation. All lengths and fields in the numerical example are dimensionless.

    A scalar field with known boundary values

    Consider a real scalar in a prescribed, positive mass background,

    (−∂x2+M2(x))ϕ(x)=J(x),0<x<1,M2(x)=9+100x2.(-\partial_x^2+M^2(x))\phi(x)=J(x), \qquad 0<x<1, \qquad M^2(x)=9+100x^2.

    This follows from the mostly-minus equation (□+M2)ϕ=J(\Box+M^2)\phi=J for a time-independent field. The background is fixed; we are solving a classical linear equation, not evaluating quantum fluctuations.

    Choose a manufactured solution

    ϕ∗(x)=ex+x(1−x),ϕ(0)=1,ϕ(1)=e.\phi_*(x)=e^x+x(1-x), \qquad \phi(0)=1,\quad \phi(1)=e.

    Differentiating it gives the required source:

    J(x)=[M2(x)−1]ex+M2(x)x(1−x)+2.J(x)=[M^2(x)-1]e^x+M^2(x)x(1-x)+2.

    Manufacturing a source is useful because it supplies a continuum answer independently of the numerical solver. It does not prove that a method will resolve every sharply varying field. Here it lets us measure precisely what refinement changes.

    Let xi=ihx_i=ih, with h=1/(n+1)h=1/(n+1) and i=1,…,ni=1,\ldots,n the interior points. Taylor expansion gives

    ϕ(xi+h)−2ϕ(xi)+ϕ(xi−h)h2=ϕ′′(xi)+h212ϕ(4)(xi)+O(h4).\frac{\phi(x_i+h)-2\phi(x_i)+\phi(x_i-h)}{h^2} =\phi''(x_i)+\frac{h^2}{12}\phi^{(4)}(x_i)+O(h^4).

    The discretized operator therefore has entries

    Aii=2h2+M2(xi),Ai,i±1=−1h2.A_{ii}=\frac{2}{h^2}+M^2(x_i), \qquad A_{i,i\pm1}=-\frac1{h^2}.

    The first equation contains −ϕ(0)/h2-\phi(0)/h^2, which moves to the right-hand side as +1/h2+1/h^2; the last receives +e/h2+e/h^2. These signs are part of the problem, not optional solver settings.

    An equivalent formulation is better for interpreting relative residuals. Lift the boundary values with

    ℓ(x)=1+(e−1)x,v=ϕ−ℓ.\ell(x)=1+(e-1)x,\qquad v=\phi-\ell.

    Since ℓ′′=0\ell''=0, solve

    Av=b,bi=J(xi)−M2(xi)ℓ(xi),v0=vn+1=0.Av=b,\qquad b_i=J(x_i)-M^2(x_i)\ell(x_i), \qquad v_0=v_{n+1}=0.

    The source bb remains of ordinary size as hh decreases. In the unlifted system the boundary entries grow like h−2h^{-2}, so a tolerance measured relative to that right-hand side can obscure the accuracy needed in the interior.

    Sparse structure and positive definiteness

    For a vector vv extended by zero to both endpoints,

    vTAv=1h2∑i=0n(vi+1−vi)2+∑i=1nM2(xi)vi2>0v^{\mathsf T}Av =\frac1{h^2}\sum_{i=0}^{n}(v_{i+1}-v_i)^2 +\sum_{i=1}^{n}M^2(x_i)v_i^2>0

    unless v=0v=0. Thus AA is symmetric positive definite. This fact establishes uniqueness of the discrete solution and permits conjugate gradients.

    Only 3n−23n-2 entries are nonzero. Matrix-vector multiplication and a tridiagonal factorization require O(n)O(n) work and storage here. A dense inverse would discard the main advantage of this one-dimensional problem. For the example, a sparse direct solve supplies a convenient reference on each grid. In several dimensions, sparse factorization can introduce substantial fill-in; that is one reason iterative methods and elliptic preconditioners become important.

    The condition number also matters. The Dirichlet difference Laplacian has eigenvalues

    λj(−Dxx)=4h2sin⁡2 ⁣(jπ2(n+1)).\lambda_j(-D_{xx})=\frac4{h^2} \sin^2\!\left(\frac{j\pi}{2(n+1)}\right).

    Its smallest eigenvalue stays finite, while the largest grows like h−2h^{-2}. Adding a bounded positive M2M^2 preserves this scaling. Refining the grid makes the approximation more accurate but the unpreconditioned system more ill-conditioned.

    Conjugate gradients and an actual preconditioner

    Conjugate gradients minimize the quadratic

    E(v)=12vTAv−bTv\mathcal E(v)=\frac12v^{\mathsf T}Av-b^{\mathsf T}v

    over successive Krylov subspaces. With rk=b−Avkr_k=b-Av_k and search direction pkp_k, the unpreconditioned recurrence is

    αk=rkTrkpkTApk,vk+1=vk+αkpk,rk+1=rk−αkApk,βk=rk+1Trk+1rkTrk,pk+1=rk+1+βkpk.\begin{aligned} \alpha_k&=\frac{r_k^{\mathsf T}r_k}{p_k^{\mathsf T}Ap_k},\\ v_{k+1}&=v_k+\alpha_kp_k,\\ r_{k+1}&=r_k-\alpha_kAp_k,\\ \beta_k&=\frac{r_{k+1}^{\mathsf T}r_{k+1}}{r_k^{\mathsf T}r_k},\\ p_{k+1}&=r_{k+1}+\beta_kp_k. \end{aligned}

    Start with p0=r0p_0=r_0. In exact arithmetic the directions are AA-orthogonal, and at most nn steps suffice. Finite precision changes that statement. The residual norm need not decrease monotonically even when the quadratic error improves. These distinctions already appear in Hestenes and Stiefel, §§2–3, pp. 410–411 (PDF).

    Use the positive definite approximation

    P=−Dxx+9I,A=P+diag⁡(100xi2).P=-D_{xx}+9I,\qquad A=P+\operatorname{diag}(100x_i^2).

    Preconditioned CG computes zk=P−1rkz_k=P^{-1}r_k, starts with p0=z0p_0=z_0, and replaces the two residual squared norms in the recurrence by rkTzkr_k^{\mathsf T}z_k. It applies P−1P^{-1} through one reused tridiagonal factorization. It does not explicitly form an inverse.

    The relevant symmetric operator is P−1/2AP−1/2P^{-1/2}AP^{-1/2}. From P≤A≤P+100IP\le A\le P+100I,

    1≤λ(P−1/2AP−1/2)≤1+100λmin⁡(P).1\le\lambda(P^{-1/2}AP^{-1/2}) \le1+\frac{100}{\lambda_{\min}(P)}.

    The upper bound approaches 1+100/(9+π2)≃6.301+100/(9+\pi^2)\simeq6.30 as the mesh is refined. The actual generalized condition number at n=63n=63 is 2.840882.84088. This is a substantive change from the h−2h^{-2} growth of the original system. A scalar rescaling of AA would not change its condition number.

    What the retained calculation shows

    The companion finite_difference.py uses NumPy 2.0.2 and SciPy 1.13.1. Every iterative solve starts from zero, uses atol=0 and a limit of 5n5n iterations. The documented CG criterion is a residual tolerance, not a bound on continuum error.

    The table uses requested relative tolerance 10−1110^{-11}. The field error is from the sparse direct solution, in ∥e∥h=(h∑iei2)1/2\|e\|_h=(h\sum_i e_i^2)^{1/2}.

    Interior points nnField error ∥ϕh−ϕ∗∥h\lVert\phi_h-\phi_*\rVert_hκ2(A)\kappa_2(A)CG stepsPCG steps
    31312.747×10−62.747\times10^{-6}1.060×1021.060\times10^231311010
    1271271.717×10−71.717\times10^{-7}1.679×1031.679\times10^31271271010
    5115111.073×10−81.073\times10^{-8}2.685×1042.685\times10^45115111010
    102310232.683×10−92.683\times10^{-9}1.074×1051.074\times10^5102310231010

    The full scan includes n=63,255n=63,255 as well. Halving hh reduces the field error by approximately four, as expected from the centered stencil. PCG needs ten matrix-vector products and ten preconditioner applications at each listed size; ordinary CG needs nn matrix-vector products in these runs.

    Second-order field convergence beside conjugate-gradient iteration counts, showing ten preconditioned steps over the full mesh scan.

    The same manufactured scalar problem separates grid error from iterative work. The left panel uses direct solutions on each mesh; the right compares CG and the stated tridiagonal preconditioner at requested relative residual 10−1110^{-11}. The retained output also records independently recomputed residuals, including finite-precision stopping discrepancies.

    There is an instructive discrepancy at the finest meshes. SciPy returns successful status, but recomputing b−Avb-Av gives relative residuals 1.66×10−111.66\times10^{-11} at n=511n=511 and 8.64×10−118.64\times10^{-11} at n=1023n=1023 for ordinary CG. The recursively updated residual and a newly computed residual have reached different rounding floors. These results are retained rather than described as satisfying the requested tolerance exactly. At n=511n=511, the CG solution still differs from the direct solution by only 9.34×10−169.34\times10^{-16} in the weighted norm.

    Residual, conditioning and discretization error

    Let vhv_h be the exact solution of the discrete system and v~\widetilde v an iterative approximation. For the recomputed residual r=b−Av~r=b-A\widetilde v,

    vh−v~=A−1r,∥vh−v~∥h≤∥r∥hλmin⁡(A).v_h-\widetilde v=A^{-1}r,\qquad \|v_h-\widetilde v\|_h\le \frac{\|r\|_h}{\lambda_{\min}(A)}.

    The full error splits as

    ϕ~−ϕ∗=(ϕ~−ϕh)+(ϕh−ϕ∗).\widetilde\phi-\phi_*= (\widetilde\phi-\phi_h)+(\phi_h-\phi_*).

    The first term is algebraic; the second is the grid error. At n=511n=511, PCG with requested tolerance 10−310^{-3} takes five steps and has algebraic error 2.16×10−72.16\times10^{-7}, larger than the 1.07×10−81.07\times10^{-8} grid error. At 10−710^{-7} it takes seven steps and the algebraic error falls to 1.57×10−101.57\times10^{-10}. Ten steps at 10−1110^{-11} reduce it to 3.54×10−153.54\times10^{-15}. Solving much more accurately than the discretization needs is sometimes useful for diagnostics, but it does not improve the continuum approximation.

    The companion also checks the cubic-polynomial stencil, symmetry, the generalized preconditioned spectrum, the residual bound, and agreement between lifted and directly inserted boundaries. Refinement is essential even after all of these checks pass. The spectral comparison changes the discretization while preserving a boundary-value problem.

    Summary

    Finite differences turn local field equations into sparse systems whose boundaries, positivity and conditioning can be derived before solving. A manufactured solution measures grid error; a direct solution on the same grid measures iterative error. The useful stopping target depends on both, and an independently recomputed residual is more informative than a success flag alone.

    Exercises

    1. The sign of the truncation defect

    For the manufactured field above, derive the first and last unlifted equations. Then calculate the leading defect A(ϕ∗−ℓ)−bA(\phi_*-\ell)-b and explain why a positive definite matrix does not force the defect itself to be positive.

    Solution

    The first row is

    (2h2+M12)ϕ1−ϕ2h2=J1+1h2.\left(\frac2{h^2}+M_1^2\right)\phi_1 -\frac{\phi_2}{h^2}=J_1+\frac1{h^2}.

    The last has Jn+e/h2J_n+e/h^2 on the right. The lift reproduces these values and its centered second derivative vanishes exactly, so both formulations are algebraically equivalent.

    Since ϕ∗(4)=ex\phi_*^{(4)}=e^x,

    τi=[A(ϕ∗−ℓ)−b]i=−h212exi+O(h4).\tau_i=[A(\phi_*-\ell)-b]_i =-\frac{h^2}{12}e^{x_i}+O(h^4).

    The minus sign comes from the negative second derivative in the field operator. Positive definiteness asserts vTAv>0v^{\mathsf T}Av>0 for a nonzero vector; it says nothing about the sign of each component of Av−bAv-b for an arbitrary trial field. Solving A(ϕh−ϕ∗)=−τA(\phi_h-\phi_*)=-\tau converts the consistency defect into field error through the inverse operator.

    2. A stopping rule with a stated meaning

    Suppose the desired algebraic error is at most η\eta in ∥⋅∥h\|\cdot\|_h. Give a sufficient residual threshold using a known lower bound λ0\lambda_0 on λmin⁡(A)\lambda_{\min}(A). Derive the preconditioner bound above, and explain why a nonzero mesh error remains after this threshold is reached.

    Solution

    The spectral decomposition of the symmetric positive matrix gives ∥A−1∥2=1/λmin⁡(A)\|A^{-1}\|_2=1/\lambda_{\min}(A). The weighted norm differs by the constant factor h\sqrt h, so the same operator bound applies. It is sufficient to require

    ∥r∥h≤λ0η.\|r\|_h\le\lambda_0\eta.

    For this problem one may use

    λ0=9+4h2sin⁡2 ⁣(π2(n+1)).\lambda_0=9+\frac4{h^2} \sin^2\!\left(\frac{\pi}{2(n+1)}\right).

    For any nonzero ww,

    wTAwwTPw=1+∑i100xi2wi2wTPw∈[1,1+100λmin⁡(P)].\frac{w^{\mathsf T}Aw}{w^{\mathsf T}Pw} =1+\frac{\sum_i100x_i^2w_i^2}{w^{\mathsf T}Pw} \in\left[1,1+\frac{100}{\lambda_{\min}(P)}\right].

    These generalized Rayleigh quotients bound the spectrum of P−1/2AP−1/2P^{-1/2}AP^{-1/2}. Its condition number is therefore at most the stated upper endpoint. The residual test concerns ϕ~−ϕh\widetilde\phi-\phi_h only. Even a zero residual leaves ϕh−ϕ∗\phi_h-\phi_*, which the mesh scan measures as an O(h2)O(h^2) effect.

    References

    • M. R. Hestenes and E. Stiefel, “Methods of Conjugate Gradients for Solving Linear Systems,” Journal of Research of the National Bureau of Standards 49 (1952), 409–436, DOI: 10.6028/jres.049.044. Consulted §§2–3, pp. 410–411, especially Eqs. (3:1)–(3:3), in the NIST copy (PDF).
    • SciPy community, scipy.sparse.linalg.cg, SciPy 1.13.1 manual: positive definiteness, inverse-preconditioner interface, residual tolerance and status.

    Scrollable formula. Use Left and Right arrow keys to reveal hidden content.

    Scrollable table. Use Left and Right arrow keys to reveal hidden columns.

    Scrollable figure. Use Left and Right arrow keys to reveal hidden content.