hp41programs

CDGL

Curl, Divergence, Gradient & Laplacian for the HP-41


Overview
 

-A similar program is listed in "Differential Geometry for the HP-41"
-Here, the 1st & 2nd partial derivatives are calculated more quickly,
  but with the same formulas of order 10.

-Thus, "CDGL" is faster and uses less data registers
-On the other hand, the program is longer.

-For complex functions & vector Laplacian, cf "Complex Derivatives and Riemannian Manifolds for the HP-41"
 

Program Listing
 

 "CDGL"  computes the Curl , divergence, gradient and Laplacian of a 3D-Vector Field   E= ( f , g , h ) = ( X , Y , Z )

      • If the coordinates are rectangular, clear flags F01 & F02
      • If the coordinates are cylindrical, set F01 and clear F02
      • If the coordinates are spherical, set F02                                            ( if F02 & F01 are set, F01 is automatically cleared )

-In the last 2 cases, the HP41 sets the RAD mode automatically.
-Flag F01 is cleared if flag F02 is set ( lines 02-03 ).
 

Formulae:

    •  Rectangular Coordinates x , y , z

  Curl E = ( h/y - g/z , f/z - h/x , g/x - f/y )

  Div E  =  f/x + g/y + h/z

  Grad f = ( f/x , f/y , f/z )       and similar formulae for g & h

  Lapl f  = 2f / x2 + 2f / y2 + 2f / z2      and similar formulae for g & h
 

    •  Cylindrical Coordinates r , f , z

  Curl E = [ (1/r) h/¶f - g/z , f/z - h/r , (1/r) ( g + r g/r - f/¶f ) ]

  Div E  =  f/r + (1/r) f + (1/r) g/¶f + h/z

 Grad f = ( f/r , (1/r) f/¶f , f/z )       and similar formulae for g & h

  Lapl f  = 2f / r2 + (1/r) f/r + (1/r2) 2f / ¶f2 + 2f / z2      and similar formulae for g & h
 

    •  Spherical Coordinates r , q , f

  Curl E = [ (1/(r.sin q) ) ( h cos q + sin q h/¶q - g/¶f ) , (1/(r.sin q) ) f/¶f - h/r - h / r , (1/r) ( g + r g/r - f/¶q ) ]

  Div E  = f/r + (2/r) f + g (cos q)/(r sin q)  + (1/r) g/¶q + (1/(r sin q)) h/¶f

 Grad f = ( f/r , (1/r) f/¶q , (1/(r sin q))  f/¶f )       and similar formulae for g & h

  Lapl f  = 2f / r2 + (2/r) f/r + (1/r2) 2f / ¶q2 + (1/(r2tan q)f / ¶q + (1/(r2sin2q)) 2f / ¶f2      and similar formulae for g & h
 
 

Data Registers:             R00 = h

                                        R01 = x         R04-R05-R06 = Curl E        R08-R09-R10 = Grad (f)    R17 = Lap (f)
                                        R02 = y         R07 = Div E                         R11-R12-R13 = Grad (g)   R18 = Lap (g)
                                        R03 = z                                                     R14-R15-R16 = Grad (h)    R19 = Lap (h)

                                        R20 to R34 contain the same results as R05 to R19

Flags: /
Subroutines:     3 programs named "X"  "Y"  "Z"  which compute X(x,y,z) , Y(x,y,z) and Z(x,y,z)
                            assuming x is in X-register, y is in Y-register and z is in Z-register upon entry.

-Lines 148-165-198 are three-byte GTO 00
 
 

 01 LBL "CDGL" 
 02 FS? 02
 03 CF 01
 04 FC? 01
 05 FS? 02
 06 RAD
 07 "X"
 08 ASTO 00
 09 XEQ 10
 10 STO 22
 11 STO 23
 12 RDN
 13 STO 24 
 14 CHS
 15 STO 21
 16 RDN
 17 STO 25
 18 STO 20
 19 X<>Y
 20 STO 32 
 21 FS? 02
 22 GTO 02
 23 FC? 01
 24 GTO 00
 25 XEQ 04
 26 +
 27 STO 32
 28 RCL 05
 29 RCL 01
 30 ST/ 21
 31 ST/ 24
 32 /
 33 ST+ 22
 34 GTO 00
 35 LBL 02
 36 XEQ 03
 37 STO 32
 38 RCL 01
 39 ST/ 21
 40 ST/ 24
 41 RCL 02
 42 SIN
 43 *
 44 ST/ 20
 45 ST/ 25
 46 RCL 05
 47 ST+ X
 48 RCL 01
 49 /
 50 ST+ 22
 51 LBL 00
 52 "Y"
 53 XEQ 09
 54 FS? 01
 55 GTO 01
 56 FS? 02
 57 GTO 02
 58 ST+ 21
 59 STO 26
 60 RDN
 61 STO 27
 62 ST+ 22
 63 RDN
 64 STO 28          
 65 CHS
 66 STO 19
 67 X<>Y
 68 STO 33
 69 GTO 00
 70 LBL 01
 71 XEQ 04
 72 +
 73 STO 33
 74 RCL 06
 75 STO 26
 76 RCL 07 
 77 RCL 01
 78 /
 79 ST+ 22
 80 STO 27
 81 RCL 08
 82 STO 28
 83 CHS
 84 STO 19
 85 RCL 06
 86 RCL 05
 87 RCL 01
 88 /
 89 +
 90 ST+ 21
 91 GTO 00
 92 LBL 02
 93 XEQ 03
 94 STO 33
 95 RCL 06
 96 STO 26
 97 RCL 07
 98 RCL 01
 99 /
100 STO 27 
101 RCL 08
102 LASTX
103 RCL 02
104 SIN
105 *
106 /
107 STO 28
108 RCL 05
109 RCL 02
110 TAN
111 /
112 RCL 07
113 +
114 RCL 01
115 /
116 ST+ 22
117 RCL 08
118 CHS
119 RCL 01
120 RCL 02
121 SIN
122 *
123 /
124 STO 19
125 RCL 05
126 RCL 01         
127 /
128 RCL 06
129 +
130 ST+ 21
131 LBL 00
132 "Z"
133 XEQ 09
134 FS? 01
135 GTO 01
136 FS? 02
137 GTO 02
138 STO 29 
139 ST- 20
140 RDN
141 STO 30 
142 ST+ 19
143 RDN
144 STO 31
145 ST+ 22
146 X<>Y
147 STO 34
148 GTO 00
149 LBL 01
150 XEQ 04
151 +
152 STO 34
153 RCL 06
154 STO 29
155 RCL 07
156 RCL 01
157 /
158 ST+ 19
159 STO 30
160 RCL 08
161 STO 31
162 ST+ 22
163 RCL 06
164 ST- 20
165 GTO 00
166 LBL 02
167 XEQ 03
168 STO 34
169 RCL 06
170 STO 29
171 RCL 07
172 RCL 01
173 /
174 STO 30
175 RCL 08
176 LASTX
177 RCL 02
178 SIN
179 *
180 /
181 STO 31
182 ST+ 22
183 RCL 07
184 RCL 05
185 RCL 02
186 TAN
187 /
188 +
189 RCL 01         
190 /
191 ST+ 19
192 RCL 06
193 RCL 05
194 RCL 01 
195 /
196 +
197 ST- 20
198 GTO 00
199 LBL 03
200 XEQ 04
201 RCL 02 
202 SIN
203 X^2
204 /
205 RCL 07
206 RCL 02
207 TAN
208 /
209 +
210 RCL 01
211 X^2
212 /
213 +
214 RTN
215 LBL 04
216 RCL 10
217 RCL 01
218 /
219 RCL 06
220 FS? 02
221 ST+ X
222 +
223 RCL 01
224 /
225 RCL 09
226 +
227 RCL 11
228 RTN
229 LBL 09
230 ASTO 00
231 RCL 04
232 RCL 03
233 RCL 02
234 RCL 01
235 LBL 10
236 STO 01
237 RDN
238 STO 02
239 RDN
240 STO 03
241 X<>Y
242 STO 04
243 STO 12
244 X<>Y
245 R^
246 R^
247 XEQ IND 00
248 STO 05
249 6
250 ST* 12
251 XEQ 01
252 125
253 ST/ 15
254 ST/ 13
255 /
256 STO 11         
257 RCL 15
258 STO 10
259 RCL 13
260 STO 09
261 RCL 14 
262 50
263 ST/ 16
264 ST/ 18
265 /
266 STO 06
267 RCL 16
268 STO 07
269 RCL 18
270 STO 08
271 XEQ 01
272 8
273 ST/ 15
274 ST/ 13
275 /
276 ST- 11
277 RCL 15
278 ST- 10
279 RCL 13
280 ST- 09
281 RCL 14
282 4
283 ST/ 16
284 ST/ 18
285 /
286 ST- 06
287 RCL 16
288 ST- 07
289 RCL 18
290 ST- 08
291 XEQ 01
292 ST+ 11
293 RCL 15
294 ST+ 10
295 RCL 13
296 ST+ 09
297 RCL 14
298 1.5
299 ST* 16
300 ST* 18
301 *
302 ST+ 06
303 RCL 16
304 ST+ 07
305 RCL 18
306 ST+ 08
307 XEQ 01
308 6
309 ST* 15
310 ST* 13
311 ST* 14
312 ST* 16
313 ST* 18
314 *
315 ST- 11
316 RCL 15 
317 ST- 10
318 RCL 13         
319 ST- 09
320 RCL 14
321 ST- 06
322 RCL 16
323 ST- 07
324 RCL 18 
325 ST- 08
326 XEQ 01
327 42
328 ST* 15
329 ST* 13
330 *
331 ST+ 11
332 RCL 15
333 ST+ 10
334 RCL 13
335 ST+ 09
336 RCL 14
337 21
338 ST* 16
339 ST* 18
340 *
341 ST+ 06
342 RCL 16
343 ST+ 07
344 RCL 18
345 ST+ 08
346 25.2
347 RCL 04
348 *
349 ST/ 06
350 ST/ 07
351 ST/ 08
352 LASTX
353 *
354 ST/ 09
355 ST/ 10
356 ST/ 11
357 RCL 09
358 RCL 10
359 RCL 11
360 +
361 +
362 RCL 08
363 RCL 07
364 RCL 06
365 RTN
366 LBL 01
367 RCL 04
368 ST- 12
369 RCL 03
370 RCL 02
371 RCL 01
372 RCL 12
373 +
374 XEQ IND 00
375 STO 13 
376 STO 14
377 RCL 03
378 RCL 02
379 RCL 01
380 RCL 12
381 -
382 XEQ IND 00
383 ST+ 13
384 ST- 14
385 RCL 03 
386 RCL 02
387 RCL 12
388 +
389 RCL 01
390 XEQ IND 00
391 STO 15
392 STO 16
393 RCL 03
394 RCL 02
395 RCL 12
396 -
397 RCL 01
398 XEQ IND 00
399 ST+ 15
400 ST- 16
401 RCL 03
402 RCL 12
403 +
404 RCL 02
405 RCL 01
406 XEQ IND 00
407 STO 17
408 STO 18
409 RCL 03
410 RCL 12
411 -
412 RCL 02
413 RCL 01
414 XEQ IND 00
415 ST- 18
416 RCL 17
417 +
418 RCL 05
419 ST+ X
420 ST- 13
421 ST- 15
422 -
423 RTN
424 LBL 00
425 RCL 04
426 STO 00
427 19.004016
428 REGMOVE
429 4.019
430 SIGN
431 RCL 07
432 RCL 06
433 RCL 05
434 RCL 04
435 END

 
     ( 669 bytes / SIZE 035 )
 
 

      STACK       INPUTS      OUTPUTS
           T            h         Div E
           Z            x3        Curl3 E
           Y            x2        Curl2 E
           X            x1        Curl1 E
           L             /         4.019

    4.019 = control number of all the results.

Example1 - Rectangular Coordinates:   CF 01  CF 02

   E = ( f , g , h )  = [ exp(-x2) Ln(y2+z) , x2y2z2 , exp(x) y2z ]     With  x = 1 , y = 2 , z = 3

-Load the short routines:
 
 

 01  LBL "X"
 02  X^2
 03  CHS
 04  E^X
 05  RDN
 06  X^2
 07  +
 08  LN
 09  R^
 10  *
 11  RTN 
 12  LBL "Y"
 13  *
 14  *
 15  X^2
 16  RTN
 17  LBL "Z"
 18  E^X
 19  RDN
 20  X^2
 21  *
 22  R^
 23  *
 24  RTN

 
      CF 01   CF 02

-If you choose  h = 0.1

  0.1  ENTER^
   3    ENTER^
   2    ENTER^
   1    XEQ "CDGL"  >>>>     8.619381890  = R04                                                                    ---Execution time = 90s---
                                 RDN    -32.56682780  = R05
                                 RDN     71.78978318  = R06
                                 RDN     45.44140669  = R07

-So,  Curl E = ( 8.619381890 ,  -32.56682780 ,  71.78978318 )

 and    Div E = 45.44140669

-You also find in registers R08 to R19:

    Grad f = ( -1.431720683 , 0.210216822 , 0.052554204 )
    Grad g = ( 72 , 36 , 24 )
    Grad h = ( 32.61938200 , 32.61938189 , 10.87312737 )

    Lapl f = 1.409197689
    Lapl g = 98
    Lapl h = 48.92907013

-We have saved  37 seconds ( execution time = 90 seconds instead of 127 seconds )
 

Example2 - Cylindrical Coordinates:   SF 01  CF 02

   E = ( f , g , h )  = (  r z2 sin2f , r2 z , r3 z cos f )                   r = 2 , f = PI/5 , z = 1
 
 
 

 01  LBL "X"
 02  RDN
 03  SIN
 04  *
 05  X^2
 06  R^
 07  *
 08  RTN
 09  LBL "Y"
 10  X^2
 11  RCL Z
 12  *
 13  RTN
 14  LBL "Z"
 15  X^2
 16  LASTX
 17  *
 18  X<>Y
 19  COS
 20  *
 21  *
 22  RTN

 
    SF 01   CF 02

-If you choose  h = 0.1

     0.1   ENTER^
       1
   PI  5  /
      2    R/S   >>>>    -6.351141010  = R04                                                                     ---Execution time = 95s---
                     RDN    -8.326237923  = R05
                     RDN     5.048943482  = R06
                     RDN     7.163118954  = R07

-So,  Curl E = ( -6.351141010 ,  -8.326237923 ,  5.048943482 )

 and    Div E = 7.163118954

-You also find in registers R08 to R19:

    Grad f = ( 0.345491503 , 0.951056518 , 1.381966010 )
    Grad g = ( 4 , 0 , 4 )
    Grad h = ( 9.708203933 , -2.351141010 , 6.472135948 )

    Lapl f = 1.863728736
    Lapl g = 4
    Lapl h = 12.94427209

-Here, we have saved  40 seconds ( execution time = 95 seconds instead of 135 seconds )
 

Example2 - Spherical Coordinates:    CF 01  SF 02
 

   E = ( f , g , h )  = (  r sin2 q cos2 f , r2sin f  , r3 cos q cos2 f )                   r = 2 , q = PI/3 , f = PI/5
 
 
 

 01  LBL "X"
 02  RDN
 03  SIN
 04  X<>Y
 05  COS
 06  *
 07  X^2
 08  R^
 09  *
 10  RTN
 11  LBL "Y"
 12  X^2
 13  RCL Z
 14  SIN
 15  *
 16  RTN
 17  LBL "Z"
 18  X^2
 19  LASTX
 20  *
 21  X<>Y
 22  COS
 23  *
 24  X<>Y
 25  COS
 26  X^2
 27  *
 28  RTN

 
   CF 01    SF 02

-If you choose  h = 0.1

     0.1
   PI  5  /
   PI  3  /
      2    R/S   >>>>   -3.379867348  = R04                                                                     ---Execution time = 145s---
                     RDN    -6.059707088  = R05
                     RDN     2.959890530  = R06
                     RDN    -0.045010876  = R08

-So,  Curl E = ( -3.379867348 ,  -6.059707088 ,  2.959890530 )

 and    Div E = -0.045010876

-You also find in registers R08 to R19:

    Grad f = ( 0.490881375 , 0.566820985 , -0.823639103 )
    Grad g = ( 2.351141010 , 0 , 1.868344718 )
    Grad h = ( 3.927050990 , -2.267283945 , -2.196370945 )

    Lapl f = 0.018237234
    Lapl g = 2.742997604
    Lapl h = 5.721039300

Notes:

-In this last example, we save 42 seconds, compared to the other version of "CDGL" listed in "Differential Geometry"
 ( Execution time = 145 seconds instead of 187 seconds )

-LBL 10 may also be called for itself to compute the 1st & 2nd partial derivatives of a function of 3 variables:
-Add  LBL "dF3"  after line 234 and - perhaps - a three-byte GTO 10  after line 423.
 

>>> The specifications for this LBL 10 -> LBL "dF3" are:
 

Data Registers:          •  R00 = Function name                           ( Register R00 is to be initialized before executing "dF3" )

                                         R01 = x      R04 = h              R06 = f 'x = f / x      R09 = f "xx = 2f / x2      R12 to R18: temp
                                         R02 = y      R05 = f(x,y,z)     R07 =  f 'y = f / y     R10 = f "yy = 2f / y2
                                         R03 = z                                 R08 = f 'z = f / z       R11 = f "zz = 2f / z2

Flags: /
Subroutine:   A program which computes f(x,y,z) assuming x is in X-register, y is in Y-register and z is in Z-register upon entry.
 
 
 

      STACK        INPUTS      OUTPUTS
           T             h            Df
           Z             z    f 'z = f / z
           Y             y    f 'y = f / y
           X             x    f 'x = f / x

   Df = Laplacian ( f ) = 2f / x2 + 2f / y2 + 2f / z2

Example:     f(x) = exp(-x2).Ln(y2+z)    x = 1 , y = 2 , z = 3
 
 

 01  LBL "T"
 02  X^2
 03  CHS
 04  E^X
 05  RDN
 06  X^2
 07  +
 08  LN
 09  R^
 10  *
 11  RTN 

 
      T  ASTO 00

-If we choose h = 0.1

   0.1   ENTER^
    3     ENTER^
    2     ENTER^
    1     XEQ "dF3-"  >>>>    f 'x  = f / x  =   -1.413720683              R04 = f "xx = 2f / x2 =  1.413720682             ---Execution time = 52s---
                                 RDN   f 'y  = f / y  =    0.210216822              R07 = f "yy = 2f / y2 = -0.015015424
                                 RDN   f 'z  = f / z   =    0.052554204              R10 = f "zz2f / z2 = -0.007507569
      RDN    2f / x2 + 2f / y2 + 2f / z2 =     1.409197689