Solution of Cubic Equations by Iteration Methods on a Pocket Calculator

4
Information Textbooks Media Resources 758 Journal of Chemical Education Vol. 81 No. 5 May 2004 www.JCE.DivCHED.org s n o i t a u q E m u i r b i l i u q E l a c i m e h C e m o S . 1 e l b a T n o i t a u q E n o i t a u q E c i b u C d e g n a r r a e R . 1 x 3 2 + x 2 3 4 . 8 + x 0 = 5 . 8 1 a 1x 3 2 + x 2 3 4 . 8 + x 0 = 5 . 8 1 . 2 H O [ 2 = ] K p s H O [ / ] 2 + K w H O [ / ] b 1x 3 0 + x 2 0 1 4 1 x 2 4 . 1 0 1 1 1 0 = . 3 K p s ( = S 2 ( ) S ) 5 1 . 0 + 2c 4x 3 0 6 . 0 + x 2 5 2 2 0 . 0 + x 6 . 1 0 1 5 0 = . 4 K p s ( = S 2 ( ) 8 0 . 0 + S) 2c 4x 3 2 3 . 0 + x 2 6 . 1 x 0 + 0 1 5 0 = . 52 6 6 . 1 ( 0 1 5 l C [ / ] 2 6 . 6 ( + ) 0 1 4 l C [ / l C [ ) ] 0 = ] d 1x 3 0 + x 2 6 . 6 0 1 4 x 2 3 . 3 0 1 5 0 = . 6 H [ + ] 3 3 + 0 1 8 H [ + ] 2 4 0 1 4 1 H [ + 3 ] 0 1 2 2 0 = 1x 3 3 + 0 1 8 x 2 4 0 1 4 1 x 3 0 1 2 2 0 = . 7 H [ + ] 3 2 . 6 + 0 1 0 1 H [ + ] 2 2 6 0 . 1 0 1 4 1 H [ + 2 . 6 ] 0 1 4 2 0 = 1x 3 2 . 6 + 0 1 0 1 x 2 2 6 0 . 1 0 1 4 1 x 2 . 6 0 1 4 2 0 = . 8 H [ + ] 3 5 8 . 8 + 0 1 4 H [ + ] 2 7 1 . 3 0 1 8 H [ + 8 6 . 5 ] 0 1 2 1 0 = 1x 3 5 8 . 8 + 0 1 4 x 2 7 1 . 3 0 1 8 x 8 6 . 5 0 1 2 1 0 = . 9 H [ + ] 3 2 . 9 + 0 1 4 H [ + ] 2 7 2 . 1 0 1 8 H [ + 3 9 . 7 ] 0 1 2 1 0 = 1x 3 2 . 9 + 0 1 4 x 2 7 2 . 1 0 1 8 x 3 9 . 7 0 1 2 1 0 = . 0 1 S 3 3 + 0 1 3 S 2 5 2 . 2 + 0 1 6 S 2 9 . 3 0 1 0 1 0 = 1x 3 3 + 0 1 3 x 2 5 2 . 2 + 0 1 6 x 2 9 . 3 0 1 0 1 0 = . 1 1 S 3 5 0 . 0 + S 2 5 2 . 6 + 0 1 4 S 4 0 1 6 0 = 1x 3 5 0 . 0 + x 2 5 2 . 6 + 0 1 4 x 4 0 1 6 0 = a f e R . 6 b f e R 7. c f e R . 1 d f e R . 8 Solving quadratic, cubic, or higher-order equations in the study of chemical equilibria is often necessary. For ex- ample in some analytical chemistry problems this kind of equation needs to be solved to find out information such as solubility, hydronium ion concentration, and so on. Algebraic methods may be applied to solve these equations, but in some cases, the equations cannot be solved analytically (by alge- braic methods) and the only method to obtain the answer is to approximate the solution by iteration methods. With a computer, iteration methods are often simpler and less prone to error, particularly for higher-order equa- tions in which algebraic methods can not be used. 1 However this approach is not cheap! In addition there may be some limitations in accessing a computer and many students may need additional instructions to use computer-based compu- tational software (1). Recently a paper in this Journal investigated methods for solving algebraic expressions without a PC; the authors used graphing calculators (1). A pocket calculator is relatively in- expensive and can be used in classroom, at exam sessions, and in the lab. Advantages of using pocket calculators in equi- librium calculations were mentioned previously in this Jour- nal (2, 3). The purpose of this article is to provide students a vi- sion of how they can write iteration programs on a inexpen- sive programmable pocket calculator, without requiring a PC or a graphing calculator. However the students should note that programming by a calculator may have some limitations and needs more creativity. In this article two iteration meth- ods are used, successive-approximations and bisection meth- ods. To write the programs a calculator with the following specification is needed: 1. Six constant memories (K) and one independent memory (M). 2. X>0 key; this function compares the last result of cal- culation (x) with zero, when the calculation process reaches this function it returns to the first step of the program if x > 0, or continues to the next step other- wise. 3. Capability of 38 programming steps (55 steps in the bisection method). The programs listed were written and tested on Casio fx- 3600, Casio fx-3600PV, and Casio fx-3800P scientific cal- culators. Program Construction Successive-Approximations Method The successive-approximations method (or fixed-point method) (4) contains four principal steps: 1. To find a root α of f (x), rearrange the equation so that x 1 = g (x), note α is also a root of g (x); Solution of Cubic Equations by Iteration Methods on a Pocket Calculator Farzad Bamdad Department of Chemistry, Shahid Beheshti Avenue, Arak University, Arak 38156, Iran; [email protected] Computer Bulletin Board edited by Steven D. Gammon Western Washington University Bellingham, WA 98225-9150

