FORTRAN

MSDOS  -  Last period we discussed some of the commands associated
 with the DOS (disk operating system) environment.  If you forget 
form of the commands, simply type help while in DOS.  This will 
bring up a list of the different DOS commands.

FORTRAN
We will be using FORTRAN 77 in this course.  FORTRAN 77 is the most
 widely used version of the language which is currently used.  
FORTRAN 90 is starting to be tested by many programmers.  Once you
 have an understanding of a single programming language, using other
 versions or languages will be much easier.  

The Fortran programming language has some very specific structural
 requirements of our programs, as well as specific syntax for most
 of the commands that we will be using in creating a program.  We 
discussed the MS Text editor last class period.  We are going to be
 using the text editor to create our Fortran programs in this course.

With the use of the text editor we create a source code for our 
program.  The source code will typically have an extension for. 
 For example program1.for.  The source code is essentially a set 
of instructions in the fortran programming language that we wish 
the computer to execute.  Although it may not appear to be English, 
source code is essentially a group of English expressions or commands
 that we understand.  The problem however is that the computer only
 understands machine language.  The machine language that the computer
 understands is a binary language composed of 0’s and 1’s.  All the
 numbers and letters that we want the computer to understand are 
essentially converted into a binary expression that the computer can
 understand.  

In order to make the computer understand the commands within our 
source code we will be employing a compiler which will translate the 
(English) source code into machine code.  The actual steps in the 
compiling process involve first compiling the source code to create
 object files, then linking the object files together to form a single
 executable file.  The executable file is a single file which will 
execute the desired computer instructions when invoked.  The executable
 file has an exe extensio.  For example program1.exe.

We will be using the DOS based compiler, MS Fortran version 5.10.  
This compiler will compile and link our programs with a single command
 (FL).  There are numerous options that can be specified for the 
compiling and linking process.  As you become more experienced you may 
wish to read up on these options in the Microsoft Fortran Reference 
Guide.  

The following expression should adequately compile and link the 
programs that we will be dealing with in this course:

FL /Fsfilename2 filename1.for /Fefilename3 

The above command will compile and link the source code filename1.for,
 make an output list named filename2.lst, and an executable file named
 filename3.exe.

For example:
		FL todd.for /Fsprogram1 /Feprogram2
will compile the source code in the file todd.for, create an output 
list named program1.lst, and create an executable file named 
program2.exe.  We could have also simply used:

		FL todd.for /Fs /FE
which would have produced an output list todd.lst and an executable 
named todd.exe.  

The output list is extremely valuable in order to find errors (debug) 
our program.  We can go through our output list if the program doesn’t
 compile and find errors with our syntax.  

ARITHMETIC COMPUTATIONS:  Chapter 2 of Fortran text (pg. 9).

Most programs will require the use of variables to make the program 
applicable to solve general problems.  The variables that are to be 
used may consist of either letter or numeric characters.  Variables 
take the following form:
		
		·1 to 6 characters in length,
		·the first character of a name must be alphabetic,
		·Fortran does not distinguish between upper and lower 
			case letters.
Examples from your text:
		DISTANCE   	-  no good, too long
		TIME		- valid
		PI			- valid
		$			- no good, illegal character($)
		TAX-RT		- no good, illegal character (-)
		B3			- valid
		2X			-no good, first character 
numeric

 DATA TYPES:

Integers				-	32, -7
Real Values			-	15.45, 0.004
Double-Precision Values	-	3.145936536, 1.000000006
Complex Values			-	1-2i, 5i, 
Character Values		-	‘velocity’, ‘Report 3’
Logical Values			-	.True., .False.

We will discuss the last four types of data in later lectures, however,
 it is extremely important to gain an understanding of variable 
requirements which apply to integers are real values to understand 
basic programming.  
An integer is essentially a "whole" number.  Integers contain no 
fractional portion and no decimal point.  Real numbers on the other
 hand contain a decimal point and may have digits past the decimal 
point.  Real values are often referred to as floating point values.  

When we define a variable we ask the computer to reserve a spot in 
memory for our particular variable.  The type of variable that we 
specify can be specified by two methods:a)  implicit typing- determined
 by the first letter of the variable that we select.  By default, 
