Post Reply 
Improving the solver experience on the 41C
02-16-2016, 03:41 AM
Post: #1
Improving the solver experience on the 41C
One of the features I like about later-model HP calculators is the use of named variables and the ability to solve for any one of the variables in an equation. The solver function available in the Advantage module is decent enough from a functional standpoint, but one generally needs to write multiple RPN functions for the same equation, depending on the variable being solved for. I came across this program which stores the values of all variables in registers and uses indirect register addressing to update the unknown variable during root finding. I extended this technique to make things more user-friendly. Here is the program I came up with:

Code:
 01◆LBL "SLV"
 02 "FN NAME?"
 03 AON
 04 STOP
 05 AOFF
 06 ASTO 06
 07◆LBL 01
 08 SF 05
 09 XEQ IND 06
 10 AVIEW
 11 STOP
 12 GTO 01
 13◆LBL A
 14 STO 01
 15 GTO 01
 16◆LBL B
 17 STO 02
 18 GTO 01
 19◆LBL C
 20 STO 03
 21 GTO 01
 22◆LBL D
 23 STO 04
 24 GTO 01
 25◆LBL E
 26 STO 05
 27 GTO 01
 28◆LBL a
 29 1
 30 STO 00
 31 GTO 00
 32◆LBL b
 33 2
 34 STO 00
 35 GTO 00
 36◆LBL c
 37 3
 38 STO 00
 39 GTO 00
 40◆LBL d
 41 4
 42 STO 00
 43 GTO 00
 44◆LBL e
 45 5
 46 STO 00
 47◆LBL 00
 48 1
 49 1 E-99
 50 "INTERVAL?"
 51 PROMPT
 52 CLA
 53 ARCL 06
 54 CF 05
 55 SOLVE
 56 STOP
 57 GTO 01
 58 END

The SLV program has two requirements: the Advantage module SOLVE function (or another root finder) and an RPN function that you write to evaluate your equation. To illustrate how the equation function is written, let's use a simple example from classical mechanics: x=v0*t+(1/2)*a*t^2. The RPN function for this equation is written in the following manner:

Code:
 01◆LBL "MOT"
 02 FS? 05
 03 GTO 00
 04 STO IND 00
 05 RCL 02     ' v0
 06 RCL 03     ' t
 07 *
 08 RCL 04     ' a
 09 RCL 03     ' t
 10 X↑2
 11 *
 12 2
 13 /
 14 +
 15 RCL 01     ' x
 16 -
 17 RTN
 18◆LBL 00
 19 " X V0 T A"
 20 END