Transcript of Solution of Cubic Equations by Iteration Methods on a Pocket Calculator

Page 1: Solution of Cubic Equations by Iteration Methods on a Pocket Calculator

Information • Textbooks • Media • Resources

758 Journal of Chemical Education • Vol. 81 No. 5 May 2004 • www.JCE.DivCHED.org

snoitauqEmuirbiliuqElacimehCemoS.1elbaT

noitauqE noitauqEcibuCdegnarraeR

.1 x3 2+ x2 34.8+ x 0=5.81– a 1x3 2+ x2 34.8+ x 0=5.81–

.2 HO[ − 2=] K ps HO[/ −]2 + Kw HO[/ −] b 1x3 0+ x2 01– � 41 x 24.1– × 01 � 11 0=

.3 K ps (= S 2() S )51.0+ 2 c 4x3 06.0+ x2 5220.0+ x 6.1– × 01 �5 0=

.4 K ps (= S 2()80.0+ S)2 c 4x3 23.0+ x2 6.1–x0+ × 01 �5 0=

.5 2 × 66.1( × 01 �5 lC[/ −]2 6.6(+) × 01 �4 lC[/ − lC[–)] − 0=] d 1x3 0+ x2 6.6– × 01 �4x 23.3– × 01 �5 0=

.6 H[ +]3 3+ × 01 �8 H[ +]2 4– × 01 � 41 H[ + 3–] × 01 � 22 0= 1x3 3+ × 01 �8x2 4– × 01 � 41 x 3– × 01 � 22 0=

.7 H[ +]3 2.6+ × 01 � 01 H[ +]2 260.1– × 01 � 41 H[ + 2.6–] × 01 � 42 0= 1x3 2.6+ × 01 � 01 x2 260.1– × 01 � 41 x 2.6– × 01 � 42 0=

.8 H[ +]3 58.8+ × 01 �4 H[ +]2 71.3– × 01 �8 H[ + 86.5–] × 01 � 21 0= 1x3 58.8+ × 01 �4x2 71.3– × 01 �8x 86.5– × 01 � 21 0=

.9 H[ +]3 2.9+ × 01 �4 H[ +]2 72.1– × 01 �8 H[ + 39.7–] × 01 � 21 0= 1x3 2.9+ × 01 �4x2 72.1– × 01 �8x 39.7– × 01 � 21 0=