Fortran sets variable names beginning with I, J, K, L, M, or N as 
integers.  Therefore, any variable such as ‘IX’ or N2 are reserved 
as integers.  If we tried to set N3=2.45, the Fortran program will 
save N3 =2.  The decimal portion of the number will be truncated. 
 Variable names beginning with the other letters A-H or O-Z will be 
stored as real values.
b)  explicit typing -   Fortran statements are used to specify a 
particular variable type:
INTEGER area, width, height     (not case sensitive)

REAL I, length, mean                  (not case sensitive)

These statements tell the compiler to assign memory locations, and to 
specify the types of values which will be stored in these locations.  

STATEMENT FORMAT

Page 22 of Etter
Each statement that we put in a fortran program is entered on a single
 line.  As we talk about the format of a line we will discuss a 
particular position on the line relative to column numbers.  The
 makeup of each line must take the following form:

1)  Columns 1 -5 Statement number or label-  not every line will have
 a statement number.  In some cases however we will want to number  a
 particular line so that we can refer to the particular line in the
 program.

2)  Column 6 is reserved for a continuation character.  If a particular
 statement is too long, it will have to be continued on the next line.
  By placing a character (such as +) in the sixth column we tell the 
compiler that the statement on this particular line is a continuation
 of the previous line.

3)  Columns 7-72 Fortran statement.  

The only exception to the above layout is that if an asterisk *, or a
 letter C are placed in column 1, the particular line is a comment 
statement.  It is highly encouraged to use liberal comment statements
 in order to organize the material in your program.  
We can include blank lines anywhere in the program to separate portions
 of the program.  

Also, in referring to the fortran statement from columns 7-73 blank 
spaces have no effect on the fortran statement (with the exception of
 formated output statements).  This actually can be very valuable when
 trying to organize the program and offset certain portions of the
 program.  

Graphing in Excel

Graphing in Excel is extremely simple with the use of the "Chart
 Wizard".  We often want to graph some sort of data points.  The
 following steps can be carried out in Excel to graph data:

1. 	Enter the data into Excel that is to be graphed.  Place the
 independent (x data) variable in the leftmost column.  This is not 
absolutely necessary, however it does make the graphing easier.  Once
 you become familiar with graphing techniques you can place the data 
in any fashion that you wish.
2. 	Highlight the data to be graphed.  If the data is not side by
 side, first highlight the x-data then hold down the shift key while
 the y-data is being highlighted.
3. 	Select Insert, Chart from the menu bar or select "Chart Wizard"
 from the button bar.  In older editions of Excel, Chart Wizard may not
 let you insert the chart as a new sheet, but instead will only allow
 you to embed the chart in the worksheet.  In these older editions of
 Excel, selecting from the Insert menu gives you the option of 
inserting the chart on the current sheet or inserting the chart as a
 new sheet.  The Excel Chart Wizard in MS Office 97 allows you to 
either embed the chart in the current sheet or insert the chart as a
 new sheet. 
4. 	Excel will walk you through the creation of the chart.  Select
 the type of chart you want ( usually XY Scatter).  Insert titles, 
axis labels, etc. 
5. 	Finally click finish.

You may edit the chart after you have made it and insert new data, 
reformat axes, etc.  Double clicking on a series within the chart 
allows you to change/add a line between the points, change the name 
of the data series in the legend, etc.  
In many situations you will want to add a new series of data to a 
chart.  One way to add a new data series to a chart is to select Add
 Data from under the Chart Menu.  A pop-up panel will appear which 
will prompt you for the data range.  You should then select the 
appropriate worksheet which contains the new data and first highlight
 the X-data range and then while holding down the Control (Ctrl) Key
 while selecting the Y-data range.  Select OK when the data is correct
 - a new pop-up panel will appear which will have options such as Add
 Cells as New Series (usually the case) (you can also just add the
 data as new points to the old series if that is what you want to do).
  You should also check that the Category X data is in the first 
Column if that is the case, otherwise Excel will assume that the 
columns which you are adding have the same X data as the first series
 that you put into the chart.  
