The Museum of HP Calculators


Euclidean Distance for the HP-41

This program is Copyright © 2007 by Jean-Marc Baillard and is used here by permission.

This program is supplied without representation or warranty of any kind. Jean-Marc Baillard and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

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
   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   •  R01 = a                  ( Registers R00 thru R02 are to be initialized before executing "PL2+" )
                                                         •  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                          Lines 12 to 15 may be replaced by   X^2   RCL 02   X^2   +   SQRT
13  R-P                                it costs 1 extra byte but it executes faster
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.
                  and ---------------------------  y = g(x)  --------------------------------------
 

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   •  R01 = a                  ( Registers R00 thru R03 are to be initialized before executing "PPL3" )
                                                         •  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 which 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') -------------------------  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') -----------  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.
                  and ---------------------------  z = g(x,y)  -----------------------------------------------------------
 

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
 

-(L) is defined by one point A( a1 , ...... , an )  and one vector  U( u1 , ...... , un )
-(L') ---------------------- 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'
-If the determinant of this system equals zero ( i-e if (L) // (L') ), "PLN" is called as a subroutine.
 

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: /
Subroutine:  "PLN" if (L) // (L') - paragraph 3-a)
                       none otherwise
 

  01  LBL "LLN"
  02  CLA
  03  RCL 00
  04  ENTER^
  05  ENTER^
  06  STO a                since synthetic register a  is used, "LLN" cannot be called as more than a first level subroutine.
  07  ST+ X
  08  STO 00              register R00 is temporarily modified, but its original content ( n ) is eventually restored
  09  +
  10  STO P               synthetic
  11  +
  12  STO Q
  13  CLST
  14  LBL 01
  15  RCL IND P
  16  RCL IND a
  17  -
  18  RCL IND Q
  19  X<>Y
  20  *
  21  ST+ Z
  22  X<> L
  23  RCL IND 00
  24  *
  25  ST+ Y
  26  X<> L
  27  ENTER^
  28  X^2
  29  ST+ O
  30  CLX
  31  RCL IND Q
  32  X^2
  33  ST+ N
  34  X<> L
  35  *
  36  ST+ M
  37  RDN
  38  DSE 00
  39  DSE P
  40  DSE Q
  41  DSE a
  42  GTO 01
  43  RCL N
  44  RCL O
  45  *
  46  RCL M
  47  X^2
  48  X#Y?                         Lines 48 to 52 may be replaced by   X=Y?   GTO "PLN"
  49  GTO 00
  50  XEQ "PLN"
  51  RTN
  52  LBL 00
  53  -
  54  X<> N
  55  RCL Y
  56  *
  57  RCL M
  58  R^
  59  ST* Y
  60  RDN
  61  -
  62  X<> M
  63  *
  64  X<>Y
  65  RCL O
  66  *
  67  -
  68  RCL N
  69  ST/ M
  70  /
  71  STO N
  72  RCL 00
  73  ENTER^
  74  ENTER^
  75  STO O
  76  ST+ X
  77  STO 00              register R00 is once again modified, but its original content ( n ) is finally restored
  78  +
  79  STO P               synthetic
  80  +
  81  STO Q
  82  CLX
  83  LBL 02
  84  RCL IND 00
  85  RCL M
  86  *
  87  RCL IND Q
  88  RCL N
  89  *
  90  -
  91  RCL IND O
  92  +
  93  RCL IND P
  94  -
  95  X^2
  96  +
  97  DSE 00
  98  DSE Q
  99  DSE P
100  DSE O
101  GTO 02
102  SQRT
103  CLA
104  END

( 166 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') -----------  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.428923171

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

Note:

-If n = 2, d is always equal to zero unless (L) and (L') are both parallel and distinct.
 

     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 12     RCL 13    RCL 11    +               +                  -                 -                 -              +
  RCL 11     RCL 15     RCL 16    X^2          RCL 13     7                  X^2           X^2            X^2          RTN
  RCL 14     -                -               RCL 12    X^2           -                  +                +                ST+ X
  -                X^2           X^2         X^2           3               RCL 14       RCL 15      RCL 16      +
  X^2           +               +              ST+ X       *               4                  5                6                X^2

-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...
 
 

Go back to the HP-41 software library
Go back to the general software library
Go back to the main exhibit hall