Simultaneous Equation Solver for the hp 39g+

Introduction

Unlike many advanced calculators, the hp 39g+ has no built-in solver for simultaneous equations. It does have the necessary matrix manipulation commands but you need to remember exactly how to use them and how to interpret the results.

This program automates the process and provides prompts and information about the solutions.



Example

Consider the set of three simultaneous equations below:
 2x + 4y - 2z =  42
-3x -  y + 4z = -32
 5x      + 3z =   3

Where x,y and z are the 'unknowns' whose values are to be found.

These equations can be represented as an 'augmented matrix' like so:
  2  4 -2  42 
 -3 -1  4 -32  
  5  0  3   3 

Here the first three columns are the coefficients, including the signs, of the unknowns x,y,z and the fourth column is the total on the right hand side of each equation.

Converting this matrix to 'reduced row echelon form' gives the result:
  1  0  0   3 
  0  1  0   7  
  0  0  1  -4 


In this matrix each of the first three columns contains a single 1, representing x, y or z in the corresponding row, and the final column contains the solutions to the equations, in this case:
  x =  3
  y =  7
  z = -4

Converting a matrix to reduced row echelon form by hand is tedious, but luckily the hp 39g+ contains a built-in command to do this.
Although only three equations with three unknowns were shown in this example, the technique works for any number of unknowns. Remember though that there must be as many independent equations as there are unknowns to solve.


Loading SimulSolve

Copy the files SIMULSOL.000, HP39DIR.000 and HP39DIR.CUR to an empty directory on a PC, start the HP39G Connectivity program and point it to the directory holding the three files.
The program consists of a single file, SimulSolve, which should be downloaded from the computer into the PROGRAM catalogue part of the hp 39g+'s memory using the RECV option on the calculator.



Running SimulSolve

Run SimulSolve from the PROGRAM catalogue or the HOME screen.
The program prompts for the number of unknowns in the set of equations, with a default value based on the current size of matrix M1. Enter a different number of unknowns if necessary and press ENTER.

Prompt for number of unknowns
Type the number of unknowns in the equations and press ENTER

The following screen is now shown as a reminder of how to enter the coefficients:

Coefficient entry instructions
Format of equations and how to enter coefficients

(The display is 'frozen' at this point; press any key to continue.)

After pressing a key the Matrix Editor is displayed for matrix M1.
Enter the coefficients for each equation into each row of the matrix, with the 'equals' part of the equation in the last column. Do not forget to use negative numbers if the coefficient is preceded by a minus sign, and if any of the unknowns are not used in a particular equation then enter zero as the coefficient. Obviously the unknowns need to be in the same order in each equation.
Note that if the number of unknowns entered in the previous step corresponds to the size M1 was already then the previous contents of M1 will be shown. This is useful for making small changes to a previously-entered set of equations.
If you have chosen a different number of unknowns then all the elements in M1 will be zeroed, as in this view:

Zeroed coefficients
Zeroed coefficients after changing the number of unknowns.

The number of rows and columns in M1 depends on the number of unknowns chosen.
Using the earlier example of three equations, after entering all the coefficients into M1 the display should look like this:

All coefficients entered
Coefficients of the equations entered

When all the coefficients have been entered press Softkey 6 (OK) to continue. Note that you should not delete any rows or columns from M1; If there are N unknowns then M1 must have N rows and N+1 columns filled in.

After a short pause the following display appears briefly:

Unique solution to equations
This system of equations has a unique solution for X, Y, Z.

This tells you that the system of equations could indeed be solved and that the values of the unknowns are now stored in matrix M2, with the first unknown in M2(1), the second unknown in M2(2), etc. After a few seconds the display changes to the Matrix Editor but this time showing M2 so that the answers can be viewed:

Solutions to equations
The solutions to the equations

Pressing Softkey 6 (OK) returns to the PROGRAM catalogue or the HOME screen, but the solutions remain in M2. The original coefficient matrix is still in M1.


Error Messages

When prompted for the number of unknowns, entering zero or a negative number produces the error message Minimum is 1, and the prompt is repeated. (The program will accept a system of one equation with one unknown, even though the solution is trivial.)

If the message Insufficient Memory - Edit program? appears, either after choosing the number of unknowns or after entering the coefficients, then the calculator does not have enough free memory to create the matrices.
Press Softkey 5 (NO) in reply and exit the program. If possible clear some space by deleting unwanted matrices, lists or graphic objects and run the program again.
Otherwise you have reached the limit of the calculator. This error is trapped by the operating system rather than the program because it is impossible to know in advance how much free memory the calculator will have, and I know of no way to obtain this information in a running program.

If any rows or columns were deleted from M1 while entering the coefficients then the message Bad Argument Value - Edit program? appears after pressing OK.
Press Softkey 5 (NO) in reply to exit the program, and run it again, leaving the correct number of rows and columns.

It is possible for the system of equations to be inconsistent, that is they contradict themselves. A simple example would be:
 2x + 4y - 2z =  42
-3x -  y + 4z = -32
-3x -  y + 4z = -20

Here the last two equations have the same coefficients of x,y,z but a different total, which is clearly nonsensical. Sometimes it is not so obvious that the equations are inconsistent but if they are then there are no solutions. In this situation SimulSolve shows:

