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 FOR J 'L(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 or review 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 .000001 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 + w2 "F" becomes {F1 F2 F3 F4} "L" becomes {w w1 w2 DP} provide guess values of w, w1, w2, and DP let w=6 becomes w = 6.77356 let w1=2 w1 = 5.0237642 let w2=4 w2 = 1.749796 let DP=600 DP = 729.40559 Note: You should not have the symbolic flag on the HP48 SX set before you run this routine, a bad argument error may result. Note: The Newton-Raphson technique is a great way to solve simul. non- linear equations but caution should be exercised. It does not always 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. ------------------------------ Additional notes by Morgan LaVake (akcs.lavake on the HP BBS) Steven Wiser may be reached on the HP BBS (internet 15.255.72.16) as akcs.wiser1. Steven provides above two slightly different versions of the same program. The downloadable version renames the files from those spelled out in the description. Old Name New Name CONT CONTROL u greek letter mu NEW.G XXX DIF.F ZZZ EVA.F YYY Also note that the downloadable version returns to the stack the time the program took to execute. The program creates several variables. "A" is created and used by the program, it is the number of equations/ unknowns. "ITER" is created, it is for your refrence it contains the number of iterations that took place. The TEMP1 and TEMP2 matrices are self explanatory. I would like to make several suggestions that make the program more friendly. First, I suggest you add the following or similar code to the CONTROL program (as Steven suggests). Where the code presently says ...DO XXX 'ITER' 1 STO+ UNTIL... replace with ... DO XXX 'ITER' 1 STO+ ITER 1 DISP TMP2 ->STR 2 DISP UNTIL... this displays the iteration number and current solution refinement and lets you get an idea of what is going on and how long you may have to wait for a solution. I would also suggest that you drive the program with a CST menu to keep things locatable as variables are created. I use the following as the CST menu in the directory: { { CONTROL } { "F'S" F } { VARS L } mu { } { "-SYM" << -3 SF >> } } This allows me to store values into mu, the F and L lists, and turn off symbolic mode without having to search for these functions in the VAR menu. I final hint I would like to leave you is that to erase the mess this program makes, you can recall the F and L lists and PURGE such to erase all the functions and variables that have been created. If you wish, write a program to do this and to erase the TEMP matrices. I have only played with this program a little, but I think it may well be the most valuable general math tool you can place in your HP. It is powerful and small.