Post Reply 
Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
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:
\<<
   @ setup local variables
   { 20. } 0. CON                @ X(20)
   4. 1. 1E-7 50.                @ N, K, T, M
   RCLF                          @ flags
   \-> X N K T M flags

   \<<
      @ setup function F
      \<<
         @ X(1)-X(2)*(1+X(3))^X(4)
         X 1. GET
         X 2. GET
         X 3. GET 1. +
         X 4. GET ^
         * -
      \>>
      \-> F

      \<<
         @ obtain X(n) values from user
         0. FIX
         1. N FOR index
            index K SAME "Guess for " "" IFTE
            "X(" +
            index \->STR
            1. OVER SIZE OVER - SUB +
            "):" +
            "" INPUT STR\->
            'X' index ROT PUT
         NEXT

         @ repeat until either:
         @   1) current delta within specified tolerance, or
         @   2) M passes have elapsed
         DO
            F EVAL                        @ F0=F
            X K GET                       @ X0=X(K)
            DUP ABS 1. + 0.01 *           @ H=0.01*(1+ABS(X(K)))
            DUP2 + 'X' K ROT PUT          @ X(K)=X0+H
            ROT F EVAL OVER - / *         @ D=H*F0/(F-F0)
            SWAP OVER - 'X' K ROT PUT     @ X(K)=X0-D
         UNTIL
            ABS T \<=
            'M' DECR NOT
            OR
         END

         @ output X(K)
         X K GET "Root" \->TAG

         @ restore flag settings
         flags STOF
      \>>
   \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE" - DavidM - 07-11-2019 01:25 PM



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