An easier way to input data into a chart is to simply go to the sheet
 that contains the data and select the cells that have the X-data and
 Y-data and select Copy from under the Edit Menu.  Then move to the
 Chart and select Paste Special from under the Edit Menu.  You will be 
asked whether this is a new series and also about the X-data.  

It is very important that you begin to play with the graphing options 
in Excel and learn how to change the scale of the axes, change the 
format of the data series, and customize the graphing area.  


      PROGRAM MATADD
      DIMENSION A(4,4),B(4,4),C(4,4)
C
C  Get dimensions of matrices
C
      PRINT *, ' This program adds two matrices.'
      PRINT *, ' Enter dimensions of the two matrices (rows, columns): '
      READ  *,NROWS,NCOLS
C      
C
      
      PRINT *, ' ENTER THE FIRST MATRIX, ROW BY ROW'
      DO 5,I=1,NROWS
      READ *,(A(I,J),J=1,NCOLS)
5     CONTINUE
      PRINT*, ' ENTER THE SECOND MATRIX, ROW BY ROW'
      DO 10,I=1,NROWS
      READ *,(B(I,J),J=1,NCOLS)
10    CONTINUE

C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C       ADD THE TWO MATRICES
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      DO 15,I=1,NROWS
      DO 15,J=1,NCOLS
      C(I,J)=A(I,J)+B(I,J)
15    CONTINUE
      PRINT *,'THE SOLUTION IS:'
      DO 25,I=1,NROWS
      PRINT 20,(C(I,J),J=1,NCOLS)
20    FORMAT(5X,4F10.2)
25    CONTINUE
      RETURN
      END

FORTRAN

DO LOOPS (Page 73-85, Etter)
In many situations we may wish to have a program execute a series of 
statements a number of times.  The best way to do this is to construct
 a loop structure which will circle through a series of commands a 
certain number of times.  This is accomplished with a DO LOOP. The 
form of the DO command is as follows:

	DO k index = initial value, final limit, increment
k in the do loop is the address number which represents the end of the
 DO LOOP and usually has a CONTINUE statement.  The index is a variable
 which is used as the loop counter.  The index will be initialized to
 the value initial value, and will be increased each loop by the amount
 given in the increment until the final limit is reached.  If the 
increment is left blank, an increment of 1 is used.  
· The index on the DO loop must be a variable, and can be either a 
real or integer value.  
· The parameters (initial value, final limit, and increment) may be 
constants, variables, or expressions and can be either real or integer
 values.
· The increment can be either positive or negative, but cannot be zero.

We will look more at DO LOOPS next class when we begin dealing with 
ARRAYS.  

ARRAYS (Page 117-123 and 138-144 of Etter)
We have talked a great deal about the use of variables in Fortran 
programming.  We typically use variables in programs for representing
 general data associated with the program.  In many situations in which
 it would be much more efficient to group certain pieces of data
 together.  This is accomplished with the use of arrays.  An array is
 a group of memory locations assigned to one name.  The individual 
members of the array are referred to as elements of the array.  Arrays
 can be either one-dimensional or two -dimensional.

One-dimensional arrays:

One dimensional arrays may be thought of as one column of data or one
 row of data.  The elements of the array are usually referred to in
terms of the name of the array followed by either a subscript or and
index (position number) in parentheses.  
	For example:   	A(1) =1.2
				A(2)=3.4
				A(3)=-0.2
				A(4)=-5.2
  In this case, A is a one-dimensional array with 4 elements.  

Arrays are typically initialized by the DIMENSION statement which 
occurs near the beginning of the program (must be specified before any
 executable statements):

DIMENSION array1(size), array2(size),…..

arrayi above is the name assigned to the array
size tells the program how many memory locations to reserve for the
 specific array.
	For Example:    DIMENSION A(4)
			reserves 4 locations for the array A.

The size of the array must be specified with a constant (not a variable
).  The DIMENSION statements are specification type statements which
 must be placed before any executable statements in your program.  

Similar to the variable names, specific types of data (real or integer
 values) can be stored in arrays.  The type of data can be assigned 
