任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401a 3366-3062 參考書籍 : 1. fortran...

38
任 : 任任任 : 任任任 任任任任 401A 3366-3062 任 : Fortran 90/95 for Scientists and Engineers Stephen J. Chapman Fortran 90 for Engineers and Scientists Larry R. Nyhoff and Sanford C. Leestma Fortran 90 Programming T. M. Ellis; Ivor R. Philips and Thomas M. Lah 任 任 任 任 任

Upload: ethelbert-marsh

Post on 04-Jan-2016

231 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

任課教師 : 諶玉真

助教 : 林漢洲 綜合教室 401A 3366-3062

參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman

2. Fortran 90 for Engineers and Scientists Larry R. Nyhoff and Sanford C. Leestma

3. Fortran 90 Programming T. M. Ellis; Ivor R. Philips and Thomas M. Lahey

計 算 機 程 式

Page 2: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Chapter 1

Introduction to Computers and the Fortran Language

Page 3: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•The computer is the most important invention of the 20th

century. It affects our lives profoundly in very many ways.

•Computers control our telephone and electric power systems, run our microwave ovens and other appliances, and even control the engines in our cars. Almost any business in the developed world would collapse over- night if it were suddenly deprived of its computers.

•Considering their importance in our lives, it is almost im- possible to believe that the first electronic computers were invented just over 50 to 60 years ago.

Page 4: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•A computer is a special type of machine that stores information and performs mathematical calculations on that information at speeds much faster than human beings can think.

•A program, which stored in the computer’s memory, tells the computer what sequence of calculations are required and which information to perform the calcu- lations on.

•Users mean that people who use computers to accom- plish certain tasks and programmers indicate people who program computers so that things can be done by the users.

Page 5: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•It is important to realize that computers do not think as we understand thinking; they merely follow the steps contained in their programs.

•When a computer appears to be doing something clever, it is because a clever person has written the program that it is executing.

•The collective creativity of humans allows the computer to perform its “miracles”.

•This class will help you learn how to write programs of your own so the computer will do what you want it to do.

Page 6: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

The Computer

The major components of the computer are the central processing unit (CPU), main memory, secondary memory, and input and output devices.

Page 7: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

A block diagram of a typical computer

Page 8: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

The CPU

•The central processing unit is the heart of any computer. It is divided into a control unit, an arithmetic-logic unit (ALU), and internal memory.

•The control unit within the CPU controls all the other parts of the computer, and the ALU performs the actual mathematical calculations.

•The internal memory within a CPU consists of a series of memory registers used for the temporary storage of intermediate results during calculations.

Page 9: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•The control unit of the CPU interprets the instructions of the computer program. It also fetches data values from input devices or main memory, stores them in the me- mory registers, and sends data values from memory registers to output devices or main memory.

•For example, if a program says to multiply two numbers together and save the result, the control unit will fetch the two numbers from main memory and store them in registers. Then it will present the numbers in the regis- ters to the ALU along with directions to multiply them and store the results in another register. Finally, after the ALU multiplies the numbers, the control unit will take the result from the destination register and store it back into main memory.

Page 10: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Main and Secondary Memory

•A computer has two major types of memory: main or primary memory, and secondary memory.

•Main memory usually consists of semiconductor chips. It is very fast and relatively expensive. Data that is stored in main memory can be fetched for use in 100 nanose- conds or less on a modern computer.

•Because it is so fast, main memory is used to temporarily store the program currently being executed by the com- puter, as well as the data that the program requires.

Page 11: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•Main memory is not used for the permanent storage of programs or data. Most main memory is volatile, meaning that it is erased whenever the computer’s power is turned off.

•Secondary memory consists of devices that are slower and less expensive than main memory. They can store much more information for much less money than main memory can.

•Most secondary memory devices are nonvolatile. They retain the programs and data stored in them whenever the computer’s power is turned off.

Page 12: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•Typical secondary memory devices are hard disks, floppy disks, and tape drives.

•Secondry storage devices are normally used to store programs and data that are not needed at the moment, but which may be needed some time in the future.

Page 13: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Input and Output Devices

•Data is entered into a computer through an input device and is output through an output device.

•The input devices are keyboards, scanners, and micro- phones, ….

•Output devices permit us to use the data stored in a computer. The output devices are monitors, printers, plotters and speakers, … etc.

Page 14: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Data Representation in a Computer

•Computer memories are composed of millions of indivi- dual switches, each of which can be ON or OFF, but not at a state in between.

•Each switch represents one binary digit (also called a bit); the ON state is interpreted as a binary 1, and the OFF state is interpreted as a binary 0.

