Solving Simultaneous Equations

Author(s): Lori Olson
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n2 (Volume 3, Issue 2)
Date: February 1982
Pages: 9

This 8K/1K program uses Crout’s reduction to solve simultaneous equations. The number of variables you can use is limited only by your available memory; in 1K this program solves up to 4 equations in variables

When you type in the program, use the INPUT keyword for the input prompts in lines 2 and 25. For example, type 2 INPUT then backspace and hit PRINT. You’ll get a space between ” and INPUT, but it saves bytes. RUN the program, then enter the number of equations to solve in response to INPUT N

Enter the coefficients of the variables in matrix form. Say you’re solving two equations in two variables: 4a+2b=17 and 2a+7b=10. In matrix form, your variables look like this:

         a        b        result
equ.1    4        2          17
equ.2    2        7          10

This is equivalent to this matrix:

         a        b        result
equ.1  K(1,1)   K(1,2)    K(1,3)
equ.2  K(2,1)   K(2,2)    K(2,3)

The first prompt asks for K(1,1), so enter 4. K(1,2) would be 2, K(1,3) would be 17. Then K(2,1) is 2, and so on.

The computer returns the answer in the form U(1)=(solution to first variable) and U(2)=(solution to second variable).

To find a polynomial of order N-1 to fit N points, add and change these lines:

  12 PRINT " INPUT X";I
14 INPUT X
16 PRINT " INPUT Y";I
18 INPUT K(I,N+1)
19 CLS delete lines 25, 35 change lines:
20 FOR J=1 TO N
30 LET K(I,J)=X**(J-1)

Enter the X and Y coordinates individually. N now equals the number of pairs of points. The computer returns the coefficients of the polynomial in this form:

 Y=U(1)+U(2)X+U(3)X²+U(4)X³ etc.
or Y=U(Z)X^(Z-1).
   2 PRINT " INPUT N"
4 INPUT N
6 CLS
8 DIM K(N,N+1)
10 FOR I=1 TO N
20 FOR J=1 TO N+1
25 PRINT " INPUT K(";I;",";J;")"
30 INPUT K(I,J)
35 CLS
40 NEXT J
50 NEXT I
60 FOR I=1 TO N
70 FOR J=I TO N
80 FOR H=I TO I-1
90 LET K(J,I)=K(J,I)-K(J,H)*K(H,I)
100 NEXT H
110 NEXT H
120 FOR J=I+1 TO N+1
130 FOR H=1 TO I-1
140 LET K(I,J)=K(I,J)-K(I,H)*K(H,J)
150 NEXT H
160 LET K(I,J)=K(I,J)/K(I,I)
170 NEXT J
180 NEXT I
190 FOR I=N TO 1 STEP -1
200 FOR H=I+1 TO N
210 LET K(I,N+1)=K(I,N+1)-K(I,H)*K(H,N+1)
220 NEXT H
230 NEXT I
240 FOR I=1 TO N
250 PRINT "U(";I;")=";K(I,N+1)
260 NEXT I
Syntactic Sum: 30176, 8K

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.