[comp.sys.handhelds] simul.non-linear equations

akcs.wiser1@hpcvbbs.UUCP (steven lee wiser) (03/24/91)

Simultaneous Non-Linear Equation Solver:  Nonlinear and linear 

    This is a program description that solves simultaneous non-linear 
equations, similar to the programs like TK-Solver Plus ( sold by 
Universal Technical Systems, 1220 Rock St. Rockford. Il. 1-800-435-
7887).  Of course this is not as powerful as TK, but I think you will be 
impressed by what the HP-48Sx can do.  I have solved up to 10 non-linear 
equations will  this routine.  Patience is necessary with your HP-48sx 
above 10 equations or if the equations are of complicated form. 

    This routine uses the solution developed in the book "Design of 
Thermal Systems", third edition, by W. R. Stoecker , published by McGraw 
Hill.   The Newton-Raphson Method with multiple equations  and unknowns 
is the mathematical method used.             

    Approximately 1k of memory is used by the basic routines.  
Additional memory is used for the equations and guess terms. 

    Additionally, linear equations can be easily solved with this 
system.  Usually only one iteration is necessary for the linear system.  
With this system, the equations can be written in any form or order as 
is convenient. 

SIMUL.EQ    Name of the directory on my HP-28S or 48SX

            The following are words in the SIMUL.EQ directory:

CONT        control program for solving the equations

u           test for convergence number

NEW.G       sequence that defines the new guesses of values

DIF.F       program sequence that takes the partial derivatives

EVA.F       program sequence to evaluate the primary functions at each 
            iteration guess value. 

F           list of equation names      ex. { f1 f2 f3 ...}

L           list of variable names      ex. { x y z ....}

CONT        << 0 'ITER' STO F 
               SIZE 'A' STO {A 1 } 
               1 CON { A 1 } 0 CON 
               TMP1' STO 'TMP2' 
               STO
                  DO NEW.G 'ITER' 1
               STO+ 
                  UNTIL TMP1 TMP2  - 
               ARRY->  DROP  A  1  -  1  
                      START +  -1 
                      STEP ABS u <
                  END 440 .5 BEEP 
             >>

u           .1    This value should vary based upon the magnitude of     
     
the guess values.   Ex. variable guess  values of 1000 should indicate 
that a relative large value for u should be used; for small variable 
guess values a small value of u should be used.  If a large guess and a 
small guess are both in the problem experience should dictate which is 
the most important value to govern the control value. 

NEW.G       << 1  A 
                  FOR J 'L(J)' EVAL
               EVAL
                  NEXT { A 1 }  ->ARRY
               'TMP1' STO 1 A 
  HHHH$$$$JR(J)' EVAL
               EVAL  
                  NEXT { A 1 }  ->ARRY
               DIF.F  -  ARRY->  DROP A 1
                  FOR K 'L(K)' EVAL
               STO   -1 
                  STEP 1 A
                  FOR J 'L(J)' EVAL
               EVAL
                  NEXT { A 1 }  ->ARRY
               'TMP2' STO
             >>

                            

DIF.F       <<  EVA.F  1  A
                   FOR J 1 A 
                      FOR K 'F(J)'        
                EVAL  'L(K)' EVAL  d    (see d  explained below)
                      NEXT
                   NEXT { A A  }  ->ARRY
                /
             >>

                    WHERE ' d  ' is the differential symbol in HP-28s
                                 and now the HP-48SX.
 

EVA.F        << 1 A
                   FOR J 'F(J)' EVAL
                EVAL EVAL 
                   NEXT { A 1 }  ->ARRY
              >>

                
    Note: for very complicated equations it may be necessary to 
experiment with additional numbers of "EVAL" where you see 2 or 3 of 
them presently. 

    Example of a equation:     f1

            z = x^2 + y*SIN(x)  with   variables x , y , z 

                rewrite f1 to be :     x^2 + y*sin(x) - z 

           
    This routine sets up the equations like this:

              ---------------MATRIX----------------------- /   EVAL-----
      f1  :   d(f1)/d(x)   d(f1)/d(y)   d(f1)/d(z)  ....   /  
