COMPLEX MATHS (ZX-81)
from Sinclair User 19 (November 1983), page 106

This is a program for the 16K ZX-81 which will enable the user to perform
some complex mathematical calculations, aided by a graphical display. It
would be able to find at least one root for a polynomial equation of any
degree and at the same time plot the graph of the equation, find the definite
or indefinite integral, or differentiate the equation with respect to X.

The program is almost entirely Basic with only a few machine code
Subroutines. The reason is that it is very difficult to handle floating point
arithmetic in machine code and it would be a very limited program if it were
only to use the Z-8O integer arithmetic facilities.

I have subdivided the program into several routines; I shall explain each
individually so that the program can be understood easily. Here is a list of
these routines:

Line 1             - Various machine code subroutines, stored in a REM
statement.
Lines 4 to 6       - The re-start routine.
Lines 10 to 130    - The initialisation process.
Lines 300 to 360   - The select routine.
Lines 500 to 996   - Some important subroutines.
Lines 1000 to 1180 - The graph plotting routine.
Lines 2000 to 2310 - The equation-solving routine.
Lines 3000 to 3090 - The differentiation routine.
Lines 4000 to 4340 - The integration routine.
Lines 5000 to 5120 - The Sigma evaluation routine.

All the machine code subroutines do is draw things on the screen. Do not
worry if you do not understand them; they are not an important part of the
program and their functions could easily be replaced by longer and less
efficient Basic subroutines.

Run the program using RUN 10. After the program has finished the user will
receive a report of 1/4. If he keys CONT the program will re-start with the
same data; if he keys RUN the program will re-start and he will be prompted
for new data.

Now for the initialisation process. The equation will store the coefficients
of X^n in the array P(n,1) and the corresponding exponents in the array
P(n,2). When entered into the computer, an equation must be of the form Y =
polynomial in X. When the equation is being solved for an unknown, x, Y must
be equal to nought and during Sigma evaluation Y should be ignored altogether.

The subroutine at line 500 prints-out the contents of the array P by looping
through it. Note, however, that a colon in the listing means to the power of
and that the display file is poked to over-write the last plus sign in line
535.

Some other subroutines we will need are one subroutine which differentiates
the equation held in the array P and places the result in another array O; a
subroutine to plot a single point when the screen is divided into four
quadrants; a subroutine which integrates the equation held in the array P and
places the result in another array I; a subroutine which, given a value for
X, will find the value of the polynomial expression held in the array P. The
positions are lines 500, 600, 700, 800, and 900 respectively.

The last one may require some more explaining. If you try the sequence of
direct commands LET X= - 5; PRINT X**2 you will soon discover the inability
of the ZX-81 to calculate negative numbers to the powers of things. In the
above subroutine line 930 handles positive values of X, line 970 handles
negative values of X and even exponents, and line 990 handles negative values
of X and odd or fractional exponents.

The routine at line 1000 plots the graph of an equation in the array T.


You can then test the program. If, for example, you wanted a graph of the
equation y=x^2+x-4 you would do the following:

1) Type RUN 10
2) Enter 3 for the number of terms
3) Enter the numbers: 1;2; 1;1; -4;0 for the coefficients and powers of x -
note that nought is used for the power of -4, because it can be written as -4x^0.
4) Enter one to select the graph plotting routine.
5) Press newline when the computer tells you the scale ratio.

That sounds like a complicated process but it is not once you are used to it.

The next part of the program solves polynomial equations, provided they
contain only one unknown and that one side is equal to nought. One way of
doing it would be to equip the computer with many formulae for each type or
equation. One example would be the well-known formula for quadratic equations:

   [-b+SQR(b^2-4ac)]/2a=x   and   [-b-SQR(b^2-4ac)]/2a=x

where a, b, and c are the coefficients of x^2, x^1 and x^0 respectively.

That method would be memory-consuming and rather complicated - try
working-out a formula for an equation with the highest term in x^10 and you
will appreciate the point. Instead we shall use the Newton-Raphson method,
based on the equation:

   x(n+1)=x(n)-[f(x(n))/f(x(n))]

where f denotes the polynomial as a function, f denotes the derivative of the
function, and the higher the subscript n the more accurate the root, since
the method is based on improving an approximate root until a desired degree
of accuracy is obtained.

We will be calculating the root to eight significant figures. The only
disadvantage of the method is that it takes a very long lime.

You can test the routine now. If you wanted to solve the equation
x^2-10x+25=9, you would first make the right- hand side equal to nought by
subtracting nine from both sides to give x^2-10x+16=0; then you would enter
the numbers in the normal way and use 2 to select the equation-solving
routine; then, assuming you did not have an approximation, you would answer
"NO" to the computer question and it would give 2 as a root for x. If instead
you entered 10 as an approximation, you would obtain the alternative value 8.

Finally, I shall leave you with the following problems:

a) Solve the equation (1/X)^2+x=3+(1/729)

- you should find at least two roots.

b) A body is moving in a straight line and t seconds after passing a point A
in the line its velocity is (3t^2 + 4) m/s. Find the distance moved by the
body while its velocity increases from 31 to 112 m/s. Also calculate the
acceleration of the body when it has moved 5m from A.

c) It is known that an enemy ship will be at a point P at precisely 15.08 and
45 seconds. Your submarine is exactly 21.97km from that point. The
acceleration of your torpedo has a rate of change of 6/100. At what time
exactly should you fire your first torpedo so that it arrives at P at the
same time as the enemy ship?

d) Calculate the sum of the first 100 multiples of 4 squared.