View Raw SPL
/*****************************************************************************
*                                                                            *
*   DYDX.SPL     Copyright (C) 1999, 2014 DSP Development Corporation        *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    XY derivative                                               *
*                                                                            *
*   Revisions:    2 Feb 1999  RRR  Creation                                  *
*                14 Aug 2014  RRR  Simplified using RDERIV                   *
*                                                                            *
*****************************************************************************/

#if @HELP_DYDX

    DYDX

    Purpose: Performs a derivative on XY data

    Syntax:  DYDX(ysig, xsig)

              ysig - An input XY series.
              xsig - Optional. A series, the explicit X values.

    Returns: A series or table.

    Example:
             W1: xy(gexp(100, .01), gline(100, .01, 1, 0))
             W2: dydx(w1);

             W2 contains the first derivative of the XY data in W1.

    Example:
             W3: dydx(gline(100, .01, 1, 0), gexp(100, .01))

             Same as above except the Y and X series are explicit.

    Example:
             W1: gsin(100, 1/100, 1);
             W2: dydx(w1);
             W3: rderiv(w1)

             W2 contains the first derivative of the data in W1. W2 is
             identical to W1 except for the last point.

    Remarks:
             DYDX computes the 2 point right derivative of an XY or interval
             series. The 2 point right derivative is defined as:

                y[n] = (s[n+1] - s[n]) / deltax(s)

             DYDX identical to the built-in RDERIV function except for the
             last point. For DYDX:

                y[end] = (s[end+1] - s[end]) / deltax(s) == y[end-1].

            For RDERIV:

               y[end] = s[end] / deltax(s)

            See DIFFS to compute the 2 point difference or the 2 point
            left or right derivative.

    See Also:
             Deriv
             Diffs
             Lderiv
             Rderiv
#endif


/* right derivative with modified endpoint */
dydx(ysig, xsig)
{
        local dy;

        if (argc < 2)
        {
                if (argc < 1)
                {
                        error("dydx - series required");
                }
        }
        else
        {
                /* separate Y and X series */
                ysig = xy(xsig, ysig);
        }

        /* handles XY */
        dy = rderiv(ysig);

        if (length(dy) > 1)
        {
                dy[end] = dy[end - 1];
        }

        return(dy);
}