Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

EXAMPLE 6.22 Solution of an Algebraic Equation For the more mathematically inclined reader, this example illustrates how computers can be used to solve algebraic equations,

EXAMPLE 6.22 Solution of an Algebraic Equation For the more mathematically inclined reader, this example illustrates how computers can be used to solve algebraic equations, including those that cannot be solved by more direct methods. Consider, for example, the equation x5 + 3x2 - 10 = 0. This equation cannot be rearranged to yield an exact solution for x. However, we can determine the solution by a repeated trial-and-error procedure (called an iterative procedure) that successively refines an initial guess. We begin by rearranging the equation into the form x = (10 -3 x 2 p Our procedure will then be to guess a value for x, substitute this value into the right-hand side of the rearranged equation, and thus calculate a new value for x. If this new value is equal (or very nearly equal) to the old value, then we will have obtained a solution to the equation. Otherwise, this new value will be substituted into the right-hand side and still another value obtained for x, and so on. This procedure will continue until either the successive values of x have become sufficiently close (i.e., until the computation has converged), or until a specified number of iterations has been exceeded. This last condition prevents the computation from continuing indefinitely in the event that the computed results do not converge. To see how the method works, suppose we choose an initial value of x = 1.0. Substituting this value into the righthand side of the equation, we obtain x = [10- 3( 1. 0)2]0*2= 1.47577 We then substitute this new value of x into the equation, resulting in x = [10 - 3( 1.47577)2]0.2= 1.28225 Continuing this procedure, we obtain x = [10 - 3( 1.213225)~IO=.~ 1 .38344 x = [10 - 3(1.38344)2]0*2= 1.33613 and so on. Notice that the successive values of x appear to be converging to some final answer. CHAP. 61 CONTROL STATEMENTS 143 The success of the method depends on the value chosen for the initial guess. If this value is too large in magnitude, then the quantity in brackets will be negative, and a negative value cannot be raised to a fractional power. Therefore we should test for a negative value of 10 - 3x2 whenever we substitute a new value of x into the right-hand side. In order to write a program outline, let us define the following symbols. count = an iteration counter (count will increase by 1 at each successive iteration) guess = the value of x substituted into the right-hand side of the equation root = the newly calculated value ofx t e s t = the quantity (10 - 3x2) error = the absolute difference between root and guess f l a g = an integer variable that signifies whether or not to continue the iteration We will continue the computation until one of the following conditions is satisfied. 1. The value of error becomes less than 0.00001, in which case we have obtained a satisfactory solution. 2. Fifty iterations have been completed (i.e., count = 50). 3. The variable t e s t takes on a negative value, in which case the computation cannot be continued. Let us monitor the progress of the computation by writing out each successive value of root. We can now write the following program outline. 1. For convenience, define the symbolic constants TRUE and FALSE. 2. Declare all variables, and initialize the integer variables f l a g and count (assign TRUE to f l a g and 0 to count). 3. Read in a value for the initial guess. 4. Carry out the following looping procedure, while f l a g remains TRUE. (a) Increase the value of count by 1. (b) Assign FALSE to f l a g if the new value of count equals 50. This will signify the last pass through the loop. (c) Examine the value of test. If its value is positive, proceed as follows. (i) Calculate a new value for root; then write out the current value for count, followed by the current value for root. (ii) Evaluate error, which is the absolute value of the difference between root and guess. If this value is greater than 0.00001, assign the current value of root to guess and proceed with another iteration. Otherwise write out the current values of root and count, and set f l a g to FALSE. The current value of root will be considered to be the desired solution. (d) If the current value of t e s t is not positive, then the computation cannot proceed. Hence, write an appropriate error message (e.g., Numbers out of range) and set f l a g to FALSE. 5. Upon completion of step 4, write an appropriate error message (e.g., Convergence not obtained) if count has a value of 50 and the value of error is greater than 0.00001. Now let us express the program outline in the form of pseudocode, in order to simplifi the transition from a general outline to a working C program. #include f i l e s #define symbolic constants main( ) t /* variable declarations and i n i t i a l i z a t i o n */ /* read input parameters */ 144 CONTROL STATEMENTS [CHAP. 6 while ( f l a g ) { / * increment count *I / * flag becomes FALSE i f count = 50 */ /* evaluate test *I i f ( t e s t > 0) { / * evaluate root *I /* display count and loop */ / * evaluate error */ i f ( e r r o r > 0.00001) guess = root; else { /* flag becomes FALSE */ /* display the f i n a l answer (root and count) */ else { / * flag becomes FALSE */ / * numbers out of range - w r i t e error message */ } / * end while */ i f ((count == 50) && ( e r r o r > 0.00001)) /* convergence not obtained - w r i t e e r r o r message */ Here is the complete C program. /* determine the roots of an algebraic equation using an i t e r a t i v e procedure */ #include #include #define TRUE 1 #define FALSE 0 main( ) i n t f l a g = TRUE, count = 0; f l o a t guess, root, t e s t , error; / * read input parameters */ p r i n t f ( I n i t i a l guess: ) ; U scanf ( "%f, &guess) ; U while ( f l a g ) { /* begin the main loop * / ++count ; i f (count == 50) f l a g = FALSE; t e s t = 10. - 3. * guess * guess; i f ( t e s t > 0) { /* another i t e r a t i o n */ root = pow(test, 0.2); p r i n t f ( " \ n I t e r a t i o n number: %2d", count); p r i n t f ( ' x= %7.5fU, r o o t ) ; e r r o r = fabs(root - guess); CHAP. 61 CONTROL STATEMENTS 145 i f ( e r r o r > 0.00001) guess = root; / * repeat the c a l c u l a t i o n *I else { / * display t h e f i n a l answer *I f l a g = FALSE; printf(" Root= %7.5fn, r o o t ) ; p r i n t f ( ' No. o f i t e r a t i o n s = %2d", count) ; 1 1 else { I* error message */ f l a g = FALSE; p r i n t f (" Numbers out of range - t r y another i n i t i a l guess"); 1 1 i f ((count == 50) && ( e r r o r > 0.00001)) / * another e r r o r message */ printf(" Convergence not obtained a f t e r 50 i t e r a t i o n s " ) ; Notice that the program contains a while statement and several i f - else statements. A f o r statement could easily have been used instead of the while statement. Also, notice the nested i f - else statements near the middle of the program. The output that is generated for an initial guess of x = 1 is shown below, with the user's responses underlined. Notice that the computation has converged to the solution x = 1 .35195 after 16 iterations. The printed output shows the successive values of x becoming closer and closer, leading to the final solution. I n i t i a l guess: 1 I t e r a t i o n number: 1 X= I.47577 I t e r a t i o n number: 2 X= 1.28225 I t e r a t i o n number : 3 X= 1 ,38344 I t e r a t i o n number : 4 X= 1.33613 I t e r a t i o n number: 5 X= 1.35951 I t e r a t ion number : 6 X= 1 .34826 I t e r a t i o n number: 7 X= 1.35375 I t e r a t i o n number: 8 X= 1.35109 I t e r a t i o n number : 9 X= 1.35238 I t e r a t i o n number: 10 X= 1.35175 I t e r a t i o n number: 11 X= I.35206 I t e r a t i o n number : 12 X= 1.35191 I t e r a t i o n number: 13 X= 1.35198 I t e r a t i o n number: 14 X= 1.35196 I t e r a t i o n number: 15 X= 1 .35196 I t e r a t i o n number: 16 X= 1.35195 Root= 1.35195 No. of i t e r a t i o n s = 16 Now suppose that a value of x = 10 had been selected as an initial guess. This value generates a negative number for t e s t in the first iteration. Therefore the output would appear as follows. I n i t i a l guess: 10 Numbers out of range - t r y another i n i t i a l guess It is interesting to see what happens when the initial guess is once again chosen as x = 1, but the maximum number of iterations is changed from 50 to 10. You are encouraged to try this and observe the result. you should understand that there are many other iterative methods for solving algebraic equations. Most converge faster than the method described above (i.e., they require fewer iterations to obtain a solution), though the mathematics is more complicated.

The equation x 5 + 3 2 - 1 0 = 0 which was presented in Example 6.22, can be rearranged into the form x = ,,/(10-x5)/3 Rewrite the program presented in Example 6.22 to make use of the above form of the equation. Run the program and compare the calculated results with those presented in Example 6.22. Why are the results different? (Do computers always generate correct answers?)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

7. What decisions would you make as the city manager?

Answered: 1 week ago

Question

8. How would you explain your decisions to the city council?

Answered: 1 week ago