RPN: Absolute Value Equations with the HP 11C and DM41X
Introduction: Solving |z * w + y| = x
Today’s blog focuses on solving the absolute value equation:
|z * w + y| = x
for the variable w, and the values of z, y, and x are given and are on the Classic RPN stack.
For example, in the problem |5 * w + 7| = 2, the stack would be set up as:
t: (anything, it doesn’t matter)
z: 5
y: 7
x: 2
(This is why I set the variable as w instead of the usual x.)
One approach is to use memory registers and other uses only stack operations. Today’s algorithm focuses on the latter.
Caution: In the above equation, x will always be non-negative. The equation will never be valid if x is negative.
The Algebra
Solve for w:
|z * w + y| = x
This leads us to solve the two equations:
(I)
z * w + y = -x
z * w = -x – y
w = -x/z – y/z
Let w- = -x/z – y/z = (-x/z) + (-y/z)
(II)
z * w + y = x
z * w = x – y
w = x/z - y/z
Let w+ = x/z – y/z = (x/z) + (-y/z)
Then:
w- = -x/z – y/z
w- = (-x/z) + (-y/z)
w- = (-x/z) + (-y/z) + (-x/z) + (x/z)
w- = 2 * (-x/z) + (x/z) + (-y/z)
w- = 2 * (-x/z) + w+
RPN Code: HP 11C (adoptable for other RPN calculators)
LBL A |
001 |
42, 21, 11 |
Program start |
R↓ |
002 |
33 |
|
R↓ |
003 |
33 |
|
X<>Y |
004 |
34 |
|
R↓ |
005 |
33 |
|
1/x |
006 |
15 |
|
× |
007 |
20 |
|
LST x |
008 |
43, 36 |
|
X<>Y |
009 |
34 |
|
R↓ |
010 |
33 |
|
× |
011 |
20 |
|
LST x |
012 |
43, 36 |
|
R↑ |
013 |
43, 33 |
|
X<>Y |
014 |
34 |
|
R↓ |
015 |
33 |
|
X<>Y |
016 |
34 |
|
CHS |
017 |
16 |
|
X<>Y |
018 |
34 |
|
+ |
019 |
40 |
|
ENTER |
020 |
36 |
|
ENTER |
021 |
36 |
|
LST x |
022 |
43, 36 |
|
- |
023 |
30 |
|
LST x |
024 |
43, 36 |
|
- |
025 |
30 |
|
RTN |
026 |
43, 32 |
Program end |
RPN Code: DM41X (HP 41C series, no module is required)
LBL “ASBEQ”
RDN
RDN
X<>Y
RDN
1/x
×
LAST X
X<>Y
RDN
×
LAST X
R↑
X<>Y
RDN
X<>Y
CHS
X<>Y
+
ENTER
ENTER
LAST X
-
LAST X
-
RTN
Notes:
* RDN is shown as R↓
* To enter R↑, press XEQ, ALPHA, R, SHIFT, ENTER, ALPHA
Subroutines Used
We used several techniques to manipulate the stack. They are presented below:
Rotate stack from X, Y, Z, T to Z, X, Y, T
R↓
R↓
X<>Y
R↓
Source:
Ball, John A. Algorithms for RPN Calculators John Wiley & Sons: New York. 1978. ISBN 0-471-03070-8. pg. 78
Multiply Y and Z by 1/X. Stack: X, Y, Z, T → X, Y/X, Z/X, T
1/x
×
LAST X
X<>Y
R↓
×
LAST X
R↑
X<>Y
Change X, Y to Y+ X, Y – X
+
ENTER
ENTER
LAST X
-
LAST X
-
The resulting stack is:
z
z
y + x
y – x
Doing LAST X, -, twice is effectively subtracting whatever is in L register twice.
Examples
|4 * w – 6| = 5
Stack:
z: 4
y: -6
x: 5
Results:
y: 2.75
x: 0.25
|2 * w + 5| = 3
Stack:
z: 2
y: 5
x: 3
Results:
y: -1
x: -4
Eddie
All original content copyright, © 2011-2026. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.