# Thompson - Computing for Scientists and Engineers

Посмотреть архив целикомПросмотреть файл в отдельном окне: 127ccb34e8c340a692abe81c4255d027.pdf

Home Next

COMPUTING FOR

SCIENTISTS AND

ENGINEERS

COMPUTING FOR

SCIENTISTS AND

ENGINEERS

A Workbook of Analysis,

Numerics, and Applications

WILLIAM J. THOMPSON

University of North Carolina

Chapel Hill, North Carolina

A Wiley-Interscience Publication

JOHN WILEY & SONS, INC.

New York / Chichester / Brisbane / Toronto / Singapore

This text is printed on acid-free paper.

Copyright © 1992 by John Wiley & Sons, Inc.

All rights reserved. Published simultaneously in Canada.

Reproduction or translation of any part of this work

beyond that permitted by Section 107 or 108 of the

1976 United State Copyright Act without the permission

of the copyright owner is unlawful. Requests for

permission or further information should be addressed to

the Permissions Department, John Wiley & Sons, Inc.,

605 Third Avenue, New York, NY 10158-0012

Library of Congress Cataloging in Publication Data:

Thompson, William J. (William Jackson), 1939 Computing for Scientists and Engineers : a workbook of analysis,

numerics, and applications /William J. Thompson.

p.

cm.

Rev. ed. of: Computing in applied science. c1984.

“A Wiley-Interscience publication.”

Includes bibliographical references and indexes.

ISBN O-471-54718-2 (cloth)

1. Numerical analysis-Data processing. 2. Science-Data

processing. 3. Engineering-Data processing. I. Thompson, William

J. (William Jackson), 1939- Computing in applied science.

II. Title.

QA297.T5 1992

519.4–dc20

92-16744

Printed in the United States of America

10 9 8 7 6 5 4 3 2

PREFACE

This preface is addressed to readers who are interested in computing but who seldom either consult manuals or read prefaces. So, I will be brief.

Computing requires an integrated approach, in which scientific and mathematical

analysis, numerical algorithms, and programming are developed and used together.

The purpose of this book is to provide an introduction to analysis, numerics, and

their applications. I believe that a firm grounding in the basic concepts and methods

in these areas is necessary if you wish to use numerical recipes effectively. The

topics that I develop extensively are drawn mostly from applied mathematics, the

physical sciences, and engineering. They are divided almost equally among review

of the mathematics, numerical-analysis methods (such as differentiation, integration,

and solution of differential equations from the sciences and engineering), and dataanalysis applications (such as splines, least-squares fitting, and Fourier expansions).

I call this a workbook, since I think that the best way to learn numerically oriented computing is to work many examples. Therefore, you will notice and, I hope,

solve many of the exercises that are strewn throughout the text like rocks in the

stream of consciousness. I try to introduce you to a technique, show you some of

the steps, then let you work out further steps and developments yourself. I also

suggest new and scenic routes rather than overtraveled highways. There are occasional diversions from the mainstream, often to point out how a topic that we are developing fits in with others in computing and its applications.

The programming language in which I present programs is C. This language is

now used extensively in systems development, data-acquisition systems, numerical

methods, and in many engineering applications. To accommodate readers who prefer Fortran or Pascal, I have used only the numerically oriented parts of C and I have

v

vi

structured the programs so that they can usually be translated line-by-line to these

other languages if you insist. The appendix summarizes the correspondences between the three languages. All programs and the functions they use are listed in the

index to programs. The fully-developed programs are usually included in the projects near the end of each chapter.

This book may be used either in a regular course or for self-study. In a onesemester course for college students at the junior level or above (including graduate

students), more than half the topics in the book could be covered in detail. There is

adequate cross-referencing between chapters, in addition to references to sources for

preparation and for further exploration of topics. It is therefore possible to be

selective of topics within each chapter.

Now that we have an idea of where we’re heading, let’s get going and compute!

William J. Thompson

Chapel Hill, July 1992

CONTENTS

1. Introduction to Applicable Mathematics and Computing

1.1 What is applicable mathematics? 1

1

Analysis, numerics, and applications 2

Cooking school, then recipes 3

Diversions and new routes 4

Roads not taken 4

1.2 Computing, programming, coding 5

The C language for the programs 6

Learning to program in C 7

Translating to Fortran or Pascal from C 8

The computing projects and the programs 9

Caveat emptor about the programs 10

The index to computer programs 11

1.3

One picture is worth 1000 words 11

Why and when you should use graphics 11

Impressive graphics, or practical graphics 12

1.4 Suggestions for using this book 12

Links between the chapters 13

The exercises and projects 13

References for the introduction 14

General references 14

References on learning and using C 15

2. A Review of Complex Variables

2.1 Algebra and computing with complex numbers 18

17

The algebra of complex numbers 18

Programming with complex numbers 20

Complex conjugation, modulus, argument 23

A program for complex conjugate and modulus 25

vii

CONTENTS

viii

2.2 The complex plane and plane geometry 27

Cartesian and plane-polar coordinates 28

De Moivre’s theorem and its uses 29

2.3 Functions of complex variables 31

Complex exponentials: Euler’s theorem 3 1

Applications of Euler’s theorem 3 2

Hyperbolic functions and their circular analogs 3 4

Trajectories in the complex plane 38

2.4 Phase angles, vibrations, and waves 41

Phase angles and phasors 4 1

Vibrations and waves 42

2.5 Diversion: Interpreting complex numbers 43

Are complex numbers real? 43

Analytic continuation 44

2.6

Project 2: Program to convert between coordinates 45

Stepping into the correct quadrant 45

Coding, testing, and using the program 46

References on complex numbers 49

3. Power Series and Their Applications

3.1 Motivation for using series: Taylor’s theorem 51

The geometric series 52

Programming geometric series 53

Alternating series 56

Taylor’s theorem and its proof 58

Interpreting Taylor series 59

3.2 Taylor expansions of useful functions 60

Expansion of exponentials 61

Computing the exponential series 62

Series for circular functions 65

Inverse circular functions 70

Hyperbolic function expansions 71

Logarithms in series expansions 72

Series expansion of x In(x) 73

3.3 The binomial approximation 76

Deriving the binomial approximation 76

Applications of the binomial approximation 7 8

Linearized square-root approximations 78

Financial interest schemes 80

3.4

Diversion: Repetition in mathematics and computing 83

Iteration 84

Recurrence 84

Recursion 84

3.5

Project 3: Testing the convergence of series 85

Coding and checking each series expansion 85

Including the hyperbolic functions 91

File output and graphics options 92

The composite program for the functions 92

Using the program to test series convergence 97

References on power series 98

51

CONTENTS

4. Numerical Derivatives and Integrals

4.1 The working function and its properties 100

ix

99

Properties of the working function 100

A C function for Homer’s algorithm 103

Programming the working function 106

4.2

Discrete data and numerical mathematics 110

The discreteness of data 110

Numerical mathematics 111

4.3 Numerical noise in computing 111

Roundoff and truncation errors 112

Unstable problems and unstable methods 114

Errors from subtractive cancellation 116

Program for roots of quadratic equations 119

4.4 How to approximate derivatives 122

Forward-difference derivatives 123

Derivatives by central differences 125

Numerical second derivatives 126

Better algorithms for second derivatives 128

4.5

Project 4A: Computing derivatives numerically 130

Derivatives of the exponential function 130

Differentiating the cosine function 132

4.6 Numerical integration methods 133

Trapezoid formula and program for integration 135

Simpson formula and program for integrals 140

Integrals with cosines 143

Higher-order polynomial integration 144

4.7

Project 4B: Electrostatic potential from a charged wire 145

Potentials by analytical integration 147

Potentials by numerical-integration methods 148

References on numerical derivatives and integrals 151

5. Fitting Curves through Data

5.1 How to fit curves using splines 154

What is a spline? 154

Properties for spline fits 156

Deriving the spline equations 156

The spline algorithm 158

5.2 Boundary conditions for spline fitting 159

Natural splines 160

5.3

Project 5: Program for spline fitting 161

The main program, Cubic Splines 166

The function SplineFit 167

5.4 Interpolating by splines 168

Interpolating values and derivatives 168

The C function Splinelnterp 169

Interpolating working-function values and derivatives 170

Interpolating cosine values and derivatives 173

153

CONTENTS

x

5.5 Integration methods using splines 175

Deriving the integration algorithm 175

The C function for spline integration 176

Integrating the working function and cosine 177

5.6 Diversion: Computers, splines, and graphics 178

References on spline fitting 179

6. Least-Squares Analysis of Data

6.1 Introduction to the least-squares criterion 182

181

Maximum likelihood and least squares 182

Least squares and the objective function 185

6.2

Orthogonal functions and linear least squares 185

What are orthogonal functions? 186

Orthogonality and least squares 188

6.3

Errors in both variables: Straight-line least squares 190

Weighting models 190

Constant ratio of weights 192

Properties of the least-squares slopes 196

6.4 Least-squares normalization factors 199

Normalizing

Normalizing

The best-fit

Program for

6.5

fitting-function values to data 200

data to fitting values 201

objective function 203

normalizing factors 204

Logarithmic transformations and parameter biases 208

The origin of bias 209

Probability analysis for bias 210

Dependence of bias on error distribution 212

6.6

Project 6: Program for straight-line least-squares fits 214

Organization of Straight-Line Least Squares 214

Testing and using the least-squares program 217

References on least-squares analysis 218

7. Introduction to Differential Equations

7.1 Differential equations and physical systems 222

Why are there differential equations? 222

Notation and classification 223

Homogeneous and linear equations 224

Nonlinear differential equations 225

7.2 First-order linear equations: World-record sprints 225

Kinematics of world-record sprints 226

Warming up to the problem 227

Program for analyzing sprint data 229

Women sprinters are getting faster 234

7.3 Nonlinear differential equations: Logistic growth 235

The logistic-growth curve 235

Exploring logistic-growth curves 238

Generalized logistic growth 239

221

CONTENTS xi

7.4 Numerical methods for first-order equations 241

Presenting error values 241

Euler predictor formulas 242

Testing the Euler predictors 244

Adams predictor formulas 245

7.5 Project 7: Program for solving first-order equations 247

Programming the differential equation solver 247

Exploring numerical first-order equations 252

References on first-order equations 255

8. Second-Order Differential Equations

8.1 Forces, second-order equations, resonances 258

257

Forces and second-order equations 258

Mechanical and electrical analogs 259

Solving and interpreting free-motion equations 261

Forced motion and resonances 265

8.2 Catenaries, cathedrals, and nuptial arches 269

The equation of the catenary 270

Catenaries of various shapes and strengths 273

Demonstrating arches 278

Practical arches and catenaries 279

8.3 Numerical methods for second-order differential equations 279

Euler-type algorithms for second-order equations 280

Removing first derivatives from second-order linear equations 284

Deriving the Noumerov algorithm for second-order equations 2 8 5

8.4 Project 8A: Progamming second-order Euler methods 287

Programming the Euler algorithms 287

Euler algorithms and the exponential function 291

Euler algorithms and the cosine function 293

8.5 Project 8B: Noumerov method for linear second-order equations 294

Programming the Noumerov method 294

Testing Noumerov for exponentials and cosines 2 9 7

The quantum harmonic oscillator 299

Noumerov solution of the quantum oscillator 301

8.6 Introduction to stiff differential equations 304

What is a stiff differential equation? 305

The Riccati transformation 306

Programming the Riccati algorithm 308

Madelung’s transformation for stiff equations 311

References on second-order equations 312

9. Discrete Fourier Transforms and Fourier Series

9.1 Overview of Fourier expansions 316

The uses of Fourier expansions 316

Types and nomenclature of Fourier expansions 316

315

CONTENTS

xii

9.2

Discrete Fourier transforms 3 18

Derivation of the discrete transform 318

Properties of the discrete transform 320

Exponential decay and harmonic oscillation 322

9.3

The fast Fourier transform algorithm 329

Deriving the FFT algorithm 329

Bit reversal to reorder the FFT coefficients 332

Efficiency of FFT and conventional transforms 333

9.4 Fourier series: Harmonic approximations 334

From discrete transforms to series 334

Interpreting Fourier coefficients 336

Fourier series for arbitrary intervals 336

9.5 Some practical Fourier series 337

The square-pulse function 338

Program for Fourier series 340

The wedge function 343

The window function 345

The sawtooth function 347

9.6 Diversion: The Wilbraham-Gibbs overshoot 349

Fourier series for the generalized sawtooth 350

The Wilbraham-Gibbs phenomenon 353

Overshoot for the square pulse and sawtooth 356

Numerical methods for summing trigonometric series 359

9.7

Project 9A: Program for the fast Fourier transform 360

Building and testing the FFT function 360

Speed testing the FFT algorithm 364

9.8

Project 9B: Fourier analysis of an electroencephalogram 365

Overview of EEGs and the clinical record 365

Program for the EEG analysis 368

Frequency spectrum analysis of the EEG 372

Filtering the EEG data: The Lanczos filter 373

References on Fourier expansions 375

10. Fourier Integral Transforms

10.1 From Fourier series to Fourier integrals 377

The transition from series to integrals 378

Waves and Fourier transforms 379

Dirac delta distributions 379

10.2 Examples of Fourier transforms 380

Exponential decay and harmonic oscillation 380

The square-pulse function 383

Fourier transform of the wedge function 384

Gaussian functions and Fourier transforms 386

Lorentzian functions and their properties 389

Fourier integral transform of a Lorentzian 391

10.3 Convolutions and Fourier transforms 393

Convolutions: Definition and interpretation 393

Convoluting a boxcar with a Lorentzian 394

Program for convoluting discretized functions 398

377

CONTENTS

xiii

Fourier integral transforms and convolutions 401

Convolutions of Gaussians and of Lorentzians 402

Convoluting Gaussians with Lorentzians: Voigt profile 406

10.4 Project 10: Computing and applying the Voigt profile 411

The numerics of Dawson’s integral 412

Program for series expansion of profile 413

Program for direct integration of profile 418

Application to stellar spectra 419

References on Fourier integral transforms 419

EPILOGUE

421

APPENDIX: TRANSLATING BETWEEN C, FORTRAN,

AND PASCAL LANGUAGES

423

INDEX TO COMPUTER PROGRAMS

429

INDEX

433

COMPUTING FOR

SCIENTISTS AND

ENGINEERS

Previous Home Next

Chapter 1

INTRODUCTION TO APPLICABLE

MATHEMATICS AND COMPUTING

The major goal for you in using this book should be to integrate your understanding,

at both conceptual and technical levels, of the interplay among mathematical analysis, numerical methods, and computer programming and its applications. The purpose of this chapter is to introduce you to the major viewpoints that I have about

how you can best accomplish this integration.

Beginning in Section 1.1, is my summary of what I mean by applicable mathematics and my opinions on its relations to programming. The nested hierarchy (a

rare bird indeed) of computing, programming, and coding is described in Section 1.2. There I also describe why I am using C as the programming language,

then how to translate from to Fortran or Pascal from C, if you insist. The text has

twelve projects on computing, so I also summarize their purpose in this section.

Section 1.3 has remarks about the usefulness of graphics, of which there are many

in this book. Various ways in which the book may be used are suggested in Section 1.4, where I also point out the guideposts that are provided for you to navigate

through it. Finally, there is a list of general references and many references on

learning the C language, especially for those already familiar with Fortran or Pascal.

1.1

WHAT IS APPLICABLE MATHEMATICS?

Applicable mathematics covers a wide range of topics from diverse fields of mathematics and computing. In this section I summarize the main themes of this book.

First I emphasize my distinctions between programming and applications of programs, then I summarize the purpose of the diversion sections, some new paths to

1

2

INTRODUCTION

familiar destinations are then pointed out, and I conclude with remarks about common topics that I have omitted.

Analysis, numerics, and applications

In computing, the fields of analysis, numerics, and applications interact in complicated ways. I envisage the connections between the areas of mathematical analysis,

numerical methods, and computer programming, and their scientific applications as

shown schematically in Figure 1.1.

FIGURE 1.1 Mathematical analysis is the foundation upon which numerical methods and

computer programming for scientific and engineering applications are built.

You should note that the lines connecting these areas are not arrows flying upward. The demands of scientific and engineering applications have a large impact on

numerical methods and computing, and all of these have an impact on topics and

progress in mathematics. Therefore, there is also a downward flow of ideas and

methods. For example, numerical weather forecasting accelerates the development

of supercomputers, while topics such as chaos, string theory in physics, and neural

networks have a large influence on diverse areas of mathematics.

Several of the applications topics that I cover in some detail are often found in

books on mathematical modeling, such as the interesting books by Dym and Ivey,

by Meyer, and by Mesterton-Gibbons. However, such books usually do not emphasize much computation beyond the pencil-and-paper level. This is not enough

for scientists, since there are usually many experimental or observational data to be

handled and many model parameters to be estimated. Thus, interfacing the mathematical models to the realities of computer use and the experience of program writing

is mandatory for the training of scientists and engi neers. I hope that by working the

materials provided in this book you will become adept at connecting formalism to

practice.

1.1

WHAT IS APPLICABLE MATHEMATICS?

3

By comparison with the computational physics books by Koonin (see also Koonin and Meredith), and by Gould and Tobochnik, I place less emphasis on the physics and more emphasis on the mathematics and general algorithms than these authors

provide. There are several textbooks on computational methods in engineering and

science, such as that by Nakamura. These books place more emphasis on specific

problem-solving techniques and less emphasis on computer use than I provide here.

Data analysis methods, as well as mathematical or numerical techniques that may

be useful in data analysis, are given significant attention in this book. Examples are

spline fitting (Chapter 5), least-squares analyses (Chapter 6), the fast Fourier transform (Chapter 9), and convolutions (Chapter 10). My observation is that, as part

of their training, many scientists and engineers learn to apply data-analysis methods

without understanding their assumptions, formulation, and limitations. I hope to

provide you the opportunity to avoid these defects of training. That is one reason

why I develop the algorithms fairly completely and show their relation to other analysis methods. The statistics background to several of the data-analysis methods is

also provided.

Cooking school, then recipes

Those who wish to become good cooks of nutritious and enjoyable food usually go

to cooking school to learn and practice the culinary arts. After such training they are

able to use and adapt a wide variety of recipes from various culinary traditions for

their needs, employment, and pleasure. I believe that computing — including analysis, numerics, and their applications — should be approached in the same way. One

should first develop an understanding of analytical techniques and algorithms. After

such training, one can usually make profitable and enlightened use of numerical

recipes from a variety of sources and in a variety of computer languages.

The approach used in this book is therefore to illustrate the processes through

which algorithms and programming are derived from mathematical analysis of scientific problems. The topics that I have chosen to develop in detail are those that I believe contain elements common to many such problems. Thus, after working

through this book, when you tackle an unfamiliar computing task you will often recognize parts that relate to topics in this book, and you can then probably master the

task effectively.

Therefore I have not attempted an exhaustive (and exhausting) spread of topics.

To continue the culinary analogs, once you have learned how to make a good vanilla

ice cream you can probably concoct 30 other varieties and flavors. I prefer to examine various facets of each topic, from mathematical analysis, through appropriate numerical methods, to computer programs and their applications. In this book, therefore, you will usually find the presentation of a topic in this order: analysis, numerics, and applications.

The level I have aimed at for mathematical and computational sophistication, as

well as for scientific applications, is a middle ground. The applications themselves

do not require expert knowledge in the fields from which they derive, although I

4

INTRODUCTION

give appropriate background references. Although I present several topics that are

also in my earlier book, Computing in Applied Science (Thompson, 1984), the preliminary and review materials in this book are always more condensed, while the developments are carried further and with more rigor. The background level necessary

for the mathematics used in this book is available from mathematics texts such as

that by Wylie and Barrett, and also from the calculus text by Taylor and Mann.

Diversions and new routes

In order to broaden your scientific horizons and interests, I have included a few diversion sections. These are intended to point out the conceptual connections between the topic under development and the wider world of science, technology, and

the useful arts.

The diversions include a discussion of the interpretation of complex numbers (in

Section 2.5), the relationships between recursion in mathematics and computing

(Section 3.4), the development of computers and the growing use of spline methods

in data analysis (Section 5.6), and the Wilbraham-Gibbs overshoot in the Fourier

series of discontinuous functions (Section 9.6). Other connections are indicated in

subsections of various chapters. Although these diversions may not necessarily be

of much help in making you an expert in your field of endeavor, they will help you

to appreciate how your field fits into the larger scientific landscape.

This book also uses some seldom-traveled routes to reach known destinations,

as well as a few tracks that are quite new. These routes and their results include linearized square-root approximations (Section 3.3), the relations between maximum

likelihood, least squares, and Fourier expansion methods (Sections 6.1, 6.2, 9.2),

algorithms for least-squares normalization factors (Section 6.4), logarithmic transformations and parameter biases (Section 6.5), generalized logistic growth (Section 7.3), a novel approach to catenaries (Section 8.2), the discrete and integral

Fourier transforms of the complex-exponential function (Sections 9.2 and 10.2),

and the Wilbraham-Gibbs overshoot (Section 9.6).

Roads not taken

