Distances

# Euclidean Distance for the HP-41

Overview

1°) 2-Dimensional Space

a) Point-Line Distance  ( Program#1 )
b) Point-Line Distance  ( Program#2 )
c) Point-Curve Distance
d) Distance between 2 Curves

2°) 3-Dimensional Space

a) Point-Line Distance
b) Point-Plane Distance
c) Point-Surface Distance
d) Line-Line Distance
e) Distance between 2 Surfaces

3°) n-Dimensional Space

a) Point-Line Distance
b) Point-Hyperplane Distance
c) Line-Line Distance ( 2 programs )
d) Distance between 2 Hypersurfaces ( one example only )

-The Euclidean distance between 2 points M( x1 , ...... , xn ) & M'( x'1 , ...... , x'n )  is given by   d = [ (x1-x'1)2 + ...... + (xn-x'n)2 ]1/2
-The following programs compute the distance between a few sets of points.
-We assume the basis are orthonormal.

1°) 2-Dimensional Space

a) Point-Line Distance ( Program#1 )

-Here, we assume the line (L) is not parallel to the y-axis so that (L) may be defined by an equation of the form:   y = m.x + p
-The point M is determined by its coordinates  M(x,y)

Formula:    distance  d = | m.x+ p - y | / ( 1 + m2 )1/2

Data Registers: /
Flags: /
Subroutines: /

 01  LBL "PL2"  02  R^  03  ST* Y  04  X<> T  05  +  06  -  07  ABS  08  X<>Y  09  X^2  10  1  11  +  12  SQRT  13  /  14  END

( 24 bytes / SIZE 000 )

 STACK INPUTS OUTPUTS T m m Z p m Y y m X x d L / sqrt(1+m2)

Example:     (L):  y = 3.x + 4    M( 5 ; 2 )

3  ENTER^
4  ENTER^
2  ENTER^
5  XEQ "PL2"  >>>>  d =  5.375872023

b) Point-Line Distance ( Program#2 )

-In the general case, (L) has an equation:  a.x + b.y = c  and the distance d between M(x,y) and (L) is given by

d = | a.x+ b.y- c | / ( a2 + b2 )1/2

Data Registers:           •  R00 = c                       ( Registers R00 thru R02 are to be initialized before executing "PL2+" )

•  R01 = a   •  R02 = b
Flags: /
Subroutines: /

 01  LBL "PL2+"  02  RCL 01  03  *  04  X<>Y  05  RCL 02  06  *  07  +  08  RCL 00  09  -  10  ABS  11  RCL 01  12  RCL 02  13  R-P  14  X<>Y  15  RDN  16  /  17  END

( 26 bytes SIZE 003 )

 STACK INPUTS OUTPUTS Z Z / Y y Z X x d L / sqrt(a2+b2)

-Register Z is saved in Y

Example:     (L): 3.x + 7.y = 10     M( 2 ; 5 )

3  STO 01    7  STO 02    10  STO 00

5  ENTER^
2  XEQ "PL2+"  >>>>  d = 4.070499419

c) Point-Curve Distance

-This program computes the distance d between one point M(x,y) and a curve (C) defined by the equation y = f(x)
-d is the smallest value MM' where M'(x',y') is on the curve (C).

Data Registers:           •  R00 = function name                                          ( Registers R00 and R01 are to be initialized before executing "PC2" )

•  R01 = x' = an estimation of the abscissa of M'
R02 = d2        R03 = h                                             R04 thru R09: temp
Flags: /
Subroutine:   "EX"  ( cf "Extrema for the HP-41" )
and a program which computes f(x) assuming x is in X-register upon entry

 01  LBL "PC2" 02  STO 06 03  RDN 04  STO 07        05  CLX 06  RCL 00 07  STO 08 08  "T" 09  ASTO 00 10  CLX 11  RCL 01 12  XEQ "EX" 13  RCL 08         14  STO 00 15  RCL 02 16  SQRT 17  CLA 18  CLD 19  RTN 20  LBL "T" 21  STO 09 22  XEQ IND 08 23  RCL 07 24  - 25  X^2 26  RCL 06 27  RCL 09 28  - 29  X^2 30  + 31  RTN             32  END

( 50 bytes / SIZE 010 )

 STACK INPUTS OUTPUTS Z h / Y y / X x d

-where h is the stepsize used by "EX"

