Fitting a Natural Spline to Samples of the Form (t, f(t)) David Eberly, Geometric Tools, Redmond WA 9852 https://wwwgeometrictoolscom/ This work is licensed under the Creative Commons Attribution 4 International License To view a copy of this license, visit http://creativecommonsorg/licenses/by/4/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 9442, USA Created: January 5, 28 Last Modified: January 5, 215 Contents 1 Introduction 2 11 Constraints for Free Splines 2 12 Constraints for Clamped Splines 3 13 Constraints for Periodic Splines 3 2 Constructing Free Splines 3 3 Constructing Clamped Splines 5 4 Constructing Periodic Splines 6 41 Three Samples 6 42 Four Samples 7 43 The General Case 9 1
1 Introduction Consider a function f : D IR m, whose domain is D = [t min, t max ] and whose range consists of m-tuples with m 1 The only information we have about the function is a set of samples, {(t i, f i )} n i=, f i = f(t i ) and t min t < t 1 < < t n < t n t max We want to fit the function with a piecewise cubic polynomial spline S (t), t [t, t 1 ] S 1 (t), t [t 1, t 2 ] S(t) = S n 2 (t), t [t n 2, t n ] S n (t), t [t n, t n ] S i (t) = a i + b i (t t i ) + c i (t t i ) 2 + d i (t t i ) 3 (2) for i n 1 Moreover, we want S(t) to be a C 2 function; that is, S(t), S (t), and S (t) must be continuous functions of t on the interval [t, t n ] Each of S i (t) is a C 2 function on the open interval (t i, t i+1 ), so the only points of concern to be C 2 every are the sample points We have n polynomials to determine, each having 4 unknown coefficients Thus, we have 4n unknowns Imposing the C 2 condition leads to linear constraints on the unknowns We need the polynomials, their first derivatives, and their second derivatives to match at the sample points t 1 through t n Specifically, S i (t i+1 ) = S i+1 (t i ), S i(t i+1 ) = S i+1(t i ), S i (t i+1 ) = S i+1(t i ), i n 2 (3) This is a set of 3(n 1) constraints on the unknowns We also need the polynomials to pass through the sample points, so S i (t i ) = f i, i n 1, and S n (t n ) = f n (4) This is a set of n + 1 constraints on the unknowns Equations (3) and (4) represent 4n 2 constraints on the 4n unknowns We have two degrees of freedom Three standard choices are the following (1) 11 Constraints for Free Splines We may allow the endpoints to have whatever slope necessary by specifying the second derivatives at those points to be zero: S (t ) =, S n(t n ) = (5) The resulting curve is said to be a free spline These two constraints and the previously mentioned ones give us 4n linear equations in 4n unknowns; that is, the coefficients of the cubic polynomials are obtained by solving a linear system of equations It turns out that the system is tridiagonal, so a specialized linear solver may be used that is O(n) General linear solvers are O(n 3 ), so the specialized solver saves computational time for a large number of sample points 2
12 Constraints for Clamped Splines We may clamp the endpoints in the sense that we specify the first derivatives at those points: S (t ) = σ, S n(t n ) = σ 1 (6) for user-specified constants σ and σ 1 The resulting curve is said to be a clamped spline These two constraints and the previously mentioned ones give us 4n linear equations in 4n unknowns As for the case of free splines, the linear system is tridiagonal and may be solved by a specialized linear solver 13 Constraints for Periodic Splines We may require that the first and last samples have the same function values, namely, that f n = f Moreover, the C 2 condition requires us to impose the derivative equality constraints S (t ) = S n(t n ), S (t ) = S n(t n ) (7) The resulting curve is said to be a periodic spline These two constraints and the previously mentioned ones produce a system of 4n linear equations in 4n unknowns, but this system is not tridiagonal However, the system may be solved by a specialized linear solver that is O(n) 2 Constructing Free Splines This section describes how to set up and solve the tridiagonal linear system of equations for the polynomial coefficients when the endpoints are required to have zero second-order derivatives The ratio of differences of function values and time values occurs often in the derivation, so let us first define q i = f i+1 f i i (8) Define i = t i+1 t i for all relevant i The conditions S i (t i+1 ) = S i+1 (t i ) and S i (t i ) = f i are equivalent to The conditions S i (t i+1) = S i+1 (t i) are equivalent to The conditions S i (t i+1) = S i+1 (t i) are equivalent to i b i + 2 i c i + 3 i d i = f i+1 f i (9) 2 i c i + 3 2 i d i = b i+1 b i (1) 3 i d i = c i+1 c i (11) Substitute Equation (11) into Equations (9) and (1) to obtain ( ) i b i + 2 ci+1 + 2c i i = f i+1 f i (12) 3 and i (c i+1 + c i ) = b i+1 b i (13) 3
Increment the index in Equation (12) to obtain ( ) i+1 b i+1 + 2 ci+2 + 2c i+1 i+1 = f i+2 f i+1 (14) 3 Compute i times Equation (14) and subtract from it i+1 times Equation (12) to obtain ( ) i i+1 (b i+1 b i ) + i 2 ci+2+2c i+1 i+1 3 i+1 2 i = i (f i+2 f i+1 ) i+1 (f i+1 f i ) ( ) ci+1+2c i 3 (15) Substitute Equation (13) into Equation (15), collect terms, and divide by i i+1 to obtain i+1 c i+2 + 2( i+1 + i )c i+1 + i c i = 3(q i+1 q i ) (16) which is defined for i n 3 We know that the constraint S (t ) = in Equation (5) implies c = The constraint S n(t n ) in Equation (5) implies n c n + 3 2 nd n = If we define c n =, this constraint fits into the construction here and Equation (16) also holds for i = n 2 Essentially, we are thinking of the spline curve having one additional segment S n (t) = f n + b n (t t n ), a line segment, so that c n = d n = Equation (16) and boundary conditions c = c n = may be solved by setting up a linear system Mc = p (17) the (n 1) (n 1) matrix M is tridiagonal, symmetric, and diagonally dominant: 2( + 1 ) 1 1 2( 1 + 2 ) 2 2 2( 2 + 3 ) 3 M = n 4 2( n 4 + n 3 ) n 3 n 3 2( n 3 + n 2 ) n 2 n 2 2( n 2 + n ) (18) The (n 1) 1 column vector c has components c 1 through c n and the (n 1) 1 column vector p has components p 1 through p n p i = 3(q i q i ) (19) The matrix M and vector p may be computed and then used as input to a linear system solver that is specialized for tridiagonal systems The solver computes c 1 through c n, and we already know that c = The d-terms are computed from Equation (11), d i = c i+1 c i 3 i (2) and the b-terms are computed from Equation (12), b i = q i i ( ci+1 + 2c i 3 ) (21) The a-terms are already known, a i = f i (22) 4
3 Constructing Clamped Splines This section describes how to set up and solve the tridiagonal linear system of equations for the polynomial coefficients when the endpoints are required to have user-specified first-order derivatives Multiply Equation (9) by 3/ i and subtract from it Equation (1) to obtain Increment the index in Equation (23) to obtain 3b i + i c i = 3q i (b i+1 b i ) (23) 3b i+1 + i+1 c i+1 = 3q i+1 (b i+2 b i+1 ) (24) Multiply Equation (23) by i+1, multiply Equation (24) by i and add the two together to obtain 3( i+1 b i + i b i+1 ) + i+1 i (c i+1 + c i ) = 3( i+1 q i + i q i+1 ) i+1 (b i+1 b i ) i (b i+2 b i+1 ) (25) In the left-hand side of Equation (25), substitute Equation (13) to eliminate the c-terms Some algebra simplifies this to i b i+1 + 2( i + i+1 )b i+1 + i+1 b i = 3( i+1 q i + i q i+1 ) (26) which is defined for i n 3 We know that the constraint S (t ) = σ in Equation (6) implies b = σ The constraint S n(t n ) = σ 1 in Equation (6) implies n b n +2 2 nc n +3 3 nd n = σ 1 If we define b n = σ 1, this constraint fits into the construction here and Equation (26) also holds for i = n 2 Essentially, we are thinking of the spline curve having one additional segment S n (t) = f n + b n (t t n ) + c n (t t n ) 2, a parabolic arc, so that 2c n = 2 n c n + 6 2 nd n and d n = Equation (26) and boundary conditions b = σ and b n = σ 1 may be solved by setting up a linear system Nb = r (27) the (n 1) (n 1) matrix N is tridiagonal, symmetric, and diagonally dominant: 2( + 1 ) 2 2( 1 + 2 ) 1 3 2( 2 + 3 ) 2 N = n 3 2( n 4 + n 3 ) n 4 n 2 2( n 3 + n 2 ) n 3 n 2( n 2 + n ) (28) The (n 1) 1 column vector b has components b 1 through b n and the (n 1) 1 column vector r has components r 1 through r n r 1 = 3( 1 q + q 1 ) 1 σ r i = 3( i q i + i q i ), 2 i n 2 (29) r n = 3( n q n 2 + n 2 q n ) n 2 σ 1 The matrix N and vector r may be computed and then used as input to a linear system solver that is specialized for tridiagonal systems The solver computes b 1 through b n The c-terms are computed from Equation (23), c i = 3q i b i+1 2b i i (3) 5
and the d-terms are computed from Equation (1), d i = b i+1 b i 2 i c i 3 2 i (31) The a-terms are already known, a i = f i (32) 4 Constructing Periodic Splines This section describes how to set up and solve the linear system of equations for the polynomial coefficients when the endpoints are required to have the same function value, same first-order derivative, and same second-order derivative 41 Three Samples Consider the simplest case of three samples: (t, f ), (t 1, f 1 ), and (t 2, f ) The two cubic polynomials are S (t) = a + b (t t ) + c (t t ) 2 + d (t t ) 3 and S 1 (t) = a 1 + b 1 (t t 1 ) + c 1 (t t 1 ) 2 + d 1 (t t 1 ) 3 The eight constraints are f = S (t ) = S 1 (t 2 ) S (t ) = S 1(t 2 ) S (t ) = S 1 (t 2 ) f 1 = S (t 1 ) = S 1 (t 1 ) S (t 1 ) = S 1(t 1 ) S (t 1 ) = S 1 (t 1 ) Define = t 1 t and 1 = t 2 t 1 In terms of the polynomial coefficients, the constraints are (33) a + b + c 2 + d 3 = a 1 = f 1 b + 2c + 3d 2 = b 1 2c + 6d = 2c 1 a 1 + b 1 1 + c 1 2 1 + d 1 3 1 = a = f b 1 + 2c 1 1 + 3d 1 2 1 = b (34) 2c 1 + 6d 1 1 = 2c The linear system of equations is 1 1 2 3 1 2 3 2 1 3 1 1 1 2 1 3 1 1 2 1 3 2 1 1 3 1 a b c d a 1 b 1 c 1 d 1 = f f 1 (35) 6
In block matrix form, M L K = f U (36) L M 1 K 1 f 1 U 1 1 M i = i 2 i 3 i 1 1 2 i 3 2, L =, K i = i 1 1 3 1 Notice that M i is invertible a i b i c i d i 1, U = The linear system may be solved by row-reducing the augmented block matrix as shown next The matrix I is the 4 4 identity matrix M L f U I M f M L M 1 f 1 U L M 1 f 1 U (37) I I M L f M U M 1 LM L (f 1I + f LM )U M L f M U I (M 1 LM L) (f 1 I + f LM )U I [f M + M 1 LM (f 1 I + f LM I (M 1 LM (f 1 I + f LM (38) = I K I K 1 K 1 = (M 1 LM L) (f 1 I + f LM ), K = f M U + M LK 1 (39) 42 Four Samples Similar to the case of three samples, the constraints may be formulated as a block-matrix system, M L K f U M 1 L K 1 = f 1 U L M 2 K 2 f 2 U (4) 7
The linear system may be solved by row-reducing the augmented block matrix, M L f U M 1 L f 1U L M 2 f 2U I M L fm U I M 1 L f1m1 L M 2 f 2U I M L fm U I M 1 L f1m1 U LM L M2 (f2i + flm )U I M L fm U I M 1 L f1m1 M 2 LM LM1 L [f2i + LM (fi + f1lm1 I M L fm U I M 1 L f1m1 I K 2 (41) K 2 = (M 2 LM LM1 L) [f 2 I + LM (f I + f 1 LM1 )]U (42) Back substitution may now be used to compute K and K 1 The first back substitution is I M L f M U I M L f M U I M 1 L f 1M1 U I K 1 I K 2 I K 2 (43) K 1 = f 1 M 1 U + M 1 LK 2 (44) The second back substitution is I M M I K I K 1 I K 1 I K 2 I K 2 (45) K = f M U + M LK 1 (46) Equations (46), (44), and (42) are the polynomial coefficients for the spline 8
43 The General Case In general, the constraints may be formulated as a block-matrix system, M L K M 1 L K 1 M n 2 L L M n K n = f U f 1 U f n U (47) whose solution is obtained by row reducing the augmented block matrix M L f U M 1 L f 1 U M n 2 L f n 2 U L M n f n U (48) Inverting the M i matrices in the first n 1 block rows leads to I M L f M U I M1 L f 1M1 U I Mn 2 L f n 2Mn 2 U L M n f n U Forward elimination by the first n 1 block rows leads to I M L f M U I M1 L f 1M1 U I Mn 2 L f n 2Mn 2 U P Q (49) (5) and Q = [ f n I + f LM + f 1 LM = f n U + LM P = M n ( LM LM1 LMn 2) L (51) ( LM1 + + f n 2 LM LM1 LMn 2)] U [ f U + LM1 (f 1 U + ) ] (52) 9
The last expression shows that Q may be computed efficiently in a nested manner Inverting P in the last row of the reduced augmented block matrix, I M L f M U I M1 L f 1M1 U I Mn 2 L f n 2Mn 2 U I K n (53) K n = P Q (54) Back substitution may be applied n 1 times to solve for the other coefficients Generally, these are K i = f i M i U + M i LK i+1, n 2 i (55) In the calculations, we need the inverses of the M i matrices These are 1 3 3 M i = i i 2 i 3 3 3 2 i 2 i i 2 3 i 1 3 i 2 i 1 i (56) 1