Sharp MZ-80A Owner's Manual page 88

Hide thumbs Also See for MZ-80A:
Table of Contents

Advertisement

98
Although the program
code
FOR
IN TO 1 STEP-1
FOR J
=
I-1
TO
N
A(
I
.
N
4-
1
)
=
A
<
I
.
N
+
l)
All.
J>*X(J)
NEXT J
X(
I
>
A(I
.
N
+
D/AU
.
I)
NEXT I
seems satisfactory,
in
this program,
when
I
=
N,
J has
a value of
N
+ 1 and the computer executes the statement within
the loop controlled by J. As it
stands,
a dimensional overflow would
occur
at
X(J) or an
incorrect
answer would result
(even
when X(N+1) is defined)
if
its content is
nonzero.
We can avoid such errors
by placing the step for
finding the
value of unknown
xn
outside of the loop; that
is,
by changing the 1-controlled loop as
follows:
X(N>
-
A< N
.
N-+- 1
)/A(
N
.
N)
FOR I
N
1 TO 1
STEP -1
NEXT
I
We
can
complete the subroutine for solving systems of simultaneous linear equations by
adding a RETURN
statement
to
the end of the above program code.
The essential problem in step
is in how to assign
the unknowns and the coefficients of the simultaneous linear
equations
in
question to
variable N
and 2-dimensional
array
A(N,
N
+
l). Many methods are possible, such as
using the
READ and DATA
statements;
however,
we have decided here
to
enter them one at a time from the keyboard.
In
step
3
,
we
decided
to
display the values of the unknowns on the
CRT
display unit in the
format:
XI
.
X2
These steps can be coded without difficulty. Finally, we obtain
a
complete program as follows:
5 REM
Read data
10 INPUT "Number of unknown
numbers
="
;N
20
DIM A(N,
N+l), X(N)
30 FORSl
=
l
TO N: FORS2=l TO N+l
40 INPUT
A
(SI. S2)
50 NEXT
S2,
SI
100 GOSUB 1000
195 REM
Print Xi
200 FOR 1
=
1 TON
210 PRINT
"X": STR$(I);"=" ;X(I)
220 NEXT I
230 END
995 REM
Elimination
1000
FOR K=l TO N
1
1010 FOR I
=
K+1 TO N
1020 FOR J
=
K+1 TO
N+l
1030 A(I.J)=A(K.J) -AO..!)
*
A(K,K)/A(I,K)
1040 NEXT J,
1,
K
1095 REM
Find Xi
2000 X(N)=A(N,N
+
1)/A(N,N)
2010 FOR I=N
1 TO 1 STEP-1
2020 FOR J
=
I
+ 1
TO N
2030 A(l, N+1)=A(I. N+l) -A(I,
J)
*
X(J)
2040 NEXT
J
2050 X(I)=A(I.N
+ 1)/A(1,I)
2060 NEXT I
2070 RETURN

Advertisement

Table of Contents
loading

Table of Contents