[Santa Clara University]
Department of Mathematics
and Computer Science

Machine Problem 5

Math 60 -- Winter, 2006
D. C. Smolarski, S.J.

               
30 points
DUE: NOON: Wednesday, March 1, 2006

This is a program in which you are to calculate the square root of 7 via "Newton's" method using the greatest accuracy possible on the departmental Unix system. The assignment has two parts to it.

Before you begin, once again, I suggest I assume you will create a new subdirectory for this program, as I suggested for the previous programs, and also create a new makefile.

First you need to experiment to determine what "KIND" of REAL variable will provide the greatest number of significant digits on our system. (As mentioned in class, a compiler will not automaticaly produce arbitrary precision accuracy. If the "KIND" value returns a negative number for your desired accuracy, you have requested numbers beyond the machine's and compiler's capabilities.) You should use the intrinsic function SELECTED_REAL_KIND to determine how large the precision may be. (See Chapter 14, Section 4 to refresh your memory.) What I suggest doing is creating a program with a loop and allowing the number of desired significant digits to grow from about 15 to about 50 while determining the "KIND." Whenever the "KIND" value turns negative, then you know you have gone beyond the capability of the compiler. You should run a test program and turn this test program in as part of the assignment!

After you have determined the "KIND" of REAL that gives the greatest accuracy in terms of the number of significant digits, you can proceed to the second part of the assignment which is to calculate the square root of 5 iteratively.

Using Newton's iterative root-finding method (cf. Thomas/Finney Calculus, 7th Edition [SCU Special Ed.], Cpt 2, sec 2.9, pg 155 ff).

	xnew = xold - f(xold)/f'(xold)
we can derive a formula for the square root of a number b (by using the equation f(x) = x2 - b as the equation whose root we seek).

The resulting formula is:

	x_new = (x_old + b/x_old)/2
You should merely use this last formula after substituting 7 for b. You do not have to do any further calculations!! (Remember to make 7 and 2 of the correct REAL "KIND" using the KIND subscript or else you will be loosing accuracy!)

As in any iterative method, one stops when successive values of x are "close enough," i.e. when abs(x_new - x_old) is less than some given tolerance.

Your program should use extended precision variables (i.e., the greatest "KIND" you can determine) to calculate the square root of 7 to as many digits as possible. It should use Newton's method and you should stop the iteration when x_new and x_old are within "epsilon" of each other, where "epsilon" has been determined by finding the largest "KIND" on this machine. For example, if the great number of digits were 20, use "epsilon" as 1.0E-19 (to allow for 19 fractional digits and 1 integral digit).

Start with x_old being equal to one. Keep count of the number of iterations needed to achieve the desired accuracy.

Also use the library square root function sqrt to calculate the root of 7 directly (but remember to input the value using the KIND subscript for a constant value). Your output should give (1) the square root derived from Newton's method, (2) the number of iterations required, and (3) the square root derived from the library function.

NOTE that all relevant variables should be declared to be the same KIND of REAL and you should indicate the KIND of constants in the appropriate way. If you ignore the KIND of the constants your answers will not have the precision you are trying to achieve.

The Newton's method section should be coded as a FUNCTION or SUBROUTINE, called from the main program (or some other segment).

HISTORICAL NOTE: Iterative methods like these are what enables computer languages and pocket calculators to "compute" square roots. They don't store all possible square root values in them, but calculate the values as required. Newton's method is one of the fastest available.

Remember to use good programming style (e.g., clear code, mnemonic variable names, comments, indentation, labeled output [including putting your name into the output]).

Turn in the source file (i.e., the program code file) and the output file. REMEMBER also to turn in code for the test program which determines the largest REAL "KIND" you can use.

As usual, your completed program may be turned in to my office, O'Connor 32, or to my mailbox in O'Connor 31. Please staple the pages yourself and do not turn in disconnected pages!


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
Last changed: 16 January 2006.