f1(x,y,z,..)    
      f2  :   d(f2)/d(x)   d(f2)/d(y)   d(f2)/d(z)  ....   /  
f2(x,y,z...) 
      f3  :   d(f3)/d(x)   d(f3)/d(y)   d(f3)/d(z)  ....   /  
f3(x,y,z...) 
       |          |            |            |              |   |
       |          |            |            |              |   |
           

         f1 f2 f3 are initially evaluated at the initial guess values

         x , y and z.... are solved based on matrix algebra 

         These new values of x, y, and z are then used to evaluate the   

equations f1, f2, f3 again to achieve a new set of x, y, and z guess 
values. This continues until the sum of the differences from one f1 to 
another f1 thru f3... is less than the test value given in the term "u." 

    To review to final answers look at either one of the "temp" matrices 
oreview the actual variables within the list.

    Whenever the test value is less than the sum of the differences the 
routine stops and beeps.  This routine can be changed in various ways.  
One is to beep at each iteration.  Another is to send to the screen the 
values of the variables to indicate how the solution is progressing. 

Have a good time with it.  Steve Wiser 
                            Richmond Va

 I assume no responsiblity for any use of this program whatsoever. Slw 

Uploadable program 
follows:

%%HP: T(3)A(D)F(.);
DIR
  F { F1 F2 F3 }
  L { X Y Z }
  \Gm LLLL  CONTROL
    \<TIME 0 'ITER'
STO F SIZE 'A' STO
{ A 1 } 1 CON { A 1
} 0 CON 'TMP1' STO
'TMP2' STO
      DO XXX 'ITER'
1 STO+
      UNTIL TMP1
TMP2 - OBJ\-> DROP A
1 - 1
        START + -1
        STEP ABS \Gm
\<=
      END 440 .5
BEEP TIME SWAP -
10000 *
    \>>
  XXX
    \<< 1 A
      FOR j 'L(j)'
EVAL EVAL
      NEXT { A 1 }
\->ARRY 'TMP1' STO 1
A
      FOR j 'L(j)'
EVAL EVAL
      NEXT { A 1 }
\->ARRY ZZZ - OBJ\->
DROP A 1
      FOR k 'L(k)'
EVAL STO -1
      STEP 1 A
      FOR j 'L(j)'
EVAL EVAL
      NEXT { A 1 }
\->ARRY 'TMP2' STO
    \>>
  ZZZ
    \<< YYY 1 A
      FOR j 1 A
        FOR k 'F(j)
' EVAL 'L(k)' EVAL
\.d
        NEXT
      NEXT { A A }
\->ARRY /
    \>>
  YYY
    \<< 1 A
      FOR j 'F(j)'
EVAL EVAL EVAL
      NEXT { A 1 }
\->ARRY
    \>>
END

An example of a set of equations if as follows:

let F1 be  .... DP-5*w^2-500             from DP = 5*w + 500
let F2 be  .... 1000-30*w1-4.75*w1^2-DP  from DP = 1000-30 * w1 - 4.75 * 
                                                  w1^2
let F3 be  .... 900-45*w2-30*w2^2-DP     from DP = 900 - 45 * w2 - 30 * 
                                                  w2^2
let f4 be ..... w-w1-w2                  from w = w1 + wlways 
return a solution to just any set of equations.

Note: This was originally written when I had my HP-28c believe it or 
not. And with it I was able to solve up to six or seven simple 
equations.

Note that the downloadable program has a change that lets you see how
much time it takes to solve an equation set. Other changes can easily be
made. One that has recently come to mind is not using the stack as I do
now and using simply an matrix instead.

slw finally figure out how to upload.  Use GeoWorks Ensemble...