home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / q / qlib56.zip / SOLVE.DOC < prev    next >
Text File  |  1992-12-11  |  17KB  |  451 lines

  1. QLIB's SOLVE subroutines and functions provide quick soultions for common
  2. mathematical equations.  Many require a math coprocessor.  As with many of
  3. QLIB's DATA subroutines, a function or subroutine with INT, LNG, SNG or DBL
  4. in its name is to be used with INTEGER, LONG, SINGLE, or DOUBLE data types,
  5. respectively.
  6.  
  7. SINGLE and DOUBLE data in SOLVE subroutines and functions must be in IEEE
  8. format.  Since this is the default format for QuickBASIC beginning with
  9. version 4.0, and for BC beginning with BC6, this will not be a problem in
  10. most cases.
  11.  
  12.  
  13.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  14.  
  15.     Function: f! = C2F(c!)
  16.     Function: c! = F2C(f!)
  17.     object code: degrees.obj
  18.  
  19.          C2F calculates degrees Farenheit f! from degrees Celcius c!.
  20.     F2C calculates degrees Celcius c! from degrees Farenheit f!.  C2F
  21.     and F2C use the 8087 if available, or use BASIC's 8087 emulator
  22.     if no 8087 is in the computer.
  23.  
  24.     Example:
  25.         REM $INCLUDE: 'qlib.bi'
  26.         f! = 100!
  27.         c! = C2F(f!)          ' 100 degrees Celcius
  28.                               ' converted to degrees Farenheit
  29.  
  30.  
  31.  
  32.  
  33.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  34.  
  35.     Subroutine: CubeFitF4(s%, o%, n%, a!, b!, c!, d!)
  36.     Subroutine: CubeFitF8(s%, o%, n%, a!, b!, c!, d!)
  37.     Subroutine: CubeFitI2(s%, o%, n%, a!, b!, c!, d!)
  38.     Subroutine: CubeFitI4(s%, o%, n%, a!, b!, c!, d!)
  39.     object file: cubefit.obj
  40.  
  41.         CubeFit subroutines use the Least Squares method to determine
  42.     the coefficients of a cubic equation to fit n sets of coordinate data.
  43.     The formula is of the form
  44.  
  45.          y = a! + b!x + c!x^2 +d!x^3
  46.  
  47.     CubeFitF4 requires coordinate data as SINGLE data.
  48.     CubeFitF8 requires coordinate data as DOUBLE data.
  49.     CubeFitI2 requires coordinate data as INTEGER data.
  50.     CubeFitI4 requires coordinate data as LONG data.
  51.  
  52.     The CubeFit subroutines use the 8087 extensively; QuickBASIC's 8087
  53.     emulator works properly when compiled with the /o switch; if you do
  54.     not have a math coprocessor, CubeFit may not work within the
  55.     QuickBASIC development enviornment.  Hint: if the computer locks up,
  56.     it's not working properly.  Save your work before trying CubeFit if
  57.     you don't have an 8087.
  58.  
  59.     Example 1:
  60.         REM  I have 5 data points, and I want a cubic equation
  61.         REM  that best describes the points
  62.         REM  I'll use a 2-dimensional array for the first example
  63.  
  64.         DIM points(1,4) AS INTEGER
  65.         points(0,0) = x0: points(1,0) = y0
  66.         points(0,1) = x1: points(1,1) = y1
  67.         points(0,2) = x2: points(1,2) = y2
  68.         points(0,3) = x3: points(1,3) = y3
  69.         points(0,4) = x4: points(1,4) = y4
  70.         s% = VARSEG(points(0,0)): o% = VARPTR(points(0,0)): n% = 5
  71.         CALL CubeFitI2(s%, o%, n%, a!, b!, c!, d!)
  72.         REM  CubeFit returns the formula  y = a! + b!x + c!x^2 + d!x^3
  73.  
  74.     Example 2:
  75.         REM  this time I'll use a one-dimensional integer array
  76.  
  77.         DIM points(9) AS INTEGER
  78.         points(0) = x0: points(1) = y0
  79.         points(2) = x1: points(3) = y1
  80.         points(4) = x2: points(5) = y2
  81.         points(6) = x3: points(7) = y3
  82.         points(8) = x4: points(9) = y4
  83.         s% = VARSEG(points(0)): p% = VARPTR(points(0)): n% = 5
  84.         CALL CubeFitI2(s%, o%, n%, a!, b!, c!, d!)
  85.  
  86.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  87.  
  88.     Function: fact# = Factorial(n%)
  89.     object code: factori.obj
  90.  
  91.          Factorials, often used in statistics, may be easily calcuated
  92.     with this subroutine.  fact# is a DOUBLE real number returned as
  93.     the factorial of the INTEGER n%.  Factorial uses the 8087 if available,
  94.     or uses BASIC's 8087 emulator if no 8087 is in the computer.
  95.  
  96.     Example:
  97.         REM $INCLUDE 'qlib.bi'
  98.         n% = 12: fact# = Factorial(n%)
  99.  
  100.  
  101.  
  102.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  103.  
  104.     Subroutine: FPrimeF4(s, p, n)
  105.     object file: fprimef4.obj
  106.  
  107.     80x87 required
  108.  
  109.  
  110.     Subroutine: FPrimeI2(s, p, n)
  111.     object file: fprimei2.obj
  112.  
  113.     80x87 not required
  114.  
  115.          Given a polynomial function f(x), FPrime calculates the first
  116.     derivative of the function, f'(x).  The coefficients of f(x) must all
  117.     be INT data for FPrimeI2, or SINGLE data for FPrimeF4.
  118.  
  119.     Example:
  120.         DEFINT A-Z
  121.         REM I want to calculate the first derivative of the function
  122.         REM   y = 2 + 3*(x**2) + 2*(x**3) + (x**5)
  123.         REM also called a fifth-order polynomial
  124.  
  125.         DIM f(4)
  126.         f(0) = 2     ' load the coefficients into the array
  127.         f(1) = 3     ' this is for the 3*(x**2) term
  128.         f(2) = 2     ' this is for the 2*(x**3) term
  129.         f(3) = 0     ' this is a place holder for the (x**4) term
  130.         f(4) = 1     ' this is for the (x**5) term
  131.         s = VARSEG(f(0)): p = VARPTR(f(0)): n = 5
  132.         call   fprimeI2(s, p, n)
  133.  
  134.         REM  result is f(0) = 3, f(1) = 4, f(2) = 0, f(3) = 4, f(4) = 0
  135.  
  136.  
  137.  
  138.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  139.  
  140.     Function: fv! = FValue(n%, i!, pmt!, pv!)
  141.     object files: fvalue.obj (xtothey.obj)
  142.  
  143.     Requires 80x87
  144.  
  145.          FValue calculates the future value of a constant cash flow
  146.     given n% periods, i! periodic interest rate (i! <> 0), pmt! constant
  147.     periodic payments and pv! present value.  i! interest rate must not
  148.     be zero or your program will come to a flying stop.
  149.  
  150.     Example:
  151.         REM $INCLUDE: 'qlib.bi'
  152.         REM  my daughter has $100 in her savings account, which accumulates
  153.         REM  interest at a 5.25% interest rate, compounded monthly.  If I
  154.         REM  add $25 to her account at the end of each month, what will she
  155.         REM  have at the end of one year?
  156.         i! = .0525 / 12          ' monthly interest rate
  157.         n% = 12                  ' 12 months
  158.         pmt! = 25!               ' $25 payment
  159.         pv! = 100!               ' starting with $100.00
  160.         fv! = FValue(n%, i!, pmt!, pv!)
  161.                                  ' fv! is the balance in the account
  162.  
  163.  
  164.  
  165.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  166.  
  167.     Subroutine: LineFitF4(s%, o%, n%, a!, b!)
  168.     Subroutine: LineFitF8(s%, o%, n%, a!, b!)
  169.     Subroutine: LineFitI2(s%, o%, n%, a!, b!)
  170.     Subroutine: LineFitI4(s%, o%, n%, a!, b!)
  171.     object file: linefit.obj
  172.  
  173.         LineFit subroutines use the Least Squares method to determine
  174.     the coefficients of a formula to fit n sets of coordinate data.
  175.     The formula is of the form
  176.  
  177.          y = a! + b!x
  178.  
  179.     LineFitF4 requires coordinate data as SINGLE data.
  180.     LineFitF8 requires coordinate data as DOUBLE data.
  181.     LineFitI2 requires coordinate data as INTEGER data.
  182.     LineFitI4 requires coordinate data as LONG data.
  183.  
  184.     The LineFit subroutines use the 8087 extensively; QuickBASIC's 8087
  185.     emulator works properly when compiled with the /o switch; if you do
  186.     not have a math coprocessor, LineFit may not work within the
  187.     QuickBASIC development enviornment.  Hint: if the computer locks up,
  188.     it's not working properly.  Save your work before trying LineFit if
  189.     you don't have an 8087.
  190.  
  191.     Example 1:
  192.         REM  I have 5 data points, and I want the equation of the line
  193.         REM  that best describes the points
  194.         REM  I'll use a 2-dimensional array for the first example
  195.  
  196.         DIM points(1,4) AS INTEGER
  197.         points(0,0) = x0: points(1,0) = y0
  198.         points(0,1) = x1: points(1,1) = y1
  199.         points(0,2) = x2: points(1,2) = y2
  200.         points(0,3) = x3: points(1,3) = y3
  201.         points(0,4) = x4: points(1,4) = y4
  202.         s% = VARSEG(points(0,0)): o% = VARPTR(points(0,0)): n% = 5
  203.         CALL LineFitI2(s%, o%, n%, a!, b!)
  204.         REM  LineFit returns the formula  y = a! + b!x
  205.  
  206.     Example 2:
  207.         REM  this time I'll use a one-dimensional integer array
  208.  
  209.         DIM points(9) AS INTEGER
  210.         points(0) = x0: points(1) = y0
  211.         points(2) = x1: points(3) = y1
  212.         points(4) = x2: points(5) = y2
  213.         points(6) = x3: points(7) = y3
  214.         points(8) = x4: points(9) = y4
  215.         s% = VARSEG(points(0)): p% = VARPTR(points(0)): n% = 5
  216.         CALL LineFitI2(s%, o%, n%, a!, b!)
  217.  
  218.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  219.  
  220.     Function: npv! = NetPValue(s%, o%, n%, i!)
  221.     object files: npvalue.obj (xtothey.obj)
  222.  
  223.     Requires 80x87
  224.  
  225.         NetPValue calculates the net present value of a series of uneven
  226.     cash flows in the array a!().  Note that the array is of SINGLE data
  227.     type.  If you are interested, I can make this work with other data
  228.     types.  i! interest rate must not be zero or your program will come
  229.     to a flying stop.  This subroutine is for ordinary cash flows, with
  230.     payment at the end of each period.
  231.  
  232.     Example:
  233.         REM $INCLUDE: 'qlib.bi'
  234.         DIM a!(99)               ' 100 payments on a loan
  235.         i! = .08
  236.         REM  program establishes values for each payment
  237.         s% = VARSEG(a!(0)): o% = VARPTR(a!(0))
  238.         npv! = NetPValue(s%, o%, n%, i!)
  239.  
  240.  
  241.  
  242.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  243.  
  244.     Function: pmt! = Payment(n%, i!, pv!, fv!)
  245.     object files: payment.obj (xtothey.obj)
  246.  
  247.     Requires 80x87
  248.  
  249.         Payment calculates equal periodic payments required given pv!, i!,
  250.     fv! and n%.  This pmt! is an ordinary annuity, for example, mortgages,
  251.     discounted notes (with or without balloon), direct reduction loans
  252.     (with or without balloon).  Note that fv! = 0 with no balloon.
  253.  
  254.     Example:
  255.         REM $INCLUDE: 'qlib.bi'
  256.         REM  a modest home in the flatlands of San Leandro, California
  257.         REM  sells for $200,000.  With an annual interest rate of 10%
  258.         REM  and a 20% down payment, what is the monthly loan payment
  259.         REM  (principle and interest) on a 30-year loan?
  260.  
  261.         pv! = 200000 * .80       ' loan amount
  262.         fv! = 0                  ' loan paid off in 30 years
  263.         n% = 30 * 12             ' payments are monthly
  264.         i! = .1 / 12             ' monthly interest rate
  265.         pmt! = Payment(n%, i!, pv!, fv!)
  266.  
  267.  
  268.  
  269.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  270.  
  271.     Function: y# = PSolveF4(s%, p%, n%, x!)
  272.     Function: y# = PSolveF8(s%, p%, n%, x!)
  273.     Function: y# = PSolveI2(s%, p%, n%, x!)
  274.     Function: y# = PSolveI4(s%, p%, n%, x!)
  275.     object file: psolve.obj
  276.  
  277.     Requires 80x87
  278.  
  279.          Solves an n-order polynomial function for y! given x!.
  280.     The PSolve functions can be twice as fast as equivalent BASIC code.
  281.  
  282.     For PSolveI2, the coefficients in the equations must be INTEGERs.
  283.     For PSolveI4, the coefficients in the equations must be LONG integers.
  284.     For PSolveF4, the coefficients in the equations must be SINGLE.
  285.     For PSolveF8, the coefficients in the equations must be DOUBLE.
  286.  
  287.     Examples of polynomial functions:
  288.  
  289.     y! = 3 + 2*x! + 7*(x!^2)       ' a quadratic function, where n = 2
  290.  
  291.     y! = -48 + x! + 3*(x!^2) - x!^3  ' a cubic equation, where n = 3
  292.  
  293.     Example:
  294.         DEFINT A-Z
  295.         REM  I'll use the second polynomial function above to calculate
  296.         REM  401 y! values for x! = .5 to 200
  297.  
  298.         DIM c(3) AS INTEGER
  299.         DIM y!(400)
  300.         c(0) = -48: c(1) = 1: c(2) = 3: c(3) = -1
  301.         n = 3
  302.         x! = .5
  303.         s% = VARSEG(c(0)): p% = VARPTR(c(0))
  304.         FOR I = 0 to 400
  305.         y!(i) = PSolveI2(s%, p%, n%, x!)
  306.         x! = x! +.5
  307.         NEXT I
  308.  
  309.  
  310.  
  311.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  312.  
  313.     Function: pv! = PValue(n%, i!, pmt!, fv!)
  314.     object files: pvalue.obj (xtothey.obj)
  315.  
  316.     Requires 80x87
  317.  
  318.          PValue calculates the present value of a constant cash flow
  319.     given n% periods, i! periodic interest rate (i! <> 0), pmt! constant
  320.     periodic payments and fv! future value.  i! interest rate must not
  321.     be zero or your program will come to a flying stop.
  322.  
  323.     Example:
  324.         REM $INCLUDE: 'qlib.bi'
  325.         REM  In this case, my goal is to have $10,000 in savings in 10
  326.         REM  years.  If I only want to make an initial deposit, then let
  327.         REM  the interest accumulate, what deposit should I make to reach
  328.         REM  that goal?
  329.  
  330.         i! = .08 / 12            ' 8.00% annual interest rate
  331.         n% = 120                 ' 120 months = 10 years
  332.         fv! = 10000.0            ' that's what I want
  333.         pmt! = 0.0
  334.         pv! = PValue(n%, i!, pmt!, fv!)
  335.  
  336.  
  337.  
  338.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  339.  
  340.     Subroutine: QuadFitF4(s%, o%, n%, a!, b!, c!)
  341.     Subroutine: QuadFitF8(s%, o%, n%, a!, b!, c!)
  342.     Subroutine: QuadFitI2(s%, o%, n%, a!, b!, c!)
  343.     Subroutine: QuadFitI4(s%, o%, n%, a!, b!, c!)
  344.     object file: quadfit.obj
  345.  
  346.         QuadFit subroutines use the Least Squares method to determine
  347.     the coefficients of a formula to fit n sets of coordinate data.
  348.     The formula is of the form
  349.  
  350.          y = a! + b!x + c!x^2
  351.  
  352.     QuadFitF4 requires coordinate data as SINGLE data.
  353.     QuadFitF8 requires coordinate data as DOUBLE data.
  354.     QuadFitI2 requires coordinate data as INTEGER data.
  355.     QuadFitI4 requires coordinate data as LONG data.
  356.  
  357.     The QuadFit subroutines use the 8087 extensively; QuickBASIC's 8087
  358.     emulator works properly when compiled with the /o switch; if you do
  359.     not have a math coprocessor, QuadFit may not work within the
  360.     QuickBASIC development enviornment.  Hint: if the computer locks up,
  361.     it's not working properly.  Save your work before trying QuadFit if
  362.     you don't have an 8087.
  363.  
  364.     Example 1:
  365.         REM  I have 5 data points, and I want the equation of the line
  366.         REM  that best describes the points
  367.         REM  I'll use a 2-dimensional array for the first example
  368.  
  369.         DIM points(1,4) AS INTEGER
  370.         points(0,0) = x0: points(1,0) = y0
  371.         points(0,1) = x1: points(1,1) = y1
  372.         points(0,2) = x2: points(1,2) = y2
  373.         points(0,3) = x3: points(1,3) = y3
  374.         points(0,4) = x4: points(1,4) = y4
  375.         s% = VARSEG(points(0,0)): o% = VARPTR(points(0,0)): n% = 5
  376.         CALL QuadFitI2(s%, o%, n%, a!, b!, c!)
  377.         REM  QuadFit returns the formula  y = a! + b!x + c!x^2
  378.  
  379.     Example 2:
  380.         REM  thie time I'll use a one-dimensional integer array
  381.  
  382.         DIM points(9) AS INTEGER
  383.         points(0) = x0: points(1) = y0
  384.         points(2) = x1: points(3) = y1
  385.         points(4) = x2: points(5) = y2
  386.         points(6) = x3: points(7) = y3
  387.         points(8) = x4: points(9) = y4
  388.         s% = VARSEG(points(0)): p% = VARPTR(points(0)): n% = 5
  389.         CALL QuadFitI2(s%, o%, n%, a!, b!, c!)
  390.  
  391.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  392.  
  393.     Subroutine: Quadratic(a!, b!, c!, x1!, x2!, isquared%)
  394.     object file: quad.obj
  395.  
  396.     Requires 80x87
  397.  
  398.          This subroutine solves a quadratic equation for its roots
  399.     x1! and x2!.  A quadratic equation is of the form
  400.  
  401.     a!*x^2 + b!*x! + c! = 0
  402.  
  403.     where a!, b! and c! are known and a! <> 0.  The two solutions of the
  404.     equation are found with the formulas
  405.  
  406.     x1! = (-b! + SQRT(b! ^ 2 - 4 * a! * c!)) / (2 * a!)
  407.     x2! = (-b! - SQRT(b! ^ 2 - 4 * a! * c!)) / (2 * a!)
  408.  
  409.     The solutions of the equation may be either real or imaginary.  Quadratic
  410.     returns isquared% = 1 if x1! and x2! are real, and isquared = -1 if the
  411.     solutions are imaginary.
  412.  
  413.     Example:
  414.         a! = 3.5: b! = 7!: c! = 12!
  415.         CALL Quadratic(a!, b!, c!, x1!, x2!, isquared%)
  416.  
  417.  
  418.  
  419.  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  420.  
  421.     Function: s! = StdDevINT(s%, p%, n%)
  422.     Function: s! = StdDevLNG(s%, p%, n%)
  423.     Function: s! = StdDevSNG(s%, p%, n%)
  424.     Function: s! = StdDevDBL(s%, p%, n%)
  425.     object file: stddev.obj
  426.  
  427.          StdDev subroutines calculate the standard deviation s! of an array
  428.     from the array's average value x!.  Note that in all StdDev subroutines
  429.     s! is a single-precision value.  s! is the sample standard deviation;
  430.     population standard deviation is calculated from s! with this formula:
  431.  
  432.     sp! = s! * SQRT((n - 1) / n)
  433.  
  434.     StdDev subroutines use the 8087 if available, or use the 8087 emulator
  435.     if no 8087 is in the computer.
  436.  
  437.     Example:
  438.         REM $INCLUDE: 'qlib.bi'
  439.         DIM a(99)              ' an integer array of 100 elements
  440.            .                   ' values of each array element established
  441.            .                   ' somewhere in program
  442.            .
  443.         n% = 100               ' now I want the average and standard
  444.                                ' deviation of the array
  445.         s% = VARSEG(a(0)): p% = VARPTR(a(0))
  446.         total! = SumINTArray(s%, p%, n%)
  447.         x! = CSNG(total! / n%) ' x! = average value of the array
  448.  
  449.         s% = VARSEG(a(0)): p% = VARPTR(a(0))
  450.         s! = StdDevINT(s%, p%, n%)
  451.