No solutions
The equations are inconsistent and have no solutions.

The display pauses at this point, and pressing a key returns to the screen from which the program was called. There will be nothing meaningful in M2 so it is not displayed.
The equations will be stored in M1 so the program can be run again and the previous values edited if there was just a typing error.

A second possibility is that the equations are not all independent. The equations being independent means that none of them can be obtained just by adding together other equations, or multiplying one by a factor. An example of non-independent equations is:
 2x + 4y - 2z =  42
-3x -  y + 4z = -32
 -x - 3y + 2z =  10

At first sight these all appear to be different but closer inspection reveals that the third equation is actually just the sum of the first two, so that in reality there are only two different equations.
With three unknowns but only two equations there are an infinite number of solutions and the calculator reports:

Infinitely many solutions
The equations are not independent and there are an infinite number of solutions.

Again the display pauses at this point, and pressing a key returns to the screen from which the program was called. The contents of M2 will not be particularly useful so it is not displayed.
It will be necessary to identify which is/are the dependent equation(s) and find an alternative, genuinely different, one before running the program again.



Hardware Requirements

SimulSolve runs on a Hewlett-Packard 39g+ calculator. It should also work on the 39g and 40g, and possibly the 38g, but has not been tested.

The program takes up 0.9 kilobytes of RAM when first loaded, which expands to 2.5 kilobytes when run. The maximum number of unknowns that can be solved for is limited by the amount of free memory. For the hp 39g+ with most of its memory free this limit is about 80 unknowns. (Which would be very time-consuming to enter since there would be 6480 numbers to type in.)



Variables Used

SimulSolve uses the following HOME variables, and will therefore overwrite any existing information stored in them:

  • Real variables I, S, U
  • Matrices M1, M2


Speed

Systems of 3 or 4 equations are solved almost instantly. 10 equations take about 4 seconds and 20 equations take 25 seconds. 50 equations take around 7 minutes to solve.



Program Listing

01) INT(ÖÕLIST(SIZE(M1)))®S:
02) DO
03) INPUT U;"Simultaneous Equations";
04) "Unknowns:";"How many unknowns?";U:
05) INT(U)®U:
06) IF U<1 THEN MSGBOX "Minimum is 1":END:
07) UNTIL U>0 END:
08) IF U¹S THEN
09) MAKEMAT(0,U,U+1)®M1:
10) ELSE REDIM M1;{U,U+1}:END:
11) ERASE:
12) DISP 1;"Enter coeffs. c of":
13) DISP 3;"c1X+c2Y+c3Z+...=d1":
14) DISP 5;"into M1 columns 1-"U:
15) DISP 6;"and d into column "U+1:
16) FREEZE:
17) EDITMAT M1:
18) ERASE:
19) SUB M1;M1;{1,1};{U,U+1}:
20) RREF(M1)®M2:
21) 0®S:
22) FOR I=1 TO U;
23) S+M2(U,1)®S:
24) DELCOL M2;1:
25) END:
26) IF S==0 THEN
27) BEEP 300;.3:BEEP 200;.8:
28) DISP 1;"** Problem **":
29) IF M2(U,1)==0 THEN
30) DISP 3;"Equations not":
31) DISP 4;"independent.":
32) DISP 5;"Infinitely many":
33) DISP 6;"solutions.":
34) ELSE
35) DISP 3;"Equations inconsistent":
36) DISP 4;"No solutions.":
37) END:
38) FREEZE:
39) ELSE
40) BEEP 900;.2:BEEP 700;.3:
41) DISP 3;"Equations have unique":
42) DISP 4;"solution.":
43) DISP 6;"X,Y,Z... in M2(1.."U")":
44) WAIT 4:
45) EDITMAT M2:
46) END:

Note: ® means 'STO'
The line numbers are just for convenience and not part of the program.
Comments

01) Initial number of unknowns based on
current size of M1.
03) Show prompt and get the number of
04) unknowns.
05) U must be an integer >= 1
06) Show error box if not and
07) repeat prompt.
08) If number of unknowns has changed then
09) create a new blank matrix,
10) else ensure matrix is correct size.
11) Clear the display.
12) Display a reminder of how to enter
13) the coefficients into M1.
14) 
15) 
16) Pause until a key is pressed.
17) Call Matrix Editor for M1.
18) Clear the display.
19) Ensure M1 is still the same size.
20) Obtain the solution in M2.
21) Zero solved? counter.
22) Go through each column of M2.
23) Count ones in last row of M2.
24) Delete all but last column of M2.
25) 
26) If there were no ones in last row
27) then equations could not be solved.
28) Show error message.
29) If last 'solution' came out as
30) zero then the equations are not
31) independent so show suitable
32) error message.
33) 
34) If last 'solution' was not zero
35) then equations are inconsistent.
36) Report this.
37) 
38) Pause while message is read.
39) If last row was not all zeroes
40) then equations have a unique
41) solution.
42) Report this, and also where
43) results are stored.
44) Let user view message then
45) show solutions in matrix editor.
46) End of program.

Program written by Peter Ochocki, December 2005.