Microsoft KB Archive/71779

= Example of Cramer's Rule Using Basic PDS Matrix Math Toolbox =

Article ID: 71779

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft BASIC Professional Development System 7.1
 * Microsoft BASIC Professional Development System 7.0

-



This article was previously published under Q71779



SUMMARY
The program below demonstrates an implementation of Cramer's Rule using the Matrix Math Toolbox of Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1. Cramer's Rule is a mathematical formula for determining the solution of a system of linear equations.



MORE INFORMATION
Cramer's Rule is a rule for solving a system of linear equations such as   A11*X + A12*Y = B1   A21*X + A22*Y = B2            (1) when the determinant of the coefficient matrix, as follows, is different from zero:   D = det(A) = det | A11 A12 | | A21 A22 | (X and Y are two unknown values that simultaneously solve all equations in the system. A11, A12, B1, A21, A22, and B2 are constants, also known as coefficients, that define the linear equation in terms of X and Y.)

The solution of the system of linear equations in (1) is unique when D is not equal to 0, and may be calculated from the following formulas:   X = det | B1 A12 |    ,     Y = det | A11 B1 | | B2 A22 |                  | A21 B2 | --              --               D                             D The numerator in the formula for X comes from replacing the first column in A (the X column) by the column of the constants B1 and B2 (the B column). Replacing the Y column by the B column gives the numerator of the Y solution.

This information is taken from &quot;Calculus and Analytical Geometry,&quot; by Thomas and Finney, sixth edition, section A-6.

Code Example
The code example below demonstrates Cramer's Rule for a linear system of equations.

Compile and link the program from MS-DOS as follows: BC CRAMERS.BAS; LINK CRAMERS,,,MATBEFR.LIB;

'This program uses Cramer's Rule to determine the solution to a linear 'system of equations. This example solves for a Current matrix (I) 'when given the Resistor matrix (R) and the Voltage matrix (V), using 'the equation R * I = V. 'Note that this example uses the Long data type and floating 'point values are not allowed as input. This program makes a call to 'the MatDetL% function to determine the Determinant of an array of 'LONG type. To run this program, the MATB.BAS matrix math 'module must be loaded, or the equivalent Quick library, 'MATBEFR.QLB, can be loaded when starting the QBX.EXE environment. 'For example: ' '  QBX /L MATBEFR ' 'Note that the underscore character (_) shown here is for viewing 'convenience and should be taken out when implementing the program. '(QB.EXE and BC.EXE automatically take out the underscore.)

DEFLNG A-Z                   'Define all variables to be LONG integer DECLARE SUB InputMatrix (Resistor, Voltage, Current, _                                                 Scratch, n%) DECLARE SUB EraseMatrix (Resistor, Voltage, Current, Scratch) DECLARE SUB CramersRule (Resistor, Voltage, Current, _                                                 Scratch, n%) DECLARE FUNCTION MatDetL% (a AS LONG, det&) CONST TRUE = -1                 'Define true and false CONST FALSE = NOT TRUE REM $DYNAMIC                   'Make all arrays dynamic OPTION BASE 1                  'Set the default base value for arrays DIM MatrixDim AS INTEGER       'The dimension of the matrices DIM okay AS INTEGER            'variable: checks for valid dimension

DO CLS LOCATE 1, 10 PRINT &quot;This uses Cramer's Rule to calculate matrix solutions&quot; DO LOCATE 3, 5 PRINT &quot;Enter dimension of square (n x n) matrix--between 1 and 5= &quot;; INPUT MatrixDim IF (MatrixDim < 6) AND (MatrixDim > 0) THEN okay = TRUE ELSE okay = FALSE PRINT &quot;Number of dimensions must be between 1 and 5&quot; END IF LOOP UNTIL okay

LOCATE 4, 1 PRINT STRING$(50, 32) 'clears line in case of input error REDIM Resistor(MatrixDim, MatrixDim)  'dimension dynamic arrays REDIM Voltage(MatrixDim) REDIM Current(MatrixDim) REDIM Scratch(MatrixDim, MatrixDim) CALL InputMatrix(Resistor,Voltage,Current,Scratch,MatrixDim) CALL CramersRule(Resistor,Voltage,Current,Scratch,MatrixDim) CALL EraseMatrix(Resistor, Voltage, Current, Scratch) INPUT &quot;Do another? &quot;, answer$ LOOP UNTIL UCASE$(answer$) = &quot;N&quot; END

SUB CramersRule (Resistor, Voltage, Current, Scratch, n%)

'Subroutine that evaluates determinates of matrixes according to   'Cramer's Rule. N% is the dimension of the Square matrix.

DIM i, j, column AS INTEGER

ErrorCode = MatDetL%(Resistor, DetSoln) IF (ErrorCode <> -1 AND ErrorCode <> -2) THEN 'check for Det = 0 and 'that matrix is square

'Below are the loops that exchange columns of the Resistor matrix 'with the Voltage matrix. Column tells which column is being replaced. FOR column = 1 TO n%       FOR i = 1 TO n%            FOR j = 1 TO n%                IF j = column THEN      'replace column #n w/ solution Scratch(i, j) = Voltage(i) ELSE Scratch(i, j) = Resistor(i, j)               END IF            NEXT j        NEXT i        ErrorCode = MatDetL%(Scratch, Current(column)) IF ErrorCode > -2 THEN PRINT &quot;I&quot;; LTRIM$(STR$(column)); &quot; = &quot;;  'Tell which value PRINT Current(column) / DetSoln    'of I and the solution. END IF  NEXT column ELSE PRINT &quot;Determinant = 0 or Not Square Matrix&quot; END IF END SUB

SUB EraseMatrix (Resistor, Voltage, Current, Scratch) 'Deallocate memory used by dynamic arrays ERASE Resistor ERASE Voltage ERASE Current ERASE Scratch END SUB

SUB InputMatrix (Resistor, Voltage, Current, Scratch, MatDim%) 'This subroutine gets the Resistor and Voltage matrix values

DIM i, j AS INTEGER

LOCATE 9, 13: PRINT &quot;R&quot;           'Tell user where Resistor matrix is FOR i = 1 TO MatDim%               'displayed FOR j = 1 TO MatDim% 'loop to setup which resistor value is being entered

a$ = &quot;R&quot; + LTRIM$(STR$(i)) + LTRIM$(STR$(j)) LOCATE 4, 1: PRINT &quot;Enter Resistor Matrix &quot;; a$; &quot; =  &quot;; LOCATE 4, 26: INPUT Resistor(i, j)          LOCATE 10 + i, 7 * j: PRINT USING &quot;#######&quot;; Resistor(i, j)        NEXT j NEXT i LOCATE 9, 51: PRINT &quot;V&quot;             'Tell user where Voltage matrix is FOR i = 1 TO MatDim%                'displayed 'loop to setup which Voltage value is being entered a$ = &quot;V&quot; + LTRIM$(STR$(i)) LOCATE 5, 1: PRINT &quot;Enter voltage &quot;; a$; &quot; =  &quot;; LOCATE 5, 18: INPUT Voltage(i) LOCATE 10 + i, 50: PRINT Voltage(i) NEXT i END SUB For more information on the Matrix Math Toolbox, query on the following words:

Basic and PDS and matrix and math

Additional query words: BasicCom 7.00 7.10

Keywords: KB71779

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.