 Shopping Bag ( 0 items )

All (9) from $50.13

New (4) from $82.82

Used (5) from $50.13
More About This Textbook
Overview
The introduction of the Fortran 90 standard is the first significant change in the Fortran language in over 20 years. this book is designed for anyone wanting to learn Fortran for the first time or or a programmer who needs to upgrade from Fortran 77 to Fortran 90.
Employing a practical, problembased approach this book provides a comprehensive introduction to the language. More experienced programmers will find it a useful update to the new standard and will benefit from the emphasis on science and engineering applications.
Audience: Scientists and Engineers.
Product Details
Related Subjects
Read an Excerpt
Fortran 90 for Scientists and Engineers
By Brian D. Hahn
Elsevier Science
Copyright © 1996 Brian D. HahnAll rights reserved.
ISBN: 9780080571607
Excerpt
CHAPTER 1
Getting Going
1.1 Introduction
1.2 Fortran
1.3 Running Fortran Programs
Greetings AIDS cases Compound interest
Summary
Exercises
1.1 Introduction
In the period since I first became an undergraduate student, some 25 years ago, I have been fortunate enough to witness the remarkable revolution in computer technology which future historians will surely regard as one of the outstanding features of the twentieth century. The first computer I programmed occupied a large room. Only one person could use it at a time, by pressing an impressive array of switches, and programs had to be punched on cards. Its "fast" memory could store about 240 numbers. Its slow memory could hold a few thousand numbers, and was located on a rotating drum which you could hear ticking as it spun.
As technology advanced, and computers became more powerful, they also became much smaller. From occupying a whole room, they now only require part of a desk, a lap, or even a palm. They have banded together to form networks, and during an average working day, it is not uncommon to send electronic mail messages around the world, and to connect directly to a computer on the other side of the world.
You may not have used a computer before (except possibly to play games) but you are probably familiar with using a calculator. The simplest sort can only do arithmetic and display an answer. Smarter ones have memory locations—where intermediate results may be stored—and function keys such as sin, log, etc. The most sophisticated calculators allow you to store the sequence of operations (instructions) needed to calculate the solution of the problem. This sequence of instructions is called a program. To carry out the entire set of calculations you only need to load the program into the calculator, press the run key, supply the necessary data, and sit back while the calculator churns out the answer. A computer, whether it is a small personal one like the IBM PC, or a large impersonal mainframe, is in principle only an advanced programmable calculator, capable of storing and executing sets of instructions, called programs, in order to solve specific problems.
You may have used a computer before, but only to run software packages that have been written by someone else. Spreadsheets, databases and word processors fall into this category. If you have taken the trouble to start reading this book, you probably have an interest in science or engineering, and are curious enough about programming to want to write your own programs to solve your particular problems, instead of relying on someone else's more general package.
1.2 Fortran
The particular set of rules for coding the instructions to a computer is called a programming language. There are many such languages, for example Fortran, BASIC, Pascal and C++. Fortran, which stands for FORmula TRANslation, was the first "high level" programming language. It made it possible to use symbolic names to represent mathematical quantities, and to write mathematical formulae in a reasonably comprehensible form, such as X = B/ (2*A). The idea of Fortran was proposed in late 1953 by John Backus, in New York, and the first Fortran program was run in April 1957.
The use of Fortran spread so rapidly that it soon became necessary to standardize it, so that a program written in the standard would be guaranteed to run at any installation which claimed to support the standard. In 1966 the first ever standard for a programming language was published. This version became known as Fortran 66 (more correctly FORTRAN 66, but the practice of capitalizing acronyms is becoming unfashionable). A new standard, Fortran 77, was published in 1978. In spite of competition from newer languages such as Pascal and C, Fortran continued to flourish, so much so that the latest standard, Fortran 90, came out in August 1991. This is the version used in this book. Connoisseurs of Fortran will be interested in the history of the language sketched by Michael Metcalf and John Reid in Fortran 90 Explained, Oxford University Press (Oxford, 1990).
If you are already experienced in Fortran, you might like to consult the Preface, which indicates where the new features may be found. You will also need to know that some old features have been declared obsolescent. These (which may include some of your old favourites) have been made redundant by the new standard, and are recommended for deletion in the next standard, i.e. the recommendation is not binding. Appendix B contains a summary of all Fortran 90 statements, and indicates which are obsolete and/or not recommended.
1.3 Running Fortran Programs
If you are new to Fortran, you should run the sample programs in this section as soon as possible, without trying to understand in detail how they work. Explanations will follow in due course. You will need to find out, from a manual or from someone else, how to enter and run Fortran programs on your computer system.
Greetings This program will greet you if you give it your name:
! My first Fortran 90 program!
! Greetings!
CHARACTER NAME*20
PRINT*, 'What is your name?'
READ*, NAME
PRINT*, 'Hi there, ' NAME
END
You should get the following output (your response is in italics):
What is your name?
Garfield
Hi there, Garfield
AIDS cases The following program computes the number of accumulated AIDS cases A(t) in the United States in year t according to the formula
A(t) = 174.6(t  1981.2)^{3}.
PROGRAM AIDS
! Calculates number of accumulated AIDS cases in USA
INTEGER T ! year
REAL A ! number of cases
READ*, T
A = 174.6 * (T  1981.2) ** 3
PRINT*, 'Accumulated AIDS cases in US by year', T, ':', A
END PROGRAM AIDS
If you supply the value 2000 for the year you should get the output
Accumulated AIDS cases in US by year 2000 : 1. 1601688E+06
The answer is given in scientific notation. E+06 means multiply the preceding number by 10^{6}, so the number of cases is about 1.16 million. Using trial and error run the program repeatedly to find out when there will be about 10 million accumulated cases.
Try typing a mistake in the value of t (2,000 for example) to see how Fortran responds.
Compound interest Suppose you have $1000 saved in the bank, which compounds interest at the rate of 9% per year. What will your bank balance be after one year? You must obviously be able to do the problem in principle yourself, if you want to program the computer to do it. The logical breakdown, or structure plan, of the problem is as follows:
(1) Get the data (initial balance and interest rate) into the computer
(2) Calculate the interest (9% of $1000, i.e. $90)
(3) Add the interest to the balance ($90 + $1000, i.e. $1090)
(4) Print (display) the new balance.
This is how the program looks:
PROGRAM MONEY
! Calculates balance after interest compounded
REAL BALANCE, INTEREST, RATE
BALANCE = 1000
RATE = 0.09
INTEREST = RATE * BALANCE
BALANCE = BALANCE + INTEREST
PRINT*, 'New balance:', BALANCE
END PROGRAM MONEY
Run the program and note that no input (from the keyboard) is required now (why not?). The output should be 1. 0900000E+03 (1090).
Summary
A computer program is a set of coded instructions for solving a particular problem.
The Fortran statement READ* is for getting data into the computer.
The Fortran statement PRINT* is for printing (displaying) results.
Exercises
1.1 Write a program to compute and print the sum, difference, product and quotient of two numbers A and B (supplied from the keyboard). The symbols for subtraction and division are  and / respectively. Use the program to discover how Fortran reacts to an attempted division by zero.
1.2 The energy stored on a condenser is CV^{2}/2, where C is the capacitance and V is the potential difference. Write a program to compute the energy for some sample values of C and V.
Solutions to most exercises are in Appendix E.
CHAPTER 2
Elementary Fortran: I
2.1 Compound Interest Again
2.2 Program Layout
Statements Significance of blanks Comments Continuation lines
2.3 Data Types
2.4 Literal Constants
Bits 'n bytes Integer literal constants Real literal constants
2.5 Names and Variables
Implicit type rule
2.6 Vertical Motion under Gravity
2.7 Programming Style
2.8 Numeric Expressions
Integer division Mixedmode expressions
2.9 Numeric Assignment
Examples
2.10 Simple Input and Output
Input Example Reading data from text files Output Sending output to the printer
Summary
Exercises
In this chapter and the next one we will look in detail at how to write Fortran programs to solve simple problems. There are two essential requirements for successfully mastering this art:
The exact rules for coding instructions must be learnt;
A logical plan for solving the problem must be developed.
These two chapters are devoted mainly to the first requirement: learning some basic coding rules. Once you have mastered these, we can go on to more substantial problems.
All Fortran 90 statements introduced in this and subsequent chapters (and some which are not) are summarized in Appendix B.
2.1 Compound Interest Again
In Chapter 1 you ran the program MONEY to compute compound interest:
PROGRAM MONEY
! Calculates balance after interest compounded
REAL BALANCE, INTEREST, RATE
BALANCE = 1000
RATE = 0.09
INTEREST = RATE * BALANCE
BALANCE = BALANCE + INTEREST
PRINT*, 'New balance:', BALANCE
END PROGRAM MONEY
We will now discuss in detail how the program works. When you run a Fortran 90 program two separate processes take place. Firstly the program is compiled. This means that each statement is translated into some sort of machine code that the computer can understand. Secondly, the compiled program is executed. In this step each translated instruction is carried out. The software package that carries out both these processes is generally called a compiler.
During compilation, space in the computer's random access memory (RAM) is allocated for any numbers (data) which will be generated by the program. This part of the memory may be thought of as a bank of boxes, or memory locations, each of which can hold only one number at a time. These memory locations are referred to by symbolic names in the program. So the statement
BALANCE = 1000
allocates the number 1000 to the memory location named BALANCE. Since the contents of BALANCE may change during the program it is called a variable.
The translated (compiled) form of our program looks roughly as follows:
(1) Put the number 1000 into memory location BALANCE
(2) Put the number 0.09 into memory location RATE
(3) Multiply the contents of RATE by the contents of BALANCE and put the answer in INTEREST
(4) Add the contents of BALANCE to the contents of INTEREST and put the answer in BALANCE
(5) Print (display) a message followed by the contents of BALANCE
(6) Stop.
During execution, these translated statements are carried out in order from the top down. After execution, the memory locations used will have the following values:
BALANCE : 1090
INTEREST : 90
RATE : 0.09
Note that the original contents of BALANCE is lost.
The PROGRAM statement in the first line introduces the program. It is optional, and may be followed by an optional name. The second line, starting with an exclamation mark, is a comment for the benefit of the reader, and has no effect on the compilation. Variables in a program can be of different type; the REAL statement declares their type in this example. The first three nonblank lines of this program are nonexecutable, i.e. no action is carried out by them (they have no counterpart in the translated form of the program above).
Try the following exercises:
(1) Run the program.
(2) Change the first executable statement to read
BALANCE = 2000
and make sure that you understand what happens when you run the program again.
(3) Leave out the line
BALANCE = BALANCE + INTEREST
and rerun. Can you explain what happens?
(4) Try to rewrite the program so that the original contents of BALANCE is not lost.
A number of questions have probably occurred to you by now, such as
What names may be used for memory locations?
How can numbers be represented?
What happens if a statement won't fit on one line?
How can we organize the output more neatly?
These questions, and hopefully many more, will be answered in the following sections.
(Continues...)
Table of Contents
Getting going
• Elementary Fortran: 1
• Elementary Fortran: 2
• Program preparation
• Decisions
• Loops
• Errors
• Subprograms and modules
• Arrays
• Advanced input and output
• Handling characters
• Derived Types: structures
• Pointer variables
• Simulation
• Matrices and their applications
• Introduction to numerical methods
• Order of statements in a program unit
• Summary of Fortran 90 statements
• Intrinsic procedures
• ASCII character codes
• Solutions to selected exercises.