Post Reply 
(12C) Solve a system of linear equations of 2 Unknown
03-23-2018, 09:40 AM
Post: #2
RE: (12C) Solve a system of linear equations of 2 Unknown
(03-23-2018 04:24 AM)Gamo Wrote:  Solve a system of linear equations for two unknown variables.

This program solves for x and y, given the equation
aX + bY = e
cX + dY = f

Gamo, this is a nice and functional program. It works correctly and solves a common problem. And it also is a nice example where applying some basic rules of good programming style may make it much more reliable and robust. So I hope you do not mind when I take the opportunity to remind us all of a basic rule for better programming:

When a program stops with R/S for display or data entry, it is always a good idea not to rely on a certain stack content when the program continues. The user may have performed a calculation before he entered a value, or some other key may have been pressed that disturbs the stack. This would cause the program to continue with wrong data and thus wrong results.

Take a look at the R/S in line 12. The next steps rely on the fact that the T-register remains unchanged. The user only has to press a number key, and T is lost. Here the 3x Rv is not required at all, simply replace this with "RCL 4" and the problem cannot occur. The program will safely continue with the correct data.

Then take a look at line 28-30. The determinant is calculated and R/S displays it. But it has not been stored at this point. It is stored after the R/S. Imagine the user inadvertedly presses a key here: then the number that happens to be in X is stored in the next step as the (wrong) determinant and the complete calculation is void.

As a general rule you should always first store a result, then display it. And, if possible, always write the program in a way that does not rely on previous stack contents when the user continues with R/S. Always remember: the common user is a strange beast. He may press keys that you don't want him to press, so you better play safe and make sure that this does not cause unexpected problems.

All this can be done easily here:

- After all six coefficients have been entered, calculate the determinant, then store it, and finally, once it has been saved, display it.

- Then calculate X. Do not use previous stack content – all required data is available in the storage registers from where it can be safely recalled.

- Finally calculate Y in the same way.

Code:
01 STO 1   // store coefficients of equation 1
02 Rv
03 STO 2
04 Rv
05 STO 3
06 1
07 R/S     // display "1"

08 STO 4   // store coefficients of equation 2
09 Rv
10 STO 5
11 Rv
12 STO 6
13 2
14 R/S     // display "2"

15 RCL 3   // compute determinant D
16 RCL 5
17  x
18 RCL 2
19 RCL 6
20  x
21  -
22 STO 0   // save D
23 R/S     // then stop and display D

24 RCL 1   // compute X
25 RCL 5
26  x
27 RCL 2
28 RCL 4
29  x
30  -
31 RCL 0
32 /
33 R/S     // stop and display X

34 RCL 3   // compute Y
35 RCL 4
36  x
37 RCL 1
38 RCL 6
39  x
40  -
41 RCL 0
42  /
43 GTO 00  // stop and display Y

Again, I hope you do not mind when I take this opportunity to discuss a basic rule of RPN programming that makes programs more reliable and safer to use.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (12C) Solve a system of linear equations of 2 Unknown - Dieter - 03-23-2018 09:40 AM



User(s) browsing this thread: 1 Guest(s)