implicitly or explicitly.  Implicit data typing for an array is 
specified by the first letter of the name of an array which follows 
the same rules as already discussed for variables.  
	First letter:        	I, J, K, L, M, N     -  integers
				A-H , O-Z             -  real 

Explicit data typing for arrays are done with the INTEGER or REAL 
statements:

		INTEGER AREA(10), WIDTH(5)
		REAL LENGTH(20)
By using the REAL or INTEGER statements, the array is automatically 
dimensioned (therefore a DIMENSION statement would not be required). 
 The values which are stored in the array must all either be real or
 integer.  
The value of a specific element of an array is assigned the same way 
that any variable is defined:

A(1)= 10.2
A(2) = 3*PI
A(3) = SIN(X) +2

When an array is used, the element number must be specified as well 
- A(1), A(3), A(J)  …    
The element can be specified with a constant or a variable.  

DO Loops are very powerful in dealing with arrays:

DO 15, I=1,10
	A(I)=0.0   
15	CONTINUE

The above  DO LOOP initializes the 10 elements of A equal to 0.0.  

 Reading Arrays from data files:
One area when dealing with arrays which requires a good deal of thought
 are reading the arrays into the program from a data file.  DO LOOPS
 are typically used to read the array, and you need to think about how
 the program reads the data in order to clearly understand how to 
construct the data file.

Reading a one dimensional array AREA from an input file (say that 
there are 10 elements expected in AREA):

SOLUTION 1:

	OPEN (UNIT=10, FILE=input,STATUS=old)
	OPEN (UNIT=11,FILE=output, STATUS=new)			
	DIMENSION A(10)
			.
			.
			.
	DO 5, I=1,10
		READ(10,*) A(I)
5	CONTINUE
			.
			.



SOLUTION 2

	OPEN (UNIT=10, FILE=input,STATUS=old)
	OPEN (UNIT=11,FILE=output, STATUS=new)			
	DIMENSION A(10)
			.
			.
			.
	READ(10,*) (A(I), I=1,10)

****************************************************
*Write a comment statement to the output file regarding the 
*ARRAY A
****************************************************
	WRITE(11,30)
30	FORMAT(5X,'THE ARRAY A IS AS FOLLOWS:')
	WRITE(11,40), (A(I),I=1,10)
40	FORMAT(5X,10F6.2)
			.
			.


Notes on input:  In solution 1 above, the READ statement is within a 
DO LOOP and is therefore executed 10 times.  This means that within 
the input file, each of the 10 elements of A must be on a separate 
line.  In solution 2, the READ statement is only executed once with
 a list of 10 variables (10 elements of array A).  In solution 2, the
 data may all be on one line or multiple lines.  The program will 
continue searching through the input file until it comes to the 10
 values asked for.  

Two Dimensional Arrays:
In many cases we will be dealing with two dimensional arrays.  A two 
dimensional array may best be thought of as  a group of columns.  
Consider the 2 dimensional array WIDTH which has 5 rows and 4 columns:
2.4	1.1	4.2	5.6	
2.2	-4.1	1.2	3.1	
-4.4	2.3	3.2	6.6	
2.1	5.3	-4.0	-1.2	
-5.4	3.3	1.3	-8.1	

We must initialize the array either with a DIMENSION statement or a REAL or INTEGER statement:
DIMENSION arrayname1(rows,columns)

DIMENSION WIDTH(5,4)

Since the array sizes in the dimension statement must be constants, we 
will usually ask for more space that we actually need.  For example,
 the above statement can be

DIMENSION WIDTH(100,100), AREA(200)

or

REAL WIDTH(100,100), AREA(200), LENGTH(150)

Reading arrays from data files:
The main difference dealing with one-dimensional and two-dimensional arrays is that the latter 
array requires two subscripts, A(I,J).

Say that we have a 10 by 5 array WIDTH that that we wish to read in 
from an input file:

	OPEN (UNIT=10, FILE=input,STATUS=old)
	OPEN (UNIT=11,FILE=output, STATUS=new)			
	DIMENSION WIDTH(100,100)
			.
			.
****************************************************
*Read the number of rows NROWS, and the number of columns
*	NCOLS in the array WIDTH from the input file
****************************************************
	READ(10,*) NROWS, NCOLS
			.
			.
			.
	DO 5, I=1,NROWS
		READ(10,*) (WIDTH(I,J), J=1,NCOLS)
