CSCI 297 Assignment E

Square Roots and Complex Numbers

This program is concerned with square roots.

FORTRAN has a library function SQRT:

   A = SQRT(B)

where the argument B is most commonly REAL but could be COMPLEX, and the return type of the function is the same type as that of B.

How else could we calculate square roots? A simple method sometimes attributed to Newton is the following:

     To find the square root of X:

       Start with Z1 = X and Z2 = 1.
       Precision = 0.001.
       While the absolute difference of Z1 and Z2 is larger than Precision
         Replace Z1 with (Z1 + Z2) / 2
         Replace Z2 with X / Z1
       End While.

Of course we could have a precision finer than 0.001.

Let's look at how many iterations this needs before it stops.

We have an input file, dataER.txt, containing some nonnegative REAL numbers. We will find the square root of each using the library function SQRT and again using the above algorithm.

When you implement the algorithm, put in a counter (starting at 0) and each time the loop executes, print out the counter value and the values of Z1 and Z2 (appropriately labeled). Stop the loop if the counter reaches 15. Also, use a precision value of 0.0000001 (or smaller).

Does the algorithm work for COMPLEX numbers? In most cases, it will, but if X is (-1.0, 0.0), it fails immediately (divide by 0), and if X is any negative REAL number, it does not converge at all. (Give it a try. It's interesting.)

We have a second input file, dataEC.txt, containing some COMPLEX numbers. We will go through the same steps for this file.

FORTRAN deals with COMPLEX numbers in the format (u, v), where u and v are REAL numbers, both for default input and for default output. For instance:

  (  1.92154408    ,  1.30101037    ) 

If you want, you can make this look better by working with REAL(Z) and AIMAG(Z), the real and complex parts of a COMPLEX number Z, and formatting them for yourself.

A much more common way to find the square root of a COMPLEX number is to convert it to polar form. That is, a COMPLEX number Z = (X, Y) can also be represented by an angle A and a radius R, where:

     R = SQRT(X*X + Y*Y)   (which is CABS(Z))
     A = ATAN(Y / X)

     X = R * COS(A)
     Y = R * SIN(A)

Then the square root of Z is repesented by an angle = A / 2 and a radius = SQRT(R).

For each COMPLEX number in the second file, we also need to convert it to polar form, find the square root and print all this out.

In your output, make it clear which value was calculated by which metod.


What should be turned in?

As in the preceding assignments, you need to compile and link your program, correcting any errors. Name your program "prog7.f90".