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:
1) NEW 2) EDIT A 3) EDIT B 4) SOLVE 5) END?
Asks you to enter one of the following choices:
1. A new set of equations. The program prompts you to enter the number of
equations, the elements of matrix A, and the element of vector B.
2. 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.
3. 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.
4. Solve the set of linear equations. The program displays the elements of
solution vector X. To view the next element press the CONT key.
5. Quit.
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] |
1) NEW 2) EDIT A 3) EDIT B 4) SOLVE 5) END? | 1[END LINE] |
NUMBER OF EQNS? | 3[END LINE] |
A(1,1)? | 1[END LINE] |
A(1,2)? | 1[END LINE] |
A(1,3)? | 1[END LINE] |
A(2,1)? | 1[END LINE] |
A(2,2)? | 1.1[END LINE] |
A(2,3)? | 1[END LINE] |
A(3,1)? | 1[END LINE] |
A(3,2)? | 1[END LINE] |
A(3,3)? | 1.1[END LINE] |
B(1)? | 6[END LINE] |
B(2)? | 6.2[END LINE] |
B(3)? | 6.3[END LINE] |
1) NEW 2) EDIT A 3) EDIT B 4) SOLVE 5) END? | 4[END LINE] |
X(1)= 1 | [CONT] |
X(2)= 2 | [CONT] |
X(3)= 3 | [CONT] |
1) NEW 2) EDIT A 3) EDIT B 4) SOLVE 5) END? | 5[END LINE] |
> |
Here is the BASIC listing:
10 ! SOLVES LINEAR EQUATIONS
20 DESTROY ALL
30 DIM A(1,1), B(1), X(1)
40 REM START
50 INPUT "1) NEW 2) EDIT A 3) EDIT B 4) SOLVE 5) END? ";C
60 IF C=1 THEN 120
70 IF C=2 THEN 190
80 IF C=3 THEN 250
90 IF C=4 THEN 310
100 IF C=5 THEN 380
110 GOTO 40
120 REM NEW
130 INPUT "NUMBER OF EQNS? ";N
140 DESTROY A, B, X
150 DIM A(N,N), B(N), X(N)
160 MAT INPUT A
170 MAT INPUT B
180 GOTO 40
190 REM EDITA
200 INPUT "ENTER ROW, COL"; I, J
210 A$ = STR$(A(I,J))
220 INPUT "NEW VAL?",A$;C
230 A(I,J) = C
240 GOTO 40
250 REM EDITB
260 INPUT "ENTER INDEX"; I
270 A$ = STR$(B(I))
280 INPUT "NEW VAL?",A$;C
290 B(I) = C
300 GOTO 40
310 REM SOLVE
320 MAT X = SYS(A,B)
330 FOR I = 1 TO N
340 DISP "X(";I;")=";X(I)
350 PAUSE
360 NEXT I
370 GOTO 40
380 REM END
390 END
The program uses the variables shown in the following table:
Variable Name |
Contents |
A | Matrix of coefficients |
B | Coefficient vector |
X | Solution vector |
I | Index |
J | Index |
C | Input |
A$ | Default input |
Copyright (c) Namir Shammas. All rights reserved.