Example:   Evaluate the distance between M(3,0) and the curve (C) defined by  y = exp(x)

LBL "Y"
E^X
RTN

"Y"  ASTO 00
0   STO 01      if we choose 0 as an estimation of x'

-And if we choose h = 1  and  FIX 4  ( remember that the accuracy given by "EX" is determined by the display format )

1  ENTER^
0  ENTER^
3  XEQ "PC2"  >>>>  ( after displaying the successive x'-approximations )  d = 2.993448536    ( in 30 seconds )

-If you want to re-calculate d in FIX 5 , use the current h-value in register R03:

FIX 5

RCL 03
0  ENTER^
3  XEQ "PC2"  >>>>  d = 2.993448536  =  the same result!

-Though x' is correct to 4 or 5 decimals only ( R01 = 0.46510 ) ,  d is exact to 10 places!
-You can compute the exact x' and y' more directly by solving the equation d(d2)/dx = 2x - 6 + 2 e2x = 0

it yields  x' = 0.465080868  whence y' = 1.592142937  and  d = 2.993448536

d) Distance between 2 Curves

-A curve (C) is defined by  y = f(x)  and a curve (C') is defined by  y = g(x)
-To compute the distance d between the 2 curves, we seek the minimum  MM'  where M(x,y) is on (C) and M'(x',y') is on (C')

Data Registers:              R00:    temp                        ( Registers R01 - R02 - R11 - R12 are to be initialized before executing "CC2" )

•  R01 = f name
•  R02 = g name                      R08 = h  ,  R10 = d2

•  R11 = x
•  R12 = x'
Flags: /
Subroutines:

"EXN"  ( cf "Extrema for the HP-41" )
a program which computes  y = f(x)  assuming x is in X-register upon entry.
a program which computes  y = g(x) assuming x is in X-register upon entry.

 01  LBL "CC2" 02  "T" 03  ASTO 00 04  2 05  XEQ "EXN" 06  SQRT 07  CLA 08  CLD 09  RTN 10  LBL "T" 11  RCL 11 12  XEQ IND 01 13  STO 03 14  RCL 12 15  XEQ IND 02 16  RCL 03 17  - 18  X^2 19  RCL 11         20  RCL 12 21  - 22  X^2 23  + 24  RTN             25  END

( 45 bytes / SIZE 013 )

 STACK INPUTS OUTPUTS X h distance

Where h is the stepsize used by "EXN"

Example:   Evaluate the distance d between the 2 curves (C) & (C') defined by  y = exp(x)  & y = sqrt(x)

LBL "Y1"    LBL "Y2"
E^X            SQRT
RTN           RTN

"Y1"  ASTO 01
"Y2"  ASTO 02

-If our guesses are  x = 0 , x' = 1       0  STO 11   1  STO 12
and if we choose  h = 1

( FIX 4 )   1  XEQ "CC2"  >>>>  ( the successive x-approximations are displayed )   d = 0.533587580   ( in 3mn08s )

-In FIX 5 , it gives  d = 0.533587576  ( the last decimal should be a 5 )

2°) 3-Dimensional Space

a) Point-Line Distance

-M(x,y,z)
-(L) is determined by one point A(a,b,c) & one vector U(u,v,w)

Formula:   d = || UxAM || / || U ||        where  "x" is the cross-product

Data Registers:             R00: unused                                              ( Registers R01 thru R06 are to be initialized before executing "PL3" )

•  R01 = a       •  R04 = u
•  R02 = b       •  R05 = v
•  R03 = c       •  R06 = w
Flags: /
Subroutine:  "CROSS"  ( cf "Dot-Product and Cross-Product for the HP-41" )

 01  LBL "PL3" 02  RCL 03 03  ST- T 04  CLX 05  RCL 02            06  ST- Z 07  CLX 08  RCL 01 09  - 10  4 11  RDN 12  XEQ "CROSS" 13  X^2 14  X<>Y 15  X^2 16  + 17  X<>Y              18  X^2 19  + 20  RCL 04 21  X^2 22  RCL 05            23  X^2 24  RCL 06 25  X^2 26  + 27  + 28  / 29  SQRT             30  END

( 46 bytes / SIZE 007 )

 STACK INPUTS OUTPUTS Z z / Y y / X x d

Example:     (L) is defined by the point A(2,3,4) & the vector U(1,4,9)   and  M(2,5,3)

2   STO 01     1   STO 04
3   STO 02     4   STO 05
4   STO 03     9   STO 06

3   ENTER^
5   ENTER^
2   XEQ "PL3"  >>>>  d = 2.233785110

b) Point-Plane Distance

