Community Server

The platform that enables you to build rich, interactive communities
Welcome to Community Server Sign in | Join | Help
in Search

Using Statistics101 to do numerical integration and differentiation

Last post 02-21-2008, 1:23 PM by Random Walker. 0 replies.
Sort Posts: Previous Next
  •  02-21-2008, 1:23 PM 45

    Using Statistics101 to do numerical integration and differentiation

    Statistics101 can be used for applications other than probability and statistics.
    To demonstrate, here are three subroutines that perform numerical integration and differentiation. They depend on the DELTAS command which is in a set of subroutines (in lib/vectorCommands.txt) that should already be in your Statistics101 installation's "lib" folder.

    INCLUDE "lib/vectorCommands.txt"

    'If y=f(x), this command computes y1 = dy/dx. X and Y must be same length.
    'Since this computation involves taking the differences
    'between adjacent elements, the resulting vector (y1) would be shorter than y by one
    'element. The output vector, y1, is adjusted to equal Y in size by appending
    'an NaN to it.
    NEWCMD DERIVATIVE x y y1 ?"Computes y1 = dy/dx. Y1 is the derivative"
       DELTAS x deltax
       DELTAS y deltay
       DIVIDE deltay deltax y1
       SCORE NaN y1
    END
    '
    'If y = f(x), this command computes z = integral(y(x)dx). X and Y must be the same length.
    'Since this computation involves taking the average of
    'adjacent elements, the resulting vector (Z) would be shorter than Y by one
    'element. The output vector, Z, is adjusted to equal Y in size by appending
    'an NaN to it.
    NEWCMD INTEGRAL x y z ?"Indefinite integral z = Integral(y(x)dx). x and y must be same length."
       CLEAR z
       SHIFT 1 y shiftedY
       ADD y shiftedY sums
       DIVIDE sums 2 yAverage
       DELTAS x deltaX
       SIZE x sizeX
       TAKE yAverage 2,sizeX averages
       MULTIPLY deltaX averages areas
       COPY 0 sum
       FOREACH area areas
          ADD sum area sum
          SCORE sum z
       END
       SCORE NaN z
    END
    '
    'This command computes the area under the curve defined by y = f(x)
    'over the range from x=a to x=b.
    NEWCMD INTEGRALDEF x y a b area ?"area is the definite integral of y(x)dx from a to b"
       TAGS x between a b range
       TAKE x range xab
       TAKE y range yab
       INTEGRAL xab yab z
       SIZE z sizeZ
       SUBTRACT sizeZ 1 lastElement
       TAKE z lastElement area
    END

    You can copy the above subroutines into a file in the "lib" folder in your Statistics101 installation folder. If you name that file "calculusCommands.txt" then you can include those subroutines in your programs by adding an INCLUDE command to your program like this:

    INCLUDE "lib/calculusCommands.txt"

    Here is a sample program using these subroutines (assumes you've created the "lib/calculusCommands.txt" file):

    INCLUDE "lib/calculusCommands.txt" 
    INCLUDE  "lib/mathConstants.txt"
    COPY 0,360 degrees
    MULTIPLY degrees degToRad radians
    SIN radians sine
    '
    'Compute some derivatives of y = sin(x):
    DERIVATIVE radians sine sineDerivative
    DERIVATIVE radians sineDerivative sine2ndDerivative
    DERIVATIVE radians sine2ndDerivative sine3rdDerivative
    'Use CLEAN to remove NaNs and make all vectors the same length:
    CLEAN degrees sine sineDerivative sine2ndDerivative sine3rdDerivative
    XYGRAPH  "Derivatives"  degrees sine sineDerivative sine2ndDerivative sine3rdDerivative
    '
    'Compute some integrals of a constant: y = 1.
    COPY 1,100 A
    COPY 100#1 B
    INTEGRAL a b integralB
    INTEGRAL a integralB integral2B
    INTEGRAL a integral2B integral3B
    INTEGRAL a integral3B integral4B
    'Use CLEAN to remove NaNs and make all vectors the same length:
    CLEAN a b integralB integral2B integral3B integral4B
    XYGRAPH loglog "Integrals" a b integralB integral2B integral3B integral4B
    '
    'Compute a definite integral of sine from 0 to pi:
    INTEGRALDEF radians sine 0 pi area
    PRINT area

    You can see the XYGRAPH results from the above sample program at:
    http://www.statistics101.net/images/derivatives.jpg
    http://www.statistics101.net/images/integrals.jpg

    or just cut and paste the program into Statistics101 and run it after you've created the "lib/calculusCommands.txt" file. I will put these commands into the library in future versions of the Statistics101 program.

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems