## Roots of General Equations

Ron Beshears

Greenwood, AR 72936

Here is a program capable of solving any general equation of the form y = f(x).

This program was introduced to me by Dr. Donald O. Pederson, Chairman of the Physics Department of the University of Arkansas at Fayetteville. I have adapted it in working form for use on the Pocket Computer (PC 1). It should run on any TRS-80 system with little or no modification.

Many functions are of such complexity that mere algebra is not sufficient to yield numerical solutions to them. This program employs a numerical method which yields roots with a reasonable degree of accuracy.

The program's method is to make an educated guess at the root's value, a value of x which makes y equal to zero and as such is symbolized on a graph of the function as a crossing of the x-axis. The program starts with a value of x supplied by the user and increments it by a value, also provided by the user, until the sign of y changes. At this value of x it is evident that an axis crossing has occurred, between the current value of x and the previous value. The program uses this zero crossing as its next guess and continues until the root has been determined to within the desired degree of accuracy.

I will now briefly describe the workings of the program.

The operator is reminded at lines 2 and 3 to enter the function on lines 10, 30, and 120 in terms of the specified variables (i.e., at line 10, Y = F(X), at line 30, F = F(B), and at line 120, G = F(C)). Once this has been done, the user can return to RUN mode and type RUN "DONE" and ENTER and execution moves on. The program then asks the user to supply an initial value of X, an increment value, and an acceptable error at line 7. At line 10 the function is evaluated for the initial X to see if a root has been immediately found. If not, X is incremented and the function is re-evaluated for this new value (B) at line 30. If a root has not been found, at line 50 a check is made to see if the axis has been crossed (if so, Y and F would have different signs and would therefore have a negative product). Line 80 reverses the direction of search if I = 1 (this occurs only on the first interaction) and if F is farther from the axis than was Y.

If the axis has not been crossed, X is incremented repeatedly until crossing occurs, and when this happens there must be a root in the interval between the current value of X and the previous value, assuming the function is continuous in that region. Once axis-crossing is established, the location of the root must be found to within plus or minus E.

The method used assumes a linear variation between X and B and estimates the value of the root (C) based on this. It uses this value to generate new values of X and B so that the corresponding values of Y and F are on opposite sides of the axis. Line 120 is derived from the formula for a point where the line between X and B crosses the axis.

Line 170 assigns C as the new X value if G is on the same side of the axis as Y; line 180 assigns C as the new value of B if G and Y are on opposite sides of the axis.

The program is most efficient if the user has some knowledge of the graph of the function being evaluated, so that he can supply a starting value of the X and an increment which requires the shortest possible execution time. However, the program can be used to search for roots. For example, if the user knows that the root of some function is somewhere between -50 and zero, he may enter an initial X of -50 and an increment of 1 or two. The user should be advised that this application may be relatively time-consuming (execution times in excess of three minutes are possible in extreme cases).

General usage notes:

- A sufficient value for acceptable error is 1 x 10-6.

- If f( x) contains any trigonometric operators, it is imperative that the computer is placed in the RAD mode before execution begins. Otherwise, the program will yield incorrect answers.

- As the program runs, it will pause the incremented values of x as it determines the value of y. Once the x values are no longer briefly displayed, the program has found the general location of the root and will shortly provide the answer.

- The program uses roughly 530 steps of program memory once the function has been entered at lines 10, 30, and 120.

1 PAUSE "ROOTS OF EQUATIONS'*

2 PAUSE "ENTER FUNCTION ON LINES"

4 "DONE" PRINT "ENTER INIT. X, INCREMENT"

5 PRINT "AND ACCEPTABLE ERROR."

6 CLEAR

7 INPUT "X(I) : ";X, "INCREMENT : ";N, "ACC. ERROR : ";E

40 IF F-0 GOTO 500

60 IF IOI GOTO 100

70 IF ABS(FX-ABS(Y) GOTO 90

100 X-B

: IF G-fi GOTO 500 160 IF Y*G<0 GOTO 180 170 X-C

: GOTO 110

500 BEEP 1

501 GOTO 6

510 END -Al

Color Computer