A First Course in Scientific Computing Fortran Version
Total Page:16
File Type:pdf, Size:1020Kb
A First Course in Scientific Computing Symbolic, Graphic, and Numeric Modeling Using Maple, Java, Mathematica, and Fortran90 Fortran Version RUBIN H. LANDAU Fortran Coauthors: KYLE AUGUSTSON SALLY D. HAERER PRINCETON UNIVERSITY PRESS PRINCETON AND OXFORD ii Copyright c 2005 by Princeton University Press 41 William Street, Princeton, New Jersey 08540 3 Market Place, Woodstock, Oxfordshire OX20 1SY All Rights Reserved Contents Preface xiii Chapter 1. Introduction 1 1.1 Nature of Scientific Computing 1 1.2 Talking to Computers 2 1.3 Instructional Guide 4 1.4 Exercises to Come Back To 6 PART 1. MAPLE OR MATHEMATICA BY DOING (SEE TEXT OR CD) 9 PART 2. FORTRAN BY DOING 195 Chapter 9. Getting Started with Fortran 197 9.1 Another Way to Talk to a Computer 197 9.2 Fortran Program Pieces 199 9.3 Entering and Running Your First Program 201 9.4 Looking Inside Area.f90 203 9.5 Key Words 205 9.6 Supplementary Exercises 206 Chapter 10. Data Types, Limits, Procedures; Rocket Golf 207 10.1 Problem and Theory (same as Chapter 3) 207 10.2 Fortran’s Primitive Data Types 207 10.3 Integers 208 10.4 Floating-Point Numbers 210 10.5 Machine Precision 211 10.6 Under- and Overflows 211 Numerical Constants 212 10.7 Experiment: Determine Your Machine’s Precision 212 10.8 Naming Conventions 214 iv CONTENTS 10.9 Mathematical (Intrinsic) Functions 215 10.10 User-Defined Procedures: Functions and Subroutines 216 Functions 217 Subroutines 218 10.11 Solution: Viewing Rocket Golf 220 Assessment 224 10.12 Your Problem: Modify Golf.f90 225 10.13 Key Words 226 10.14 Further Exercises 226 Chapter 11. Visualization with Fortran 228 11.1 An Overview of Dislin 228 2-D Graphs within Fortran using Dislin 229 Running the Sample Program 231 Dislin Matrix Plots 233 Dislin Surface Plots 234 11.2 Setting up Dislin * 236 11.3 Gnuplot Basics 238 Printing Plots 240 Gnuplot Surface (3-D) Plots 241 Chapter 12. Flow Control via Logic; Projectiles 243 12.1 Problem: Frictionless Projectile Motion 243 12.2 Theory: Kinematics 244 12.3 Computer Science: Designing Structured Programs 245 Flow Charts and Pseudocode 246 12.4 Flow Control via Logic 247 Conditional and Relational Operators 248 Control Structures 249 Select Case 253 12.5 Implementation: projectile.f90 254 12.6 Solution: Projectile Trajectories 254 12.7 Key Words 255 12.8 Supplementary Exercises 255 Chapter 13. Fortran Input and Output 257 13.1 Basic I/O 258 13.2 Formatted Output 259 13.3 File I/O 260 Chapter 14. Numerical Integration; Power and Energy Usage 262 14.1 Problem (Same as Chapter 6): Power and Energy 262 14.2 Algorithms: Trapezoid and Simpson’s Rules 263 Trapezoid Rule 264 CONTENTS v 14.3 Implementation: Trap.f90 266 Implementation with Nested Loops 266 Improved Method: Simpson’s Rule 267 14.4 Assessment: Which Rule Is Better? 268 14.5 Key Words and Concepts 269 14.6 Supplementary Exercises 269 Chapter 15. Differential Equations with Maple and Fortran* 271 15.1 Problem: Projectile Motion with Drag 271 15.2 Model: Velocity-Dependent Drag 272 15.3 Algorithm: Numerical Differentiation 273 15.4 Math: Solving Differential Equations 273 Implementation: ProjectileAir.f90 275 15.5 Assessment: Balls Falling Out of the Sky? 275 Improved Algorithm: Verlet* 277 15.6 Maple: Differential-Equation Tools 278 Extract Right-Hand Side: rhs 280 Second-Order ODE with Plot 280 System of ODEs 281 15.7 Maple Solution: Drag ∝ Velocity 282 15.8 Extract Operands 284 Solution for R and T 285 2 15.9 Drag ∝ v (Exercise) 286 3 2 15.10 Drag ∝ v / 287 Defining Functions from Procedures 289 15.11 Exploration: Planetary Motion* 290 Implementation: Planet.java* 291 15.12 Key Words 291 15.13 Supplementary Exercises 292 Chapter 16. Object-Oriented Programming; Complex Currents 293 16.1 Problem: Resonance in RLC Circuit 293 16.2 Math: Complex Numbers 293 Complex Arithmetic Review 296 16.3 Theory: Resistance Becomes Impedance 297 Solution for Complex Current 298 16.4 CS: Abstract Data Types, Objects 299 16.5 Objects in Fortran 301 Data by Reference 301 Sample Data Type 301 Object Constructors 305 Declaring and Creating Objects 305 Instances 306 16.6 Fortran Solution: Complex Currents 306 vi CONTENTS 16.7 Maple Solution: Complex Currents 307 Maple’s Surface Plots of Complex Impedance 309 16.8 Key Words 311 16.9 Fortran and Maple Exercises 311 Chapter 17. Arrays: Vectors, Matrices; Rigid-Body Rotations 312 17.1 Problem: Rigid-Body Rotations 312 17.2 Theory: Angular-Momentum Dynamics 314 17.3 Computer Science and Math: Arrays vs. Vectors and Matrices 315 17.4 Array Declaration and Instantiation 316 Array Sizes 318 Fixed-Sized Arrays 318 Assumed-Shape Arrays* 319 Allocatable Arrays* 319 17.5 Arrays as Arguments to Procedures* 321 Computation: Changing Array Arguments in Functions 322 17.6 Application to Rotations 324 17.7 Key Words 325 17.8 Supplementary Exercises 325 Chapter 18. Advanced Function Methods, Objects 329 18.1 Procedure Modules and Interfaces 329 18.2 Changing the Arguments of a Function 332 18.3 Functions Calling Functions 334 18.4 Function Overloading 335 Chapter 19. Discrete Math, Arrays as Bins; Bug Dynamics* 337 19.1 Problem: Variability of Bug Populations 337 19.2 Theory: Self-Limiting Growth, Discrete Maps 337 19.3 Assessment: Properties of Nonlinear Maps 339 19.4 Exploration: Bifurcation Diagram, Bugs.f90* 340 Implementation; Bifurcation Diagram 341 Binning 342 19.5 Exploration: Other Discrete Maps* 343 Chapter 20. 2-D Arrays: File I/O, PDE’s; Realistic Capacitor 344 20.1 Problem: Field of Realistic Capacitor 344 20.2 Theory and Model: Electrostatics and PDEs 344 Laplace’s Partial Differential Equation* 346 20.3 Algorithm: Finite Differences 346 20.4 Implementation: LaplaceFile.f90 348 LaplaceFile.f90 Orientation and Visualization 349 20.5 Exploration 350 CONTENTS vii 20.6 Exploration: 3-D Capacitor* 352 20.7 Key Words 352 Chapter 21. Web Computing (see Java Version) 353 PART 3. LATEX SURVIVAL GUIDE 355 Chapter 22. LATEX for Text 357 22.1 Why LATEX? 357 22.2 Structure of a LATEX Document 358 22.3 Sample Input File (Sample.tex) 358 22.4 Sample LATEX Output 360 Brief Look at Input File 361 Special Characters 361 Paragraphs, Spaces, and Breaks 362 Quotation Marks and Dashes 363 22.5 Fonts for Text 364 Type Sizes 365 Fancy Text Accents 365 Math Symbols in Text 365 22.6 Environments 366 22.7 Lists 366 Text Tables 367 Floating Tables 368 22.8 Sections 369 Subsections 369 Quotations and Footnotes 370 Chapter 23. LATEX for Mathematics 371 23.1 Entering Mathematics: Math Mode 371 23.2 Mathematical Symbols and Greek 372 Greek Letters 372 Relations 373 Negative Relations 373 Binary (and Other) Operators 374 Arrows 374 Math Parentheses 374 Miscellaneous Math Symbols 374 “Big” Operators 375 23.3 Math Accents 375 23.4 Superscripts and Subscripts 375 23.5 Calculus and Sums 375 23.6 Changing Math Fonts 376 viii CONTENTS 23.7 Math Functions 376 23.8 Fractions 376 23.9 Roots 377 23.10 Brackets (Delimiters) 377 23.11 Multiline Equations 378 23.12 Matrices and Math Arrays 379 23.13 Including Graphics 380 23.14 Exercise: Putting It All Together 382 Appendix A. Glossary 386 Appendix B. Fortran Quick Reference 395 B.1 Transferring Files from the CD 398 B.2 Using our Fortran Programs 399 Bibliography 400 Index 405 List of Figures 1.1 Left: Computational science is a multidisciplinary field that combines science with computer science and mathematics. Right: A new paradigm for science in which simulation plays as es- sential a role as does experiment and theory. 2 1.2 A schematic view of a computer’s kernel and shells. 3 9.1 Steps in compiling and executing. 199 10.1 A schematic representation of the limits of single-precision floating-point numbers, and the consequences of exceeding those limits. 210 10.2 Left: The structure of a Fortran program with no procedures. Right: The structure of a Fortran program with a main pro- gram, a subroutine, and a function. The main program and the two procedures are contained in the same source file, for instance, ProgName.f90. 216 10.3 A plot of the function γ(v) output from the program Golf.f90. Compare to similar plot done with Maple in Chap. 3. 223 11.1 The Dislin output from the program EasyDislinPlot.f90. 231 11.2 The Dislin output from the program MatrixPlot.f90. 234 11.3 The Dislin output from the program Laplace.f90. 237 11.4 A gnuplot graph for three data sets with impulses and lines. 239 11.5 Gnuplot’s surface plot of a scattering amplitude ImT as a func- tion of complex energy E. 240 12.1 Left: The trajectory of a projectile fired with initial velocity V0 in the θ direction. The nonparabolic curve includes air re- sistance. Right: The components of the initial velocity V0 projected onto the x and y axes. 243 12.2 A flow chart illustrating a possible program to compute pro- jectile motion. On the left, we show the very high-level basic components of the program, while on the right, we show some of the details of the logic flow structures. 246 x LIST OF FIGURES 12.3 Left: Sequential or linear programming. Right: The if-then- else structure, one of several structures used to write nonlinear program segments based on logical decisions. 247 12.4 Left: The Fortran Do-loop iteration. Right: The Fortran Do- while loop uses a general test, in contrast to the Do-loop’s iter- ation count.