Cubic Equation Iteration
Cubic Equation Iteration
edited by
Computer Bulletin Board Steven D. Gammon
Western Washington University
Bellingham, WA 98225-9150
Solving quadratic, cubic, or higher-order equations in that programming by a calculator may have some limitations
the study of chemical equilibria is often necessary. For ex- and needs more creativity. In this article two iteration meth-
ample in some analytical chemistry problems this kind of ods are used, successive-approximations and bisection meth-
equation needs to be solved to find out information such as ods. To write the programs a calculator with the following
See https://pubs.acs.org/sharingguidelines for options on how to legitimately share published articles.
2. Estimate a value for x (call it x0) to find x1, and substi- List 1. Program 1–Successive-Approximations Method
tute x1 into g(x) so that x2 = g(x1); 1. Kout6 11. + 21. Kout5
3. Compare x2 with x1, if the difference between x1 and 2. Kin5 12. Kout2 22. =
x2 is sufficiently small, (x2 − x1) ≤ E, then x2 is the 3. Kout4 13. ⫻ 23. X2
solution (E or tolerance, determines the minimum ac- 4. {–} 14. Kout5 24. √x
ceptable difference between two successive answers, for
5. ÷ 15. + 25. –
more accurate answers choose a smaller value of E);
6. ( 16. Kout3 26. MR
4. If (x2 − x1) > E repeat steps 2 and 3 by calculating xn+1
7. Kout1 17. ) 27. =
= g(xn), n = 0, 1, 2, …, until successive values of x are
8. ⫻ 18. = 28. X > 0
equal within the desired level of accuracy determined
by E. 9. Kout5 19. Kin6 29. Kout6
10. X2 20. –
The programming steps for this method are shown in List 1.
Note that Koutx recalls the content in the xth constant
memory, Kinx enters the number into the xth constant List 2. Program 2—Modified
memory, and MR recalls the contents of the independent Successive-Approximations Method
memory. In some cases this program may diverge; to over- 1. Kout6 13. X2 25. Kin6
come this problem use modified versions such as programs 2. Kin5 14. + 26. –
2 and 3 (Lists 2 and 3). 3. ÷ 15. Kout2 27. Kout5
After that all steps entered, rearrange equations to the 4. 2 16. ⫻ 28. =
general form ax3 + bx2 + cx + d = 0, then input coefficients 5. – 17. Kout5 29. X2
a, b, c, and d into constant memories 1 to 4, respectively. 6. ( 18. + 30. √x
Enter E into the independent memory M, finally enter the 7. Kout4 19.Kout3 31. –
first approximation of x (x0) into the 6th constant memory,
8. ÷ 20. ) 32. MR
then run the programs and wait for the result.
9. ( 21. ÷ 33. =
Bisection Method 10. Kout1 22. 2 34. X > 0
The principal advantage of the bisection method is that 11. ⫻ 23. ) 35. Kout6
it is guaranteed to converge (5). Detail discussion on this 12. Kout5 24. =
method can be found in any numerical analysis textbooks.
In the bisection program the three following steps must be
List 3. Program 3—Modified
satisfied: Successive-Approximations Method
1. Define x2 = (x0 + x1)兾2; 1. Kout6 11. Kout4 20.Kout5
2. If (x1 − x2) ≤ E, accept x2 as the root and stop; 2. Kin5 12. = 21. =
3. If [sign f (x1)] × [sign f (x2)] ≤ 0, set x0 = x2, otherwise 3. X2 13. {–} 22. X2
set x1 = x2 and return to step 1. 4. ⫻ 14. ÷ 23.√x
The programming steps are shown in List 4. 5.Kout2 15. Kout1 24. –
In this case, rearrange equations to the general form ax3 6. + 16. = 25. MR
2
+ bx + cx + d = 0, and divide all coefficients by a to sim- 7. Kout5 17. x1/3 26. =
plify equation as x3 + bx2 + cx + d = 0, then enter b, c, and d 8. ⫻ 18. Kin6 27. X > 0
into constant memories 1 to 3, respectively. Enter E into the 9. Kout3 19. – 28. Kout6
independent memory M; and enter the distance (x0 to x1)
10. +
that contains the root, into the constant memories 4 and 6,
then run the program.
List 4. Program 4—Bisection Method
To compare programs 1–4 some typical problems of
1. Kout6 15. ⫻ 29. Kout5 43. Kin4
chemical equilibrium from the literature were selected and
solved. Cubic equations and their solutions are summarized 2. Kin5 16. Kout1 30. X2 44. {–}
in Tables 1 and 2. 3. Kout4 17. + 31. ⫻ 45. +
4. + 18. Kout6 32. Kout1 46. Kout5
Discussion 5. Kout5 19. ⫻ 33. + 47. Kin6
6. = 20. Kout2 34. Kout5 48. =
It should be noted that equations of order two or higher 7. ÷ 21. + 35. × 49. X2
have more than one root but usually only one of them is 8. 2 22. Kout3 36. Kout2 50. √x
chemically relevant. Thus programs 1–4 should find this root.
9. = 23. = 37. + 51. –
By choosing proper x0 (it can be determined by simple ap-
proximations) in programs 1–3, the probability of converg- 10. Kin6 24. ⫻ 38. Kout3 52. MR
ing to the useful answer will be enhanced. In program 4 the 11. X3 25. ( 39. ) 53. =
interval that contains the chemically relevant root should be 12. + 26. Kout5 40. = 54. X > 0
selected; by this interval, the iteration is limited so that the 13. Kout6 27. X3 41. X > 0 55. Kout4
program converges to the desired root. To determine this 14. X 2
28. + 42. Kout6
Table 2. Results Obtained from Iteration Prorams Table 3. First Approximations of x (x0)
Applied To Solve Equations Listed in Table 1 and Intervals (x0 − x1) That Were Employed
To Solve Equations of Table 1
Results
Eqs Equations x0 (Programs 1–3) x0 − x1 (Program 4)
Program 1 Program 2 Program 3 Program 4
1 1.402 1.402 1.402 1.402 1 0 0, 2
2 diverge diverge 2.42 × 10᎑4 2.42 × 10᎑4 2 1 × 10᎑7 1 × 10᎑7, 1
᎑4 ᎑4 ᎑4
3 6.98 × 10 6.98 × 10 diverge 6.98 × 10 3 0.016 0, 0.016
4 diverge 6.79 × 10᎑3 diverge 6.79 × 10᎑3 4 0.016 0, 0.016
5 diverge diverge 3.89 × 10᎑2 3.89 × 10᎑2 5 0.042 0, 0.042
6 diverge diverge 1.88 × 10᎑7 1.86 × 10᎑7 6 1.73 × 10᎑7 1 × 10᎑7, 1.1 × 10᎑6
᎑8
7 diverge diverge 9.79 × 10᎑8 1.02 × 10᎑7 7 2.49 × 10 1 × 10᎑7, 1.1 × 10᎑6
8 diverge 9.41 × 10᎑5 diverge 9.41 × 10᎑5 8 3 × 10᎑4 1 × 10᎑7, 1.001 × 10᎑4
᎑4
9 diverge 9.49 × 10 ᎑5
diverge 9.49 × 10 ᎑5
9 3 × 10 1 × 10᎑7, 1.001 × 10᎑4
᎑4
10 1.45 × 10 ᎑4
1.45 × 10 ᎑4
diverge 1.45 × 10 ᎑4
10 7.32 × 10 0, 7.32 × 10᎑4
᎑3 ᎑3 ᎑3
11 4.57 × 10 4.57 × 10 diverge 4.57 × 10 11 0.016 0, 0.016
NOTE: E = 1 × 10᎑8 for all calculations.
interval the same routes that are discussed in ref 1 for selec- In addition to the previous equilibria, cubic equations
tion of the appropriate graph window on the graphing cal- may be encountered in solubility calculations of sparingly
culators could be useful. The first guesses and intervals used soluble MX2 salts in the presence of common ion X−. The
by programs 1–4 are summarized in Table 3. following equation describes these kinds of equilibria,
In eqs 6–9 in Table 1 the hydronium ion concentration
is calculated. To find [H3O+] in dilute solutions of weak acids 2
the following cubic equation can be applied: S 3 + C MX 2 S 2 +
(C MX2 ) S −
K sp
= 0
4 4
3 + 2
[ H3O+ ] + Ka [ H 3 O ]
where S is the molar solubility of the salt. In cases that Ksp is
+ small and [X−] is relatively large the simplified form S =
− ( K C HA + K w ) [ H3 O ] − Ka K w = 0
Ksp兾(CMX2)2 can be used with no significant error. Calcula-
tion of solubility of Ba(IO3)2 (Ksp = 1.57 × 10᎑9) in 3 × 10᎑3
In programs 1–3 the first guess of [H3O+] is calculated us- M IO3− solution leads to eq 10 in Table 1. The error using
ing simple approximate equation [H3O+] = (KaCHA)1/2; by the simplified equation is 20.3%, which seems large. The cal-
entering this value as x0 the final result is obtained. In pro- culation of the solubility of PbCl2 (Ksp = 1.6 × 10᎑5) in 0.05
gram 4 the interval that contains the answer is determined M Cl− solution, eq 11, resulted in an error of 39.9%. Using
by the fact that the chemically relevant root lies somewhere approximations for solving eqs 3, 4, and 5 will cause errors
in the range, 10᎑7 < [H3O+] < (10᎑7 + CHA); the lower and of about 2%, 5%, and 45%, respectively (1).
upper limits of this range are the hydrogen ion concentra-
tion of pure water (if the acid does not dissociate) and the
hydrogen ion concentration resulting from complete disso- Conclusion
ciation. As shown by results obtained in Table 2 and calculated
Equations 6 and 7 in Table 1 are derived for determina- errors in previously mentioned, the cubic equations should
tion of [H3O+] in 10᎑6 M solutions of HOCl (Ka = 3 × 10᎑8) be solved for more exact answers, especially in cases in which
and HCN (Ka = 6.2 × 10᎑10), respectively. Using some sim- approximations lead to large errors. It is obvious that most
plifying approximations we can solve them as quadratic equa- convergence is satisfied by programs 2 and 4 (Table 2). In fact
tions with 14.7% and 76% error, respectively. Employing the program 4 is more suitable for solving cubic equations, be-
quadratic equation leads to large error especially in eq 7. In cause it is based on bisection method that is guaranteed to con-
contrast, the general equation verge. However because this program contains more steps (5),
more time is usually needed to obtain the answer relative to
3 2
[ H3O+ ] + K a1 [ H3 O+ ] program 2. For example the execution time for solving eq
11 by program 4 is about 30 seconds more than by program
+ 2 (K a1 K a 2 − K a1C H2A ) [ H3 O+ ] 2 (on Casio fx-3800P calculator). In addition to some cubic
equations, program 2 can be applied to solve quadratic equa-
− K a1 K a 2C H2A = 0 tions.2
Programs that have been discussed in this article are
is used to find [H3O+] in solutions of diprotic acids (H2A) simple and low-cost alternatives relative to higher-priced PC
such as 10᎑4 M fumaric acid (eq 8) and 10᎑4 M tartaric acid software or graphing calculators for solving cubic equations.
(eq 9). The results obtained by quadratic equations have 3.5% Programs of this type enable students to employ maximum
and 4.1% error, respectively. ability of their calculators.