5	CONTINUE

****************************************************
*Write a comment statement to the output file regarding the 
*	array WIDTH
****************************************************
	WRITE(11,10)
10		FORMAT(5X,'THE ARRAY WIDTH IS AS 
       +		FOLLOWS:')
	DO 20, I=1,NROWS
		WRITE(11,15), (WIDTH(I,J),J=1,NCOLS)
15		FORMAT(5X,5F10.2,/)
20	CONTINUE
			.
.

FORTRAN
INPUT and OUTPUT (page 94 of Etter)

From last lecture we used READ and PRINT statements:

	READ*, AREA, LENGTH    
			.
			.
	PRINT 7, AREA, LENGTH
    7	FORMAT(5X,'The Area of the rod is ',F8.2,/,
       +                 5X,'The Length of the rod is ',F8.2)

The READ statement reads the data for the two variables AREA and LENGTH 
which are input from the keyboard.  The PRINT statement will print the values 
of AREA and LENGTH to the screen along with the text in the format statement
 shown in the line with address 7.

The above statements deal with list directed input and output in which we read the data directly 
from the keyboard and printing our output back to the screen.  This is satisfactory for situations in 
which we have a few values to input or output, however it is very tedious and impractical for larger 
problems.  In most situations we will want to work with a data file that we have constructed or perhaps
 the data file may be generated as output from another program.  

In order to use a data file with a program (for input or output) the 
file must first be opened.  This is accomplished with an OPEN statement
:  

OPEN (UNIT=integer expression, File=filename, STATUS=literal)

The integer expression designates the unit number to the file.  The 
unit number will be referred to in read or write statements when 
referring to the file.  Some systems reserve specific unit numbers 
for printers, the keyboard, or the screen.  The unit number is any 
integer, 10, 11,…

The filename is the actual filename that you will be using.  If this is 
 be read), you need to put the name of the file that you will be using 
---  input, input.txt, data.txt, etc.

The file status depends on whether this is going to be an input file
 or an output file.  If data is to be read from the file (input file),
 the file will be created before the program runs and you should use 
STATUS='OLD'.  If data from the program will be written to the file 
(output file), the program will be creating the file and you should 
use STATUS='NEW'.

You must use the OPEN statement before you read or write to the file.

The READ statement does change slightly from the form that we used 
before and we use a WRITE statement instead of a PRINT statement:

			READ (unit number,*) variable list
			WRITE(unit number,*) expression list, or
			WRITE(unit number,k) expression list.

In the first WRITE statement with the * we place the variables and 
any character strings directly after the WRITE statement.  In the 
latter WRITE statement we refer to a FORMAT statement at address k.


To employ files instead of list directed input:

	OPEN (UNIT=10, FILE=input,STATUS=old)
	OPEN (UNIT=11,FILE=output, STATUS=new)			
				.
				.
	READ (10,*) AREA, LENGTH
	WRITE (11,7) AREA, LENGTH
    7	FORMAT(5X,'The Area of the rod is ',F8.2,/,
       +                 5X,'The Length of the rod is ',F8.2)

In the above expressions, we first must OPEN the files that we are 
going to be using for input and output.  These files may be opened 
at any location in the program before we try to read or write to the files.
  In many cases it is best to OPEN the files near the beginning of the program.

Notes on reading from files:
· Input files should contain lines of data.  Each READ statement starts
 on a new line of data.  ·If a line does not contain enough values to
 match the number requested in the read statement, the program will 
begin on the next line.  If there are values left on the previous 
line which were not read, these values will be skipped.

Consider the following data in an input file which has been designated
 UNIT 10:

line 1:	0.1	10.1	
line 2:	0.2	11.5	
line 3:	0.5	10.5	
line 4:        1.1    15.6

If we had the following lines in our program:
REAL AREA1, LGTH1, MASS1
		.
		.
READ(10,*) AREA1, LGTH1, MASS1
READ(10,*) AREA2, LGTH2, MASS2

