Numerical Function Differentiator V4.0
Inception & Programming by Vajrang Parvate

This program uses numerical techniques to differentiate a given function
and plots the original function along with the differential curves upto a
differential order of 9.
The given function / curve may be in the form of a true mathematical equation
or may be a collection of points.

Alternatively, it can also be used to plot curves for three-dimensional
equations.

To use :

1. Start Microsoft QuickBasic and load FNDASH.BAS.

2. Edit the SETOFGRAPHS variable to indicate mode of operation. Its value
   should be 0 if the program is to work as a function differentiator and
   value should be 1 if the program is to work as a function plotter for
   three-dimensional equations.

3. Edit the NNMAX variable to indicate the number of curves required.
   If SETOFGRAPHS is 1, then NNMAX dictates minimum increment in the
   secondary variable.
   If SETOFGRAPHS is 0, then NNMAX dictates the highest order differential
   possible.
   (TIP: Although the program incorporates options to display different
   differential curves, the program will run faster if NNMAX is smaller.
   Hence, define NNMAX to give you only the *maximum* required differential
   order).

4. If SETOFGRAPHS is 0 -
   Edit the variable POINTS to indicate the increment required on the x-axis
   (TIP: One would naturally assume that a higher number of points would
   give a smoother curve. This is not true, in the sense that for higher
   order differentials (about 6 and over), the algorithm fails, since the
   process goes beyond the accuracy delivered by even double-precision
   floating point values. Hence, use the guideline given below to modify
   the POINTS variable.
       NNMAX points
       9                100
       8                150
       7                250
       6                500
       5                750
       4 & below        1000
   Having said all this, you may need to modify the POINTS value (usually
   reduce it), since the working of the algorithm also depends on the nature
   of the function defined).
   If SETOFGRAPHS is 1, then you may set POINTS as high as 2500 or so.

5. If you require an output from the program, i.e. if you want the program
   to work as a graphical differentiating system, edit the MONITOR variable
   to 1 for an output to screen, 2 for an output to a file, defined by the
   FILE$ variable.
   The increment of values required is to be stored against the INCR variable.
   All values upto the order defined by the NNMAX variable will be provided.
   So be careful of the NNMAX variable before outputting the results to a
   file.
   (TIP: Before you directly output the results to a file, try them out first
   on the screen output. Once the program is pausing satisfactorily at each
   increment, then direct the results to a file output.)

6. Press F2 and select the function defined as "fx".

7. Set the FUNCDEF variable to 0 if a mathematical function is to be
   processed. Enter the function as given below:
   Define the required function as in the example given below:
   If the function derivatives are required:
   fx# = SIN(x#) + 3 * x# ^ 2

   If three-dimensional equations are to be plotted:
   fx# = SIN(y# * x#)
   In the given example, y# can be varied from say, 0.5 to 2 in steps of 0.1
   Then the curves of SIN(0.5*x),SIN(0.6*x),....,SIN(1.9*x) and SIN(2*x)
   would be plotted.
   Be careful when defining functions in this mode - if one of the functions
   is invalid, you will not get any output. e.g. if the function is x#/y#,
   for y#=0, the function is not defined - so avoid y#=0; use a low value for
   y# like 0.0001 etc.
   (NOTE: The hash, #, signs are necessary to indicate double-precision
   variables. Also see item nos.4,5 & 6 in 'Other info' below.).
   Data can also be entered as a collection of points (such as those obtained
   in an experiment, etc.) by setting the FUNCDEF variable to 1 and entering
   the points by following the instructions that appear on the screen.
   (NOTE: See also item nos.8 & 9 in 'Other Info' below.)

8. Press Shift+F5 to run the program.

9. Enter the lower and upper domain values (-ve and +ve x) and lower and
   upper function values (-ve and +ve y).

10.If used as a function differentiator:
   By responding with 'y' to whether all differential curves governed by the
   currently defined value of NNMAX should be plotted, (NNMAX+1) curves will
   be plotted.
   If you respond with 'n', you can individually select which curves are to
   be plotted - y0 meaning the original function, y1 meaning the first order
   differential, y2 meaning the second order differential and so on.
   If used as a 3-D function plotter:
   Limits of y# need to be entered. Then you can either enter number of
   curves required to fit in those limits OR respond with zero and enter
   the increment in the y# value.

11.Edit the LINESJOIN variable - set to 0 if only a series of points are to
   be plotted; set to 1 if the points are to be joined by line segments.
   See item no.3 in 'Other Info' below.


Output screen :

1. The output screen will show the x- and y- axes (depending, of course, on
   whether either of them lie in the defined domains).

2. The right hand top corner denotes approximately how much more time is
   required to complete the plots.

