The following program solves a system of linear equations:
A X = B
Where A is the matrix of coefficients, and B and X are the coefficient
and solution vectors.
The program is menu driven. The menu prompts which is:
SOLVE LINEAR EQUATIONS MAIN MENU ========= 0) QUIT 1) NEW (KEYBOARD INPUT) 2) FILE INPUT 3) FILE OUTPUT 4) VIEW COEFFICIENTS 5) EDIT MATRIX A ELEMENT 6) EDIT VECTOR B ELEMENT 7) SOLVE SELECT CHOICE BY NUMBER:Asks you to enter one of the following choices:
2. File input. The program prompts you to enter the input filename. This file must contain (on each separate line) the number of equations to solve, the elements of matrix A (listed row-wise with each element on a separate line), and the elements of vector B.
3 File output. The program allows you to store the equation that you entered using option 1 and/or edited using options 5 and 6. The program prompts you to enter the output filename. You can view the contents of that file using your any text editor.
4. View the coefficients. The program displays the elements of matrix A and vector B.
5. Edit an element in the matrix A. This choice first prompts you to enter the
row and column indices for the element in A. The program then displays the
current value you are editing and prompts you to enter a new value.
6. Edit an element in the vector B. This choice first prompts you to enter index
for the element in B. The program then displays the current value you are
editing and prompts you to enter a new value.
7. Solve the set of linear equations. The program displays the
elements of solution vector X.
You select from the above menus by entering the menu number.
Here is an example to solve the following equations:
X1 | + | X2 | + | X3 | = | 6 |
X1 | + | 1.1 X2 | + | X3 | = | 6.2 |
X1 | + | X2 | + | 1.1 X3 | = | 6.3 |
PROMPT/DISPLAY |
ENTER/PRESS |
[RUN] | |
SELECT CHOICE BY NUMBER: | 1[Enter] |
NUMBER OF EQNS? | 3[Enter] |
A(1,1)? | 1[Enter] |
A(1,2)? | 1[Enter] |
A(1,3)? | 1[Enter] |
A(2,1)? | 1[Enter] |
A(2,2)? | 1.1[Enter] |
A(2,3)? | 1[Enter] |
A(3,1)? | 1[Enter] |
A(3,2)? | 1[Enter] |
A(3,3)? | 1.1[Enter] |
B(1)? | 6[Enter] |
B(2)? | 6.2[Enter] |
B(3)? | 6.3[Enter] |
SELECT CHOICE BY NUMBER: | 7[Enter] |
PRESS ANY KEY TO CONTINUE | [A] |
X(1)= 1 | |
X(2)= 2 | |
X(3)= 3 | |
PRESS ANY KEY TO CONTINUE | [A] |
SELECT CHOICE BY NUMBER: | 0[Enter] |
END OF PROGRAM |
Here is the BASIC listing:
OPTION NOLET
OPTION TYPO
! SOLVES LINEAR EQUATIONS
! BY NAMIR C SHAMMAS
! CREATED JAN 2, 2006
! LAST UPDATE:
! VERSION 1.0
DECLARE NUMERIC C, I, J, N
DECLARE STRING A$
DIM A(1,1), AINV(1,1), B(1,1), X(1,1)
CLEAR
N = 0
DO
REM START
PRINT
PRINT TAB(15);"SOLVE LINEAR EQUATIONS"
PRINT TAB(20);"MAIN MENU"
PRINT TAB(20);"========="
PRINT "0) QUIT"
PRINT "1) NEW (KEYBOARD INPUT)"
PRINT "2) FILE INPUT"
PRINT "3) FILE OUTPUT"
PRINT "4) VIEW COEFFICIENTS"
PRINT "5) EDIT MATRIX A ELEMENT"
PRINT "6) EDIT VECTOR B ELEMENT"
PRINT "7) SOLVE"
INPUT PROMPT "SELECT CHOICE BY NUMBER: ": C
REM NEW
IF C=1 THEN
INPUT PROMPT "NUMBER OF EQNS? ":N
MAT REDIM A(N,N), AINV(N,N), B(N,1), X(N,1)
MAT INPUT A
MAT INPUT B
ELSEIF C=2 THEN
INPUT PROMPT "ENTER FILENAME? ":A$
WHEN ERROR IN
OPEN #1: NAME A$, ORG TEXT, CREATE OLD, ACCESS INPUT
INPUT #1: N
MAT REDIM A(N,N), AINV(N,N), B(N,1), X(N,1)
FOR I = 1 TO N
FOR J = 1 TO N
INPUT #1: A(I,J)
NEXT J
NEXT I
FOR I = 1 TO N
INPUT #1: b(I,1)
NEXT I
CLOSE #1
CALL VIEWSYS(A(,),B(,),N)
USE
PRINT "COULD NOT OPEN OR READ FROM FILE ";A$
END WHEN
ELSEIF C=3 AND N>0 THEN
INPUT PROMPT "ENTER FILENAME? ":A$
WHEN ERROR IN
OPEN #1: NAME A$, ORG TEXT, CREATE NEWOLD, ACCESS OUTIN
ERASE #1
PRINT #1: N
FOR I = 1 TO N
FOR J = 1 TO N
PRINT #1: A(I,J)
NEXT J
NEXT I
FOR I = 1 TO N
PRINT #1: b(I,1)
NEXT I
CLOSE #1
PRINT "DATA SAVED IN FILE ";A$
USE
PRINT "COULD NOT OPEN OR WRITE TO FILE ";A$
END WHEN
ELSEIF C=4 AND N>0 THEN
CALL VIEWSYS(A(,),B(,),N)
ELSEIF C=5 AND N>0 THEN
REM EDITA
INPUT PROMPT "ENTER ROW AND COL? ":I,J
PRINT "CURRENT VALUE=";A(I,J)
INPUT PROMPT "NEW VAL? ": C
A(I,J) = C
ELSEIF C=6 AND N>0 THEN
REM EDITB
INPUT PROMPT "ENTER INDEX? ": I
PRINT "CURRENT VALUE=";B(I,1)
INPUT PROMPT "NEW VAL?":C
B(I,1) = C
ELSEIF C=7 AND N>0 THEN
REM SOLVE
MAT AINV = INV(A)
MAT X = AINV*B
PRINT "SOLUTION IS:"
FOR I = 1 TO N
PRINT "X(";I;")=";X(I,1)
NEXT I
ELSE
IF C<>0 THEN PRINT "INVALID CHOICE"
END IF
IF C<>0 THEN
PRINT "PRESS ANY KEY TO CONTINUE";
GET KEY I
END IF
LOOP UNTIL C=0
PRINT "END OF PROGRAM"
END
SUB VIEWSYS(A(,),B(,),N)
DECLARE NUMERIC I, J
PRINT
PRINT "MATRIX/VECTOR IS:"
FOR I = 1 TO N
FOR J = 1 TO N
PRINT A(I,J);"X";STR$(J),
NEXT J
PRINT "=";B(I,1)
NEXT I
PRINT
END SUB
Copyright (c) Namir Shammas. All rights reserved.