•A single switch can represent only the numbers 0 and 1. Since we obviously need to work with numbers other than 0 and 1, a number of bits are grouped together to repre- sent each number used in a computer.

Page 15: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•When several bits are grouped together, they can be used to represent numbers in the binary (base 2) number system.

•The smallest common grouping of bits is called a byte. A byte is a group of 8 bits that are used together to re- present a binary number.

•The byte is the fundamental unit used to measure the capacity of a computer’s memory.

•The next larger grouping of bits in a computer is a word. A word consists of 2, 4 or more consecutive bytes that are used to represent a single number in memory. The size of a word varies from computer to computer.

Page 16: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

The Binary Number System

In the familiar base 10 number system, the number 12210 is

)102()102()101(122 01210

In the binary number system

5)21()20()21(101 0122

7)21()21()21(111 0122

Page 17: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Note that three binary digits can be used to represent 8 possible values: 0 (=0002) to 7 (=1112). In general, if n bits are grouped together to form a binary number, then they can represent 2n possible values.

values possible 2964,294,967, bytes) (4 bits 32

values possible 65,536 bytes) (2 bits 16

values possible 256 byte) (1 bits 8

Typically, half of all possible values are reserved for re- presenting negative numbers, and half of the values are reserved for representing positive numbers.

32767 to 32,768- bytes) (2 bits 16

127 to 128- byte) (1 bits 8

Page 18: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Computer Languages

•When a computer executes a program, it executes a string of very simple operations such as load, store, add, substract, and multiply.

•Each operation has a unique binary pattern called an operation code (op code) to specify it.

•The program that a computer executes is just a string of op codes ( and the data associated with the op codes) in the order necessary to achieve a purpose.

•Op codes are collectively called machine language, since they are the actual language that a computer recognizes and executes.

Page 19: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•Unfortunately, we humans find machine language very hard to work with.

•We prefer to work with English-like statements and alge- braic equations that are expressed in forms familiar to us, instead of arbitrary patterns of zeros and ones.

•We like to program computers with high-level languages.

•We write out our instructions in a high-level language and then use special programs called compilers and linkers to convert the instructions into the machine language that the computer understands.

Page 20: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•Programmers use many different high-level languages, with different characteristics.

•Some of them are designed to work well for business problems, while others are designed for general scientific use. Still others are especially suited for applications like operating-systems programming.

•It is important to pick a proper language to match the pro- blem that you are trying to solve.

•Some of the high-level computer languages in use today are Basic, C, COBOL, Fortran and Pascal.

Page 21: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•Fortran is the preeminent language for general scientific computers.

•It has been around in one form or another for more than 50 years and has been used to implement everything from computer models of nuclear power plants to aircraft design programs …etc.

•The language is especially useful for numerical analysis and technical calculations.

•In addition, Fortran is the dominant language in the world of supercomputers and massively parallel computers.

Page 22: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

A Brief History of Fortran

•One of the first high-level languages to gain widespread acceptance was FORTRAN (FORmula TRANslation), which was developed for the IBM 704 computer by John Backus and a team of 13 other programmers at IBM over a 3-year period (1954-1957).

•In the years that followed, other computer manufacturers developed FORTRAN compilers for their machines. Several of these provided extensions and variations of FORTRAN that were specific to their particular computers.

Page 23: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•Programs written for one machine could not be used on a different machine without modification.

•Over time, users developed large collections of FORTRAN programs, and the cost of converting all these programs whenever a new computer was installed became prohibi- tive.

•To remedy these problems, efforts were made to standize FORTRAN so that programs were portable, which means they can be processed on several different machines with little or no alteration.

•One of the earliest standard versions appeared in 1966, and in 1977 another revision appeared, known as FORTRAN 77.

Page 24: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•While the changes introduced in FORTRAN 77 were significant, it did not solve all of the problems in earlier versions, nor did it include many of the new features that were appearing in newer programming languages.

•Consequently, work began almost immediately on a new standard version; in 1991, an extensive revision known as Fortran 90 appeared, and compilers to support this new version of Fortran have been developed.

•The American National Standards Institute (ANSI), which establishes standards for programming languages, decided that during the transition period there should be two American standards for Fortran, FORTRAN 77 and Fortran 90.

Page 25: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•However, International Standards Organization (ISO) groups decided that Fortran 90 will be the only inter- national Fortran standard.

•Fortran 90 has standardized earlier versions and provides a base from which newer versions of the lan- guage will be developed.

•In fact, another ANSI committee is already at work preparing a new version that will make it possible to design Fortran programs that will execute efficiently on massively parallel computers and that incorporate modern programming techniques such as object- oriented programming.

Page 26: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Programming with FORTRAN

• FORTRAN : a combination of the words FORmula TRANslator

(1) high-level (2) English-like language (3) Engineering-oriented

• It is very easy to read and understand a Fortran program.

Page 27: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

An example

program example

! This program is to calculate the summation ! and average of two variables, a and b.

real :: a, b, sum, avg

read (*,*) a, b sum = a+b avg = (a+b)/2. write(*,*) a, b, sum, avg

end program example

Page 28: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Earlier versions of FORTRAN have rather strict rules about where statements and comments can be placed. However, Fortran 90 allows considerably more flexibility in the program format.

• The main rules that must be followed are (1)A line may have a maximum of 132 characters. (2)A line may contain more than one statement, provided the statements are separated by semicolons (;).

program example real:: a,b,sum,avg; read *, a,b sum=a+b; avg=(a+b)/2.0; print *,a,b,sum,avg end program example

Program Format

Page 29: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

(3)An ampersand (&) must be placed at the end of each line that is to be continued to the next line. At most 39 continuation lines are permitted.

For example

avg = (a+b)/2. avg = & (a+b)/2. avg = & (a+b)& /2.

Page 30: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

(4)Any characters following an exclamation (!) and running to the end of the line form a comment. program example ! This program is to calculate the summation ! and average of two variables, a and b. real:: a,b,sum,avg read *, a,b sum=a+b avg=(a+b)/2. print *, a,b,sum,avg end program example

•Comments may also be attached to statements. sum=a+b ! Calculate the summation of a and b

Page 31: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

(5)If a statement requires a statement label, this label must precede the statement and be separated from it by at least one blank. Statement labels must be integers in the range 1 to 99999.

. . goto 1000 . . 1000 sum=a+b avg=(a+b)/2. . .

Page 32: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Types of Data Stored in Memory

•Three common types of data are stored in a computer’s memory: character data, integer data, and real data.

•Each type of data has different characteristics and takes up a different amount of memory in the computer.

Character data•The character data type consists of characters and symbols. A typical system for representing character data in a non-Oriental language must include the following symbols:

Page 33: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•The 26 uppercase letters ‘A’ through ‘Z’.

•The 26 lowercase ‘a’ through ‘z’.

•The 10 digits ‘0’ through ‘9’.

•Miscellaneous common symbols, such as “,(){} !~@#$%^&*

•Any special letters or symbols required by the language, such as áçñ.

Since the total number of characters and symbols required to write non-Oriental languages is less than 256, it is cus- tomary to use 1 byte of memory to store each character.

Page 34: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Integer data

•The integer data type consists of the positive integers, the negative integers, and zero. The amount of memory devoted to storing an integer will vary from computer to computer, but it will usually be 1, 2, 4, or 8 bytes. The most common type of integers in modern computers are 4-byte integers.

•Since a finite number of bits are used to store each value, only integers that fall within a certain range can be represented on a computer.

Page 35: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•Usually, the smallest number and the largest number that can be stored in an n-bit integer is

1

1-n

1-n

2 Value Integer Largest

2- Value Integer Smallest

•For a 4-byte integer, the smallest and largest possible value are -2,147,483,648 and 2,147,483,647, respec- tively.

•Attempts to use an integer larger than the largest possi- ble value or smaller than the smallest possible value result in an error called an overflow condition.

Page 36: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

Real data

• The integer type has two fundamental limitations:

1. It is not possible to represent numbers with fractional parts(0.25, 1.5, 3.14159 etc) as integer data.2. There are not enough bits available to represent integers or very small negative integers.

• To get around these limitations, computers include a real or floating-point data type.

• The real data type stores numbers in a type of scien- tific notation.

Page 37: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•We all know that very large or very small numbers can be most conveniently written in scientific notation.

•For example, the speed of light in a vacuum is about 299,800,000 meters per second. This number is easier to work with in scientific notation:

m/s 810998.2

mantissa

exponent

Page 38: 任課教師 : 諶玉真 助教 : 林漢洲 綜合教室 401A 3366-3062 參考書籍 : 1. Fortran 90/95 for Scientists and Engineers Stephen J. Chapman 2. Fortran 90 for Engineers

•The real numbers in a computer are similar to the scien- tific notation above except that a computer works in the base 2 system instead of in the base 10 system.

•Real numbers usually occupy 32 bits (4 bytes) of com- puter memory, divided into two components: a 24-bit mantissa and an 8-bit exponent.