PROGRAM: Non-linear Equations.
BASIC DEMO FILENAME = $.NLinEq - Side 0
ASCII TEXT FILENAME = T.NLinEq - Side 0
The program consists of two main modules: 'graph plotter' that depicts
any function y=f(x) on the screen and 'equation solver' which
calculates roots of an equation f(x)=0 using one of the three
iteration methods. The former module has been included to obtain some
idea of a range in which a solution is likely to appear (if a solution
exists at all). When running the program you are asked to enter the
formula for f(x). It must be given in terms of 'X' (or 'x') and must
be a valid BBC BASIC expression, except that lower-case letters and
capitals can be used interchangeably. Although an attempt has been
made to check the formula as soon as it is entered, some mistakes are
detected a little bit later and the program returns you to re-enter
the formula. Therefore, do not forget to use '*' whenever
multiplication occurs and make sure that the number of opening
brackets is equal to the number of closing brackets.
When you have defined the function you will be asked to specify the
range of X and Y to appear on the plot. If you do not choose these
suitably you may lose most or all of the points by plotting them way
off screen somewhere. It is advisable, however, to use rather broad
X and Y ranges initially, say [-5,+5] and [-10,+10], respectively.
You can always change the range later and re-plot the graph with the
refined range.
The plotting routine is programmed so that it works even though the
function is not continuous at some points or it does not exist at all
within some intervals. However, as the X-axis must always appear on
the screen, so the user is forced to enter a negative number as the Y
minimal value and a positive number as the Y maximum value. While
plotting is in progress, X-values, at which the function value is
zero, are displayed in the text window at the bottom of the screen.
These values should be treated as rough estimates and not as a final
solution. In some circumstances (in particular when the equation
y=f(x) has two or more coincident roots) those X-values do not appear
in the text window even though a solution of the equation exists
within the given X-range. If the curve does not cross (or does not
touch) the X-axis you will have to try a different X-range, unless you
have decided there is no solution at all, in which case either exit or
change the formula.
When the graph is drawn you can use the second module, 'equation
solver'. Again, you will be asked to answer a number of questions and
the first of them deals with selection of the method of calculation.
There are three method available: a tangent method of Newton as
modified by Raphson, a bisection method, and the so called 'regula
falsi' in its most efficient version ('variant Illinois'). None of
them is universal and each fails under some circumstances. However,
if one method will fail you can use other method and it may work
perfectly. Whatever method you choose you will be asked for the
accuracy required. It is reasonable to start with low accuracy value,
for example 1E-4 (ie. 0.0001) and then increase the accuracy by
decreasing the 'epsylon' value, even to its limit (ie. 1E-9).
With the Newton-Raphson method you have to enter an initial guess
value. This value should be close to the expected root value. With
two other methods you have to enter an initial range of X values
straddling the expected root value so that the function must change
sign within this range (ie. if the function value is positive at the
lower (ie. left) bound, it must be negative at the upper (ie. right)
bound, and vice versa).
If the method will work for a given case, a result is displayed on the
screen and you are offered to a number of options. So you can alter
single parameters, change a method, return to the graph plotting
section or re-run the program. As each method allows to find only one
solution at a time so if you want to find another root just press 'P'
key and change initial values. The program is highly subject to user
errors. However, it is believed that all possible errors are trapped.
The program simply displays various messages and returns you to the
point, where you can change everything and try again.
At last a short comment on the output of results.
Using the program you will discover very soon that solving the
equation x^2-4=0 you may obtain as a solution '2.00000247' or
'1.99999036' instead of '2'. There is no error at all as long the
accuracy value declared by you is 1E-5 (ie. 0.00001) or greater.
Unfortunately, similar 'strange' results often appear on the screen
when the highest permissible accuracy of 1E-9 has been declared and in
such cases inaccuracy is due to the round-off errors generated by the
computer during floating point calculations.
As you probably know, when decimal numbers are entered into the
computer they are converted into binary floating point notation and
stored as the 32-bit 'mantissa' and the 8-bit 'exponent' (40 bits or 5
bytes in total). This allows to store the first 9 digit of a decimal
number without error. However, if two numbers, each with 32 binary
digits mantissa, are multiplied together, their true product will have
64 digits but only 32 of them can be stored, thus causing rounding
error. With a large number of multiplications or other floating point
operations some of these rounding errors compensate but the others can
accumulate, contaminating the final result.
Miroslaw Bobrowski L1J.