.01 S3 3+ × 01 �3S2 52.2+ × 01 �6S 29.3– × 01 � 01 0= 1x3 3+ × 01 �3x2 52.2+ × 01 �6x 29.3– × 01 � 01 0=

.11 S3 50.0+ S2 52.6+ × 01 �4S 4– × 01 �6 0= 1x3 50.0+ x2 52.6+ × 01 �4x 4– × 01 �6 0=a feR .6 b feR 7. c feR .1 d feR .8

Solving quadratic, cubic, or higher-order equations inthe study of chemical equilibria is often necessary. For ex-ample in some analytical chemistry problems this kind ofequation needs to be solved to find out information such assolubility, hydronium ion concentration, and so on. Algebraicmethods may be applied to solve these equations, but in somecases, the equations cannot be solved analytically (by alge-braic methods) and the only method to obtain the answer isto approximate the solution by iteration methods.

With a computer, iteration methods are often simplerand less prone to error, particularly for higher-order equa-tions in which algebraic methods can not be used.1 Howeverthis approach is not cheap! In addition there may be somelimitations in accessing a computer and many students mayneed additional instructions to use computer-based compu-tational software (1).

Recently a paper in this Journal investigated methods forsolving algebraic expressions without a PC; the authors usedgraphing calculators (1). A pocket calculator is relatively in-expensive and can be used in classroom, at exam sessions,and in the lab. Advantages of using pocket calculators in equi-librium calculations were mentioned previously in this Jour-nal (2, 3).

The purpose of this article is to provide students a vi-sion of how they can write iteration programs on a inexpen-sive programmable pocket calculator, without requiring a PCor a graphing calculator. However the students should note

that programming by a calculator may have some limitationsand needs more creativity. In this article two iteration meth-ods are used, successive-approximations and bisection meth-ods. To write the programs a calculator with the followingspecification is needed:

1. Six constant memories (K) and one independentmemory (M).

2. X>0 key; this function compares the last result of cal-culation (x) with zero, when the calculation processreaches this function it returns to the first step of theprogram if x > 0, or continues to the next step other-wise.

3. Capability of 38 programming steps (55 steps in thebisection method).

The programs listed were written and tested on Casio fx-3600, Casio fx-3600PV, and Casio fx-3800P scientific cal-culators.

Program Construction

Successive-Approximations MethodThe successive-approximations method (or fixed-point

method) (4) contains four principal steps:1. To find a root α of f (x), rearrange the equation so that

x1 = g(x), note α is also a root of g(x);

Solution of Cubic Equations by Iteration Methodson a Pocket CalculatorFarzad BamdadDepartment of Chemistry, Shahid Beheshti Avenue, Arak University, Arak 38156, Iran; [email protected]

Computer Bulletin Boardedited by

Steven D. GammonWestern Washington UniversityBellingham, WA 98225-9150

Page 2: Solution of Cubic Equations by Iteration Methods on a Pocket Calculator

Information • Textbooks • Media • Resources

www.JCE.DivCHED.org • Vol. 81 No. 5 May 2004 • Journal of Chemical Education 759

2. Estimate a value for x (call it x0) to find x1, and substi-tute x1 into g(x) so that x2 = g(x1);

3. Compare x2 with x1, if the difference between x1 andx2 is sufficiently small, (x2 − x1) ≤ E, then x2 is thesolution (E or tolerance, determines the minimum ac-ceptable difference between two successive answers, formore accurate answers choose a smaller value of E);

4. If (x2 − x1) > E repeat steps 2 and 3 by calculating xn+1

= g(xn), n = 0, 1, 2, …, until successive values of x areequal within the desired level of accuracy determinedby E.

The programming steps for this method are shown in List 1.Note that Koutx recalls the content in the xth constantmemory, Kinx enters the number into the xth constantmemory, and MR recalls the contents of the independentmemory. In some cases this program may diverge; to over-come this problem use modified versions such as programs2 and 3 (Lists 2 and 3).

