Algorithms¶
This page outlines the main numerical algorithms used internally by PDIndexer. It is a migrated and reorganized version of the explanatory PDF (PDIndexerAlgorithm.pdf) that used to be bundled with the distribution. The goal is to convey what is minimized and how it is solved rather than full mathematical rigor.
Three topics are covered:
- Lattice-constant refinement — linear least squares
- Peak fitting — nonlinear least squares by the Marquardt method, and the profile functions
- Cubic spline derivation — background curve
For the theory of equations of state (EOS), see Equation of States.
Lattice-constant refinement¶
Generalized linear least squares¶
Given \( n \) sets of observations \( (X^1_1, X^2_1, \dots, X^m_1, Z_1),\ (X^1_2, X^2_2, \dots, X^m_2, Z_2),\ \dots,\ (X^1_n, X^2_n, \dots, X^m_n, Z_n) \), fitting the linear observation equation
over the \( m \) parameters \( (a^1, a^2, \dots, a^m) \) is achieved by minimizing the sum of squared residuals. In matrix form,
where \( W \) is a diagonal matrix of weights. Minimizing the weighted sum of squares
by setting its derivative with respect to \( \mathbf{a} \) to zero,
gives the solution
Fitting the reciprocal metric tensor¶
For lattice-constant refinement the observation equation depends on the crystal system, but in the most general (triclinic) case the relation between the interplanar spacing \( d \) and the indices \( (h,k,l) \),
is treated as a linear model:
where \( a^*, b^*, \dots \) are the reciprocal lattice constants. Solving this with the linear least squares above yields the components of the reciprocal metric tensor, from which the lattice constants follow.
Choice of weights¶
The weight depends on the error. Assuming the error lies only in the diffraction angle \( 2\theta \), the response of \( G = (1/d)^2 = 4\sin^2\theta/\lambda^2 \) to \( \theta \) is
so a change \( \delta\theta \) shifts \( (1/d)^2 \) by \( \dfrac{4\sin(2\theta)}{\lambda^2}\delta\theta \). Therefore \( 1/\sin^2(2\theta) \) (the inverse of the squared error) is an appropriate weight for \( (1/d)^2 \):
Here \( 1/\sin^2(2\theta) \) represents only the ratio of the inverse variances of the points, not their absolute value, yet the optimum is still recovered: in \( (X^{\mathsf{T}}W X)^{-1} X^{\mathsf{T}}W Y \) the factor \( W \) appears twice, so the absolute scale cancels out.
Parameter errors¶
The errors (variances) of \( \mathbf{a} \) come from the diagonal of \( (X^{\mathsf{T}}W X)^{-1} \), but since \( W \) was fixed only up to a ratio, the absolute scale must be determined separately. Using the definition of variance,
(\( N \): number of data, \( P \): number of parameters, \( \delta_i \): residual of the \( i \)-th datum, \( s_i \): variance of the \( i \)-th datum), the variance scale is fixed from the obtained parameters as
and its square root is the error. This is the error of the reciprocal lattice constants; converting it to the error of the lattice constants requires propagating the error further, which is straightforward in principle.
Peak fitting¶
Marquardt method¶
PDIndexer fits peaks with the Marquardt method (Levenberg–Marquardt), a nonlinear iterative scheme similar to Newton's method. It combines fast convergence with stability and finds the optimum with sufficient accuracy.
Let the fitting function be \( F = F(a_1, a_2, \dots, a_m, X) \) and the residual at the initial parameters \( \mathbf{a}^0 \) be
Build the \( m\times m \) matrix \( \alpha \) and the \( m \)-vector \( \beta \) as follows. Multiplying only the diagonal by \( (1+\lambda) \) is the key idea of the Marquardt method, with \( \lambda \) controlling stability and convergence speed:
The parameters are updated by
Compute the new residual \( R' \) and:
- if \( R' < R \), accept the update and shrink \( \lambda \) (by a factor of 0.1–0.5);
- if \( R' > R \), reject the update and grow \( \lambda \) (by a factor of 2–10).
Repeat until the change in \( R \) is sufficiently small. As \( \lambda \to 0 \) the method approaches the quadratically convergent Gauss–Newton method; for large \( \lambda \) it approaches steepest descent along the residual gradient \( \nabla R \). Switching continuously between the two via \( \lambda \) yields stable, fast convergence.
Profile functions¶
PDIndexer offers the Pseudo Voigt function (a mixture of Gaussian and Lorentzian), the Pearson VII function (a probability density function), and their asymmetric extensions Split Pseudo Voigt / Split Pearson VII. For speed and convergence stability, Symmetric Pseudo Voigt is the default. All functions are normalized to unit area.
Symmetric Pseudo Voigt
Split Pseudo Voigt (Toraya 1990, modified)
Symmetric Pearson VII
Split Pearson VII (Toraya 1990, modified)
The first two are symmetric about \( x=0 \), while the split forms change shape depending on the sign of \( x \) to express asymmetry (such as a low-angle tail). In general, Pearson VII tends to give the better fit (smaller residual), while Pseudo Voigt tends to converge more stably.
Symbols¶
| Symbol | Meaning |
|---|---|
| \( H_k \) | full width at half maximum (FWHM) |
| \( \pi \) | the circle constant |
| \( \eta \) (\( \eta_l, \eta_h \)) | Lorentzian/Gaussian mixing ratio (low-angle / high-angle side for split forms) |
| \( \Gamma \) | gamma function |
| \( R \) (\( R_l, R_h \)) | Pearson exponent |
| \( A \) | asymmetry parameter |
| \( Z \) | normalization constant (\( \sqrt{\pi\ln 2} \)) |
Fitting function with background¶
In practice the profile function \( f \) is extended with a linear background:
(\( I \): integrated intensity, \( B_1, B_2 \): linear background, \( \Theta \): peak center, \( \theta \): observed position). Within a given range, the parameters are varied by the Marquardt method so that \( R = \sum (Y - F)^2 \) is minimized.
The partial derivatives of each function are complex; the Marquardt method uses these analytic gradients. Representative expressions are given below for reference.
Partial derivatives of Symmetric Pseudo Voigt
Writing \( u = \dfrac{\theta-\Theta}{H_k} \),
Partial derivatives of Pearson VII
The simple derivatives with respect to intensity and background (\( \partial F/\partial I,\ \partial F/\partial B_1,\ \partial F/\partial B_2 \)) are omitted. The original document denotes the Pearson exponent by both \( R \) and \( m \) (the same quantity). Writing \( u = \dfrac{\theta-\Theta}{H_k} \),
Cubic spline derivation¶
PDIndexer uses a cubic spline curve to draw the background. The true background shape cannot be solved exactly, but the software automatically detects the non-peak regions and connects the detected points with a spline to form the background curve. A spline approximates the data uniformly, including its derivatives, and the approximation improves as the data points are made denser.
Given \( n \) points \( (X_1,Y_1), (X_2,Y_2), \dots, (X_{n-1},Y_{n-1}), (X_n,Y_n) \), we seek a curve that is cubic on each interval and joins smoothly so that value, slope, and curvature match at every point (the two end intervals \( \{-\infty, X_1\} \) and \( \{X_n, \infty\} \) are taken to be linear).
Let the function on interval \( \{X_{m-1}, X_m\} \) be
Interior points (\( 2 \le m \le n-1 \)). Continuity of value, first derivative, and second derivative gives
— that is, \( 4n-8 \) conditions.
Start (\( m=1 \), left end interval is linear):
— 4 conditions. The end (\( m=n \)) likewise gives
— another 4 conditions.
In total, \( 4n \) conditions determine \( 4n \) unknowns, reducing the problem to a system of simultaneous equations. Writing it out as a matrix and inverting solves it easily.
Related pages¶
- 6. Fitting diffraction peaks — how to use it in practice
- Equation of States — EOS theory such as the Birch–Murnaghan and Mie–Grüneisen equations