The values which will be read are as follows:
	AREA1=0.1, LGTH1=10.1, MASS1 = 0.2
	AREA2=0.5, LGTH2=10.5, MASS2=1.1

Since there are only two values per line, the program will read MASS1 
from the second line.  The value of 11.5 is not read since the READ 
statement is only asking for 3 values.  When the second read statement
 is issued, the program begins on the next line (line 3) of the data 
file and will start with 0.5.

LOGICAL IF STRUCTURES (Page 58-66 of Etter)

We dealt very briefly in Excel with if statements that are used to 
carry out specific actions based on certain logical conditions.  There
 are a number of situations in Fortran programming when logical 
comparisons are valuable in determining the actions that the program 
will take.


To make a logical comparison we will require the following operators:
		OPERATOR		MEANING
			.EQ.			equal to
			.NE.			not equal to
			.LT.			less than
			.LE.			less than or equal to
			.GT.			greater than
			.GE.			greater than or equal to

In order to use these operators we make a comparison such as
	(AREA.LT.0)      which translates to "AREA less than zero". 
 This is a logical expression which can be used in an IF statement.
A program may make use of a simple logical IF statement, or a block 
IF statement.
Making use of a logical IF statement which may be expressed on a single
line takes the following form:

IF (logical expression) executable statement

	For example:
		IF (STRESS.GT.Fy) STRESS=Fy

In many cases the conditions require more than a simple executable
 statement.  In these situations we will use a "block IF statement".

If(logical expression) Then
statement 1
        		.
		.
		.
statement n
END IF
· If the logical statement is true, the statements within the Block If will be executed.
· If the logical statement is false, the program will jump to the END IF statement and none of the 
statements within the BLOCK IF will be executed.

There are other statements associated with the BLOCK IF structure such as the ELSE and ELSE IF statements:

	If(logical expression 1) THEN
			statement 1
				.
				.
			statement m
		ELSE
			statement m+1
				.
				.
			statement n
	END IF

The above command will evaluate the logical expression, and if true will execute statements 1
 through m.  If the logical expression is false (ELSE) statements m+1 through statements n will be
 carried out.

In many situations we may have multiple conditions in which case the ELSE IF command becomes useful:

	If(logical expression 1) THEN
			statement 1
				.
				.
			statement m
		ELSE IF (logical expression 2) THEN
			statement m+1
				.
				.
			statement n
		ELSE
			statement n+1
				.
				.
			statement o
	END IF

The above command will first evaluate the logical expression 1, and if true will execute statements 
1 through m and then skip to the END IF.  If logical expression 1 is false (ELSE IF), logical expression
 2 will be evaluated, and if true statements m+1 through n will be executed and then the program 
will skip to the END IF.  If the both logical expressions 1 and 2 are false (ELSE) statements n+1 
through statement o will be carried out and the IF BLOCK will terminate at the END IF statement.  

We can string together several IF, ELSEIF statements to satisfy the conditions of our problem. 
 The BLOCK IF structure can sometimes be confusing to follow so it is highly encouraged to indent
 individual sections of the BLOCK IF to set the different sections apart.
Consider the following engineering material problem:

Figure 1 : Stress - strain curve for material



Write a simple Fortran program to evaluate the stress in the rod for a given load, area, and yield stress (Fy)


DO LOOPS
In many situations we may wish to have a program execute a series of
 statements a number of times.  The best way to do this is to
 construct a loop structure which will circle through a series of
 commands a certain number of times.  This is accomplished with a 
DO LOOP. The form of the DO command is as follows:

	DO k index = initial value, final limit, increment
k in the do loop is the address number which represents the end of
 the DO LOOP and usually has a CONTINUE statement.  The index is a 
variable which is used as the loop counter.  The index will be 
initialized to the value initial value, and will be increased each 
loop by the amount given in the increment until the final limit is 
reached.  If the increment is left blank, an increment of 1 is used.  

· The index on the DO loop must be a variable, and can be either a real
 or integer value.  
· The parameters (initial value, final limit, and increment) may be constants, variables, or 
expressions and can be either real or integer values.
· The increment can be either positive or negative, but cannot be zero.

We will look more at DO LOOPS next class when we begin dealing with 
ARRAYS.