3. If you press <ESC> anytime during the plot pauses the plotting for a
   response.
   Pressing <ESC> again will quit; pressing 'r' will take you back to opening
   screen; 's' will display the status of the plot; any other key will
   continue plotting.

4. If the function is being differentiated, at end of plot, a colour legend
   will be displayed at the bottom right corner, giving the legend of colours
   associated with the plots. As before, 0 means original function, 1 means
   first order differential, and so on.

5. Press 'n' to quit; any other key will repeat.

6. If you want a printout of the plot, load the GRAPHICS.COM driver before
   starting QuickBasic and press Shift-Print after the curves fully appear
   on the screen (the right hand top corner should report "Done").


Other info:

1. The major flaw, so to speak, in this algorithm is that the differential
   curves will not be plotted in the domain in which the original function is
   not defined. The simplest example is LOG(x), which is defined for x>0 and
   will be plotted only in that region. Although its first order derivative,
   1/x is defined for x<>0 (x not equal to 0), it will not be plotted for
   x<=0. Same is applicable for its higher order derivatives.
   The reason for this is the basic idea of numerical differentiation. All
   the subsequent differentials for a function are calculated by taking a
   finite discrete number of segments of the function in the defined domain.
   Obviously, if the function itself is not defined in a certain region, its
   further differentials cannot be calculated.

2. The other minor problem is seen during the plotting of derivatives of
   functions having one or more cusps in the defined region. The derivative
   at the point of the cusp is not defined, which makes the differential
   function discontinuous. Since points are plotted instead of line segments
   (due to reason given below), this discontinuous region appears a gap in
   the plotted curve.

3. Ideally, the function to be plotted should be displayed as a series of
   points, joined by line segments, to give it a smooth and continuous
   appearance.
   But, during higher order differentiation, even the double precision
   accuracy gives out and some points appear to be randomly plotted. If these
   are joined by line segments, the result is obvious and needless to say,
   unwanted.
   Since such an occurence is not usually encountered while plotting 3-d
   functions, you can safely use line segments by setting LINESJOIN to 1.
   There are, of course, exceptions to this - functions such as TAN(x#), for
   example have two values for the function at x#=pi/2 i.e. + and - ; at
   this point, the curve crosses over the y-axis abruptly - this crossing
   over is also joined by a vertical straight line, which actually isn't part
   of the function.
   In differentiation mode, the above explained result is seen, especially
   during differentiation of functions having cusps, e.g. ABS(SIN(x#)),
   or in functions where the function value tends to infinity in the finite
   portion of the domain, e.g. TAN(x#) or 1/x#, etc. To avoid this, set
   LINESJOIN to 0 to plot the curve as a series of points only.

4. One of the hidden beauties is the way functions can be defined. e.g.,
   The default function defined in the given program is time-response of a
   mass suspended by means of a spring and a damper. Although the formula
   for the response is different for 3 distinct ranges of the damping
   coefficient, the function can be defined to incorporate this variation.
   Another common example is where the function definition itself varies in
   different domains of x, such as:

   IF x#<0 THEN
        fx# = SIN(x#)
   ELSE
        fx# = COS(x#)
   END IF

   ...and the like.

   For a preliminary tryout,
   Run FNDASH.BAS as it is without any changes, using the following
   INPUT values

        Enter left domain limit : 0
        Enter right domain limit : 2
        Enter lower function limit : -1
        Enter upper function limit : 1
        
        Enter variation in the y# value:
        y# to vary from : 0
                     to : 1.4
        Number of curves : 0
        Increment in y# : 0.2

5. Another inherent drawback is that the functions to be defined must be
   explicit.

6. For a list of predefined mathematical functions available in QuickBasic,
   see under Help - Contents - Keywords by programming task - Perform
   mathematical calculations. Other mathematical operators are:
   +    for addition
   -    for subtraction
   /    for division
   *    for product
   ^    for exponentiation

7. The program incorporates error handling capabilities for domains in which
   the given function is not defined. Other problems such as division by zero
   and function overflows are also taken care of.

8. When data is entered as a collection of points, the classical Lagrange's
   Interpolation method is used to draw a continuous curve. One must keep in
   mind that the method is for INTERPOLATION only, that is to say,
   extrapolation far beyond the range of the points given will produce
   results, which need not necessarily be correct. Results will be fairly
   correct within the minimum and maximum of domain of entered data.
   (The actual points will be marked on the curve by white circles for
   reference).

9. When data is entered as a collection of points, they are stored in a file
   called LASTSESS.DAT in the same working directory. The first line contains
   the number of points contained in the file, followed by x(i),y(i).
   These may be edited externally.

Contact for more details.
vparvate@giaspn01.vsnl.net.in

Comments & feedback are most welcome.

