Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
|
07-07-2019, 08:45 PM
(This post was last modified: 07-07-2019 08:46 PM by Namir.)
Post: #1
|
|||
|
|||
Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
Hi All,
I have a presentation for HHC2019 that Richard Nelson asked me to do. It is a multi-variable solver. Meaning that given an equation with N variables, you can solve for any variable given fixed values for the remaining N-1 variables. I would like an RPL version (see teh HP-71B and HP-4C versions that solve for any variable in simple financial equation FV=PV*(1+i)^n). I will give credit to the author of the author of the version I will use in my presentation. I will also mention that author in my presentation that will appear on YouTube. Here is the HP-71B Listing Code: 10 REM SOLVER FOR ANY VARIABLE For an HP-41C Code, here is the memory map: Code: R00 = I And here is the HP-41C Listing: Code: LBL "MVSOLV" |
|||
07-08-2019, 01:16 PM
Post: #2
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
(07-07-2019 08:45 PM)Namir Wrote: Hi All, The "white menu" solver in the 48/50 series does exactly what you are looking for, and with the best (in my opinion) open UI you can get for a solver: just assign values to some variables and solve for others, clean and simple, and no RPL code needed. The other numerical solver in the 50g can also do that with a different UI (more visual, in my opinion less practical to assign values to variables quickly), also no RPL code needed at all. There are some programmatic ways of using the same built-in solvers, but honestly with two different user interfaces, it's hard to justify writing a program to do it. You also have available the multiple equation solver, for multiple variables in multiple equations so there's even less need for RPL code. If you still want RPL code, it only needs to store the equation and call the proper menu number to activate the "white menu" solver: Code:
By the way, if it's only the financial equation you are interested in solving, the 50g has the full TVM solver, just setting the payment amounts to 0 you end up with the equation above. That also has a nice UI built-in. |
|||
07-08-2019, 06:26 PM
Post: #3
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
Namir, if you're simply wanting an RPL version of the same program logic that you're using in the other supplied programs, then the following may provide a starting point.
This is meant to mimic your 71B structure/flow as closely as possible, and uses the same variable names that you specified in that version. As Claudio has mentioned, the built-in solver would be better to use, but I'm guessing you simply want to have an RPL equivalent to the other programs. That's what I've attempted to do here. Code: \<< No attribution needed, you (and everyone else) should feel free to make improvements or modify however you like. My goal in this case was to replicate the same logic flow as the other programs as opposed to focusing on code size, speed, or special RPL features. I used an algebraic for F in this case to make the coding a closer match to the 71B version. If logic flow parity wasn't your intended target for this, please ignore and move on. Hope this helps! - David |
|||
07-08-2019, 09:25 PM
(This post was last modified: 07-08-2019 09:26 PM by Namir.)
Post: #4
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
Thank you David. Your ode will beautifully do the job!!!
Richard Nelson wanted a geneal RPL code that would also work with the HP-48GX. Namir |
|||
07-08-2019, 09:45 PM
Post: #5
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
(07-08-2019 09:25 PM)Namir Wrote: ...that would also work with the HP-48GX. Oops! I wrote that code with a 50g target, and I know of at least one command it uses (R→I) that isn't applicable to the 48 series. I'm also not sure if array indexing with subscripts works the same way on a 48, so I may need to adjust that as well. I'll redo this and post an update after I've had a moment to go through it with the 48GX as a target (hopefully later this evening). |
|||
07-08-2019, 11:52 PM
Post: #6
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
So there were actually 3 commands not supported on a 48GX, and I was right about the array subscripts (which are not supported on the 48 series). Also, one additional layer of locals is now needed since the function F references locals from the array that have to already have been declared and in-scope -- being able to define it as an algebraic in the previous example worked around that issue.
I also rearranged the input loop a bit to make it easier to read (IMHO). It's always surprising to me how often I use commands and features that didn't come into play until the 49-50 series. It doesn't "feel" like I do it that often when I'm coding, but on those rare occasions when I write 48-series code I see the evidence firsthand. Code: \<< Note: I'm just transcoding here, so I'm not asserting that this program is fully debugged and operational. I haven't done much testing -- just a couple of basic scenarios which seem to work. Question: Have you insured in the algorithm that the indefinite loop exit condition is guaranteed to occur? In other words, is it possible that some inputs might cause the "delta" to always be greater than the tolerance value? |
|||
07-09-2019, 01:06 AM
(This post was last modified: 07-09-2019 07:47 AM by Namir.)
Post: #7
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
Quote:Question: Have you insured in the algorithm that the indefinite loop exit condition is guaranteed to occur? In other words, is it possible that some inputs might cause the "delta" to always be greater than the tolerance value? Good point. Here is the HP-71B BASIC listing using a new variable M to control the maximum number of iterations: Code:
Here is the update memory map for the HP-41C to include R28 to store the maximum number of iterations. Code: R00 = I And here is the HP-41C listing that uses register R28 to control the maximum number of iterations: Code: LBL "MVSOLV" Can you plese update the HP48GX (and also the HP-50G) listing to use the maximum number of iterations of 55????? with many thanks! Namir |
|||
07-09-2019, 01:37 PM
Post: #8
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
(07-09-2019 01:06 AM)Namir Wrote: Can you plese update the HP48GX (and also the HP-50G) listing to use the maximum number of iterations of 55????? Your question above indicated 55 for M, but the 71B code used 50. Since my focus is matching the 71B code as much as possible, I used 50. Rather than maintaining two separate versions, I've made one version which is compatible with the 48GX..50g. That required changing the input routine so that the prompt formatting would be consistent for each of those platforms. I also included radix marks on constants so that approximate (real) numbers would be forced on 49-or-later platforms (again for consistency). Finally, I changed the ordering of the statements that use PUT in the indefinite loop so that they now follow the same pattern as the others (Line 1: operation, Line 2: STO/PUT). That required an additional command to be added for the PUTs (ROT), but I believe the consistency aids readability more than the cost of a couple extra commands. Here's what I've got so far: Code: %%HP: T(3)A(R)F(.); |
|||
07-10-2019, 05:02 AM
Post: #9
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
Thanks David!!!!
Namir |
|||
07-11-2019, 01:25 PM
(This post was last modified: 07-12-2019 01:38 PM by DavidM.)
Post: #10
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
One more RPL version...
This one is a bit more of a traditional RPL approach. Instead of replicating the exact flow of the 71B steps, it uses the stack for holding the temporary variables F0, X0, H, and D while seeking the root. This makes better use of the calculator's resources (saves 116.5 bytes and executes faster), but also makes the source much harder to read and maintain (IMHO). In many ways, this follows the usual pattern for RPL (and RPN) code: the more refined and efficient it is, the more fragile it becomes. Whereas in the previous version you could actually see the temporary variable modifications, holding the variables on the stack during execution makes it much harder to follow simply by looking at the code. If the comments are removed, it would be an arduous task to go back and make changes. Code: \<< |
|||
07-12-2019, 03:55 PM
Post: #11
|
|||
|
|||
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
David,
Thanks for your good work. This one looks better. Namir |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)