V3FIT
cubsplb.f
1  subroutine cubsplb ( tau, c, n, ibcbeg, ibcend )
2 c
3 c from * a practical guide to splines * by c. de boor
4 c ************************ input ***************************
5 c n = number of data points. assumed to be .ge. 2.
6 c (tau(i), c(1,i), i=1,...,n) = abscissae and ordinates of the
7 c data points. tau is assumed to be strictly increasing.
8 c ibcbeg, ibcend = boundary condition indicators, and
9 c c(2,1), c(2,n) = boundary condition information. specifically,
10 c ibcbeg = 0 means no boundary condition at tau(1) is given.
11 c in this case, the not-a-knot condition is used, i.e. the
12 c jump in the third derivative across tau(2) is forced to
13 c zero, thus the first and the second cubic polynomial pieces
14 c are made to coincide.)
15 c ibcbeg = 1 means that the slope at tau(1) is made to equal
16 c c(2,1), supplied by input.
17 c ibcbeg = 2 means that the second derivative at tau(1) is
18 c made to equal c(2,1), supplied by input.
19 c ibcend = 0, 1, or 2 has analogous meaning concerning the
20 c boundary condition at tau(n), with the additional infor-
21 c mation taken from c(2,n).
22 c *********************** output **************************
23 c c(j,i), j=1,...,4; i=1,...,l (= n-1) = the polynomial coefficients
24 c of the cubic interpolating spline with interior knots (or
25 c joints) tau(2), ..., tau(n-1). precisely, in the interval
26 c (tau(i), tau(i+1)), the spline f is given by
27 c f(x) = c(1,i)+h*(c(2,i)+h*(c(3,i)+h*c(4,i)/3.)/2.)
28 c where h = x - tau(i). the function program *ppvalu* may be
29 c used to evaluate f or its derivatives from tau,c, l = n-1,
30 c and k=4.
31 c
32  implicit none
33  integer ibcbeg,ibcend,n, i,j,l,m
34 c
35  real c(4,n),tau(n), divdf1,divdf3,dtau,g
36 c
37 c****** a tridiagonal linear system for the unknown slopes s(i) of
38 c f at tau(i), i=1,...,n, is generated and then solved by gauss elim-
39 c ination, with s(i) ending up in c(2,i), all i.
40 c c(3,.) and c(4,.) are used initially for temporary storage.
41  l = n-1
42 compute first differences of tau sequence and store in c(3,.). also,
43 compute first divided difference of data and store in c(4,.).
44  do 10 m=2,n
45  c(3,m) = tau(m) - tau(m-1)
46  10 c(4,m) = (c(1,m) - c(1,m-1))/c(3,m)
47 construct first equation from the boundary condition, of the form
48 c c(4,1)*s(1) + c(3,1)*s(2) = c(2,1)
49  if (ibcbeg-1) 11,15,16
50  11 if (n .gt. 2) go to 12
51 c no condition at left end and n = 2.
52  c(4,1) = 1.
53  c(3,1) = 1.
54  c(2,1) = 2.*c(4,2)
55  go to 25
56 c not-a-knot condition at left end and n .gt. 2.
57  12 c(4,1) = c(3,3)
58  c(3,1) = c(3,2) + c(3,3)
59  c(2,1) =((c(3,2)+2.*c(3,1))*c(4,2)*c(3,3)+
60  > c(3,2)**2*c(4,3))/c(3,1)
61  go to 19
62 c slope prescribed at left end.
63  15 c(4,1) = 1.
64  c(3,1) = 0.
65  go to 18
66 c second derivative prescribed at left end.
67  16 c(4,1) = 2.
68  c(3,1) = 1.
69  c(2,1) = 3.*c(4,2) - c(3,2)/2.*c(2,1)
70  18 if(n .eq. 2) go to 25
71 c if there are interior knots, generate the corresp. equations and car-
72 c ry out the forward pass of gauss elimination, after which the m-th
73 c equation reads c(4,m)*s(m) + c(3,m)*s(m+1) = c(2,m).
74  19 do 20 m=2,l
75  g = -c(3,m+1)/c(4,m-1)
76  c(2,m) = g*c(2,m-1) + 3.*(c(3,m)*c(4,m+1)+c(3,m+1)*c(4,m))
77  20 c(4,m) = g*c(3,m-1) + 2.*(c(3,m) + c(3,m+1))
78 construct last equation from the second boundary condition, of the form
79 c (-g*c(4,n-1))*s(n-1) + c(4,n)*s(n) = c(2,n)
80 c if slope is prescribed at right end, one can go directly to back-
81 c substitution, since c array happens to be set up just right for it
82 c at this point.
83  if (ibcend-1) 21,30,24
84  21 if (n .eq. 3 .and. ibcbeg .eq. 0) go to 22
85 c not-a-knot and n .ge. 3, and either n.gt.3 or also not-a-knot at
86 c left end point.
87  g = c(3,n-1) + c(3,n)
88  c(2,n) = ((c(3,n)+2.*g)*c(4,n)*c(3,n-1)
89  * + c(3,n)**2*(c(1,n-1)-c(1,n-2))/c(3,n-1))/g
90  g = -g/c(4,n-1)
91  c(4,n) = c(3,n-1)
92  go to 29
93 c either (n=3 and not-a-knot also at left) or (n=2 and not not-a-
94 c knot at left end point).
95  22 c(2,n) = 2.*c(4,n)
96  c(4,n) = 1.
97  go to 28
98 c second derivative prescribed at right endpoint.
99  24 c(2,n) = 3.*c(4,n) + c(3,n)/2.*c(2,n)
100  c(4,n) = 2.
101  go to 28
102  25 if (ibcend-1) 26,30,24
103  26 if (ibcbeg .gt. 0) go to 22
104 c not-a-knot at right endpoint and at left endpoint and n = 2.
105  c(2,n) = c(4,n)
106  go to 30
107  28 g = -1./c(4,n-1)
108 complete forward pass of gauss elimination.
109  29 c(4,n) = g*c(3,n-1) + c(4,n)
110  c(2,n) = (g*c(2,n-1) + c(2,n))/c(4,n)
111 carry out back substitution
112  30 j = l
113  40 c(2,j) = (c(2,j) - c(3,j)*c(2,j+1))/c(4,j)
114  j = j - 1
115  if (j .gt. 0) go to 40
116 c****** generate cubic coefficients in each interval, i.e., the deriv.s
117 c at its left endpoint, from value and slope at its endpoints.
118  do 50 i=2,n
119  dtau = c(3,i)
120  divdf1 = (c(1,i) - c(1,i-1))/dtau
121  divdf3 = c(2,i-1) + c(2,i) - 2.*divdf1
122  c(3,i-1) = 2.*(divdf1 - c(2,i-1) - divdf3)/dtau
123  50 c(4,i-1) = (divdf3/dtau)*(6./dtau)
124 c
125  return
126  end