The function starts with a global label. Following that, a check of flag 05 is performed, which informs the function whether it's being called during root finding or for generating a user prompt. When evaluating the function during root finding, SLV clears flag 05, and the current estimate for the unknown variable is stored indirectly into the appropriate register in line 04. Lines 05-16 evaluate the function by recalling the values of the variables and performing the arithmetic. This is where you would enter the RPN steps for your equation f(x1,x2,...)=0. You can use up to 5 variables, and it's up to you to decide which variable goes into which register, 01-05. The alpha string following LBL 00 provides a user prompt and can be designed so that the variable name appears above the top-row key corresponding to the register (key "A" for 01, etc.). Here are the instructions for running the SLV program:

  1. Enter your equation as an RPN function as described previously.
  2. XEQ "SLV"
  3. Enter the name of your function (alpha is already on), followed by R/S.
  4. You will now have a list of variables above the top-row keys. If you forget the order, you can restore this prompt at any time by hitting R/S.
  5. Enter the values for n-1 variables by entering the value and then pressing the top-row key corresponding to that variable (or hit R/S first if you forgot which key).
  6. Press [SHIFT] followed by the top-row key corresponding to the variable you wish to solve for.
  7. (Optional) Enter the initial interval (a ENTER b) over which you wish to search for the value of the unknown variable, followed by R/S. If you press R/S without entering anything, the interval is set to (0, 1|.
  8. The value of the root is displayed if the solver was successful. At this point, you can press R/S to solve the equation again, for the same or a different variable.

I like this technique because the equation and its prompting string are stored as a single function. While it takes a moment to plan out register usage, once you've created the function, you don't need to remember which register is which any more. I'd be pleased to hear any suggestions for improvement. It seems like something similar should have been written in the past, but I was unable to find anything. The AECROM has a nice interface for entering an equation and generating a program from it, but you have to decide ahead of time which variable you're going to solve for.

David Brunell
Houston, Texas
Find all posts by this user
Quote this message in a reply
02-16-2016, 10:19 AM (This post was last modified: 02-16-2016 10:42 AM by Ángel Martin.)
Post: #2
RE: Improving the solver experience on the 41C
Very nice improvement on Stefan Vorkoetter's program; here's my proposal using the data entry flag F22 to re-write your SLV routine as follows:

Code:

1    LBL "SLV+
2    "F. NAME?"
3    AON
4    STOP
5    AOFF
6    ASTO 06
7    LBL 01
8    SF 05
9    XEQ IND 06
10    CF 22
11    PROMPT
12    GTO 01
13    LBL A
14    1
15    GTO 02
16    LBL B
17    2
18    GTO 02
19    LBL C
20    3
21    GTO 02
22    LBL D
23    4
24    GTO 02
25    LBL E
26    5
27    LBL 02
28    FC? 22
29    GTO 00
30    X<>Y
31    STO IND Y
32    GTO 01
33    LBL 00
34    STO 00
35    1
36    1 E-99
37    "a^b=?"
38    PROMPT
39    CLA
40    ARCL 06
41    CF 05
42    FROOT
43    STOP
44    GTO 01
45    END

Only 45 steps are needed, and a bunch of bytes are saved as well. Also no need to use [SHIFT] to determine the unknown - so it's standard with the other solvers, like the TMV$. So you'll enter all the knowns and then press the soft-key corresponding to the unknown to trigger the SOLVE process. BTW I've used FROOT from the SandMath instead..

Writing an MCODE equation solver has always lured me but I guess I don't know enough to tackle the job.

BTW - There's one ROM image I prepared a while ago that deals with this very same subject; it's called "Interchangeable Solutions" and contains the FOCAL programs of the same name available at TOS? (written by Humbert Hans Suarez, from the User's Program Library Europe, REF#01475C). It's a tad more complex but quite flexible in the number of variables and governing equations.

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
02-16-2016, 07:44 PM
Post: #3
RE: Improving the solver experience on the 41C
Thanks, Ángel. That is a nice simplification and space savings. I'll check out the Interchangeable Solutions ROM as well. It's not on TOS, but I found it on Geir's site.

It's too bad the 41C doesn't have a 2-line display as that would improve the user interface. However, I happen to have a 2-line OLED display that fits perfectly where the existing LCD sits, and a small ARM processor board running nonpareil...

David Brunell
Houston, Texas
Find all posts by this user
Quote this message in a reply
02-17-2016, 06:25 AM (This post was last modified: 02-17-2016 03:52 PM by Ángel Martin.)
Post: #4
RE: Improving the solver experience on the 41C
Last night I re-submitted to TOS the Interchangeable Solutions Module and UPLE program documentation (needed for it to be useful); should be available in a few days if all goes well.

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
02-20-2016, 04:42 PM (This post was last modified: 02-20-2016 04:58 PM by Jeff_Kearns.)
Post: #5
RE: Improving the solver experience on the 41C
(02-16-2016 03:41 AM)quantalume Wrote:  One of the features I like about later-model HP calculators is the use of named variables and the ability to solve for any one of the variables in an equation. The solver function available in the Advantage module is decent enough from a functional standpoint, but one generally needs to write multiple RPN functions for the same equation, depending on the variable being solved for.

Karl Schneider wrote an excellent article on how SOLVE and INTEG work in HP's RPN-based models. In it, he addresses the issue you raised about 'generally' needing to write multiple RPN functions in order to solve for different variables in a multi-variable equation by invoking a multiple-input, single-output (MISO) procedure. I have found this technique particularly useful in the HP-15C - especially for the TVM equation with 6 variables - and the use of the indirect storage register (e.g IND 00 in the HP-41C) allows for solving any variable.

While your program certainly has some interesting features in that it creates a menu of variables, why go to all the trouble to write a new solver routine rather than use the existing Advantage Pac program, if you have it already? Can the menu feature be combined with the ADV routine? I get the impression that Stefan Vorkoetter's approach was initially based on his HP-19C (29C) solver routine and was ported to the HP-41 because he didn't have an Advantage Pac. Please correct me if I am mistaken.

Jeff Kearns
Find all posts by this user
Quote this message in a reply
02-20-2016, 05:08 PM
Post: #6
RE: Improving the solver experience on the 41C
(02-20-2016 04:42 PM)Jeff_Kearns Wrote:  While your program certainly has some interesting features in that it creates a menu of variables, why go to all the trouble to write a new solver routine rather than use the existing Advantage Pac program, if you have it already? Can the menu feature be combined with the ADV routine?

That's exactly what Dave's program does - not a re-write but a "shell" around SOLVE.
My version uses a shorter shell, and is around FROOT instead (both are equivalent).

"To live or die by your own sword one must first learn to wield it aptly."
Find all posts by this user
Quote this message in a reply
02-20-2016, 05:59 PM
Post: #7
RE: Improving the solver experience on the 41C
(02-20-2016 05:08 PM)Ángel Martin Wrote:  Very nice improvement on Stefan Vorkoetter's program...

That's exactly what Dave's program does - not a re-write but a "shell" around SOLVE.
My version uses a shorter shell, and is around FROOT instead (both are equivalent).

Ángel: Thanks for the clarification. I look forward trying this out.

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




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