20.04.2015 Views

The Resource Magazine For Apple, Atari, and Commodore ...

The Resource Magazine For Apple, Atari, and Commodore ...

The Resource Magazine For Apple, Atari, and Commodore ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

34 COMPUTE! September/October. 198O. Issue 6<br />

may drain from your rosy cheeks when you realize<br />

that this equation has two answers, <strong>and</strong> the method<br />

of successive substitutions will not work to find the<br />

other<br />

answer.<br />

How do we know that the equation has two<br />

answers? If you write a short program to print ihc<br />

value of ex -4x for some values of x, you obtain the<br />

results in Table 4. Note that the function ex - 4x is<br />

positive at x = 0.2 while it is negative at x = 0.4.<br />

That means that somewhere between 0.2 <strong>and</strong> 0.4 the<br />

function ex - 4x went through zero, <strong>and</strong> at that point<br />

(he equation was satisfied. That is the answer we<br />

found above, namely x = 0.3574029. Note also that<br />

at x =2 the function is negative, while at x = 2.2 the<br />

function is positive, indicating that another answer is<br />

to be found between 2.0 <strong>and</strong> 2.2. Try to find this<br />

answer with successive substitutions.<br />

Table 4. <strong>The</strong> value<br />

of cx -4x versus x.<br />

X<br />

0.0<br />

0.2<br />

0.4<br />

0.6<br />

0.8<br />

1.0<br />

1.2<br />

1.4<br />

1.6<br />

1.8<br />

2.0<br />

2.2<br />

2.4<br />

EXP(X) - 4'X<br />

1.00<br />

0.42<br />

-(1.11<br />

-0.58<br />

-0.97<br />

-1.28<br />

-1.4")<br />

-1.54<br />

-1.44<br />

-1.15<br />

-0.61<br />

0.22<br />

1.42<br />

<strong>The</strong> Method Of Interval Halving<br />

<strong>The</strong> failure of the method of successive substitutions<br />

to converge to an answer in certain situations is<br />

reason enough to look for another method. <strong>The</strong><br />

method of interval halving is particularly attractive<br />

because you are (almost) guaranteed that it will find<br />

an answer if you know that the answer lies between<br />

two numbers. Refer again to our problem of finding<br />

the solution to the equation ex -4x = 0 <strong>and</strong> Table 4.<br />

Table 4 indicates that one answer is between 0.2 <strong>and</strong><br />

0.4 <strong>and</strong> another answer is between 2.0 <strong>and</strong> 2.2,<br />

because the function cx -4x changes sign on these internals.<br />

<strong>The</strong> first step in the interval halving method is to put<br />

the equation to be solved in the form<br />

f(x) = 0 (7)<br />

<strong>and</strong> to find two values of x (call (hem xl <strong>and</strong> xr, L<br />

<strong>and</strong> R for left <strong>and</strong> right) such that (he function f(x) is<br />

positive for one of these values of x <strong>and</strong> it is negative<br />

for the other.<br />

Suppose we deal with our example, ex - 4x = 0.<br />

It already is in the form f(x) = 0. Furthermore, let<br />

us concentrate for the moment on the solution that<br />

we could not find with the method of successive<br />

substitutions. That solution we know to be between x<br />

= 2.0 <strong>and</strong> x = 2.2. Thus, xt = 2.0 <strong>and</strong> xR = 2.2<br />

<strong>The</strong> second step is to try a value of x half-way<br />

between x^, <strong>and</strong> x]^. This is where the name "inter<br />

val halving" originates. This value of x, call it xjyj<br />

(M for middle) is given by the simple expression,<br />

*M = (*L + *R)/2 (8)<br />

Now comes the tricky part. Suppose f"| is the value<br />

of the function when xj^ is substituted (plugged in)<br />

into f(x), <strong>and</strong> suppose ij^j is the value of (he function<br />

when XjYj is plugged into the function f(x). If the<br />

product (1"l . f\j) is positive, then xjyj lies to the led<br />

of the answer just like xj^. We know this because the<br />

product can only be positive if jFl <strong>and</strong> f j^ have<br />

the same sign. In this case, we replace xj with a new<br />

value, namely x^. On the other h<strong>and</strong>, if the product<br />

Cl ■ '\l) 's negative, (hen f^ <strong>and</strong> l\/[ have opposite<br />

signs, <strong>and</strong> xjyj is to the right of the answer. In that<br />

case we replace xr with xjyf, giving a new value for<br />

xr. In either case, we have bracketed the answer in<br />

an interval half as wide as the interval we started<br />

with.<br />

Repeating this process allows us to bracket the<br />

answer in as small an interval as we wish, with the<br />

answer as accurate as we wish. Each time we<br />

calculate a new xj^ we must test the sign of the pro<br />

duct (Fl . fjyj) to see if xjyj is to the right or left of<br />

the answer. If the answer was originally known to be<br />

in an interval of width w, where w is the difference<br />

between our first xr <strong>and</strong> xj^, then after n iterations<br />

or repetitions of the interval halving process, the er<br />

ror in the answer is w/2n. Thus, after 10 iterations<br />

the error in the answer is about 1/1000 of the<br />

original uncertainty in the answer.<br />

A program to solve the equation ex - 4x =0<br />

with the method of interval halving is given in<br />

Listing 4. With little modification, this program can<br />

be used lor other equations as well. <strong>The</strong> variables in<br />

(he program in Listing 4 are closely related to our<br />

previous discussion, so no further explanation will be<br />

given. I expect that most people can underst<strong>and</strong><br />

BASIC" about as well as algebra. Table* 5 shows the<br />

answers we obtain for both of the solutions to this<br />

equation. A total of 20 iterations are done in the pro<br />

gram, giving an error of less than 0.00000096 if the<br />

distance between the original xj^ <strong>and</strong> xj^ were less<br />

than one. <strong>The</strong> roundoff error in many machines will<br />

exceed<br />

this.<br />

Listing 4. Interval halving used to solve ex - 4x. = 0.<br />

10 INPUT XL, XR<br />

20 FOR 1 = 1 TO 20<br />

30 XM = (XI. + XR)/2<br />

40 FL = F.XP(XL) -4'XL<br />

50 FM = KXF(XM) = 4'X.\I<br />

60 IF FL*FM XM<br />

100 PRINT XM<br />

110 NEXT 1<br />

Are there any practical applications of these techni<br />

ques for ordinary citizens? <strong>The</strong> answer is yes. Sup-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!