-The plane (P) has an equation:  a.x + b.y + c.z = d  and the distance between M(x,y,z) and (P) is given by

dist = | a.x+ b.y + c.z - d | / ( a2 + b2 + c2 )1/2

Data Registers:           •  R00 = d                     ( Registers R00 thru R03 are to be initialized before executing "PPL3" )

•  R01 = a
•  R02 = b
•  R03 = c
Flags: /
Subroutines: /

 01  LBL "PPL3" 02  RCL 01       03  * 04  X<>Y 05  RCL 02 06  * 07  + 08  X<>Y 09  RCL 03       10  * 11  + 12  RCL 00       13  - 14  ABS 15  RCL 01 16  X^2 17  RCL 02       18  X^2 19  RCL 03 20  X^2 21  + 22  + 23  SQRT        24  / 25  END

( 34 bytes / SIZE 004 )

 STACK INPUTS OUTPUTS Z z / Y y / X x dist

Example:    (P): 2x + 3y + 5z = 9       M(4,6,1)

9  STO 00

2  STO 01
3  STO 02
5  STO 03

1  ENTER^
6  ENTER^
4  XEQ "PPL3"  >>>>  dist = 3.568871265

c) Point-Surface Distance

-We want to evaluate the distance d between a point M(x,y) and a surface (S) defined by  z = f(x,y)
-This is the minimum  MM' where M'(x',y') is on the surface (S)

Data Registers:           •  R00 = function name                         ( Registers R00 thru R02 are to be initialized before executing "PS3" )

•  R01 = x'
•  R02 = y'    R03 = d2    R04 = h           R05 to R11: temp
Flags: /
Subroutines:

"EXY"  ( cf "Extrema for the HP-41" )
and a program that computes  z = f(x,y)  assuming x is in X-register and y is in Y-register upon entry.

 01  LBL "PS3" 02  STO 06         03  RDN 04  STO 07 05  RDN 06  STO 08 07  CLX 08  RCL 00 09  STO 09 10  "T" 11  ASTO 00 12  CLX 13  RCL 02         14  RCL 01 15  XEQ "EXY" 16  RCL 09 17  STO 00 18  RCL 03 19  SQRT 20  CLA 21  CLD 22  RTN 23  LBL "T" 24  STO 10         25  X<>Y 26  STO 11 27  X<>Y 28  XEQ IND 09 29  RCL 08 30  - 31  X^2 32  RCL 07 33  RCL 11 34  - 35  X^2 36  + 37  RCL 06 38  RCL 10         39  - 40  X^2 41  + 42  RTN 43  END

( 62 bytes / SIZE 012 )

 STACK INPUTS OUTPUTS T h / Z z / Y y / X x d

-where h is the stepsize used by "EXY"

Example:   Calculate the distance between M(7,5,0) and the elliptic paraboloid (S) defined by  z = x2 + 2y2

LBL "Z"     X^2         RTN
X^2           ST+ X
X<>Y        +

"Z"  ASTO 00
0   STO 01   STO 02    if we choose x' = y' = 0 as first guesses

-And if we choose h = 1  and  FIX 4  ( the accuracy given by "EXY" is determined by the display format )

1  ENTER^
0  ENTER^
5  ENTER^
7  XEQ "PS3"  >>>>  ( after displaying the successive x'-approximations )  d = 7.585176722    ( in 98 seconds )

-In FIX 5 , it yields  d = 7.585176721   which is correct to 10 places though  x' = R01 = 1.29613  and  y' = R02 = 0.51011  are exact to 5 decimals only.

d) Line-Line Distance

