[comp.sys.handhelds] Integration program for HP-48SX

hammes@senf.informatik.uni-kl.de (Stefan Hammes) (05/27/91)

-------------------------------------------------------------------------------
|                                                                             |
| This is posted for (and written by) a friend of mine, who cannot send mail. |
|                                                                             |
-------------------------------------------------------------------------------

Hi HP-48SX users,                                               [ Release 1.0 ]

I wrote a program that helps you to integrate an expression like the following:



           (      2
           |     x - 5x + 8
(1)        |  --------------- dx = ? 
           |   4    2
           )  x - 6x + 8x - 3



To integrate this expression it is necessary to integrate parts of fractions.


           (             (             (             (
           |   A         |   B         |   C         |   D
(2)        |--------dx + |--------dx + |--------dx + |--------dx  = ?
           | Root 1      | Root 2      | Root 3      | Root 4
           )             )             )             )



The result of expression (2) is equivalent to the result of expression (1). We
only have to find  the constants (A, B, C, D) and all roots of the denominator
polynomial.

Finding the roots shouldn't be a problem for HP48 users.

We will find four roots, x1 =  1

                         x2 =  1
                                           3
                         x3 =  1   => (x-1)     (multiple root)
                                  
                         x4 = -3   => (x+3)     (single   root)


and get expression (3)


           (             (             (             (
           |   A         |   B         |   C         |   D
(3)        |--------dx + |--------dx + |--------dx + |--------dx  = ?
           |      3      |      2      |             |
           ) (x-1)       ) (x-1)       ) (x-1)       ) (x+3)



To compute the constants, the stack should be configured as follows:



     2
3: 'x - 5*x + 8'     @ Nominator-polynomial

         
2: '(x-1)^3'         @ Multiple root as a linear factor


1: 'x+3'             @ Single   root as a linear factor


Now start the program [PBZ]. After a few seconds (41, wait for the next
version) the stack should look like this:


1: [ 1 -1 .5 -.5 ]


This means: A = 1, B = -1, C = .5, D = -.5


Now let's integrate ...


(             (               (            (
|   1         |   1           |  1         |  1
|--------dx - |--------dx + .5|-----dx - .5|-----dx = 
|      3      |      2        |            |
) (x-1)       ) (x-1)         ) x-1        ) x+3


... and get this as result:


     1        1
= ------- - ----- + .5 * ln|x-1| - .5 * ln|x+3|            @ Final result.
        2
  2(x-1)     x-1

 ==============================================



Notes: If a root results as 0, input 'x+0' as linear factor otherwise it
       (SPLT) will cause an error.

       Notation of a complex linear factor: 'x-(1,2)'    (i.e.)


       Only important for HP-28 users: Use this version of 'SPLT' !

       'SPLT'
       << DUP
          IF DUP 5 EXGET TYPE 9 ==
           THEN DROP
           ELSE DUP 5 EXGET 1 - 5 SWAP EXSUB EVAL SPLT
          END       
       >>

1565 Bytes Checksum: # 43BAh

------------Cut here------------Cut here-------------Cut here-----------------

%%HP: T(3)A(D)F(.);
DIR
  PBZ
    \<< DEPTH 1 - \->LIST { } \-> za ne pn
      \<< 1 ne SIZE
          FOR j
              ne j GET SPLT
          NEXT
          DEPTH \->LIST 'pn' STO
          za P\->L 'za' STO 1 ne SIZE
          FOR j
              ne j GET P\->L 'ne' j ROT PUT
          NEXT
          ne OBJ\-> 1 - 1 SWAP
          START
                PMUL
          NEXT
          'ne' STO 1 pn SIZE 
          FOR j
              'pn' j pn j GET P\->L PUT
          NEXT
          1 pn SIZE
          FOR j
              za .125 j * PVAL
          NEXT
          DEPTH
          \->ARRY 1 pn SIZE
          FOR j
              'pn' j ne pn j GET PDIV DROP PUT 
          NEXT
          1 pn SIZE
          FOR j
              1 pn SIZE
              FOR k
                  'pn' k GET .125 j * PVAL
              NEXT
          NEXT
          pn SIZE DUP 2 \->LIST \->ARRY
          \-> B A
          \<<
              B A / B A 3 PICK RSD A / +
          \>>
      \>>
    \>>