Because this book is directed to a readership of mathematicians, scientists, engineers, and other professionals who may be starting work in fields from which examples in this book are drawn, there are many byways that I have not ventured upon.

Rather, I emphasize principles and methods that are of both general validity and general applicability.

One road not taken is the one leading to topics from linear algebra (except incidentally) and matrix manipulation. Mathematics texts are replete with examples of

methods for 3 x 3 matrices, many of which will usually fail for matrices of typically interesting size of 100 x 100. I believe that matrix computation is best taught

and handled numerically by using the powerful and somewhat advanced methods

specially developed for computers, rather than methods that are simply extensions of

methods suitable for hand calculations on small matrices.

1.2

COMPUTING, PROGRAMMING, CODING

5

Symbolic calculation, also misleadingly termed “computer algebra,” is not discussed here either. I believe that it is dangerous to just “give it to the computer”

when mathematical analysis is necessary. Machines should certainly be used to

solve long and tedious problems reliably and to display results graphically. However, the human who sets up the problem for the computer should always understand clearly the problem that is being solved. This is not likely to be so if most of

one’s training has been through the wizardry of the computer. I have the same objection to the overuse of applications programs for numerical work until the principles have been mastered. (Of course, that’s one reason to set oneself the task of

writing a book such as this.) In spite of my warnings, when you have the appropriate understanding of topics, you should master systems for doing mathematics by

computer, such as those described in Wolfram’s Mathematica.

Finally, I have deliberately omitted descriptions of computational methods that

are optimized for specialized computer designs, such as vectorized or parallel architectures. You should leam and use these methods when you need them, but most of

them are developed from the simpler principles that I hope you will learn from this

book. You can become informed on both parallel computation and matrix methods

by reading the book by Modi.

1.2 COMPUTING, PROGRAMMING, CODING

When computing numerically there are three levels that I envision in problem analysis: program design, coding, and testing. They are best described by Figure 1.2.

FIGURE 1.2 Computing, programming, and coding form a nested hierarchy. An example of

this nesting activity is that of converting between coordinates (Section 2.6).

6

INTRODUCTION

In Figure 1.2 the activity of computing includes programming, which includes

coding. The right side of the figure shows the example of converting between Cartesian and polar coordinates — the programming project described in Section 2.6.

The aspects of computing that lie outside programming and coding are numerical

analysis and (to some degree) algorithm design. In the example, the formulas for

calculating coordinates are part of numerical analysis, while deciding how quadrants

are selected is probably best considered as part of algorithm design.

At the programming level one first has to decide what one wants to calculate, that

is, what output the program should produce. Then one decides what input is needed

to achieve this. One can then decide on the overall structure of the program; for example, the conversions for Cartesian and polar coordinates are probably best handled by separate branches in the program. At this level the choices of computing system and programming language usually become important.

Finally, as shown in Figure 1.2, one reaches the coding level. Here the program is built up from the language elements. In the C language, for example, functions are written or obtained from function libraries, variable types are declared, and

variables are shared between functions. Detailed instructions are coded, the interfaces to files or graphics are written, and the program is tested for corectness of formulas and program control, such as the method used to terminate program execution. If you think of the activity of computing as a nested three-level system, as

schematized in Figure 1.2, then you will probably produce better results faster than

if you let your thoughts and actions become jumbled together like wet crabs scuttling

in a fishing basket.

In the following parts of this section, I make remarks and give suggestions about

programming as described in this book. First, I justify my choice of C as the language for preparing the programs, then I give you some pointers for learning to program in C, and for translating the programs in this book to Fortran or Pascal from C

(if you insist on doing this). Next, I summarize programming aspects of the projects that occur toward the end of each of Chapters 2 through 10, and I remark on

the correctness and portability of these programs. Finally in this section, I draw

your attention to a convenience, namely, that all the programs and functions provided in this book are listed by section and topic in the index to computer programs

before the main index.

The C language for the programs

I decided to write the sample programs in C language for the following reasons.

First, C is a language that encourages clear program structure, which leads to programs that are readable. My experience is that C is easier to write than Pascal because the logic of the program is usually clearer. For example, the use of a topdown structure in the programs is closer to the way scientists and enginners tackle

real problems. In this aspect C and Fortran are similar. The C language is more demanding than Fortran, in that what you want to do and the meanings of the variables

must all be specified more accurately. Surprisingly, scientists (who pride themselves on precise language) often object to this demand from a computer. I estimate

1.2 COMPUTING, PROGRAMMING, CODING

7

that C is the easiest of the three languages in which to write programs that are

numerically oriented. Ease is indicated by the time it takes to produce a correctly

executing program in each language.

The second reason for using C in this book is that it is intermediate in complexity

between Fortran and Pascal, as illustrated by the comparison chart in the appendix.

That is, there are very few elements in the Fortran language (which makes it simple

to write but hard to understand), most of the elements of Fortran are in C, and some

of the elements of Pascal are also in C. For data handling, C is much more powerful

and convenient than Fortran because facilities for handling characters and strings

were designed into the original C language.

The third reason for my choice of C is that it is now the language of choice for

writing applications programs for workstations and personal computers. Therefore,

programs that you write in C for these machines will probably interface easily with

such applications programs. This is a major reason why C is used extensively in engineering applications.

A fourth reason for using C is that its developers have tried to make it portable

across different computers. Lack of portability has long been a problem with Fortran. Interconnectivity between computers, plus the upward mobility of programs

developed on personal computers and workstations to larger machines and supercomputers, demand program portability. Since very few large computer systems

have extensive support for Pascal, C is the current language of choice for portability.

One drawback of C lies with input and output. Some of the difficulty arises

from the extensive use of pointers in C, and some inconvenience arises from the limited flexibility of the input and output functions in the language. For these reasons, I

have written the input and output parts of the sample programs as simply as practicable, without any attempt to produce elegant formats. Since you probably want to

modify the programs to send the output to a file for processing by a graphics program, as discussed in Section 1.3, for this reason also such elegance is not worthwhile in the sample programs.

Complex numbers are not part of the C language, although they are used extensively in numerical applications, as discussed in Chapter 2. Our calculations that

use complex variables convert the complex numbers to pairs of real numbers, then

work with these. Extensive practice with programming using complex numbers in

this way is given in Sections 2.1 and 2.6. In Numerical Recipes in C, Press et al.

also discuss (Chapter 1 and Appendix E) handling complex numbers in C.

Learning to program in C

This book does not claim to be a guide to learning the C programming language. It

will, however, provide extensive on-the-job training for the programming of numerical applications in C. If you wish to learn how to program in C, especially for the

numerically oriented applications emphasized herein, there are several suitable textbooks. Starting with books which do not assume that you have much familiarity

with progamming then moving upward, there are Eliason’s C, a Practical Learning

Guide and Schildt’s Teach Yourself C, then the text by Darne11 and Margolis, C, a

8

INTRODUCTION

Software Engineering Approach. Many of C’s more subtle and confusing aspects

are described by Koenig in C Traps and Pitfalls.

If you are familiar with other programming languages and wish to use the C programs in this book, there are several texts that should be of considerable help to you.

For general use there is the book by Gehani, which emphasizes the differences between C and other procedural programming languages. Gehani also discusses the

advanced aspects of C as implemented on UNIX systems. A second cross-cultural

book that will help you with the language barrier is Kerrigan’s From Fortran to C,

which has extensive discussions and examples of how to learn C and to reprogram

from Fortran. The book by M?ldner and Steele, C as a Second Language, and that

by Shammas, Introducing C to Pascal Programmers, are especially suitable for those

who are familiar with Pascal but wish to learn to program effectively in C.

Finally, for detailed references on the C language there are C: A Reference Manual by Harbison and Steele, and The Standard C Library by Plauger. You should

also consult the programming manuals for C provided with the implementation of C

for your computing environment, and the manuals that explain the connection between C and your computer’s operating system.

The references on learning to program in the C language are listed together in the

reference section at the end of this chapter. The appendix provides examples of

translating between C, Fortran, and Pascal that are drawn from the C programs in

the first chapters.

Translating to Fortran or Pascal from C

By choosing to present the example programs and the project programs in C language, I know that I will have made a few friends but I may have alienated others.

Especially for the latter, I have tried to decrease their animosity by avoiding use of

some of the useful constructions in C that are sometimes not available or are awkward to implement in other numerically oriented procedural languages. This should

make the programs easier to translate on-the-fly into Fortran or Pascal. Among my

main concessions are the following.

In arrays the [0] element is usually not used by the program, so that the used elements of the array range from [1] upward. The only confusion this may cause when

programming is that the array size must be declared one larger than the maximum element that will ever be used. For example, if you want to be able to use elements

1...100, then the maximum array size (which is always defined as MAX) should be

101. This labeling starting with [1] usually also makes the correspondence between

the mathematics and the coding simpler, because most of the summation and iteration indices in formulas (k or j) begin with unity: any zeroth-index value in a

summation or iteration has usually to be treated specially. I use the [0] element in an

array only if this tightens the connections between the mathematical analysis, the algorithm, and the code.

In summations and indexing, the C construction of ++ to denote incrementing

by one, and similarly - - for decrementing, is not used except in for loops. Although general avoidance of ++ and - - is less efficient, it is less confusing when

translating to Fortran or Pascal, which do not allow such useful constructions.

1.2 COMPUTING, PROGRAMMING, CODING

9

The for loop in C is such a practical and convenient programming device that I

use it without concession to Fortran programmers, who are often confined to the

much clumsier DO loop. However, I use the for loop in a consistent style to which

you can readily adapt.

I have avoided go to statements, so there are no statement labels in the programs. Consequently, you will have to go elsewhere if your favorite computing

recipes include spaghetti. (The come from statement, which might rescue many a

programmer from distress, is also scrupulously avoided.) These omissions do not

make C programs difficult to write or to use.

There are a few operators in C, especially the logical operators, that look quite

different and may be confusing to Fortran and Pascal programmers. I explain these

operators where they appear in programs, especially in the early chapters. They are

listed with their Fortran and Pascal counterparts at the end of the appendix on translating between C, Fortran, and Pascal.

In C the exit function terminates execution when it is called. (Technically, it

terminates the calling process. All open output streams are flushed, all open files are

closed, and all temporary files are removed.) There is a conventional distinction,

which we follow, between exit (0) and exit (1) . The first is for successful termination and graceful exit, while the second is to signal an abnormal situation. In

some computing environments the process that refers to the terminating program

may be able to make use of this distinction.

Within the text, the font and style used to refer to names of programs, functions, and variables is 10-point Monaco (since all programming involves an element of gambling). All the programs and functions are listed in the index to computer programs, which is discussed below.

The computing projects and the programs

Several of the exercises and projects, as described in Section 1.1, require that you

modify programs that are in the text. By this means you will practice what is so

common in scientific and engineering computing, namely the assembling of tested

and documented stand-alone function modules to make a more powerful program

tailored for your use. One advantage of this method is that, provided you are careful

how you make the modifications, you will usually be able to check the integrity of

the program module by comparison with the stand-alone version.

The sample programs, both in the text and in the projects, are written for clarity

and efficiency of writing effort. In particular, when there are choices between algorithms, as in the numerical solution of differential equations, the different algorithms

are usually coded in-line so that it is easy for you to compare them. Therefore, if

you wish to transform one of the chosen sections of in-line code into a function you

will need to be careful, especially in the type declarations of variables used.

I have not attempted to make the sample programs efficient in terms of execution

speed or use of memory. If you want to use a particular computing technique for

production work, after you have understood an algorithm by exploring with the pro-

10

INTRODUCTION

grams provided, you should use a program package specially developed for the purpose and for the computer resources that you have available. At an intermediate

level of efficiency of your effort and computer time are the programs available (in C,

Fortran, and Pascal) as part of the Numerical Recipes books of Press et al.

My view of the connections among materials in this book, the C language, the

Numerical Recipes books, systems such as Mathematica, and their uses in scientific

applications is summarized in Figure 1.3.

FIGURE 1.3 Connections among topics in this book, C language, the Numerical Recipes

books, the Mathematica system, and scientific applications.

In Figure 1.3 the lines are connectors, not arrows. They indicate the strongest

two-way connections between the topics and books (names written in italics). Some

significant links have been omitted, mostly for topological reasons. For example,

many of the scientific applications examples in this book do not require C programs

or use of the Mathematica system. Also, much of the latter is programmed in C, and

it can convert its symbolic results into C (or Fortran) source code, as described in

Wolfram’s book on Mathematica.

Caveat emptor about the programs

The sample programs included in this book have been written as simply as practical

in order that they could readily be understood by the human reader and by the compiler. In order to keep the programs easy to read, I have not included extensive

checking of the allowed range of input variables, such as choices that control program options. My rule of thumb has been to put in a range check if I made an input

1.3 ONE PICTURE IS WORTH 1000 WORDS

11

error while testing a program, or if lack of a check is likely to produce confusing results. There are checks for array bounds if they are simple to code and do not interrupt program flow. Errors of use or input that are diagnosed by our C programs

always begin with a double exclamation, ! !, followed by an explanation of the error.

Program execution will often continue after some reasonable fix-up is attempted. A

typical fix-up is just to request another input for the troublesome variable.

Because the programs are written to be translated easily to Fortran or Pascal, as

described in a previous subsection and shown in the appendix, I have tried to avoid

nonstandard parts of C. The compiler that I use claims to follow ANSI standards. I

also checked for compatibility with the C language as described in the second edition

of Kernighan and Ritchie’s book.

In spite of all these precautions, I have two words of advice: caveat emptor — let

the buyer beware. The programs are supplied as is and are not guaranteed. For

each program you use, I suggest that you make at least the checks I have indicated.

If you can devise other tests of program correctness, I encourage you to do so.

The index to computer programs

Because this book has many computer programs with associated functions, I have

included an annotated index to all the programs and functions. They are listed, by

order of appearance, in the index that immediately precedes the regular index. The

programs and functions also appear alphabetically by name in the regular index.

1.3

ONE PICTURE IS WORTH 1000 WORDS

In this book graphical output is usually suggested as a way to improve the presentation of results, especially in the projects. Since graphics are so hardware dependent,

my suggestions for graphics in the projects are necessarily vague. You should familiarize yourself as much as practicable with techniques of graphical presentation.

If you have access to a powerful system that combines graphics and mathematics,

such as Mathematica as described by Wolfram or by Wagon, you may wish to develop some of the projects by using such a system.

Why and when you should use graphics

Tufte, in his two books on displaying and envisioning quantitative information, has

given very interesting discussions and examples of effective (and ineffective) ways

of displaying quantitative information from a variety of fields. In numerical applications of mathematics, graphics are especially important because of the enormous

number of numbers that come spewing out of the computer in a stream of numerical

environmental pollution. If there are many values to be compared, or if you want to

show trends and comparisons (as we usually do), it is worth the effort to write a

graphical interface for your program. If there are just a few check values to output,

12

INTRODUCTION

it is not worth the extra coding and possible lack of clarity that graphics may

produce.

If you have access to Mathematica or some other system that combines mathematics, numerics, and graphics, your learning will be enhanced if you combine the

three elements when working the exercises and projects. Wagon’s book provides

many examples of graphics techniques that would be useful in conjunction with this

workbook.

Impressive graphics, or practical graphics

In many books that relate to computing you will see elegant and impressive graphics

that have been produced by long runs on powerful computers using special-purpose

programs. Although these illustrations may improve your comprehension, and perhaps inspire you to become a computer-graphics artist, their production is usually

not practicable for most computer users. Therefore, I urge you to find a simple

graphics system that interfaces easily to your programming environment, that is

readily available to you, and that is inexpensive to use. For example, there are about

a hundred line drawings in this book. They were all produced by using only two

applications programs (one for graphics and one for drafting). The graphics program used input files that the C programs produced, so the numbers were seldom

touched by human hand, and the graphics output was produced on the same laser

printer that printed the text.

Many of the programs in this book produce simple output files. I most often

used these files for input to graphics, and sometimes for preparing tables. If you

make a similar interface and use it often to produce graphics (perhaps through the

intermediary of a spreadsheet), I think it will improve your comprehension of the

numerical results, without burdening you with much coding effort.

If you have convenient access to a state-of-the-art graphics system, it may be

useful for a few of the projects in this book. Just as I believe that an approach to numerical computing that is completely recipe-based is unwise, I believe that using

computer-graphics systems without an understanding of their background is similarly unwise. A comprehensive treatment of many aspects of computer graphics is

provided in the treatise by Foley et al. Methods for preparing high-resolution

graphics, and how to implement them in Pascal, are described in the book by Angell

and Griffith.

1.4 SUGGESTIONS FOR USING THIS BOOK

This book may be used for both self-study and for class use. I have some suggestions that should help you to make most effective use of it. First I indicate the conections between the remaining nine chapters, then there are remarks about the exercises and the projects.

1.4 SUGGESTIONS FOR USING THIS BOOK

13

Links between the chapters

Because we cover a large amount of territory and a variety of scientific and engineering landscapes in this book, it is useful to have an indication of the connections between its nine other chapters. Table 1.1 summarizes the strength of the links between the chapters.

TABLE 1.1 Cross-reference chart for use of this book.

Key:

chapter above is necessary preparation

chapter above is desirable preparation

chapter above is optional preparation

Complex variables

Power series

Numerical derivatives and integrals

Fitting curves through data

Least-squares analysis of data

Introduction to differential equations

Second-order differential equations

Discrete Fourier transforms and series

Fourier integral transforms

For example, if you plan to work through Chapter 7 (introduction to differential

equations), use of Chapters 2, 5, and 6 is optional, Chapter 3 (power series) is desirable, whereas Chapter 4 (numerical derivatives and integrals) is necessary preparation. Within each chapter you should read not only the text, but also the exercises,

which are embedded in the text. Exercises containing equations are especially important to be read, since these equations often form part of the development. Therefore, read over every exercise, even if you don’t work it through in detail.

The exercises and projects

Since this book has an overwhelming number of exercises, many of them nontrivial,

a guide to use of the exercises is appropriate. It will be clear to you that I always insert an exercise whenever I don’t want to show you all the steps of a development.

This is not laziness on my part, because I assure you that I have worked through every step. Rather, an exercise provides a checkpoint where you should pause, take

stock of what you have been reading, then test your understanding by trying the exercise. If you have difficulty with this exercise, reread all of the subsection

containing the exercise, even past the troublesome exercise. Then work the exercise

14

INTRODUCTION

once more. I believe that by doing this you will have a realistic estimate of your progress in comprehension. If you are using this book for self-study, this procedure

should help you considerably.

Some exercises are more than checkpoints, they are crossroads where concepts

and techniques developed previously are focused on relevant and interesting problems. This type of exercise, which often appears in a project toward the ends of

chapters, is always indicated. Such exercises provide very good tests of your overall comprehension of the material in the current and previous chapters.

The projects, of which there is at least one per chapter after this introductory

chapter, are designed to bring together many of the aspects of analysis and numerics

emphasized within the chapter. They provide you with opportunities to explore the

numerics and the science by using the number-crunching and graphical powers of

computers. Programming aspects of the projects are discussed in Section 1.2.

REFERENCES FOR THE INTRODUCTION

General references

Angell, I. O., and G. Griffith, High-Resolution Computer Graphics Using Pascal,

Macmillan Education, Basingstoke, England, 1988.

Dym, C. L., and E. S. Ivey, Principles of Mathematical Modeling, Academic Press,

New York, 1980.

Foley, J. D., A. van Dam, S. K. Feiner, and J. F. Hughes, Computer Graphics,

Addison-Wesley, Reading, Massachusetts, third edition, 1990.

Gould, H. and J. Tobochnik, An Introduction to Computer Simulation Methods,

Addison-Wesley, Reading, Massachusetts, 1988.

Koonin, S. E., Computational Physics, Addison-Wesley, Redwood City,

California, 1986.

Koonin, S. E., and D. C. Meredith, Computational Physics: FORTRAN Version,

Addison-Wesley, Redwood City, California, 1990.

Mesterton-Gibbons, M., A Concrete Approach to Mathematical Modelling,

Addison-Wesley, Reading, Massachusetts, 1989.

Meyer, W. J., Concepts of Mathematical Modeling, McGraw-Hill, New York,

1984.

Modi, J. J., Parallel Algorithms and Matrix Computation, Clarendon Press, Oxford,

England, 1988.

Nakamura, S., Computational Methods in Engineering and Science, WileyInterscience, New York, 1986.

Taylor, A. E., and W. R. Mann, Advanced Calculus, Wiley, New York, third edition, 1983.

Thompson, W. J., Computing in Applied Science, Wiley, New York, 1984.

REFERENCES FOR THE INTRODUCTION

15

Tufte, E. R., The Visual Display of Quantitative Information, Graphics Press,

Cheshire, Connecticut, 1983.

Tufte, E. R., Envisioning Information, Graphics Press, Cheshire, Connecticut,

1990.

Wagon, S., Mathematica in Action, W. H. Freeman, New York, 1991.

Wolfram, S., Mathematica: A System for Doing Mathematics by Computer,

Addison-Wesley, Redwood City, California, second edition, 1991.

