XLPack 7.0
XLPack Numerical Library (Excel VBA) Reference Manual
Loading...
Searching...
No Matches

◆ Pchsp()

Sub Pchsp ( Ic() As  Long,
Vc() As  Double,
N As  Long,
X() As  Double,
F() As  Double,
D() As  Double,
Info As  Long,
Optional Idxfd As  Long = 0,
Optional Incfd As  Long = 1 
)

Piecewise cubic spline interpolation

Purpose
This routine computes the derivatives needed to determine the Hermite representation of the cubic spline interpolant to given data, with specified boundary conditions.

The resulting piecewise cubic spline function may be evaluated by Pchfe or Pchfd.
Parameters
[in]Ic()Array Ic(LIc - 1) (LIc >= 2)

Specifies desired boundary conditions.
Ic(0) = Ibeg, desired condition at beginning of data.
Ic(1) = Iend, desired condition at end of data.

Ibeg = 0: To set D(0) so that the third derivative is continuous at X(1). This is the "not a knot" condition, which is the default boundary condition.
Ibeg = 1: The first derivative at X(0) is given in Vc(0).
Ibeg = 2: The second derivative at X(0) is given in Vc(0). For the "natural" boundary condition, use Ibeg = 2 and Vc(0) = 0.
Ibeg = 3: To use the 3-point difference formula for D(0) (reverts to the default boundary conditions if n < 3).
Ibeg = 4: To use the 4-point difference formula for D(0) (reverts to the default boundary conditions if n < 4).

Iend may take on the same values as Ibeg, but applied to derivative at X(N-1). In the case Iend = 1 or 2, the value is given in Vc(1). For the "natural" boundary condition, use Iend = 2 and Vc(1) = 0.
[in]Vc()Array Vc(LVc - 1) (LVc >= 2)
Specifies desired boundary values, as indicated above.
Vc(0) need to be set only if Ic(0) = 1 or 2.
Vc(1) need to be set only if Ic(1) = 1 or 2.
[in]NNumber of data points. (N >= 2)
[in]X()Array X(LX - 1) (LX >= N)
Independent variable values. The elements of X() must be strictly increasing.
[in]F()Array F(LF - 1) (LF >= Incfd*(N - 1) + 1)
Dependent variable values to be interpolated. F(Idxfd + I*Incfd) is the value corresponding to X(I) (I = 0 to N - 1).
[out]D()Array D(LD - 1) (LD >= Incfd*(N - 1) + 1)
Derivative values at the data points. These values will determine the cubic spline interpolant with the requested boundary conditions. The value corresponding to X(I) is stored in D(Idxfd + I*Incfd) (I = 0 to N - 1). No other entries in D() are changed.
[out]Info= 0: Successful exit.
= -1: The argument Ic() is invalid or had an illegal value. (Ic(0) < 0 or Ic(0) > 4, Ic(1) < 0 or Ic(1) > 4)
= -2: The argument Vc() is invalid.
= -3: The argument N had an illegal value. (N < 2)
= -4: The argument X() is invalid or had an illegal value. (X() is not strictly increasing)
= -5: The argument F() is invalid.
= -6: The argument D() is invalid.
= -9: The argument Incfd had an illegal value. (Incfd < 1)
[in]Idxfd(Optional)
Index of the first data in F() and D(). (default = 0)
[in]Incfd(Optional)
Increment between successive values in F() and D(). This argument is provided primarily for 2-D applications. (Incfd >= 1) (default = 1)
Reference
SLATEC (PCHIP)
Example Program
Compute ln(0.115) and ln(0.125) by interpolating the following natural logarithm table with cubic spline (natuaral spline).
  x       ln(x)
------ ---------
 0.10   -2.3026
 0.11   -2.2073
 0.12   -2.1203
 0.13   -2.0402
------ ---------
Sub Ex_Pchsp()
Const N = 4, Ne = 2
Dim X(N - 1) As Double, Y(N - 1) As Double, D(N - 1) As Double
Dim Ic(1) As Long, Vc(1) As Double
Dim Xe(Ne - 1) As Double, Ye(Ne - 1) As Double
Dim Info As Long
'-- Data
X(0) = 0.1: Y(0) = -2.3026
X(1) = 0.11: Y(1) = -2.2073
X(2) = 0.12: Y(2) = -2.1203
X(3) = 0.13: Y(3) = -2.0402
'-- Spline interpolation (Natural spline)
Ic(0) = 2: Ic(1) = 2
Vc(0) = 0: Vc(1) = 0
Call Pchsp(Ic(), Vc(), N, X(), Y(), D(), Info)
If Info <> 0 Then
Debug.Print "Error in Pchsp: Info =", Info
Exit Sub
End If
'-- Compute interpolated values
Xe(0) = 0.115: Xe(1) = 0.125
Call Pchfe(N, X(), Y(), D(), Ne, Xe(), Ye(), Info)
Debug.Print "ln(" + CStr(Xe(0)) + ") =", Ye(0)
Debug.Print "ln(" + CStr(Xe(1)) + ") =", Ye(1)
Debug.Print "Info =", Info
End Sub
Sub Pchfe(N As Long, X() As Double, F() As Double, D() As Double, Ne As Long, Xe() As Double, Fe() As Double, Info As Long, Optional Skip As Boolean=False, Optional Idxfd As Long=0, Optional Incfd As Long=1)
Evaluation of function values for piecewise cubic Hermite (and cubic spline) interpolation
Sub Pchsp(Ic() As Long, Vc() As Double, N As Long, X() As Double, F() As Double, D() As Double, Info As Long, Optional Idxfd As Long=0, Optional Incfd As Long=1)
Piecewise cubic spline interpolation
Example Results
ln(0.115) = -2.16266
ln(0.125) = -2.0797675
Info = 0