home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
POINT Software Programming
/
PPROG1.ISO
/
basic
/
qlib
/
solve.doc
< prev
next >
Wrap
Text File
|
1993-06-04
|
17KB
|
453 lines
QLIB's SOLVE subroutines and functions provide quick solutions for common
mathematical equations. Many require a math coprocessor. As with many of
QLIB's DATA subroutines, a function or subroutine with INT, LNG, SNG or DBL
in its name is to be used with INTEGER, LONG, SINGLE, or DOUBLE data types,
respectively.
SINGLE and DOUBLE data in SOLVE subroutines and functions must be in IEEE
format. Since this is the default format for QuickBASIC beginning with
version 4.0, and for BC beginning with BC6, this will not be a problem in
most cases.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: f! = C2F(c!)
Function: c! = F2C(f!)
object code: degrees.obj
C2F calculates degrees Farenheit f! from degrees Celcius c!.
F2C calculates degrees Celcius c! from degrees Farenheit f!. C2F
and F2C use the 8087 if available, or use BASIC's 8087 emulator
if no 8087 is in the computer.
Example:
REM $INCLUDE: 'qlib.bi'
f! = 100!
c! = C2F(f!) ' 100 degrees Celcius
' converted to degrees Farenheit
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Subroutine: CubeFitF4(s%, o%, n%, a!, b!, c!, d!)
Subroutine: CubeFitF8(s%, o%, n%, a!, b!, c!, d!)
Subroutine: CubeFitI2(s%, o%, n%, a!, b!, c!, d!)
Subroutine: CubeFitI4(s%, o%, n%, a!, b!, c!, d!)
object file: cubefit.obj
CubeFit subroutines use the Least Squares method to determine
the coefficients of a cubic equation to fit n sets of coordinate data.
The formula is of the form
y = a! + b!x + c!x^2 +d!x^3
CubeFitF4 requires coordinate data as SINGLE data.
CubeFitF8 requires coordinate data as DOUBLE data.
CubeFitI2 requires coordinate data as INTEGER data.
CubeFitI4 requires coordinate data as LONG data.
The CubeFit subroutines use the 8087 extensively; QuickBASIC's 8087
emulator works properly when compiled with the /o switch; if you do
not have a math coprocessor, CubeFit may not work within the
QuickBASIC development environment. Hint: if the computer locks up,
it's not working properly. Save your work before trying CubeFit if
you don't have an 8087.
Example 1:
REM I have 5 data points, and I want a cubic equation
REM that best describes the points
REM I'll use a 2-dimensional array for the first example
DIM points(1,4) AS INTEGER
points(0,0) = x0: points(1,0) = y0
points(0,1) = x1: points(1,1) = y1
points(0,2) = x2: points(1,2) = y2
points(0,3) = x3: points(1,3) = y3
points(0,4) = x4: points(1,4) = y4
s% = VARSEG(points(0,0)): o% = VARPTR(points(0,0)): n% = 5
CALL CubeFitI2(s%, o%, n%, a!, b!, c!, d!)
REM CubeFit returns the formula y = a! + b!x + c!x^2 + d!x^3
Example 2:
REM this time I'll use a one-dimensional integer array
DIM points(9) AS INTEGER
points(0) = x0: points(1) = y0
points(2) = x1: points(3) = y1
points(4) = x2: points(5) = y2
points(6) = x3: points(7) = y3
points(8) = x4: points(9) = y4
s% = VARSEG(points(0)): o% = VARPTR(points(0)): n% = 5
CALL CubeFitI2(s%, o%, n%, a!, b!, c!, d!)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: fact# = Factorial(n%)
object code: factori.obj
Factorials, often used in statistics, may be easily calculated
with this subroutine. fact# is a DOUBLE real number returned as
the factorial of the INTEGER n%. Factorial uses the 8087 if available,
or uses BASIC's 8087 emulator if no 8087 is in the computer.
Example:
REM $INCLUDE 'qlib.bi'
n% = 12: fact# = Factorial(n%)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Subroutine: FPrimeF4(s, p, n)
object file: fprimef4.obj
80x87 required
Subroutine: FPrimeI2(s, p, n)
object file: fprimei2.obj
80x87 not required
Given a polynomial function f(x), FPrime calculates the first
derivative of the function, f'(x). The coefficients of f(x) must all
be INT data for FPrimeI2, or SINGLE data for FPrimeF4.
Example:
DEFINT A-Z
REM I want to calculate the first derivative of the function
REM y = 2 + 3*(x**2) + 2*(x**3) + (x**5)
REM also called a fifth-order polynomial
DIM f(5)
f(0) = 2 ' load the coefficients into the array
f(1) = 0 ' there is no x term
f(2) = 3 ' this is for the 3*(x**2) term
f(3) = 2 ' this is for the 2*(x**3) term
f(4) = 0 ' this is a place holder for the (x**4) term
f(5) = 1 ' this is for the (x**5) term
s = VARSEG(f(0)): p = VARPTR(f(0)): n = 6
call fprimeI2(s, p, n)
REM result is
REM f(0) = 0, f(1) = 6, f(2) = 6, f(3) = 0, f(4) = 5, f(5) = 0
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: fv! = FValue(n%, i!, pmt!, pv!)
object files: fvalue.obj (xtothey.obj)
Requires 80x87
FValue calculates the future value of a constant cash flow
given n% periods, i! periodic interest rate (i! <> 0), pmt! constant
periodic payments and pv! present value. i! interest rate must not
be zero or your program will come to a flying stop.
Example:
REM $INCLUDE: 'qlib.bi'
REM my daughter has $100 in her savings account, which accumulates
REM interest at a 5.25% interest rate, compounded monthly. If I
REM add $25 to her account at the end of each month, what will she
REM have at the end of one year?
i! = .0525 / 12 ' monthly interest rate
n% = 12 ' 12 months
pmt! = 25! ' $25 payment
pv! = 100! ' starting with $100.00
fv! = FValue(n%, i!, pmt!, pv!)
' fv! is the balance in the account
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Subroutine: LineFitF4(s%, o%, n%, a!, b!)
Subroutine: LineFitF8(s%, o%, n%, a!, b!)
Subroutine: LineFitI2(s%, o%, n%, a!, b!)
Subroutine: LineFitI4(s%, o%, n%, a!, b!)
object file: linefit.obj
LineFit subroutines use the Least Squares method to determine
the coefficients of a formula to fit n sets of coordinate data.
The formula is of the form
y = a! + b!x
LineFitF4 requires coordinate data as SINGLE data.
LineFitF8 requires coordinate data as DOUBLE data.
LineFitI2 requires coordinate data as INTEGER data.
LineFitI4 requires coordinate data as LONG data.
The LineFit subroutines use the 8087 extensively; QuickBASIC's 8087
emulator works properly when compiled with the /o switch; if you do
not have a math coprocessor, LineFit may not work within the
QuickBASIC development environment. Hint: if the computer locks up,
it's not working properly. Save your work before trying LineFit if
you don't have an 8087.
Example 1:
REM I have 5 data points, and I want the equation of the line
REM that best describes the points
REM I'll use a 2-dimensional array for the first example
DIM points(1,4) AS INTEGER
points(0,0) = x0: points(1,0) = y0
points(0,1) = x1: points(1,1) = y1
points(0,2) = x2: points(1,2) = y2
points(0,3) = x3: points(1,3) = y3
points(0,4) = x4: points(1,4) = y4
s% = VARSEG(points(0,0)): o% = VARPTR(points(0,0)): n% = 5
CALL LineFitI2(s%, o%, n%, a!, b!)
REM LineFit returns the formula y = a! + b!x
Example 2:
REM this time I'll use a one-dimensional integer array
DIM points(9) AS INTEGER
points(0) = x0: points(1) = y0
points(2) = x1: points(3) = y1
points(4) = x2: points(5) = y2
points(6) = x3: points(7) = y3
points(8) = x4: points(9) = y4
s% = VARSEG(points(0)): o% = VARPTR(points(0)): n% = 5
CALL LineFitI2(s%, o%, n%, a!, b!)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: npv! = NetPValue(s%, o%, n%, i!)
object files: npvalue.obj (xtothey.obj)
Requires 80x87
NetPValue calculates the net present value of a series of uneven
cash flows in the array a!(). Note that the array is of SINGLE data
type. If you are interested, I can make this work with other data
types. i! interest rate must not be zero or your program will come
to a flying stop. This subroutine is for ordinary cash flows, with
payment at the end of each period.
Example:
REM $INCLUDE: 'qlib.bi'
DIM a!(99) ' 100 payments on a loan
i! = .08
REM program establishes values for each payment
s% = VARSEG(a!(0)): o% = VARPTR(a!(0))
npv! = NetPValue(s%, o%, n%, i!)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: pmt! = Payment(n%, i!, pv!, fv!)
object files: payment.obj (xtothey.obj)
Requires 80x87
Payment calculates equal periodic payments required given pv!, i!,
fv! and n%. This pmt! is an ordinary annuity, for example, mortgages,
discounted notes (with or without balloon), direct reduction loans
(with or without balloon). Note that fv! = 0 with no balloon.
Example:
REM $INCLUDE: 'qlib.bi'
REM a modest home in the flatlands of San Leandro, California
REM sells for $200,000. With an annual interest rate of 10%
REM and a 20% down payment, what is the monthly loan payment
REM (principle and interest) on a 30-year loan?
pv! = 200000 * .80 ' loan amount
fv! = 0 ' loan paid off in 30 years
n% = 30 * 12 ' payments are monthly
i! = .1 / 12 ' monthly interest rate
pmt! = Payment(n%, i!, pv!, fv!)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: y# = PSolveF4(s%, p%, n%, x!)
Function: y# = PSolveF8(s%, p%, n%, x!)
Function: y# = PSolveI2(s%, p%, n%, x!)
Function: y# = PSolveI4(s%, p%, n%, x!)
object file: psolve.obj
Requires 80x87
Solves an n-order polynomial function for y! given x!.
The PSolve functions can be twice as fast as equivalent BASIC code.
For PSolveI2, the coefficients in the equations must be INTEGERs.
For PSolveI4, the coefficients in the equations must be LONG integers.
For PSolveF4, the coefficients in the equations must be SINGLE.
For PSolveF8, the coefficients in the equations must be DOUBLE.
Examples of polynomial functions:
y! = 3 + 2*x! + 7*(x!^2) ' a quadratic function, where n = 2
y! = -48 + x! + 3*(x!^2) - x!^3 ' a cubic equation, where n = 3
Example:
DEFINT A-Z
REM I'll use the second polynomial function above to calculate
REM 401 y! values for x! = .5 to 200
DIM c(3) AS INTEGER
DIM y!(400)
c(0) = -48: c(1) = 1: c(2) = 3: c(3) = -1
n = 3
x! = .5
s% = VARSEG(c(0)): p% = VARPTR(c(0))
FOR I = 0 to 400
y!(i) = PSolveI2(s%, p%, n%, x!)
x! = x! +.5
NEXT I
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: pv! = PValue(n%, i!, pmt!, fv!)
object files: pvalue.obj (xtothey.obj)
Requires 80x87
PValue calculates the present value of a constant cash flow
given n% periods, i! periodic interest rate (i! <> 0), pmt! constant
periodic payments and fv! future value. i! interest rate must not
be zero or your program will come to a flying stop.
Example:
REM $INCLUDE: 'qlib.bi'
REM In this case, my goal is to have $10,000 in savings in 10
REM years. If I only want to make an initial deposit, then let
REM the interest accumulate, what deposit should I make to reach
REM that goal?
i! = .08 / 12 ' 8.00% annual interest rate
n% = 120 ' 120 months = 10 years
fv! = 10000.0 ' that's what I want
pmt! = 0.0
pv! = PValue(n%, i!, pmt!, fv!)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Subroutine: QuadFitF4(s%, o%, n%, a!, b!, c!)
Subroutine: QuadFitF8(s%, o%, n%, a!, b!, c!)
Subroutine: QuadFitI2(s%, o%, n%, a!, b!, c!)
Subroutine: QuadFitI4(s%, o%, n%, a!, b!, c!)
object file: quadfit.obj
QuadFit subroutines use the Least Squares method to determine
the coefficients of a formula to fit n sets of coordinate data.
The formula is of the form
y = a! + b!x + c!x^2
QuadFitF4 requires coordinate data as SINGLE data.
QuadFitF8 requires coordinate data as DOUBLE data.
QuadFitI2 requires coordinate data as INTEGER data.
QuadFitI4 requires coordinate data as LONG data.
The QuadFit subroutines use the 8087 extensively; QuickBASIC's 8087
emulator works properly when compiled with the /o switch; if you do
not have a math coprocessor, QuadFit may not work within the
QuickBASIC development environment. Hint: if the computer locks up,
it's not working properly. Save your work before trying QuadFit if
you don't have an 8087.
Example 1:
REM I have 5 data points, and I want the equation of the line
REM that best describes the points
REM I'll use a 2-dimensional array for the first example
DIM points(1,4) AS INTEGER
points(0,0) = x0: points(1,0) = y0
points(0,1) = x1: points(1,1) = y1
points(0,2) = x2: points(1,2) = y2
points(0,3) = x3: points(1,3) = y3
points(0,4) = x4: points(1,4) = y4
s% = VARSEG(points(0,0)): o% = VARPTR(points(0,0)): n% = 5
CALL QuadFitI2(s%, o%, n%, a!, b!, c!)
REM QuadFit returns the formula y = a! + b!x + c!x^2
Example 2:
REM this time I'll use a one-dimensional integer array
DIM points(9) AS INTEGER
points(0) = x0: points(1) = y0
points(2) = x1: points(3) = y1
points(4) = x2: points(5) = y2
points(6) = x3: points(7) = y3
points(8) = x4: points(9) = y4
s% = VARSEG(points(0)): o% = VARPTR(points(0)): n% = 5
CALL QuadFitI2(s%, o%, n%, a!, b!, c!)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Subroutine: Quadratic(a!, b!, c!, x1!, x2!, isquared%)
object file: quad.obj
Requires 80x87
This subroutine solves a quadratic equation for its roots
x1! and x2!. A quadratic equation is of the form
a!*x^2 + b!*x! + c! = 0
where a!, b! and c! are known and a! <> 0. The two solutions of the
equation are found with the formulas
x1! = (-b! + SQRT(b! ^ 2 - 4 * a! * c!)) / (2 * a!)
x2! = (-b! - SQRT(b! ^ 2 - 4 * a! * c!)) / (2 * a!)
The solutions of the equation may be either real or imaginary. Quadratic
returns isquared% = 1 if x1! and x2! are real, and isquared = -1 if the
solutions are imaginary.
Example:
a! = 3.5: b! = 7!: c! = 12!
CALL Quadratic(a!, b!, c!, x1!, x2!, isquared%)
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Function: s! = StdDevINT(s%, p%, n%)
Function: s! = StdDevLNG(s%, p%, n%)
Function: s! = StdDevSNG(s%, p%, n%)
Function: s! = StdDevDBL(s%, p%, n%)
object file: stddev.obj
StdDev subroutines calculate the standard deviation s! of an array
from the array's average value x!. Note that in all StdDev subroutines
s! is a single-precision value. s! is the sample standard deviation;
population standard deviation is calculated from s! with this formula:
sp! = s! * SQRT((n - 1) / n)
StdDev subroutines use the 8087 if available, or use the 8087 emulator
if no 8087 is in the computer.
Example:
REM $INCLUDE: 'qlib.bi'
DIM a(99) ' an integer array of 100 elements
. ' values of each array element established
. ' somewhere in program
.
n% = 100 ' now I want the average and standard
' deviation of the array
s% = VARSEG(a(0)): p% = VARPTR(a(0))
total! = SumINTArray(s%, p%, n%)
x! = CSNG(total! / n%) ' x! = average value of the array
s% = VARSEG(a(0)): p% = VARPTR(a(0))
s! = StdDevINT(s%, p%, n%)