Wylie, C. R., and L. C. Barrett, Advanced Engineering Mathematics, McGrawHill, New York, fifth edition, 1982.

References on learning and using C

Darnell, P. A., and P. E. Margolis, C, a Software Engineering Approach, SpringerVerlag, New York, 1991.

Eliason, A. L., C, a Practical Learning Guide, Macmillan, New York, 1988.

Gehani, N., C: An Advanced Introduction, Computer Science Press, Rockville,

Maryland, 1985.

Harbison, S. P., and G. L Steele, Jr., C: A Reference Manual, Prentice Hall,

Englewood Cliffs, New Jersey, third edition, 199 1.

Kernighan, B. W., and D. M. Ritchie, The C Programming Language, Prentice

Hall, Englewood Cliffs, New Jersey, second edition, 1988.

Kerrigan, J. F., From Fortran to C, Windcrest Books, Blue Ridge Summit,

Pennsylvania, 199 1.

Koenig, A., C Traps and Pitfalls, Addison-Wesley, Reading, Massachusetts, 1989.

Miildner, T., and P. W. Steele, C us a Second Language, Addison-Wesley,

Reading, Massachusetts, 1988.

Plauger, P. J., The Standard C Library, Prentice Hall, Englewood Cliffs, New

Jersey, 1992.

Press, W. H., B. P. Flannery, S. A. Teukolsky, and W. T. Vetterling, Numerical

Recipes in C, Cambridge University Press, New York, 1988.

Schildt, H., Teach Yourself C, Osborne McGraw-Hill, Berkeley, California, 1990.

Shammas, N., Introducing C to Pascal Programmers, Wiley, New York, 1988.

16

Previous Home

Chapter 2

A REVIEW OF COMPLEX VARIABLES

The purpose of this chapter is to review your understanding of complex numbers

and complex variables, and to summarize results that are used extensively in subsequent chapters. Complex variables are treated elegantly and completely in many

mathematics texts, but a treatment in which the aim is to develop intuition in the scientific applications of complex numbers may have much more modest goals, and is

best done from a geometrical perspective, rather than from an analytic or algebraic

viewpoint.

We start with the algebra and arithmetic of complex numbers (in Section 2.1)

including a simple program, then turn in Section 2.2 to the complex-plane representation because of its similarities to plane-polar coordinates and to planar vectors.

The simplest (and most common) functions of complex variables-complex exponentials and hyperbolic functions- are reviewed in Section 2.3. Phase angles, vibrations, and complex-number representation of waves, which are all of great interest to scientists and engineers, are summarized in Section 2.4 before we take a diversion in Section 2.5 to discuss the interpretation of complex numbers. Project 2,

which includes program Cartesian & Polar Coordinate Interconversion

for converting between plane-polar and Cartesian coordinates, concludes the text of

this chapter. This program serves to emphasize the ambiguities in calculating polar

coordinates from Cartesian coordinates, and it will be useful in the later programming applications. References on complex numbers complete the chapter.

The discussion of complex variables is limited to the above topics, and does not

develop extensively or with any rigor the topics of analytic functions in the complex

plane, their differentiation, or their integration. Although several derivations later in

this book, especially those involving integrals, would be simplified if the methods of

contour integration were used, the methods of derivation used here are usually direct

and do not require the extra formalism of contours and residues. Readers who have

experience with functions of a complex variable will often be able to substitute their

own methods of proof, which may be more direct than those provided here.

17

Next

18

COMPLEX VARIABLES

Many of the examples and exercises in this chapter anticipate steps in our later

developments that use complex variables, especially the material on Fourier expansions (Chapters 9 and 10). Since we always have a reference to the later material,

you may wish to look ahead to see how the complex variables are used.

2.1

ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

In this section we summarize the algebraic properties of complex numbers, their properties for numerical computation, the relation between complex numbers and plane

geometry, and the special operations on complex numbers — complex conjugation,

modulus, and argument.

When you have reviewed this section, you will have the conceptual and technical

skills for the more extensive developments of complex numbers that are presented in

the remainder of this chapter. In particular, Project 2 — the program for converting

between coordinates (Section 2.6) -requires most of the ideas from this section.

If you are experienced with complex variables, you may try the project before working this section. If you have difficulties with the mathematics in the project (as distinguished from the programming involved), return and rework this section.

The algebra of complex numbers

We indicate a complex number, z, symbolically by

(2.1)

in which x and y are understood to be both real numbers. The sign + in this formula does not mean arithmetic addition, although it has many rules that are similar to

those for addition. You will have already encountered yet another meaning of + as

a sign used in the addition of vectors, which is also distinct from arithmetic addition.

In (2.1), the symbol i has the property that

(2.2)

with a unique value being assumed for i itself. In engineering contexts it is more

usual to find the symbol i replaced by the symbol j, thus avoiding possible confusion when complex numbers are used to describe currents (i) in electrical circuits, as

in our Section 8.1. We will use i, recalling its origins in the initial letter of the historical term “imaginary.”

Complex numbers may also be thought of as pairs of numbers, in which the order in the pair is significant. Thus we might write

(2.3)

analogously to vectors in a plane. Just as the coordinates (x, y) and (y,x) are usually distinct, so are the analogous complex numbers. The notation in (2.3) avoids

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

19

ambiguities in using the + sign and in the necessity of inventing a symbol satisfying (2.2). Further, many of the rules for manipulating complex numbers have a

strong similarity to those for vectors in a plane if the notation (2.3) is used. Although we will write our results for complex-number algebra and arithmetic in the

notation (2.1), you are invited to try the number-pair notation in Exercise 2.1 (c).

This notation is also used in some programming languages that allow complexarithmetic operations.

The rules for manipulating complex numbers must be consistent with those for

purely real numbers ( y = 0) and for purely imaginary numbers (x = 0). In the following, let z = x + iy generically, and let z1 = x1+ iy1, z2 = x2 + i y2 represent two particular complex numbers. Then the following properties hold:

(2.4)

Negation of a complex number is defined by

(2.5)

which is often written casually as

(2.6)

A complex number is zero only if both its real and imaginary parts are zero, which is

consistent with zero being the only solution of the equation z = -z.

Addition or subtraction of two complex numbers is accomplished by

(2.7)

Multiplication of complex numbers is performed by

(2.8)

Reciprocal of a complex number is defined by

(2.9)

which has the property that z (1/z) = 1, as for the arithmetic of real numbers.

Division of one complex number into another is based on the reciprocal of the divisor, and is therefore undefined if the divisor is zero:

(2.10)

20

COMPLEX VARIABLES

In order to check your comprehension of these rules for complex arithmetic, try

the following exercise.

Exercise 2.1

(a) Verify that the rules (2.7) through (2.10) are consistent with those for real

arithmetic by checking them for y1 = y2 = 0.

(b) Check the consistency of (2.7) through (2.10) for purely imaginary numbers

by setting x1= x2 = 0 and noting the condition on i, (2.2).

(c) Use the notation for complex numbers as ordered-number pairs, as indicated

by (2.3), to write down the preceding complex-arithmetic rules, (2.4) through

(2.10). n

Now that we have summarized the formal basis of complex-variable algebra, it is

time to consider complex arithmetic, especially for computer applications.

Programming with complex numbers

Few computer languages are designed to include complex-variable types in their

standard definition. They are available in Fortran, but not in C or Pascal. In Wolfram’s Mathematica system for doing mathematics by computer, which has both

symbolic and numeric capabilities, complex numbers can be handled readily. An

introduction to their use is provided in Section 1.1 of Wolfram’s book.

To appreciate why computer hardware is not built and computer software is not

designed to assume that numbers they handle are complex, consider the following

exercise.

Exercise 2.2

Show that the total number of real-arithmetic operations needed for complexnumber addition and subtraction is 2, the number for multiplication is 6, and the

number for division is 11 or 14, depending on whether or not the divisor in

(2.9) is stored. n

We now show a simple program in C language for performing complex arithmetic by the rules given in the preceding subsection. The purpose of this program is

twofold: if you are unfamiliar with the C language the program will provide a simple

introduction, while it will also develop your understanding of complex arithmetic.

The program Complex-Arithmetic Functions takes as input x1, y1, x2, y2

for the components of two complex numbers z1 and z2. After checking that both

numbers are nonzero, it calls the functions for addition, subtraction, multiplication,

and division, namely CAdd, CSub, CMult, and CDiv, then prints the results before

returning for more input. Here is the program.

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

21

PROGRAM 2.1 Functions for performing complex arithmetic; addition, subtraction, multiplication, and division.

#include

#include

main ()

{

/* Complex-Arithmetic Functions */

double

x1,x2,y1,y2,x1a2,y1a2,x1S2,y1S2,x1m2,y1m2,x1d2,y1d2;

void CAdd(),CSub(),CMult(),CDiv();

printf("Complex-Arithmetic Functions\n");

x1 = 1; y1 = 1; x2 = 1; y2 = 1;

/* Check that at least one complex numbers is not zero */

while ( xl !=0 || yl != 0 || x2 != 0 || y2 != 0 )

printf("\nInput x1,y1,x2,y2 (all zero to end):\n");

scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);

if ( xl == 0 && y1 == 0 && x2 == 0 && y2 == 0 )

printf("\nEnd

exit(0);

}

Complex-Arithmetic

Functions");

CAdd(xl,yl,x2,y2,&x1a2,&y1a2); /* complex Add */

/* returns x1a2, y1a2 */

CSub(xl,yl,x2,y2,&x1s2,&y1s2); /* complex Subtract */

/* returns x1s2, yls2 */

CMult(x1,y1,x2,y2,&x1m2,&y1m2);

/* complex Multiply */

/* returns x1m2, y1m2 */

CDiv(x1,yl,x2,y2,&x1d2,&y1d2); /* complex Divide */

/* returns x1d2, y1d2 */

printf("\nz1+z2=(%lf) + i(%lf)",x1a2,y1a2);

printf("\nz1-z2=(%lf) + i(%lf)",x1s2,y1s2);

printf("\nz1*z2=(%lf) + i(%lf)",X1m2,y1m2);

printf("\nz1/z2=(%lf) + i(%lf)\n",x1d2,y1d2);

22

COMPLEX VARIABLES

void CAdd(x1,y1,x2,y2,x1a2,y1a2)

/* Complex Addition function */

double x1,y1,x2,y2,*x1a2,*y1a2;

