(12C) Solve a system of linear equations of 2 Unknown

03232018, 04:24 AM
(This post was last modified: 03242018 03:31 AM by Gamo.)
Post: #1




(12C) Solve a system of linear equations of 2 Unknown
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:
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 

03232018, 09:40 AM
Post: #2




RE: (12C) Solve a system of linear equations of 2 Unknown
(03232018 04:24 AM)Gamo Wrote: Solve a system of linear equations for two unknown variables. 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 Tregister 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 2830. 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 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 

03232018, 12:02 PM
(This post was last modified: 03232018 12:05 PM by Gamo.)
Post: #3




RE: (12C) Solve a system of linear equations of 2 Unknown
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 

03232018, 12:27 PM
(This post was last modified: 03232018 12:36 PM by Dieter.)
Post: #4




RE: (12C) Solve a system of linear equations of 2 Unknown
(03232018 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. (03232018 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. 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 

03242018, 03:22 AM
Post: #5




RE: (12C) Solve a system of linear equations of 2 Unknown
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 

03242018, 08:21 AM
(This post was last modified: 03242018 08:22 AM by Dieter.)
Post: #6




RE: (12C) Solve a system of linear equations of 2 Unknown
(03242018 03:22 AM)Gamo Wrote: Program Update: Changed the Display Marker Input Looks good. But the FIX commands are not required, and the 111.... can be reused to get the 222....: Code: 01 STO 1 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 

« Next Oldest  Next Newest »

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