Nth order solver Message #6 Posted by Tizedes Csaba on 26 Aug 2003, 5:26 a.m., in response to message #3 by Steven Soto
Hi Steven,
this is a solution for your problem. This program can solve polinomequations ANY of order (limited by mem). It's not too long, only 92 byte ;). It work fast with builtin solver, and use Hornermethod:
Only one problem with it, it's solve JUST REAL ROOTS... But in the real problems, like mechanical stress calculations, the roots are reals.
This is the HP32SII version:
E01 LBL E #prepare of 'i'
FIX 3
RCL N
1
add
1E3
div
1
add
RND
STO i
0
E13 RTN
P01 LBL P #the 'dinamical polynom'
XEQ E
H01 LBL H #Horner's method
RCL mul X
RCL add (i)
ISG i
GTO H
H06 RTN
X01 LBL X #polynom solver
SOLVE X
GTO R #if found root goto 'R'
SF 0 #no real root, set flag 0
X05 RTN
D01 LBL D #decrement polynom order,
XEQ E
J01 LBL J #use a little modified Horner method
RCL mul X
RCL add (i)
STO (i) #overwrite coefficients
ISG i #with decremented polyn.'s coefficients
GTO J
J07 GTO X
S01 LBL S #startroutine
CF 0
CF 1
INPUT N
S05 XEQ E
I01 LBL I #coefficient inputroutine
INPUT (i)
ISG i
GTO I
FN= P
I06 GTO X
R01 LBL R #we found a root
VIEW X
1
STO sub N
VIEW N #number of roots, what we dont found yet
RCL N
x<>0? #more root?
GTO D #go decrement order, and solve again
SF 1 #no more root, and all was REAL: set flag 1
R10 RTN
'add', 'sub', 'mul' and 'div' mean 'add', 'substract', 'multiply' and 'divide' in this order.
The checksums:
E:CK=0BE8 27.5 byte
P:CK=E452 3.0 byte
H:CK=3E78 9.0 byte
X:CK=3894 7.5 byte
D:CK=11DF 3.0 byte
J:CK=C765 10.5 byte
S:CK=BB8E 7.5 byte
I:CK=32B5 9.0 byte
R:CK=E775 15.0 byte
An example:
solve this polynom with this program:
2*x^6 6*x^5 +82*x^4 +174*x^3 800*x^2 888*x +1440 =0
A*X^6 +B*X^5 +C*X^4 +D*X^3 +E*X^2 +F*X +G=0
In the program the coefficients are A, B, C, ... The limit of order N=12, because the 14th variable is N, and we use this variable for the order of solved polynom. If you replace it with Z, you can solve the 24th order of polynomials. And it's not a joke...
Now, start the program: [XEQ S] the calc prompting for order of polynom:
N? type 6 [R/S].
Then the program prompts for all of coefficients, type it in decrease order:
A? 2 [R/S]
B? 6 [R/S]
C? 82 [R/S]
D? 174 [R/S]
E? 800 [R/S]
F? 888 [R/S]
G? 1440 [R/S]
Then you will see the 'RUNNING/SOLVING' message, and the program stops with one of six roots: for example X=1. Press [R/S], and you'll see N=5 this mean, the polynom's got 5 root yet. Press [R/S] and repeat this method until all of roots founded.
When N=0 press [R/S], the 'flag1' anunciator set and program end. 'flag1' set mean, all of roots was real.
This polynom got 6 real roots: 1, 2, 3, 4, 5, 6, and the simplyfied form is: 2*(x1)*(x+2)*(x3)*(x+4)*(x5)*(x+6)
I hope it help to you and anybody, who want to solve polynomials. The roots are 'more exacts' if the round off error is not a very big.
Csaba
