Post Reply 
Solving for the roots of a cubic polynomial
07-02-2024, 07:57 PM
Post: #6
RE: Solving for the roots of a cubic polynomial
Hi, Namir

It is not zeros that caused the problem. You could even try x^3 = 0, and it still work.

The problem is cube root of negative numbers. (note: all coefficients are real)
Code seems to assume first cube root is always positive.
2nd cube root use flag 01 to get sign(x) * ³√|x|

This is why y^3 - y/3 + 2/27 = 0 roots are 0

y1 = ³√(-1/27+0) + ³√(-1/27−0), mistakenly calculated as 1/3 - 1/3 = 0

We could reuse flag 01 for both cube roots, or do without. ³√(x) = sign(x) * ³√|x|

Note: I only touched cube roots. (LINE 65 .. 71, copy/paste to LINE 76 .. 82)
Code:
00 { 281-Byte Prgm }
01▸LBL "CUBE"
02▸LBL A
03 CF 00
04 "b?"
05 PROMPT
06 STO 07
07 "a?"
08 PROMPT
09 STO 08
10 ENTER
11 ×
12 3
13 ÷
14 -
15 STO 01
16 RCL 08
17 3
18 ÷
19 ENTER
20 ENTER
21 ENTER
22 ×
23 ×
24 2
25 ×
26 X<>Y
27 RCL 07
28 ×
29 -
30 "c?"
31 PROMPT
32 +
33 STO 02
34 ENTER
35 ×
36 4
37 ÷
38 RCL 01
39 ENTER
40 ENTER
41 ×
42 ×
43 27
44 ÷
45 +
46 RND
47 STO 03
48 "d="
49 ARCL ST X
50 PROMPT
51▸LBL 11
52 DEG
53 X=0?
54 SF 00
55 X<0?
56 GTO 31
57 SQRT
58 STO 03
59 RCL 02
60 +/-
61 2
62 ÷
63 STO 02
64 +
65 SIGN
66 LASTX
67 ABS
68 3
69 1/X
70 Y↑X
71 ×
72 STO 04
73 RCL 02
74 RCL 03
75 -
76 SIGN
77 LASTX
78 ABS
79 3
80 1/X
81 Y↑X
82 ×
83 STO 05
84 RCL 04
85 +
86 STO 04
87 RCL 08
88 3
89 ÷
90 STO 08
91 -
92 "X1="
93 ARCL ST X
94 PROMPT
95 RCL 04
96 2
97 ÷
98 +/-
99 STO 06
100 RCL 08
101 -
102 STO 11
103 FS?C 00
104 GTO 00
105 RCL 04
106 RCL 05
107 2
108 ×
109 -
110 2
111 ÷
112 3
113 SQRT
114 ×
115 STO 05
116 STO 12
117 CLA
118 ARCL 11
119 ├","
120 ARCL 12
121 PROMPT
122 CLA
123 RCL 12
124 +/-
125 ARCL 11
126 ├","
127 ARCL ST X
128 PROMPT
129 RTN
130▸LBL 31
131 RCL 02
132 RCL 01
133 ENTER
134 ENTER
135 ×
136 ×
137 +/-
138 27
139 ÷
140 SQRT
141 2
142 ×
143 ÷
144 +/-
145 ACOS
146 3
147 ÷
148 STO 02
149 COS
150 RCL 01
151 3
152 ÷
153 X↑2
154 SQRT
155 SQRT
156 2
157 ×
158 STO 01
159 ×
160 RCL 08
161 3
162 ÷
163 STO 08
164 -
165 "X1="
166 ARCL ST X
167 PROMPT
168 RCL 02
169 120
170 +
171 COS
172 RCL 01
173 ×
174 RCL 08
175 -
176 "X2="
177 ARCL ST X
178 PROMPT
179 RCL 02
180 240
181 +
182 COS
183 RCL 01
184 ×
185 RCL 08
186 -
187 "X3="
188 ARCL ST X
189 PROMPT
190 RTN
191▸LBL 00
192 "X2="
193 ARCL 11
194 PROMPT
195 "X3="
196 ARCL 11
197 PROMPT
198 RTN
199 END
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Solving for the roots of a cubic polynomial - Albert Chan - 07-02-2024 07:57 PM



User(s) browsing this thread: 1 Guest(s)