After that all steps entered, rearrange equations to thegeneral form ax3 + bx2 + cx + d = 0, then input coefficientsa, b, c, and d into constant memories 1 to 4, respectively.Enter E into the independent memory M, finally enter thefirst approximation of x (x0) into the 6th constant memory,then run the programs and wait for the result.

Bisection MethodThe principal advantage of the bisection method is that

it is guaranteed to converge (5). Detail discussion on thismethod can be found in any numerical analysis textbooks.In the bisection program the three following steps must besatisfied:

1. Define x2 = (x0 + x1)�2;

2. If (x1 − x2) ≤ E, accept x2 as the root and stop;

3. If [sign f (x1)] × [sign f (x2)] ≤ 0, set x0 = x2, otherwiseset x1 = x2 and return to step 1.

The programming steps are shown in List 4.In this case, rearrange equations to the general form ax3

+ bx2 + cx + d = 0, and divide all coefficients by a to sim-plify equation as x3 + bx2 + cx + d = 0, then enter b, c, and dinto constant memories 1 to 3, respectively. Enter E into theindependent memory M; and enter the distance (x0 to x1)that contains the root, into the constant memories 4 and 6,then run the program.

To compare programs 1–4 some typical problems ofchemical equilibrium from the literature were selected andsolved. Cubic equations and their solutions are summarizedin Tables 1 and 2.

Discussion

It should be noted that equations of order two or higherhave more than one root but usually only one of them ischemically relevant. Thus programs 1–4 should find this root.By choosing proper x0 (it can be determined by simple ap-proximations) in programs 1–3, the probability of converg-ing to the useful answer will be enhanced. In program 4 theinterval that contains the chemically relevant root should beselected; by this interval, the iteration is limited so that theprogram converges to the desired root. To determine this

deifidoM—3margorP.3tsiLdohteMsnoitamixorppA-evisseccuS

6tuoK.1 4tuoK.11 5tuoK.025niK.2 =.21 =.12

