Pascal Programs : for scientists and engineers (SYBEX, 1981)
Introduction, The purpose of this book is twofold: to help the reader develop a proficiency in the use of Pascal, and to build a library of programs that can be used to solve problems frequently encountered in science and engineering. The programs in this book will prove valuable to the practicing scientist or engineer. The material is also suitable for a junior- or senior-level engineering course in numerical methods. The reader should have a working knowledge of an applications language such as Pascal, FORTRAN, or BASIC. In addition, experience with vector operations and with differential and integral calculus will be helpful. The many distinctive features of Pascal lead to clear and elegant programming practices. Two of these features that are particularly valuable in scientific programs are long variable names and block structure. When identifiers such as SUM-X-SQUARED and NO-CHANGE can be utilized in preference to shorter symbols, thee resulting source code is easier to understand. Pascal also offers a greater variety of iterative statements than those supplied in the most common BASIC and FORTRAN compilers. The statem FORI: = 1 TO N DO corresponds to the FOR... NEXT loop in BASIC and DO ... CONTINUE in FORTRAN. In Pascal this statement is complemented by the other iterative constructions WHILE...DO and REPEAT... UNTIL. The major features of Pascal are summarized in Pascal User Manual and Report, by Jensen and Wirth'; additional details can be found in Introduction to Pascal, by Zaks?. However, not all of the features included in the original specification of Pascal have been incorporated into actual Pascal compilers. For example, most Pascal compilers do not allow procedure and function names to be used as parameters to other procedures. (This problem is discussed in Chapter 8 under the heading generalizing Procedure Calls.") On the other hand, several desirable features, omitted from the original definition of Pascal, have been added to commercial Pascal compilers. (Unfortunately, these additions have not been uniformly implemented.) One such feature is the incorporation of the dynamic string type. Another is the inclusion of a random number generator, a feature discussed in Chapter 2. Throughout this work, features common to other high-level languages such as FORTRAN, BASIC, and ALGOL have frequently been utilized in preference to the more elegant techniques of Pascal. For example, arrays are used in prererence to records. Asa consequence, the algorithms presented here can be more easily converted into other languages. The reader who is primarily interested in the Pascal programs developed in this book will have no trouble locating them; the sections that contain programs, procedures, or functions are clearly labeled. However, this book is designed to be read from beginning to end each chapter discusses and develops tools that will be used again in subsequent chapters. The mathematical algorithms of each program are methodically described before the program itself is implemented, and sample output is supplied for most of the programs. The following brief descriptions summarize the contents of each chapter: Chapter 1, Evaluation ofa Pascal Compiler, identifies weak points in several commercial compilers, and supplies programs for testing any Pascal compiler. The results of these tests will be used to select various constants and procedures in later chapters. Also included in Chapter 1 is a discussion of logarithmic, exponential, and trigonometric functions in Pascal. Chapter 2, Mean and Standard Deviation, discusses some basic statistical algorithms and presents a program for implementing them. Procedures for generating-and testingboth uniform and Gaussian random numbers are also given. Chapter 3, Vector and Matrix Operations, summarizes the operations of vector and matrix arithmetic, including dot product, cross product, matrix multiplication, and matrix inversion. Two important programs are developed-one for carrying out matrix multiplication, and another for calculating determinants. Chapter 4, Simultaneous Solution of Linear Equations, presents programs to carry out the algorithms of Cramer's rule, the Gauss elimination method, the Gauss-Jordan elimination method, and the Gauss-Seidel method-all for solving simutaneous equations. In addition, ill-conditioning is studied through a program that generates Hillbert matrices, and a program is developed for solving equations with complex coetcients. Chapter 5, Development of a Curve-Fitting Program, is the first of a series of chapters on curve fitting In a good illustration of top-down program development, a linear least-squares curve-fitting program is written and discussed. The program includes procedures to simulate data, plot curves, compute the fitted cunve, and supply the correlation coefficient. Chapter 6, Sorting. describes and compares several Pascal sorting routines, including two bubble sorts, a Shell sort, and a recursive and nonrecursive quick sort. A sort routine is incorporated into the curve-fitting program of Chapter 5 to enable the program to handle real experimental data. Chapter 7, General Least-Squares Curve Fitting, extends the curve-fitting program to general polynomial equations, and finds curve fits for three examples: heat capacity, vapor pressure, and superheated steam. Chapter 8, Solution of Equations by Newton's Method, presents a series of programs that use Newton's algorithm for finding the roots of an equation. This tool will be used again in Chapter 10 for nonlinear curve fitting. Chapter 9, Numerical Integration, develops programs for three different integration methods-trapezoidal rule, Simpson's rule, and the Romberg method. End correction is also discussed. Simpson's rule will be used in Chapter 11 for evaluating the Gaussian error function. Chapter 10, Nonlinear Curve Fitting Equations, discusses curve-1itting algorithms for the rational function and the exponential function. Two examples are given-the Clausing factor, and the diffusion equation. Chapter 11, Advanced Applications: The Normal Curve, the Gaussian Error Function, the Gamma Function, and the Bessel Function, addresses several advanced topics in programming for mathematical applications. This last chapter summarizes and expands upon a number of the concepts presented earlier in the book. For the reader who is approaching Pascal for the first time, a summary of the syntax, standard functions, and reserved words of Pascal is included in the appendices. It is hoped, however, that the real educational experience of this book will be gained througha careful study of the programs themselves.