# pchim.f

          SUBROUTINE PCHIM (N, X, F, D, INCFD, IERR)
    C***BEGIN PROLOGUE  PCHIM
    C***PURPOSE  Set derivatives needed to determine a monotone piecewise
    C            cubic Hermite interpolant to given data.  Boundary values
    C            are provided which are compatible with monotonicity.  The
    C            interpolant will have an extremum at each point where mono-
    C            tonicity switches direction.  (See PCHIC if user control is
    C            desired over boundary or switch conditions.)
    C***LIBRARY   SLATEC (PCHIP)
    C***CATEGORY  E1A
    C***TYPE      SINGLE PRECISION (PCHIM-S, DPCHIM-D)
    C***KEYWORDS  CUBIC HERMITE INTERPOLATION, MONOTONE INTERPOLATION,
    C             PCHIP, PIECEWISE CUBIC INTERPOLATION
    C***AUTHOR  Fritsch, F. N., (LLNL)
    C             Lawrence Livermore National Laboratory
    C             P.O. Box 808  (L-316)
    C             Livermore, CA  94550
    C             FTS 532-4275, (510) 422-4275
    C***DESCRIPTION
    C
    C          PCHIM:  Piecewise Cubic Hermite Interpolation to
    C                  Monotone data.
    C
    C     Sets derivatives needed to determine a monotone piecewise cubic
    C     Hermite interpolant to the data given in X and F.
    C
    C     Default boundary conditions are provided which are compatible
    C     with monotonicity.  (See PCHIC if user control of boundary con-
    C     ditions is desired.)
    C
    C     If the data are only piecewise monotonic, the interpolant will
    C     have an extremum at each point where monotonicity switches direc-
    C     tion.  (See PCHIC if user control is desired in such cases.)
    C
    C     To facilitate two-dimensional applications, includes an increment
    C     between successive values of the F- and D-arrays.
    C
    C     The resulting piecewise cubic Hermite function may be evaluated
    C     by PCHFE or PCHFD.
    C
    C ----------------------------------------------------------------------
    C
    C  Calling sequence:
    C
    C        PARAMETER  (INCFD = ...)
    C        INTEGER  N, IERR
    C        REAL  X(N), F(INCFD,N), D(INCFD,N)
    C
    C        CALL  PCHIM (N, X, F, D, INCFD, IERR)
    C
    C   Parameters:
    C
    C     N -- (input) number of data points.  (Error return if N.LT.2 .)
    C           If N=2, simply does linear interpolation.
    C
    C     X -- (input) real array of independent variable values.  The
    C           elements of X must be strictly increasing:
    C                X(I-1) .LT. X(I),  I = 2(1)N.
    C           (Error return if not.)
    C
    C     F -- (input) real array of dependent variable values to be inter-
    C           polated.  F(1+(I-1)*INCFD) is value corresponding to X(I).
    C           PCHIM is designed for monotonic data, but it will work for
    C           any F-array.  It will force extrema at points where mono-
    C           tonicity switches direction.  If some other treatment of
    C           switch points is desired, PCHIC should be used instead.
    C                                     -----
    C     D -- (output) real array of derivative values at the data points.
    C           If the data are monotonic, these values will determine a
    C           a monotone cubic Hermite function.
    C           The value corresponding to X(I) is stored in
    C                D(1+(I-1)*INCFD),  I=1(1)N.
    C           No other entries in D are changed.
    C
    C     INCFD -- (input) increment between successive values in F and D.
    C           This argument is provided primarily for 2-D applications.
    C           (Error return if  INCFD.LT.1 .)
    C
    C     IERR -- (output) error flag.
    C           Normal return:
    C              IERR = 0  (no errors).
    C           Warning error:
    C              IERR.GT.0  means that IERR switches in the direction
    C                 of monotonicity were detected.
    C           "Recoverable" errors:
    C              IERR = -1  if N.LT.2 .
    C              IERR = -2  if INCFD.LT.1 .
    C              IERR = -3  if the X-array is not strictly increasing.
    C             (The D-array has not been changed in any of these cases.)
    C               NOTE:  The above errors are checked in the order listed,
    C                   and following arguments have **NOT** been validated.
    C
    C***REFERENCES  1. F. N. Fritsch and J. Butland, A method for construc-
    C                 ting local monotone piecewise cubic interpolants, SIAM
    C                 Journal on Scientific and Statistical Computing 5, 2
    C                 (June 1984), pp. 300-304.
    C               2. F. N. Fritsch and R. E. Carlson, Monotone piecewise
    C                 cubic interpolation, SIAM Journal on Numerical Ana-
    C                 lysis 17, 2 (April 1980), pp. 238-246.
    C***ROUTINES CALLED  PCHST, XERMSG
    C***REVISION HISTORY  (YYMMDD)
    C   811103  DATE WRITTEN
    C   820201  1. Introduced  PCHST  to reduce possible over/under-
    C             flow problems.
    C           2. Rearranged derivative formula for same reason.
    C   820602  1. Modified end conditions to be continuous functions
    C             of data when monotonicity switches in next interval.
    C           2. Modified formulas so end conditions are less prone
    C             of over/underflow problems.
    C   820803  Minor cosmetic changes for release 1.
    C   870813  Updated Reference 1.
    C   890411  Added SAVE statements (Vers. 3.2).
    C   890531  Changed all specific intrinsics to generic.  (WRB)
    C   890703  Corrected category record.  (WRB)
    C   890831  Modified array declarations.  (WRB)
    C   890831  REVISION DATE from Version 3.2
    C   891214  Prologue converted to Version 4.0 format.  (BAB)
    C   900315  CALLs to XERROR changed to CALLs to XERMSG.  (THJ)
    C   920429  Revised format and order of references.  (WRB,FNF)
    C***END PROLOGUE  PCHIM