-(L) is determined by one point A(a,b,c) & one vector U(u,v,w)
-(L') is determined by one point A'(a',b',c') & one vector U'(u',v',w')

Formula:   d = | (UxU').AA' | / || UxU' ||        where  "x" is the cross-product and "." is the dot-product

Data Registers:             R00: unused                                              ( Registers R01 thru R12 are to be initialized before executing "LL3" )

•  R01 = a       •  R04 = u     •  R07 = a'       •  R10 = u'
•  R02 = b       •  R05 = v     •  R08 = b'       •  R11 = v'
•  R03 = c       •  R06 = w    •  R09 = c'        •  R12 = w'
Flags: /
Subroutines:  "CROSS"  ( cf "Dot-Product and Cross-Product for the HP-41" )  and "PL3"  ( only if (L) // (L')  )

 01  LBL "LL3" 02  4 03  RCL 12 04  RCL 11 05  RCL 10 06  XEQ "CROSS" 07  STO M 08  X^2 09  R^ 10  X^2 11  + 12  RCL Y 13  X^2 14  + 15  X=0? 16  GTO 01           17  SQRT 18  ST/ M 19  ST/ Z 20  / 21  RCL 08 22  RCL 02 23  - 24  * 25  X<>Y 26  RCL 09            27  RCL 03 28  - 29  * 30  + 31  RCL 07 32  RCL 01            33  - 34  0 35  X<> M 36  * 37  + 38  ABS 39  RTN 40  LBL 01 41  CLA 42  RCL 09 43  RCL 08            44  RCL 07 45  XEQ "PL3" 46  END

( 70 bytes / SIZE 013 )

 STACK INPUTS OUTPUTS X / distance

Example:

(L) is defined by A(2,3,4) & U(1,4,7)
(L') is defined by A'(2,1,6) & U'(2,9,5)

2  STO 01    1  STO 04    2  STO 07    2  STO 10
3  STO 02    4  STO 05    1  STO 08    9  STO 11
4  STO 03    7  STO 06    6  STO 09    5  STO 12

XEQ "LL3"  >>>>  d = 0.364106847

-If  U'(2,8,14)  (L) // (L') and it gives  d = 2.730301349

e) Distance between 2 Surfaces

-A surface (S) is defined by  z = f(x,y)  and a surface (S') is defined by  z = g(x,y)

-To compute the distance d between the 2 surfaces, we seek the minimum  MM'  where M(x,y,z) is on (S) and M'(x',y',z') is on (S')

Data Registers:              R00:    temp                      ( Registers R01 , R02 & R11 thru R14 are to be initialized before executing "SS3" )

•  R01 = f name
•  R02 = g name                      R08 = h  ,  R10 = d2

•  R11 = x    •  R13 = x'
•  R12 = y    •  R14 = y'
Flags: /
Subroutines:

"EXN"  ( cf "Extrema for the HP-41" )
a program which computes  z = f(x,y)  assuming x is in X-register and y is in Y-register upon entry.
a program which computes  z = g(x,y) assuming x is in X-register and y is in Y-register upon entry.

 01  LBL "SS3" 02  "T" 03  ASTO 00      04  4 05  XEQ "EXN" 06  SQRT 07  CLA 08  CLD 09  RTN 10  LBL "T" 11  RCL 12 12  RCL 11 13  XEQ IND 01 14  STO 03 15  RCL 14 16  RCL 13 17  XEQ IND 02 18  RCL 03 19  - 20  X^2 21  RCL 11 22  RCL 13 23  - 24  X^2 25  + 26  RCL 12         27  RCL 14 28  - 29  X^2 30  + 31  RTN             32  END

( 52 bytes / SIZE 015 )

 STACK INPUTS OUTPUTS X h distance

Where h is the stepsize used by "EXN"

Example:   Evaluate the distance d between the 2 paraboloids (S):  z = x2 + 2y2  and (S'):  z = PI - 2(x-7)2 - (y-5)2

LBL "Z1"     ST+ X          LBL "Z2"      ST+ X       X^2        -
X^2             +                  7                   X<>Y       +             RTN
X<>Y          RTN            -                    5               PI
X^2                                 X^2               -               X<>Y

"Z1"  ASTO 01
"Z2"  ASTO 02

-If our guesses are  x = y = 0 & x' = 7 , y' = 5     0  STO 11  STO 12  7  STO 13  5  STO 14
and with  h = 1

( FIX 4 )   1  XEQ "SS3"  >>>>  ( the successive x-approximations are displayed )   d = 6.146981416   ( in 7mn57s )

-In FIX 5 ,  RCL 08  XEQ "SS3" ,  it yields  d = 6.146981412  which is exact to 10 places.

3°) n-Dimensional Space

a) Point-Line Distance

-The point is determined by its coordinates:  M( x1 , ...... , xn )
-The line (L) is defined by a point  A( a1 , ...... , an )  and a vector  U( u1 , ...... , un )

Formula:           d = || AM - t.U ||     where  t = AM.U / || U ||2

Data Registers:           •  R00 = n                                                        ( these (3n+1) registers are to be initialized before executing "PLN"  )

•  R01 = a1   •  Rn+1 = u1  •  R2n+1 = x1
..............        ...............      .................

•  Rnn = an   •  R2n  = un   •  R3n  =  xn
Flags: /
Subroutines: /

 01  LBL "PLN" 02  CLA 03  RCL 00        04  ENTER^ 05  ENTER^ 06  ST+ X 07  STO M 08  + 09  STO N 10  CLX 11  LBL 01 12  RCL IND N 13  RCL IND Z 14  - 15  RCL IND M 16  ST* Y 17  X^2 18  ST+ O 19  RDN 20  + 21  DSE M 22  DSE N 23  DSE Y 24  GTO 01 25  RCL O 26  / 27  STO O       28  RCL 00        29  3 30  * 31  0 32  LBL 02 33  RCL IND N 34  RCL O 35  * 36  RCL IND M 37  + 38  RCL IND Z 39  - 40  X^2 41  + 42  DSE Y 43  DSE N 44  DSE M        45  GTO 02 46  SQRT 47  CLA 48  END

( 78 bytes / SIZE 3n+1 )

 STACK INPUTS OUTPUTS X / distance

Example:      (L) is defined by A(2,3,4,5) & U(1,4,7,9)     and     M(1,3,7,9)

n = 4  STO 00

2  STO 01     1  STO 05     1  STO 09
3  STO 02     4  STO 06     3  STO 10
4  STO 03     7  STO 07     7  STO 11
5  STO 04     9  STO 08     9  STO 12

XEQ "PLN"  >>>>  d =  2.160246900

b) Point-Hyperplane Distance

-The point is determined by its coordinates:  M( x1 , ...... , xn )
-The hyperplane (H) by one of its equations:  a1.x1 + ...... + an.xn = b

Formula:    d = | a1.x1 + ...... + an.xn - b | / ( a12 + ..... + an2 )1/2

Data Registers:           •  R00 = b                                            ( these (2n+1) registers are to be initialized before executing "PHP"  )

•  R01 = a1   •  Rn+1 = x1
..............        ...............

•  Rnn = an   •  R2n  = xn
Flags: /
Subroutines: /

 01  LBL "PHP" 02  CLA 03  STO N        04  ST+ X 05  RCL 00 06  LBL 01 07  RCL IND N 08  X^2 09  ST+ M        10  X<> L 11  RCL IND Z 12  * 13  - 14  DSE Y 15  DSE N 16  GTO 01 17  ABS 18  RCL M        19  SQRT 20  / 21  X<>Y         22  SIGN 23  RDN 24  CLA 25  END

( 43 bytes / SIZE 2n+1 )

 STACK INPUTS OUTPUTS X n distance L / n

Example:     (H):  2x + 3y + 4z + 7t = 9     M(1,3,9,6)

9  STO 00

2  STO 01     1  STO 05
3  STO 02     3  STO 06
4  STO 03     9  STO 07
7  STO 04     6  STO 08

n = 4   XEQ "PHP"  >>>>  d =  9.058216273

c) Line-Line Distance ( 2 programs )

-(L) is defined by one point A( a1 , ...... , an )  and one vector  U( u1 , ...... , un )
-(L') is defined by one point B( b1 , ...... , bn )  and one vector  V( v1 , ...... , vn )

Formula:

d = [ Sum i=1,...,n ( bi - ai - t.ui + t'.vi )2] 1/2     where    t & t'  are the solutions of the following system:

t  || U ||2  -  t'  U.V  =  AB.U              where  "." is the dot-product
t  U.V   - t'  || V ||2  =  AB.V

-This linear system is obtained after equating to zero the partial derivatives of d with respect to t and t'

Data Registers:           •  R00 = n                                                                       ( these (4n+1) registers are to be initialized before executing "LLN"  )

•  R01 = a1   •  Rn+1 = u1  •  R2n+1 = b1  •  R3n+1 = v1
..............        ...............      .................       ................

•  Rnn = an   •  R2n  = un   •  R3n  =  bn     •  R4n = vn
Flags:  /
Subroutines:  /

 01  LBL "LLN"   02  CLA   03  RCL 00   04  STO M   05  ST+ X   06  ST+ X   07  STO N   08  CLST   09  STO Q   10  LBL 01   11  RCL N   12  RCL 00   13  -   14  RDN   15  RCL IND T   16  RCL IND M   17  -   18  RCL 00   19  ST+ M   20  CLX   21  RCL IND M 22  X^2   23  ST+ T   24  X<> L   25  X<>Y   26  *   27  ST+ O   28  CLX   29  RCL IND N   30  LASTX   31  X<>Y   32  *   33  ST+ P   34  X<> L   35  X^2   36  +   37  RCL IND M   38  RCL IND N   39  *   40  ST+ Q   41  CLX   42  RCL 00 43  ST- M   44  RDN   45  DSE N   46  DSE M           47  GTO 01   48  X<>Y   49  RCL P   50  *   51  RCL O   52  SIGN   53  CLX   54  RCL Q   55  ST* L   56  X<> L   57  -   58  STO P   59  X<> T   60  RCL Y   61  *   62  RCL Q   63  X^2 64  -   65  X#0?   66  ST/ P   67  X<> Q   68  RCL P   69  *   70  RCL O   71  -   72  R^   73  /   74  STO Q   75  RCL 00   76  STO M   77  CLX   78  LBL 02   79  RCL IND M   80  RCL IND N   81  -   82  RCL N   83  RCL 00   84  + 85  RDN   86  RCL IND T   87  RCL P   88  *   89  +   90  RCL M   91  RCL 00   92  +   93  RDN   94  RCL IND T   95  RCL Q   96  *   97  -   98  X^2   99  + 100  DSE N 101  DSE M 102  GTO 02 103  SQRT 104  CLA 105  END

( 162 bytes / SIZE 4n+1 )

 STACK INPUTS OUTPUTS X / distance

Example:

(L) is defined by A(2,3,4,5) & U(1,4,7,2)
(L') is defined by B(1,2,3,7) & V(2,5,1,3)

n = 4    STO 00

2  STO 01     1  STO 05     1  STO 09     2   STO 13
3  STO 02     4  STO 06     2  STO 10     5   STO 14
4  STO 03     7  STO 07     3  STO 11     1   STO 15
5  STO 04     2  STO 08     7  STO 12     3   STO 16

XEQ "LLN"  >>>>  d =  2.428923172

-With  V(2,8,14,4)  the 2 lines are parallel and it yields:  d = 2.466924054

Notes:

-If n = 2, d is always equal to zero unless (L) and (L') are both parallel and distinct.
-Unlike the previous version, synthetic register a is unused and this new program does not use any subroutine.
-As usual, synthetic registers M N O P Q  may be replaced by any unused registers, for instance R95 R96 R97 R98 R99  ... provided  n < 24.

-We can also store the coordinates in other registers:

Data Registers:              R00 to R10: temp    R01 = bb.ee  ....  R04 = bb.ee'''                     ( the 4n  registers are to be initialized before executing "LLN"  )

•  Rbb = a1   •  Rbb' = u1   •  Rbb" = b1       •  Rbb''' = v1
..............        ...............      .................       ................

•  Ree = an   •  Ree'  = un   •  Ree"  =  bn     •  Ree''' = vn
Flags:  /
Subroutines:  /

 01 LBL "LLN"  02 STO 04  03 X<>Y  04 STO 03            05 R^  06 STO 01  07 R^  08 STO 02  09 XEQ 00  10 STO 05  11 RCL 04  12 XEQ 00  13 STO 06  14 RCL 02  15 RCL 04  16 XEQ 02  17 STO 07  18 RCL 03  19 RCL 02  20 XEQ 02  21 STO 08  22 RCL 01  23 RCL 02 24 XEQ 02  25 ST- 08  26 RCL 03  27 RCL 04            28 XEQ 02  29 STO 09  30 RCL 01  31 RCL 04  32 XEQ 02  33 ST- 09  34 RCL 09  35 RCL 07  36 /  37 STO 10  38 RCL 05  39 RCL 06  40 *  41 RCL 07  42 X^2  43 -  44 STO 00  45 X=0?  46 GTO 04 47 RCL 06  48 RCL 08  49 *  50 RCL 07  51 RCL 09            52 *  53 -  54 X<>Y  55 /  56 STO 10  57 RCL 07  58 RCL 08  59 *  60 RCL 05  61 RCL 09  62 *  63 -  64 RCL 00  65 /  66 STO 00  67 CLX  68 GTO 04  69 LBL 00 70 0  71 LBL 01  72 RCL IND Y  73 X^2  74 +  75 ISG Y  76 GTO 01  77 RTN  78 LBL 02  79 0  80 STO 00            81 RDN  82 LBL 03  83 RCL IND Y  84 RCL IND Y  85 *  86 ST+ 00  87 CLX  88 SIGN  89 +  90 ISG Y  91 GTO 03  92 RCL 00 93 RTN  94 LBL 04  95 RCL IND 04  96 RCL 00  97 *  98 RCL IND 02  99 RCL 10           100 * 101 - 102 RCL IND 03 103 + 104 RCL IND 01 105 - 106 X^2 107 + 108 SIGN 109 ST+ 01 110 ST+ 02 111 ST+ 03 112 LASTX 113 ISG 04 114 GTO 04 115 SQRT 116 END

( 159 bytes / SIZE ??? )

 STACK INPUTS OUTPUTS T bbb.eee(A) / Z bbb.eee(U) / Y bbb.eee(B) / X bbb.eee(V) distance

-With  all   bbb > 010

Example:

(L) is defined by A(2,3,4,5) & U(1,4,7,2)
(L') is defined by B(1,2,3,7) & V(2,5,1,3)

-If we store the coordinates in R11 ....  R26

2  STO 11     1  STO 15     1  STO 19     2   STO 23
3  STO 12     4  STO 16     2  STO 20     5   STO 24
4  STO 13     7  STO 17     3  STO 21     1   STO 25
5  STO 14     2  STO 18     7  STO 22     3   STO 26

11.014  ENTER^
15.018  ENTER^
19.022  ENTER^
23.026  XEQ "LLN"  >>>>  d =  2.428923172                                                           ---Execution time = 13s---

-With  V(2,8,14,4)  it yields:  d = 2.466924054

Notes:

-When the program stops, R01 thru R04 are replaced by   n+bb.ee  ,  ......  ,  n+bb.ee'''
-The coordinates are unchanged.

d) Distance between 2 Hypersurfaces  ( one example only )

-Let 2 hypersurfaces (H) & (H')  defined by  t = x2 + 2y2 + 3z2  &  t = 7 - (x-4)2 - (y-5)2 - 2(z-6)2
-To compute the distance d between (H) & (H'), we try to minimize MM' ( or MM'2 )  where M(x,y,z,t) is on (H) and M'(x',y',z',t') is on (H')

-We have

MM'2 = (x-x')2 + (y-y')2 + (z-z')2 + (t-t')2
= (x-x')2 + (y-y')2 + (z-z')2 + [ x2 + 2y2 + 3z2 - 7 + (x'-4)2 + (y'-5)2 + 2(z'-6)2 ]2

-We can use "EXN" to minimize this function of 6 variables.
- x , y , z , x' , y' , z'  will be stored in registers R11 thru R16 ( cf the instructions for use in "Extrema for the HP-41" )

-So we load the following routine:

 LBL "T"  RCL 11  RCL 14  -  X^2  RCL 12  RCL 15  -  X^2  + RCL 13  RCL 16  -  X^2  +  RCL 11  X^2  RCL 12  X^2  ST+ X +  RCL 13  X^2  3  *  +  7  -  RCL 14  4 -  X^2  +  RCL 15  5  -  X^2  +  RCL 16  6 -  X^2  ST+ X  +  X^2  +  RTN

-Then  "T"  ASTO 00

-If we choose  x = y = z = 0  and  x' = 4 , y' = 5 , z' = 6 as initial guesses

0  STO 11    4  STO 14
0  STO 12    5  STO 15
0  STO 13    6  STO 16

-And with h = 1 and  n = 6  variables

FIX 4
1  ENTER^
6  XEQ "EXN"  >>>>  d2 = 32.75378845   whence   d = 5.723092560   ( in 18mn17s )

-In   FIX 5
RCL 08   ( the last h-value)
6  R/S   >>>>  d2 = 32.75378841   whence   d = 5.723092556    ( in fact, the same result is also obtained in FIX 9 )

Important Note:

-Like all the programs that call  "EX" , "EXY" or "EXN" , this method can lead to a local minimum
which is different from "the" minimum = distance d
if the guesses are too bad...