{

*x1a2 = x1+x2; *y1a2 = y1+y2;

void CSub(x1,y1,x2,y2,x1s2,y1s2)

/* Complex Subtraction function */

double x1,y1,x2,y2,*x1s2,*y1s2;

{

*x1s2 = xl-x2; *y1s2 = y1-y2;

}

void CMult(x1,y1,x2,y2,x1m2,y1m2)

/* Complex Multiplication function */

double x1,y1,x2,y2,*x1m2,*y1m2;

{

*x1m2 = x1*x2-y1*y2;

*y1m2 = x1*y2+y1*x2;

}

void CDiv(x1,y1,x2,y2,x1d2,y1d2)

/* Complex Division function */

double x1,y1,x2,y2,*x1d2,*y1d2;

{

double den;

den = x2*x2+y2*y2;

if ( den == 0 )

{

printf("!! CDiv denominator = 0; dividend set to zero");

*x1d2 = 0; *y1d2 = 0;

}

else

{

*x1d2 = (x1*x2+y1*y2)/den;

*y1d2 = (y1*x2-x1*y2)/den;

The program reveals an immediate difficulty with modifying a language to include complex variables, in that two values must be returned by a complex-variable

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

23

function. In C this cannot be done simply by a conventional function (which returns

just one value, at most). One can get around the problem by using the indirection

(dereferencing) operator, written as an asterisk (*) preceding a variable name, as

used for each of the two real-variable values returned by the program functions.

Here are some suggestions for exploring complex numbers by using the program Complex-Arithmetic Functions.

Exercise 2.3

(a) Use several pairs of real numbers for both inputs ( y1 = 0, y2 = 0 ) in order to verify that the complex numbers contain real numbers as special cases.

(b) Input purely imaginary numbers ( xl = 0, x2 = 0 ) to the program and

verify the correctness of the arithmetic.

(c) Show by a careful analytical proof that if the product of two complex numbers is zero, then at least one of the complex numbers is identically zero (both

real and imaginary parts zero). Prove that if one of a pair of complex numbers is

zero, their product is zero. Verify this by using the program. n

With this background of algebraic and arithmetic properties of complex numbers, we are prepared to review some more formal definitions and properties.

Complex conjugation, modulus, argument

In complex-variable algebra and arithmetic one often needs complex quantities that

are related by reversal of the sign of just their imaginary parts. We therefore have

the operation called complex conjugation. In mathematics texts the notation for this

operation is often denoted by a bar, - , while other scientists often use an asterisk,

as * . In the latter notation the complex-conjugate value of z is

z*=x-iy

(2.11)

z=x+iy

(2.12)

if and only if

From the definition of complex conjugation we can readily derive several interesting

results.

Exercise 2.4

(a) Prove that

(2.13)

where the notation Re stands for “real part of.”

24

COMPLEX VARIABLES

(b) Similarly, prove that

(2.14)

where Im denotes “imaginary part of.”

(c) Derive the following properties of complex conjugation:

(2.15)

(2.17)

which show that complex conjugation is distributive over addition, subtraction,

multiplication, and division. n

The identity

zz*=x2 y2

(2.18)

shows that z z * is zero only if z is identically zero, which is an example of the condition from Exercise 2.3 (c) for vanishing of the product of two complex numbers.

The frequent occurrence of z z * and its connection with vectors in two dimensions lead to the notation of the modulus of a complex number z, denoted by

(2.19)

Thus mod z indicates the magnitude of z if we picture it as a vector in the x - y

plane. Another name for modulus is absolute value. For example, the modulus of a

real number is just its value without regard to sign, that is, its absolute value. The

modulus of a pure imaginary number is just the value of y without regard to sign.

The modulus of a complex number is zero if and only if both its real and its imaginary part are zero.

The argument of a complex number is introduced similarly to polar coordinates

for two-dimensional vectors. One defines the arg function for a complex variable

by the requirement that

(2.20)

and the requirement that

25

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

(2.21)

which are necessary and sufficient conditions for definition of arg z to within a

multiple of 27

Exercise 2.5

Explain why the commonly given formula

(2.22)

is not sufficient to specify arg z uniquely, even to within a multiple of 27

n

In Section 2.6, in the program to convert between coordinates, we return to this

problem of ambiguous angles. The argument of a complex number is sometimes

called the phase angle, or (confusingly) the amplitude.

One aspect of the argument relates to the analogy between complex variables and

planar vectors. If the pair (x,y) formed the components of a vector, then arg z

would be the angle that the vector makes with the positive x axis. For example,

arg (Re z) = ± arg i = /2, and arg (-i) = - / 2 .

A program for complex conjugate and modulus

For applications with complex variables it is worthwhile to have available programs

for complex functions. We provide here a program that invokes functions returning

complex conjugate and modulus values. The more-involved coding for the argument function is provided in the programming project, Section 2.6. Here is the program Conjugate & Modulus Functions.

PROGRAM 2.2 Conjugate and modulus functions for complex numbers.

#include

#include

main()

{

/* Conjugate & Modulus Functions */

double x,y,xc,yc, zmod;

void CConjugate();

double CModulus();

26

COMPLEX VARIABLES

printf("Complex Conjugate & Modulus Functions\n");

x = 1; y = 1;

/* Check for x not zero or y not zero */

while ( x != 0 || y != 0 )

{

printf("\nInput x,y (both zero to end):\n");

scanf("%lf%lf",&x,&y) ;

if ( x == 0 && y == 0 )

{

printf("\nEnd Conjugate & Modulus Functions");

exit (0) ;

}

CConjugate(x,y,&xc,&yc); /* conjugate x+iy */

zmod = CModulus(x,y); /* modulus of x+iy */

printf("z* = (%lf) + i(%lf)",xc,yc);

printf("\nmod [(%lf) + i(%lf)] = %lf\n",x,y,zmod);

}

void CConjugate(x,y,xc,yc)

/* Complex Conjugation function */

double x,y,*xc,*yc; /* xc & yc are returned */

{

*xc = x; "yc = -y;

}

double CModulus(x,y)

/* Complex Modulus function */

double x,y;

{

double mod;

mod = sqrt (x*x+y*y);

return mod;

Some remarks on programming the functions CConjugate and CModulus in

the C language are in order:

1. Note that complex conjugation performs an operation on a complex number, albeit a simple one. So it does not return a value in the sense of a C function value.

Therefore, the “function” CConjugate is declared to be “void.” The value of the

2.2 THE COMPLEX PLANE AND PLANE GEOMETRY

27

complex conjugate is returned in the argument list of CConjugate as xc and yc,

which are dereferenced variables (preceded by a *, which should not be confused

with complex conjugation, or even with / * and * / used as comment terminators).

2. On the other hand, CModulus is declared as “double” because it is a function

which returns a value, namely mod (inside CModulus), which is assigned to zmod

within the main program. Note that CModulus might also be used within an arithmetic statement on the right-hand side of the = sign for zmod.

3. The program continues to process complex-number pairs while the input number

pair is nonzero. If the zero-valued complex number (x = 0 and y = 0 ) is entered,

the program exits gracefully by exit (0) rather than with the signal of an ungraceful

termination, exit (1).

With this background to programming complex conjugation and modulus, plus

the program for arguments in Section 2.6, you are ready to compute with complex

variables.

Exercise 2.6

Run several complex-number pairs through the program Conjugate & Modulus

Functions. For example, check that the complex conjugate of a complex-conjugate number produces the original number. Also verify that the modulus values of (x, y) and (y, x) are the same. ?

2.2

THE COMPLEX PLANE AND PLANE GEOMETRY

In the preceding section on algebra and computing with complex numbers we had

several hints that there is a strong connection between complex-number pairs (x, y)

and the coordinates of points in a plane. This connection, formally called a “mapping,” is reinforced when we consider successive multiplications of z = x + i y

by i itself.

Exercise 2.7

(a) Show that if z is represented by (x, y), then i z is (-y, x), then i2z is

(-x,-y), i3z is (y,-x), and i4z regains (x , y).

(6) Verify that in plane-polar geometry these coordinates are displaced from

each other by successive rotations through / 2, as shown in Figure 2.1. n

The geometric representation of complex numbers shown in Figure 2.1 is variously known as the complex plane, the Argand diagram, or the Gauss plane. The

relations between rotations and multiplication with complex numbers are summarized in the following subsections.

28

COMPLEX VARIABLES

FIGURE 2.1 Rotations of complex numbers by /2 in the complex plane. Note that rotations

do not change the length of a complex number, as indicated by the dashed circle.

Cartesian and plane-polar coordinates

Before reviewing the connections between the complex plane and plane geometry,

let us recall some elementary relations between Cartesian and plane-polar coordinates. If the polar coordinates are r, the (positive) distance from the origin along a

line to a point in the plane and the angle (positive in the anticlockwise direction)

that this line makes with the x axis, then the Cartesian coordinates are given as

in a right-handed coordinate system. The polar coordinates are indicated in Figure 2.1. Inverting these equations to determine r and which is not as trivial as it

may look, is discussed in the first part of Section 2.6.

In the complex plane we may therefore write z as

(2.24)

so that the modulus, which is also the radius, is given by

2.2 THE COMPLEX PLANE AND PLANE GEOMETRY

29

(2.25)

and the polar angle with respect to the x axis is given by

(2.26)

The principal value of the polar angle is the smallest angle lying between

and

Such a specification of the principal value allows unique location of a point

in the complex plane. Other choices that limit may also be encountered, for

example, the range 0 to 27.

Complex conjugation is readily accomplished by reflecting from to since

(2.27)

In the language of physics, z, and its complex conjugate are related through a parity

symmetry in a two-dimensional space.

With this angular representation of complex variables, we can derive several interesting results.

De Moivre’s theorem and its uses

A theorem on multiplication of complex numbers in terms of their polar-coordinate

representations in the complex plane was enunciated by Abraham De Moivre (16671754). We derive his theorem as follows. Suppose that we have two complex

numbers. the first as

(2.28)

and the second as

(2.29)

Their product can be obtained by using the trigonometric identities for expanding cosines and sines of sums of angles, to obtain

(2.30)

From this result we see that multiplication of complex numbers involves conventional multiplication of their moduli, the r1r2 part of (2.30), and addition of their angles.

30

COMPLEX VARIABLES

Therefore, multiplication in the complex plane, as well as addition, can readily be

shown, as in Figure 2.2.

FIGURE 2.2 Combination of complex numbers in the complex plane. The complex numbers

and their sum are indicated by the dashed lines, while their product is shown by the solid line.

Equation (2.30) can be generalized to the product of n complex numbers, as the

following exercise suggests.

Exercise 2.8

(a) Prove by the method of mathematical induction that for the product of n

complex numbers, z1,z2, ..., zn, one has in polar-coordinate form

(2.31)

(b) From this result, setting all the complex numbers equal to each other, prove

that for the (positive-integer) nth power of a complex number

(2.32)

which is called De Moivre’s theorem. n

This remarkable theorem can also be proved directly by using induction on n.

Reciprocation of a complex number is readily performed in polar-coordinate

form, and therefore so is division, as you may wish to show.

2.3 FUNCTIONS OF COMPLEX VARIABLES

31

Exercise 2.9

(a) Show that for a nonzero complex number, z, its reciprocal in polar-coordinate form is given by

(2.33)

(b) From the result in (a) show that the quotient of two complex numbers can be

written in polar-coordinate form as

(2.34)

where it is assumed that r2 is not zero, that is, z2 is not zero. n

Thus the polar-coordinate expressions for multiplication and division are much simpler than the Cartesian-coordinate forms, (2.8) and (2.10).

Although we emphasized in Exercise 2.2 that complex-number multiplication

and division in Cartesian form are much slower than such operations with real numbers, these operations may be somewhat speedier in polar form, especially if several

numbers are to be multiplied. An overhead is imposed by the need to calculate cosines and sines. Note that such advantages and disadvantages also occur when using logarithms to multiply real numbers.

2.3 FUNCTIONS OF COMPLEX VARIABLES

In the preceding two sections we reviewed complex numbers from algebraic, computational, and geometric viewpoints. The goal of this section is to summarize how

complex variables appear in the most common functions, particularly the exponential, the cosine and sine, and the hyperbolic functions. We also introduce the idea of

trajectories of functions in the complex plane.

Complex exponentials: Euler’s theorem

In discussing De Moivre’s theorem at the end of the preceding section we noticed

that multiplication of complex numbers may be done by adding their angles, a procedure analogous to multiplying exponentials by adding their exponents, just the

procedure used in multiplication using logarithms. Therefore, there is probably a

connection between complex numbers in polar-coordinate form and exponentials.

This is the subject of Euler’s theorem.

A nice way to derive Euler’s theorem is to write

(2.35)

32

COMPLEX VARIABLES

then to note the derivative relation

(2.36)

But the general solution of an equation of the form

(2.37)

is given by

(2.38)

Exercise 2.10

Show, by identifying (2.36) and (2.37) with the result in (2.38), that = i.

Then choose a special angle, say = 0, to show that = 1. Thus, you have

proved Euler’s theorem,

(2.39)

which is a remarkable theorem showing a profound connection between the geometry and algebra of complex variables. n

It is now clear from Euler’s theorem why multiplication of complex numbers involves addition of angles, because the angles are added when they appear in the exponents. Now that we have the formal derivation of Euler’s theorem out of the way,

it is time to apply it to interesting functions.

Applications of Euler’s theorem

There are several interesting and practical results that follow from Euler’s theorem

and the algebra of complex numbers that we reviewed in Section 2.1. The trigonometric and complex-exponential functions can be related by noting that, for real angles,

(2.40)

which follows by taking complex conjugates on both sides of (2.39). On combining

(2.39) and (2.40) for the cosine we have

(2.41)

while solving for the sine function gives

2.3 FUNCTIONS OF COMPLEX VARIABLES

33

(2.42)

Both formulas are of considerable usefulness for simplifying expressions involving

complex exponentials.

Exercise 2.11

Use the above complex-exponential forms of the cosine and sine functions to

prove the familiar trigonometric identity

(2.43)

the familiar theorem of Pythagoras. n

Although our derivation of Euler’s theorem does not justify it, since is assumed to

be real in the differentiation (2.36) the theorem holds even for being a complex

variable. Thus the Pythagoras theorem also holds for complex .

Some remarkable results, which are also often useful in later chapters, are found

if multiples of /2 are inserted in Euler’s identity, (2.39). Derive them yourself.

Exercise 2.12

Use Euler’s theorem to show that

(2.44)

which gives the values for successive rotations in the complex plane by r/2.

Compare these results with Figure 2.1. n

The exponential form

is generally much more symmetric and therefore is

easier to handle analytically than are the cosine and sine functions, with their awkward function changes and sign changes upon differentiation compared with the

simplicity of differentiating the complex exponential. This simplicity is very powerful when used in discussing the solution of differential equations in Chapter 8, and

also in deriving the fast Fourier transform (FFT) algorithm in Chapter 9.3.

An interesting application of the complex-exponential function that anticipates its

use in the FFT algorithm is made in the following exercise.

Exercise 2.13

Consider the distributive and recurrence properties of the complex-exponential

function defined by

(2.45)

(a) Prove the following properties of powers of E(N):

34

COMPLEX VARIABLES

(2.46)

(2.47)

for any a, b, and for any p 0.

(b) Using these results, show that if N = 2V, where v is a positive integer,

then, no matter how many integer powers of E(N) are required, only one evaluation of this complex-exponential function is required. n

As a further topic in our review of functions of complex variables, let us consider the hyperbolic and circular functions.

Hyperbolic functions and their circular analogs

Exponential functions with complex arguments are required when studying the solutions of differential equations in Chapters 7 and 8. A frequently occurring combination is made from exponentially damped and exponentially increasing functions.

This leads to the definition of hyperbolic functions, as follows.

The hyperbolic cosine, called “cosh,” is defined by

(2.48)

while the hyperbolic sine, pronounced “sinsh,” is defined by

(2.49)

If u is real, then the hyperbolic functions are real-valued. The name “hyperbolic”

comes from noting the identity

(2.50)

in which, if u describes the x and y coordinates parametrically by

(2.5 1)

then an x - y plot is a rectangular hyperbola with lines at /4 to the x and y axes

as asymptotes.

Exercise 2.14

(a) Noting the theorem of Pythagoras,

(2.52)

2.3 FUNCTIONS OF COMPLEX VARIABLES

35

for any (complex) u, as proved in Exercise 2.11, explain why the cosine and

sine functions are called “circular” functions.

(b) Derive the following relations between hyperbolic and circular functions

(2.53)

a n d

(2.54)

valid for any complex-valued u. n

These two equations may be used to provide a general rule relating signs in identities

for hyperbolic functions to identities for circular functions:

An algebraic identity for hyperbolic functions is the same as that for circular

functions, except that in the former the product (or implied product) of two sinh

functions has the opposite sign to that for two sin functions.

For example, given the identity for the circular functions

(2.55)

we immediately have the identity for the hyperbolic functions

(2.56)

Exercise 2.15

Provide a brief general proof of the hyperbolic-circular rule stated above. n

Note that derivatives, and therefore integrals, of hyperbolic and circular functions do

not satisfy the above general rule. The derivatives of the hyperbolic functions are

given by

(2.57)

and by

(2.58)

in both of which the real argument, u, is in radians. There is no sign change on differentiating the hyperbolic cosine, unlike the analogous result for the circular cosine.

36

COMPLEX VARIABLES

The differential equations satisfied by the circular and hyperbolic functions also

differ by signs, since the cosine and sine are solutions of

(2.59)

which has oscillatory solutions, whereas the cosh and sinh are solutions of

(2.60)

which has solutions exponentially increasing or exponentially decreasing.

Exercise 2.16

(a) Prove the two derivative relations (2.57) and (2.58) by starting with the defining equations for the cosh and sinh.

(b) Use the relations between hyperbolic and circular functions, (2.53) and

(2.54), to compute the derivatives of the hyperbolic functions in terms of those

for the circular functions.

(c) Verify the appropriateness of the circular and hyperbolic functions as solutions of the differential equations (2.59) and (2.60), respectively. n

To complete the analogy with the circular functions, one also defines the hyperbolic tangent, called “tansh,” by

(2.6 1)

which is analogous to the circular function, the tangent, defined by

(2.62)

Among these six hyperbolic and circular functions, for real arguments there are

three that are bounded by ±1 (sin, cos, tanh) and three that are unbounded (sinh,

cosh, tan). Therefore we show them in a pair of figures, Figures 2.3 and 2.4, with

appropriate scales.

By displaying the bounded hyperbolic tangent on the same scale as the sine function in Figure 2.3, we notice an interesting fact- these two functions are equal to

within 10% for | x | < 2, so may often be used nearly interchangeably. The explanation for their agreement is given in Section 3.2, where their Maclaurin series are

presented. Figure 2.4 shows a similar near-coincidence of the cosh and sinh functions for x > 1.5, where they agree to better than 10% and the agreement improves

as x increases because they both tend to the exponential function.

2.3

FUNCTIONS OF COMPLEX VARIABLES

37

FIGURE 2.3 Bounded circular and hyperbolic functions, sine, cosine, and hyperbolic tangent.

FIGURE 2.4 The unbounded circular and hyperbolic functions, tangent, hyperbolic cosine, and

hyperbolic sine. For x greater than about 1.5, the latter two functions are indistinguishable on the

scale of this figure.

The tangent function is undefined in the limit that the cosine function in the denominator of its definition (2.62) tends to zero. For example, in Figure 2.4 values

of the argument of the tangent function within about 0.1 of x = ± /2 have been

omitted.

38

COMPLEX VARIABLES

Trajectories in the complex plane

Another interesting concept and visualization method for complex quantities is that

of the trajectory in the complex plane. It is best introduced by analogy with particle

trajectories in two space dimensions, as we now summarize.

When studying motion in a real plane one often displays the path of the motion,

called the trajectory, by plotting the coordinates x (t) and y (t), with time t being

the parameter labeling points on the trajectory. For example, suppose that

x (t) = A cos ( t) and y (t) = B sin ( ), with A and B positive, then the trajectory is an ellipse with axes A and B, and it is symmetric about the origin of the X- y

coordinates. As t increases from zero, x initially decreases and y initially increases. One may indicate this by labeling the trajectory to indicate the direction of increasing t. The intricate Lissajous figures in mechanics, obtained by superposition

of harmonic motions, provide a more-involved example of trajectories.

Analogously to kinematic trajectories, in the complex plane real and imaginary

parts of a complex-valued function of a parameter may be displayed. For example,

in Section 8.1 we discuss the motion of damped harmonic oscillators in terms of a

real dimensionless damping parameter Expressed in polar-coordinate form, the

amplitude of oscillation is

(2.63)

where the complex “frequency” (if x represents time) is given by

(2.64)

The trajectory of v depends on the range of

square root in (2.64).

Exercise 2.17

(a) Show that if

and on the sign associated with the

1, which gives rise to damped oscillatory motion, then

(2.65)

and that the trajectory of v+ is a counterclockwise semicircle in the upper half

plane, while the trajectory of v- is a clockwise semicircle in the lower half

plane. In both trajectories is given as increasing from -1 to +l.

(b) Suppose that >1, which produces exponential decay called overdamped

motion. Show that v± is then purely real and negative, so the trajectory lies

along the real axis. Show that v- increases from -1 toward the origin as increases, while v+ decreases toward as

increases. n

The complex-plane trajectory, with

as displayed in Figure 2.5.

as parameter, expressed by (2.64) is therefore

2.3 FUNCTIONS OF COMPLEX VARIABLES

39

FIGURE 2.5 Frequency trajectory in the complex plane according to (2.64) as a function of the

damping parameter

As a final note on this example, there is no acceptable solution for v- if < - 1

and if x > 0 is considered in (2.63), since y (x) is then divergent.

Another interesting example of a trajectory in the complex plane arises in the

problem of forced oscillations (Section 10.2) in the approximation that the energy

dependence is given by the Lorentzian

(2.66)

where c is a proportionality constant and the complex Lorentzian amplitudes L± are

given by

(2.67)

where and

are dimensionless frequency and damping parameters. The analysis of this example is similar to the first one.

40

COMPLEX VARIABLES

FIGURE 2.6 Frequency trajectory in the complex plane for the Lorentzian amplitude described

by (2.67).

Exercise 2.18

(a) Show that the Lorentzian amplitudes in (2.67) satisfy

(2.68)

so that the trajectories of L± lie on circles of radius l/2 in the complex plane.

(b) Investigate the details of the trajectory by showing that L+ describes the anticlockwise semicircle in the lower half plane, while L- describes the clockwise

semicircle in the upper half complex plane. For both of these trajectories the directions are for going from

to

n

The trajectories of the Lorentzian amplitudes are shown in Figure 2.6. They are

discussed more completely in Chapter 3 of the text by Pippard in the context of

Cole-Cole plots of complex-valued dielectric constants as a function of frequency.

In both of our examples the trajectories in the complex plane lie on circles. This

is neither a mere coincidence nor is it uncommon, as the following exercise should

convince you.

2.4 PHASE ANGLES, VIBRATIONS, AND WAVES

41

Exercise 2.19

Consider the following complex function z, called a linear fractional transformation of the real variable p according to

(2.69)

, and are complex constants, with

in which

0. Now consider the

z’

that

is

obtained

from

z

by

the

shift

and

scaling

transformation

function

(2.70)

By analogy with the result in Exercise 2.18, argue that z’ lies on a circular trajectory and therefore that the original z in (2.69) lies on a circular trajectory. n

Thus, the functions (2.67) and (2.69) are both special cases of the more general circular trajectory given by (2.70). From these examples we see that the notion of a

trajectory in the complex plane is useful for visualizing the properties of complexvalued functions.

2.4 PHASE ANGLES, VIBRATIONS, AND WAVES

The angle in the complex plane often has interesting interpretations in scientific

applications, particularly in the context of vibrations and waves. In this section we

summarize some of the main results. An encyclopedic treatment is provided in Pippard’s book on the physics of vibration.

The topics introduced here are developed and applied throughout this book. In

particular, Section 8.1 discusses free-motion and resonant vibrations in mechanical

and electrical systems, then the quantum oscillator is considered briefly in Section 8.5. In Chapters 9 and 10 we develop Fourier expansions, emphasizing the

complex-exponential treatment for the discrete, series, and integral expansions.

Phase angles and phasors

Suppose that we have a (real) angle = , where is a constant angular frequency,

= 2 f (with f the frequency) and t denotes time. Then

(2.71)

42

COMPLEX VARIABLES

describes in the complex plane uniform circular motion of the point z1, while the

projections onto the real and imaginary axes (x and y) describe simple harmonic motions.

Exercise 2.20

Prove that the motion of z1 is periodic by showing that

(2.72)

where the period T = 2

n

If a second uniform circular motion in the complex plane is described by

(2.73)

then this motion has the same period as that described by z1, but at a given time z2

has its phase advanced by over the phase of zl.

Whether one refers to a positive value of

as a lag or a lead depends on the scientific field in which one is working. If

> 0, in mechanics the phase of z2 is said

to lug that of zl, whereas in electrical-circuit applications z2 is said to lead zl.

A complex-plane diagram showing the magnitude of z and a relative phase (with

t usually suppressed) is called a vibration diagram or phasor diagram. Its use

gives a visualization of complex-variable relationships which often improves comprehension and interpretation.

Vibrations and waves

We can broaden the discussion of phases to include both spatial as well as temporal

variation in the amplitude of a complex vibration. For example, a wave that has constant amplitude of unity at all points along the x direction and at all times t can be described by

(2.74)

in which the wavenumber, k, is given in terms of wavelength, ?, by

(2.75)

Although the wavenumber is physically a less intuitive quantity than is the wavelength, computationally and in most applications k is a much simpler quantity to deal

with. Note that k has the dimensions of an inverse length, just as the angular frequency, has the dimensions of inverse time. Thus, the argument of the exponential function in (2.74) is dimension-free, as should be the argument of any function

in mathematics.

2.5 DIVERSION: INTERPRETING COMPLEX NUMBERS

43

Recall also that the wave described by (2.74) is monochromatic (unique values

of and k) and that points of constant phase have an associated phase velocity, vp,

given by

(2.76)

Exercise 2.21

Discuss from the viewpoint of wave motion why vp in (2.76) is called the phase

velocity. n

The superposition of such waves of constant phase to build up a dispersive wave in

which components with different frequencies transport energy at different speeds is

an extension of the Fourier expansions in Chapters 9 and 10. A comprehensive and

lucid discussion is given by Baldock and Bridgeman in their book on wave motion.

In Chapters 9 and 10 on Fourier expansions we make detailed study of phenomena described in terms of x or in terms of the complementary variable k, or in terms

oft and its complementary variable

Clear expositions of the relations between

complex exponentials and vibrations are given in detail with many applications in

Pippard’s omnibus book. Vibrations and waves are described very completely at an

introductory level in the book by Ingard.

2.5 DIVERSION: INTERPRETING COMPLEX NUMBERS

The development of the interpretation of complex numbers provides an example of

the consequences of education and of the dominance of scientific thought by mathematical representations. Since many scientists claim that a phenomenon is not understood until it can be described mathematically, it is interesting to discuss the relation

between “the queen and servant of science” and the natural sciences.

Are complex numbers real?

Before the quantum physics revolution of the 192Os, scientists usually apologized

for using complex numbers, since they provided only mathematically convenient

shortcuts and shorthand for problem solving. Indeed, Leonard Euler of Euler’s theorem in Section 2.3 coined the Latin “imaginarius” for the quantity i = . The

first major use of complex numbers was made by C. P. Steinmetz (1865-1923), a

research electrical engineer who used them extensively (as we do in Section 8.1) to

simplify the analysis of alternating-current circuits.

In quantum mechanics, for example in the Schr?dinger equation that we use in

Section 8.5, the wave function is fundamentally a complex variable that is not a

shorthand for two real quantities such as magnitude and phase. Many quantities derived from wave functions, such as scattering amplitudes, are also intrinsically

complex-valued. This leads to the scientific use of “analytic continuation,” a concept

and technique familiar in mathematics but of more recent use in the natural sciences.

44

COMPLEX VARIABLES

Analytic continuation

We have displayed in Figures 2.3 and 2.4 the circular functions and the hyperbolic

functions, respectively. In terms of complex variables, however, these hyperbolic

functions are essentially just the circular functions evaluated for purely imaginary arguments, or vice versa. It is therefore interesting, and sometimes useful, to think of

there being just a single set of functions, say the circular functions, which may be

evaluated along the real axis (then they are the conventional trigonometric functions)

or they may be evaluated along the imaginary axis (then they are the hyperbolic

functions, within factors of i), or they may be evaluated for the argument which

takes on a value anywhere in the complex plane.

When we make this last bold step off either the real or the imaginary axis and

into the complex plane we are making an analytic continuation of the functions.

The concept of analytic continuation and some understanding of the techniques

applied to it are best appreciated by working the following exercise.

Exercise 2.22

Consider the behavior of the complex-valued function of complex-variable argument, Z, defined as follows:

(2.77)

(a) Show that for a real argument A is just the hyperbolic cosine function discussed in Section 2.3, while for purely imaginary z it is the circular cosine

function.

(b) Sketch the graph of cos x along the real axis of the complex-z plane and

the graph of cosh y along the imaginary axis of the same plane. They look

quite different, don’t they?

(c) Devise a graphical representation of A (z) that is suitable for arbitrary complex z, and make some representative sketches of the function thus graphed.

One possible form of representation is to sketch contours of constant Re A and

of constant ImA. n

In scientific research analytic continuation is often a useful technique. As an example, experiments on wave scattering (such as in acoustics, optics, electromagnetism, and subatomic physics) are, at best, obtained in the range of scattering angles from zero to . How would the data look if they could be analytically continued into the complex-angle plane? Similarly, data obtained at real energies or frequencies may be interesting to extrapolate to complex energies or complex frequencies. Indeed, we explore this possibility in discussing the Lorentzian resonances in

Section 10.2.

2.6 PROJECT 2: PROGRAM TO CONVERT BETWEEN COORDINATES

2.6

45

PROJECT 2: PROGRAM TO CONVERT

BETWEEN COORDINATES

The program Cartesian & Polar Coordinate Interconversion

developed in

this project serves both to develop your understanding of the relations between these

two coordinate systems and to give you practice with writing programs in C.

The conversion from plane-polar to Cartesian coordinates is straightforward and

unambiguous. Given r and one has immediately (as discussed in Section 2.2)

(2.78)

which can be programmed directly.

Stepping into the correct quadrant

The transformation from Cartesian coordinates to polar coordinates is less direct than

the inverse transformation just considered. The required formulas are

(2.79)

which is straightforward to compute, and, in terms of the atan or tan-l function,

(2.80)

which is ambiguous. This formula does not uniquely determine the quadrant in

which lies because only the sign of the quotient in (2.80) is available after the division has been made. The relative signs of the circular functions in the four quadrants indicated in Figure 2.7 may be used to determine the angle uniquely from the

signs of x and y.

In some programming languages, including C, two functions are available for

the inverse tangent. In one, such as the atan (t) in C language (with t the argument of the function), the angle is usually returned in the range /2 to /2, and the

user of this function has to determine the appropriate quadrant by other means.

In the second function for the inverse tangent, such as atan2 (y, x) in the C

language, the angle is located in the correct quadrant by the function itself. If we

were to use at atan2 in the program, the conversion from Cartesian to polar representation would be very direct. For practice in C and to reinforce your understanding of

plane-polar coordinates we use the simpler function atan.

46

COMPLEX VARIABLES

FIGURE 2.7 Signs of the circular functions in each quadrant of the Cartesian plane.

If we begin with an angle calculated into the first quadrant by using the absolute

value of y/x, then multiples of /2 have to be added or subtracted to get the angle

into the correct quadrant. Computationally, the best way to get at the accuracy of

your

COMPUTING FOR

SCIENTISTS AND

ENGINEERS

COMPUTING FOR

SCIENTISTS AND

ENGINEERS

A Workbook of Analysis,

Numerics, and Applications

WILLIAM J. THOMPSON

University of North Carolina

Chapel Hill, North Carolina

A Wiley-Interscience Publication

JOHN WILEY & SONS, INC.

New York / Chichester / Brisbane / Toronto / Singapore

This text is printed on acid-free paper.

Copyright © 1992 by John Wiley & Sons, Inc.

All rights reserved. Published simultaneously in Canada.

Reproduction or translation of any part of this work

beyond that permitted by Section 107 or 108 of the

1976 United State Copyright Act without the permission

of the copyright owner is unlawful. Requests for

permission or further information should be addressed to

the Permissions Department, John Wiley & Sons, Inc.,

605 Third Avenue, New York, NY 10158-0012

Library of Congress Cataloging in Publication Data:

Thompson, William J. (William Jackson), 1939 Computing for Scientists and Engineers : a workbook of analysis,

numerics, and applications /William J. Thompson.

p.

cm.

Rev. ed. of: Computing in applied science. c1984.

“A Wiley-Interscience publication.”

Includes bibliographical references and indexes.

ISBN O-471-54718-2 (cloth)

1. Numerical analysis-Data processing. 2. Science-Data

processing. 3. Engineering-Data processing. I. Thompson, William

J. (William Jackson), 1939- Computing in applied science.

II. Title.

QA297.T5 1992

519.4–dc20

92-16744

Printed in the United States of America

10 9 8 7 6 5 4 3 2

PREFACE

This preface is addressed to readers who are interested in computing but who seldom either consult manuals or read prefaces. So, I will be brief.

Computing requires an integrated approach, in which scientific and mathematical

analysis, numerical algorithms, and programming are developed and used together.

The purpose of this book is to provide an introduction to analysis, numerics, and

their applications. I believe that a firm grounding in the basic concepts and methods

in these areas is necessary if you wish to use numerical recipes effectively. The

topics that I develop extensively are drawn mostly from applied mathematics, the

physical sciences, and engineering. They are divided almost equally among review

of the mathematics, numerical-analysis methods (such as differentiation, integration,

and solution of differential equations from the sciences and engineering), and dataanalysis applications (such as splines, least-squares fitting, and Fourier expansions).

I call this a workbook, since I think that the best way to learn numerically oriented computing is to work many examples. Therefore, you will notice and, I hope,

solve many of the exercises that are strewn throughout the text like rocks in the

stream of consciousness. I try to introduce you to a technique, show you some of

the steps, then let you work out further steps and developments yourself. I also

suggest new and scenic routes rather than overtraveled highways. There are occasional diversions from the mainstream, often to point out how a topic that we are developing fits in with others in computing and its applications.

The programming language in which I present programs is C. This language is

now used extensively in systems development, data-acquisition systems, numerical

methods, and in many engineering applications. To accommodate readers who prefer Fortran or Pascal, I have used only the numerically oriented parts of C and I have

v

vi

structured the programs so that they can usually be translated line-by-line to these

other languages if you insist. The appendix summarizes the correspondences between the three languages. All programs and the functions they use are listed in the

index to programs. The fully-developed programs are usually included in the projects near the end of each chapter.

This book may be used either in a regular course or for self-study. In a onesemester course for college students at the junior level or above (including graduate

students), more than half the topics in the book could be covered in detail. There is

adequate cross-referencing between chapters, in addition to references to sources for

preparation and for further exploration of topics. It is therefore possible to be

selective of topics within each chapter.

Now that we have an idea of where we’re heading, let’s get going and compute!

William J. Thompson

Chapel Hill, July 1992

CONTENTS

1. Introduction to Applicable Mathematics and Computing

1.1 What is applicable mathematics? 1

1

Analysis, numerics, and applications 2

Cooking school, then recipes 3

Diversions and new routes 4

Roads not taken 4

1.2 Computing, programming, coding 5

The C language for the programs 6

Learning to program in C 7

Translating to Fortran or Pascal from C 8

The computing projects and the programs 9

Caveat emptor about the programs 10

The index to computer programs 11

1.3

One picture is worth 1000 words 11

Why and when you should use graphics 11

Impressive graphics, or practical graphics 12

1.4 Suggestions for using this book 12

Links between the chapters 13

The exercises and projects 13

References for the introduction 14

General references 14

References on learning and using C 15

2. A Review of Complex Variables

2.1 Algebra and computing with complex numbers 18

17

The algebra of complex numbers 18

Programming with complex numbers 20

Complex conjugation, modulus, argument 23

A program for complex conjugate and modulus 25

vii

CONTENTS

viii

2.2 The complex plane and plane geometry 27

Cartesian and plane-polar coordinates 28

De Moivre’s theorem and its uses 29

2.3 Functions of complex variables 31

Complex exponentials: Euler’s theorem 3 1

Applications of Euler’s theorem 3 2

Hyperbolic functions and their circular analogs 3 4

Trajectories in the complex plane 38

2.4 Phase angles, vibrations, and waves 41

Phase angles and phasors 4 1

Vibrations and waves 42

2.5 Diversion: Interpreting complex numbers 43

Are complex numbers real? 43

Analytic continuation 44

2.6

Project 2: Program to convert between coordinates 45

Stepping into the correct quadrant 45

Coding, testing, and using the program 46

References on complex numbers 49

3. Power Series and Their Applications

3.1 Motivation for using series: Taylor’s theorem 51

The geometric series 52

Programming geometric series 53

Alternating series 56

Taylor’s theorem and its proof 58

Interpreting Taylor series 59

3.2 Taylor expansions of useful functions 60

Expansion of exponentials 61

Computing the exponential series 62

Series for circular functions 65

Inverse circular functions 70

Hyperbolic function expansions 71

Logarithms in series expansions 72

Series expansion of x In(x) 73

3.3 The binomial approximation 76

Deriving the binomial approximation 76

Applications of the binomial approximation 7 8

Linearized square-root approximations 78

Financial interest schemes 80

3.4

Diversion: Repetition in mathematics and computing 83

Iteration 84

Recurrence 84

Recursion 84

3.5

Project 3: Testing the convergence of series 85

Coding and checking each series expansion 85

Including the hyperbolic functions 91

File output and graphics options 92

The composite program for the functions 92

Using the program to test series convergence 97

References on power series 98

51

CONTENTS

4. Numerical Derivatives and Integrals

4.1 The working function and its properties 100

ix

99

Properties of the working function 100

A C function for Homer’s algorithm 103

Programming the working function 106

4.2

Discrete data and numerical mathematics 110

The discreteness of data 110

Numerical mathematics 111

4.3 Numerical noise in computing 111

Roundoff and truncation errors 112

Unstable problems and unstable methods 114

Errors from subtractive cancellation 116

Program for roots of quadratic equations 119

4.4 How to approximate derivatives 122

Forward-difference derivatives 123

Derivatives by central differences 125

Numerical second derivatives 126

Better algorithms for second derivatives 128

4.5

Project 4A: Computing derivatives numerically 130

Derivatives of the exponential function 130

Differentiating the cosine function 132

4.6 Numerical integration methods 133

Trapezoid formula and program for integration 135

Simpson formula and program for integrals 140

Integrals with cosines 143

Higher-order polynomial integration 144

4.7

Project 4B: Electrostatic potential from a charged wire 145

Potentials by analytical integration 147

Potentials by numerical-integration methods 148

References on numerical derivatives and integrals 151

5. Fitting Curves through Data

5.1 How to fit curves using splines 154

What is a spline? 154

Properties for spline fits 156

Deriving the spline equations 156

The spline algorithm 158

5.2 Boundary conditions for spline fitting 159

Natural splines 160

5.3

Project 5: Program for spline fitting 161

The main program, Cubic Splines 166

The function SplineFit 167

5.4 Interpolating by splines 168

Interpolating values and derivatives 168

The C function Splinelnterp 169

Interpolating working-function values and derivatives 170

Interpolating cosine values and derivatives 173

153

CONTENTS

x

5.5 Integration methods using splines 175

Deriving the integration algorithm 175

The C function for spline integration 176

Integrating the working function and cosine 177

5.6 Diversion: Computers, splines, and graphics 178

References on spline fitting 179

6. Least-Squares Analysis of Data

6.1 Introduction to the least-squares criterion 182

181

Maximum likelihood and least squares 182

Least squares and the objective function 185

6.2

Orthogonal functions and linear least squares 185

What are orthogonal functions? 186

Orthogonality and least squares 188

6.3

Errors in both variables: Straight-line least squares 190

Weighting models 190

Constant ratio of weights 192

Properties of the least-squares slopes 196

6.4 Least-squares normalization factors 199

Normalizing

Normalizing

The best-fit

Program for

6.5

fitting-function values to data 200

data to fitting values 201

objective function 203

normalizing factors 204

Logarithmic transformations and parameter biases 208

The origin of bias 209

Probability analysis for bias 210

Dependence of bias on error distribution 212

6.6

Project 6: Program for straight-line least-squares fits 214

Organization of Straight-Line Least Squares 214

Testing and using the least-squares program 217

References on least-squares analysis 218

7. Introduction to Differential Equations

7.1 Differential equations and physical systems 222

Why are there differential equations? 222

Notation and classification 223

Homogeneous and linear equations 224

Nonlinear differential equations 225

7.2 First-order linear equations: World-record sprints 225

Kinematics of world-record sprints 226

Warming up to the problem 227

Program for analyzing sprint data 229

Women sprinters are getting faster 234

7.3 Nonlinear differential equations: Logistic growth 235

The logistic-growth curve 235

Exploring logistic-growth curves 238

Generalized logistic growth 239

221

CONTENTS xi

7.4 Numerical methods for first-order equations 241

Presenting error values 241

Euler predictor formulas 242

Testing the Euler predictors 244

Adams predictor formulas 245

7.5 Project 7: Program for solving first-order equations 247

Programming the differential equation solver 247

Exploring numerical first-order equations 252

References on first-order equations 255

8. Second-Order Differential Equations

8.1 Forces, second-order equations, resonances 258

257

Forces and second-order equations 258

Mechanical and electrical analogs 259

Solving and interpreting free-motion equations 261

Forced motion and resonances 265

8.2 Catenaries, cathedrals, and nuptial arches 269

The equation of the catenary 270

Catenaries of various shapes and strengths 273

Demonstrating arches 278

Practical arches and catenaries 279

8.3 Numerical methods for second-order differential equations 279

Euler-type algorithms for second-order equations 280

Removing first derivatives from second-order linear equations 284

Deriving the Noumerov algorithm for second-order equations 2 8 5

8.4 Project 8A: Progamming second-order Euler methods 287

Programming the Euler algorithms 287

Euler algorithms and the exponential function 291

Euler algorithms and the cosine function 293

8.5 Project 8B: Noumerov method for linear second-order equations 294

Programming the Noumerov method 294

Testing Noumerov for exponentials and cosines 2 9 7

The quantum harmonic oscillator 299

Noumerov solution of the quantum oscillator 301

8.6 Introduction to stiff differential equations 304

What is a stiff differential equation? 305

The Riccati transformation 306

Programming the Riccati algorithm 308

Madelung’s transformation for stiff equations 311

References on second-order equations 312

9. Discrete Fourier Transforms and Fourier Series

9.1 Overview of Fourier expansions 316

The uses of Fourier expansions 316

Types and nomenclature of Fourier expansions 316

315

CONTENTS

xii

9.2

Discrete Fourier transforms 3 18

Derivation of the discrete transform 318

Properties of the discrete transform 320

Exponential decay and harmonic oscillation 322

9.3

The fast Fourier transform algorithm 329

Deriving the FFT algorithm 329

Bit reversal to reorder the FFT coefficients 332

Efficiency of FFT and conventional transforms 333

9.4 Fourier series: Harmonic approximations 334

From discrete transforms to series 334

Interpreting Fourier coefficients 336

Fourier series for arbitrary intervals 336

9.5 Some practical Fourier series 337

The square-pulse function 338

Program for Fourier series 340

The wedge function 343

The window function 345

The sawtooth function 347

9.6 Diversion: The Wilbraham-Gibbs overshoot 349

Fourier series for the generalized sawtooth 350

The Wilbraham-Gibbs phenomenon 353

Overshoot for the square pulse and sawtooth 356

Numerical methods for summing trigonometric series 359

9.7

Project 9A: Program for the fast Fourier transform 360

Building and testing the FFT function 360

Speed testing the FFT algorithm 364

9.8

Project 9B: Fourier analysis of an electroencephalogram 365

Overview of EEGs and the clinical record 365

Program for the EEG analysis 368

Frequency spectrum analysis of the EEG 372

Filtering the EEG data: The Lanczos filter 373

References on Fourier expansions 375

10. Fourier Integral Transforms

10.1 From Fourier series to Fourier integrals 377

The transition from series to integrals 378

Waves and Fourier transforms 379

Dirac delta distributions 379

10.2 Examples of Fourier transforms 380

Exponential decay and harmonic oscillation 380

The square-pulse function 383

Fourier transform of the wedge function 384

Gaussian functions and Fourier transforms 386

Lorentzian functions and their properties 389

Fourier integral transform of a Lorentzian 391

10.3 Convolutions and Fourier transforms 393

Convolutions: Definition and interpretation 393

Convoluting a boxcar with a Lorentzian 394

Program for convoluting discretized functions 398

377

CONTENTS

xiii

Fourier integral transforms and convolutions 401

Convolutions of Gaussians and of Lorentzians 402

Convoluting Gaussians with Lorentzians: Voigt profile 406

10.4 Project 10: Computing and applying the Voigt profile 411

The numerics of Dawson’s integral 412

Program for series expansion of profile 413

Program for direct integration of profile 418

Application to stellar spectra 419

References on Fourier integral transforms 419

EPILOGUE

421

APPENDIX: TRANSLATING BETWEEN C, FORTRAN,

AND PASCAL LANGUAGES

423

INDEX TO COMPUTER PROGRAMS

429

INDEX

433

COMPUTING FOR

SCIENTISTS AND

ENGINEERS

Previous Home Next

Chapter 1

INTRODUCTION TO APPLICABLE

MATHEMATICS AND COMPUTING

The major goal for you in using this book should be to integrate your understanding,

at both conceptual and technical levels, of the interplay among mathematical analysis, numerical methods, and computer programming and its applications. The purpose of this chapter is to introduce you to the major viewpoints that I have about

how you can best accomplish this integration.

Beginning in Section 1.1, is my summary of what I mean by applicable mathematics and my opinions on its relations to programming. The nested hierarchy (a

rare bird indeed) of computing, programming, and coding is described in Section 1.2. There I also describe why I am using C as the programming language,

then how to translate from to Fortran or Pascal from C, if you insist. The text has

twelve projects on computing, so I also summarize their purpose in this section.

Section 1.3 has remarks about the usefulness of graphics, of which there are many

in this book. Various ways in which the book may be used are suggested in Section 1.4, where I also point out the guideposts that are provided for you to navigate

through it. Finally, there is a list of general references and many references on

learning the C language, especially for those already familiar with Fortran or Pascal.

1.1

WHAT IS APPLICABLE MATHEMATICS?

Applicable mathematics covers a wide range of topics from diverse fields of mathematics and computing. In this section I summarize the main themes of this book.

First I emphasize my distinctions between programming and applications of programs, then I summarize the purpose of the diversion sections, some new paths to

1

2

INTRODUCTION

familiar destinations are then pointed out, and I conclude with remarks about common topics that I have omitted.

Analysis, numerics, and applications

In computing, the fields of analysis, numerics, and applications interact in complicated ways. I envisage the connections between the areas of mathematical analysis,

numerical methods, and computer programming, and their scientific applications as

shown schematically in Figure 1.1.

FIGURE 1.1 Mathematical analysis is the foundation upon which numerical methods and

computer programming for scientific and engineering applications are built.

You should note that the lines connecting these areas are not arrows flying upward. The demands of scientific and engineering applications have a large impact on

numerical methods and computing, and all of these have an impact on topics and

progress in mathematics. Therefore, there is also a downward flow of ideas and

methods. For example, numerical weather forecasting accelerates the development

of supercomputers, while topics such as chaos, string theory in physics, and neural

networks have a large influence on diverse areas of mathematics.

Several of the applications topics that I cover in some detail are often found in

books on mathematical modeling, such as the interesting books by Dym and Ivey,

by Meyer, and by Mesterton-Gibbons. However, such books usually do not emphasize much computation beyond the pencil-and-paper level. This is not enough

for scientists, since there are usually many experimental or observational data to be

handled and many model parameters to be estimated. Thus, interfacing the mathematical models to the realities of computer use and the experience of program writing

is mandatory for the training of scientists and engi neers. I hope that by working the

materials provided in this book you will become adept at connecting formalism to

practice.

1.1

WHAT IS APPLICABLE MATHEMATICS?

3

By comparison with the computational physics books by Koonin (see also Koonin and Meredith), and by Gould and Tobochnik, I place less emphasis on the physics and more emphasis on the mathematics and general algorithms than these authors

provide. There are several textbooks on computational methods in engineering and

science, such as that by Nakamura. These books place more emphasis on specific

problem-solving techniques and less emphasis on computer use than I provide here.

Data analysis methods, as well as mathematical or numerical techniques that may

be useful in data analysis, are given significant attention in this book. Examples are

spline fitting (Chapter 5), least-squares analyses (Chapter 6), the fast Fourier transform (Chapter 9), and convolutions (Chapter 10). My observation is that, as part

of their training, many scientists and engineers learn to apply data-analysis methods

without understanding their assumptions, formulation, and limitations. I hope to

provide you the opportunity to avoid these defects of training. That is one reason

why I develop the algorithms fairly completely and show their relation to other analysis methods. The statistics background to several of the data-analysis methods is

also provided.

Cooking school, then recipes

Those who wish to become good cooks of nutritious and enjoyable food usually go

to cooking school to learn and practice the culinary arts. After such training they are

able to use and adapt a wide variety of recipes from various culinary traditions for

their needs, employment, and pleasure. I believe that computing — including analysis, numerics, and their applications — should be approached in the same way. One

should first develop an understanding of analytical techniques and algorithms. After

such training, one can usually make profitable and enlightened use of numerical

recipes from a variety of sources and in a variety of computer languages.

The approach used in this book is therefore to illustrate the processes through

which algorithms and programming are derived from mathematical analysis of scientific problems. The topics that I have chosen to develop in detail are those that I believe contain elements common to many such problems. Thus, after working

through this book, when you tackle an unfamiliar computing task you will often recognize parts that relate to topics in this book, and you can then probably master the

task effectively.

Therefore I have not attempted an exhaustive (and exhausting) spread of topics.

To continue the culinary analogs, once you have learned how to make a good vanilla

ice cream you can probably concoct 30 other varieties and flavors. I prefer to examine various facets of each topic, from mathematical analysis, through appropriate numerical methods, to computer programs and their applications. In this book, therefore, you will usually find the presentation of a topic in this order: analysis, numerics, and applications.

The level I have aimed at for mathematical and computational sophistication, as

well as for scientific applications, is a middle ground. The applications themselves

do not require expert knowledge in the fields from which they derive, although I

4

INTRODUCTION

give appropriate background references. Although I present several topics that are

also in my earlier book, Computing in Applied Science (Thompson, 1984), the preliminary and review materials in this book are always more condensed, while the developments are carried further and with more rigor. The background level necessary

for the mathematics used in this book is available from mathematics texts such as

that by Wylie and Barrett, and also from the calculus text by Taylor and Mann.

Diversions and new routes

In order to broaden your scientific horizons and interests, I have included a few diversion sections. These are intended to point out the conceptual connections between the topic under development and the wider world of science, technology, and

the useful arts.

The diversions include a discussion of the interpretation of complex numbers (in

Section 2.5), the relationships between recursion in mathematics and computing

(Section 3.4), the development of computers and the growing use of spline methods

in data analysis (Section 5.6), and the Wilbraham-Gibbs overshoot in the Fourier

series of discontinuous functions (Section 9.6). Other connections are indicated in

subsections of various chapters. Although these diversions may not necessarily be

of much help in making you an expert in your field of endeavor, they will help you

to appreciate how your field fits into the larger scientific landscape.

This book also uses some seldom-traveled routes to reach known destinations,

as well as a few tracks that are quite new. These routes and their results include linearized square-root approximations (Section 3.3), the relations between maximum

likelihood, least squares, and Fourier expansion methods (Sections 6.1, 6.2, 9.2),

algorithms for least-squares normalization factors (Section 6.4), logarithmic transformations and parameter biases (Section 6.5), generalized logistic growth (Section 7.3), a novel approach to catenaries (Section 8.2), the discrete and integral

Fourier transforms of the complex-exponential function (Sections 9.2 and 10.2),

and the Wilbraham-Gibbs overshoot (Section 9.6).

Roads not taken

Because this book is directed to a readership of mathematicians, scientists, engineers, and other professionals who may be starting work in fields from which examples in this book are drawn, there are many byways that I have not ventured upon.

Rather, I emphasize principles and methods that are of both general validity and general applicability.

One road not taken is the one leading to topics from linear algebra (except incidentally) and matrix manipulation. Mathematics texts are replete with examples of

methods for 3 x 3 matrices, many of which will usually fail for matrices of typically interesting size of 100 x 100. I believe that matrix computation is best taught

and handled numerically by using the powerful and somewhat advanced methods

specially developed for computers, rather than methods that are simply extensions of

methods suitable for hand calculations on small matrices.

1.2

COMPUTING, PROGRAMMING, CODING

5

Symbolic calculation, also misleadingly termed “computer algebra,” is not discussed here either. I believe that it is dangerous to just “give it to the computer”

when mathematical analysis is necessary. Machines should certainly be used to

solve long and tedious problems reliably and to display results graphically. However, the human who sets up the problem for the computer should always understand clearly the problem that is being solved. This is not likely to be so if most of

one’s training has been through the wizardry of the computer. I have the same objection to the overuse of applications programs for numerical work until the principles have been mastered. (Of course, that’s one reason to set oneself the task of

writing a book such as this.) In spite of my warnings, when you have the appropriate understanding of topics, you should master systems for doing mathematics by

computer, such as those described in Wolfram’s Mathematica.

Finally, I have deliberately omitted descriptions of computational methods that

are optimized for specialized computer designs, such as vectorized or parallel architectures. You should leam and use these methods when you need them, but most of

them are developed from the simpler principles that I hope you will learn from this

book. You can become informed on both parallel computation and matrix methods

by reading the book by Modi.

1.2 COMPUTING, PROGRAMMING, CODING

When computing numerically there are three levels that I envision in problem analysis: program design, coding, and testing. They are best described by Figure 1.2.

FIGURE 1.2 Computing, programming, and coding form a nested hierarchy. An example of

this nesting activity is that of converting between coordinates (Section 2.6).

6

INTRODUCTION

In Figure 1.2 the activity of computing includes programming, which includes

coding. The right side of the figure shows the example of converting between Cartesian and polar coordinates — the programming project described in Section 2.6.

The aspects of computing that lie outside programming and coding are numerical

analysis and (to some degree) algorithm design. In the example, the formulas for

calculating coordinates are part of numerical analysis, while deciding how quadrants

are selected is probably best considered as part of algorithm design.

At the programming level one first has to decide what one wants to calculate, that

is, what output the program should produce. Then one decides what input is needed

to achieve this. One can then decide on the overall structure of the program; for example, the conversions for Cartesian and polar coordinates are probably best handled by separate branches in the program. At this level the choices of computing system and programming language usually become important.

Finally, as shown in Figure 1.2, one reaches the coding level. Here the program is built up from the language elements. In the C language, for example, functions are written or obtained from function libraries, variable types are declared, and

variables are shared between functions. Detailed instructions are coded, the interfaces to files or graphics are written, and the program is tested for corectness of formulas and program control, such as the method used to terminate program execution. If you think of the activity of computing as a nested three-level system, as

schematized in Figure 1.2, then you will probably produce better results faster than

if you let your thoughts and actions become jumbled together like wet crabs scuttling

in a fishing basket.

In the following parts of this section, I make remarks and give suggestions about

programming as described in this book. First, I justify my choice of C as the language for preparing the programs, then I give you some pointers for learning to program in C, and for translating the programs in this book to Fortran or Pascal from C

(if you insist on doing this). Next, I summarize programming aspects of the projects that occur toward the end of each of Chapters 2 through 10, and I remark on

the correctness and portability of these programs. Finally in this section, I draw

your attention to a convenience, namely, that all the programs and functions provided in this book are listed by section and topic in the index to computer programs

before the main index.

The C language for the programs

I decided to write the sample programs in C language for the following reasons.

First, C is a language that encourages clear program structure, which leads to programs that are readable. My experience is that C is easier to write than Pascal because the logic of the program is usually clearer. For example, the use of a topdown structure in the programs is closer to the way scientists and enginners tackle

real problems. In this aspect C and Fortran are similar. The C language is more demanding than Fortran, in that what you want to do and the meanings of the variables

must all be specified more accurately. Surprisingly, scientists (who pride themselves on precise language) often object to this demand from a computer. I estimate

1.2 COMPUTING, PROGRAMMING, CODING

7

that C is the easiest of the three languages in which to write programs that are

numerically oriented. Ease is indicated by the time it takes to produce a correctly

executing program in each language.

The second reason for using C in this book is that it is intermediate in complexity

between Fortran and Pascal, as illustrated by the comparison chart in the appendix.

That is, there are very few elements in the Fortran language (which makes it simple

to write but hard to understand), most of the elements of Fortran are in C, and some

of the elements of Pascal are also in C. For data handling, C is much more powerful

and convenient than Fortran because facilities for handling characters and strings

were designed into the original C language.

The third reason for my choice of C is that it is now the language of choice for

writing applications programs for workstations and personal computers. Therefore,

programs that you write in C for these machines will probably interface easily with

such applications programs. This is a major reason why C is used extensively in engineering applications.

A fourth reason for using C is that its developers have tried to make it portable

across different computers. Lack of portability has long been a problem with Fortran. Interconnectivity between computers, plus the upward mobility of programs

developed on personal computers and workstations to larger machines and supercomputers, demand program portability. Since very few large computer systems

have extensive support for Pascal, C is the current language of choice for portability.

One drawback of C lies with input and output. Some of the difficulty arises

from the extensive use of pointers in C, and some inconvenience arises from the limited flexibility of the input and output functions in the language. For these reasons, I

have written the input and output parts of the sample programs as simply as practicable, without any attempt to produce elegant formats. Since you probably want to

modify the programs to send the output to a file for processing by a graphics program, as discussed in Section 1.3, for this reason also such elegance is not worthwhile in the sample programs.

Complex numbers are not part of the C language, although they are used extensively in numerical applications, as discussed in Chapter 2. Our calculations that

use complex variables convert the complex numbers to pairs of real numbers, then

work with these. Extensive practice with programming using complex numbers in

this way is given in Sections 2.1 and 2.6. In Numerical Recipes in C, Press et al.

also discuss (Chapter 1 and Appendix E) handling complex numbers in C.

Learning to program in C

This book does not claim to be a guide to learning the C programming language. It

will, however, provide extensive on-the-job training for the programming of numerical applications in C. If you wish to learn how to program in C, especially for the

numerically oriented applications emphasized herein, there are several suitable textbooks. Starting with books which do not assume that you have much familiarity

with progamming then moving upward, there are Eliason’s C, a Practical Learning

Guide and Schildt’s Teach Yourself C, then the text by Darne11 and Margolis, C, a

8

INTRODUCTION

Software Engineering Approach. Many of C’s more subtle and confusing aspects

are described by Koenig in C Traps and Pitfalls.

If you are familiar with other programming languages and wish to use the C programs in this book, there are several texts that should be of considerable help to you.

For general use there is the book by Gehani, which emphasizes the differences between C and other procedural programming languages. Gehani also discusses the

advanced aspects of C as implemented on UNIX systems. A second cross-cultural

book that will help you with the language barrier is Kerrigan’s From Fortran to C,

which has extensive discussions and examples of how to learn C and to reprogram

from Fortran. The book by M?ldner and Steele, C as a Second Language, and that

by Shammas, Introducing C to Pascal Programmers, are especially suitable for those

who are familiar with Pascal but wish to learn to program effectively in C.

Finally, for detailed references on the C language there are C: A Reference Manual by Harbison and Steele, and The Standard C Library by Plauger. You should

also consult the programming manuals for C provided with the implementation of C

for your computing environment, and the manuals that explain the connection between C and your computer’s operating system.

The references on learning to program in the C language are listed together in the

reference section at the end of this chapter. The appendix provides examples of

translating between C, Fortran, and Pascal that are drawn from the C programs in

the first chapters.

Translating to Fortran or Pascal from C

By choosing to present the example programs and the project programs in C language, I know that I will have made a few friends but I may have alienated others.

Especially for the latter, I have tried to decrease their animosity by avoiding use of

some of the useful constructions in C that are sometimes not available or are awkward to implement in other numerically oriented procedural languages. This should

make the programs easier to translate on-the-fly into Fortran or Pascal. Among my

main concessions are the following.

In arrays the [0] element is usually not used by the program, so that the used elements of the array range from [1] upward. The only confusion this may cause when

programming is that the array size must be declared one larger than the maximum element that will ever be used. For example, if you want to be able to use elements

1...100, then the maximum array size (which is always defined as MAX) should be

101. This labeling starting with [1] usually also makes the correspondence between

the mathematics and the coding simpler, because most of the summation and iteration indices in formulas (k or j) begin with unity: any zeroth-index value in a

summation or iteration has usually to be treated specially. I use the [0] element in an

array only if this tightens the connections between the mathematical analysis, the algorithm, and the code.

In summations and indexing, the C construction of ++ to denote incrementing

by one, and similarly - - for decrementing, is not used except in for loops. Although general avoidance of ++ and - - is less efficient, it is less confusing when

translating to Fortran or Pascal, which do not allow such useful constructions.

1.2 COMPUTING, PROGRAMMING, CODING

9

The for loop in C is such a practical and convenient programming device that I

use it without concession to Fortran programmers, who are often confined to the

much clumsier DO loop. However, I use the for loop in a consistent style to which

you can readily adapt.

I have avoided go to statements, so there are no statement labels in the programs. Consequently, you will have to go elsewhere if your favorite computing

recipes include spaghetti. (The come from statement, which might rescue many a

programmer from distress, is also scrupulously avoided.) These omissions do not

make C programs difficult to write or to use.

There are a few operators in C, especially the logical operators, that look quite

different and may be confusing to Fortran and Pascal programmers. I explain these

operators where they appear in programs, especially in the early chapters. They are

listed with their Fortran and Pascal counterparts at the end of the appendix on translating between C, Fortran, and Pascal.

In C the exit function terminates execution when it is called. (Technically, it

terminates the calling process. All open output streams are flushed, all open files are

closed, and all temporary files are removed.) There is a conventional distinction,

which we follow, between exit (0) and exit (1) . The first is for successful termination and graceful exit, while the second is to signal an abnormal situation. In

some computing environments the process that refers to the terminating program

may be able to make use of this distinction.

Within the text, the font and style used to refer to names of programs, functions, and variables is 10-point Monaco (since all programming involves an element of gambling). All the programs and functions are listed in the index to computer programs, which is discussed below.

The computing projects and the programs

Several of the exercises and projects, as described in Section 1.1, require that you

modify programs that are in the text. By this means you will practice what is so

common in scientific and engineering computing, namely the assembling of tested

and documented stand-alone function modules to make a more powerful program

tailored for your use. One advantage of this method is that, provided you are careful

how you make the modifications, you will usually be able to check the integrity of

the program module by comparison with the stand-alone version.

The sample programs, both in the text and in the projects, are written for clarity

and efficiency of writing effort. In particular, when there are choices between algorithms, as in the numerical solution of differential equations, the different algorithms

are usually coded in-line so that it is easy for you to compare them. Therefore, if

you wish to transform one of the chosen sections of in-line code into a function you

will need to be careful, especially in the type declarations of variables used.

I have not attempted to make the sample programs efficient in terms of execution

speed or use of memory. If you want to use a particular computing technique for

production work, after you have understood an algorithm by exploring with the pro-

10

INTRODUCTION

grams provided, you should use a program package specially developed for the purpose and for the computer resources that you have available. At an intermediate

level of efficiency of your effort and computer time are the programs available (in C,

Fortran, and Pascal) as part of the Numerical Recipes books of Press et al.

My view of the connections among materials in this book, the C language, the

Numerical Recipes books, systems such as Mathematica, and their uses in scientific

applications is summarized in Figure 1.3.

FIGURE 1.3 Connections among topics in this book, C language, the Numerical Recipes

books, the Mathematica system, and scientific applications.

In Figure 1.3 the lines are connectors, not arrows. They indicate the strongest

two-way connections between the topics and books (names written in italics). Some

significant links have been omitted, mostly for topological reasons. For example,

many of the scientific applications examples in this book do not require C programs

or use of the Mathematica system. Also, much of the latter is programmed in C, and

it can convert its symbolic results into C (or Fortran) source code, as described in

Wolfram’s book on Mathematica.

Caveat emptor about the programs

The sample programs included in this book have been written as simply as practical

in order that they could readily be understood by the human reader and by the compiler. In order to keep the programs easy to read, I have not included extensive

checking of the allowed range of input variables, such as choices that control program options. My rule of thumb has been to put in a range check if I made an input

1.3 ONE PICTURE IS WORTH 1000 WORDS

11

error while testing a program, or if lack of a check is likely to produce confusing results. There are checks for array bounds if they are simple to code and do not interrupt program flow. Errors of use or input that are diagnosed by our C programs

always begin with a double exclamation, ! !, followed by an explanation of the error.

Program execution will often continue after some reasonable fix-up is attempted. A

typical fix-up is just to request another input for the troublesome variable.

Because the programs are written to be translated easily to Fortran or Pascal, as

described in a previous subsection and shown in the appendix, I have tried to avoid

nonstandard parts of C. The compiler that I use claims to follow ANSI standards. I

also checked for compatibility with the C language as described in the second edition

of Kernighan and Ritchie’s book.

In spite of all these precautions, I have two words of advice: caveat emptor — let

the buyer beware. The programs are supplied as is and are not guaranteed. For

each program you use, I suggest that you make at least the checks I have indicated.

If you can devise other tests of program correctness, I encourage you to do so.

The index to computer programs

Because this book has many computer programs with associated functions, I have

included an annotated index to all the programs and functions. They are listed, by

order of appearance, in the index that immediately precedes the regular index. The

programs and functions also appear alphabetically by name in the regular index.

1.3

ONE PICTURE IS WORTH 1000 WORDS

In this book graphical output is usually suggested as a way to improve the presentation of results, especially in the projects. Since graphics are so hardware dependent,

my suggestions for graphics in the projects are necessarily vague. You should familiarize yourself as much as practicable with techniques of graphical presentation.

If you have access to a powerful system that combines graphics and mathematics,

such as Mathematica as described by Wolfram or by Wagon, you may wish to develop some of the projects by using such a system.

Why and when you should use graphics

Tufte, in his two books on displaying and envisioning quantitative information, has

given very interesting discussions and examples of effective (and ineffective) ways

of displaying quantitative information from a variety of fields. In numerical applications of mathematics, graphics are especially important because of the enormous

number of numbers that come spewing out of the computer in a stream of numerical

environmental pollution. If there are many values to be compared, or if you want to

show trends and comparisons (as we usually do), it is worth the effort to write a

graphical interface for your program. If there are just a few check values to output,

12

INTRODUCTION

it is not worth the extra coding and possible lack of clarity that graphics may

produce.

If you have access to Mathematica or some other system that combines mathematics, numerics, and graphics, your learning will be enhanced if you combine the

three elements when working the exercises and projects. Wagon’s book provides

many examples of graphics techniques that would be useful in conjunction with this

workbook.

Impressive graphics, or practical graphics

In many books that relate to computing you will see elegant and impressive graphics

that have been produced by long runs on powerful computers using special-purpose

programs. Although these illustrations may improve your comprehension, and perhaps inspire you to become a computer-graphics artist, their production is usually

not practicable for most computer users. Therefore, I urge you to find a simple

graphics system that interfaces easily to your programming environment, that is

readily available to you, and that is inexpensive to use. For example, there are about

a hundred line drawings in this book. They were all produced by using only two

applications programs (one for graphics and one for drafting). The graphics program used input files that the C programs produced, so the numbers were seldom

touched by human hand, and the graphics output was produced on the same laser

printer that printed the text.

Many of the programs in this book produce simple output files. I most often

used these files for input to graphics, and sometimes for preparing tables. If you

make a similar interface and use it often to produce graphics (perhaps through the

intermediary of a spreadsheet), I think it will improve your comprehension of the

numerical results, without burdening you with much coding effort.

If you have convenient access to a state-of-the-art graphics system, it may be

useful for a few of the projects in this book. Just as I believe that an approach to numerical computing that is completely recipe-based is unwise, I believe that using

computer-graphics systems without an understanding of their background is similarly unwise. A comprehensive treatment of many aspects of computer graphics is

provided in the treatise by Foley et al. Methods for preparing high-resolution

graphics, and how to implement them in Pascal, are described in the book by Angell

and Griffith.

1.4 SUGGESTIONS FOR USING THIS BOOK

This book may be used for both self-study and for class use. I have some suggestions that should help you to make most effective use of it. First I indicate the conections between the remaining nine chapters, then there are remarks about the exercises and the projects.

1.4 SUGGESTIONS FOR USING THIS BOOK

13

Links between the chapters

Because we cover a large amount of territory and a variety of scientific and engineering landscapes in this book, it is useful to have an indication of the connections between its nine other chapters. Table 1.1 summarizes the strength of the links between the chapters.

TABLE 1.1 Cross-reference chart for use of this book.

Key:

chapter above is necessary preparation

chapter above is desirable preparation

chapter above is optional preparation

Complex variables

Power series

Numerical derivatives and integrals

Fitting curves through data

Least-squares analysis of data

Introduction to differential equations

Second-order differential equations

Discrete Fourier transforms and series

Fourier integral transforms

For example, if you plan to work through Chapter 7 (introduction to differential

equations), use of Chapters 2, 5, and 6 is optional, Chapter 3 (power series) is desirable, whereas Chapter 4 (numerical derivatives and integrals) is necessary preparation. Within each chapter you should read not only the text, but also the exercises,

which are embedded in the text. Exercises containing equations are especially important to be read, since these equations often form part of the development. Therefore, read over every exercise, even if you don’t work it through in detail.

The exercises and projects

Since this book has an overwhelming number of exercises, many of them nontrivial,

a guide to use of the exercises is appropriate. It will be clear to you that I always insert an exercise whenever I don’t want to show you all the steps of a development.

This is not laziness on my part, because I assure you that I have worked through every step. Rather, an exercise provides a checkpoint where you should pause, take

stock of what you have been reading, then test your understanding by trying the exercise. If you have difficulty with this exercise, reread all of the subsection

containing the exercise, even past the troublesome exercise. Then work the exercise

14

INTRODUCTION

once more. I believe that by doing this you will have a realistic estimate of your progress in comprehension. If you are using this book for self-study, this procedure

should help you considerably.

Some exercises are more than checkpoints, they are crossroads where concepts

and techniques developed previously are focused on relevant and interesting problems. This type of exercise, which often appears in a project toward the ends of

chapters, is always indicated. Such exercises provide very good tests of your overall comprehension of the material in the current and previous chapters.

The projects, of which there is at least one per chapter after this introductory

chapter, are designed to bring together many of the aspects of analysis and numerics

emphasized within the chapter. They provide you with opportunities to explore the

numerics and the science by using the number-crunching and graphical powers of

computers. Programming aspects of the projects are discussed in Section 1.2.

REFERENCES FOR THE INTRODUCTION

General references

Angell, I. O., and G. Griffith, High-Resolution Computer Graphics Using Pascal,

Macmillan Education, Basingstoke, England, 1988.

Dym, C. L., and E. S. Ivey, Principles of Mathematical Modeling, Academic Press,

New York, 1980.

Foley, J. D., A. van Dam, S. K. Feiner, and J. F. Hughes, Computer Graphics,

Addison-Wesley, Reading, Massachusetts, third edition, 1990.

Gould, H. and J. Tobochnik, An Introduction to Computer Simulation Methods,

Addison-Wesley, Reading, Massachusetts, 1988.

Koonin, S. E., Computational Physics, Addison-Wesley, Redwood City,

California, 1986.

Koonin, S. E., and D. C. Meredith, Computational Physics: FORTRAN Version,

Addison-Wesley, Redwood City, California, 1990.

Mesterton-Gibbons, M., A Concrete Approach to Mathematical Modelling,

Addison-Wesley, Reading, Massachusetts, 1989.

Meyer, W. J., Concepts of Mathematical Modeling, McGraw-Hill, New York,

1984.

Modi, J. J., Parallel Algorithms and Matrix Computation, Clarendon Press, Oxford,

England, 1988.

Nakamura, S., Computational Methods in Engineering and Science, WileyInterscience, New York, 1986.

Taylor, A. E., and W. R. Mann, Advanced Calculus, Wiley, New York, third edition, 1983.

Thompson, W. J., Computing in Applied Science, Wiley, New York, 1984.

REFERENCES FOR THE INTRODUCTION

15

Tufte, E. R., The Visual Display of Quantitative Information, Graphics Press,

Cheshire, Connecticut, 1983.

Tufte, E. R., Envisioning Information, Graphics Press, Cheshire, Connecticut,

1990.

Wagon, S., Mathematica in Action, W. H. Freeman, New York, 1991.

Wolfram, S., Mathematica: A System for Doing Mathematics by Computer,

Addison-Wesley, Redwood City, California, second edition, 1991.

Wylie, C. R., and L. C. Barrett, Advanced Engineering Mathematics, McGrawHill, New York, fifth edition, 1982.

References on learning and using C

Darnell, P. A., and P. E. Margolis, C, a Software Engineering Approach, SpringerVerlag, New York, 1991.

Eliason, A. L., C, a Practical Learning Guide, Macmillan, New York, 1988.

Gehani, N., C: An Advanced Introduction, Computer Science Press, Rockville,

Maryland, 1985.

Harbison, S. P., and G. L Steele, Jr., C: A Reference Manual, Prentice Hall,

Englewood Cliffs, New Jersey, third edition, 199 1.

Kernighan, B. W., and D. M. Ritchie, The C Programming Language, Prentice

Hall, Englewood Cliffs, New Jersey, second edition, 1988.

Kerrigan, J. F., From Fortran to C, Windcrest Books, Blue Ridge Summit,

Pennsylvania, 199 1.

Koenig, A., C Traps and Pitfalls, Addison-Wesley, Reading, Massachusetts, 1989.

Miildner, T., and P. W. Steele, C us a Second Language, Addison-Wesley,

Reading, Massachusetts, 1988.

Plauger, P. J., The Standard C Library, Prentice Hall, Englewood Cliffs, New

Jersey, 1992.

Press, W. H., B. P. Flannery, S. A. Teukolsky, and W. T. Vetterling, Numerical

Recipes in C, Cambridge University Press, New York, 1988.

Schildt, H., Teach Yourself C, Osborne McGraw-Hill, Berkeley, California, 1990.

Shammas, N., Introducing C to Pascal Programmers, Wiley, New York, 1988.

16

Previous Home

Chapter 2

A REVIEW OF COMPLEX VARIABLES

The purpose of this chapter is to review your understanding of complex numbers

and complex variables, and to summarize results that are used extensively in subsequent chapters. Complex variables are treated elegantly and completely in many

mathematics texts, but a treatment in which the aim is to develop intuition in the scientific applications of complex numbers may have much more modest goals, and is

best done from a geometrical perspective, rather than from an analytic or algebraic

viewpoint.

We start with the algebra and arithmetic of complex numbers (in Section 2.1)

including a simple program, then turn in Section 2.2 to the complex-plane representation because of its similarities to plane-polar coordinates and to planar vectors.

The simplest (and most common) functions of complex variables-complex exponentials and hyperbolic functions- are reviewed in Section 2.3. Phase angles, vibrations, and complex-number representation of waves, which are all of great interest to scientists and engineers, are summarized in Section 2.4 before we take a diversion in Section 2.5 to discuss the interpretation of complex numbers. Project 2,

which includes program Cartesian & Polar Coordinate Interconversion

for converting between plane-polar and Cartesian coordinates, concludes the text of

this chapter. This program serves to emphasize the ambiguities in calculating polar

coordinates from Cartesian coordinates, and it will be useful in the later programming applications. References on complex numbers complete the chapter.

The discussion of complex variables is limited to the above topics, and does not

develop extensively or with any rigor the topics of analytic functions in the complex

plane, their differentiation, or their integration. Although several derivations later in

this book, especially those involving integrals, would be simplified if the methods of

contour integration were used, the methods of derivation used here are usually direct

and do not require the extra formalism of contours and residues. Readers who have

experience with functions of a complex variable will often be able to substitute their

own methods of proof, which may be more direct than those provided here.

17

Next

18

COMPLEX VARIABLES

Many of the examples and exercises in this chapter anticipate steps in our later

developments that use complex variables, especially the material on Fourier expansions (Chapters 9 and 10). Since we always have a reference to the later material,

you may wish to look ahead to see how the complex variables are used.

2.1

ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

In this section we summarize the algebraic properties of complex numbers, their properties for numerical computation, the relation between complex numbers and plane

geometry, and the special operations on complex numbers — complex conjugation,

modulus, and argument.

When you have reviewed this section, you will have the conceptual and technical

skills for the more extensive developments of complex numbers that are presented in

the remainder of this chapter. In particular, Project 2 — the program for converting

between coordinates (Section 2.6) -requires most of the ideas from this section.

If you are experienced with complex variables, you may try the project before working this section. If you have difficulties with the mathematics in the project (as distinguished from the programming involved), return and rework this section.

The algebra of complex numbers

We indicate a complex number, z, symbolically by

(2.1)

in which x and y are understood to be both real numbers. The sign + in this formula does not mean arithmetic addition, although it has many rules that are similar to

those for addition. You will have already encountered yet another meaning of + as

a sign used in the addition of vectors, which is also distinct from arithmetic addition.

In (2.1), the symbol i has the property that

(2.2)

with a unique value being assumed for i itself. In engineering contexts it is more

usual to find the symbol i replaced by the symbol j, thus avoiding possible confusion when complex numbers are used to describe currents (i) in electrical circuits, as

in our Section 8.1. We will use i, recalling its origins in the initial letter of the historical term “imaginary.”

Complex numbers may also be thought of as pairs of numbers, in which the order in the pair is significant. Thus we might write

(2.3)

analogously to vectors in a plane. Just as the coordinates (x, y) and (y,x) are usually distinct, so are the analogous complex numbers. The notation in (2.3) avoids

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

19

ambiguities in using the + sign and in the necessity of inventing a symbol satisfying (2.2). Further, many of the rules for manipulating complex numbers have a

strong similarity to those for vectors in a plane if the notation (2.3) is used. Although we will write our results for complex-number algebra and arithmetic in the

notation (2.1), you are invited to try the number-pair notation in Exercise 2.1 (c).

This notation is also used in some programming languages that allow complexarithmetic operations.

The rules for manipulating complex numbers must be consistent with those for

purely real numbers ( y = 0) and for purely imaginary numbers (x = 0). In the following, let z = x + iy generically, and let z1 = x1+ iy1, z2 = x2 + i y2 represent two particular complex numbers. Then the following properties hold:

(2.4)

Negation of a complex number is defined by

(2.5)

which is often written casually as

(2.6)

A complex number is zero only if both its real and imaginary parts are zero, which is

consistent with zero being the only solution of the equation z = -z.

Addition or subtraction of two complex numbers is accomplished by

(2.7)

Multiplication of complex numbers is performed by

(2.8)

Reciprocal of a complex number is defined by

(2.9)

which has the property that z (1/z) = 1, as for the arithmetic of real numbers.

Division of one complex number into another is based on the reciprocal of the divisor, and is therefore undefined if the divisor is zero:

(2.10)

20

COMPLEX VARIABLES

In order to check your comprehension of these rules for complex arithmetic, try

the following exercise.

Exercise 2.1

(a) Verify that the rules (2.7) through (2.10) are consistent with those for real

arithmetic by checking them for y1 = y2 = 0.

(b) Check the consistency of (2.7) through (2.10) for purely imaginary numbers

by setting x1= x2 = 0 and noting the condition on i, (2.2).

(c) Use the notation for complex numbers as ordered-number pairs, as indicated

by (2.3), to write down the preceding complex-arithmetic rules, (2.4) through

(2.10). n

Now that we have summarized the formal basis of complex-variable algebra, it is

time to consider complex arithmetic, especially for computer applications.

Programming with complex numbers

Few computer languages are designed to include complex-variable types in their

standard definition. They are available in Fortran, but not in C or Pascal. In Wolfram’s Mathematica system for doing mathematics by computer, which has both

symbolic and numeric capabilities, complex numbers can be handled readily. An

introduction to their use is provided in Section 1.1 of Wolfram’s book.

To appreciate why computer hardware is not built and computer software is not

designed to assume that numbers they handle are complex, consider the following

exercise.

Exercise 2.2

Show that the total number of real-arithmetic operations needed for complexnumber addition and subtraction is 2, the number for multiplication is 6, and the

number for division is 11 or 14, depending on whether or not the divisor in

(2.9) is stored. n

We now show a simple program in C language for performing complex arithmetic by the rules given in the preceding subsection. The purpose of this program is

twofold: if you are unfamiliar with the C language the program will provide a simple

introduction, while it will also develop your understanding of complex arithmetic.

The program Complex-Arithmetic Functions takes as input x1, y1, x2, y2

for the components of two complex numbers z1 and z2. After checking that both

numbers are nonzero, it calls the functions for addition, subtraction, multiplication,

and division, namely CAdd, CSub, CMult, and CDiv, then prints the results before

returning for more input. Here is the program.

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

21

PROGRAM 2.1 Functions for performing complex arithmetic; addition, subtraction, multiplication, and division.

#include

#include

main ()

{

/* Complex-Arithmetic Functions */

double

x1,x2,y1,y2,x1a2,y1a2,x1S2,y1S2,x1m2,y1m2,x1d2,y1d2;

void CAdd(),CSub(),CMult(),CDiv();

printf("Complex-Arithmetic Functions\n");

x1 = 1; y1 = 1; x2 = 1; y2 = 1;

/* Check that at least one complex numbers is not zero */

while ( xl !=0 || yl != 0 || x2 != 0 || y2 != 0 )

printf("\nInput x1,y1,x2,y2 (all zero to end):\n");

scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);

if ( xl == 0 && y1 == 0 && x2 == 0 && y2 == 0 )

printf("\nEnd

exit(0);

}

Complex-Arithmetic

Functions");

CAdd(xl,yl,x2,y2,&x1a2,&y1a2); /* complex Add */

/* returns x1a2, y1a2 */

CSub(xl,yl,x2,y2,&x1s2,&y1s2); /* complex Subtract */

/* returns x1s2, yls2 */

CMult(x1,y1,x2,y2,&x1m2,&y1m2);

/* complex Multiply */

/* returns x1m2, y1m2 */

CDiv(x1,yl,x2,y2,&x1d2,&y1d2); /* complex Divide */

/* returns x1d2, y1d2 */

printf("\nz1+z2=(%lf) + i(%lf)",x1a2,y1a2);

printf("\nz1-z2=(%lf) + i(%lf)",x1s2,y1s2);

printf("\nz1*z2=(%lf) + i(%lf)",X1m2,y1m2);

printf("\nz1/z2=(%lf) + i(%lf)\n",x1d2,y1d2);

22

COMPLEX VARIABLES

void CAdd(x1,y1,x2,y2,x1a2,y1a2)

/* Complex Addition function */

double x1,y1,x2,y2,*x1a2,*y1a2;

{

*x1a2 = x1+x2; *y1a2 = y1+y2;

void CSub(x1,y1,x2,y2,x1s2,y1s2)

/* Complex Subtraction function */

double x1,y1,x2,y2,*x1s2,*y1s2;

{

*x1s2 = xl-x2; *y1s2 = y1-y2;

}

void CMult(x1,y1,x2,y2,x1m2,y1m2)

/* Complex Multiplication function */

double x1,y1,x2,y2,*x1m2,*y1m2;

{

*x1m2 = x1*x2-y1*y2;

*y1m2 = x1*y2+y1*x2;

}

void CDiv(x1,y1,x2,y2,x1d2,y1d2)

/* Complex Division function */

double x1,y1,x2,y2,*x1d2,*y1d2;

{

double den;

den = x2*x2+y2*y2;

if ( den == 0 )

{

printf("!! CDiv denominator = 0; dividend set to zero");

*x1d2 = 0; *y1d2 = 0;

}

else

{

*x1d2 = (x1*x2+y1*y2)/den;

*y1d2 = (y1*x2-x1*y2)/den;

The program reveals an immediate difficulty with modifying a language to include complex variables, in that two values must be returned by a complex-variable

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

23

function. In C this cannot be done simply by a conventional function (which returns

just one value, at most). One can get around the problem by using the indirection

(dereferencing) operator, written as an asterisk (*) preceding a variable name, as

used for each of the two real-variable values returned by the program functions.

Here are some suggestions for exploring complex numbers by using the program Complex-Arithmetic Functions.

Exercise 2.3

(a) Use several pairs of real numbers for both inputs ( y1 = 0, y2 = 0 ) in order to verify that the complex numbers contain real numbers as special cases.

(b) Input purely imaginary numbers ( xl = 0, x2 = 0 ) to the program and

verify the correctness of the arithmetic.

(c) Show by a careful analytical proof that if the product of two complex numbers is zero, then at least one of the complex numbers is identically zero (both

real and imaginary parts zero). Prove that if one of a pair of complex numbers is

zero, their product is zero. Verify this by using the program. n

With this background of algebraic and arithmetic properties of complex numbers, we are prepared to review some more formal definitions and properties.

Complex conjugation, modulus, argument

In complex-variable algebra and arithmetic one often needs complex quantities that

are related by reversal of the sign of just their imaginary parts. We therefore have

the operation called complex conjugation. In mathematics texts the notation for this

operation is often denoted by a bar, - , while other scientists often use an asterisk,

as * . In the latter notation the complex-conjugate value of z is

z*=x-iy

(2.11)

z=x+iy

(2.12)

if and only if

From the definition of complex conjugation we can readily derive several interesting

results.

Exercise 2.4

(a) Prove that

(2.13)

where the notation Re stands for “real part of.”

24

COMPLEX VARIABLES

(b) Similarly, prove that

(2.14)

where Im denotes “imaginary part of.”

(c) Derive the following properties of complex conjugation:

(2.15)

(2.17)

which show that complex conjugation is distributive over addition, subtraction,

multiplication, and division. n

The identity

zz*=x2 y2

(2.18)

shows that z z * is zero only if z is identically zero, which is an example of the condition from Exercise 2.3 (c) for vanishing of the product of two complex numbers.

The frequent occurrence of z z * and its connection with vectors in two dimensions lead to the notation of the modulus of a complex number z, denoted by

(2.19)

Thus mod z indicates the magnitude of z if we picture it as a vector in the x - y

plane. Another name for modulus is absolute value. For example, the modulus of a

real number is just its value without regard to sign, that is, its absolute value. The

modulus of a pure imaginary number is just the value of y without regard to sign.

The modulus of a complex number is zero if and only if both its real and its imaginary part are zero.

The argument of a complex number is introduced similarly to polar coordinates

for two-dimensional vectors. One defines the arg function for a complex variable

by the requirement that

(2.20)

and the requirement that

25

2.1 ALGEBRA AND COMPUTING WITH COMPLEX NUMBERS

(2.21)

which are necessary and sufficient conditions for definition of arg z to within a

multiple of 27

Exercise 2.5

Explain why the commonly given formula

(2.22)

is not sufficient to specify arg z uniquely, even to within a multiple of 27

n

In Section 2.6, in the program to convert between coordinates, we return to this

problem of ambiguous angles. The argument of a complex number is sometimes

called the phase angle, or (confusingly) the amplitude.

One aspect of the argument relates to the analogy between complex variables and

planar vectors. If the pair (x,y) formed the components of a vector, then arg z

would be the angle that the vector makes with the positive x axis. For example,

arg (Re z) = ± arg i = /2, and arg (-i) = - / 2 .

A program for complex conjugate and modulus

For applications with complex variables it is worthwhile to have available programs

for complex functions. We provide here a program that invokes functions returning

complex conjugate and modulus values. The more-involved coding for the argument function is provided in the programming project, Section 2.6. Here is the program Conjugate & Modulus Functions.

PROGRAM 2.2 Conjugate and modulus functions for complex numbers.

#include

#include

main()

{

/* Conjugate & Modulus Functions */

double x,y,xc,yc, zmod;

void CConjugate();

double CModulus();

26

COMPLEX VARIABLES

printf("Complex Conjugate & Modulus Functions\n");

x = 1; y = 1;

/* Check for x not zero or y not zero */

while ( x != 0 || y != 0 )

{

printf("\nInput x,y (both zero to end):\n");

scanf("%lf%lf",&x,&y) ;

if ( x == 0 && y == 0 )

{

printf("\nEnd Conjugate & Modulus Functions");

exit (0) ;

}

CConjugate(x,y,&xc,&yc); /* conjugate x+iy */

zmod = CModulus(x,y); /* modulus of x+iy */

printf("z* = (%lf) + i(%lf)",xc,yc);

printf("\nmod [(%lf) + i(%lf)] = %lf\n",x,y,zmod);

}

void CConjugate(x,y,xc,yc)

/* Complex Conjugation function */

double x,y,*xc,*yc; /* xc & yc are returned */

{

*xc = x; "yc = -y;

}

double CModulus(x,y)

/* Complex Modulus function */

double x,y;

{

double mod;

mod = sqrt (x*x+y*y);

return mod;

Some remarks on programming the functions CConjugate and CModulus in

the C language are in order:

1. Note that complex conjugation performs an operation on a complex number, albeit a simple one. So it does not return a value in the sense of a C function value.

Therefore, the “function” CConjugate is declared to be “void.” The value of the

2.2 THE COMPLEX PLANE AND PLANE GEOMETRY

27

complex conjugate is returned in the argument list of CConjugate as xc and yc,

which are dereferenced variables (preceded by a *, which should not be confused

with complex conjugation, or even with / * and * / used as comment terminators).

2. On the other hand, CModulus is declared as “double” because it is a function

which returns a value, namely mod (inside CModulus), which is assigned to zmod

within the main program. Note that CModulus might also be used within an arithmetic statement on the right-hand side of the = sign for zmod.

3. The program continues to process complex-number pairs while the input number

pair is nonzero. If the zero-valued complex number (x = 0 and y = 0 ) is entered,

the program exits gracefully by exit (0) rather than with the signal of an ungraceful

termination, exit (1).

With this background to programming complex conjugation and modulus, plus

the program for arguments in Section 2.6, you are ready to compute with complex

variables.

Exercise 2.6

Run several complex-number pairs through the program Conjugate & Modulus

Functions. For example, check that the complex conjugate of a complex-conjugate number produces the original number. Also verify that the modulus values of (x, y) and (y, x) are the same. ?

2.2

THE COMPLEX PLANE AND PLANE GEOMETRY

In the preceding section on algebra and computing with complex numbers we had

several hints that there is a strong connection between complex-number pairs (x, y)

and the coordinates of points in a plane. This connection, formally called a “mapping,” is reinforced when we consider successive multiplications of z = x + i y

by i itself.

Exercise 2.7

(a) Show that if z is represented by (x, y), then i z is (-y, x), then i2z is

(-x,-y), i3z is (y,-x), and i4z regains (x , y).

(6) Verify that in plane-polar geometry these coordinates are displaced from

each other by successive rotations through / 2, as shown in Figure 2.1. n

The geometric representation of complex numbers shown in Figure 2.1 is variously known as the complex plane, the Argand diagram, or the Gauss plane. The

relations between rotations and multiplication with complex numbers are summarized in the following subsections.

28

COMPLEX VARIABLES

FIGURE 2.1 Rotations of complex numbers by /2 in the complex plane. Note that rotations

do not change the length of a complex number, as indicated by the dashed circle.

Cartesian and plane-polar coordinates

Before reviewing the connections between the complex plane and plane geometry,

let us recall some elementary relations between Cartesian and plane-polar coordinates. If the polar coordinates are r, the (positive) distance from the origin along a

line to a point in the plane and the angle (positive in the anticlockwise direction)

that this line makes with the x axis, then the Cartesian coordinates are given as

in a right-handed coordinate system. The polar coordinates are indicated in Figure 2.1. Inverting these equations to determine r and which is not as trivial as it

may look, is discussed in the first part of Section 2.6.

In the complex plane we may therefore write z as

(2.24)

so that the modulus, which is also the radius, is given by

2.2 THE COMPLEX PLANE AND PLANE GEOMETRY

29

(2.25)

and the polar angle with respect to the x axis is given by

(2.26)

The principal value of the polar angle is the smallest angle lying between

and

Such a specification of the principal value allows unique location of a point

in the complex plane. Other choices that limit may also be encountered, for

example, the range 0 to 27.

Complex conjugation is readily accomplished by reflecting from to since

(2.27)

In the language of physics, z, and its complex conjugate are related through a parity

symmetry in a two-dimensional space.

With this angular representation of complex variables, we can derive several interesting results.

De Moivre’s theorem and its uses

A theorem on multiplication of complex numbers in terms of their polar-coordinate

representations in the complex plane was enunciated by Abraham De Moivre (16671754). We derive his theorem as follows. Suppose that we have two complex

numbers. the first as

(2.28)

and the second as

(2.29)

Their product can be obtained by using the trigonometric identities for expanding cosines and sines of sums of angles, to obtain

(2.30)

From this result we see that multiplication of complex numbers involves conventional multiplication of their moduli, the r1r2 part of (2.30), and addition of their angles.

30

COMPLEX VARIABLES

Therefore, multiplication in the complex plane, as well as addition, can readily be

shown, as in Figure 2.2.

FIGURE 2.2 Combination of complex numbers in the complex plane. The complex numbers

and their sum are indicated by the dashed lines, while their product is shown by the solid line.

Equation (2.30) can be generalized to the product of n complex numbers, as the

following exercise suggests.

Exercise 2.8

(a) Prove by the method of mathematical induction that for the product of n

complex numbers, z1,z2, ..., zn, one has in polar-coordinate form

(2.31)

(b) From this result, setting all the complex numbers equal to each other, prove

that for the (positive-integer) nth power of a complex number

(2.32)

which is called De Moivre’s theorem. n

This remarkable theorem can also be proved directly by using induction on n.

Reciprocation of a complex number is readily performed in polar-coordinate

form, and therefore so is division, as you may wish to show.

2.3 FUNCTIONS OF COMPLEX VARIABLES

31

Exercise 2.9

(a) Show that for a nonzero complex number, z, its reciprocal in polar-coordinate form is given by

(2.33)

(b) From the result in (a) show that the quotient of two complex numbers can be

written in polar-coordinate form as

(2.34)

where it is assumed that r2 is not zero, that is, z2 is not zero. n

Thus the polar-coordinate expressions for multiplication and division are much simpler than the Cartesian-coordinate forms, (2.8) and (2.10).

Although we emphasized in Exercise 2.2 that complex-number multiplication

and division in Cartesian form are much slower than such operations with real numbers, these operations may be somewhat speedier in polar form, especially if several

numbers are to be multiplied. An overhead is imposed by the need to calculate cosines and sines. Note that such advantages and disadvantages also occur when using logarithms to multiply real numbers.

2.3 FUNCTIONS OF COMPLEX VARIABLES

In the preceding two sections we reviewed complex numbers from algebraic, computational, and geometric viewpoints. The goal of this section is to summarize how

complex variables appear in the most common functions, particularly the exponential, the cosine and sine, and the hyperbolic functions. We also introduce the idea of

trajectories of functions in the complex plane.

Complex exponentials: Euler’s theorem

In discussing De Moivre’s theorem at the end of the preceding section we noticed

that multiplication of complex numbers may be done by adding their angles, a procedure analogous to multiplying exponentials by adding their exponents, just the

procedure used in multiplication using logarithms. Therefore, there is probably a

connection between complex numbers in polar-coordinate form and exponentials.

This is the subject of Euler’s theorem.

A nice way to derive Euler’s theorem is to write

(2.35)

32

COMPLEX VARIABLES

then to note the derivative relation

(2.36)

But the general solution of an equation of the form

(2.37)

is given by

(2.38)

Exercise 2.10

Show, by identifying (2.36) and (2.37) with the result in (2.38), that = i.

Then choose a special angle, say = 0, to show that = 1. Thus, you have

proved Euler’s theorem,

(2.39)

which is a remarkable theorem showing a profound connection between the geometry and algebra of complex variables. n

It is now clear from Euler’s theorem why multiplication of complex numbers involves addition of angles, because the angles are added when they appear in the exponents. Now that we have the formal derivation of Euler’s theorem out of the way,

it is time to apply it to interesting functions.

Applications of Euler’s theorem

There are several interesting and practical results that follow from Euler’s theorem

and the algebra of complex numbers that we reviewed in Section 2.1. The trigonometric and complex-exponential functions can be related by noting that, for real angles,

(2.40)

which follows by taking complex conjugates on both sides of (2.39). On combining

(2.39) and (2.40) for the cosine we have

(2.41)

while solving for the sine function gives

2.3 FUNCTIONS OF COMPLEX VARIABLES

33

(2.42)

Both formulas are of considerable usefulness for simplifying expressions involving

complex exponentials.

Exercise 2.11

Use the above complex-exponential forms of the cosine and sine functions to

prove the familiar trigonometric identity

(2.43)

the familiar theorem of Pythagoras. n

Although our derivation of Euler’s theorem does not justify it, since is assumed to

be real in the differentiation (2.36) the theorem holds even for being a complex

variable. Thus the Pythagoras theorem also holds for complex .

Some remarkable results, which are also often useful in later chapters, are found

if multiples of /2 are inserted in Euler’s identity, (2.39). Derive them yourself.

Exercise 2.12

Use Euler’s theorem to show that

(2.44)

which gives the values for successive rotations in the complex plane by r/2.

Compare these results with Figure 2.1. n

The exponential form

is generally much more symmetric and therefore is

easier to handle analytically than are the cosine and sine functions, with their awkward function changes and sign changes upon differentiation compared with the

simplicity of differentiating the complex exponential. This simplicity is very powerful when used in discussing the solution of differential equations in Chapter 8, and

also in deriving the fast Fourier transform (FFT) algorithm in Chapter 9.3.

An interesting application of the complex-exponential function that anticipates its

use in the FFT algorithm is made in the following exercise.

Exercise 2.13

Consider the distributive and recurrence properties of the complex-exponential

function defined by

(2.45)

(a) Prove the following properties of powers of E(N):

34

COMPLEX VARIABLES

(2.46)

(2.47)

for any a, b, and for any p 0.

(b) Using these results, show that if N = 2V, where v is a positive integer,

then, no matter how many integer powers of E(N) are required, only one evaluation of this complex-exponential function is required. n

As a further topic in our review of functions of complex variables, let us consider the hyperbolic and circular functions.

Hyperbolic functions and their circular analogs

Exponential functions with complex arguments are required when studying the solutions of differential equations in Chapters 7 and 8. A frequently occurring combination is made from exponentially damped and exponentially increasing functions.

This leads to the definition of hyperbolic functions, as follows.

The hyperbolic cosine, called “cosh,” is defined by

(2.48)

while the hyperbolic sine, pronounced “sinsh,” is defined by

(2.49)

If u is real, then the hyperbolic functions are real-valued. The name “hyperbolic”

comes from noting the identity

(2.50)

in which, if u describes the x and y coordinates parametrically by

(2.5 1)

then an x - y plot is a rectangular hyperbola with lines at /4 to the x and y axes

as asymptotes.

Exercise 2.14

(a) Noting the theorem of Pythagoras,

(2.52)

2.3 FUNCTIONS OF COMPLEX VARIABLES

35

for any (complex) u, as proved in Exercise 2.11, explain why the cosine and

sine functions are called “circular” functions.

(b) Derive the following relations between hyperbolic and circular functions

(2.53)

a n d

(2.54)

valid for any complex-valued u. n

These two equations may be used to provide a general rule relating signs in identities

for hyperbolic functions to identities for circular functions:

An algebraic identity for hyperbolic functions is the same as that for circular

functions, except that in the former the product (or implied product) of two sinh

functions has the opposite sign to that for two sin functions.

For example, given the identity for the circular functions

(2.55)

we immediately have the identity for the hyperbolic functions

(2.56)

Exercise 2.15

Provide a brief general proof of the hyperbolic-circular rule stated above. n

Note that derivatives, and therefore integrals, of hyperbolic and circular functions do

not satisfy the above general rule. The derivatives of the hyperbolic functions are

given by

(2.57)

and by

(2.58)

in both of which the real argument, u, is in radians. There is no sign change on differentiating the hyperbolic cosine, unlike the analogous result for the circular cosine.

36

COMPLEX VARIABLES

The differential equations satisfied by the circular and hyperbolic functions also

differ by signs, since the cosine and sine are solutions of

(2.59)

which has oscillatory solutions, whereas the cosh and sinh are solutions of

(2.60)

which has solutions exponentially increasing or exponentially decreasing.

Exercise 2.16

(a) Prove the two derivative relations (2.57) and (2.58) by starting with the defining equations for the cosh and sinh.

(b) Use the relations between hyperbolic and circular functions, (2.53) and

(2.54), to compute the derivatives of the hyperbolic functions in terms of those

for the circular functions.

(c) Verify the appropriateness of the circular and hyperbolic functions as solutions of the differential equations (2.59) and (2.60), respectively. n

To complete the analogy with the circular functions, one also defines the hyperbolic tangent, called “tansh,” by

(2.6 1)

which is analogous to the circular function, the tangent, defined by

(2.62)

Among these six hyperbolic and circular functions, for real arguments there are

three that are bounded by ±1 (sin, cos, tanh) and three that are unbounded (sinh,

cosh, tan). Therefore we show them in a pair of figures, Figures 2.3 and 2.4, with

appropriate scales.

By displaying the bounded hyperbolic tangent on the same scale as the sine function in Figure 2.3, we notice an interesting fact- these two functions are equal to

within 10% for | x | < 2, so may often be used nearly interchangeably. The explanation for their agreement is given in Section 3.2, where their Maclaurin series are

presented. Figure 2.4 shows a similar near-coincidence of the cosh and sinh functions for x > 1.5, where they agree to better than 10% and the agreement improves

as x increases because they both tend to the exponential function.

2.3

FUNCTIONS OF COMPLEX VARIABLES

37

FIGURE 2.3 Bounded circular and hyperbolic functions, sine, cosine, and hyperbolic tangent.

FIGURE 2.4 The unbounded circular and hyperbolic functions, tangent, hyperbolic cosine, and

hyperbolic sine. For x greater than about 1.5, the latter two functions are indistinguishable on the

scale of this figure.

The tangent function is undefined in the limit that the cosine function in the denominator of its definition (2.62) tends to zero. For example, in Figure 2.4 values

of the argument of the tangent function within about 0.1 of x = ± /2 have been

omitted.

38

COMPLEX VARIABLES

Trajectories in the complex plane

Another interesting concept and visualization method for complex quantities is that

of the trajectory in the complex plane. It is best introduced by analogy with particle

trajectories in two space dimensions, as we now summarize.

When studying motion in a real plane one often displays the path of the motion,

called the trajectory, by plotting the coordinates x (t) and y (t), with time t being

the parameter labeling points on the trajectory. For example, suppose that

x (t) = A cos ( t) and y (t) = B sin ( ), with A and B positive, then the trajectory is an ellipse with axes A and B, and it is symmetric about the origin of the X- y

coordinates. As t increases from zero, x initially decreases and y initially increases. One may indicate this by labeling the trajectory to indicate the direction of increasing t. The intricate Lissajous figures in mechanics, obtained by superposition

of harmonic motions, provide a more-involved example of trajectories.

Analogously to kinematic trajectories, in the complex plane real and imaginary

parts of a complex-valued function of a parameter may be displayed. For example,

in Section 8.1 we discuss the motion of damped harmonic oscillators in terms of a

real dimensionless damping parameter Expressed in polar-coordinate form, the

amplitude of oscillation is

(2.63)

where the complex “frequency” (if x represents time) is given by

(2.64)

The trajectory of v depends on the range of

square root in (2.64).

Exercise 2.17

(a) Show that if

and on the sign associated with the

1, which gives rise to damped oscillatory motion, then

(2.65)

and that the trajectory of v+ is a counterclockwise semicircle in the upper half

plane, while the trajectory of v- is a clockwise semicircle in the lower half

plane. In both trajectories is given as increasing from -1 to +l.

(b) Suppose that >1, which produces exponential decay called overdamped

motion. Show that v± is then purely real and negative, so the trajectory lies

along the real axis. Show that v- increases from -1 toward the origin as increases, while v+ decreases toward as

increases. n

The complex-plane trajectory, with

as displayed in Figure 2.5.

as parameter, expressed by (2.64) is therefore

2.3 FUNCTIONS OF COMPLEX VARIABLES

39

FIGURE 2.5 Frequency trajectory in the complex plane according to (2.64) as a function of the

damping parameter

As a final note on this example, there is no acceptable solution for v- if < - 1

and if x > 0 is considered in (2.63), since y (x) is then divergent.

Another interesting example of a trajectory in the complex plane arises in the

problem of forced oscillations (Section 10.2) in the approximation that the energy

dependence is given by the Lorentzian

(2.66)

where c is a proportionality constant and the complex Lorentzian amplitudes L± are

given by

(2.67)

where and

are dimensionless frequency and damping parameters. The analysis of this example is similar to the first one.

40

COMPLEX VARIABLES

FIGURE 2.6 Frequency trajectory in the complex plane for the Lorentzian amplitude described

by (2.67).

Exercise 2.18

(a) Show that the Lorentzian amplitudes in (2.67) satisfy

(2.68)

so that the trajectories of L± lie on circles of radius l/2 in the complex plane.

(b) Investigate the details of the trajectory by showing that L+ describes the anticlockwise semicircle in the lower half plane, while L- describes the clockwise

semicircle in the upper half complex plane. For both of these trajectories the directions are for going from

to

n

The trajectories of the Lorentzian amplitudes are shown in Figure 2.6. They are

discussed more completely in Chapter 3 of the text by Pippard in the context of

Cole-Cole plots of complex-valued dielectric constants as a function of frequency.

In both of our examples the trajectories in the complex plane lie on circles. This

is neither a mere coincidence nor is it uncommon, as the following exercise should

convince you.

2.4 PHASE ANGLES, VIBRATIONS, AND WAVES

41

Exercise 2.19

Consider the following complex function z, called a linear fractional transformation of the real variable p according to

(2.69)

, and are complex constants, with

in which

0. Now consider the

z’

that

is

obtained

from

z

by

the

shift

and

scaling

transformation

function

(2.70)

By analogy with the result in Exercise 2.18, argue that z’ lies on a circular trajectory and therefore that the original z in (2.69) lies on a circular trajectory. n

Thus, the functions (2.67) and (2.69) are both special cases of the more general circular trajectory given by (2.70). From these examples we see that the notion of a

trajectory in the complex plane is useful for visualizing the properties of complexvalued functions.

2.4 PHASE ANGLES, VIBRATIONS, AND WAVES

The angle in the complex plane often has interesting interpretations in scientific

applications, particularly in the context of vibrations and waves. In this section we

summarize some of the main results. An encyclopedic treatment is provided in Pippard’s book on the physics of vibration.

The topics introduced here are developed and applied throughout this book. In

particular, Section 8.1 discusses free-motion and resonant vibrations in mechanical

and electrical systems, then the quantum oscillator is considered briefly in Section 8.5. In Chapters 9 and 10 we develop Fourier expansions, emphasizing the

complex-exponential treatment for the discrete, series, and integral expansions.

Phase angles and phasors

Suppose that we have a (real) angle = , where is a constant angular frequency,

= 2 f (with f the frequency) and t denotes time. Then

(2.71)

42

COMPLEX VARIABLES

describes in the complex plane uniform circular motion of the point z1, while the

projections onto the real and imaginary axes (x and y) describe simple harmonic motions.

Exercise 2.20

Prove that the motion of z1 is periodic by showing that

(2.72)

where the period T = 2

n

If a second uniform circular motion in the complex plane is described by

(2.73)

then this motion has the same period as that described by z1, but at a given time z2

has its phase advanced by over the phase of zl.

Whether one refers to a positive value of

as a lag or a lead depends on the scientific field in which one is working. If

> 0, in mechanics the phase of z2 is said

to lug that of zl, whereas in electrical-circuit applications z2 is said to lead zl.

A complex-plane diagram showing the magnitude of z and a relative phase (with

t usually suppressed) is called a vibration diagram or phasor diagram. Its use

gives a visualization of complex-variable relationships which often improves comprehension and interpretation.

Vibrations and waves

We can broaden the discussion of phases to include both spatial as well as temporal

variation in the amplitude of a complex vibration. For example, a wave that has constant amplitude of unity at all points along the x direction and at all times t can be described by

(2.74)

in which the wavenumber, k, is given in terms of wavelength, ?, by

(2.75)

Although the wavenumber is physically a less intuitive quantity than is the wavelength, computationally and in most applications k is a much simpler quantity to deal

with. Note that k has the dimensions of an inverse length, just as the angular frequency, has the dimensions of inverse time. Thus, the argument of the exponential function in (2.74) is dimension-free, as should be the argument of any function

in mathematics.

2.5 DIVERSION: INTERPRETING COMPLEX NUMBERS

43

Recall also that the wave described by (2.74) is monochromatic (unique values

of and k) and that points of constant phase have an associated phase velocity, vp,

given by

(2.76)

Exercise 2.21

Discuss from the viewpoint of wave motion why vp in (2.76) is called the phase

velocity. n

The superposition of such waves of constant phase to build up a dispersive wave in

which components with different frequencies transport energy at different speeds is

an extension of the Fourier expansions in Chapters 9 and 10. A comprehensive and

lucid discussion is given by Baldock and Bridgeman in their book on wave motion.

In Chapters 9 and 10 on Fourier expansions we make detailed study of phenomena described in terms of x or in terms of the complementary variable k, or in terms

oft and its complementary variable

Clear expositions of the relations between

complex exponentials and vibrations are given in detail with many applications in

Pippard’s omnibus book. Vibrations and waves are described very completely at an

introductory level in the book by Ingard.

2.5 DIVERSION: INTERPRETING COMPLEX NUMBERS

The development of the interpretation of complex numbers provides an example of

the consequences of education and of the dominance of scientific thought by mathematical representations. Since many scientists claim that a phenomenon is not understood until it can be described mathematically, it is interesting to discuss the relation

between “the queen and servant of science” and the natural sciences.

Are complex numbers real?

Before the quantum physics revolution of the 192Os, scientists usually apologized

for using complex numbers, since they provided only mathematically convenient

shortcuts and shorthand for problem solving. Indeed, Leonard Euler of Euler’s theorem in Section 2.3 coined the Latin “imaginarius” for the quantity i = . The

first major use of complex numbers was made by C. P. Steinmetz (1865-1923), a

research electrical engineer who used them extensively (as we do in Section 8.1) to

simplify the analysis of alternating-current circuits.

In quantum mechanics, for example in the Schr?dinger equation that we use in

Section 8.5, the wave function is fundamentally a complex variable that is not a

shorthand for two real quantities such as magnitude and phase. Many quantities derived from wave functions, such as scattering amplitudes, are also intrinsically

complex-valued. This leads to the scientific use of “analytic continuation,” a concept

and technique familiar in mathematics but of more recent use in the natural sciences.

44

COMPLEX VARIABLES

Analytic continuation

We have displayed in Figures 2.3 and 2.4 the circular functions and the hyperbolic

functions, respectively. In terms of complex variables, however, these hyperbolic

functions are essentially just the circular functions evaluated for purely imaginary arguments, or vice versa. It is therefore interesting, and sometimes useful, to think of

there being just a single set of functions, say the circular functions, which may be

evaluated along the real axis (then they are the conventional trigonometric functions)

or they may be evaluated along the imaginary axis (then they are the hyperbolic

functions, within factors of i), or they may be evaluated for the argument which

takes on a value anywhere in the complex plane.

When we make this last bold step off either the real or the imaginary axis and

into the complex plane we are making an analytic continuation of the functions.

The concept of analytic continuation and some understanding of the techniques

applied to it are best appreciated by working the following exercise.

Exercise 2.22

Consider the behavior of the complex-valued function of complex-variable argument, Z, defined as follows:

(2.77)

(a) Show that for a real argument A is just the hyperbolic cosine function discussed in Section 2.3, while for purely imaginary z it is the circular cosine

function.

(b) Sketch the graph of cos x along the real axis of the complex-z plane and

the graph of cosh y along the imaginary axis of the same plane. They look

quite different, don’t they?

(c) Devise a graphical representation of A (z) that is suitable for arbitrary complex z, and make some representative sketches of the function thus graphed.

One possible form of representation is to sketch contours of constant Re A and

of constant ImA. n

In scientific research analytic continuation is often a useful technique. As an example, experiments on wave scattering (such as in acoustics, optics, electromagnetism, and subatomic physics) are, at best, obtained in the range of scattering angles from zero to . How would the data look if they could be analytically continued into the complex-angle plane? Similarly, data obtained at real energies or frequencies may be interesting to extrapolate to complex energies or complex frequencies. Indeed, we explore this possibility in discussing the Lorentzian resonances in

Section 10.2.

2.6 PROJECT 2: PROGRAM TO CONVERT BETWEEN COORDINATES

2.6

45

PROJECT 2: PROGRAM TO CONVERT

BETWEEN COORDINATES

The program Cartesian & Polar Coordinate Interconversion

developed in

this project serves both to develop your understanding of the relations between these

two coordinate systems and to give you practice with writing programs in C.

The conversion from plane-polar to Cartesian coordinates is straightforward and

unambiguous. Given r and one has immediately (as discussed in Section 2.2)

(2.78)

which can be programmed directly.

Stepping into the correct quadrant

The transformation from Cartesian coordinates to polar coordinates is less direct than

the inverse transformation just considered. The required formulas are

(2.79)

which is straightforward to compute, and, in terms of the atan or tan-l function,

(2.80)

which is ambiguous. This formula does not uniquely determine the quadrant in

which lies because only the sign of the quotient in (2.80) is available after the division has been made. The relative signs of the circular functions in the four quadrants indicated in Figure 2.7 may be used to determine the angle uniquely from the

signs of x and y.

In some programming languages, including C, two functions are available for

the inverse tangent. In one, such as the atan (t) in C language (with t the argument of the function), the angle is usually returned in the range /2 to /2, and the

user of this function has to determine the appropriate quadrant by other means.

In the second function for the inverse tangent, such as atan2 (y, x) in the C

language, the angle is located in the correct quadrant by the function itself. If we

were to use at atan2 in the program, the conversion from Cartesian to polar representation would be very direct. For practice in C and to reinforce your understanding of

plane-polar coordinates we use the simpler function atan.

46

COMPLEX VARIABLES

FIGURE 2.7 Signs of the circular functions in each quadrant of the Cartesian plane.

If we begin with an angle calculated into the first quadrant by using the absolute

value of y/x, then multiples of /2 have to be added or subtracted to get the angle

into the correct quadrant. Computationally, the best way to get at the accuracy of

your

Чтобы не видеть никакую рекламу на сайте, нужно стать

**VIP**-пользователем.

Это можно сделать совершенно бесплатно. Читайте подробности тут.