X.3 2 }–{.31 X.22 2

.4 � .41 ÷ .32 √x2tuoK.5 1tuoK.51 –.42

+.6 =.61 RM.525tuoK.7 .71 x 3/1 =.62

.8 � 6niK.81 0>X.723tuoK.9 –.91 6tuoK.82

+.01

dohteMnoitcesiB—4margorP.4tsiL6tuoK.1 .51 � 5tuoK.92 4niK.34

5niK.2 1tuoK.61 X.03 2 }–{.444tuoK.3 +.71 .13 � +.54

+.4 6tuoK.81 1tuoK.23 5tuoK.645tuoK.5 .91 � +.33 6niK.74

=.6 2tuoK.02 5tuoK.43 =.84.7 ÷ +.12 .53 × X.94 2

2.8 3tuoK.22 2tuoK.63 .05 √x=.9 =.32 +.73 –.15

6niK.01 .42 � 3tuoK.83 RM.25X.11 3 (.52 ).93 =.35+.21 5tuoK.62 =.04 0>X.45

6tuoK.31 X.72 3 0>X.14 4tuoK.55X.41 2 +.82 6tuoK.24

dohteMsnoitamixorppA-evisseccuS–1margorP.1tsiL6tuoK.1 +.11 5tuoK.12

5niK.2 2tuoK.21 =.224tuoK.3 .31 � X.32 2

}–{.4 5tuoK.41 .42 √x.5 ÷ +.51 –.52(.6 3tuoK.61 RM.62

1tuoK.7 ).71 =.72.8 � =.81 0>X.82

5tuoK.9 6niK.91 6tuoK.92X.01 2 –.02

deifidoM—2margorP.2tsiLdohteMsnoitamixorppA-evisseccuS

6tuoK.1 X.31 2 6niK.525niK.2 +.41 –.62

.3 ÷ 2tuoK.51 5tuoK.722.4 .61 � =.82–.5 5tuoK.71 X.92 2

(.6 +.81 .03 √x4tuoK.7 3tuoK.91 –.13

.8 ÷ ).02 RM.23(.9 .12 ÷ =.33

1tuoK.01 2.22 0>X.43.11 � ).32 6tuoK.53

5tuoK.21 =.42

Page 3: Solution of Cubic Equations by Iteration Methods on a Pocket Calculator

Information • Textbooks • Media • Resources

760 Journal of Chemical Education • Vol. 81 No. 5 May 2004 • www.JCE.DivCHED.org

interval the same routes that are discussed in ref 1 for selec-tion of the appropriate graph window on the graphing cal-culators could be useful. The first guesses and intervals usedby programs 1–4 are summarized in Table 3.

In eqs 6–9 in Table 1 the hydronium ion concentrationis calculated. To find [H3O+] in dilute solutions of weak acidsthe following cubic equation can be applied:

a3 2

H O+3[ ] H O+

3[ ]+ K

a w 0H O+3CHA [ ] − =w− +K K K K( )

In programs 1–3 the first guess of [H3O+] is calculated us-ing simple approximate equation [H3O+] = (KaCHA)1/2; byentering this value as x0 the final result is obtained. In pro-gram 4 the interval that contains the answer is determinedby the fact that the chemically relevant root lies somewherein the range, 10�7 < [H3O+] < (10�7 + CHA); the lower andupper limits of this range are the hydrogen ion concentra-tion of pure water (if the acid does not dissociate) and thehydrogen ion concentration resulting from complete disso-ciation.

Equations 6 and 7 in Table 1 are derived for determina-tion of [H3O+] in 10�6 M solutions of HOCl (Ka = 3 × 10�8)and HCN (Ka = 6.2 × 10�10), respectively. Using some sim-plifying approximations we can solve them as quadratic equa-tions with 14.7% and 76% error, respectively. Employing thequadratic equation leads to large error especially in eq 7. Incontrast, the general equation

a

a

1

1

3 2

2

H O+3[ ]

H O+3[ ]

H O+3[ ]+

+

K

K KK K

K K

a a

a a

2 1

1

H A2

2

−( )− = 0

C

H A2C

is used to find [H3O+] in solutions of diprotic acids (H2A)such as 10�4 M fumaric acid (eq 8) and 10�4 M tartaric acid(eq 9). The results obtained by quadratic equations have 3.5%and 4.1% error, respectively.

In addition to the previous equilibria, cubic equationsmay be encountered in solubility calculations of sparinglysoluble MX2 salts in the presence of common ion X−. Thefollowing equation describes these kinds of equilibria,

S S SK sp

4 4+ + − =3 2 0

2MX2

CMX2

C( )

where S is the molar solubility of the salt. In cases that Ksp issmall and [X−] is relatively large the simplified form S =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

M IO3− solution leads to eq 10 in Table 1. The error using

the simplified equation is 20.3%, which seems large. The cal-culation of the solubility of PbCl2 (Ksp = 1.6 × 10�5) in 0.05M Cl− solution, eq 11, resulted in an error of 39.9%. Usingapproximations for solving eqs 3, 4, and 5 will cause errorsof about 2%, 5%, and 45%, respectively (1).

Conclusion

As shown by results obtained in Table 2 and calculatederrors in previously mentioned, the cubic equations shouldbe solved for more exact answers, especially in cases in whichapproximations lead to large errors. It is obvious that mostconvergence is satisfied by programs 2 and 4 (Table 2). In factprogram 4 is more suitable for solving cubic equations, be-cause it is based on bisection method that is guaranteed to con-verge. However because this program contains more steps (5),more time is usually needed to obtain the answer relative toprogram 2. For example the execution time for solving eq11 by program 4 is about 30 seconds more than by program2 (on Casio fx-3800P calculator). In addition to some cubicequations, program 2 can be applied to solve quadratic equa-tions.2

Programs that have been discussed in this article aresimple and low-cost alternatives relative to higher-priced PCsoftware or graphing calculators for solving cubic equations.Programs of this type enable students to employ maximumability of their calculators.

smarorPnoitaretImorfdeniatbOstluseR.2elbaT1elbaTnidetsiLsnoitauqEevloSoTdeilppA

sqEstluseR

1margorP 2margorP 3margorP 4margorP1 204.1 204.1 204.1 204.12 egrevid egrevid 24.2 × 01 �4 24.2 × 01 �4

3 89.6 × 01 �4 89.6 × 01 �4 egrevid 89.6 × 01 �4

4 egrevid 97.6 × 01 �3 egrevid 97.6 × 01 �3

5 egrevid egrevid 98.3 × 01 �2 98.3 × 01 �2

6 egrevid egrevid 88.1 × 01 �7 68.1 × 01 �7

7 egrevid egrevid 97.9 × 01 �8 20.1 × 01 �7

8 egrevid 14.9 × 01 �5 egrevid 14.9 × 01 �5

9 egrevid 94.9 × 01 �5 egrevid 94.9 × 01 �5

01 54.1 × 01 �4 54.1 × 01 �4 egrevid 54.1 × 01 �4

11 75.4 × 01 �3 75.4 × 01 �3 egrevid 75.4 × 01 �3

N ETO : E 1= × 01 �8 .snoitaluclacllarof

fosnoitamixorppAtsriF.3elbaT x (x0)(slavretnIdna x0 −−−−− x1 deyolpmEereWtahT)

1elbaTfosnoitauqEevloSoT

snoitauqE x0 )3–1smargorP( x0 − x1 )4margorP(

1 0 2,02 1 × 01 �7 1 × 01 �7 1,3 610.0 610.0,04 610.0 610.0,05 240.0 240.0,06 37.1 × 01 �7 1 × 01 �7 1.1, × 01 �6

7 94.2 × 01 �8 1 × 01 �7 1.1, × 01 �6

8 3 × 01 �4 1 × 01 �7 100.1, × 01 �4

9 3 × 01 �4 1 × 01 �7 100.1, × 01 �4

01 23.7 × 01 �4 23.7,0 × 01 �4

11 610.0 610.0,0

Page 4: Solution of Cubic Equations by Iteration Methods on a Pocket Calculator

Information • Textbooks • Media • Resources

www.JCE.DivCHED.org • Vol. 81 No. 5 May 2004 • Journal of Chemical Education 761

Notes

1. Third- and fourth-order equations can be solved by alge-braic methods, but higher-order equations can not.

2. Program 2 can be used to solve the following equations(9, 10):

H O+3[ ] H O+

3[ ]− × − × =− −2 3 61 75 10 3 885 10 0. .

H O+3[ ] H O+

3[ ]− − × 2

0 0280 9 60. . 110 04− =

H O+3[ ] H O+

3[ ]+ −2

0× 1 110 3− × 1 110 6− =

Literature Cited

1. Donato, H. J. Chem. Educ. 1999, 76, 632–634.2. Weldert, R. W. J. Chem. Educ. 1977, 54, 628.

3. Garst, J. F. J. Chem. Educ. 1977, 54, 114.4. Atkinson, L. V.; Harley, P. J.; Hudson, J. D. Numerical Meth-

ods with Fortran 77 A Practical Introduction, 1st ed.; Addison-Wesley: Reading, MA, 1989; pp 83–85.

5. Atkinson, K. E. Elementary Numerical Analysis, 2nd ed.; JohnWiley & Sons: New York, 1993; pp 62–64.

6. Skoog, D. A.; West, D. M.; Holler, F. J. Fundamentals of Ana-lytical Chemistry, 5th ed.; Saunders College Publishing:Pliladelphia, PA, 1988; p 834.

7. Skoog, D. A.; West, D. M.; Holler, F. J.; Crouch, S. R. Ana-lytical Chemistry An Introduction, 7th ed.; Saunders CollegePublishing: Pliladelphia, PA, 2000; p 240.

8. Donato, H. J. Chem. Educ. 2000, 77, 1120.9. Skoog, D. A.; West, D. M.; Holler, F. J. Fundamentals of Ana-

lytical Chemistry, 5th ed.; Saunders College Publishing: Phila-delphia, PA, 1988; pp 213–216.

10. Laitinen, H. A.; Harris, W. E. Chemical Analysis, 2nd ed.;McGraw-Hill: New York, 1975; p 35.