CSCI 297 Assignment B

Selective and Repetitive Execution: Best-Fit Line

This assignment involves using FORTRAN to determine what is called the "best-fit line". This is an idea from statistics: we have a collection of points in the X-Y plane, and we want to know what line Y = M * X + B comes closest to going through all of the points. Usually no line is perfect, but we want the best one.

The algorithm for finding the best-fit line can be found here.

You may find that a good way to start on this is to read the algorithm and figure out what parts repeat, that is, what loops are needed. Decide what variables you need.

To calculate the best-fit line for one set of points requires a loop. We will have several such sets of points, so we wil need another loop as well.


What does the data look like?

Each line of data contains two floating-point numbers, the X and Y coordinates of a point.

Copy the data file into your directory. You can find it here.

The lines are actually organized into groups. In each group, there are lines representing points, and then there is a line containing -777.0 (twice) to mark the end of the group. This marker line does not represent a point. At the end of the whole file is a line containing -999.0 (twice). It is a delimiter which is there just to tell us when to stop.


Steps:

  1. Write the program and save it as "progB.f90".

  2. The algorithm reads two input values X and Y from each line until it reads two input values of -999.0. For the points indicated, it computes the slope M and intercept B of the best-fit line (Y = M * X + B) and prints the results.

  3. Modify the program (as in Assignment 3) so it will process data for more than one best-fit line by reading more than one sequence of input values. The end of each sequence of input values will be indicated by two values of -777.0. The end of the entire input will be recognized by the -999.0 values. Thus we will have a loop inside a loop. Each loop begins with "DO WHILE" and ends with "END DO".

  4. We are going to use input redirection again, so you may omit the lines which ask the user to enter the data.

  5. Your program should use a minimum number of variables to do the job. It should be efficient and well-structured. Include the line IMPLICIT NONE in the specification part of your program.

  6. It is possible that when we start this assignment, we may not yet have covered loops. You can find examples of these in Chapter 4. We also have a web page on loops.

  7. To make the output easier to read, skip two blank lines using
             PRINT *
             PRINT *
    between groups of input values.

  8. Compile and link the program as in the previous assignments.

  9. After you obtain a copy of the input file, run your program with input and output redirection as in Assignment 3:
              prog4 < data4.txt > output4.txt