HP-11C/15C Code optimization
|
10-26-2024, 03:14 PM
(This post was last modified: 10-26-2024 03:22 PM by jam.)
Post: #1
|
|||
|
|||
HP-11C/15C Code optimization
Hello everyone, I appreciate anyone who takes the time to read my concerns.
I am not an expert in programming the HP11C-15C. In some forum posts, I’ve seen very clever techniques to reduce the number of steps in certain programs, and I've been amazed. I'm trying to program the solution of the Colebrook equation to calculate the friction coefficient in a pipe using Newton’s method. Initially, with the parameters `4.2E-5 ENTER 126400 GSB B`, the runtime was 17 seconds. With the program below, the runtime was reduced to 10.18 seconds (on an HP11C, a bit longer on an HP15C). Can anyone recommend any improvements? My main interest is to increase execution speed, even if that means compromising a bit on program length. However, any suggestions are welcome. The Colebrook equation: \[ x = -2 \cdot \log(a + b \cdot x) \] where \( f = \frac{1}{x^2} \). The evaluation of Newton's method gives the following approximation: \[ x_{i+1} = x - \frac{x + 2 \cdot \log(a + b \cdot x)}{1 + \frac{2b}{\ln(10)(a + b \cdot x)}} \] LBL B 2.51 / 1/X STO 3 2 X 10 LN / STO 7 X<>Y 3.7 / STO 4 EEX CHS 5 STO 5 10 STO 2 LBL 1 RCL 2 RCL 4 RCL 3 RCL 2 X + STO 6 LOG 2 X + RCL 7 RCL 6 / 1 + / - STO 2 - ABS RCL 5 X<=Y GOTO 1 RCL 2 X^2 1/X RTN |
|||
10-26-2024, 08:04 PM
Post: #2
|
|||
|
|||
RE: HP-11C/15C Code optimization
(10-26-2024 03:14 PM)jam Wrote: Can anyone recommend any improvements? The only thing that I see is replacing: Code: 047 { 30 } - Using storage arithmetics: Code: 047 { 44 30 2 } STO - 2 But I doubt that it will improve the runtime considerably. After all it takes only 3 iterations to converge. This is the listing for the HP-15C: Code: 001 { 42 21 12 } f LBL B Another approach could be to use fixed-point iteration: Code: 056 { 42 21 11 } f LBL A Example 10 GSB A 7.35586 GSB A 7.60587 GSB A 7.57890 GSB A 7.58177 GSB A 7.58147 GSB A 7.58150 We can see that it takes more iterations to converge but each iteration is faster. But I haven't compared the two methods. Usually Newton still wins. In case of the HP-15C you can use recall arithmetics which might speed up the calculation a bit. And then you could of course simply use SOLVE. |
|||
10-26-2024, 08:56 PM
Post: #3
|
|||
|
|||
RE: HP-11C/15C Code optimization
(10-26-2024 08:04 PM)Thomas Klemm Wrote:(10-26-2024 03:14 PM)jam Wrote: Can anyone recommend any improvements? Thank you very much! So, I understand that the storage arithmetic is working. Now the code takes 9.76 seconds. |
|||
10-26-2024, 09:22 PM
(This post was last modified: 10-27-2024 01:58 AM by naddy.)
Post: #4
|
|||
|
|||
RE: HP-11C/15C Code optimization
If we're talking micro-optimizations:
The best calculator is the one you actually use. |
|||
10-26-2024, 10:24 PM
(This post was last modified: 10-26-2024 10:59 PM by Albert Chan.)
Post: #5
|
|||
|
|||
RE: HP-11C/15C Code optimization
Hi, jam
It may be simpler to solve Colebrook with lambertw function. x = -2 * log10(a*x + b) // {a,b} switched to match https://en.wikipedia.org/wiki/Darcy_fric...r_formulae let y = a*x+b, k = 2a/ln(10) (y-b)/a = -k/a * ln(y) y + k*ln(y) = b --> y = k * W(z) , where z = e^(b/k)/k OP example lua> a = 2.51/126400 lua> b = 4.2e-5/3.7 lua> k = 2*a/log(10) lua> z = exp(b/k)/k lua> z -- guess for exp(W(z)) 111963.8779391653 lua> (_ + z) / (log(_) + 1) 17735.543271562103 lua> (_ + z) / (log(_) + 1) 12027.775216761022 lua> (_ + z) / (log(_) + 1) 11928.038961742051 lua> (_ + z) / (log(_) + 1) 11927.999039108403 lua> (_ + z) / (log(_) + 1) -- = exp(W(z)) 11927.99903910197 lua> z / _ -- = W(z) 9.386643775886386 lua> (k*_ - b)/a -- = x 7.5814974176865215 Or, slightly more complicated Newton setup, straight for W(z) lua> log(z) -- guess for W(z) 11.625931579856678 lua> _ - (_ + log(_/z)) / (1+1/_) 9.366995046326272 lua> _ - (_ + log(_/z)) / (1+1/_) 9.386641790802015 lua> _ - (_ + log(_/z)) / (1+1/_) 9.386643775886366 lua> _ - (_ + log(_/z)) / (1+1/_) 9.386643775886386 |
|||
10-27-2024, 12:21 AM
Post: #6
|
|||
|
|||
RE: HP-11C/15C Code optimization
(10-26-2024 09:22 PM)naddy Wrote: If we're talking micro-optimizations: Thank you. I've gained a few hundredths of a second with ENTER +. Storing the inverse in R7 would mean including divisions in the loop, so I don’t think it would work as well, or I apologize if I didn’t understand you. |
|||
10-27-2024, 12:28 AM
Post: #7
|
|||
|
|||
RE: HP-11C/15C Code optimization
(10-26-2024 10:24 PM)Albert Chan Wrote: Hi, jam Yes, it's a great idea; even the asymptotic approximations of the Lambert W function work very well. However, the purpose of this post is to learn a bit about code optimization on these beautiful calculators (HP-11C, 15C). In professional work (when I’m not using a specialized simulator), what I usually do is enable iterative calculation in Microsoft Excel and calculate friction coefficients using the fixed-point method. I’m a big fan of the HP Voyager series, and they've become a hobby. I’m not really interested in the new versions (15C LE, CE); what’s charming about the first machines is how clever the engineers were in getting the most out of them. I wish I could have lived in that era; for now, I'm content to be just an enthusiast. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)