@
@ Convert polynomial into list
@
  P\->L
    \<< 0 DEPTH 2 - \-> p j d
      \<<
        DO
          p 0 'X' STO \->NUM
          j FACT / 'X' PURGE
          p 'X' \.d 'p' STO j 1 + 'j' STO
        UNTIL p 0 SAME END
        DEPTH d - 2
        FOR j
            j ROLLD
        -1 STEP
        DEPTH d - \->LIST
      \>>
    \>>
@
@ Compute value of polynomial
@
  PVAL
    \<< OVER SIZE \-> l x ll
      \<< 0 1 ll
          FOR j
              l j GET x ll j - ^ * +
          NEXT
      \>>
    \>>
@
@ Add two polynomials
@
  PADD
    \<< DUP2 SIZE SWAP SIZE
      IF > THEN SWAP END
      OBJ\-> DUP 2 + ROLL OBJ\-> DUP 2 + ROLL \-> g s
      \<< 1 g
        FOR n
            n s
            IF \<=
              THEN g 1 + ROLL + COLCT
              END
            g ROLLD
        NEXT
        g \->LIST
      \>>
    \>>
@
@ Multiply two polynomials
@
  PMUL
    \<< DUP SIZE ROT DUP SIZE DUP 4 PICK
      IF > THEN 4 ROLL 4 ROLL END
      \-> g gs sh s
      \<< { } 1 s
        FOR ss 
            g OBJ\-> 1 SWAP
            START
                 sh ss GET * gs ROLL
            NEXT
            1 s ss - DUP2
            IF \<=   THEN  START 0 NEXT
                     ELSE  DROP2
            END
            gs s ss - + \->LIST PADD
        NEXT
      \>>
    \>>
@
@ Divide polynomials
@
  PDIV
    \<< DUP 1 GET OVER SIZE \-> d t n
      \<< { } SWAP DUP SIZE n - 1 + 1 SWAP
        START
             DUP 1 GET t / COLCT ROT OVER + 3 ROLLD 1 n
             FOR m
                 OVER m GET d m GET 3 PICK * -
                 ROT m ROT PUT SWAP
             NEXT
             DROP 2 100 SUB
        NEXT
        d DROP
      \>>
    \>>
@
@Split linear factors ( For HP-48SX only )
@
  SPLT
    \<< OBJ\-> DUP \->STR 
        IF "^" SAME NOT
          THEN SWAP DROP EVAL
          ELSE SWAP DROP EVAL DUP
               OBJ\-> DROP2 1 - ^ SPLT
        END
    \>>
END

-----------------Cut here------------------------Cut here----------------------

*******************************************************************************
*                  *                                                          *
*                  * Internet: kemme@jupiter.rz.uni-osnabrueck.de             *
* |)  /\  |/       *                                                          *
* |\./--\.|\       ************************************************************
*                  *                                                          *
* Rainer A.  Kemme * An the moment I cannot send mail out of Germany, and I   *
* Sedanstrasse  16 * don't know if I can receive mail from other countries.   *
* W4500 Osnabrueck * If there are problems, please send mail to my friend     *
* Germany          * Stefan Hammes via Internet: hammes@informatik.uni-kl.de  *
*                  * Replies sent to him will be forwarded.                   *
*                  *                                                          *
*******************************************************************************
 



+---------------------------------------+-------------------------------------+
| Stefan Hammes                         | e-Mail: hammes@informatik.uni-kl.de |
| FB Informatik, SFB 124-D1             +-------------------------------------+
| Universitaet Kaiserslautern, P.O.Box 3049, D-W6750 Kaiserslautern, Germany  |
+-------------+------------------------------------------------+--------------+
              | Language definition: Recursion - see recursion |
              +------------------------------------------------+