In-class exercises for Tuesday, Oct. 9

Due on Tuesday, Oct. 16.

Introduction

This exercise illustrates some basic Fortran programs. The ECA computers have the Gfortran compiler installed. It is still a work in progress (insofar as it still has several bugs), but should be stable enough for our purposes. An alternative to Gfortran is G95, a free Fortran 95 compiler developed by Andy Vaught (who is an ASU alumnus). Versions of both compilers are available for Windows, Mac OS X. Linux, and other platforms from their respective websites.

A Fortran source file, say prog.f90, can be compiled and linked into an executable program called prog with the command

gfortran prog.f90 -o prog
on the ECA computers or with
g95 prog.f90 -o prog
if you are using G95. (Unlike C compilers, which almost always can be invoked with cc, Fortran compiler names vary.)

On many, but not all, Fortran implementations, the suffix .f indicates source in fixed format and the suffixes .f90 and/or .f95 indicate free-form source. We will use free-form source; fixed-form is considered obsolescent. Both G95 and Gfortran support this convention.

Here is a sample module and another program to generate normally distributed deviates. They illustrate some aspects of Fortran syntax in more detail.

Exercises

You will email this assignment. You will create three programs, each of which is entirely contained in a single source file. Call your source files

stats.f90 (Exercise 1)
linlsq.f90 (Exercise 2)
quad.f90 (Exercise 3).
You will package these files into an archive (file of files) using the tar command as follows:
tar cf hw5.tar stats.f90 linlsq.f90 quad.f90
This command creates a file called hw5.tar.

Email your assignment with a subject line of the form

Your_name MAT 420 HW5
to mat420hw at gmail.com. Include hw5.tar as an attachment. This assignment is due on Tuesday, Oct. 16.

The usual homework policies apply. You may work with a partner and turn in a single assignment with both of your names on it.

Exercise 1. Here is a sample program that reads one number per line and prints the total. (Compile it and try it out.) Modify the program so that it prints

mean    standard_error
instead. (One estimator of the standard error is the square root of E(X2)-(E(X))2, where E(X) is the mean of X.) A single main program suffices for this exercise.

Exercise 2. Here is a skeleton of a program that reads in a number, n, followed by n lines of input of the form

x   y
Complete the subroutine linlsq so that it computes the slope and y-intercept of the linear least-squares regression line through the xy data. (Here are some formulas that you can use.) Compile and test your program (call the executable linlsq) on appropriate data.

Exercise 3. Write a program that, given the values a, b, and c on a single line read from the standard input, prints the roots of the polynomial

ax2 + bx + c = 0
using the quadratic formula. A single main program suffices.

Copyright (c) 2007 by Eric J. Kostelich. All rights reserved.