HP Forums
(12C) Solve a system of linear equations of 2 Unknown - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (12C) Solve a system of linear equations of 2 Unknown (/thread-10376.html)



(12C) Solve a system of linear equations of 2 Unknown - Gamo - 03-23-2018 04:24 AM

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

Remark: If "Error" show that indicate determinant D = 0, where D = ad - bc

Example:
X + Y = 10
X - Y = 4

1 ENTER 1 ENTER 10 R/S > 1,111,111,111 (Display First Equation Input)
1 ENTER 1 CHS ENTER 4 R/S > 2,222,222,222 (Display Second Equation Input)
R/S > -2 D
R/S > 7 (X)
R/S > 3 (Y)

Program:
Code:

01 STO 1
02 Rv
03 STO 2
04 Rv
05 STO 3
06 FIX 9
07  9
08  1/x
09  EEX
10  1
11  0
12  x
13 FIX 4
14 R/S
15 STO 4
16 Rv
17 STO 5
18 Rv
19 STO 6
20 FIX 9
21  4
22  .
23  5
24  1/x
25 EEX
26  1
27  0
28  x
29 FIX 4
30 R/S
31 RCL 3
32 RCL 4
33  x
34 RCL 6
35 RCL 1
36  x
37  -
38 RCL 3
39 RCL 5
40   x
41 RCL 2
42 RCL 6
43  x
44  -
45 R/S
46 STO 7
47  /
48 RCL 1
49 RCL 5
50  x
51 RCL 2
52 RCL 4
53  x
54  -
55 RCL 7
56  /
57 R/S
58 X<>Y
59 GTO 00

Example of the "No Solutions Equation"

x - 2y = -4
-3x + 6y = 0

1 ENTER 2 CHS ENTER 4 CHS R/S > 1,111,111,111
3 CHS ENTER 6 ENTER 0 R/S > 2,222,222,222

R/S > 0
R/S > Error 0

This cause Error because each of the two lines has the same slope, 1/2, so the lines don't intersect.



Gamo


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

(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


RE: (12C) Solve a system of linear equations of 2 Unknown - Gamo - 03-23-2018 12:02 PM

Hello Dieter

Any comment is very welcome. I like how you improve and make correction on how I program. I'm not the expert here and I like to learn on how to program it better. Each time that you give me a comment I look over, make change and try to improve the next time. Any suggestion is welcome too.

Very nice idea to also try to make the program as user friendly as possible so that to avoid any mistake.
I have learn that putting ..... 1 R/S and .....2 R/S to separate equation #1 and #2 then show Determinant with the next R/S so that not to confuse user.

I have changed the #1 and #2 equations display and triple Rv to RCL 4

Thank You

Gamo


RE: (12C) Solve a system of linear equations of 2 Unknown - Dieter - 03-23-2018 12:27 PM

(03-23-2018 12:02 PM)Gamo Wrote:  Any comment is very welcome. I like how you improve and make correction on how I program.

Fine, thank you.

(03-23-2018 12:02 PM)Gamo Wrote:  Very nice idea to also try to make the program as user friendly as possible so that to avoid any mistake.
I have learn that putting ..... 1 R/S and .....2 R/S to separate equation #1 and #2 then show Determinant with the next R/S so that not to confuse user.

All this looks even nicer if the display shows 1,111111–11 and 2,222222–22. This shows more clearly that this is a marker and not an entered number. You can see how this looks like if you store these two numbers in R7 and R8 and replace the "1" and "2" with RCL 7 and RCL 8. ;-)

BTW, if you replace the final "GTO 00" in my version with "GTO 23" pressing R/S continuously displays X, Y, X, Y, X, Y...

Dieter


RE: (12C) Solve a system of linear equations of 2 Unknown - Gamo - 03-24-2018 03:22 AM

Program Update: Changed the Display Marker Input

Instead of the equation input display as 1 and 2 changed to

Equation Input 1 to 1,111,111,111
Equation Input 2 to 2,222,222,222

Gamo


RE: (12C) Solve a system of linear equations of 2 Unknown - Dieter - 03-24-2018 08:21 AM

(03-24-2018 03:22 AM)Gamo Wrote:  Program Update: Changed the Display Marker Input

Instead of the equation input display as 1 and 2 changed to

Equation Input 1 to 1,111,111,111
Equation Input 2 to 2,222,222,222

Looks good. But the FIX commands are not required, and the 111.... can be reused to get the 222....:

Code:
01 STO 1
02 Rv
03 STO 2
04 Rv
05 STO 3
06  9
07 EEX
08  1
09  0
10 CHS
11 1/x
12 STO 0   // show 1111111111
13 STO+0   // and keep 2222222222 in R0
14 R/S
15 STO 4
16 Rv
17 STO 5
18 Rv
19 STO 6
20 RCL 0   // show 2222222222
21 R/S
.. ...

Nine steps less. :-)

You can also do this with R7, but since here storage arithmetics are not possible you would have to replace the STO 0  STO+0 with STO 7 and the RCL 0 with RCL 7  2  x.

Dieter