FORTRAN 90 for Scientists and Engineers.pdf
(
2207 KB
)
Pobierz
Introduction to Fortran 90 for Scientists and Engineers
Índice:
Cap 1 - Getting Going: 5
Cap 2 - Chapter 2 Elementary Fortran: I: 8
Cap 3 - Chapter 3 Elementary Fortran: II: 24
Cap 4 - Chapter 4 Program Preparation: 41
Cap 5 - Chapter 5 Decisions: 47
Cap 6 - Chapter 6 Loops: 58
Cap 7 - Chapter 7 Errors: 80
Cap 8 - Chapter 8 Subprograms and Modules: 86
Cap 9 - Chapter 9 Arrays: 108
Cap 10 - Chapter 10 Advanced Input and Output: 126
Cap 11 - Chapter 11 Handling Characters: 141
Cap 12 - Chapter 12 Derived Types: Structures: 152
Cap 13 - Chapter 13 Pointer Variables: 171
Cap 14 - Chapter 14 Simulation: 186
Cap 15 - Chapter 15 Matrices and Their Applications: 197
Cap 16 - Chapter 16 Introduction to Numerical Methods: 224
App B - Appendix B Summary of Fortran 90 Statements:248
App C - Appendix C Intrinsic Procedures:
265
App D - Appendix D ASCII Character Codes:
273
App E - Appendix E Solutions to Selected Exercises:
274
1
Introduction to Fortran 90 for Scientists and Engineers
An easy to use online version of "
Fortran 90 for Scientists and Engineers
," by Brian Hahn,
is provided with Fortran PowerStation 4.0 for your use. If you find this book helpful, and
would like a paper copy, an order form has been provided. You can either print the order
form and mail it to the address provided, phone in your order, or use email to order the
book.
Fortran 90 for Scientists and Engineers
Brian D Hahn
University of Cape Town
Department of Applied Mathematics
Hahn, Brian D
Fortran 90 for Scientists and Engineers
I. Title
005.13
ISBN 0-340-60034-9
All rights reserved. No part of this publication may be reproduced or transmitted in any form
or by any means, electronically or mechanically, including photocopying, recording or any
information storage or retrieval system, without either prior permission in writing from the
publisher or a licence permitting restricted copying. In the United Kingdom such licences
are issued by the Copyright Licensing Agency: 90 Tottenham Court Road, London WIP
9HE
Whilst the advice and information in this book is believed to be true and accurate at the
date of going to press, neither the author nor the publisher can accept any legal
reaponsibility or liability for any errors or omissions that may be made.
Printed and bound in Great Britain for Edward Arnold, a division of Hodder Headline PLC,
338 Euston Road, London NW1 3BH, by the University Press, Cambridge
Foreword
The present text arises from an extensive revision of our previous book {Advanced
Methods of Data Exploration and Modelling}. Since so much new material is included,
particularly in those sections dealing with linear models and latent variable models, we
thought it appropriate to regard the work as new rather than simply a second edition.
Consequently we have taken the opportunity to give the book a more appropriate title.
Preface
The Fortran 90 standard represents the first significant change in Fortran in over 20 years,
and brings it into line with most modern structured programming languages. This book is
one of a handful on Fortran 90, and one of even fewer in which every program (unless
otherwise clearly stated) has been tested on a working compiler: the FTN90 compiler for
PCs.
If you are a newcomer to Fortran, you should read the book in the conventional way, from
the beginning.
However, if you are a Fortran 77 user you may like to dip immediately into later chapters to
see some of the new features of the language. Probably the two most important advances
are the new array facilities (Chapters 9 and 15) and the impressively enlarged collection of
intrinsic procedures (Appendix C), including the so-called elemental functions which
operate on all or selected elements of array arguments. You may now define your own
types, or structures (Chapter 12) and even construct linked lists with them using pointers
(Chapter 13). Modules (Chapter 8) may be independently compiled, and may contain type
definitions and variable declarations, as well as procedures. The use of interface blocks
(Chapter 8) makes it possible to overload specific procedure names with generic names,
and also to overload operators. Conditional loops are possible now with DO WHILE
(Chapter 6), and there is a new CASE statement (Chapter 6). However, you should
2
probably first have a look at Sections 2.2--2.5 (program layout, variable declarations, etc.)
and Section 3.5(kind) to see some important changes in the basics of Fortran.
In keeping with the spirit of the earlier edition, this book is a problem-solving exposition of
Fortran 90, and not a technical reference manual. You will therefore not necessarily find all
the references to a particular topic in one place in the text (e.g.\ arrays are covered in
Chapters 9 and 15) as this would interfere with the informal style of the book. There are,
however, appendices with summaries of all the statements and the intrinsic procedures,
and a comprehensive index.
I should like to thank the following in particular: David Mackin of Edward Arnold for his
helpful editorial suggestions, and for arranging the loan of an FTN90 compiler; Peter
Anderton of The Numerical Algorithms Group for the loan of the compiler; the University of
Cape Town for leave in order to write this book, and for financial support for the project; my
long-suffering colleagues for leaving me alone while I was writing; and my wife, Cleone,
who patiently reminds me when programs won't work, that computers are like that, aren't
they?
Brian D. Hahn
Department of Applied Mathematics
University of Cape Town
Rondebosch
South Africa
June 1993
Preface to Problem Solving with FORTRAN 77
So many books on FORTRAN have been written that the appearance of yet another one
seems to require some justification. There are three particular areas where this book can
claim to make a distinctive contribution.
Firstly, the approach taken is a problem-solving one, developed over many years of
teaching programming to first-year university students with no computing experience. The
computer is presented as a tool (probably the most exciting one of the 20th century) for
solving interesting, real world problems, and examples from many areas, particularly
science and engineering, are discussed. The technicalities of each new FORTRAN
construction are therefore generally presented only after motivation by the posing of a
suitable problem. Since the objective of this book is to enable you to solve problems using
a computer, the first 12 chapters are in a sense a preparation for the final three. In these
later chapters you will be introduced to some modern computer applications such as
simulation, modelling and numerical methods. There are also a large number of exercises,
involving a variety of applications. Most of these have solutions provided. Those that do
not have solutions may be suitable for use as class projects in a teaching situation.
Secondly, structured problems are developed throughout. The beginner is shielded from
the devastating effect of the GOTO statement until well into the text. When it is introduced,
the use of GOTO is encouraged in one well-defined situation only: this feature appears to
be unique in all the vast literature on FORTRAN.
Thirdly, emphasis is laid throughout the book on what has come to be called programming
style, and guidelines for writing clear, readable programs are presented.
This book has developed out of notes originally written as a supplement to lectures for
students taking courses in applied mathematics at the University of Cape Town, with no
prior experience of computing. It can therefore be used as a ``teach yourself" guide by
anyone who wants to learn FORTRAN 77 (officially known as FORTRAN ANSI X3-9 1978),
the current international standard, which is the version used here.
Although this is primarily a text for beginners, the more experienced programmer should be
able to find plenty of interest, particularly in the applications. He may even learn something!
The appendices contain summaries of all the FORTRAN 77 intrinsic functions and
3
statements (including those which are not recommended for stylistic reasons), with
examples of their general usage.
No specialized mathematical background is needed to follow most of the examples. There
are occasional forays into first-year university mathematics, but these are self-contained
and may be glossed over without loss of continuity (you may even find them instructive!).
Thanks are due to John Newmarch of the University of Cape Town Information Technology
Services for his critical reading of the original manuscript on which this book is based, and
for his invaluable suggestions regarding programming style. Thanks are also due to the
generations of students who have patiently endured my efforts to improve my methods of
teaching computing. I also wish to thank my colleague, Ruth Smart, who collaborated with
me on an earlier version of this book, for her helpful advice and painstaking reading of the
manuscript. Finally, I should like to acknowledge a deep debt of gratitude to my wife,
Cleone, for her continual support and encouragement during the preparation of this book.
It is hoped this book will give some insight into the ways that computers may be used to
solve real problems, and that after working through it you will be better able to find out
more about this fascinating subject for yourself.
Epilogue Programming Style
Throughoutthis bookthe emphasishas been on writing clear,coherent programstosolve interesting
problems. A program whichis written any oldhow,althoughit may do whatis required, is goingto
be difficultto understand when you gothroughit again after a month or two. Serious programmers
therefore pay a fair amount of attention to whatis called
programming style
,in orderto maketheir
programs clearer and more readable bothto the mselves, andto other potential users. You may find
this irritating, if you are starting to program for the first time, because you will naturally be
impatient to get on with the job. But a little extra attention to your program layout will pay
enor mous dividends in the long run, especially when it comes to debugging.
Some hints on how to improve your program ming style are given below.
•
You should make liberal use of com m ents, both at the beginning of a program unit or
subprogram,to describe briefly whatit does and anyspecial methodsthat may have been used,
and also throughoutthe coding to introduce different logical sections. Any restrictions on the
size andtype of datathat m ay be used as input should be stated clearlyin the comments(e.g.
maximu m sizes of arrays).
•
The meaning ofeach variable should be described briefly in a com ment atits declaration. You
should declare variables system atically, e.g. in alphabetical order by type.
•
Subprograms should be arranged in alphabetical order with at least one blank line between
the m.
•
Blank lines should be freely used to separate sections of coding (e.g. before and after loop
structures).
•
Codinginside structures(loops, decisions,etc)should beindented afew columnsto makethem
stand out.
•
Blanks should be used in statements to make them m ore readable, e.g. on either side of
operators and equal signs; after com mas
•
However,blanks maybe omitted in placesin complicated expressions, wherethis may makethe
structure clearer.
•
FORMAT statem ents should be grouped together.
•
The GOTO statement should
never be used
, under
any
circu mstances.
•
You should try to avoid breaking out of structures in the middle, e.g. with CYCLE or EXIT.
•
Statements which generate an obsolescence warning should be avoided —they could well
disappear during revision for the next standard.
4
Chapter 1 Getting Going
1.1. Introduction
1.2. Fortran
1.3. Running Fortran Programs
•
Greetings
•
AIDS cases
•
Compound interest
Chapter 1 Summary
Chapter 1 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 co mputer is called a
programming
language
. There are many suchlanguages, for example Fortran, BASIC, Pascal and C++. Fortran,
which stands for FO R mula TRANslation, wasthefirst"high level" programminglanguage.It made
it possible to use symbolic namesto represent mathematical quantities, and to write mathematical
formulae in a reasonably comprehensible form, such as X = B/(2*A). Theidea of Fortran was
proposedinlate 1953 byJohn Backus,in New York,andthe first Fortran program wasrunin April
1957.
5
Plik z chomika:
antonif
Inne pliki z tego folderu:
CUDA Fortran for Scientists and Engineers.pdf
(2628 KB)
Computational Methods of Linear Algebra[ Linpack+Fortran].pdf
(135002 KB)
Numerical Recipes in Fortran 90 Vol 2.pdf
(27213 KB)
! Developing_Statistical_Software_in_Fortran_95.pdf
(5740 KB)
William H Press Numerical Recipes In Fortran 90 - The Art Of Parallel Scientific Computing (2Nd E.pdf
(3042 KB)
Inne foldery tego chomika:
Code Blocks Fortran
Fortran
języki programowania (fortran 2.0)
Kompilatory Intel
PGI Visual Fortran + CUDA Fortran
Zgłoś jeśli
naruszono regulamin