A First Course in Scientific Computing: Symbolic, Graphic, and Numeric Modeling Using Maple, Java, Mathematica, and Fortran90 9781400841172

This book offers a new approach to introductory scientific computing. It aims to make students comfortable using compute

214 53 8MB

English Pages 512 [501] Year 2011

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

A First Course in Scientific Computing: Symbolic, Graphic, and Numeric Modeling Using Maple, Java, Mathematica, and Fortran90
 9781400841172

Citation preview

A First Course in Scientific Computing: The electronic materials consist of Java programs, LaTeX files, data files, Maple worksheets, Mathematica notebooks, and Fortran programs. The Maple worksheets are equivalent to the Maple section of the text, but in an interactive format. The Mathematica notebooks repeat these same tutorials via interacting with Mathematica. The Fortran90 programs are equivalent to the Java programs, but in a different computer language. This supplemental material is available online at: http://press.princeton.edu/landau_firstCourse/

A First Course in Scientific Computing Symbolic, Graphic, and Numeric Modeling Using Maple, Java, Mathematica, and Fortran90

A First Course in Scientific Computing Symbolic, Graphic, and Numeric Modeling Using Maple, Java, Mathematica, and Fortran90

RUBIN H. LANDAU Contributors: Robyn Wangberg (Mathematica), Kyle Augustson (Fortran90), ´ M. J. Paez, C. C. Bordeianu, C. Barnes PRINCETON UNIVERSITY PRESS PRINCETON AND OXFORD

iv

c 2005 by Princeton University Press Copyright  Published by Princeton University Press, 41 William Street, Princeton, New Jersey 08540 In the United Kingdom: Princeton University Press, 3 Market Place, Woodstock, Oxfordshire OX20 1SY All Rights Reserved Library of Congress Cataloging-in-Publication Data Landau, Rubin H. A first course in scientific computing: symbolic, graphic, and numeric modeling using Maple, Java, Mathematica, and Fortran90 / Rubin H. Landau; contributors, R. Wangberg, K. Augustson . . . [et al.]. p. cm. Includes bibliographical references and index. ISBN 0-691-12183-4 (cloth : alk. Paper) 1. Science—Data processing. I. Wangberg, R, Augustson K. II. Title 2. Q183.9.L36 2005 3. 502 .85—dc22 2004062443 British Library Cataloging-in-Publication Data is available The publisher would like to acknowledge the author of this volume for providing the camera-ready copy from which this book was printed. Printed on acid-free paper. ∞ pup.princeton.edu Printed in the United States of America 1 3 5 7 9 10 8 6 4 2

v

For Loren Landau and your first book

This page intentionally left blank

Contents

List of Figures

xv

List of Tables

xix

Preface

xxi

Chapter 1. Introduction 1.1 Nature of Scientific Computing 1.2 Talking to Computers 1.3 Instructional Guide 1.4 Exercises to Come Back To

1 2 4 6

PART 1.

7

MAPLE (OR MATHEMATICA) BY DOING

Chapter 2. Getting Started with Maple 2.1 Setting Up Your Work Space 2.2 Maple’s Problem-Solving Environment 2.3 Maple’s Command Structure 2.4 Sums and sums 2.5 Execution Groups 2.6 Key Words and Concepts 2.7 Supplementary Exercises Chapter 3. Numbers, Expressions, Functions; Rocket Golf 3.1 Problem: Viewing Rocket Golf 3.2 Theory: Einstein’s Special Relativity 3.3 Math: Integer, Rational and Irrational Numbers 3.4 CS: Floating-Point Numbers 3.5 Complex Numbers 3.6 Expressions 3.7 Assignment Statements 3.8 Equality (rhs, lhs)

1

9 9 10 14 16 21 22 23 25 25 26 27 29 31 32 34 36

viii

3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

CONTENTS

Functions User-Defined Functions Reexpressing Answers CS: Overflow, Underflow, and Round-Off Error Solution: Viewing Rocket Golf Extension: Tachyons* Key Words and Concepts Supplementary Exercises

Chapter 4. Visualizing Data, Abstract Types; Electric Fields 4.1 Why Visualization? 4.2 Problem: Stable Points in Electric Fields 4.3 Theory: Stability Criteria and Potential Energy 4.4 Basic 2-D Plots: plot 4.5 Compound (Abstract) Data Types: [Lists] and {Sets} 4.6 3-D (Surface) Plots of Analytic Functions 4.7 Solution: Dipole and Quadrupole Fields 4.8 Exploration: The Tripole 4.9 Extension: Yet More Plot Types* 4.10 Visualizing Numerical Data 4.11 Plotting a Matrix: matrixplot* 4.12 Animations of Data* 4.13 Key Words and Concepts 4.14 Supplementary Exercises

36 39 39 44 45 50 51 51 55 55 56 56 58 63 69 73 76 76 91 97 102 104 105

Chapter 5. Solving Equations, Differentiation; Towers 5.1 Problem: Maximum Height of a Tower 5.2 Model: Block Stacking 5.3 Math: Equations as Challenges 5.4 Solving a Single Equation: solve, fsolve 5.5 Solving Simultaneous Equations (Sets) 5.6 Solution to Tower Problem 5.7 Differentiation: limit, diff, D 5.8 Numerical Derivatives* 5.9 Alternate Solution: Maximum Tower Height 5.10 Assessment and Exploration 5.11 Auxiliary Problem: Nonlinear Oscillations 5.12 Key Words and Concepts 5.13 Supplementary Exercises

107

Chapter 6. Integration; Power and Energy Usage (Also 14) 6.1 Problem: Relating Power and Energy Usage 6.2 Empirical Models 6.3 Theory: Power and Energy Definitions

134

107 107 109 110 113 115 117 126 127 128 129 131 131

134 134 136

ix

CONTENTS

6.4 6.5 6.6 6.7

Maple: Tools for Integration Problem Solution: Energy from Power Key Words and Concepts Supplementary Exercises

136 139 143 144

Chapter 7. Matrices and Vectors; Rotation 7.1 Problem: Rigid-Body Rotation 7.2 Math: Vectors and Matrices 7.3 Theory: Angular Momentum Dynamics 7.4 Maple: Linear Algebra Tools 7.5 Matrix Arithmetic and Operations 7.6 Solution: Rotating Rigid Bodies 7.7 Exploration: Principal Axes of Rotation* 7.8 Key Words and Concepts 7.9 Supplementary Exercises

145

Chapter 8. Searching, Programming; Dipsticks 8.1 Problem: Volume of Liquid in Spherical Tanks 8.2 Math: Volume Integration 8.3 Algorithm: Bisection Searches 8.4 Programming in Maple 8.5 Solution: Volume from Dipstick Height 8.6 Key Words and Concepts 8.7 Supplementary Exercises

184

PART 2.

JAVA (OR FORTRAN90) BY DOING

197

Chapter 9. Getting Started with Java 9.1 Compiled Languages 9.2 Java Program Pieces 9.3 Entering and Running Your First Program 9.4 Looking Inside Area.java 9.5 Key Words 9.6 Supplementary Exercises

199

Chapter 10. Data Types, Limits, Methods; Rocket Golf 10.1 Problem and Theory (Same as Chapter 3) 10.2 Java’s Primitive Data Types 10.3 Methods (Functions) and Modular Programming 10.4 Solution: Viewing Rocket Golf 10.5 Your Problem: Modify Golf.java 10.6 Coercion and Overloading* 10.7 Key Words

145 147 149 151 157 171 176 181 182

184 184 185 187 194 195 196

199 201 202 205 207 207 208 208 208 215 219 223 224 229

x

CONTENTS

10.8

Supplementary Exercises

229

Chapter 11. Visualization with Java, Classes, Packages 11.1 2-D Graphs within Java: PtPlot 11.2 Installing PtPlot: See Appendix C* 11.3 Classes and Packages* 11.4 Gnuplot Basics 11.5 Java Archives: jar*

232

Chapter 12. Flow Control via Logic; Projectiles 12.1 Problem: Frictionless Projectile Motion 12.2 Theory: Kinematics 12.3 Computer Science: Designing Structured Programs 12.4 Flow Control via Logic 12.5 Implementation: Projectile.java 12.6 Solution: Projectile Trajectories 12.7 Key Words 12.8 Supplementary Exercises

247

Chapter 13. Java Input and Output* 13.1 Basic Input with Scanner 13.2 Streams: Standard Output, Input, and Error 13.3 I/O Exceptions: FileCatchThrow.java 13.4 Automatic Code Documentation: javadoc 13.5 Nonstandard Formatted Output: printf

232 238 238 240 244

247 248 249 251 258 259 259 260 262 263 263 272 274 275

Chapter 14. Numerical Integration; Power and Energy Usage 14.1 Problem (Same as Chapter 6): Power and Energy 14.2 Algorithms: Trapezoid and Simpson’s Rules 14.3 Assessment: Which Rule Is Better? 14.4 Key Words and Concepts 14.5 Supplementary Exercises

281

Chapter 15. Differential Equations with Java and Maple* 15.1 Problem: Projectile Motion with Drag 15.2 Model: Velocity-Dependent Drag 15.3 Algorithm: Numerical Differentiation 15.4 Math: Solving Differential Equations 15.5 Assessment: Balls Falling Out of the Sky? 15.6 Maple: Differential-Equation Tools 15.7 Maple Solution: Drag ∝ Velocity 15.8 Extract Operands 15.9 Drag ∝ v 2 (Exercise) 15.10 Drag ∝ v 3/2

290

281 282 288 289 289

290 291 292 292 295 297 302 303 306 306

CONTENTS

15.11 Exploration: Planetary Motion* 15.12 Key Words 15.13 Supplementary Exercises

xi 310 311 311

Chapter 16. Object-Oriented Programming; Complex Currents 16.1 Problem: Resonance in RLC Circuit 16.2 Math: Complex Numbers 16.3 Theory: Resistance Becomes Impedance 16.4 CS: Abstract Data Types, Objects 16.5 Java Solution: Complex Currents 16.6 Maple Solution: Complex Currents 16.7 Explorations: OOP Worked Examples* 16.8 Key Words 16.9 Java and Maple Exercises

313

Chapter 17. Arrays: Vectors, Matrices; Rigid-Body Rotations 17.1 Problem: Rigid-Body Rotations 17.2 Theory: Angular-Momentum Dynamics 17.3 CS, Math: Arrays, Vectors, and Matrices 17.4 Implementation: Inertia.java, Inertia3D.java 17.5 Jama: Java Matrix Library* 17.6 Key Words 17.7 Supplementary Exercises

341

Chapter 18. Advanced Objects; Baton Projectiles* 18.1 Problem: Trajectory of Thrown Baton 18.2 Theory: Combined Translation and Rotation 18.3 CS: OOP Design Concepts 18.4 Key Words 18.5 Supplementary Exercises

355

Chapter 19. Discrete Math, Arrays as Bins; Bug Dynamics* 19.1 Problem: Variability of Bug Populations 19.2 Theory: Self-Limiting Growth, Discrete Maps 19.3 Assessment: Properties of Nonlinear Maps 19.4 Exploration: Bifurcation Diagram, BugSort.java* 19.5 Exploration: Other Discrete Maps*

378

Chapter 20. 2-D Arrays: File I/O, PDEs; Realistic Capacitor 20.1 Problem: Field of Realistic Capacitor 20.2 Theory and Model: Electrostatics and PDEs 20.3 Algorithm: Finite Differences 20.4 Implementation: Laplace.java 20.5 Exploration: 2-D Capacitor

385

313 313 317 319 329 330 334 340 340

341 343 344 347 349 353 353

355 356 359 377 377

378 378 380 381 384

385 385 387 389 391

xii

CONTENTS

20.6 20.7

Exploration: 3-D Capacitor* Key Words

393 393

Chapter 21. Web Computing, Applets, Primitive Graphics 21.1 What Is Web Computing? 21.2 Implementation: Get This to Work First 21.3 Exploration: Modify Applet1.java 21.4 Extension: PtPlot as Applet* 21.5 Extension: Applet with Button Input* 21.6 Extension: AWT, JFC, and Swing* 21.7 Example: Baton Applet, Jparabola.java* 21.8 Key Words 21.9 Supplementary Exercises

394

PART 3. LATEX SURVIVAL GUIDE

411

Chapter 22. LATEX for Text 22.1 Why LATEX? 22.2 Structure of a LATEX Document 22.3 Sample Input File (Sample.tex) 22.4 Sample LATEX Output 22.5 Fonts for Text 22.6 Environments 22.7 Lists 22.8 Sections

413

394 396 401 402 403 405 407 410 410

413 414 414 416 420 422 422 425

Chapter 23. LATEX for Mathematics 23.1 Entering Mathematics: Math Mode 23.2 Mathematical Symbols and Greek 23.3 Math Accents 23.4 Superscripts and Subscripts 23.5 Calculus and Sums 23.6 Changing Math Fonts 23.7 Math Functions 23.8 Fractions 23.9 Roots 23.10 Brackets (Delimiters) 23.11 Multiline Equations 23.12 Matrices and Math Arrays 23.13 Including Graphics 23.14 Exercise: Putting It All Together

427 428 431 431 431 432 432 432 433 433 434 435 436 438

Appendix A. Glossary

441

427

CONTENTS

xiii

Appendix B. Maple Quick Reference, Debugging Help

450

Appendix C. Java Quick Reference and Installing Software C.1 Java Elements C.2 Transferring Files from the CD C.3 Using our Maple Worksheets C.4 Using our Java Programs C.5 Installing PtPlot (or Other) Packages C.6 Installing Java Developer’s Kit

461

Bibliography

471

Index

477

461 465 466 466 467 469

This page intentionally left blank

List of Figures

1.1

Left: Computational science is a multidisciplinary field that combines science with computer science and mathematics. Right: A new paradigm for science in which simulation plays as essential a role as does experiment and theory.

2

1.2

A schematic view of a computer’s kernel and shells.

3

2.1

An image of Maple’s classic desktop environment.

10

3.1

Observer Ben on Earth watching Michele on a rocket hitting a golf ball. Ben is at rest in frame O on the left and Michele is moving to the right with velocity v (horizontal arrow) in Frame O . Michele sees her golf ball hit with velocity U  .

25

A function as a meat grinder; arguments x and y go in, function f (x, y) comes out.

37

4.1

Static configurations for two, three, and four electric charges.

55

4.2

Coordinate systems for two- and three-charge configurations.

57

5.1

A stack of blocks. Observe how the bottom blocks get thinner.

107

5.2

A plot showing that x sin x passes through zero endlessly.

113

5.3

A mass m attached to a spring with constant k that is stretched a distance x from its equilibrium position.

129

Electrical power use for Australia (left) and Mad del Plata (right) on an hourly basis over a one-year period.

135

A barbell rotating about a fixed axis in the z direction. In Maple you can grab and rotate this pointplot3d figure.

145

Left: A 2-D rectangular plate of side 2 with the origin at its center and a mass at each corner. Right: A cube of side 2 with the origin at its center and a mass at each corner. In Maple you can grab and rotate these pointplot3d figures.

146

3.2

6.1

7.1 7.2

xvi

LIST OF FIGURES

7.3

7.4

8.1 8.2

9.1

Left: Representation of a vector as an arrow in 2-D space. Right: Representation of a vector as an arrow in 3-D space. The projection of the arrow along each axis equals the components of the vector. In Maple you can rotate the 3-D figure. The angular velocity vector ω in black and the angular momentum vector L in gray. In Maple you can grab and rotate this figure. A spherical tank of radius R filled with a liquid to height H above its bottom. A graphical representation of the steps involved in solving for a zero of f (x) using the bisection algorithm. In each step the algorithm takes the midpoint of the interval as its new guess for x, thereby reducing the interval’s size by one half. The first interval is (x1 , x−1 ), the second (x2 , x−2 ), etc. Steps in compiling and executing a Java program.

10.1 An illustration of the limits of single-precision floating point numbers and the consequences of exceeding those limits. 10.2 Left: The structure of a Java class with only a main method. Right: The structure of a Java class with two methods. 10.3 A plot of the function γ(v) output from the program Golf.java using PtPlot. Compare to Maple’s plot in Chapter 3. 11.1 Sample output from advanced use of PtPlot in which two plots are placed side by side (see TwoPlotExample.java). 11.2 Sample output from PtPlot in which three data sets are placed on one plot. Observe the error bars on two sets. 11.3 The Format submenu located under the Edit menu in a PtPlot application. This submenu controls the plot’s basic features. 11.4 A gnuplot graph for three data sets with impulses and lines. 11.5 Gnuplot’s surface plot of a scattering amplitude ImT as a function of complex energy E . 12.1 Left: The trajectory of a projectile fired with initial velocity V0 in the θ direction. The nonparabolic curve includes air resistance. Right: The x and y components of V0 . 12.2 A flowchart illustrating a program to compute projectile motion. On the left are the basic components of the program and on the right are details. When writing a program, first map out the basic components, and then design the structure and fill in the details. 12.3 Left: Sequential or linear programming. Right: The if-thenelse structure, one of several used in nonlinear programming.

148

171

184

186 200

211 215 221

233 235 236 241 242

247

250 251

LIST OF FIGURES

12.4 Left: For-loop iteration (test before action). Right: Do-loop iteration (test after action). 12.5 A break iteration. 13.1 A sample of the automatic code documentation produced by running javadoc on the program TrapMethods.java. 14.1 Three models of power consumption. The time is in 100 days and the power is in gigawatts. 14.2 Left: Straight-line sections used for the trapezoid rule. An individual trapezoid with area h2 [f (xi ) + f (xi+1 )] is highlighted. Right: Parabolas used in Simpson’s rule (a single parabola is fit to each pair of consecutive intervals). 14.3 Energy consumption as a function of time for model 1 computed with Maple. 15.1 Left: The gravitational force on a planet a distance r from the sun. The x and y components of the force are indicated. Right: Output from the applet PlanetRHL showing the precession of a planet’s orbit when the gravitational force ∝ 1/r4 . 16.1 Left: An RLC circuit connected to an alternating voltage source. Right: Two RLC circuits connected in parallel to an alternating voltage. Observe that one of the parallel circuits has double the values of R, L, and C as does the other. 16.2 Representation of a complex number as a vector in space. 16.3 An abstract drawing, or what? 16.4 Superposition of two waves with similar wave numbers (a PtPlot). 16.5 The trajectory of a satellite as seen from the sun. 17.1 Left: A plate sitting in the x − y plane with a coordinate system at its center. Right: A cube sitting in the center of a three-dimensional coordinate system. 18.1 The baton before it is thrown. “x” marks the CM. 18.2 Left: The initial conditions for the baton as it is thrown. Right: The baton spinning in the air under the action of gravity. 18.3 Left: The trajectory (x(t), y(t)) followed by the baton’s CM. Right: The applet JParabola.java showing the entire baton as its CM follows a parabola. 19.1 The insect population xn versus generation number n for various survival rates: (A) µ = 2.8, a period-one cycle; (B) µ = 3.3, a period-two cycle; (C) µ = 3.5, a period-four cycle; (D) µ = 3.8, a chaotic regime.

xvii

255 256

275

282

283 289

310

314 315 319 334 336

342 355 356

357

380

xviii

LIST OF FIGURES

19.2 The bifurcation plot, attractor populations versus survival rate, for the logistics map. 20.1 Left: Electric-field lines betweens the plates of an ideal parallel-plate capacitor. The equal spacing and single direction indicate a uniform field. Right: A representation of the field between the plates of a realistic capacitor. The field tends to “fringe” and extend beyond the ends of the plates. 20.2 Left: A parallel-plate capacitor within a grounded box. A realistic capacitor would have the plates closer together in order to condense the field. Right: A visualization of the electric potential for this geometry. The contours projected onto the xy plane give the equipotential surfaces. 20.3 Left: The capacitor’s field is computed only for those (x, y) values on the grid. The voltage of the plates and containing box are kept constant. Right: The algorithm for Laplace’s equation in which the potential at the point (x, y) = (i, j)∆ equals the average of the potential values at the four nearestneighbor points. 20.4 Contour plot of capacitor’s equipotential surfaces. The electric-field lines are perpendicular to these contours and point toward lower potential.

382

386

386

388

393

21.1 When running an applet, a compiled class file is executed from the command line or from a Web browser. 21.2 A screen dump of the computer screen showing output from Applet1. 21.3 Left: The coordinate system used to define the window (pixel) coordinates. The upper left corner corresponds to the point (0,0), the width of the window is 400 pixels, and the height is 350 pixels. The ywin coordinate increases downwards. Right: An image of the computer screen showing Applet2. 21.4 Flowchart of the applet JParabola.

401 408

22.1 The steps followed and utilities used in preparing a document with LATEX. The source file is Paper.tex, the deviceindependent file is Paper.dvi, and the file ready for printing/posting is Paper.ps or Paper.pdf.

414

23.1 A scaled-down and rotated Figure 9.1.

437

394 396

List of Tables

8.1 8.2

Maple’s relational operators. A truth table showing how Boolean variables combine.

187 188

10.1 Java’s basic data types and their sizes in bytes (B). 10.2 The use of three bits a, b, and c, to represent the eight integers from 0 to 23 − 1 = 7. 10.3 Java’s mathematical functions. 10.4 Results of double- and single-precision computations. 10.5 Results of double- and single-precision computations for the second case.

209 218 222

11.1 Some of Java’s packages and the classes they contain.

239

12.1 12.2 12.3 12.4 12.5 12.6

Relational operators of Java. Logical operators of Java. Truth table. Logical flow control structures in Java. Java’s increment and decrement operators. Java’s compound assignment operators.

252 252 253 254 257 258

13.1 13.2 13.3 13.4 13.5 13.6 13.7

Control characters for formatting output. String to primitive type conversion methods. File readers and writers. Tag forms for javadoc. Nonstandard formatted I/O commands (static methods). Components of the printf/scanf formatted I/O commands. Sample formatted output for floating-point numbers.

264 265 270 274 276 276 277

209

222

19.1 Maps for generating xn sequences and bifurcation plots.

384

22.1 A sample floating table with a number and a caption.

425

This page intentionally left blank

Preface

This book contains an introduction to scientific computing appropriate for all lower-division college students. Its goal is to make students comfortable using computers to do science and to provide them with tools and knowledge they can utilize throughout their college careers. Its approach is to introduce the requisite mathematics and computer science in the course of solving realistic problems. On that account care is given to indicate how each discipline uses its own language to describe the same concept, how their tools are useful to us, and how computations are often concrete examples of abstract ideas. This is easier said than done. On the one hand, lower-division students are simultaneously learning elementary mathematics and physics, and so this may be the first place they encounter the science and mathematics used in the problems. On the other hand, in order for the tools and techniques to be useful for more than the assigned problem, we give more than an introduction (the original title of this book) to the computational tools. We address the first issue in our teaching by reminding the students that our focus is on having them learn the techniques in the proper context, and that any new science and mathematics they become familiar with will make it easier for them in their other courses. We address the second issue by placing an asterisk * in the title of chapters and sections containing optional materials and by reminding the students of which sections are most appropriate for the problem at hand. This book covers some of the basics of computation, numerical analysis, and programming from a computational science point of view. We want the reader to acquire some ideas of what is possible with computers, what type of tools there are for it, and how to go about getting all the pieces to work together. After that, it is easy to use on-line help or the references to get more details. As a result, our presentation is more practical and more focused on mathematics and science than an introductory programming or computer science text, with minimal discussion of computer science theory. The book follows our own personal preference for “just enough” computer information in that it avoids going through every option for every command and instead presents realistic examples. We follow the dictum that science and engineering students learn computing best while sitting down at a computer in a trial-and-error mode. Hence, we adopt

xxii

PREFACE

a tutorial approach in which readers work along with us in solving a problem, learn by doing, and then work on their own version of the problem (there are also additional exercises at the end of each chapter). We use the command-line mode for the compiled languages that makes the tutorial as universal as possible, and, we believe, is better pedagogically. It then follows that this book is closer to a workbook than a reference book. Yet because one always comes back to find worked examples of commands, it should be valuable for reference as well. A problem solving environment such as Maple or Mathematica is probably the easiest way to start scientific computing, is natural to use with trial and error, and is what we do in Part 1. Its graphical interface is friendly, it shows the user a wide spectrum of what can be done with modern computation, such as symbolic manipulations, 2-D, 3-D visualization and linear algebra, and is immediately useful in other courses and for writing technical reports. After the first week with Maple or Mathematica, students with computer fright usually feel better. These environments also demonstrate how computers can give an immediate response in beautiful mathematical notation, or fail at what should be the simplest of tasks. Part 2 of the text is on Java (or Fortran). We believe that learning to program in a compiled language teaches more of the basics of computation, gets closer to the actual algorithms used, teaches better the importance of logic, and opens up a broader range of technical opportunities (jobs) for the students than the use of a problem solving environment. In addition, compiled languages also tend to be more powerful and flexible for numerically intensive tasks, and students naturally move to them for specialized projects. Likewise, after covering Parts 1 and 2 of the text it may make sense for a student to use environments like Matlab, which combine elements of both compiled languages and problem solving environments. Many students may find that the logic and the precision of language required in programming is more challenging than anything they have ever faced before. Others find programming satisfying and a natural complement to their study of mathematics and foreign languages. We try to decrease the slope of the learning curve by starting the neophytes with sample programs to run and modify, rather than requiring them to write all their own programs from scratch. This process is more exciting, saves a great deal of time otherwise spent in frustrating debugging, and helps students learn by example. Even though it might not be evident from all the hype about Java and Web computing, Java is actually a good language for beginning science students. It demands proper syntax, produces useful error messages, is consistent and intelligent in handling precision, goes a long way towards being computer-system independent, has Sun Microsystems providing free program-development environments [SunJ], and runs fast enough for nonindustrial purposes (its speed is increasing, as are the number of scientific subroutine libraries being developed in Java). Part 3 of the text provides a short LATEX survival guide. A number of colleagues have suggested the need for such materials, and since using LATEX is quite similar

PREFACE

xxiii

to compiling a code, it does make a useful extension of the text. Even though we do not try to reveal the full power and complexity of LATEX, we do give enough of its basic elements for the reader to write beautiful-looking scientific documents. Depending on how many chapters and modules are used, this book contains enough materials for a one- or two-semester course. Our course has one lecture and two labs every week, with roughly one instructor for every 10 students in lab. Attending lectures and reading the materials before lab are important in acquainting the students with the general concepts behind the exercises and in providing a broad picture of what we are trying to do. The supervised lab is where the real learning occurs. We believe that a modern student should be acquainted with several approaches to scientific computing. Notwithstanding our avowed claim that there are multiple paths leading to good scientific computing, we have had to make some choices as to what to place in the printed version of this book and what to place on the CD. The basic ideas behind scientific computing are language independent, yet the details are not. For all these reasons we have decided to cover Maple, Java, and LATEX in the printed version of this book, but to place other languages on the CD that comes with the text. The CD is platform independent and has been tested on Windows, Macs, and Unix. The Java and Fortran programs are pure text. The Maple and Mathematica files, however, require the respective programs to execute them. The pdf files will require Abode Acrobat, which is free. Any difficulties with the CD should be reported to [email protected]. Additions and corrections to the CD are found on our Web pages (http://www.physics.orst.edu/˜rubin/IntroBook) or through Princeton University Press Web pages (http://pup.princeton.edu/titles/7916.html). Specifically, the CD contains Java programs, LATEX files, data files, and various supplementary materials. As indicated, it also contains Maple worksheets with essentially identical materials as in the Maple section of the text but in an interactive format that we recommend over reading the paper version. Furthermore, the CD contains essentially identical materials to the Maple tutorials as Mathematica notebooks. These can be read with Mathematica or printed out as an alternative version of the text. Likewise, the CD also contains the Java materials of the text converted over to Fortran90, as well as the appropriate Fortran programs. Though we do not recommend trying to learn two languages simultaneously, having alternative versions of the text does present some interesting teaching possibilities. Additions and corrections to the CD are found on our Web pages.

Acknowledgements This book was developed on seven year’s worth of students in the introductory scientific computing class at Oregon State University. The course was motivated by the pioneering text by Zachary [Zach 96] and encouraged by [UCES]. The

xxiv

PREFACE

course has been taught by Albert Stetz, David McIntyre, and the author; I am proud to acknowledge their friendship and the inclusion of their materials in the text. I have been blessed with some excellent student assistants without whose efforts the course could not be taught and the materials developed; these include Matt Des Voigne, Robyn Wangberg, Kyle Augustson, Connelly Barnes, and Juan Vanegas. Valuable materials and invaluable friendships have also been contributed by Manuel P´aez and Cristian Bordeianu, coauthors of our Computational Physics text, and Sally Haerer. They have helped make this book more than the introduction it would have been otherwise. I look forward to their continued collaborations. Financial support for developing our degree program in computational physics and associated curricular materials comes from the National Science Foundation’s Course, Curriculum, and Laboratory Improvement program directed by Duncan McBride, and the Education, Outreach and Training Thrust area of the National Partnership for Computational Infrastructure, under the leadership of Gregory Moses and Ann Redelfs. The courses and materials have benefited from formative and summative assessments by Julie Foertsch of the LEAD Center of the University of Wisconsin. This work has also benefited from formative comments by various reviewers and colleagues, in particular Jan Tobochnik and David Cook. Thanks also goes to my editor, Vickie Kearn at Princeton University Press, who has been particularly insightful, encouraging, and courageous in helping me develop this multidisciplinary text and having it turn out so well, and to Ellen Foos for her caring production of the book. And Jan Landau, always. RHL

Chapter One Introduction

1.1 NATURE OF SCIENTIFIC COMPUTING Computational scientists solve tomorrow’s problems with yesterday’s computers; computer scientists seem to do it the other way around. —anonymous

The goal of scientific computing is problem solving. The computer is needed for this, because real-world problems are often too difficult or complex for analytic or human solution, yet workable with the computer. When done right, the use of a computer does not replace our intellect, but rather leverages it by providing a super-calculating machine or a virtual laboratory so that we can do things that were heretofore impossible. The mathematical modeling and problem-solving orientation of scientific computing places it in the discipline of computational science. In contrast, computer science, which studies computers for their own intrinsic interest, provides the underpinning for the development of the hardware and software tools that computational scientists use. As illustrated on the left of Figure 1.1, computational science is a multidisciplinary field that combines a traditional discipline, such as physics or finance, with computer science and mathematics, without ignoring the rigor of each. Books such as this, which employs materials from multiple fields, aim to be the central bridge in this figure, connecting and drawing together the three fields. Studying a multidisciplinary field is challenging. Not only must you learn more than one discipline, you must also work with the separate languages and styles of the different disciplines. To illustrate, a computational scientist may be pleased with a particular solution because it is reliable, self-explanatory, and easy to run on different computers without modification. A computer scientist may view this same solution as lengthy, inelegant, and old-fashioned. Each may be right in the sense that they are making judgments based on the differing values of different disciplines.

2

CHAPTER 1

Figure 1.1 Left: Computational science is a multidisciplinary field that combines science with computer science and mathematics. Right: A new paradigm for science in which simulation plays as essential a role as does experiment and theory.

Another, possibly more fundamental, view of how computation is playing an increasingly important role in science is illustrated on the right of illustrated in Figure 1.1. This symbolizes a paradigm shift in which science’s traditional foundation in theory and experiment is extended to include computer simulation. While one may argue that the future will see the CSE on the left of Figure 1.1 get absorbed into the individual disciplines, we think all would agree that the simulation on the right of Figure 1.1 will play an increasing role in science. 1.2 TALKING TO COMPUTERS As anthropomorphic as your view of computers may be, it is good to keep in mind that a computer always does exactly as told. This means that you should not take the computer’s response personally, and also that you must tell the computer exactly what you want it to do. Of course programs can be so complicated that you may not care to figure out what they will do in detail, but it is always possible in principle. Thus it follows that a basic goal of this book is to provide you with enough understanding so that you feel well enough in control, no matter how illusionary, to figure out what the computer is doing. Before you tell the computer to obey your orders, you need to understand that life is not simple for computers. The instructions they understand are in a basic machine language1 that tells the hardware to do things like move a number stored in one memory location to another location, or to do some simple, binary 1 The “BASIC” (Beginner’s All-purpose Symbolic Instruction Code) programming language should not be confused with basic machine language.

3

INTRODUCTION

ls

cd dir

javac

xmaple

Figure 1.2 A schematic view of a computer’s kernel and shells.

arithmetic. Hardly any computational scientist really talks to a computer in a language it can understand. Instead, when writing and running programs, we usually talk to the computer through a shell or in a high-level language. Eventually these commands or programs all get translated to the basic machine language. A shell is a name for a command-line interpreter, that is, a place where you enter a command for the computer to obey. It is a set of medium-level commands or small programs, run by the computer. As illustrated in Figure 1.2, it is helpful to think of these shells as the outer layers of the computer’s operating system. While every general-purpose computer has some type of shell, usually each computer has its own set of commands that constitute its shell. It is the job of the shell to run various programs, compilers, and utilities, as well as the programs of the users. There can be different types of shells on a single computer, or multiple copies of the same shell running at the same time for different users. The nucleus of the operating system is called, appropriately, the kernel. The user seldom interacts directly with the kernel, but the kernel interacts directly with the hardware. The operating system is a group of instructions used by the computer to communicate with users and devices, to store and read data, and to execute programs. The operating system itself is a group of programs that tells the computer what to do in an elementary way. It views you, other devices, and programs as input data for it to process; in many ways it is the indispensable office manager. While all this may seem unnecessarily complicated, its purpose is to make life easier for you by letting the computer do much of the nitty-gritty work that enables you to think

4

CHAPTER 1

higher-level thoughts and communicate with the computer in something closer to your normal, everyday language. Operating systems have names such as Unix, OSX, DOS, and Windows. In Part 1 of this book we will use a high-level interpreted language, either Maple or Mathematica. In Part 2 we will use a high-level compiled language, either Java or Fortran90. In an interpreted language the computer translates one statement at a time into basic machine instructions. In a compiled language the computer translates an entire program unit all at once. Compiled languages usually lead to faster programs, permit the use of vast libraries of subprograms, and tend to be portable. Interpreted languages appear to be more responsive, interactive, and, consequently, more “user friendly.” When you submit a program to your computer in a compiled language, the computer uses a compiler to process it. The compiler is another program that treats your program as a foreign language and uses a built-in dictionary and set of rules to translate it into basic machine language. As you can imagine, the final set of instructions is quite detailed and long, especially after the compiler has made several passes through your program to translate your convoluted logic into fast code. The translated statements ultimately form an executeable code that runs when loaded into the computer’s memory. 1.3 INSTRUCTIONAL GUIDE Landau’s Rules of Education: Much of the educational philosophy applied in this book is summarized by these three rules: 1. Most of education is learning what the words mean; the concepts are usually quite simple once you understand what you are being told. 2. Confusion is the first step to understanding. 3. Traumatic experiences tend to be the most educational ones. This book has an attitude, and we hope you will develop one too! We enjoy computing and relish the increased creativity and productivity resulting from powerful computing tools. We believe that computing has so become part of the fabric of science that an introductory scientific computing course should be part of every lower-division university student’s education. Hence we deliberately mix the languages of mathematics, science, and computer science. This mix of languages is how modern scientists think about things, and since ideas must be communicated with these same words and ideas, this is how the book is written. However, we are sensitive to the confusion multiple definitions may reap and do provide a section at the end of each chapter indicating some key words and concepts, and a glossary at the end of the book defining many technical terms and jargon.

INTRODUCTION

5

Because we aim to give an introduction to computational science, we cover some basics of numerical analysis, information about how data are stored, and the concordant limits of computation. However, we present very little discussion of hardware, computer architecture, and operating systems. That is not to say that these are not interesting and important topics, but rather that we want to get the reader busy computing and acquiring familiarity with concrete examples. In our experience, science and engineering students need this practical experience before they can appreciate the more abstract principles of computer science. We have aimed our presentation at first- and second-year college students. We believe that the chapters and sections not marked optional with an asterisk * provide a good introductory course for them. The inclusion of the optional chapters would raise of the level of the presentation and lead to a longer course. To maintain the logical organization of materials, we have intermixed the optional chapters with the others. However, there should be nothing in the optional chapters that is required in order to understand the chapters that follow. It is hard to keep up with the rapid changes in computer technology and with the knowledge of how to use them. That is not such a big issue with scientific computing, because it is the basic principles of mathematics, logic, science, and computing that are important, and not the details of hardware and software. Nevertheless, students and faculty often do not enjoy computing because of the frustration and helplessness they feel when computers do not work the way they should. Although we commiserate with those feelings, one of the things we try to teach is that it is not unusual to have new things not work quite right, but that if you relax and follow a trial-and-error approach, then you usually find success working around them. Be warned, there are some exercises in this book that give the wrong answer, that lead to error messages, or that may “break” the program (but not harm the computer). Learning to cope with the limits of computation gets easier and less traumatic after you have done it a few times. We understand, however, that many instructors may not appreciate a computer’s failure that they cannot explain, and so we have assembled an Instructor’s Survival Guide, available upon request through the Web. It is important that students become familiar with the material in the text before they come to lab. A lecture helps, but reading and working through the materials is essential. Even though it may not be that hard to work through the problems in lab by having instructors and fellow students prompt you with the appropriate commands to enter, there is little to be gained from that. Our goal is to make this introductory experience very much a “lab” that develops an attitude of experimentation and discovery and that nurtures rewarding feelings when a project finally computes correctly.

6

CHAPTER 1

Many of the problems we assign require numerical solution and therefore are not covered in elementary texts. These include nonlinear oscillators, the motion of projectiles with drag, and the rotation of cubes about an arbitrary axis. Notwithstanding our prediction that other elementary courses and texts will eventually be more multidisciplinary, some readers may feel that we are requiring them to understand phenomena at a level higher than in their other courses. Our hope is that the readers will recognize that they are pioneers, will be stimulated by their new-found powers, and will help modernize their other courses. On the technical side, we have developed the materials with Maple 6–9.5, Mathematica 4.2, and the Java Development Kit (JDK or Java 2 Standard Edition, J2SE) [SunJ]. Even though studio and workbench environments are available, we prefer the pedagogical value in using a shell to issue separate commands for compilation and execution, and then having to deal with the source and class files directly. In addition, many students are able to load JDK onto their home computers and work there as well. We have found that WinEdt [WinEdt] and TextPad work well to edit and run source code on a Windows platform, and that Xemacs [Gnu] with Java tools is excellent for Unix/Linux machines. In addition, jEdit, the Open Source programmer’s editor [jEdit], is an excellent tool that, because it itself is written in Java, runs on most any platform. Visualization is very important in computational science. It is built into Maple and Mathematica and is excellent. Part of the power of Java is that it has strong graphical capabilities built right into the language, although calling them up is somewhat involved. Consequently, we have adopted the free, open source package PtPlot [PtPlot] as our standard approach to plotting with Java. However, gnuplot [Gnuplot] and Grace [Grace] are also recommended as stand-alone applications. For 3-D graphics, a more specialized application, we give instructions on using gnuplot and refer the interested reader to OpenDx [DX] and VisAd [Visad]. These too are free, powerful, and available for Unix/linux and Windows computers. 1.4 EXERCISES TO COME BACK TO Consider the following list of problems to which a computational approach could be applied. Indicate with an “M,” “J,” or “E” whether the best approach would be the use of Maple, Java, or either. 1. 2. 3. 4. 5. 6.

calculate the escape velocity from Jupiter write a spreadsheet (accounting) program from scratch solve problems from a calculus textbook prove an algebraic identity determine the time required for a sky diver to reach terminal velocity write a compiler for a programming language

PART 1

MAPLE (OR MATHEMATICA) BY DOING

This page intentionally left blank

Chapter Two Getting Started with Maple

2.1 SETTING UP YOUR WORK SPACE An important part of scientific computing is being organized. Good organization provides you with more time for the important things like being original and creative in your work, as well as giving you more time to get your work done (or to play). This organizational concept is particularly true for scientific computing, where you may have to deal with many files or many versions of the same file. For students, good organization also keeps you on task. An important step in organization is knowing where you have stored things. One of the best ways to get organized is to create files in directories and subdirectories (also called folders and subfolders). You need to be able to navigate through your directories, see your files, and tell what is in them by their names. In addition, you need to be able to create, copy, move, and remove files and directories. Finally, you should protect your files from accidental or unintentional intrusions.1 For whatever operating system you are using, try out and get familiar with the commands to: • list the names of all files and directories in your working directory or folder • list the names with details of all files and directories in your working directory or folder • display the name of the directory in which you are working • make (create) a directory • copy file1 to file2 and then delete file1 • copy file1 to a different directory and then delete local file1 • copy file1 to file2, leaving copy of file1

To get started: • Create a subdirectory Intro in your home directory. • Create subdirectories Week1, Week2, . . ., Week10 within directory Intro. • Place a copy of the Maple worksheet version of Appendix B, Maple Quick 1 How

to do these things under Unix is explained in [L&F 93] or the Web tutorial [UnixWeb].

10

CHAPTER 2

Figure 2.1 An image of Maple’s classic desktop environment.

Reference, into the Intro directory. One way to do that is to Open the CD file with Maple and then SaveAs into your directory. • Change to the subdirectory Week1 and use it for this weeks’ work. 2.2 MAPLE’S PROBLEM-SOLVING ENVIRONMENT Maple is an example of what computer scientists call a problem-solving environment. View it as a complete computing system (a really big program) that does nearly everything you need for mathematical calculations and does it within an intuitive, graphical user interface. Maple and its cousins, like Mathematica, extend scientific computations from dealing with numbers to also manipulating algebraic symbols. Even though problem-solving environments are also used for numerical calculations, compiled languages are faster and more flexible. (Matlab, while also called a problem-solving environment by some, is predominantly a friendly interface for doing numerical calculations. It is often used for high-powered design, simulations, and data analysis.)

2.2.1 Look Around, Smell the Roses Ideally, you are reading this as an electronic Maple worksheet (a file with the suffix .mws) within Maple. If not, we recommend that you do so or at least open up a Maple window to experiment with. To do so on a Unix/Linux operating system, type xmaple & at a Unix prompt. To do so on other systems, start it as you would any other program. You should get an interface like that in Figure 2.1.

11

GETTING STARTED WITH MAPLE

Look around in the window and note how it has dropdown menus and buttons that make it look very much like a word processor or a text editor, with which you may already be familiar. Pull down the menus and take note of the types of functions they perform. Before you get in too deep, note that the File menu has both Close and Exit as options. Close will terminate the Maple window that you are currently working on, while Exit will terminate the entire Maple session. One of the worst experiences in computing is losing a file containing all of your hard work. Beware, even though you may have assigned a name to a file, its contents do not get permanently stored until you issue the Save or Save As command. Although Maple will usually ask if you want to save a file before discarding it, it is a good idea for you to explicitly save your file in the desired location before exiting or closing. You save a file with the Save command (the Save command under the File pull-down menu). In fact, it is a good idea to save your file on a regular basis while working on it; then even if Maple crashes, you will not lose all your work. The Save As command under the File pull-down menu lets you change the format of the file as well as the location where it is to be stored. (Make sure you save your files in a directory where you will find them again; the default directory may be in some godforsaken part of the computer.) Exercise: Now do as you are told! • Save this worksheet to your home directory or one of your other places. • Now Close this window and see what happens. • Now Exit Maple, start it again, and then Return Here.



• Welcome back! Observe that there is a valuable Help menu as the right-most menu. Before you try it out, be warned that the Help window may cover up the window in which you are reading this. You get back to this screen by going to the Window menu, which will show you all the windows that you have open. You select this one to place it on top. • Try out the Help facility and notice that you can search for commands, search on topics, or go on a tour. • Try selecting (with your mouse) a word in the preceding text, such as Exit. Then go to the Help menu and note there is automatically a menu item there for help on Exit. Go ahead and read what it says. • Go to the Help menu again and search for information on Help. Examine the changes in the windowing environment and the arrows on the Help dashboard

12

CHAPTER 2

pointing right and left. They are hyperlinks to other pages for you to try. • Select (highlight) the above paragraph by dragging your mouse over it (holding down the left mouse button). Now try out the buttons that center the selected text, that right- and left-justify it, and that make it bold (B), italic (I) or underlined (U). Likewise, you can select some text and change the values in the little white windows to the left of the B button and get all types of formatting options.

While we are talking about what Maple does with text, it is a good idea to experience Maple’s active section and subsection notation. Right below this paragraph is a boxed + indicating a collapsed section. Mouse clicking on the + changes it to a − and opens up the material in the section for viewing. Do it now and experience the power of Maple! Clicking on a − reverses the process; do it too to close up the sections.

2.2.2 Try It, You’ll Like It! In a well-designed Maple worksheet, or book for that matter, the section and subsection titles should form an outline of the material covered. This outline assists the learning process, since looking at just the titles of the sections of a document helps map out your journey through the material. Try it with this document: • Go to the View menu and select Expand All Sections. Then select Collapse All Sections. Imagine how useful collapsing and expanding sections is for writing technical documents; you can first construct an outline by creating the section and subsection titles, and then fill in the material after the logic is clear. • Whoops! You have now uncovered some of the materials that we have tried to hide in collapsed sections. So, close this document without saving it, and open up the original again. • Return Here. During our ordering you about, you may as well open and close some windows. In fact, it is good to learn how to open and close windows properly before you run the risk of losing some valuable work. Try closing and then opening the window within Maple in which this text appears. With a Windows operating system, a window is closed by depressing the lower most button on the upper right side with the x in it; on a Unix/Linux system, a window is closed by pushing the lower-most button on the upper left side with a − in it, and then selecting the Close option. You will then have to go to the File menu and select Open, or select the name of this file, if Maple was nice enough to write its name there. • Try minimizing (iconizing) and then maximizing the window within Maple in which this text appears. With a Windows operating system, you minimize and

GETTING STARTED WITH MAPLE

13

maximize by depressing the lower-most button on the upper right side with an underscore in it; on a Unix/Linux system this is done by pushing the lower-most button on the upper right side with a center dot . in it. Then click on the maple-leaf icon to get the full window back. • Finally, find the button that makes the window in which you are reading this text smaller or larger—but does not close or minimize the window. On a Windows machine that button has a single box, or maybe two overlapping boxes in it; on a Unix machine it is a depressed or raised (shaded) single box. • Maple is placed in different modes when writing or when doing mathematics. Check that you are still in text [T] mode, go to the Insert menu, and insert a paragraph. This action just makes some space for you to enter the new paragraph, you must still do the writing. To do that, enter text in the space inserted for the new paragraph. Insert a section and enter several lines of nonsense text. Insert two subsections, entering several lines of text after each. Check if your little boxes are working right, and then try deleting your additions when they are in both expanded and collapsed forms. • Now try playing with the indent buttons (arrows, someplace above the B), as well as the three sizes of magnifying glasses or x’s (real cute) that let you progressively enlarge the screen as your eyes get tired (or old). Inspect the explanation of a button’s purpose that appears on the information bar at the bottom of your screen when you push that button and hold it down. • Before you start thinking that Maple is just a program for technical writing, take note of the three adjacent buttons on the task bar (on the top of the window) with Σ, T, and [> on them. • Press the T button. This action places you in text mode, in which you get these cute windows letting you control the text format, type, and size. Click your mouse into some existing text and start typing something. • Next push the Σ button and note that it tells you that this is for “inserting inert standard math into a text region.” (Inert math looks good but has no symbolic value.) This option  is useful for writing reports (like this), since you can insert (−2) into your text. Usually inert math is black and fancy symbols like ∞ i=1 i active math is red when you enter it. However, some of the colors in this worksheet have been lost as it was transferred down from one generation of Maple to another.  (−2) . If We could also have inserted math in executable or active mode as ∞ i=1 i you select either symbol by clicking on it, you can then use the button with the Maple leaf on it to toggle (switch back and forth) between executable and inert modes. • It sometimes may seem a little hard to get out of the executable math mode. If

14

CHAPTER 2

that happens to you, try pressing the T or the [> button to get back into one of the other modes. Just for fun, use your mouse to select the mathematical summation symbol above (just click on it). You will notice that the Maple command that is used to create this symbol appears in a little window at the top of the screen. Now click on the little x button that appears when the symbol is highlighted, and note how the command gets slipped into the text in place of the symbols. If you highlight the command and click again, the symbol is slipped back into the text. This series of commands acts like another toggle switch. • Seeing that you have messed up this worksheet over which we have labored so long, you do not want to save it, since that would replace our nice version with your messed-up one. (You may use the Save As option on the File menu to save your messed-up version as a different file.)

2.3 MAPLE’S COMMAND STRUCTURE At last we will do some mathematics with Maple. • Pressing the [> button places you in the execute mode that permits manipulation of symbols and expressions. The greater-than symbol > is Maple’s way of prompting you to enter a command to the right of it (in fact, it is called a “prompt” for just this reason). When you enter commands after the [> prompt, Maple gets to work for you. If you enter commands while Maple is in the text mode, then Maple just copies them onto the worksheet and sits there awaiting your next entry. • Place Maple into execute mode, tell it to add 3 + 82, and then hit the Enter/Return key. Observe that your command to Maple shows up in red and is in a fixed-width font like a typewriter or a teletype. The teletype font is meant either to evoke the aura of the computing days of the past or to indicate that this is your command to the computer. Check next that Maple speaks back to you in blue and black. If you did as told, Maple has probably responded with a blue streak: Warning, premature end of input

This warning is Maple’s way of saying that it does not think your command is over yet, even though you appear to think so. To end your command properly you have place a semicolon ; at the end of the line before hitting the Enter key (on some computer systems you may have to hit the Return key). • So, go back into execute mode [>, insert two prompts into the text, enter 3+82; after the first prompt, and then hit Enter. This command should give 85 as an answer and leave you at the next prompt [>. • Now use the arrow keys to go back up in this worksheet and fix your previous command by placing the missing ; at its end (after the 82). Then pressing Enter

15

GETTING STARTED WITH MAPLE

should fix things up. In general, when you make a mistake you can go back to the command you have already entered, correct it, and then hit Enter/Return any place in the command to execute the command again. This correction is a lot better than trying to enter the entire command again, especially when the commands get complicated and you are annoyed with the computer and the slow pace of learning. (It is an often-experienced but not fully tested fact that computers seem to know when you are annoyed with them, and then react by giving you an even harder time.) • To get the most out of this tutorial you should personally key in the Maple commands rather than cut and paste them to a Maple prompt. In order to force this point, we make some commands inert. A good way to enter commands is to go to the File menu and start a New window within Maple (you may have many windows open simultaneously). • If you go back to the first [> prompt in the Maple worksheet version of this text and hit Enter, you will note how after executing your command, Maple automatically skips down through the text and output to the next [> prompt and waits there for your next command. In this case, Maple’s response means that unless you get into the habit of inserting some extra prompts in your document, you may end up skipping over some valuable material.) So find your way back up to this point in the worksheet and note the action of the Tab key. It jumps you through the worksheet with stops at each prompt or executable expression. • In the next section you will enter a new calculation on your worksheet directly after the (blue) result of a previous calculation. To do that, place your cursor to the left of the previous result (possibly 85), and press the execute [> button, causing a new execution group to start at that location.

2.3.1 Maple as a Pocket Calculator Maple does everything a pocket calculator does and much more. It understands the usual arithmetic operators, + and - for addition and subtraction, uses * and / for multiplication and division, and understands either ** or ˆ for exponentiation (raising to a power). As an example, 3 × 45 − 7 6 would be entered as 3 * 45 - 7 ˆ 6, where the spaces do not matter. Go ahead and try it by placing your cursor anywhere after the prompt below and hitting Enter. # This field is a nonexecuted comment

> 3 * 45 - 7 ˆ 6 ;

−117514

Look at how Maple has jumped to the next prompt. If we had not placed an extra [> there, you would have had to look for the cursor and bring it back to here.

16

CHAPTER 2

2.3.2 Rules of Precedence When performing calculations like those above, it is important for Maple to know the order in which you want the various operations performed. The order of executions (also followed by most pocket calculators) is ˆ

then

*

or

/

then

+

or

-

There may still be ambiguity, depending upon the way in which you apply this rule. As a case in point, let us say we want to add 4 to 2 and then multiply the sum by 3. Have Maple try this by evaluating the (ambiguous) expression 4 + 2 * 3. • If you got the answer 10, then you did not do what we wanted. Clearly, this calculation is a case where the order of operations matters. Try (4 + 2) * 3, where the parenthesis makes our intentions clearer. • Next try 400/10/2 and explain your result. Because computers tend to do division from left to right (left associative), Maple knows what it is doing, but the result may not be what you want. • Now see if 400/10/2 is the same as (400/10)/2 or 400/(10/2). In the worksheet we give you only one prompt for this as a way of encouraging you to try entering the three commands on a single line with a semicolon ; after each command, but only one Return at the end of the line. • Remember, if there is a chance that the results may be ambiguous or unclear, use parentheses to force the operations you want and to ensure clarity. For instance, enter 3 ˆ 4 ˆ 5ˆ 6. # Enter 3 ˆ4 ˆ5 ˆ6

>

Maple tells you something is wrong. At times when Maple cannot figure out what you have told it to do, it may respond by just repeating your command back to you (a polite way of suggesting that you get it right next time).

2.4 SUMS AND SUMS Many problems in science and mathematics require you to evaluate sums. At times the sums are over a finite number of terms, and at other times the number of terms is infinite. With a little cleverness you can write a sum as a single expression. As an instance, here is an infinite sum ∞  (−1)i x(2 i) i=0

(2 i)!

17

GETTING STARTED WITH MAPLE

The summation index i starts from 0 and runs all the way to infinity. We show two related ways to enter sums into Maple. The first is # Revealing, but inert

> Sum((-1)ˆi * xˆ(2*i) / (2*i)!, i = 0..infinity); ∞  i=0

(−1)i x(2 i) (2 i)!

In spite of this output being pretty and being a great way to check that you have entered the sum correctly, it is inert (cannot be used to do the math). The active form for this sum, as well as a number of other functions in Maple, is obtained by writing the function without the capital letter, in this case as sum(. . .). Try converting the passive sum to an active sum: # Paste and edit passive form here

>

If life is fair, you should have obtained cos x as the value that Maple actually evaluates for the sum. This means that Maple has recognized this infinite sum as the power-series expansion for cos x. Actually, you can have the best of both worlds; use the command Sum to have Maple show you what it thinks you want (useful for debugging your typing), and then enter value(%) to have Maple perform the calculation: > Sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i = 0..infinity ); ∞  (−1)i x(2 i) i=0

(2 i)! # Evaluate previous expression

> value (%);

cos(x) > Sum( (-1)ˆi*xˆ(2*i) / (2*i)!, i = 0..infinity ) = sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i = 0..infinity ); ∞  (−1)i x(2 i) i=0

(2 i)!

# Use both forms

= cos(x)

Take a close look at the Sum() command we have used here. Like all Maple commands, it has a format that should be followed precisely (although inserting spaces causes no harm). To begin with, note that after the name of the command there is a pair of parentheses. The parentheses contain the arguments to the command. In this case, there are two arguments separated by a comma. The first argument or field contains the expression to be summed, while the second field contains the range of values to be covered by the summation index. Descriptions of ranges in Maple are of the form i = a..b or i = a .. b, where a and b are the lower and upper limits of the range (b = ∞ for our example). If you deviate from this format, Maple may tell you that you did something wrong,

18

CHAPTER 2

or Maple may assume that you know what you are doing (Maple is good at math, not at judging character) and make a reasonable attempt to follow your command. To illustrate, try these: > Sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i =0 . infinity ) = # Left off a . in range sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i = 0.infinity ); Error, missing operator or ‘;‘ > Sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i = 0 .. infty ) = sum ((-1)ˆi * xˆ(2*i) / (2*i)!, i = 0..infty); infty  i=0

# Spelled infinity wrong

(−1)i x(2 i) = cos(x) + x(2 infty+2) + · · · (2 i)!

In the first case you should get an error message, while in the second case Maple evaluated the sum treating infty as if were just another algebraic symbol.

2.4.1 Strings and Quotes In those cases when there is not an inert form of a command, you can still get Maple to produce output that contains your command and Maple’s response on the same line. You accomplish this task with the judicious use of quotes. However, since Maple contains three types of quotes, "double quotes," ’left quotes,’ and ’right quotes,’ this whole quoting business gets to be rather confusing at times (Landau’s second rule): Maple’s quote types. Example Name "a string" double quotes, quotation marks ‘John Dull‘ left quotes, accents grave, back quotes ’x ˆ 3’ right quotes, apostrophes

Function creates a string delimits names suppresses evaluation

We start with double quotes. Anything placed inside of double quotes becomes a string. A string is just a sequence of characters that is treated literally with no inherent meaning or value. As an example, the string "this is now a string" is just letters and spaces to Maple. However, variables can be assigned to strings, as if the strings were their values, and these variables can then be manipulated. (We shall see in our study of Java input and output, that strings are parsed to extract their meaning.) Here are some examples: > "3 * 45 - 7 ˆ 6" = 3 * 45 - 7 ˆ 6;

# String in double quotes, output shows quotes

”3 * 45 - 7 ˆ 6” = -117514 > ‘3 * 45 - 7 ˆ 6‘ = 3 * 45 - 7 ˆ 6;

# Left quotes, quotes do not show

19

GETTING STARTED WITH MAPLE

3 * 45 - 7 ˆ 6 = −117514 # Right quotes, delays execution

> ’3 * 45 - 7 ˆ 6’ = 3 * 45 - 7 ˆ 6;

−117514 = −117514

In this case we see that the double quotes showed the command as entered, with the quotes themselves visible in Maple’s response. The left (back) quotes produced a similar output, but with the quotes not visible and with a font change. In contrast, the right quotes may have delayed some execution, but not enough for this simple a calculation to stop Maple from returning just a number. We will often use these left and right quotes to produce nice output and to assist in the learning process. Likewise, you may want to use them to produce nice worksheets. In the next examples we see more clearly that the left quotes just show us what command we have entered, while the right quotes have Maple set the command in proper mathematical notation, but not evaluate the expression: > ‘sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i = 0 .. infinity )‘= # Delay execution sum ( (-1)ˆi *xˆ(2*i) / (2*i)!, i = 0 .. infinity ); ∞  (−1)i x2i i=0

2i!

= cos x

> ’sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i = 0 .. infinity )’= # Display command sum ( (-1)ˆi *xˆ(2*i) / (2*i)!, i = 0 .. infinity );

sum ( (-1)ˆi * xˆ(2*i) / (2*i)!, i = 0 ..

infinity )

= cos x

Now we enter some strings and manipulate them: # Use of string

> "a + b " = a + b;

“a + b ” = a + b > String := "whatever I put here";

# String now a variable

String := “whatever I put here” > String;

“whatever I put here” # LHS = a string

> "String" = String;

“String” = “whatever I put here” An important use of the backquote ‘ is to enclose names containing spaces or special characters that might otherwise give Maple trouble. For example, if the name you give to a variable contains a space, then Maple (and Unix) will assume

20

CHAPTER 2

that it is two names. Yet if you place the two names within back quotes, then the two words will be treated as a single grouping: # Not acceptable as has blank

> My name; Error, missing operator or ‘;‘

# Now acceptable

> ‘My name‘;

My name > ‘My name with colon:‘;

My name with colon :

# Colon does not end line

Scrutinize how the item within left quotes is not the same as a string (which is just a literal recall of the characters); the name in back quotes is the true name of some item in Maple: # Set ‘My name‘ to numeric value.

> ‘My name‘ := 10;

My name := 10 > ‘My name‘ ;

> ‘My name‘ := "Rubin";

# Print out value of ‘My name‘

10

# Set ‘My name‘ to a string

My name := “Rubin”

> ‘My name‘;

# Print out value of ‘My name‘

“Rubin”

See again, in contrast, how right quotes delay the execution of a command: # Evaluate the sine of Pi;

> sin(Pi);

0 > ’sin(Pi)’;

sin(π)

> eval(%);

0

# Enter, but do not evaluate the sine of Pi; # Now evaluate the previous expression.

In summary, use double quotes ". . ." for strings, left quotes ‘. . .‘ for a name that contains spaces or special characters, and right quotes ’. . .’ to delay execution of a command. 2.5 EXECUTION GROUPS In the electronic version of this book, we usually want you to observe the effect of a single, or each group of, commands one at a time. For this reason we often place the symbol [> in our Maple worksheets to stop execution. As a case in point, ⎡ ⎣

> ’sin(3.)’

= sqrt(10.) sin(3.) = 0.1411200081 √ 10. = 3.162277660

= sin(3.); ’sqrt(10.)’

GETTING STARTED WITH MAPLE

21

[>

Monitor how Maple jumps through all these commands in a flash and then stops at the blank prompt. Now remove these lines of output (either by selecting them and hitting the Delete key, or by using the commands in the Edit submenu), place your cursor at the last of the three commands (the one dealing with sqrt(10)), and hit Enter again. What is happening here is easy to understand. The Maple prompt (the symbol that indicates Maple is ready to have a command entered) is the greater-than sign >. The long and short square brackets on the left indicate execution groups. The rule is that Maple executes all commands within an execution group and then jumps to the next execution group to resume execution. So in our case, we see that Maple stopped at the [> symbol because it is the next execution group, not because it was a blank prompt! To test this rule, select and then delete the blank prompt above this paragraph, and execute the preceding execution group. You should notice that Maple now jumps to the execution group below this paragraph. An execution group may contain things like text and graphics, in addition to commands, and may get to be quite long. As such, they are a convenient way to organize your worksheets. In this book we will keep our execution groups small in order to encourage you to study the consequence of each command. We suggest that you also keep your execution groups short as a way of reviewing what your worksheet is doing and of checking that it is working properly. When you insert an execution group, Maple also inserts a command prompt from which you execute commands. To insert an execution group: • Go to the Insert menu. • Choose Execution Group. • Choose Before or After Cursor.

To add more command prompts to an existing execution group, • Place the cursor to the left of an existing >. • Hit the Enter key. • Check that the new prompt appears above the original >.

On occasion you end up with a bunch of execution groups that would be better off joined together. To join two or more execution groups: • Place the cursor in the first of the two execution groups to be joined together. • From the Edit menu, choose Split or Join, and then Join Execution Groups. • Press the Enter key.

22

CHAPTER 2

Alternatively, each computer system has some function keys that let you join and split execution groups rather easily. (They are F3 and F4 on the systems we have tried.) The joining of execution groups works from top to bottom, the splitting from bottom to top. Each time you enter the command, another group gets added or split. To illustrate, the following (whose mathematics we will come to in due course) is a single execution group, with one prompt before and one after the commands: ⎡

>

⎢ > ’(xˆ4 + 7*x - 33)ˆ3’ = expand( (xˆ4 + 7*x - 33)ˆ3 ); ⎢ ⎣ > Diff (%, x) = diff(%, x); >

Try splitting this group at the beginning of the Diff line to produce two execution groups. Then try joining the split groups into one execution group. Execute the result to make sure both commands are done in a single execution. 2.6 KEY WORDS AND CONCEPTS active mathematical expression execution group mathematical expression precedence rules

arithmetic expression hidden section on-line help strings

command inert prompt

1. Explain the meaning of these key words. 2. Mathematics is often called the universal language of science. Evaluate this remark in the context of what Maple does. 3. What is the difference between a Maple command and a Maple response? 4. How does Maple differ from a pocket calculator? 5. How does the computer know the difference between a string and the name of a variable? 6. If computers at their heart understand only numbers, how does Maple understand algebraic symbols? 2.7 SUPPLEMENTARY EXERCISES 1. Use the text mode of Maple to write a worksheet containing: a. Your name b. Your educational level c. Your computer experience d. What you would like to get out of this course (or book) Convert your name into a bold font, convert your educational background into italics, and underline your experience. It is a good habit to save your worksheet often as you are working on it, and especially before trying to

23

GETTING STARTED WITH MAPLE

2. 3.

4.

5.

6.

7.

8.

9.

print it out. Sooner or later it is likely that Maple will freeze up or quit on you and lose track of the active worksheet. That being the case, you will only have to make up the work since your last save. Save your worksheet as a file in the directory Intro and the subdirectory Week1. Do not close the worksheet yet. Print out this first worksheet and hand it in. To do that, you may have to look at the page that comes up when you issue the print command and pick the printer to which you want the output sent. Use the sum command to find a simple expression for the sum of the first n integers, 1 + 2 + . . . n. You might want to experiment with the commands simplify(%) and factor(%) to get a compact expression. What is the ratio of (n + 1)! to n!? How about (n + 50)! to n!? What happens when you try to calculate the ratio of (n + m)! to n!? Use simplify(%) to get a better idea of the result. Do you obtain what you expect to? Explain. Make three different execution groups, with the last two ending in a blank line of execution (that is, as >). a. Have each group contain four lines, and have each line raise 2 to a power equal to the sum of the line number plus execution group number. b. Now enlarge each execution group so it contains an additional line for input, namely, an extra >. c. Add comments on your choice to one of the execution groups. Evaluate a. sin(pi), sin(PI), and sin(Pi) and explain, b. the natural logarithm of e, the base for natural logarithms, c. the natural logarithm of 10, d. the logarithm to the base 10 of 10. Explore two ways of accessing Maple’s on-line help. a. Use Maple to select the word help from the above line and then go to the Help menu on the Maple dashboard (in the upper right), and get help on help. b. Insert an execution group on this worksheet and enter the ?help command. Verify that the following infinite series equals π/4:

1 1 1 + − + ... 3 5 7 10. (Optional) Although you may not believe it, the following infinite series equals π/4 for all 0 < x < π : 1−

sin(3x) sin(5x) + + ... 3 5 See if you can manipulate Maple to give this result. Even getting Maple to indicate that the sum is independent of x is a step in the right direction. 11. Use the precedence and arithmetic rules of Maple to predict the values of each of the following expressions: sin(x) +

24

CHAPTER 2

a. 2 × 3 + 4/2 b. 5 − 6 + 72 c. 1 − 23/4 12. Use the precedence and arithmetic rules of Maple to predict the values of each of the following expressions: a. (4/2)3 − 78 + 7**8 b. 10/14 c. 10./14 d. 8/(4/2) e. 8/4/2 f. 62 + 5 − 7 g. 8 + 24 /2 13. For the following inputs, what would Maple produce as output: a. sum ( (-1)ˆi / (2*i)!, i = 1 .. 2 ) b. Sum ( (-1)ˆi / (2*i)!, i = 1 .. 2 ) c. sum ( (+x)ˆi / (2*i)!, i = 1 .. 2 )

Chapter Three Numbers, Expressions, Functions; Rocket Golf Near Lightspeed

Note to Instructor: This problem, as stated, requires the same level of relativity as covered in elementary physics texts and is thus appropriate for first- or second-year college students. Even if the students do not understand the full implications of the two equations we use, (3.4) and (3.5), it is their application that is the point of this chapter. However, it is conceptually challenging to go further and ask about the golf ball’s trajectory as viewed in different frames, as this involves the questions of simultaneity and acceleration in relativity. 3.1 PROBLEM: VIEWING ROCKET GOLF Michele, a golf fanatic, would like to break the world’s record for the fastest golf ball hit. Money being no object, she rents a rocket and hits her golf balls from the moving rocket. The rocket is fast, with a speed of c/2, where c is the speed of light (299 792 458 m/s). (This really is fast considering that no object with mass can attain a velocity √ of c.) As indicated in Figure 3.1, Michele hits her drive with  a speed of U = 1/ 3c, at an angle θ = 30o , as she measures with respect to

y

U’

y’ Ben

Michele θ

O

x

O’

x’

Figure 3.1 Observer Ben on Earth watching Michele on a rocket hitting a golf ball. Ben is at rest in frame O on the left and Michele is moving to the right with velocity v (horizontal arrow) in Frame O . Michele sees her golf ball hit with velocity U  .

26

CHAPTER 3

the moving rocket. She observes her drive to remain in the air for a hang time T  = 2.6 × 107 seconds (which is almost a year!). Ben, an observer on the earth, sees her rocket moving to the right with a velocity v = c/2, and this velocity somehow gets added to that of Michele’s golf ball. Problem a: Since some readers may not be familiar with special relativity, we do not expect you to solve this problem on your own, or even to understand all the equations. Although the theory of special relativity does have it subtleties and fine points, it is accessible to beginning students and we will use only a few simple results. We will lead you through the solution for two cases and then ask you to follow our steps for a third case. This should give you a good working knowledge of some tools (the main focus of this chapter). The third case you work out is the interesting one in which round-off error arising from the use of floating-point numbers (“floats” or “doubles”) becomes evident. Usually it takes many computational steps before you see the effect of round-off error, but in this problem it arises fairly simply. Your explicit problem is: 1. How would Ben, watching Michele’s drive from the earth, describe the golf ball in terms of its speed, angle φ, and hang time T ? 2. How would the answer to 1. change if Michele hit her ball to the left with the same inclination θ, namely, in a direction opposite to the rocket’s velocity? 3. How would the answer to 1. change if Michele hit her ball to the left at an angle 60o below the horizon, namely, with θ = 240o ? 4. If Michele hit the ball with a speed U = c, how would the answers change?

3.2 THEORY: EINSTEIN’S SPECIAL RELATIVITY Being told that an object is traveling with a velocity near that of the speed of light is a hint that the problem is not a simple mechanics one. Indeed, this means that we need to apply Einstein’s special theory of relativity [Smith 65, Ser 00] to it. As indicated in Figure 3.1, we start by considering Ben in a reference frame O with axes (x, y) attached to the earth, and Michele in frame O with axes (x , y  ) attached to the moving rocket. Relative to Ben, frame O (the rocket) is moving along the positive x axis with uniform velocity v . Likewise, Michele feels herself at rest in the rocket and sees Ben moving to the left with velocity −v . Both are equally correct and agree on their relative velocity; indeed, this is the reason it is called the theory of relativity. Ben and Michele may agree on their relative velocities, but they do not agree on measurements of times and distances. As an example, if Michele says that her golf ball was in the air for a period of time T  , then Ben will say that the ball was in the air for a longer time ∆T = γ T  (time dilation),

(3.1)

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

27

where γ is a function of relative velocity v that is always greater than 1: γ=

1 > 1. 1 − v 2 /c2

(3.2)

In view of the fact that the speed of light c is so much larger than any velocities we normally encounter, γ is usually very close to 1 and only gets large for exceedingly high velocities (normally restricted to elementary particles). The fact that Ben would measure a longer hang time than Michele is called time dilation and is often described with the phrase “moving clocks run slow.” The additional result we need from special relativity relates the velocities seen by Michele and Ben. If Michele in the moving rocket O sees her golf ball having velocity components U  = (Ux , Uy ), (3.3) then Ben on earth O, will see her golf ball move with velocity components U = (Ux , Uy ), Ux =

(3.4)

Ux + v , 1 + vUx /c2

Uy =

Uy , γ(1 + vUx /c2 )

(3.5)

where γ is the function (3.2) as before. If you study (3.4) a bit, you will notice that the numerator describes the usual addition of relative velocities, while the denominator is the relativistic correction. In turn, the relativistic effects are in the numerator of (3.5). 3.3 MATH: INTEGER, RATIONAL AND IRRATIONAL NUMBERS There are different types of numbers that are used in mathematics and in computation. For instance, there is a countable infinity of integers, 0, 1, 2, 3, . . ., and nearly twice that many if we also include negative integers. Maple stores these integers exactly and tries to retain infinite precision for its pure mathematical operations with them. Yet while a computer program may be smart, the computer is finite and sooner or later it will run out of room if we try to compute with very large numbers that are stored exactly. For this reason computers often store very large (and very small) numbers approximately, using the floating-point representation, a variation of scientific notation. We shall discuss that soon. Even though the number of integers is infinite, there is a yet larger infinity of rational numbers, namely, of numbers formed by the ratio of two integers, N/D. Maple stores these rational numbers exactly, by storing the numerator and denominators as integers and then displaying a fraction rather than a bunch of digits. To see what this means in action, enter these operations and interpret the results: 1/3;

4/2;

2485/3479;

3(1/3);

3 * (1/3);

28

CHAPTER 3

After the Maple prompts we obtain:1 1/3

2

5/7

3

1

There are also numbers in our world that cannot be expressed as the ratio of integers, and so would require an infinite number of digits to store. These are called irrational numbers and include numbers like π or e (the base of natural logarithms). When Maple encounters irrational numbers, it usually uses the familiar symbol for them rather than approximating them by a fraction or a decimal. By way of example, see what happens when you ask Maple to use some of its built-in functions to determine the angle whose cosine is −1, or raise e to the first power: > arccos(-1);

π > ’exp (1)’ = exp (1);

e=e

These results are more than pretty symbols. Maple knows the meaning of these and other symbols, as seen from the response to log(%). Here the percent sign % means “the previous expression,” and %% means the one before that. Try it: 1. Modify the previous command with arccos(-1) and add a cos(%) at its end. 2. Likewise, add a log(%) to the command exp(1). 3. In both cases explain the effect of execution: > arccos(-1);

cos(%);

π > ’exp (1)’ = exp (1);

−1

log(%);

e = e,

1

Maple understands more than just symbols, it actually knows an incredible amount about a myriad of functions, too. Some common ones are ln(x), log10(x), abs(x), cos(x), sin(x), sqrt(x), arcsin(x), cosh(x).

Others are given in an Appendix B or found under Maple’s Help menu. Now and then you will need to include π or e, or some other irrational, into one of your commands. Maple normally accepts expressions like pi, Pi, PI, e, or E as algebraic symbols. You may always define these symbols to be what you want (with techniques that we will get to shortly), but Maple has predefined many such symbols for general use. 1 Instructor’s

note: Maple interprets 3(1/3) as a function 3 with argument 1/3 and evaluates it as 3.

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

29

So, now we prompt you to run Maple and deduce what it thinks of each. You should have found that cos(pi) and cos(PI) produce nice output, since Maple recognizes pi and PI as lower- and uppercase Greek letters. However, they are treated like any other algebraic symbol and do not have values associated with them (unless you have assigned values to them, of course). In contrast, Maple recognizes Pi as the ratio of the circumference to the diameter of a circle and evaluates cos(Pi) = -1 just fine. However, we do have to input the rather inelegant exp(1) to get the mathematical e (this was not true in previous versions of Maple and may well change again). 3.4 CS: FLOATING-POINT NUMBERS In spite of integers being neat to work with, there are limits to the kind of calculations possible with them. Occasionally you might want to see what the numerical value of an irrational number like π or sin(1) is, or you may need to deal with a really large number, like the size of the universe. Numbers with decimal points are stored on the computer in a format known as floating-point notation or real. This notation is similar to scientific or engineering notation of the form mantissa × 2 exponent . The term “floating-point” denotes the idea that the decimal (or binary) point moves around or “floats” during calculations in order to keep the number of digits in the mantissa constant. One easy way to tell Maple that you want to do some work using floatingpoint numbers (“floats”) is to place a decimal point in one of the numbers of your calculation. This automatically makes it a floating-point number, and if one of your numbers is a float, Maple often assumes that you want all the other numbers to be as well. To experience this in action, enter and observe Maple’s response to each of the following (we give answers here): 1/3,

1/3

1/3.,

1./3,

0.3333333333

1./3.,

0.3333333333

2 + 1./3.

0.3333333333

2.333333333

The other way to get Maple to do a calculation using floats or reals is to explicitly tell it to evaluate some expression as a floating-point number. This is done with the evalf function or command (the f means “float”): > eval (1/3) ;

evalf (1/3) ;

1 , 3 > evalf (1/3, 40) ;

0.3333333333 # Evaluate as a float with 40 digits

0.3333333333333333333333333333333333333333

30

CHAPTER 3

If you count the number of 3’s in the first case, you will see that Maple has stored 10 decimal places (the word length). This is the default value and is about as good as a pocket calculator. Actually, there is a variable internal to Maple called Digits that determines this number of significant figures, and it is usually set to 10. You may change the value of Digits quite easily if you want more or less precision: > Digits := 4 ;

# Sets global number for float digits

1/3.;

Digits := 4

0.3333

3.4.1 Powers of 10 We have just seen how Maple stores numbers in floating-point or scientific notation. Unless you are dealing with an exact power of 2, like 256, this floating-point representation of a number is always approximate, since only a finite number of digits is used to store the mantissa. In the course of discussing exponents and exponentials, which are related but different, it is useful to see how to enter a number as a power of ten in Maple.2 Enter each of the following with the spacing exactly as given, and explain Maple’s response to each (we give answers here in the printed text): > 3E6;

3 E6;

3E 6;

0.3 107 > 3e6;

3*E6;

3.*E6;

# One good, three bads

3.E-6;

Error, missing operator or ‘;’ · · · 3*e6;

# Variations on a theme

3eˆ1;

0.3 107 3 E6 3. E6 3 e6 3. > ln(1 * eˆ1);

exp(1);

ln(e)

e

ln(exp(1));

1

2.718281828

exp(1.);

ln(exp(1.));

0.9999999998

We deduce from these results that: • With no spaces, Maple recognizes 3E6 and 3e6 as the floating-point number 3 × 106 , with mantissa 3 and (decimal) exponent 6. • The spaces in 3 E6, 3E 6, and 3 e6 lead Maple to believe that E6, 3E, or e6 are algebraic symbols, in which case it believes you have left out an operand that would connect it to the rest of the expression. • In contrast, the expressions 3*E6 and 3*e6 are quite acceptable to Maple, and it assumes that you just want to multiply 3 by E6 and e6, respectively. Thus while this representation may look like scientific notation, it is not. 2 Although

the computer stores them as powers of 2, it is easier for humans to work with powers of 10.

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

31

• Likewise, eˆ1 raises the symbol e to the first power. Even though this expression looks like the base to the natural logarithm system, evaluating its logarithm shows otherwise. • The last four cases are illuminating. As we have seen before, Maple has a built-in exponential function exp() and it knows that exp(1) is the irrational base of the natural logarithm system e. Consequently, ln(exp(1)) = 1 exactly. However, when we repeat these same steps starting with exp(1.), the decimal point places Maple in floating-point mode, in which numbers are stored only approximately, and, consequently, the ln(exp(1.)) does not exactly equal 1.

3.5 COMPLEX NUMBERS A complex number is√the sum of a real number and an imaginary number. An 3 imaginary number is −1 times a real √ number. Usually mathematicians and physicists use the symbol i to represent −1, while engineers often use the symbol j ; regardless of its name, it is the same number, and it does not really exist in √ our world. Maple, in turn, marches to its own drummer and uses I to represent −1. To see how this works (we will encounter complex numbers in a number of places again), we give you some examples: > ’sqrt (-1)’ = sqrt (-1);



# What does Maple return?

−1 = I # Complex number algebra

> 1 + (2 + 2*I) / (2 - 2*I);

1+I > convert (%, polar);

> polar( % % );

> sqrt(I);

> % * %;

√ π polar( 2, ) 4 √ π polar( 2, ) 4 √ 2 1 √ + I 2 2 2 √ 2 1 √ 2 ( + I 2) 2 2

# Convert to polar notation (r,θ)

# Alternate way to convert

# Square root of i (not obvious answer)

# Verify result by squaring

# Not what expected so simply

> simplify (%);

I 3 If you need some review of complex numbers, you may want to read the introductory material in Chapter 16 in the Java part of this book.

32

CHAPTER 3

3.6 EXPRESSIONS We started our work with Maple by using it as an expensive pocket calculator. You key in commands one at a time and Maple evaluates them one at a time. Maple does much more. One of the truly unique things it does is mathematics, including calculus, with algebraic symbols. Many of the commands in Maple work with either numbers or symbols. We shall see some examples of that. In a semantic sense, most of the commands that we have so far given to Maple are expressions and statements. Whereas you may know what these terms mean in common English, they have rather specific meanings in both computer science and mathematics.4 Expressions are objects that have numerical values (or would have values if the variables contained in them had values). Commands like 1 + 4; and sin(Pi/2) are expressions with values 5 and 1 respectively. On the other hand, exp(x) - 1 is also an expression, but it has a numerical value only if x has previously been assigned a value. Try it: # Evaluate ex − 1

> exp(x) - 1;

ex − 1 > x := 3;

# Assign value to x, evaluate ex − 1 again

exp(x) - 1;

x=3

e3 − 1

Observe, when Maple just echoes what you entered, it is being diplomatic and telling you that it heard what you have said but cannot do anything about it. If you want more, then you need to tell it more!

3.6.1 restart and unassign When we refer to a constant, we mean an object whose value does not change. As an instance, PI = π is such a constant. When we refer to a variable, we mean a data item whose value may change. For example, the circumference C = 2 * PI * r will have different values depending upon the radius r of the circle. When we refer to a symbol, such as x, we view it as one would in algebra; namely, as an abstract object representing something more concrete. Sometimes x may have a numerical value, and at other times it may not. In either case we may manipulate it using the rules of algebra and calculus. When a symbol has been assigned within a worksheet, it normally stays assigned until you change it or until you close that particular worksheet. (It is often useful to have several worksheets open at one time, possibly even different 4 This

is an example of Landau’s First Rule of Education.

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

33

versions of the same worksheet.) Every so often you may be happily working away or editing deep within a worksheet that was working just fine for you, and for some reason your statements no longer appear to be working correctly. The problem may be that you have forgotten that some variable has been assigned previously, and you are using it as if it had not been. That being the case, you need to unassign the variables. Also, if you just jump into the middle of a worksheet, you do not know that variables have been assigned or that packages have been called, in which case it would be safest to “start with a clean slate.” If Maple really seems to be working improperly, you should save your files, exit Maple, and then start it again. To start again with all new variables as if it were a whole new session: # Unassign all variables

> restart;

Unless you have reason not to, it is a good idea to issue the restart command at the beginning of each new group of operations. To unassign a single variable, use single quotes to assign that variable back to a literal or symbolic version of itself, or use the unassign command: # The assignment statement

> x := 10;

x := 10 > x;

> x := ’x’;

x := x

> x;

> x := 10;

# Tell me present value

10

# Assign as symbol. Or use unassign(’x’); # Tell me present value

x x;

unassign(’x’);

x;

x := 10, 10 x := x x

3.6.2 Exercise (How Expressions Are Stored) The factorial operation n! = n(n − 1)(n − 2) · · · (2)(1) occurs frequently in mathematics. If n is an integer, then Maple evaluates n! just as you would expect. Run Maple to determine what is the ratio of (1001)! to 1000!: # The 1000’s should cancel

> 1001!/1000!;

1000

However, when n is a symbolic variable with no value assigned to it, Maple prefers to store it as n! rather than as the product of many terms. This makes sense because it saves memory and because it makes further manipulations easier:

34 > (n + 1)!/n!;

CHAPTER 3 # Some simple ratios

(n + 50)!/n!;

(n + 1)! (n + 50)! n! n! It may appear that Maple is arrogantly repeating to you the command that you have just issued, yet a more understanding view is that Maple does not think like you and so has to be told more explicitly just what you have in mind. In this case, Maple has worked out an answer that apparently looks fine to it, while we might prefer something simpler. So we tell Maple to simplify the answer: > ’(n + 1)!/n!’

# Simplify RHS

= simplify((n + 1)!/n!);

(n + 1)! =n+1 n! Exercise: Calculate the ratio of (n + 50)! to n!, remembering to simplify. Calculate the ratio of (n + m)! to n!, where n and m are algebraic. (Maple may output Γ for the gamma function. This is the correct answer. You would have to tell Maple that m and n are integers to get m! as the answer.) ♠

3.7 ASSIGNMENT STATEMENTS indexStatements Whereas expressions have values, statements cause things to happen. In other words, statements have side effects, such as assigning values to variables or plotting up some graphs. The most common sort of statement is the assignment statement setting one object equal to another: # Expression: current value of Digits

> Digits;

10 > 1./3.;

# Expression: performs calculation

0.3333333333

# Assignment statement: assigns value to x

> x := 1./3 ;

x := 0.3333333333 > Digits := 4;

# Assignment statements

x := 1./3;

Digits := 4

x := 0.3333

We see that the side effects of these statements are that Digits (which determines the precision of floating-point calculations) will subsequently have the value 4, and that x will have the value 0.3333. Because assignment statements set things equal to each other, they must contain an equal sign. The equal sign := with a colon in front is called an assignment

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

35

operator. It may be thought of as saying “set the variable on my left equal to the value on my right.” To a computer this means “go to the location in memory where the value of the variable on my left is stored, and place the value on my right into that memory location.” In contrast to conventional programming languages, like C or Java, Maple permits the value on the RHS to be purely symbolic. In spite of the assignment statements above looking like equations, they do not express the same type of equality as algebraic equations. To illustrate, when we say x = y in algebra, we mean that the objects on both sides of the equal sign are the same. Thus it follows that y = x is also true. Yet when we issued the Maple statement Digits := 4;, we are issuing a command to replace whatever was stored in memory as the value of Digits with the new value 4. What we most certainly do not want, is 4 = Digits; namely, to have all values of 4 replaced by the present value of Digits, which is usually equal to 10. To make it clear that assignment equality is not the same type of equality as in algebra, the := sign is used instead of the ordinary = equal sign. On account of computer languages containing two types of equality, you have to exercise some care with your equal signs. To prove the point, if you leave off the colon : Maple does not complain, but the effect is not the same: > restart;

Digits;

10 > 1./3;

0.3333333333

> Digits = 3;

> 1./3;

10 = 3

10;

0.3333333333

10

Thus we see that when tell Maple Digits = 3;, it just tells us that we are stating that 10 = 3 without resetting the value of Digits. As there are no side effects associated with =, no harm is done, but then again, no good is done either. A more proper use of assignment statements is: > y := 3;

# Assign y value 3, then check

y;

> x := y;

x;

y := 3,

3

x := 3

3

# Assign x value y

Here we assigned y the value 3 and then assigned x the current value of y . Maple told us that x was assigned the value 3, as expected. Assignment statements are not limited to numerical values. You are able to assign symbolic values as well: > restart;

x;

# Clear all assigned values, check that x = x

36

CHAPTER 3

x > x := y;

x := y

# Assign y to x

Now that we have set x equal to y , Maple remembers that y and x are the same, until you assign another value to x. > ‘x/y‘ = x/y ;

x /y = 1

3.8 EQUALITY (RHS, LHS) Maple does have a place for the usual = equal sign. In fact, it uses it in its output, for example, when the solution to an equation is given as x = y . Notwithstanding that both sides of an equation are equal to each other, it is often helpful to deal with the left-hand side (LHS) and the right-hand side (RHS) separately: # Repeat previous assignment statement # Repeat previous assignment statement

> x := y; > ‘x/y‘ = x/y;

x := y > rhs(‘x/y‘ = x/y);

x/y = 1

lhs(‘x/y‘ = x/y);

1

x/y

3.9 FUNCTIONS Mathematicians and scientists make their work easier by employing functions. Computer scientists do much the same thing with what they also call methods and subroutines, although these latter two are able to do more than a simple function might. Consequently, while we will speak of “functions” in Maple and mathematics, when we get to programming we will speak of “methods,” which is just another name for the same thing. As visualized in Figure 3.2, a function f (x) may be thought of as a mathematical meat grinder in which you feed in some variables at one end and get out some values of the function at the other end. As a case in point, f (x) = 10x4 sin(3 x) is such a function. You feed it the variable x, the function multiplies x by 3, evaluates the sine of 3x, multiplies that by 10x4 , and then returns the result. In other words, x is the independent variable that we choose, and y = f (x) is the dependent variable. We know that this may sound familiar to you, but it is worth repeating that the numbers 3 and 10 occur in our function as parameters that are not expected to be changing much. Thus, if we replace these numerical parameters with algebraic symbols f (x) = A x4 sin(k x), then x is still the independent variable,

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

37

Figure 3.2 A function as a meat grinder; arguments x and y go in, function f (x, y) comes out.

and A and k are now the parameters, although they are represented by symbols that take on arbitrary values. Apparently, whether a symbol is a variable or a parameter is determined by its use, which we usually figure out from the context. Therefore we also could write our function as a function of three variables, f (x, k, A) = A x4 sin(k x). Functions of more than one variable are called multivariate functions and are much like our meat grinder in Figure 3.2, except that we now have a number of ingredients. When we instruct a computer program such as Maple to create a function for us, we have to decide which symbols are variables and which are parameters. As a rule of thumb, it is probably best to base your decision on convenience: if it is unlikely that the values for the parameters will change, then it is simpler and clearer to build them into the function, for example, 3 and 10 in our function. If it is likely that you will want to change the parameters, for example, to create plots, then it is probably best to treat them as variables.

3.9.1 Built-In Functions (List in Appendix B) Even though mathematical functions occur in descriptions of our world, computers do not inherently know about them. However, there are often collections of programs called libraries that supplement computer languages and contain many functions and methods. Maple has an extensive library of such functions, and we give a complete list in Appendix B, which you can also get as the Maple’s Help menu. Some functions that you may find most useful are: exp(x), ln(x), log10(x), abs(x), sin(x), sqrt(x), arccos(x), cosh(x)

Most conventional computer languages handle these common functions as would your calculator; you key in a number, you get back a number. In contrast, symbolic languages also know much about the mathematical properties of these functions so

38

CHAPTER 3

that you can also input symbolic values and have them processed. In its usual insightful manner, if you give Maple a floating-point number as input, it will evaluate the function numerically. If one argument is symbolic, then Maple will return a symbolic answer. We try this with our example: > x := 10;

x := 10 > y := 10 * xˆ4 * sin(3*x);

y := 100000 sin(30)

We see that Maple has remembered the value of x from one line to the next (it actually will remember the value of x until we do something to reassign it or until we enter restart). Regardless of the fact that we entered purely numerical values for the argument of the sine function, Maple has still not given us the answer you would get from a calculator! Do not get annoyed. The answer y = 100000 sin(30) is an exact mathematical expression with no approximations made, and so Maple keeps it that way. If we want a floating-point or decimal number as an answer, then you must force Maple to make the approximation by having it evaluate the answer as a floating-point number: > evalf(%);

−98803.16241

You could also have done this in one step: > y := evalf( 10*xˆ 4 * sin(3*x) );

y := −98803.16241

where we have used the := on the LHS to get a nice-looking answer. Alternatively, you could have used a floating-point number in the argument of the function, in which case Maple would automatically convert to floats: > y := 10 * xˆ4 * sin(3.*x)

y := −98803.16241

Now we try some variations on the preceding commands in which either x is a float, or the factor in front of the sine function is a float: > x := 10.;

x := 10. > x := 10;

# x as a float

y := 10 * xˆ4 * sin(3*x);

y := 10.

x := 10

y := −98803.16241

*xˆ4 * sin(3*x);

y := 100000.0 sin(30)

evalf(%)

# 10 as a float

− 98803.16241

As we have said before, unless you instruct otherwise, Maple performs all floatingpoint calculations with 10 significant figures.

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

39

3.10 USER-DEFINED FUNCTIONS In addition to Maple’s built-in functions, you can define your own functions and call them anything you want. To name an instance, let us say we want to define the function 10 x4 sin 3x as the function g(x): # Clear out old assignments # Define single-variable function

> restart; > g := (x) -> 10 * xˆ4 * sin(3*x);

g := x → 10 x4 sin(3 x)

Survey a few things here. The LHS of := contains the name of the function g being defined. The RHS is immediately followed by the argument x of the function and then an arrow pointing to the function definition. Accordingly, if you think of the function g(x) as “gee of ex,” then the g goes to the left of the := and the x goes to the right. Scrutinize how the arrow sign -> is entered as a minus - followed by the greater-than > sign. Maple responds by showing you this so-called arrow function or mapping in its proper mathematical notation. You then use your function g(x) just like a built-in one. Evaluate g(10) and see if you get the same answer as we did above: # Enter g(10) or y = g(10) # Try a float as the argument to g

> >

Defining functions of several variables (multivariates) requires you to add some more variables to the argument list. This is a useful way to feed a singlevariable function the parameter values it needs. By way of example, to create our old friend y = A x4 sin(b x) as a function of three variables, we replace (x) with (A,b,x): # Define 3-variable function

> y :=(A, b, x) -> A * xˆ4 *sin(b*x);

y := (A, b, x) → A x4 sin(b x)

Use y(A, b, x) to evaluate 10 x4 : # 2nd argument always gives sin(Pi/2)

> y(10, Pi/(2*x), x); 4

10x

3.11 REEXPRESSING ANSWERS √ Maple knows that 16 is 4 and will evaluate it with no further ado. However, if there is no exact root to some number, then Maple will reduce the number down as far as it is able but may leave some part of the number in root form. Numbers with explicit roots √ are called algebraic numbers. For instance, if we ask Maple to evaluate (1 + 2)2 :

40

CHAPTER 3

> (1 + sqrt(2))ˆ2;

(1 +



# Should be simple answer 2

2)

all we get is a pretty form of what we entered. So we will tell Maple that we would like the expanded form of the previous expression: > expand((1 + sqrt(2))ˆ2);

# Ah, that is better



3+2 2

There are a bunch of Maple commands, such as simplify

factor

allvalues

expand

value

eval

collect

expand

normal

that are useful in getting Maple to change the form of its answers to something you might like better. For example, how about some cute cube roots: > (-8)ˆ(1/3);

simplify(%); 1

(−8) 3 ,

1+



3I

where I is the imaginary number. As we get into more complicated calculations, we shall see that there is little question that Maple does algebra well. However, the results it gives are not always in a form that humans find revealing. Indeed, learning how to use Maple is more than just learning the names and grammar of a whole bunch of commands, it is also acquiring a trademarked trial-and-error state of mind in which you naturally try to manipulate Maple’s output until it produces the most mathematically revealing form for you. In this section we look at some manipulations and some “tricks.” Maple is real good at summing finite and infinite series: > sum(i, i = 1 ..

# Sum the integers from 1 to n

n);

(n + 1)2 n 1 − − 2 2 2

This result is not the outcome n(n + 1)/2 that we learned in our algebra, so we try to simplify it: > expand(%);

factor(sum(i, i = 1 ..

1 2 1 n + n 2 2

# Try out two

n));

1 n(n + 1) 2

Now we produce some other complicated expressions and try to simplify them: > Product(((xˆ2 + 3*i - 1) / (i+3)), i = 0 ..

10);

# Inert, but revealing

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

41

10 2  x + 3i − 1 i=0

i+3 # Active

> product(((xˆ2 + 3*i - 1)/(i+3)), i = 0..10); (

y 2 4 y 2 11 y 2 7 y 2 17 y 2 y 2 23 y 2 13 y 2 29 y2 1 y2 1 y2 − ) ( + ) ( +1) ( + ) ( + ) ( + ) ( + ) ( +2) ( + )( + ) ( + ) 3 3 4 2 5 6 3 7 7 8 4 9 9 10 11 11 12 6 13 13 # Lots of output to follow

> expand (%);

1 1 187 13 280831 14 308441 12 8594731 10 y 22 + y 20 + y 18 + y 16 + y + y + y +· · · 3113510400 20217600 56609280 103680 94348800 6739200 18869760 # Better

> factor(%);

(y − 1) (y + 1) (y 2 + 11) (y 2 + 20) (y 2 + 8) (y 2 + 29) (y 2 + 17) (y 2 + 5) (y 2 + 26) (y 2 + 14)(y 2 + 2) (y 2 + 23) 3113510400

You see here that expand and factor mean the same things as they do in algebra. The command simplify uses identities and properties of functions to simplify an expression and has a number of advanced forms (read about them in the Help pages). The normal command, on the other hand, is useful for simplifying rational expressions: # Simplify using identities

> simplify(cos(x)ˆ2 + sin(x)ˆ2);

1 > normal (product (((xˆ2 + 3*i - 1) / (i+3)), i = 0..10)); (x2 −1) (x2 +2) (x2 +5) (x2 +8) (x2 +11) (x2 +14) (x2 +17) (x2 +20)(x2 +23) (x2 +26)(x2 +29) /3113510400

3.11.1 Simplifying Examples > expand( tan(a + b) );

tan(a) + tan(b) 1 − tan(a) tan(b) > combine( % );

−tan(a) − tan(b) −1 + tan(a) tan(b)

# Fails

> combine( %, trig );

−tan(a) − tan(b) −1 + tan(a) tan(b)

# Still fails

> expand( (1-x)ˆ2 * (x+2) );

−3 x + 2 + x3

> combine( % );

−3 x + 2 + x3

# Fails

42

CHAPTER 3

> simplify( % );

> factor( % );

# Fails

−3 x + 2 + x3

# Succeeds

(x + 2) (x − 1)2 ea eb

> expand( exp(a - b) );

> combine( % );

# Succeeds

e(a−b)

# Fails

> simplify( cos(x)ˆ2 - sin(x)ˆ2 );

cos(x)2 − sin(x)2 > combine( cos(x)ˆ2 - sin(x)ˆ2 );

> factor( xˆ4 - xˆ2 );

# Succeeds

cos(2y)

x2 (x − 1) (x + 1)

> 1/(x+1) + 1/(x-1);

1 1 + x+1 x−1

> combine( % );

1 1 + x+1 x−1

# Fails

> normal( % );

2x (x − 1) (x + 1)

# Sort of succeeds

2x −1 + x2

# Really succeeds

> simplify( % );

3.11.2 Expressions Behaving Like Functions: eval, evaln It seems perfectly reasonable to think of a variable like x or y as a symbol that does not vary the way functions do. However, Maple is so smart and has such a good memory that if you set a variable equal to a complicated expression, such as polynomial, Maple will treat that variable much as it does the complicated expression and substitute in values for the variables contained in it. Consequently, it should come as no surprise that the manipulations we did on expressions are allowed on variables representing expressions: > restart;

# Variable z represents expression

z := (x+2)ˆ15;

z := (x + 2)15 > y := expand (z);

y := 32768 + x15 + 30 x14 + 420 x13 + 3640 x12 + 21840 x11 + · · · > factor (y);

43

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

(x + 2)15

Even though we are manipulating z as if it were a function, it is not a true function and does get evaluated differently. To cite an instance, if we enter z(3) as if the expression were a function, we get something interesting but not 515 : > ‘z( x = 3 )‘ = z(3);

z (x = 3 ) = (x(3) + 2)15

To evaluate z for x = 3, either set x = 3 first, or use the eval function: > ‘z( x = 3 )‘ = eval (z, x = 3);

z (x = 3 ) = 30517578125

Seeing that we have brought up the eval function, we should also indicate that there is a variant of it called evaln that evaluates a variable to a name. Its purpose is essentially the same as the use of quotes to set a variable back to symbolic form after it has been assigned a numerical value, however it is what is needed if you are dealing with subscripted variables: > x := 3;

Assign, check value

x;

x := 3, 3 > x := evaln(x);

x;

Assign, check value

x := x, x

3.11.3 Converting Expressions to Functions: unapply Even though the difference between functions and variables may seem mainly semantic, there are instances where the difference matters. The unapply command is used to convert an expression or variable into a true function. The funny name arises from the idea that we normally “apply” a function to a variable to obtain a value, while here we are working backwards and creating a function from a variable. The unapply command takes an expression and a variable as its two arguments and returns a function of the second argument: function = unapply (expression, variable) As an instance, here we convert z into the function zfun(x) (our name): # Define z as polynomial

> z := (x + 2)ˆ15;

z := (x + 2)15 > zfun := unapply(z, x);

> expand ( zfun(x) );

zfun := x → (x + 2)15

# Create function # Check if expand works on zfun

32768 + x15 + 30 x14 + 420 x13 + 3640 x12 + 21840 x11 + · · ·

44

CHAPTER 3 # Evaluate for x = -1 as check

> zfun(-1);

1

3.12 CS: OVERFLOW, UNDERFLOW, AND ROUND-OFF ERROR All computers are finite and, alas, Maple is too. This means that if you keep making numbers bigger and bigger, at some point the computer cannot handle them and an overflow exception occurs. Likewise, if you keep making numbers smaller and smaller, at some point an underflow exception occurs. In addition, since floating-point numbers are stored with a finite number of significant figures, the digit beyond the last one is truncated, and the last digit is rounded off. Similarly, if the truncated digit is greater than 5, then the last stored digit is rounded up, and this leads to round-off errors. (For convenience, both truncation and round-off errors are usually called “round-off errors.”) In principle, the algorithms used by Maple for calculations have no limit to their precision and should be exact if no floating-point numbers are called. In practice, Maple uses 524, 279 digits, which should not be too hard to live with, but you may ask for more. Serious problems result if you write a program and underflows or overflows occur within it, or if there is a great deal of round-off error. Enough talk, let us have some fun and see if we are able to break Maple using the factorial, n! = n(n − 1)(n − 2) · · · (2) (1), and powers of 10. Do not worry, you will do no permanent harm and no one will yell at you if you break Maple, although you may lose the active worksheet. In general, you get no warning that Maple is about to break, so drive defensively and Save Often! If Maple stops responding for a considerable amount of time, it may be working, or it may be stuck. Before you quit or kill Maple, you may want to try the Stop button in the tool bar and see if Maple responds again. Else you have to quit. Here we challenge Maple and show how it admits failure: # Increase exponent until integers overflow

> 10ˆ100000 ; 10ˆ1000000 ;

# Increase exponent until floats overflow

> 10.ˆ10000 ; 10.ˆ1000000 ;

# Decrease exponent until floats underflow

> 10.ˆ(-100) ; 10.ˆ(-10000) ; ‘Integer too large for display’

1

‘Integer too large for display‘

Float(∞)

0

Because calculations that employ floating-point numbers are are often truncating or rounding off the last digit in the mantissa, and because these errors tend to accumulate in time, their final results may be less precise than the numbers you input. This is called truncation and round-off error. We use the term “error” here in much the same way one does when speaking about the experimental “error” in a physics laboratory. It is not an error in the sense that you read the meter wrong or had your finger caught in the caliper but is really an uncertainty arising from the finite number of digits used to represent numbers.

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

45

To see a living example of round-off error, work through the following commands (we decrease the normal precision to make the effects more evident): > Digits := 4;

# Store only 4 decimal places

x := 1./3;

Digits := 4, > 2 * x - 2/3;

x := 0.3333 # See effect on calculation with floats

−0.0001

# Repeat with 10 digits

> Digits := 10;

3.13 SOLUTION: VIEWING ROCKET GOLF Problem a: We will now apply our Maple tools to the relativistic golf-ball problem. Michele on the moving rocket sees her golf ball travel with velocity U  at an angle θ, with a hang time T  : c U  = √ , θ = 30o , T  = 2.6 × 107 , 3 c(θ) c Ux = √ cos, Uy = √ sin(θ). 3 3 Ben on the earth observes the rocket moving with a speed v = c/2. He sees the golf ball with velocity components Ux and Uy and a hang time of T . The equations of special relativity (that we have not derived and that we do not expect you to fully understand) relate Michele’s and Ben’s observations Ux =

U x + v , 1 + v U  x /c2

T = γ T ,

Uy =

γ(v) = 

U y γ (1 + v U  x /c2 )

1 . 1 − v 2 /c2

Because the time dilation function γ(v) is rather complicated and is used in several places, we start our solution by defining a Maple function for it: > gamma := (v)-> 1/sqrt(1 - vˆ2/cˆ2); Error, attempting to assign to ‘gamma‘ which is protected

Whoops! Although this was the obvious thing to do, it does not work, because Maple apparently has its own function gamma and does not want us to steal its name. On account of this we try defining our function using a capital G: > Gamma := (v)-> 1/sqrt(1 - vˆ2/cˆ2);

Γ := v → 

1 1 − v 2 /c2

Ah, that is better. To make life simple for us beginners, let us adopt the convention that we measure all velocities as a fraction of the speed of light c. Thus, rather

46

CHAPTER 3

than saying v = c/2, we would just say v = 1/2. This will make the equations look simpler and save us some time. This being the case, we redefine Gamma as: > Gamma := (v)-> 1/sqrt(1 - vˆ2);

1 1 − v2 This function is apparently acceptable to Maple, but we need to test it before we believe it is right. We know by looking at the equation defining γ(v) that γ(v = 0) = 1, and that γ should get progressively larger as v approaches the speed of light c. So we try: Γ := v → √

> Gamma(0);

Gamma(1/2);

Gamma(1/sqrt(3));

Gamma(99/100);

√ √ √ 6 2 3 100 199 1, , , 3 2 199 These appear to be getting larger, but to be sure let us repeat the calculation using floating-point numbers. We do that by acting on the function with the evalf function or just by placing some decimal points in the arguments. We will be lazy: > Gamma(0);

1,

Gamma(1/2.);

1.154700538,

Gamma(1/sqrt(3.));

1.224744871,

Gamma(99/100.);

7.088812050

Yes, γ does grow as v approaches the speed of light. But what happens if the velocity equals the speed of light? > ‘Gamma(1)‘ = Gamma(1); Error, (in Gamma) numeric exception:

division by zero

Well this does not tell us much, because division by zero is not defined. However in a mathematical sense we really want the limit as v approaches c in infinitesimal steps, and so we will try the limit command: > ‘Gamma(1)‘ = limit(Gamma(v), v = 1);

Gamma(1 ) = undefined

Maple clearly has a problem here, and it is telling us that we did not give it a good enough description of what we want. Well, this is a case where precision in mathematical language is called for. The cause of the problem is that there is an infinity at v = 1 (a division by 0), and the limit depends on just how we approach that infinity. As told by the Help reference, we need an option to the limit command to indicate that we are approaching the singularity from the left, namely, from v < c: > ‘Gamma(v=c)‘ = limit(Gamma(v), v=1,left);

Gamma(v = c) = ∞

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

47

This is better. We see that the time dilation factor γ approaches ∞ as the rocket speed approaches the speed of light. We will discuss plotting in more detail in Chapter 4. Nevertheless, we make here a simple plot of γ(v) using Maple’s plot command. Because we have set c = 1, there are no variables with symbolic values in γ(v), and so Maple evaluates it as a number and plots it: > plot(Gamma(v), v=0..1, title=‘Gamma(v) vs v/c‘);

Gamma(v) vs v/c 25 20 15 10 5 0

0.2

0.4

0.6

v

0.8

1

We see that γ(v) is essentially 1 until the velocity gets close to the speed of light. Indeed, this is why we do not usually experience relativistic effects in our everyday lives. For the problem at hand, the exact and floating-point values of γ that we will use are: > restart: Gamma := (v)-> 1/sqrt(1 - vˆ2): > ‘Gamma(1/sqrt(3))‘ = Gamma(1/sqrt(3)); evalf(%);

√ Gamma(1 /sqrt(3 )) =

6 2

1.224744871

The difference in value from 1 indicates that there is a significant relativistic effect for our problem. Specifically, Ben says that the golf ball stayed in the air 22% longer than Michele thought it did: > T := Gamma(1/sqrt(3.))

* 2.6E7;

T := 0.3184336665 108

Next we calculate the speed U that Ben observes the golf ball to have, knowing that Michele says she hit it with velocity U  . We need then to calculate: Ux =

Ux + v , 1 + v Ux

Uy =

Uy , γ (1 + v Ux )

48

CHAPTER 3

where we have assumed all velocities will be entered as a fraction of c, and so v = c/sqrt3. To be flexible, we create functions to calculate these velocity transformations, one of which calls our previously defined Gamma function: > Ux :=(Up, v) -> (Up*cos(theta) + v) / (1 + v*Up*cos(theta));

Ux := (Up, v) →

Up cos(θ) + v 1 + v Up cos(θ)

> Uy := (Up, v) -> Up*sin(theta) / (Gamma(v)*(1 + v*Up*cos(theta)));

Uy := (Up, v) →

Up sin(θ) Γ(v) (1 + v Up cos(θ))

Ben knows Michele’s value for θ, and, after converting it to radians, uses it as input: > theta := 30 * Pi/180;

# Convert degrees to radians

π θ := 6

> convert(theta, degrees);

# Check our conversion vs Maple

theta;

30 degrees > UxBen := Ux(1/sqrt(3), 1/2);

UxBen :=

π 6

evalf(%);

4 5

> UyBen := Uy(1/sqrt(3), 1/2);

# Ben’s Ux , as decimal

0.8000000000 evalf(%);

# Ben’s Uy , as decimal

1 0.2000000000 5 If nonrelativistic kinematics are applied to this problem, we would see Michele’s golf ball moving at the combined speeds of the rocket plus its speed relative to the rocket: c c UnonRel = v + Ux = + √ cos(30o ) = c. (3.6) 2 3 UyBen :=

Yet relativity teaches us that this is not possible for anything but light. If we look at the answer we just calculated, we see that the velocity seen by Ben is less than c (1 in our units): > ‘UBen‘ = sqrt(UxBenˆ2 + UyBenˆ2);



evalf(%);

17 0.8246211252 5 Now that we have calculated the components Ux and Uy of the velocity in Ben’s frame, we calculate the angle φ at which Ben sees the golf ball fly off by using the fact that Uy Uy tanφ = , or φ = tan−1 . (3.7) Ux Ux UBen =

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

49

Inasmuch as the argument to the arctangent function could be infinite if Ux = 0, Maple’s arctangent function takes Uy and Ux as separate arguments: > ‘phi‘ = arctan(UyBen, UxBen);

evalf(%);

1 φ = arctan( ) 4

0.2449786631 # Maple keeps π factor

> convert(%, degrees);

180 degrees φ 44.09615936 degrees = π π # Force Maple to give float

> evalf(%);

57.29577950 degrees

φ = 14.03624346 degrees

Thus we see that while Michele saw her ball fly up at 30 degrees, Ben sees it “pushed forward” to about 14 degrees. Problem b: In this version of the problem, we have Michele hitting her golf ball backwards, namely, in a direction opposite to the direction of the rocket’s flight. If the ball follows the same trajectory as in Problem a, only now in the negative x direction, then it is equivalent to replacing θ by π − θ in the preceding analysis: # Convert degrees to radians

> theta := (180 - 30)*Pi/180;

5π θ := 6 > convert(theta, degrees);

theta;

150 degrees > UxBen := Ux(1/sqrt(3), 1/2);

# Check our conversion against Maple’s

5π 6

UxBen := 0

> UyBen := Uy(1/sqrt(3), 1/2);

1 3 Assessment: These are interesting results! As expected, if Michele hits her ball in the opposite direction to the rocket’s path, the rocket’s forward speed cancels out the ball’s backwards speed, and Ben sees the ball hit straight up into the air! UyBen :=

This is also an interesting result from the computational point of view, because we got exactly 0 as an answer even though there were a number of steps involved. This is a consequence of our not entering floating-point numbers anywhere in the problem and of Maple being able to do exact arithmetic. However, we could have placed some decimal points in some of the quantities and then ended up with a floating-point computation. We look at what the results would have been if we did the calculation in floating point with six places of precision: > Digits := 6;

Digits := 6

50

CHAPTER 3

> theta := evalf( (180 - 30.5)*Pi/180.

);

# Convert degrees to radians

θ := 2.60927 > UxBen := Ux(1./sqrt(3.), 1./2.);

UxBen := 0.00337563 > UyBen := Uy(1./sqrt(3.), 1./2.);

UyBen := 0.337786

We see now that with reduced precision, the x component of velocity is no longer exactly zero and that we apparently have only three decimal places of precision. If we ask “what is the relative error in the x component of velocity,” the answer would have to be 0.00337563/0 = ∞. However, we should recognize that the answer UxBen := 0.003 is consistent with 0, given the number of decimal places of precision that we have. In view of the fact that it is very hard for floating-point calculations to calculate 0 with high precision, all we may say is that the answer is small and possibly 0. Some further analysis would be needed to see if 0 is really the correct answer. Problem c: Now repeat this exercise for a golf ball hit at 60o down and backwards, namely, at θ = 240o . As a consequence of velocities perpendicular to motion not being reversed by a Lorentz transformation, there is no way that this ball will be seen as hit above the horizontal. However, it is possible for the horizontal component of velocity to be reversed by viewing the golf ball in different reference frames, and so it is possible for Ben and Michele to disagree on that. 3.14 EXTENSION: TACHYONS* One of the principles of relativity is that no particle can be made to go faster than the speed of light. A particle of light, the photon, does travel at the speed of light, yet it has zero mass. To accelerate a particle with mass to v = c would require an infinite amount of energy, since the energy of the particle E=

m0 c2 1 − v 2 /c2

(3.8)

is seen to approach infinity as v → c. The thought has consequently arisen [Fein 76] that if a particle somehow started off its life with v > c, then its energy would be finite, albeit imaginary! Such particles are called tachyons, from the Greek tachus meaning speedy. These would be very strange particles. If you increased their velocities, their energy would decrease, while if you decreased their velocity, their energy would increase, with infinite energy would require to slow them down to c.

51

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

Repeat these exercises assuming that Michele’s golf ball was a tachyon. 3.15 KEY WORDS AND CONCEPTS arguments expression irrational number round-off error 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

assignment floating-point number truncation error side-effect statement

complex function overflow mantissa

equality imaginary rational number underflow

exponent integer real

Explain the meaning of these key words. Are irrational numbers irrational only in the decimal system? Do irrational numbers occur in nature? Do floating-point numbers occur in nature? Do floating-point numbers occur in mathematics? What price is paid for Maple’s use of infinite precision? Might we be able to eliminate round-off and truncation error if we were more careful in our calculations? What is the difference between a statement of equality and an assignment statement? Explain why it makes sense to think of functions as mappings. Are integers purer numbers than irrational numbers? Are integers purer numbers than floats? Does the form of a mathematical expression change its meaning? How do you know when to tell Maple to simplify an expression? What is the difference between an expression and a function in Maple?

3.16 SUPPLEMENTARY EXERCISES 1. Make a new worksheet that includes examples of overflow, underflow, and round-off. Save it. Hint: 10ˆ(-100) raises 10 to a negative power: 10−100 . 2. To see how devious and subtle floating-point arithmetic may be, for Digits := 2

Evaluate 2(1./3) − 2./3. Evaluate the upward sum 1 − 1/2 − 1/4 − 1/8 − 1/10 − 1/12. Evaluate the downward sum −1/12 − 1/10 − 1/8 − 1/4 − 1/2 + 1. Now change the 1’s to 1.’s and see what answers you get. Explain what is happening. (If you happen to know how, do not define functions for this problem; Maple is so smart that it may fix up your errors without telling you!) 3. Do you expect the following inputs to give the same results? a. 1/3 + 1/3 + 1/3; b. 1./3. + 1./3. + 1./3.; If not, why not? a. b. c. d. e.

52

CHAPTER 3

4. Use Maple to do this complex multiplication and division: a. (10 + 99i)(10 − 99i) b. (10 + 99i)/(10 − 99i) 5. Determine a numerical (floating-point) value for a. log (11) b. sin( π8 ) 6. Type: Maple has the command type(expression, datatype) that tells you what kind of data type the expression is. There is a large number of data types that Maple recognizes (see help) including: algebraic, Array, array, boolean, complex, constant, cubic, imaginary, equation, even, numeric, rational, finite, float, fraction, function, global, imaginary, infinity, integer, laurent, linear, list, listlist, literal, local, logical, mathfunc, Matrix, negative, numeric, odd, operator, polynom, positive, prime, procedure, quadratic, quartic, radical, radnum, rational, scalar, series, set, sqrt, string, symbol, table, taylor, undefined, Vector. a. Enter the Maple statement x := y = 3; at an execution group. b. Test if x is a float, a numeric, a boolean, an equation, or a logical. c. Test for the type of data that is y.

d. Explain your results. 7. Recall some of the Maple commands we introduced that are used to simplify algebraic expressions. Use whatever commands you need to place a a+b



a a−b

+

1 a

into a simpler form. Add comments to your command lines explaining what you are doing. 8. Determine the resistance of three resistors in parallel. Circuit theory tells us that the equivalent resistance is 1 R

=

1 R1

+

1 R2

+

1 R3 .

Determine the simplest form for R. 9. Write the following numbers in scientific notation so they reflect the given number of significant digits: a. 25.3 to four significant figures b. 0.00005 to two significant figures c. 1.351 to two significant figures d. 84000 to three significant figures 10. Suppose that the floating-point number system on your computer has twodigit mantissas and exponents ranging from −2 to 1. Indicate whether the following expressions each would result in overflow, underflow, round-off error, or an exact answer: a. 20. + 20. b. 50. * 50. c. 20. + 0.01

NUMBERS, EXPRESSIONS, FUNCTIONS; ROCKET GOLF NEAR LIGHTSPEED

11.

12.

13.

14.

15.

53

d. 20. * 0.01 e. 0.01 + 0.01 f. 0.01 * 0.01 (Adapted from [Zach 96]) Indicate with an “R” or “F” which of the following values should be represented with rational numbers and which with floatingpoint numbers? a. the speed of light b. the number of protons in an atom c. the distance from the Earth to the moon d. the acceleration due to gravity on the planet Jupiter e. the number of megabytes of memory in a computer You are given the expression 10 x4 sin(k x) to use in Maple. a. How would you enter this as a function of the variable x? b. How would you enter this as a function of the variables x and k ? c. How would you enter this as an expression (an object that takes no explicit arguments)? d. How would you evaluate the expression for x = 3? √ e. How would you evaluate the expression for x = y ? f. How would you evaluate the function for x = 3? √ g. How would you evaluate the function for x = y ? What is the effect of the following Maple statements: a. ’x/y’ := ’z/r’; b. diff(xˆ3, x); c. x := 3; x := ’x’; d. evalf(1/3); e. eval(xˆ3, x = 1/3); f. x := (z) -> zˆ5 - (1/3)*cos(z); Explain in just a few words what is meant by: a. an integer b. an irrational number c. a floating-point number d. truncation error e. a statement being different from an expression f. x = y not being the same as x := y. g. a complex number h. a string Use Maple to simplify (to a form that looks simple to you) a. x+2 x √ 2

x b. (x +1) x 16. Consider a tachyon with m0 c2 = 1. Determine its energy for vc = 3, 2, and 1. 1 17. Assign the variable y to the expression π6 , and then evaluate y 3 , y 3 , and tan(y) both exactly and numerically. 18. Express y 8 − 2 y 4 + 1 as the product of factors.

54

CHAPTER 3



2

b 19. For a = 9.2, b = 1.5, and c = 100, evaluate 2ca−3 3 −20 . 20. Much of what we do throughout this book is to examine various data types and the associated methods to handle them. For each data, variable, or expression type on the left, find the best description on the right. Indicate your answer with a letter (some letters may repeat, while others may not be used at all).

string float class rational floats abstract final array symbolic subscript boolean irrational

a b c d e f g h i j k l m n o p q r s

variable with algebraic values exactly expressible as ratio of two integers ordered set of numbers variable used to store text a sequence of numbers not expressible as ratio of two integers integer appearing within square brackets data with sign, mantissa, and exponent not expressible as ratio of two floats constant not changed within program positive or negative true or false matrix or vector exactly expressible as ratio of two floats a sequence of letters one symbol, multiple parts shared among all methods float appearing within square brackets data with mantissa and exponent, but no sign

Chapter Four Visualizing Data, Abstract Data Types; Electric Fields of Multipoles

4.1 WHY VISUALIZATION? One of the most rewarding uses of computers is visualizing the results of calculations. This is done with 2-D and 3-D plots (especially with colored surfaces), with contour maps, and with animations. These types of visualization are sometimes breathtakingly beautiful and often provide deep insight into a problem by letting you see and “handle” the functions with which you are working. Visualization also assists the program debugging process, the development of physical and mathematical intuition, and the all-around enjoyment of your work. Some of the reasons for this may arise from the fact that some large fraction (≈ 50%) of our brain gets involved in visual processing, and if you are able to use this extra brainpower in your scientific work, then you have extended what was otherwise possible with solely logical abilities. Traditionally, visualization of a scientific problem was the last step in problem solving. After studying tables of numbers for hours and gaining confidence that they are right, a scientist might then go to the trouble of making a bunch of 2-D plots to examine various aspects of the data. Well, in present times computational scientists have demonstrated how much there is to be gained by going beyond 2-D plots. Now it is regular practice to use surface plots, volume rendering (dicing and slicing), and animations (movies). In this chapter we use some of these techniques within the context of visualizing the electric field around charges.

Figure 4.1 Static configurations for two, three, and four electric charges.

56

CHAPTER 4

4.2 PROBLEM: STABLE POINTS IN ELECTRIC FIELDS You are given the simple configurations of charges shown in Figure 4.1. The two charges are fixed on a line at coordinates (1,0), (-1,0); the three √ √ charges are fixed to the corners of an equilateral triangle at coordinates (0,1), 3 (1/2, -1/2), − 3 (1/2, -1/2); and the four charges are fixed to the corners of a square at coordinates (1,1), (1,-1), (-1,-1), (-1,1). The origin is at the center of each geometric figure. Your problem is to determine the electric potential at the point (x, y) and see if there might be some points in space at which we a free charge at rest will remain even if perturbed. For the equivalent gravitational problem, these stable points are known as Lagrange points and are the location of asteroids for the Earth-sun system. 4.3 THEORY: STABILITY CRITERIA AND POTENTIAL ENERGY Coulomb’s law tells us that if we have a charge q at the origin, then the electric field E (the force per unit charge) at a distance r from that charge is E(r) =

ke q ˆr. r2

(4.1)

where ˆr is a unit vector in the radial (r) direction.1 Here ke = 8.9875 109 N m2 /C 2 is Coulomb’s constant in SI units, and the electric field E is directed radially away from the charge. Because E is a vector, the electric force field about a charge is a vector field with both magnitude and direction at each point. However, no information is lost, and it is much simpler if, instead of the electric force field E, we consider the electric potential field V (r) =

ke q q ≡ . r r

(4.2)

In the second form of this equation, we have left off the electric constant ke for simplicity; since this affects just the magnitudes of the graphs and not their shapes, it will not change the conclusions we draw. We see that V (r) falls off less rapidly than E and is a scalar, namely, has no direction associated with it. Our problem requires us to determine the potentials the for two- and threecharge systems shown in Figure 4.2 and then to look for stable points in these potentials. To determine the potential for two charges, we use Pythagoras’s theorem to determine the distance to the charges, 

r1 =

(x − a)2 + y 2 ,



r2 =

(x + a)2 + y 2 ,

(4.3)

1 A vector is a mathematical object with both magnitude and direction[Ser 00]. They are discussed further in Chapter 7.

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

57

Figure 4.2 Coordinate systems for two- and three-charge configurations.

and then just add up the potentials from the individual charges: q1 q2 V2 (x, y) =  + . 2 2 (x − a) + y (x + a)2 + y 2

(4.4)

For three charges at the corners of the equilateral triangle, we know the coordinates are (0, a), (acos θ, −asin θ), (−acos θ, −asin θ), where θ = 30o . Again we use Pythagoras’s theorem and add the potentials from the individual charges to obtain q1 q2 V3 (x, y) =  2 + 2 2 x + (y − a) (x − a cos θ) + (y + a sin θ)2 q3 + . (4.5) 2 (x + a cos θ) + (y + a sin θ)2 These equations for the electric potentials are what we wish to visualize. To make them simpler to visualize, we set a = 1 and substitute for θ: q1 V1 (x, y) =  2 , (4.6) x + y2 q1 q2 V2 (x, y) =  + , (4.7) 2 2 (x − 1) + y (x + 1)2 + y 2 q1 q2  V3 (x, y) =  2 + √ x + (y − 1)2 (x − 3 )2 + (y + 1 )2 +



(x +

2

q3

3 2 2 )

. + (y +

2

(4.8)

1 2 2)

Owing to its two-dimensional nature, a purely mathematical solution for the equilibrium points in these potentials gets complicated. Instead, we will solve it graphically and rely on our intuitive understanding of how balls roll under the action of gravity. Specifically, we know that a ball released on a surface rolls downhill, and that if the ball is placed in a concave depression, it will remain there. Because the gravitational potential near the Earth’s surface is proportional to height, our description of the ball on a surface is equivalent to a description of how a particle behaves in a potential energy field. It therefore follows that charges will “roll down” the electric potential surface and will find a stable position at the

58

CHAPTER 4

concave minimum of the potential. So our problem translates into drawing pictures of the electric potential surfaces and looking for minima at the bottom of hills. 4.4 BASIC 2-D PLOTS: PLOT Before we get to Maple’s plotting commands, let us examine some general principles. First, keep in mind that the point of visualization is to make the science clearer and to better communicate your work to others. So it follows that when you produce a figure, you should look at it and think if there are some better choices of units, ranges of axes, colors, style, et cetera, that might get the message across better and provide better insight. Taking into account that we are dealing with the complexity of human perception and cognition, there may not be one definite way to do things, and some trial and error is necessary to see what looks best. Our general recommendation for visualization is to make each figure as clear, informative, and self-explanatory as possible. This means labels for various curves and data points, a title, and labels on the axes. We know, you are thinking this is really a lot of work for a lousy assignment or report, and that you do not need all those time-consuming extras to comprehend what is going on. Yet the more often you do it, the quicker and better you get at it, and the more useful will your work be to others (and yourself in the future). The convention when plotting is to have the independent variable, say x, along the abscissa (horizontal axis) and the dependent variable, say y = f (x), along the ordinate. (Remember that your mouth spreads horizontally across when you say “abscissa” and that it puckers up vertically when you say “ordinate.”) If you have trouble deciding which variable is independent, think of an experiment in which you measure the position or velocity of a ball as a function of time. Because you are free to pick the times at which you make the measurement, time is an independent variable. However, once you have chosen the time, nature picks what the position of the ball is at that particular time, so position and velocity are dependent variables.

4.4.1 Loading the plots Package Maple excels at easily producing graphs of all sorts, and indeed, visualization is one of the most valuable aspects of Maple. Although we will discuss and give examples of a number of possible plots, Maple affords more options than we discuss, and we recommend you look at the commands listed after the with(plots) statement and browse the help pages to create just the graph you want. We will first make a simple plot and then embellish it with things like labels and colors. > restart;

with(plots);

# Loads plotting tools

59

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

[animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal, conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, cylinderplot, densityplot, display, display3d, fieldplot, fieldplot3d, gradplot, gradplot3d, graphplot3d, implicitplot, implicitplot3d, inequal, interactive, listcontplot, listcontplot3d, listdensityplot, listplot, listplot3d, loglogplot, logplot, matrixplot, odeplot, pareto,

plotcompare, pointplot, pointplot3d , polarplot, polygonplot, polygonplot3d,

polyhe-

dra supported, polyhedraplot, replot , rootlocus, semilogplot, setoptions, setoptions3d, spacecurve, sparsematrixplot, sphereplot, surfdata, textplot, textplot3d, tubeplot]

We see that in response to the with(plots) command, Maple displays all of the plotting commands that are available with this package. (We sometimes use with(plots): with a colon rather than a semicolon to avoid the listing.) Now that we have the tools, let us look at the electric potential for a single charge: > V := (r) -> 1/r;

V := r →

1 r # Plot function, range # Remove infinity

> plot( V(r), r = 0 .. 0.2 ); > plot( V(r), r = 1/50 .. 1 );

50

7000 6000

40

5000 30

4000 3000

20

2000 10

1000 0

0.04

0.08

r

0.12

0.16

0.2

0.2

0.4

r

0.6

0.8

1

You will observe from the first figure that the second argument to the plot command gives the range of values for the abscissa (r in this case). Our interest is really for r between 0 and infinity, but this does not produce such a useful result, since we primarily see the repulsive peak at the origin. In view of that, we get a more revealing plot by not letting r get quite so close to the origin. The second plot eliminates the part of the graph with the infinity at r = 0, and so does not fully convey the image that the potential is infinite there. However, we tailor our plot more to our liking by giving some limits to the ordinate (also works if called the generic y ): > plot( V(r), r = 0 .. 1, V = 0 .. 10 ); > plot( min(V(r), 10), r = 0 .. 1 );

# Limit the y range # Keep ordinate less than 10, another way

60

CHAPTER 4

10

10

8

8

6

6

V 4

4 2 2 0

0.2

0.4

r

0.6

0.8

1

0

0.2

0.4

r

0.6

0.8

1

As an alternative, it is possible to tell Maple that you want to see the full dependence of the potential from r = 0 to ∞, plot( V(r), r = 0..infinity ), but then you lose some details. Try leaving off the range for the abscissa to test Maple’s capabilities: > plot( V(r) ); Plotting error, empty plot

You see that because Maple was not given a range of r values to plot, it does not think it has anything to plot (empty plot). The plot above shows the basic physics. If we view V (r) as an equivalent gravitational potential, a small positive charge (mass) placed near the fixed positive charge will be repelled (roll downhill) out to infinity. There are no locations where a charge remains at rest in equilibrium. If we had fixed a negative charge at the origin, the potential would have the opposite sign: > plot( -V(r), r = 0 ..

1, V = 0 .. 0.2

0.4

-10 ); r

0.6

0.8

1

0 –2 –4 V –6 –8 –10

This shows that, regardless of where we place it, our positive test charge will fall into the hole at the origin. As we have just seen by placing a minus sign in front of the first argument to the plot command, it is allowable to have the argument be an expression and not just a function:

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES > plot( -5/r, r = 0 ..

5,V = 0 ..

-20 );

61

# Plot explicit expression

In summary, the first argument to the plot command is the name of the function or expression to be plotted along the ordinate, namely, the dependent variable. The second argument is the range of values for the abscissa, namely, the independent variable. The double period .. is used to specify the range, so -10 .. 10 means from −10 to +10. If the upper end of the range is a decimal value, say .5, then it is clearer to enter it with a leading zero as 0.5, so that the range looks like -10 .. 0.5, and not the confusing (to the reader and to Maple) -10...5. Before we get on to embellishing the plot command, let us have some fun with the pretty graph you just produced:

• Click on the graph to select it. Inspect how a box is formed around it and that there are dark little nodes at the corners and in the middle of the sides. • Use your mouse to resize the graph by grabbing one of the nodes and dragging it with the mouse button still depressed. Monitor how when a node is selected, a little arrow appears to show you the direction in which the frame can be resized. Resizing is possible diagonally along the corners or horizontally and vertically along the edges. • Select the graph, copy it (it is placed on the clipboard), and then paste it back to the worksheet so that you now have two graphs. • Make one of your graphs wide and short and the other one tall and thin. Check how the tall one emphasizes the variation in the magnitude of V (r), while the tall one emphasizes the range of r values to which V (r) extends. Both are perfectly legitimate ways to view a function, with one emphasizing the singular nature near the origin and the other the long range. • Another way to view a function, especially one that has orders of magnitude variation in value, is with a semilog or log-log plot (although you need to avoid log(0)). In the execution group below, use the log10( ) function to see how a semilog plot changes the appearance of the same f (x) we have been viewing. • Next try the explicit semilog plot function logplot( ), following the instruction in the comments fields below:

> plot( log10(xˆ2), x = 0 .. 10 ); > logplot( xˆ2, x = 0 .. 10 );

# Repeat plot with log(V (r)) # Explicit semilog plot; log(1st argument)

62

CHAPTER 4

2

.1e3

1 2

4

x

6

8

10

0

.1e2

–1 1.

–2

2

4

x

6

8

10

–3 .1

–4

# Try log-log plot here

>

4.4.2 Labels and Titles (the Plot Thickens) Any plot worth looking at is worth explaining. This is done by placing labels along the axes and by placing a title above the curves: > plot( 1/r, r = 1/10 .. 5, labels=[‘radius r (natural units)‘,‘V(r)‘], title = ‘Potential for a positive point charge at the origin‘ );

Potential for point charge at origin 10 8 6 V(r) 4 2

1

2 3 radius r (natural units)

4

5

Take stock of how we just added a comma after the range and then added the options, separated by commas, to produce the labels and title. Because there are two axes, the labels field has an entry for the x axis and then the y axis. The labels and title are enclosed in back quotes in order to delimit the expressions: >

# Enter previous plot using ordinary accents

Modify the previous command so that the words “abscissa” and “ordinate” appear in the appropriate places and so that the actual expression being plotted appears in the title:

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

63

# Plot with your modified labels and title

>

You may have noticed that we have placed r along the “x-axis” and V (r) along the “y ” axis. In fact, there may be cases in which y is the independent variable. Thus you see why it may be better to use the words “ordinate” and “abscissa” than “x” and “y ” axes. 4.5 COMPOUND (ABSTRACT) DATA TYPES: [LISTS] AND {SETS} As we proceed with our exercises in visualization, you will see how to enter arguments to the plot commands using different types of parentheses. We recognize that some users may prefer just following the rules without questioning them. Nevertheless, the commands will make more sense, and will be easier to generalize, if you have some understanding of the method behind the madness. And so we now take a little excursion in which we define some terms that are frequently used in mathematics and computer science and employed by Maple commands. We have already seen a number of ways in which Maple displays data. At times there is just a single symbol, sometimes there is a bunch of symbols separated by commas, sometimes there is a bunch of things in parentheses, and sometimes the symbols are in quotes. To illustrate, in Chapter 5 you will see that when you solve an equation that has several solutions, Maple separates the solutions with commas: > solve( xˆ4 - 1 = 0, x );

−1, 1, I, −I > solve( xˆ4 - 1 = 0, x )[1];

−1

# Solve for only 1 root

Take note of two types of parentheses here and the different forms given for the solutions. Abstract, compound data types: An object in computer science denotes a data type with multiple parts. It may also be called an abstract data type, or a compound data type. Here the word “abstract” means that there is more to something than meets the eye; namely, the data type may contain multiple parts. Many of the individual symbols or variables used in Maple can be replaced by objects. We will discuss objects in more depth when we study Java, which is known as an object oriented language. Data types: In specifying labels for the plot command, we placed the x and y labels in square parentheses [..]. These are called “brackets.” Maple also uses standard parentheses (..) and curly parentheses {..} called “braces.” Brackets and braces are used to construct abstract data types or objects from the more elementary data types we have already seen.

64

CHAPTER 4

Sequence: A collection of variables (objects or data types) separated by commas is called a sequence. As a case in point, the arguments given to the solve command above, and indeed to most Maple commands, are variables separated by commas, and, hence, form sequences. While we are speaking of sequences, we may as well indicate that when we give arguments to a Maple command as a comma-separated list, the parenthesis indicate sequence. It is often convenient to let Maple form a sequence for you with the seq command: > seq( 2*n-1, n = 1 ..

4 );

1, 3, 5, 7

List: A list is a sequence of numbers, or abstract data types, separated by commas and placed within square brackets.The order matters for a list, while it does not for a set (to be defined soon). We use a list when we issue the plot command: > plot( 1/r, r = 1/10 .. 5, labels=[‘radius r (natural units)‘,‘V(r)‘], title = ‘Potential for point charge at origin‘ );

Potential for point charge at origin 10 8 6 V(r) 4 2

1

2 3 radius r (natural units)

4

5

Now we try creating the plot again, this time changing the order of the list: # Plot with reordered list

>

Clearly order matters here, as we would not want the labels for the abscissa and ordinates interchanged! You may enter the elements of a list by hand, as in > list1 := [2,4,6,8];

list1 := [2, 4, 6, 8]

Or use Maples seq command to help generate the elements of the list: > list2 := [seq(2*n-1, n = 1 ..

4)];

list2 := [1, 3, 5, 7]

The individual elements of a list are referenced via Maple’s square bracket notation (a standard way of indicating subscripts): > list2[1]; list1[1]; list2[2]; list1[2];

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

1,

2,

3,

65

4

Set: A set is a well-defined collection of related objects or elements with no repeated element [Fral 76]. In contrast to a list, the order of the elements in a set does not matter. Sets are also used as arguments to Maple commands, but only in cases where the order of the elements does matter, for example, a set of equations to be solved. Sets are usually described by enumerating their elements, separated by commas, as a sequence within braces. To prove the point, the sets of equations and solutions used when solving simultaneous equations: > solve({a + 3*b + 4*c = 41, x5*a + 6*b + 7*c = 20}, {a,b}); > solve({x5*a + 6*b + 7*c = 20, a + 3*b + 4*c = 41}, {b,a});

{a =

c − 62 , −2 + x5

b=−

# Any order for set # Any order for set

−7 c + 4 x5 c − 41 x5 + 20 } 3 (−2 + x5 )

Maple uses braces to denote sets: > NiceSet := {0, 2, 4, 6};

NiceSet := {0, 2, 4, 6}

Seeing that lists and sets both contain comma-separated sequences within them, we emphasize that it is legal for the same element to occur more than once in a list, with the order of the elements in the list part of its definition. As an example, if we define a set with repeated elements in arbitrary order, then Maple will remove the repeats and reorder for us: > MessySet := {6, 4, 0, 4, 2, 0};

MessySet := {0, 2, 4, 6}

In contrast, Maple does not change the order or elements of a list: > MessyList := [6, 4, 0, 4, 2, 0];

MessyList := [6, 4, 0, 4, 2, 0]

Arrays: Another data type related to vectors and matrices are arrays. We discuss them in Chapter 7.

4.5.1 Several Curves on One Plot, Sets We have seen that the first argument to the plot command is the function to be plotted. As a case in point, imagine that as part of our charge problem, we want

66

CHAPTER 4

to compare, in a single plot, the r dependence of the potential and the magnitude of the electric field due to both positive and negative charges. Seeing that Maple 1 −1 treats the argument as an object, we substitute the set { 1r , −1 r , r 2 , r 2 } as the first argument to the plot command. The fact that we use a set as the object to plot 1 −1 rather than a list [ 1r , −1 r , r 2 , r 2 ] makes sense since order does not matter and there is no point in plotting identical functions on top of each other: > plot({1/r, -1/r, 1/rˆ2, -1/rˆ2}, r = 1/10..5, y = -20..20, labels=[‘r‘,‘V, E‘]);

20

V, E

10

0

1

2

r

3

4

5

–10

–20

If you are reading the electronic version of this book, you will notice that Maple has chosen a different color for each of the functions. Experiment now with using a list as the first argument and noting how the colors assigned to the curves differ: # Use a list [...] for function argument

>

4.5.2 Using the Figure Toolbar The colors and line formats that Maple picks for graphs may look great on your screen but may not print out or project well (green and yellow are often barely visible). In the next subsection we discuss how to customize the colors to your preference. In this subsection we will explore some of the options using the figure toolbar. • Select the graph with your mouse (you should notice a box appearing about the graph after it is selected). • While selected, observe that the second line of the toolbar at the top of your screen now contains icons for graphical options. Explore what each of these icons does. This is both useful and fun. • Go to the Style pull-down menu and, under Line Width, select Broad. Observe especially the difference it makes for the yellow and green curves. Go to the Legend menu and enable Show Legend. • Again go to the Legend pull-down, and select Edit Legend. Change the legends so they are V (r), −V (r), E(r), −E(r), and r for the appropriate curves.

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

67

• Explore how the different buttons controlling the placement of the axes work.

4.5.3 Customizing Colors and Line Types Maple automatically chooses different color multifunction plots. You control the color of your graphs by adding the color option to the end of the plot command: > plot( [1/r, 1/rˆ2, r], r = 0 ..

1.5, y = 0..25, color=black );

25 20 15 y 10 5

0

0.2

0.4

0.6

0.8 r

1

1.2

1.4

Taking into account that options are objects with multiple parts permitted, you enter a list (order matters) of colors to specify the colors of each curve: > plot( [1/r, 1/rˆ2, r], r = 0..1.5, y=0..25, color = [red, blue, maroon] );

Likewise, you choose different styles for each curve (like dashed and solid) to help tell the curves apart, even in basic black. You do that with the linestyle option, which may also be a list for each curve: linestyle=[ 1 (solid), 2 (dotted), 3 (dashed), 4 (dot-dashed) ] > >

# Plot black with linestyles as list # Plot default colors with linestyles as list

An especially effective way to distinguish different curves on the same plot without the use of color, is to draw them with different thicknesses. Apply this change with the thickness = n option, where again, a list for the curves is a legal option. The possible values for thickness are: n = 0, 1, 2, and 3, where 0 is the default thickness. >

# Replot with different thickness for each curve

68

CHAPTER 4

4.5.4 Legends, Titles, and Labels Legends explain to the reader just what is being plotted with each curve. They are invaluable and do wonders for your presentation. When presenting several curves in one plot, it is important that the viewer not only be able to tell them apart by the different color or line style used for each, but also be given information as to what the different curves represent. It is good practice to explain in the caption below a graph what each curve means, as well as in the text (or in your talk) when the graph gets referenced. However, it is also good practice to have a legend in the plot itself explaining what each curve means. Captions and your explanations may get removed, but it is a lot harder to remove a legend. The legend option specifies a single string or a list of strings in the same order as the curves with a legend for each curve: > plot( [1/r, 1/rˆ2], r = 0..1.5, y = 0..25, legend = [ ‘V(r)‘, ‘E(r)‘ ] );

25

20

15 y 10

5

0

0.2

0.4

0.6

0.8

1

1.2

1.4

r V(r) E(r)

4.5.5 Other Options As we have said, there are many ways to customize your graph, and Maple’s Help pages are a good place to find out about them. Once in a while your graph may not show the features you want, because one function gets very large and Maple automatically adjusts the ordinate range to accommodate that. Here are a number of ways to limit the range of the ordinates: > plot( [sin(x), tan(x),x], x = -Pi..Pi ); # tan(x) overshadows sin(x) > plot( [sin(x), tan(x), x], x = -Pi..Pi, y = -10..10 ); # Now with y limits

69

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

10 –3

–2

–1

1

x

2

8

3

6

y

4 2

–500 –3

–2

0 –2

–1

–1000

1

x

2

3

–4 –6

–1500

–8 –10

# Limit +y values to 10 # Limit -y values to -10

> plot( [sin(x), min(10,tan(x) ), x], x = -Pi..Pi ); > plot( [sin(x), max(-10,tan(x)), x], x = -Pi..Pi );

There are occasions when a function falls off slowly, and so you might want to see its behavior for values of its argument from −∞ to +∞. It is clear that Maple is not afraid of big numbers, yet it is nice to see that it makes this type of graph in a finite amount of space: > plot( exp(-xˆ2), x = -infinity..infinity, title = ‘Gaussian‘ ); > plot( {1/r,1/rˆ2}, r = 0..infinity, title = ‘V and E of Point Charge‘ );

25

Gaussian

E, V, r

20 15 10 5

-infinity

0 x

infinity

0

0.2

0.4

0.6

0.8 r

1

1.2

1.4

Look at the second plot and its labels along the axes, rather than horizontal. This was accomplished with the commands > plot( [1/r, 1/rˆ2, r], r = 0..1.5, y = 0..25, labels = [‘r‘, ‘E, V, r‘] ); > plot( [1/r, 1/rˆ2, r], r = 0..1.5, y = 0..25, labels = [‘r‘, ‘E,V, r‘], labeldirections = [horizontal, vertical] );

# Rotate coordinate labels

4.6 3-D (SURFACE) PLOTS OF ANALYTIC FUNCTIONS We have examined the potential field V (r) = 1/r surrounding a single charge as a function of r. A 2-D plot is fine for this, since there is only one independent

70

CHAPTER 4

variable r. However, when the same potential is expressed as a function of the Cartesian coordinates x and y , V(x, y) = 

1 , x2 + y 2

we have two independent variables, x and y , and so need a 3-D visualization. We get that by creating a world in which the z dimension (mountain height) is the value of the potential, and x and y lie on a flat plane below the mountain. Because the surface we are creating is a 3-D object, it is not possible to draw it on a flat screen, and so different techniques are used to give the impression of three dimensions to our brains. We do that by rotating the object, shading it, employing parallax, and so forth. The command plot3d makes a 3-D plot and is just like our old friend plot: # Loads tools for you to use # Define function of two variables

> restart; with(plots): V := (x,y) -> 1/sqrt( xˆ2 + yˆ2 );

# Basic form

> plot3d( V(x,y), x = -4..4, y = -4..4 );

1 V := (x, y) →  2 x + y2

2.5 2 V(x,y) 1.5 1 0.5 –4

–4 –2

–2 y

0

0 2

2 4

x

4

The first plot is a fairly interesting, but it primarily shows the singular nature of the potential near the origin. As seen in the second plot, we get a more useful visualization if we limit the maximum value of V to 2.5 and add labels: > plot3d(min(2.5,V(x,y)), x=-4..4, y=-4..4, axes =BOXED, labels=[‘x‘,‘y‘,‘V(x,y)‘]);

We try to make this plot more intuitively informative by making the color red correspond to the highest values of the potential, blue smaller, and green cooler still. Color may be specified as the option color = red or with a number. Consequently, we try to be clever and use the actual value of the potential V (x, y) as the color of the graph with color = V(x,y) option (yet we minimize the maximum value of the V in order to keep the singularity from confusing the color function).

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

71

Seeing as how the potential varies continuously, this means that the color will as well: > plot3d( min(2.5, V(x,y)), x = -4..4, y = -4..4, color = min(2.5, V(x,y)) );

Even though there are many options possible as part of the plot3d command, it is both easier and more fun to first make a basic plot and then use Maple’s graphical user interface (GUI) to modify the plot:

• Select this surface with your mouse (a box with filled little squares on the perimeter should appear). • Grab the surface by depressing your left (or only) mouse button and holding it down. Now as you move your mouse, the surface will rotate in three dimensions. Make sure to move both right to left, and up and down, in order to get your brain to see the object as three-dimensional. • With the surface still selected, notice the extra buttons that appear on the control panel. You should experiment and try to make sense of them. Remember that if you hold down a button, a message with the purpose of the button appears at the bottom of the screen. In particular, note: a. the different ways to draw axes, b. the different ways to render the surface, and c. how contour plots compare to the actual surface.

4.6.1 Contours and Equipotential Surfaces To further help your mind understand that different colors mean different potential values, and that the surface is three-dimensional, we now include the style = patchcontour option. This adds contour lines that show different levels of the potential: > V := (x,y) -> 1/sqrt( xˆ2 + yˆ2 ); > plot3d( min(2.5,V(x,y)), x = -4..4, y = -4..4, axes = BOXED, labels = [‘x‘, ‘y‘, ‘V(x,y)‘], color = min(2.5,V(x,y)), style = patchcontour );

V := (x, y) → 

1 + y2

x2

72

CHAPTER 4

2.5 2 V(x,y) 1.5 1 0.5 –4

–4 –2

–2 y

0

0 2

2 4

x

4

In analogy to gravity, contours lines may be viewed as lines of equal elevation, which means that walking along a contour line does not change your elevation. For our electrical potential, the contours are called equipotential surfaces. The contourplot command also supports the option of making only a 2D contour plot of the surface (we prefer the 3-D contours to be shown soon). Because the contours are not being projected onto a curved surface or being viewed obliquely, these have the potential of being more precise: > contourplot( V(x,y), x = -4..4, y = -4..4 );

1.5 2

1

y

y 1

0.5 –1.5

–1

–0.5

0.5

–0.5 –1

x

1

1.5

–2

–1

0

1

x

2

–1 –2

–1.5

However, we see in the left plot that the equipotential surfaces appear as ellipses, and not the circles to be expected for the symmetric case of a single charge. The reason is that the viewing screen tends to be broader than higher, and so plots are spread out that way. To get a plot with the same scales along the ordinate and abscissa, as done on the right, we add the scaling = constrained option: > contourplot( V(x,y), x = -4..4, y = -4..4, scaling = constrained );

This produces a more symmetrical figure, but still not round. Apparently, the rapid rise of the potential near the origin is not being handled well by Maple, and so we exclude it by use of the min function:

73

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

> contourplot(min(2,V(x,y)), x = -4..4, y = -4..4, grid=[75,75], scaling=constrained);

To produce the circles on the right plot, we actually had to use the grid = [75,75] option to increase the default grid Maple uses to evaluate the potential, from the default 25 × 25 to a finer 75 × 75. The contours we have just drawn have all been projected onto the x − y plane. The command contourplot3d draws the same contours on a 3-D surface that can be rotated as well for better visualization (looking straight down produces 2-D contours). Actually, contourplot3d is faster and more accurate than the 2-D contourplot as a consequence of it being written in the compiled language C: > contourplot3d( min(3,V(x,y)), x = -3..3, y = -3..3 ); > contourplot3d( min(3,V(x,y)), x = -3..3, y = -3..3, scaling=constrained );

4.7 SOLUTION: DIPOLE AND QUADRUPOLE FIELDS We have used a number of visualization tools to examine the potential field surrounding a single positive and a single negative charge. The tools only showed us what we probably knew already, namely, that the potential field does not have concave areas in which a charge may remain stably at rest. This was as intended; it is a good idea to learn about new tools by applying them to problems for which you know the right answer. We are now in the position to finally investigate the electric potential due to a dipole and tripole. We start with the dipole configuration shown in Figure 4.2: V2 (x, y) = 

q1 q2 + . 2 2 (x − 1) + y (x + 1)2 + y 2

(4.9)

We start by defining a Maple function: > V2 := (x,y,q1,q2) -> q1/sqrt( (x-1)ˆ2 + yˆ2) + q2/sqrt( (x+1)ˆ2+yˆ2 );

74

CHAPTER 4

V2 := (x, y, q1 , q2 ) → 

q1 q2 + 2 2 (x − 1) + y (x + 1)2 + y 2

Now we visualize a dipole with one positive and one negative charge: > q1 := 1; q2 := -1;

q2 := −1

q1 := 1,

> plot3d( V2(x,y,1,-1), x = -3.5..3.5, y = -3.5..3.5, color = min( 3, V2(x,y,1,-1)), labels = [‘x‘,‘y‘,‘V2(x,y)‘], axes = boxed);

4 V2(x,y)

0 –4 –3

–2

–1 0 y

1

2

3

3

2

1

0

–1 x

–2

–3

If you grab and rotate this plot you will see that wherever you place a charge, it will either roll downhill away from the positive charge or fall into the hole of the negative charge. There is no stable equilibrium. For that reason enter the commands to look at two charges of the like sign: > >

# Make 3-D plot for two positive charges # Make 3-D plot for two negative charges

The figure resulting from these commands looks like a saddle. There is a region between the two peaks where it appears that a charge will remain at rest, and where it will roll back towards the midpoint if it is displaced along the positive or negative x axes. However, due to the saddle, if the charge is displaced in the y direction, then it rolls away to infinity. The charge is thus stable for x displacements but unstable for y displacements. This type of equilibrium point is known as a saddle point, and occurs for two positive or two negative charges. If the charges have unequal values but the same sign, then the shape gets distorted but still has the same property. As a check on our analysis, look at the contours for this surface: >

# Create a contourplot3d here

You should see the saddle point structure as a single point where two equipotential

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

75

surfaces cross. Now we look at the quadrupole potential (we leave the tripole for you). Our intuition tells us that the high degree of symmetry here must lead to a stable position at the center. We define the potential and then we plot it as a 3-D surface and as 3-D contours: > V4 := (x,y) -> 1/sqrt( (x-1)ˆ2 + (y-1)ˆ2 ) + 1/sqrt( (x-1)ˆ2 + (y+1)ˆ2) +1/sqrt((x+1)ˆ2 + (y-1)ˆ2 ) + 1/sqrt( (x+1)ˆ2 + (y+1)ˆ2 ); > plot3d(V4(x,y), x = -3.5..3.5, y = -3.5..3.5, color = min(4.5,V4(x,y)), labels = [‘x‘, ‘y‘, ‘V4(x,y)‘], axes = box ); > contourplot3d(min(4.5,V4(x,y)), x = -3..3, y = -3..3, scaling=constrained);

1 1 + 2 2 (x − + (y − 1) (x − 1) + (y + 1)2 1 1 + + 2 2 2 (x + 1) + (y − 1) (x + 1) + (y + 1)2

V4 := (x, y) → 

1)2

6 V4(x,y) 4 2 –3

–2

–1 0 y

1

2

3

3

2

1

0

–1 x

–2

–3

Indeed, we do see a large central, flat region surrounded by a lip to hold the charge in. We check this out further by looking at some slices through the central region: > plot( V4(x,0), x = -3..3, title = ‘V4(x,y=0) vs x‘); > plot( min(4,V4(x,x)), x = -3..3, title = ‘V4(x=y) vs x‘ );

# min for details

76

CHAPTER 4

V4(x,y=0) vs x

V4(x=y) vs x 4

2.8 3.5

2.6 2.4

3

2.2

2.5

2

2

1.8 1.5

1.6 1.4 –3

–2

–1

1 0

1

x

2

3

–3

–2

–1

0

1

x

2

3

So we see that the central portion of this potential is indeed a flat region with a “lip” all around it. Consequently, a charge placed there will be have no force on it (the flatness) and will be stable for small displacements (the lip). 4.8 EXPLORATION: THE TRIPOLE Repeat the analysis carried out for the dipole and quadrupole now for the tripole. Be sure to make a 3-D plot with labels and title as well as including contours. Slice your plot through its center to verify that you have found a stable point. 4.9 EXTENSION: YET MORE PLOT TYPES* 4.9.1 2-D Animations We have just seen that surface-rendering techniques permit us to create images from mathematical functions that give the impression of viewing a true threedimensional object. This literally gives a new dimension to our visualizations. In addition, if we have plots that show the behavior of some quantity as a function of space, and if this behavior changes gradually with time, then the observation of a sequence of plots of the spatial dependencies, each one for a slightly different time, gives the impression of a continuous evolution of the spatial function in time. The function appears to be alive and, indeed, creating a series of snapshots in time is known as animation. To produce animations, we add the dimension of time to our 2-D plots. To name an instance, let us say we wanted to show the changing temperature distributions along the x direction of a metal bar as it cools with increasing time t. We could plot T1 (x) and then T2 (x) and so forth, where the subscript indicates the time. It is more elegant and concise to envision a single function T (x, t) that contains both the space and time dependencies. By way of example, if the bar was initially hot in the center, a possible

77

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

temperature distribution is [Krey 88] T (x, t) = sin x e−t −

sin 3x e−9 t . 9

(4.10)

We make a 3-D surface plot of this function from t = 0 to 20 and x = 0 to π : > plot3d( sin(x) * exp(-0.3*t) - sin(3*x) * exp(-9*0.3*t)/9, x = 0..Pi, t = 0..20, axes = BOXED, labels = [‘x‘, ‘t‘,‘T(x,t)‘] );

1 0.8

1 0.8 T(x,t) 0.6 0.4 0.2 0 0

T(x,t) 0.6 0.4 5 10 t

15 20

3

2.5

2

1.5 x

1

0.5

0 0.2 0

0.5

1

1.5 x

2

2.5

3

Observe that it is important to label the axes so that you know which variable is time and which is space. Go ahead and grab, enlarge, and rotate the left plot. The plot on the right uses animate to animate this function: > with(plots): with(plottools): # This loads the needed tools > animate(sin(x) * exp(-0.3*t) - sin(3*x) * exp(-9*0.3*t)/9, x = 0..Pi, t = 0..20, labels = [‘x‘,‘T(x,t)‘] );

Whereas the plot on the right does not look different from a static 2-D plot, if you are reading the electronic version of this book, then you make it come alive by selecting it with your mouse. Then a bunch of buttons appear on the control bar on top that permit you to “play” the animation as you might a CD. Do it now! Investigate the buttons for stop, forward, reverse, fast forward, and fast reverse. However there is also a button that lets the animation loop around and thus play continuously. We recommend that. An animation works by displaying (flipping through) a sequence of slightly modified images. In movie parlance, these images are called frames, and the more you have of them the smoother and slower will be your animation: >

# Animate with frames = 100 option

78

CHAPTER 4

4.9.2 3-D Animation Well, if you have been reading and executing up to this point, it is fairly clear what 3-D animations are about. If you have a function of two space coordinates that also varies in time, then you make a 3-D surface plot to visualize the space dependence at any one time, or an animation to visualize the time dependence of the surface. For example, assume the temperature distribution is now a function of two space coordinates as well as time: T (x, y, t) = sin x e−.3t −

sin 3x e−9.9t sin 3 y e−9.9 t sin y e−.3t − . 9 9

This is complicated enough that we will define a Maple function rather than try to stuff it into a command. Other than using the name animate3d, the format of the command is the same as before. We start with the function T (x, y, t), then give the ranges for each variable, and then the labels: > with(plots):

# Load needed tools

1 1 T := (x, y, t) → (sin(x) e−0.3 t − sin(3 x) e−9.9 t ) (sin(y) e−0.3 t − sin(3 y) e−9.9 t ) 9 9 > animate3d( T(x,y,t), x = 0..Pi, y = 0..Pi, t = 0..20, labels=[‘x‘,‘y‘,‘T(x,y)‘] );

Again, if you are reading the electronic version, then select this plot, play it, and even rotate it while it is vibrating (it looks like a vibrating drum head).

4.9.3 Phase-Space (Parametric) Plots In science we often encounter several physical quantities that are simultaneous functions of the same variable. For instance, the position x(t), velocity v(t), and acceleration a(t) of a mass undergoing simple harmonic motion are all trigono-

79

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

metric functions of time: x(t) = sinωt, v(t) = −ω cosωt, a(t) = −ω 2 sinωt.

(4.11)

We easily plot the position and velocity on the same graph; for example, > plot( [sin(wt), -2*cos(wt)], wt = 0..

8*Pi, legend = [‘x(wt)‘,‘v(wt)‘] );

2 1 0

5

10

wt

15

20

25

–1 –2 x(wt) v(wt)

We observe the position and velocity as out of phase, but with the same period. A more direct way to observe the relation of two dependent variables (x and v in our example) as a function of the same independent variable t is known as a phase-space or parametric plot. These types of plots have now proven themselves to be highly illuminating and valuable. Phase space is an extension of the usual space of position that also includes velocity as if it were a new dimension, along with position. Explicitly, we plot x(t) along the abscissa as if it were the independent variable and V (t) along the ordinate. In a sense then, a phase-space plot is a plot of v(x). Recognizing that it might be impossible, or very complicated, to analytically eliminate the time dependencies permitting two functions to be expressed in terms of each other, it is still fairly easy to do this graphically. Explicitly, Maple breaks up the total time interval into a number of steps and then records the pair of values (x, v) for each time step. These values then get plotted as v(t) versus x(t): > plot( [sin(wt), -2*cos(wt), wt = 0..8*Pi], labels = [‘Position‘, ‘Velocity‘] );

80

CHAPTER 4

2

–1

Velocity

1

–0.5

0

0.5 Position

1

–1

–2

Observe that all we have done here is use a list (square brackets) as the function argument and included the range of time values as part of the list. The general syntax for a two-dimensional (2-D) parametric plot is plot( [x(t), y(t), t = a..b], options), where t is known as the parametric variable or simply parameter, and x(t) and y(t) denote the horizontal and vertical functions, respectively. As far as the output goes, this phase-space plot looks like an ellipse. Yet it has properties that agree with the observations we have made before: when the mass is at its maximum positions, at the extreme right and left edges of the ellipse, the velocity is zero. When the mass has it maximum speed, at the top and bottom of the ellipse, it has zero position, namely, it is passing through its equilibrium position. So while a harmonic oscillator is described via a complicated set of position, velocity, and acceleration functions versus time, in phase space its motion is an elliptical orbit on which the mass passes over and over. You may be wondering in looking at this graph just how we hit upon the exact range of wt values for which the graph exactly closes on itself. Well, we really did not. In fact, our plot covers two full cycles and plots them on top of each other (which you cannot see). If, on the other hand, your phase-space plot did not form a closed figure, then you would need to run for more values of the time. Try out smaller and smaller ranges for the phase wt in the plot command until you are able to generate 1/2 and 1/4 of an elliptical orbit: >

# Create some phase-space plots here

As you see from looking at the monitor in front of you, visual displays tend to be broader than they are high. For this reason graphs tend to get stretched horizontally (“scaled”) in order to fill the screen. As long as the graph looks good this is not normally a concern, yet it is if you are trying to determine the proper shape of a geometrical figure. For this reason Maple has the scaling = constrained option to avoid undue stretching: >

# Add scaling = constrained option

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

81

4.9.4 Energy Conservation and Implicit Plots In our discussion of parametric plots, we looked at the position x(t) and velocity v(t) of an oscillator as functions of time. Maple solves numerically for the function x(v) or v(x). There may also be cases where you know some functional relation between two variables, say x and v , and wish to make a plot of x versus v . To cite an instance, let us say that we have a spring with a nonlinear force law so that the potential energy stored in it is V(x) = k x6 .

(4.12)

The kinetic energy of a mass attached to this spring is, as always, 1 K = m v2 . (4.13) 2 We know that the sum of kinetic plus potential energy is conserved, which means we have an implicit relation between position and velocity, namely, m v2 . (4.14) 2 This last equation permits us to make a parametric plot even though we do not have explicit solutions for x(t) and v(t). We do it with the implicitplot command that plots x versus v given the equation relating the two: E = V + K = k x6 +

> implicitplot( 5*xˆ6 + (13/2)*vˆ2 = 1, x = -1..1, v = -1..1 );

0.4

v

0.2

–0.6 –0.4 –0.2 0

0.2

0.4 x

0.6

–0.2

–0.4

See how this phase-space plot changes if the potential energy varies as x5 .

4.9.4.1 Implicitplot3d In analogy to the 2-D implicit plots we just made for a nonlinear oscillator, the implicitplot3d command plots the 3-D surface defined by some explicit relation between x, y , and z :

82

CHAPTER 4

> implicitplot3d( exp(zˆ2) = sqrt(xˆ2 + yˆ2), x = -3..3, y = -3..3, z = -1..1 );

As with other 3-D plots, it is possible to grab and rotate the electronic plot.

4.9.5 Vector Fields: fieldplot and fieldplot3d* Consider again the electric dipole shown in Figure 4.2. The problem we examined dealt with the electric potential for this type of system. Even though potentials are easier to compute and visualize than fields, it is usually fields that are related to the forces and measured in experiments. As an extension of our charge problem, we now visualize the electric field vector E(x, y) for the dipole, E=

(r − r1 ) (r − r2 ) , 2 + |r − r1 | |r − r2 |2

(4.15)

where the E and r’s in this equation are all vector quantities. Mathematically, we determine the electric field as the derivative of the potential using the techniques of vector calculus and then plot the individual components. This is complicated, so let us have Maple do the work. We start with the x and y components of the electric field E : > Ex := (x, y) -> (x+1) / ((x+1)ˆ2 + yˆ2) - (x-1) / ((x-1)ˆ2 + yˆ2); > Ey := (x, y) -> y / ((x+1)ˆ2 + yˆ2) - y / ((x-1)ˆ2 + yˆ2);

x+1 x−1 − 2 2 (x + 1) + y (x − 1)2 + y 2 y y Ey := (x, y) → − 2 2 (x + 1) + y (x − 1)2 + y 2

Ex := (x, y) →

We visualize the components in two surface plots (we show only one on paper): > plot3d(Ex(x,y), x = -4..4, y = -4..4, labels = [‘x‘,‘y‘,‘E‘], axes=boxed);

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

83

8 4 Ex(x,y)

0 –4 –3

–2

–1 0 y

1

2

3

3

2

1

0

–1 x

–2

–3

Although this plot shows Ex at each point in space, it is rather hard to get a good feel for the magnitude and direction of the field. For this purpose there is the fieldplot command: > fieldplot( [Ex(x,y), Ey(x,y)], x = -4..4, y = -4..4, color = Ex, arrows=THICK );

3 2

y

1 –3

–2

–1

1 –1

x

2

3

–2 –3

This plot shows the direction of the field as given by direction of arrows, and the magnitude as given by the length of the arrows. You may not know it from elementary physics classes, but the real world is actually three dimensional. Because of this the electric field actually has a z component as well: > Ez := (x,y,z) -> z / ((x+1)ˆ2 + yˆ2 + zˆ2) - z / ((x-1)ˆ2 + yˆ2 + zˆ2);

Ez := (x, y, z) →

(x +

1)2

z z − 2 2 2 +y +z (x − 1) + y 2 + z 2

Here we visualize a 3-D vector field with the fieldplot3d command:

84

CHAPTER 4

> fieldplot3d( [Ex(x,y,z), Ey(x,y,z), Ez(x,y,z)], x = -4..4, y = -4..4, z = -4..4, arrows = THICK );

Regardless of how good you might think this plot looks on paper, it truly comes alive when you grab and rotate it electronically.

4.9.6 Polar Plots Polar coordinates describe a 2-D plane in terms of radius r and orientation θ, rather than the more common Cartesian coordinates x and y . A polar plot is a representation of the function r(θ) created by placing a point a distance r(θ) from the origin for each angle θ. They are illuminating because the angle on the plot corresponds to the actual angle of the function’s argument, and so the shape of the plot lets you visualize the variation of the function in actual space. If r were independent of θ, then the polar plot would be a circle. Other dependencies are less obvious. Making polar plots with the plot command is similar to making parametric plots, only now we add a coord = polar option and use theta instead of time as the parametric variable. Or do it directly with the polarplot command: > plot( [r(theta), theta, theta = 0..2*Pi], coords = polar ); > polarplot( [r(theta), theta(theta), theta = 0..2*Pi] );

To get a feel for how this works, let us make a polar plot of a function that is independent of θ and has r = 1: > plot( [1, theta, theta = 0..2*Pi], coords = polar );

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

85

> polarplot( [ r, theta, theta = 0..2*Pi ] );

1

0.5

–1

–0.5

0.5

1

–0.5

–1

As a more realistic example, consider the expression for the intensity of lowenergy X-rays scattered off a reflecting sphere as a function of the scattering angle: σ(θ) = 3 + 2 cos(θ)4 + 2 cos(θ).

(4.16)

> plot( [3+2*cos(theta)ˆ4 + 2*cos(theta), theta, theta = 0..2*Pi], coords = polar );

3 2 1 –2

0

2

4

6

–1 –2 –3

You intuitively feel where the scattering is large and where it is small.

4.9.7 Surface Plots of Complex Functions* In Chapter 14 we give a review of complex numbers in preparation for representing them as objects in Java. If you are not familiar with complex numbers, then you may want read that review in order to better understand this section. Maple has the commands complexplot and complexplot3d for visualizing complex functions. We have found complexplot3d the more useful of the two,

86

CHAPTER 4

and will discuss only it. We start with the complex function f of the complex argument z = x + iy , f (z) = f (x + i y) = Ref + i Imf.

(4.17)

The command complexplot3d makes a 3-D visualization of a complex function or expression. The form of the visualization differs if the input is a implicit function of a complex argument z or an explicit function of the real and imaginary parts x and y . In the latter case complexplot3d plots the Ref while coloring the graphic using the Imf . As an instance, consider the complex function z 2 = (x + i y)2 = x2 − y 2 + 2 i x y.

(4.18)

> complexplot3d( [xˆ2 - yˆ2, 2*x*y], x = -2..2, y = -2..2, axes = framed );

4 2 0 –2 –4 –2

–2 –1

–1 y

0

0 1

1 2

x

2

Here the height of the plot is the value Ref = x2 − y 2 , changing sign along the line x = y , while the color is controlled by Imf = 2 x y . We obtain the same plot as before using explicit functions of x and y : > Ref := (x, y) -> xˆ2 - yˆ2;

Ref := (x, y) → x2 − y 2 > Imf := (x,y) -> 2 * x * y;

Imf := (x, y) → 2 y x > complexplot3d( [Ref, Imf], -2..2, -2..2, axes = framed, labels = [x, y, Ref] );

The other approach to visualizing complex functions is to enter complex numbers directly. In this case, complexplot3d plots the magnitude of the function and colors the resulting surface with the phase θ, defined as tan−1 (Ref /Imf ), of the function: > complexplot3d( zˆ2, z = -2 - 2*I ..

2 + 2*I, axes = framed );

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

87

8 6 4 2 0 –2

–2 –1

–1 y

0

0 1

1 2

x

2

A more realistic example is the complex function 1 E−2+i

f (E) =

(4.19)

that describes a resonance as a function of E that may occur in a circuit containing a resistor, inductor, and capacitor in series (discussed in Chapter 16) or in setting up standing waves in a tube. Regardless of the fact that experiments are done only at pure real values of E , this function has a pole (equals ∞) at a complex energy E = 2 − i. To help understand the visualization, we rewrite f in a form with a real denominator (multiply the numerator and denominator by the complex conjugate of the denominator): f (E = x + iy) =

x − 2 − i (y + 1) . (x − 2)2 + (y + 1)2

(4.20)

We visualize f (z) using the different forms of complexplot3d: > complexplot3d( 1 / (z-2+I), z = 1-3*I..3+2*I, axes = framed );

12 10 8 6 4 2 1

–3 –2 –1 y

1.5 2

0

2.5

1 2

x

3

Inasmuch as there are no sign changes here, it must be the modulus of the function that is being plotted. If we look at (4.20) we see that the modulus does have a maximum when x = 2 and when y = −1, and that the modulus falls off in value

88

CHAPTER 4

as we get away from the pole position. As expected, we see those features on the plot. We visualize the real and imaginary parts of f (E) separately: > Ref := (x,y) -> (x-2) / ((x-2)ˆ2 + (1+y)ˆ2); > Imf := (x,y) -> -(y +1) / ((x-2)ˆ2 + (1+y)ˆ2); > complexplot3d( [Ref, Imf], 1..3, -3..2, axes = framed, labels = [ReE,ImE,Ref] );

x−2 (x − 2)2 + (1 + y)2 1+y Imf := (x, y) → − (x − 2)2 + (1 + y)2 Ref := (x, y) →

6 4 2 Ref 0 –2 –4 –6 –3

5 Imf

0 –5 –10

1 –2 –1 ImE

1.5 2

0

2.5

1 2

1

–3 –2

ReE

3

–1 ImE

1.5 2

0

2.5

1 2

ReE

3

As we see in the plot, there is a sign change at 2 along one of the axes. Consequently, that must be the x axis and the function plotted must be Ref (Imf does not have a sign change). Check next that Ref does not change sign as a function of y , but does have a maximum at y = −1 where the denominator gets small. In fact at (x, y) = (2, −1) there is a pole, yet it has residue 0, which we see as a very large oscillation in Ref . To see how the Imf varies as a function of complex energy, we reverse the real and imaginary parts in the argument call: > complexplot3d( [Imf, Ref], 1..3, -3..2, axes = framed, labels = [ReE,ImE,Imf] );

4.9.8 Plotting Lists and Sets: pointplot* If you do measurements in a real lab or run numerical simulations in a virtual lab, you will end up with some numerical data to plot. The data are of the form [ (x1 , y1 ), (x2 , y2 ), (x3 , y3 ), · · · (xN , yN ) ].

We will now generate such points, both so that we will have some data to plot, and as a further exercise with sets (not ordered), lists (ordered), and sequences. First we generate a sequence of ordered pairs (each a sublist in square brackets) with the seq command:

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

89

> sequence = seq( [i, iˆ2], i = 0..10 );

sequence = ( [0, 0], [1, 1], [2, 4], [3, 9], · · · [10, 100] )

If we store this sequence in a list, then the order is preserved, yet if we store it as a set, then the order is not: # Generate list, order preserved # Generate set, order not preserved

> List := [ seq([i, iˆ2], i = 0..10) ]; > Set := { seq([i, iˆ2], i = 0..10) };

List := [ [0, 0], [1, 1], [2, 4], · · · [10, 100] ] Set := { [0, 0], [1, 1], [2, 4], · · · [10, 100] }

The difference becomes evident when we plot the set and list using the pointplot command and the connect = true option to connects the points. If the points are sequentially ordered, then the list should yield a single-valued function, otherwise the curve will loop back upon itself: # Continuous with list # Loops back occurs with list

> pointplot( List, connect = true ); > pointplot( Set, connect = true );

4.9.9 Creating Simple Figures: pointplot, pointplot3d* Here we give some examples of the use of pointplot and pointplot3d to create simple figures of a barbell (we use the figures in Chapter 7). There are three steps involved. First we plot just the data points using circles for the points. Then we plot lines connecting the data points. Finally we use Maple’s display command to display both the points and lines on the same graph. In 2-D, the left plot below, we give points as a list of doublets [x,y]: > p1 := pointplot( {[-0.7, 0.7],[0.7, -0.7]}, thickness = 2, style = LINE ); > p2 := pointplot({[-0.7, 0.7],[sqrt(2)/2, -0.7]}, symbol = CIRCLE, thickness = 2, symbolsize = 45,axes = normal, labels=[x,y]); > display(p1,p2);

0.6 0.4

y

0.2 –0.6

–0.4

–0.2

0

–0.2 –0.4 –0.6

0.4

–1 –0.5 0.2

0.4 x

0.6

y 0.6

–0.6 z –0.4 0 –0.2 –0.4x 1

90

CHAPTER 4

In 3-D, the right plot above, we give the points as a list of triplets [x,y,z]: > p1 := pointplot3d({[-0.7,0,0.7],[0.7, 0, -0.7]}, thickness = 2, style = LINE); > p2 := pointplot3d({[-0.7,0,0.7],[0.7, 0, -0.7]}, symbol = CIRCLE, thickness = 2, symbolsize = 100, axes = normal, labels=[x,y,z]); > display(p1, p2);

4.9.10 Plotting Vectors: arrow* Maple’s LinearAlgebra package is discussed in Chapter 7. That package permits us to define vectors, matrices, and arrays of arbitrary sizes and dimensions (although matrices are always 2-D). There you will find some easy-to-use tools for visualizing vectors and matrices. The arrow command plots a 3-D vector as an arrow and lets you grab it and rotate it. If the vector is 2-D, then the arrow cannot be rotated. The display command lets you place several arrows together. Notwithstanding vectors having more than three components, the arrow command works only for 2-D and 3-D vectors. > with(plots): with(LinearAlgebra): > Omega := Vector( [1,-3,6] ); L := Vector( [6,0,6] ); # Plot Omega as black arrow

> arrow( Omega, color = black, shape = double arrow); > arrow(L);





1 ⎣ −3 ⎦ , Ω := 6



# Not shown



6 ⎣ 0 ⎦ L := 6

Omega (black) and L

6 5 4 z3 2 1 0 –3 –2 y –1

0

6

5

4

3x

2

1

0

Once we have visualizations of the vectors L and Omega, we place them on the same graph (the right plot above, which can be grabbed electronically) by assigning objects (named variables) to each arrow, and then displaying the arrows: > w := arrow(Omega, color = black, shape = double arrow);

# Assign object w

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES > L1 := arrow(L):

91

# Assign object L1

> display(w, L1, axes = BOXED, scaling = constrained, labels = [x,y,z], title = ‘Omega (black) and L‘);

# Display Omega and L on same graph

> Omega := Vector([1,-3]);

We also use the arrow command to plot several arrows (or sequences of arrows) at one time. To illustrate, here are the familiar three unit vectors: > arrow({[0,0,1], [0,1,0], [1,0,0]});

Study our creation of arrows with the options to control the color and shape of the arrows (see Help arrow for other properties). Although there are limited options for the arrow command, the display command supports the usual ones, such as labels and titles.

4.10 VISUALIZING NUMERICAL DATA 4.10.1 2-D Plots of Data Most realistic computations in science produce numerical output, not analytic functions. Possibly in view of the fact that there is more work involved in plotting numerical data than there is for an analytic function, Maple has a number of ways to make 2-D plots of numerical data.2 In fact, the various packages that are used to extend the basic Maple capabilities, such as those for statistics or linear algebra, often have their own plotting techniques. Here we demonstrate the use of listplot from the plot package and scatterplot from the statistical package.

2 David

McIntyre assisted with methods for plotting data.

92

CHAPTER 4

4.10.2 Numerical Plots: listplot The listplot command creates a 2-D plot from a list of numerical data values. If only y values are given, say as the four-element list, Ydata := [1, 8, 27, 100],

then listplot will assign x values by counting from 1 to 4 to produce {(1, 1), (2, 8), (3, 27), (4, 400)} > with(plots):

# Plot y with x as order number

listplot( [1, 8, 27, 100] );

100 80 60 40 20

0 1

1.5

2

2.5

3

3.5

4

The same plot is obtained if y is entered as a list: # Enter y data into list Ydata # Plot data object Ydata

> Ydata := [1, 8, 27, 100]; > listplot( Ydata );

Ydata := [1, 8, 27, 100]

If you want to give explicit x values to your data, then you place each (xi , yi ) value in its own two-element list, and make a big list up of these sublists. This placement and listing produces the left plot below: [ [x1 , y1 ], [x2 , y2 ], [x3 , y3 ], [x4 , y4 ] ] # Plot list

> listplot( [[0, 1], [sqrt(3)/2, 1/2], [-sqrt(3)/2, 1/2] ] );

1

1

0.9

0.9

0.8

0.8

0.7

0.7

0.6

0.6

–0.8 –0.6 –0.4 –0.2

0

0.2

0.4

0.6

0.8

–0.8 –0.6 –0.4 –0.2

0

0.2

0.4

0.6

0.8

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

93

To get the plot to close on itself and thereby form a geometric figure (right plot), we repeat the first point: > listplot( [[0,1], [sqrt(3)/2,1/2], [-sqrt(3)/2,1/2], [0,1]] ); # Repeat 1st pt

The above samples are basic commands. There are options for plotting only points, for controlling their size and color, and with connect = true, to connect the points in the true order in which they are plotted.

4.10.3 Numerical Plots: scatterplot* A scatter plot is a plot of points in which the points are not connected. To use the command, we give as arguments a list of the x values of our data, and a corresponding list of y values:

scatterplot

# Load statistics package # [List] of all x values

> with(plots): with(stats[statplots]); > Xdata := [ 0, sqrt(3.)/2, -sqrt(3.)/2 ];

# [List] of corresponding y values

> Ydata := [ 1, -1/2, -1/2 ];

Xdata := [ 0, 0.8660254040, −0.8660254040 ] −1 −1 , ] Ydata := [ 1, 2 2 > scatterplot( Xdata, Ydata ); > scatterplot( Xdata, Ydata, color = red, symbolsize = 35 );

# Plot points, basic # Plot + options

1 0.8 0.6 0.4 0.2 –1

–0.5

0 –0.2

0.5

1

–0.4 –0.6

4.10.4 Histograms and Box Plots* Here we make a histogram and then a box plot of some random data: > data := [-2., -.8, 2., .0, -.5, -.5, 1.6,.8,.5,-.5,-.2, -.2, .2,-.1]; > histogram( data, area = count); > boxplot( data );

94

CHAPTER 4

data := [−2., −0.8, 2., 0., −0.5, −0.5, 1.6, 0.8, 0.5, −0.5, −0.2, −0.2, 0.2, −0.1]

2

8

1

6 –0.4

4

–0.2

0

0.2

0.4

–1 2 –2 –2

–1

1

2

A histogram is used to create bins or bars, with the height of the bar proportional to the number of events that occur within the x values covered by the bar. A box plot is seen to contain a central line showing the median, a lower line showing the first quartile, and an upper line showing the third quartile.

4.10.5 Surface Plots of Data: listplot3d* We have already seen how to make 3-D or surface plots z = f (x, y) of analytic functions of two variables. Here we describe how to make the same kind of plot from a table of numbers. In Chapter 11 we describe how to use the free, plotting program gnuplot to make surface plots of numerical data (we find gnuplot easier). Most often, realistic calculations produce numerical data rather than analytic functions as their output. This type of output is a consequence of the real world being less simple than the assumptions made in those models that lead to purely analytic answers. In realistic cases, the resulting equations may still be solved, only they must be solved numerically. Trying to understand if there is meaning in long lists of numbers obtained as output is quite the challenge, yet this point is exactly where visualization tools are most valuable. While yet to be discussed (we do in Chapter 7), the use of matrices and arrays makes the bookkeeping for multidimensional data rather straightforward. Hence, you may want to review matrices and then return to this subsection. As with all 3-D visualizations, we want to create a surface in 3-D space that represents our data. Picture data describing the temperature T (x) as a function of distance x along a one-dimensional bar. However, the bar is cooling as a function of time, so there is a temperature distribution for each time that we put together as a function of both time and position T (t, x). Consequently, we will make a graph of

95

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

T (t, x) with T as the vertical distance above the plane formed by the time t as one horizontal coordinate and with position x as the other horizontal coordinate. The fact that this 3-D surface does not truly exist in nature is one reason this approach is called “visualization.”

The plotting of numerical data is done in two steps. First we read the data into a matrix, with one index for position and other for time. Then we plot the matrix. We have placed some sample T (t, x) data in the file EqHeat z.dat on the CD, and you should read that file into a convenient location now. It seems obvious that the data file should contain x and t values with the associated T (t, x) value. Nonetheless, the problem is made simpler by just giving T values, indicated by the subscript z in the file EqHeat z.dat. The assumption is that these T values correspond to a rectangular array of uniformly spaced x and t values, and we do not need to know the actual spacing to make the visualization. To see how this works, here are the first three lines in EqHeat z.dat: 0. 0. 0.

100. 32.5 22.7

100. 59.8 43.

100. 78.9 59.0

100. 89.5 69.1

100. 92.8 72.5

100. 89.5 69.1

100. 78.9 59.

100. 59.8 43.

100. 32.5 22.7

0. 0. 0.

Think of each line of data as a row of a big matrix. Whereas the number of digits used for each number may differ, and so the length of each line may differ, each row has the same number of entries or columns. These data are values of the temperature for increasing x values along the rod. Explicit values of the time are not given, however, subsequent lines (rows of the matrix) contain the temperature distributions for later and later times. In other words, the full matrix is T (x, t) = T (column, row) = z(column, row)

where each new row corresponds to the next value of the time. As no explicit values are given for the time or the position, the plotting program will assume uniform spacing and time steps and will assign each integer values, 1, 2, 3 . . . for the plot. Think of these as “the first, second, et cetera” times and the “first, second, et cetera” positions. The data are read into Maple with the readdata command. We view each row of the matrix as a list (order matters) [value 1, value 2, . . . , value numcols]

and the collection of rows as a list of lists (listlist): [[row1data], [row2data], . . . [row numrows data]]

Once we know how many rows or columns there are, it is easy to build the matrix with all the elements in their correct rows and columns. We will show you how to make an explicit matrix up out of these data soon, but first we will work with just

96

CHAPTER 4

the z values as a list of lists. We use the variable numcols to tell Maple the number of elements (columns) in each row, and then have Maple count the number of rows: > restart; with(plots): > numcols := 11;

with(LinearAlgebra):

# Initialize # Number columns

The data (values of z ) are read into the variable (object or abstract data type) that we name data with Maple’s readdata command. Because files are stored on your individual computer using the local operating system’s conventions for files, just how you specify the file name depends somewhat on the computer system you are on and where the file is on that system. For all but the simple Unix version, we have placed a # sign in front of the command so that Maple will treat the command as a comment. Examine the use of left quote or accent grave, ‘ as opposed to the normal, ’ in the command to delineate the name of the data file. To see what works for you, try deleting the comment symbol and seeing if the command completes with no error message: > # data := readdata(‘EqHeat z.dat‘, numcols); # Unix, file in working directory # Apple MacIntosh > # data := readdata("Mac G3 HD:DMc:EqHeat z.dat",numcols): > data := readdata("C:\\Documents and Settings\\rubin\\

# Windows extra \

My Documents\\Rubin\\Books\\Intro\\Maple\\EqHeat z.dat",numcols);

data := [ [0., 100., 100., 100., 100., 100., 100., 100., 100., 100., 0.], [0., 32.5, 59.8, 78.9, 89.5, 92.8, 89.5, 78.9, 59.8, 32.5, 0.], . . . > data := readdata("C:/Documents and Settings/rubin/ Documents/Rubin/Books/Intro/Maple/EqHeat z.dat", numcols);

# Also for Windows

data := [[0., 100., 100., 100., 100., 100., 100., 100., 100., 100., 0.], . . .

As you see here from the Maple output, the variable data is a data object composed of a list [. . . ] containing other lists [[. . . ], [. . . ], . . . ]. Each sublist is the temperature all along the bar at a different time. To look at any individual part of this list, for example, the second row (the second list): > data[2];

[0., 32.5, 59.8, 78.9, 89.5, 92.8, 89.5, 78.9, 59.8, 32.5, 0.]

In case you have trouble reading these files, you may input the data by hand with the list of lists format, data := [[...], [...], ...].

4.10.6 listplot3d The listplot3d command creates a 3-D plot of a list of lists of numeric values. Remaining arguments are interpreted as plot options. See the plot3d help menu for

97

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

a list of possible options. > listplot3d(data); > listplot3d(data, style=patch, orientation=[-55,30], shading=z);

# Basic # Options

4.11 PLOTTING A MATRIX: MATRIXPLOT* Another way to make a 3-D plot is with the matrixplot command. Though this is an easy command to use, we first need to place the data in a matrix. In Chapter 7 we discuss matrices and give examples of plotting matrices and vectors as well. If you have trouble following the procedure below, you may want to learn some more about matrices in Chapter 7. We start by reading in the data file. In order to figure out how many rows there are in the matrix without our doing the counting, we use the nops (number of operands) command: > restart; with(plots): with(linalg): > data := readdata( ‘EqHeat z.dat‘, numcols );

numcols :=11; # Unix, file in working directory

> data := readdata( "Mac G3 HD:DMc:EqHeat z.dat",numcols ):

# MacIntosh

> data := readdata("C:\\My Documents\\Rubin\\Books\\EqHeat z.dat", numcols): # Windows, note extra \ > data := readdata("C:/Documents and Settings/rubin/ My Documents/Rubin/Books/Intro/Maple/EqHeat z.dat", numcols);

# Also Windows

data := [ [0., 100., 100., 100., 100., 100., 100., 100., . . . , 0. ], . . . > numrows := nops(data);

numrows := 20

We now convert the list named data to the matrix named data matrix with the convert command: > data matrix := convert(data,matrix);

98

CHAPTER 4





0. 100. 100. 100. 100. 100. 100. 100. 100. 100. 0. ⎢ 0. 32.5 59.8 78.9 89.5 92.8 89.5 78.9 59.8 32.5 0. ⎥ data matrix := ⎣ ⎦ .. .

As a check, you should determine the individual element in row 5, column 2. Here we do it for the fifth elements in the second row: # Element in row 2, column 5

> data matrix[2, 5];

89.5

4.11.1 Surface Plots with matrixplot* An entire 2-D matrix is visualized in one step with the matrixplot command. This provides a plot that may be grabbed and rotated, which is useful for finding missing elements. The options are much the same as those for plot3D: > with(LinearAlgebra): with(plots): > Slant := Matrix (); # Enter 3 × 4 matrix > ID := IdentityMatrix(6);



1 5

9

# The identity matrix of specified dimension







1 0 0 0 0 0 ⎣ 0 1 0 0 0 0 ⎦ ID := ···

⎢ 2 6 10 ⎥ ⎥ Slant := ⎢ ⎣ 3 7 11 ⎦ ,

4 8 12

> matrixplot( ID, axes = boxed, title = ‘Identity Matrix‘, style = point, # Plot points only

symbol=CIRCLE, thickness=2, symbolsize = 20 ); > matrixplot( ID, axes = boxed, title = ‘Identity Matrix‘ );

# Plot surfaces

> matrixplot( Slant, axes = boxed, title = ‘Slant Matrix‘ );

# A new slant

Identity Matrix

Identity Matrix

1

1

0.6

0.6

0.2

0.2 1

1 3 row

3 column 6

6

1

1 3 row

3 column 6

6

When working with data, the hard part is clearly getting the matrix into Maple. Plotting with the matrixplot command is easy. For our data, different rows correspond to different values for the time t, and different columns correspond to

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

99

different positions x along the bar. The rows will be plotted along the x and y axis in the plot, namely, along the base of the solid figure. The temperature will be plotted as the height of the surface above the base. To help with the visualization, we will also use the color of the surface to convey the temperature (ideally with red being hot and blue cold). We vary the color by using the shading option, with its value set equal to hue; this varies the color through the entire spectrum as a function of the z value: > matrixplot( data matrix, shading = zhue ); # Basic > matrixplot( data matrix, axes = boxed, labels = [Time, Position, Temp], title = ‘Cooling‘,orientation = [-20,70], style = patchnogrid, shading = zhue );

Cooling of Hot Bar 100 80 60 Temp 40 20 0 5 Time 10 15 20

2

4

8 6 Position

10

You should see is a colored 3-D surface with many curves drawn on the surface. This is interesting but not necessarily an effective visualization. You are probably the best judge of effectiveness, and to do that you need to try out the options and see what works. So here is another command-line version of matrixplot with many options for things like tick marks at informative places, contours, and better fonts and sizes for the labels: > matrixplot( data matrix, axes = boxed, labels = [Time, Position, Temp], style = patchcontour, labelfont = [HELVETICA,14], axesfont = [HELVETICA,9], tickmarks = [4,4,4], title = ‘Cooling‘, titlefont = [HELVETICA,18], orientation = [-20,70], shading = zhue );

At a Maple prompt, try including and removing some of these options in order to see the effects. Go back to the simplest version of the matrix plot command matrixplot(data) and obtain similar options from within Maple’s graphical user interface. In particular, notice how the 3-D effects depend on having made a good choice for the orientation angle.

100

CHAPTER 4

4.11.2 Non-Uniform x-y Grid with surfdata The matrixplot command assumes the grid spacing is uniform, namely, that the x and y values are uniformly spaced. If they are not, then you need to provide information about how the grid spacing depends on grid location (column and row number). An alternative approach is to read in the set of values (x, y, z) so that you effectively have the function z(x, y). The surfdata command is used to plot a surface from a set of (x, y, z) data. To make a surface plot of data we need data. To start we make a list of uniform (x, y) values using the sequence command seq. As before, we make each set of (x, y) values into a list and then place all of these sublists into a list of lists. For our temperature example, this placement means we will represent time with the row index, position with the column index, and temperature with the z coordinate. Rather than read in a big file of (x, y, z) values, we will add explicit (x, y) values to our list of data values. We start with the seq command that creates a sequence of integers. First we get a sequence of x values from 0 to 100 in steps of 50, and then a sequence of [x, t] with x running from 1 to 1,000 in steps of 100: data list

> numrows;

numcols;

20,

11

> seq( [(j-1) * 10], j = 1..numcols);

# Sequence of x values

[0], [10], [20], [30], [40], [50], [60], [70], [80], [90], [100] > seq( [(i-1) * 50], i = 1..numrows);

# Sequence of t values

[0], [50], [100], [150], [200], [250], . . . [950]

Now we insert the values of the data matrix to form a list of triplets [x, y, z]: > data list := [seq([ seq( [(i-1) * 50, (j-1)*10, data matrix[i,j]],j = 1..numcols)], i = 1..numrows)];

data list

:= [[[0, 0, 0.], [0, 10, 100.], [0, 20, 100.], [0, 30, 100.], [0, 40, 100.], . . .

Each point to plot is a set of (x, y, z) values that we are representing in Maple as a sublist. For example, for row 1 and column 2 there is the list > data list[4,2];

30

We now plot the graph with the surfdata command, first in simple form, and then with labels and contours: > surfdata(data list); # Basic > surfdata(data list, labels = [Time, Position, Temperature], axes=boxed,

101

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

style = patchcontour, orientation = [-20,50], shading = zhue); # Options

100 80 60

Temperature

40 20 0 0 200 400 Time 600 800 0

20

60 40Position

80

100

Reading in (x, y, z) Data Sets In a more realistic case our data might be the result of a numerical simulation or a measurement and would reside in an external file. For this purpose we supply the file EqHeat xyz.dat on the CD. We read in these data as before with the readdata command, but now with three columns for the values of (x, y, z): > data list ex := readdata(‘EqHeat xyz.dat‘, 3);

# Unix read present directory, or:

> data list ex := readdata(‘/home/mcintyre/cpug/heat/EqHeat xyz.dat‘,3); > data list ex := readdata("Mac G3 HD:DMc:OSU work:CPUG:EqHeat xyz.dat", 3): > data list ex:

= readdata("C:/Documents and Settings/rubin/MyDocuments/Rubin/Books/ # Works also on Windows

Intro/Maple/EqHeat xyz.dat", 3);

data list ex := [[1., 0., 0.], · · · # Convert data list to matrix

> data matrix := convert(data,matrix);



data matrix :=

0. 100. 100. 100. 100. 100. 100. 100. 0. .. .

> type(data, listlist);

type(data, list);

true, > type(data matrix,list);

# Is data a list of lists, or a list?

true false

# Is data matrix a list?

where we have tested if data matrix is an array, and if data is an array. We then convert from a list of lists to a matrix with the convert command: > convert(data, matrix);



# Convert variable data to matrix

0. 100. 100. 100. 100. 100. 100. 100. 100. 100. 0. .. .

102

CHAPTER 4 # Will work only if data is a matrix

> matrixplot( data);

# Convert variable data to list of list # Works only if data is a list; same plot

> convert(data, listlist); > listplot3d(data);

[[0., 100., 100., 100., 100., 100., 100., 100., 100., 100., 0.], . . .

4.12 ANIMATIONS OF DATA* Below we present an animated plot of the wave motion resulting from plucking a string. The string is hanging under its own weight and is affected by friction.3 This animation comes from a numerical simulation [CP 05] that outputs its results to a file in the gnuplot format used for surface z(x, y) plots. As described in §4.10.5, the data are in the form of a matrix of z values, with the rows of the matrix separated by blank lines. The first row corresponds to time 1, with the place in the row corresponding to different x values. The second row contains all the z values for time 2, and so forth. As we see in the figures, the surface plot shows many ripples corresponding to oscillations of the string, but it is not nearly as effective a visualization as playing the animation below.

5.14

5.12

5.1

5.08

5.06

20

40

60

80

100

In this section we indicate the steps needed to input numerical data in surface 3 We

thank Juan Vanegas for this example.

103

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

plot format and convert it into an animation. We start by forming a very long list of data named data from the file function.dat (Unix reads from the present directory, Windows requires the full path name): # Unix

> data := readdata( ’function.dat’, 1, float ): > data := readdata( "C:/Documents and Settings/rubin/

# Windows, note \

My Documents/Rubin/Books/function.dat", 1, float): > data[1];

# Examine individual elements in a list

data[4];

5.126560431,

5.126343673 # Covert list data to array DATA

> DATA := array(data): > DATA[1]; DATA[4];

5.126560431,

5.126343673

We next break up the array DATA into a list of sublists. The first sublist funct[1] corresponds to row 1 of the original matrix, the second sublist, funct[2] corresponds to row 2 of the original matrix, and so forth: > for t from 0 by 1 to 199 do func[t] := [seq(DATA[i],i=101*t+1..101*(t+1))]: # For loop repeats for multiple t values > end do:

As a check, we print out the sublist func[1]. It is the first row of input and will be the first frame of the movie: # Print out individual row

> func[1];

[5.126560431, 5.126467879, 5.126395193, 5.126342347, 5.126296259, . . . 5.126560431]

Now that we know that the data look good, we plot several of the frames we will put together to form the movie: > listplot( func[0] ); listplot( func[20] );

5.144 5.125

5.142 5.14

5.12

5.138

5.115

5.136 5.11

5.134 5.132

5.105

5.13

5.1

5.128 5.126

5.095 0

20

40

60

80

100

0

20

40

60

80

100

To create the movie, we use the display command. Yet, if we try to display several frames together, we just get multiple frames on top of each other. In order

104

CHAPTER 4

not to display all of the frames on top of each other, we use the seq command to form a time-ordered sequence of plots. Hence, we have created an animation. > display(seq(listplot( func[t]), t = 1..199), insequence = true);# Animation

4.13 KEY WORDS AND CONCEPTS abstract contour plot list sequence polar plot 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

14.

data type dependent variable matrix surface plot potential energy

abscissa implicit plot nonlinear functions 2-D plot

animation independent variable ordinate set parametric plot

Is there a direct way of measuring potential energy in nature? Is there a reason electric charges occur always in integer values? How does an abstract data type differ from an algebraic symbol? How do you decide if a variable is independent or dependent? When might it be a bad idea to use color in your plotting? List three ways in which you may change the interpretation of data by changing the way in which it is presented. What might be a dishonest way of presenting your data? Give examples of the type of data that may be appropriate for 1-D, 2-D, 3-D, and 4-D visualizations. When are animations a useful way to display data? How, in a mathematical sense, does a phase-space (parametric) plot differ from an ordinary 2-D plot? What is the difference between a list and a set? How is a table of numerical data similar to, and different from, a mathematical function? Give an example or two of the types of functions that would be visualized best with each of the following plots: a. 2-D plot b. 3-D plot c. multifunction plot d. parametric plot e. animation f. 3-D animation Explain in just a few words what is meant by a. an abstract data type b. a parametric or phase-space plot c. a function of three variables d. a set of three variables e. a list of three variables

VISUALIZING DATA, ABSTRACT DATA TYPES; ELECTRIC FIELDS OF MULTIPOLES

105

4.14 SUPPLEMENTARY EXERCISES 1. On a single graph, display the functions x3 sin(x), x3 cos(x), and x log(x), each in a different color. Use an equal negative and positive x range, and pick that range to obtain the most interesting comparison of the three functions. 2. A graphical approach to solving equations plots the right- and left-hand sides of an equation as two separate functions, and then finds the solution as the value of the abscissa at which the two curves intersect. Use Maple’s ability to plot several functions in the same graph to determine the approximate solutions of the following equations: a. sin(x) = x2 , b. x2 + 6 x + 1 = 0, c. H 3 − 9 H 2 + 4 = 0. 3. Do a graphical experiment in which you prove to yourself these very useful mathematical facts: a. The exponent ex grows faster than any power xn . b. The logarithm ln(x) grows slower than any power xn . Hint: To avoid overflow problems with very large x values, you may want to make semilog plots. 4. Do a graphical experiment to find the value of n for which these equations are true: a. n sin(2 x) = sin(x) cos(x), b. n cos(x)2 = 1 + cos(2 x). 5. If two tones very close in frequency are played together, your ear hears them as a single tone with an oscillating amplitude. Make plots as a function of time of the results of adding the two sine functions sin(100 t) + sin(b t).

Make a series of plots for b in the range 90 < b < 100.

Make sure to plot for a long enough range of t values to see at least three cycles of any periodic behavior. 6. Here are nine measurements given in the form (x, y): (0, 10.6), (25, 16.0), (50, 45.0), (75, 83.5), (100, 52.8), (125, 19.9), (150, 10.8), (175, 8.25), (200, 4.7) Make a plot of these data points. 7. The orbits of planets and comets are known to follow conic sections. Conic sections are the 2-D curves formed when a cone is cut (sectioned) by a plane and are given by the parametric equations in which s is the parameter: a. Hyperbola: (x(s), y(s)) = (4 cosh(s), 1.4 sinh(s)) b. Ellipse: (x(s), y(s)) = (4 cos(s), 1.4 sin(s)) c. Parabola: (x(s), y(s)) = (s cos(θ) − s2 sin(θ), s2 cos(θ) + s sin(θ)), θ = arbitrary parameter

106

CHAPTER 4

Make parametric plots of these conic sections. Cover as much range as is needed for the parameter s in order to obtain the full shapes. 8. Planetary orbits are best described by the polar-coordinate form of the conic section: α r

= 1 + ε cos(θ)

where ε is the eccentricity and 2 α is the lataus rectum of the orbit. An ellipse occurs when 0 < ε < 1, a hyperbola for ε > 1, and a parabola for ε = 1. Make polar plots of these three kinds of orbits by using different values of ε. Try also various values for the parameter α (it is inversely proportional to the total energy of the planet). 9. Make a surface plot of the potential: V(x, y) = 

x e(−r) cos( ) r r

where r = x2 + y 2 . 10. Make a contour plot of this same potential. 11. A standing wave on a string is described by the equation y(x, t) = sin(10 x) cos(12 t)

where x is the distance along the string, y is the height of the disturbance, and t is the time. a. Create an animation of this function. b. Create a surface plot of this function, and see if you agree with us that it is not as revealing as the animation. 12. A traveling wave on a string is described by the equation y(x, t) = sin(10 x − 12 t)

where x is the distance along the string, y is the height of the disturbance, and t is the time. a. Create an animation of this function. b. Create a surface plot of this function, and see if you agree with us that it is not as revealing as the animation. 13. Plot the function f(x) = sec(x) + 4 over the interval [0, 4 π]. Seeing that Maple’s automatic scaling does not work well here, you will need to specify a range for the ordinates to obtain a useful visualization. 14. Given the points (1, 0.53), (1.5, 0.65), (2, 0.91), (2.5, 0.95), and (3, 1.10), create a plot containing these points as well as the functions a(x) = sin( x2 ), 2 b(x) = x5 . Which function fits the data best?

Chapter Five Solving Equations, Differentiation; Building Towers

5.1 PROBLEM: MAXIMUM HEIGHT OF A TOWER The Washington monument in the District of Columbia is 555 feet tall and is constructed from 36,000 white granite stones. The weighty marble and granite walls at the base are 15 feet thick, tapering to 18 inches thick at the top of the shaft. [USN&WR] In our quest to rise above our fellow beings, we wish to build a really tall tower [Zach 96]. We are not that good with big plans, but we are really good at building one-level structures, and so we plan to stack our one-level structures on top of one another until we reach the desired height. Your problem is to incorporate into your design the fact that all materials compress when made to bear weight, and thereby to see how this limits the maximum height of the tower. 5.2 MODEL: BLOCK STACKING We model each level of the tower as a block of thickness t. By working with two blocks in our garage, we have determined that when one block is placed atop another, there is a very small decrease a in the thickness t of the lower block: t → t − a.

(5.1)

After some hard mental processing, we deduce that if we have three blocks stacked atop each other, as in Figure 5.1, then the top block would have thickness t, the middle block thickness t−a, and the bottom block t−2a. The double compression

Figure 5.1 A stack of blocks. Observe how the bottom blocks get thinner.

108

CHAPTER 5

of the bottom block arises because it must support the weight of the two blocks atop it.

5.2.1 Model Problem Now that we have some idea of the model and tools we are able to apply to our problem, we reformulate it to one we are able to solve. On the other hand, while this is an important part of problem solving, it does means that when we are done solving our problem, we must verify that we have a correct mathematical solution and then validate that our mathematical solution solves the physical problem: 1. Compute the height of a five-block tower by explicitly entering and summing the thickness of each of the blocks. Observe how the sum is accumulated and use that to deduce an algebraic expression for the height. 2. Suppose you had 10, 000 blocks. You do not want to type in all those terms. Prove that the height H of a tower composed of n levels is:

3. 4. 5. 6. 7.

1 H = nt − n(n − 1)a. (5.2) 2 Hint: First prove that the sum of the first n integers is n(n + 1)/2. Check that your expression gives the same answer for the five-story tower as you worked out by hand. Express H as a function H(n, t, a) of three variables. Use your function to determine, for arbitrary t and a, the number of blocks needed to erect a tower of height 200. How many blocks are needed to make a tower of height 200 feet for t = 15 feet and a = 0.001t? One way of defining maximum height is to imagine adding another level to the tower but not have its height increase (later on we will use a different definition of the maximum height). This is equivalent to finding a solution to the equation H(Nmax ) = H(Nmax + 1) (5.3)

Find the algebraic value of Nmax that satisfies (5.3). 8. Show that the solution to (5.3) corresponds to slipping the additional block onto the bottom of the stack and having it shrink to zero thickness. 9. If t = 15 feet and a = 0.001t, what is the maximum height of the tower? 10. If we think of Nmax as a continuous variable and take a mathematical point of view, then the maximum height occurs at the point where an infinitesimal change in n leads to no change at all in the tower’s height. Calculus tells us that this occurs when the derivative vanishes, that is, when dH(n, t, a) = 0. dn

(5.4)

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

109

Determine this derivative. 11. Determine the value of n that solves (5.4). 12. Explain in your own words why this is not the same as the previous definition we used for maximum. 13. Plot up the results as a check. State in your own words how your graph shows the relation between the two definitions of maximum. 5.3 MATH: EQUATIONS AS CHALLENGES Consider the equation: a x20 + b x = −c.

(5.5)

You may think of this equation as a statement of mathematical fact: that the righthand and left-hand sides of the equation have the same value. You may also view this equation as a challenge: for given values of the parameters a, b, and c, find some values of x that make both sides equal. Although equations usually have variables on both sides of the equal sign, nevertheless, it is also legitimate to write them in generic form: 20

ax

f (x) = 0, + bx + c = 0,

(5.6) (5.7)

where the second version is for our problem. If f (x) is a symbolic expression, then Maple will automatically search for a symbolic solution of f (x) = 0. If f (x) is numeric (contains floats or decimal points), then Maple will conduct a numerical search. We are not using the words “search” and “challenge” just to make mathematics seem exciting. For an arbitrary f (x), there is no guarantee that a solution exists or that Maple will find it even if one does exist. In many cases, the function f (x) has a form for which no analytic solution exists. To prove the point, even the simple equation x sin x = 1

(5.8)

does not have analytic solutions. However, there may be numeric ones, which leads to the somewhat philosophical question “If it is possible to find a numerical solution, does that mean we should also be able to find an analytic solution if we were smarter?” In some cases the answer is “no,” in other cases “yes,” and in others “maybe.” In any case, it is usually faster for Maple to find a numerical solution than an analytic one. Therefore, if your ultimate interest is in a numerical solution, for example to plot a graph, then it makes good sense to have Maple solve the equation numerically in the first place. If you truly want an analytic solution, you may have to rearrange or simplify the equation so that Maple has a better chance with it, or even try some other program, like Mathematica, that employs different algorithms.

110

CHAPTER 5

5.4 SOLVING A SINGLE EQUATION: SOLVE, FSOLVE Maple solves equations with the solve command. Let us try it for the general quadratic equation: # Find an x that solves the 1st argument # Alternate way of writing equation

> solve (a * xˆ2 + b*x + c = 0, x); > solve (a * xˆ2 + b*x = -c, x);

−b +



b2 − 4 a c , 2a

−b −

√ b2 − 4 a c 2a

We see that there are two roots, and if you look among what might appear to be a garble of symbols, you will see that Maple separates them by a comma (we added some space to make it clearer). Take note that the first argument to solve is the equation to be solved, and the second argument is the variable whose value we want as the solution. We suspect that you think it obvious that since a x2 +b x+c = 0 is a quadratic equation in x, that Maple should know that you want the x values that solve it. Yet from an algebraic point of view, the parameters a, b, and c are also symbols for variables, and you may be interested in the solution of this equation for one of them. So try it and observe the differences: # Solve the equation for a, b, and then c

>

As mentioned before, the conventional way to present an equation to be solved is f (x) = 0. Yet once one knows the convention, it is no longer necessary to include the = 0 part of the equation. For this reason the same solve command may be used with just f (x): > solve (a * xˆ2 + b*x + c, x);

# Find an x that is a root of 1st argument

Sometimes the roots of an expression may be a real number, sometimes a complex number containing the symbol I , sometimes an algebraic expression, and sometimes they cannot be found. As long as the solution is exact, the solve command is applicable: >

# Find roots of x2 − 1, x2 + 1, x2 + b

Yet sometimes even a simple function does not have exact roots: # Find the zeros of x sin(x) − 1

> solve(x * sin(x) -1);

RootOf( Z sin( Z ) − 1)

Confusion seems appropriate after looking at Maple’s response of RootOf(). This is not an answer, but rather an admission of defeat from the internal algorithm used by solve. In this case, the transcendental equation x sin x − 1 = 0 does not have an analytic solution, and so it is no surprise that Maple fails. However, because Maple does not know that the analytic solution truly does not exist, but only that it was unable to find it, Maple blames itself and gives you an indication of where it failed. Knowledge of how and where Maple fails may be useful for advanced

111

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

work, but for beginners it is easier to rearrange the equation and try again. (Soon we will see that the command fsolve solves this equation numerically.) Various formulas give closed-form expressions for the roots of quadratic, cubic, and quartic polynomials. And of course Maple knows them. Maple has tricks for higher-order polynomials, and it will try to use them as much as possible. However, there are some higher-order polynomials whose roots Maple cannot find. We give here a case that works, and invite you to change some powers or add some terms until Maple fails (at least for some of the roots): # Maple succeeds here

> solve (xˆ6 + 2 * xˆ2 + 3, x);



I, −I, −

2 + 2I 2





11

,

2 + 2I 2





11

,−

2 − 2I 2

 √ √ 11 2 − 2 I 11 , 2

> solve(xˆ8 + 2 * xˆ3 + 5, x) ;

RootOf(%1, index = 1), . . . # Modify parameters until Maple succeeds

>

5.4.1 Verifying Solutions: eval It is often convenient to use a variable to represent the equation to be solved and thereby avoid rewriting it many times and having very long commands. Suppose the equation to solve contains the product of factors (which makes it easy for us and Maple to know what the solution should be): (x − a)2 (x + b)3 (x − c) = 0.

(5.9)

We use a symbol like eqn to represent the entire equation and then work with it: > eqn := (x-a)ˆ2 * (x+b)ˆ3 *(x-c) = 0; 2

# Enter equation

eqn := (x − a) (x + b) (x − c) = 0 3

# The full form of the equation

> expand(eqn);

x6 − x5 c + 3 x5 b − 3 x4 b c + 3 x4 b2 − 3 x3 b2 c + x3 b3 − . . . # Solve for roots (obvious from factored form)

> solve (eqn, x);

c, a, a, −b, −b, −b

Once we have a solution, we verify it by substituting it back into the equation with the eval command: # Evaluate equation with substituted solution

> eval(eqn, x = a);

0=0

112

CHAPTER 5

5.4.2 Symbolic Solution: roots The solve command is useful for solving equations. Yet sometimes you may have more specialized needs. As an example, you may want to find all the zeros or roots of a polynomial, as well as their multiplicity. The command roots is a powerful tool for finding the exact roots of a polynomial (but not the zeros of a transcendental equation). However, it is specialized to mathematical purposes, and we have often found its use less robust and less obvious than solve. However, it nicely tells us the multiplicity of the root: > ‘roots (xˆ2 - 1)‘ = roots (xˆ2 - 1); 2

roots(x − 1 ) = [ [1, 1], [−1, 1] ]

Here the notation [-1,1] in the output means that −1 is a root with multiplicity 1. However, roots is rather limited in that it only works if it is possible to write the roots as algebraic expressions with real integers: > ‘roots (xˆ2 + 1)‘ = roots (xˆ2 + 1); 2

# Fails as they are imaginary

roots(x + 1 ) = [ ]

In this last case the roots are obviously imaginary, so roots just quits. However, adding I as a second argument to roots extends the capability of the algorithm to complex numbers: > ‘roots( xˆ2 + 1, I )‘ = roots ( xˆ2 + 1, I ); # Find root, complex number OK > ’roots( xˆ4 - 4,sqrt(2) )’ = roots( xˆ4-4,sqrt(2) ); # Try and explain

roots(x2 + 1, I) = [ [−I, 1], [I, 1] ] √ √ √ roots(x4 − 4, 2) = [ [− 2, 1], [ 2, 1] ]

5.4.3 Numerical Solution: fsolve If solve fails to find a solution, or if all you want is a numerical solution, then fsolve, which finds solutions numerically, is the tool to use. As a case in point, we have already seen that solve cannot solve x sin x = 1, yet Figure 5.2 should convince you that there are an infinite number of x values at which x sin x = 1. The problem is that these values are not exact numbers. This calls for the command fsolve, which works just like solve, except it uses floating-point numbers and finds numerical solutions: > fsolve (x * sin(x) - 1., x);

−1.114157141

Of course, the above solution is only one solution out of an infinite number, but

113

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

50

x sin(x)

–100 –80 –60 –40 –20

20

40

x

60

80 100

–50

–100

Figure 5.2 A plot showing that x sin x passes through zero endlessly.

that is because fsolve finds only one solution at a time. To find more solutions (one at a time), restrict the range of x by modifying the second argument to provide a range: # Restrict x to range 0 − 2 # Restrict x to range 2 − 4

> fsolve (x * sin(x) - 1, x = 0..2); > fsolve (x * sin(x) - 1, x = 2..4);

1.114157141,

2.772604708

5.5 SOLVING SIMULTANEOUS EQUATIONS (SETS) 5.5.1 Algebraic Equations If you have a few simultaneous equations to solve, then solve works just fine. If you have a large number, say five or more, then it is probably more convenient and elegant to set your problem in matrix form and use the techniques of linear algebra. We start with two simultaneous equations in two unknowns x and y : x + 3y + 4 = 41, 5x + 6y + 7 = 20.

(5.10) (5.11)

First we assign obvious variable names to the equations: > eqn1 := x + 3*y + 4 = 41; > eqn2 := 5*x + 6*y + 7 = 20;

eqn1 := x + 3 y + 4 = 41,

eqn2 := 5 x + 6 y + 7 = 20

Then we group this set of equations together within braces as {eqn1, eqn2} to form the first argument to solve. Recall, a set is an unordered sequence of distinct expressions enclosed in braces. As the second argument, we enter the set of variables we want to solve for:

114

CHAPTER 5

> solve({eqn1, eqn2}, {x, y});

−61 172 , x= } 9 3 Observe how Maple uses braces for its answer set. Verify that the same solution is obtained regardless of the order of the equations {eqn1, eqn2} in their set, or of the variables {x,y} in their set: {y =

# Enter interchanged equations and variables

>

This example was fairly obvious, in part, because we used a standard notation for the unknowns and had numerical values for the parameters. Yet consider now the two simultaneous equations: a + 3 b + 4 c = 41, 5 a + 6 b + 7 c = 20.

(5.12) (5.13)

There are clearly two equations and three unknowns a, b, and c. The rules of mathematics being as they are, this means it is possible to solve for any two of the unknowns in terms of the other one, but one unknown will always remain. As before, we first define variable names as labels for the equations: > eqn1 := a + 3*b + 4*c = 41;

eqn1 := a + 3 b + 4 c = 41

eqn2 := 5*a + 6*b + 7*c = 20;

eqn2 := 5 a + 6 b + 7 c = 20

We solve simultaneously for {a, b} in terms of the c: > solve( {eqn1, eqn2}, {a, b} );

13 c 185 c 62 + , a= − } 9 9 3 3 Modify this solution so that it solves for {b,c} in terms of a. {b = −

5.5.2 Numeric Equations For a change of pace, let us say you want a numerical solution of the same two simultaneous equations we previously solved exactly, x + 3 y + 4 = 41, 5 x + 6 y + 7 = 20.

The numerical solution should be faster and more reliable than the exact solution, yet Maple knows that it knows the exact solution, and so returns that from fsolve. However we will get the numerical solution if we use decimal numbers in the equations: > >

# Enter eqn1, eqn2 with decimals # Solve {eqn1,eqn2} with fsolve

115

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

5.5.3 Nonlinear Equations Nonlinear equations are exactly what their name implies. They are equations containing functions that are not linear. Whereas quadratic, cubic, and quartic equations are all nonlinear by this definition, Maple knows the closed-form solutions to these types of equations and has no trouble applying them. However, there may not be analytic solutions to other, even simple, nonlinear equations. In those cases the best that Maple does is to find numerical solutions. Consider the simultaneous, nonlinear equations: sin(x) + 3 cos(y) = 1, x2 + y 3 = 12.

(5.14) (5.15)

Enter these equations as a set and try solving them with solve. You should not find much success. So, try solving them with fsolve. Use eval to check how well the solution satisfies the equations: > eval( eqn1, {x = . > eval( eqn2, {x = .

. .

, y = . , y = .

.} ) .} )

5.6 SOLUTION TO TOWER PROBLEM q: Determine an algebraic expression for the height of a five-block tower. ♠ > H := t + (t - a) + (t - 2*a) + (t - 3*a) + (t - 4*a);

H := 5 t − 10 a

q: Prove that the sum of the first n integers is n(n + 1)/2. ♠ > Sum( i, i = 1..n ) = sum( i, i = 1..n ); > factor( % ); n  (n + 1)2

i =

i=1 n  i=1

i =

2



n 1 − 2 2

n (n + 1) 2

q: Explain why the height of the tower consisting of n levels is: H = nt − n(n − 1)a/2. ♠

The total height of n noncompressed blocks is nt. However, with our model, each block other than the top one gets compressed. Consequently, the total height is computed by adding the heights of n successively thinner blocks: H = t + (t − a) + (t − 2a) + . . . + (n − 1)a = nt − a − 2a − . . . − (n − 1)a = nt − a[1 + 2 + 3 + . . . + (n − 1)]

116

CHAPTER 5

However, we recognize the series here as the one we have previously summed and so substitute: > H := n*t - a * sum( i, i = 1..n-1 ); > H := n*t - a * factor( sum( i, i = 1..n-1 ) );

# Factor sum

1 1 H := n t − a ( n2 − n) 2 2 a n (n − 1) H := n t − 2 q: Check that this is the same answer as before for a five-story tower. ♠ # Evaluate H for n = 5

> H = eval( H, n=5 );

a n (n − 1) nt − = 5 t − 10 a 2 This agrees with the explicit five-term calculation, namely, H = 5 t − 10 a.

q: Express H as a function of three variables, Hf (n, t, a). ♠ > Hf := (n, t, a) -> n*t - a*n*(n-1) / 2;

1 a n (n − 1) 2 q: Use your function and Maple’s solve command to determine how many blocks n are needed to make a tower of height 200 for arbitrary t and a. ♠ Hf := (n, t, a) → n t −

> ‘n‘ = solve( Hf(n, t, a) = 200, n );



n=

2t + a +

 √ √ 4 t2 + 4 t a + a2 − 1600 a 2 t + a − 4 t2 + 4 t a + a2 − 1600 a , 2a 2a

We see that there are two solutions for arbitrary t and a, as expected for a quadratic equation. We suspect that only one of them will be physically meaningful. q: Using your function, solve for the number of blocks of thickness t = 15 feet needed to make a tower of height 200 feet if the compression a = 0.001t. ♠ > ‘n‘ = solve( 200 = Hf(n, 15, 0.001*15 ), n );

n = (1987.583372, 13.41662797)

We get two answers, n = 1987.6 and n = 13.4, neither of them an integer. Because 13 × 15 = 195 and 14 × 15 = 210, the answer 13.4 is the one we would expect. The 1,988-block tower seems unreasonable and is related to a flaw in our model (as we shall see). q: Is it possible within our model to add another level to the tower but not have its height increase? Explain how the answer to this question is equivalent to finding a solution to the equation H(Nmax ) = H(Nmax + 1). ♠

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

117

This definition of “maximum” corresponds to adding another block to the tower and not having it get any higher. In some sense this is the maximum height, as adding another block beyond this will actually make the tower shorter in our model. What is happening is that at n = Nmax , the bottom block is being squeezed to zero thickness, and so adding another block gives the bottom block negative thickness. This is clearly a breakdown of our simple model. q: Find the algebraic value of Nmax that satisfies this last equation. ♠ > restart:

Hf := (n,t,a) -> n*t - a*n*(n-1) / 2;

1 a n (n − 1) 2 Here we placed both commands on one line and used a colon after restart to suppress Maple’s response. Now we solve it: Hf := (n, t, a) → n t −

> Nmax := solve( Hf(n, t, a) - Hf(n+1, t, a), n );

t a q: If t = 15 feet and a = 0.001t, what is the maximum height of the tower? ♠ Nmax :=

> t := 15;

a := 0.001*t;

t := 15,

Nmax;

a := 0.015,

1000.000000

We could also do the evaluation more explicitly with eval: > Nmax := solve( Hf(n, t, a) - Hf(n+1, t, a), n ); > eval(Nmax, [a = 0.015, t = 15]);

t a q: Test your function both numerically and symbolically by verifying that adding another level to the maximum number of levels does not increase the height. ♠ Nmax :=

> Nmax := t/a; > Hf( Nmax + 1, t, a); Hf(Nmax, t, a);

factor(%);

t t ( + 1) t t ( − 1) 2 t t (t + a) a , − a , 2 a 2 2a where we used the factor command for simplification.

5.7 DIFFERENTIATION: LIMIT, DIFF, D Calculus was invented, at least from our point of view, to help solve physics problems. Even today calculus is essential for solving scientific problems, and Maple is good at calculus. When speaking of calculus we usually think of differentiation, integration, and differential equations. We study differentiation in this chapter, integration in Chapter 6, and differential equations in Chapter 15.

118

CHAPTER 5

Derivatives arose in the study of kinematics, where there was the need to define the velocity of an object at each instant of time. Specifically, instantaneous velocity is defined as the limit of the rate of change of position, for infinitesimally small time intervals: > v(t) = Limit( (x(t + Delta) - x(t) )/Delta, Delta = 0);

x(t + ∆) − x(t) ∆→0 ∆ Of course you should recognize this limit as the definition of derivative. Indeed, if we use the active form of Maple’s limit command, we do get the derivative: v(t) = lim

> Limit( (x(t+Delta)-x(t)) / Delta, Delta = 0 ) = limit( (x(t+Delta)-x(t)) / Delta, Delta = 0 );

x(t + ∆) − x(t) = D(x)(t) ∆ Here D(x) indicates the derivative of x, and (t) a function of time t. lim

∆→0

Maple has the two commands diff and D for differentiation. The most straightforward is diff, although D is more powerful and useful. You give diff an expression or a function, and it returns the derivative as an expression. However, if you want your derivative to be a function or a mapping, then use D. (Alternatively, you may use unapply to convert diff’s expression into a function.)

5.7.1 Derivative as Expressions: diff # Inert, but pretty diff

> Diff( sin(x), x );

d sin(x) dx > diff( sin(x), x );

cos(x)

# Active form of diff

Just as we have seen with sum and Sum, capitalizing the first letter of diff produces the inert form Diff. In spite of inert forms doing no work, they are valuable as a check that Maple will do the computation you intend: > Diff( sin(x), x ) = diff( sin(x), x ); > ’diff( sin(x), x )’ = diff( sin(x), x );

# Derivative of expression # Delayed execution on LHS

d sin(x) = cos(x) dx Here we took the derivative of a built-in function sin(x). It is also legitimate to use diff to take the derivative of a user-defined function: > f := (x) -> ln(x) / (1-x);

# Define function f (x) = ln(x)/(1 − x)

119

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

f := x →

ln(x) 1−x

> Diff( f(x), x ) = diff( f(x), x );

d ln(x) 1 ln(x) ( )= + dx 1 − x x (1 − x) (1 − x)2

Here we use diff to take the derivative of an expression: > Diff( xˆ7 / (1-x), x ) = diff ( xˆ7 / (1-x), x);

d x7 7 x6 x7 ( )= + dx 1 − x 1 − x (1 − x)2

5.7.2 Second Derivatives To take a second derivative, you differentiate twice in succession, either by repeating the diff command or by repeating the x part: > f := (x) -> sin(x);

f := sin > ’diff( f(x), x )’ = diff( f(x), x );

# Single quotes for delayed execution

d f(x) = cos(x) dx > ’diff( %, x )’ = diff( %, x );

d d ( f(x) = cos(x)) = (−sin(x) = −sin(x)) dx dx > Diff( f, x, x ) = diff( f(x), x, x );

# Curly d’s indicate partial derivatives

∂2 sin = −sin(x) ∂x2 In these commands we started with the function f (x) = sin x and took the derivative once with respect to x to get df /dx, and then took the derivative of the derivative d(df /dx)/dx. Executing the above on f (x), as you see, returns us to − sin(x). Taking a derivative of some function twice gives you the second derivative d2 f /dx2 .

Another notation for second derivative is to use Maple’s sequence operator $. This operator says to repeat the preceding symbol by the number of times indicated after it. To name an instance, x$2 is the same as ,x,x. Thus we express higher derivatives in the succinct forms: > restart; f := (x) -> sin(x);

f := sin

120

CHAPTER 5

> ’diff( f, x$2 )’ = diff( f(x), x$2 );

# Derivative with respect to x2

∂2 f = −sin(x) ∂x2 > ’diff( f, x$3 )’ = diff( f(x), x$3 );

# Derivative with respect to x3

∂3 f = −cos(x) ∂x3 > Diff( xˆ7 / (1-x), x$2 ) = diff ( xˆ7 / (1-x), x$2 );

# Second derivative

d2 42 x5 14 x6 x7 2 x7 )= + ( + 2 2 dx 1 − x 1 − x (1 − x) (1 − x)3 ln x Exercise: Take the first derivative twice of f (x) = 1−x and see if you get the ♠ same answer as taking the second derivative just once.

5.7.3 Partial Derivatives with diff* If it is legal only only to take a derivative with respect to a variable, then why must we always enter that variable into diff? The answer is not trivial. On occasion the expression to differentiate may be one like f (x) = sin(x)b . To Maple, both x and b are equally good variables for differentiation, and so Maple must be told which one you would like to differentiate with respect to. At the prompts below, take the derivatives and see the differences: # Derivative of sin(x)b with respect to x # Derivative of sin(x)b with respect to b

> >

When a function contains more than one variable, then the differentiation process is called partial differentiation. The resulting derivatives are called partial derivatives and in mathematics are denoted with curly d’s, for example, ∂f (x, y)/∂y. In addition, since there are several variables, another type of second derivative is formed by differentiating once with respect to each of two different 2 variables. By way of example, we form ∂y∂ ∂x sin(x)y by first differentiating with respect to x, and then y : # 2nd partial derivative with respect to x, y

> Diff( (sin(x))ˆy, x, y );

∂2 ∂y ∂x

sin(x)y

> diff( (sin(x))ˆy, x, y );

sin(x)y ln(sin(x)) y cos(x) sin(x)y cos(x) + sin(x) sin(x)

# Active form

121

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS > Diff( (sin(x))ˆy, y, x )= diff( (sin(x))ˆy, y, x );

# Another second derivative

∂2 sin(x)y ln(sin(x)) y cos(x) sin(x)y cos(x) sin(x)y = + ∂x ∂y sin(x) sin(x)

5.7.4 Derivatives of Functions: D There will be times when you need to have the derivative you calculate be a function or mapping. For cases such as these, the D operator is used to take the derivative of a function (not an expression) and to return the result as another function. This type of derivative and functional analysis is more subtle than the derivatives we have been taking with diff, yet sometimes it is needed to get Maple to do what you want. For instance, consider the two expressions for derivatives: d(cos t) d cos , (t). dt dt The left expression implies the derivative of cos t with respect to t, while the right expression indicates a new function, the derivative of cos, that gets evaluated at t. It is the right type of expression that we want to use as a function. You may think that this is just semantics, yet observe that d d cos π = (−1) = 0. dt dt

Hence, if the t in d cos t/dt indicates some value, then this is the derivative of a constant, which is clearly 0. Consequently, we must be careful with arguments when we transform one function, such as cos, into another function, such as d cos /dt. The D form of derivative is designed to be used with this type of care. We will look at some examples to see what this means. Imagine that we are given the position of a mass attached to a nonlinear spring as x(t) = sin t + 0.3 sin2 t. We want to find the velocity v(t) = dx/dt and the acceleration a(t) = dv/dt as functions of time. We first try: # Defines function or mapping x(t)

> x := (t) -> sin(t) + 0.3*(sin(t))ˆ2;

x := t → sin(t) + 0.3 sin(t)

2

> v := (t) -> diff( x(t), t );

# Appears to define v(t) as derivative of x(t)

v := t → diff(x(t), t) > v(1); Error, (in v) wrong number (or type) of parameters in function diff

122

CHAPTER 5

The above response shows that diff(x(t),t) does not work as a proper function when a number is given as the argument. The reason is that Maple does not accept v(1) as the velocity at t = 1 although that clearly is what we want. This problem arises from the fact that diff does not produce a true function as an answer. One solution, which we find rather awkward, is to use unapply to convert the expression into a function: # Define v(t) as derivative of x(t)

> v := (t) -> diff( x(t), t );

v := t → diff(x(t), t) > v(t);

# OK for symbol, not OK for numeric argument

v(1);

cos(t) + 0.6 sin(t) cos(t) Error, (in v) wrong number (or type) of parameters in function diff # Use unapply for proper function

> v := unapply ( diff( x(t), t), t );

v := t → cos(t) + 0.6 sin(t) cos(t) > ’v(1)’ = v(1);

v(1) = cos(1) + 0.6 sin(1) cos(1)

In contrast, the command D produces a proper function that is equal to the derivative of the original function. Scrutinize how the arguments of the functions are implicit and do not have to be (and should not be) specified: # x(t) is arrow function

> x := (t) -> sin(t) + 0.3 * (sin(t))ˆ2;

x := t → sin(t) + 0.3 sin(t)2 # Form new function = derivative of x

> D(x);

t → cos(t) + 0.6 sin(t) cos(t) > v := D(x);

# Form v(t) as true arrow function or mapping

v := t → cos(t) + 0.6 sin(t) cos(t) > ’v(1)’ = v(1);

v(1) = cos(1) + 0.6 sin(1) cos(1)

When the derivative function is used as a regular function, for plotting and so forth, its argument needs to be specified. To cite an instance, here we plot position, velocity, and acceleration: > a := D(v);

# Form new function for acceleration 2

a := t → −sin(t) + 0.6 cos(t) − 0.6 sin(t) 2

> plot( {x(t), v(t), a(t)}, t = 0..10 ); # Plot three functions > plot ( {x(t), D(x)(t), D(D(x))(t)}, t = 0..10 ); # Same plot, using D for derivatives

123

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

1 0.5 2

4

t

6

8

10

0 –0.5 –1 –1.5

We look now at some more examples of the use of D for creating derivatives of an arrow function: > ’D(g)’ = D (g);

D(g) = D(g)

OK, we get back what we entered, which may be correct, but is not exciting. This is related to the fact that Maple does not know anything about g . If we give D a function in which the argument is implicit (not given explicitly), then Maple returns the derivative as an implicit function: # Take derivative of sin, an implicit function

> ’D( sin )’ = D( sin );

D(sin) = cos > f := (x) -> ln(x) / (1-x); > ’D (f)’ = D (f);

# An explicit, arrow function of x # Produces arrow function = derivative

ln(x) 1−x ln(x) 1 + D(f ) = (x → ) x (1 − x) (1 − x)2 f := x →

We see that if f (x) is an explicit function of x, then Maple uses the arrow sign to make it clear to us that we have defined a new function of x. It is best to think of D(f) as an entirely new function, the derivative, and not as an operator acting on f (x). This point is often confusing in the mathematical notation for mappings and transformations (Landau’s second rule). To evaluate this new function obtained by differentiation, we add the argument to D(f), or in this case D(sin): > ’D(sin) (Pi)’ = D(sin) (Pi);

D(sin)(π) = −1

’D(sin) (t)’ = D(sin) (t);

D(sin)(t) = cos(t)

This makes sense if we think that sin is the argument to the D function and that it creates a new function D(sin). This new function, in turn, is evaluated at an

124

CHAPTER 5

argument x. Naturally, we are allowed to take the function D(f) and evaluate it with any argument we want: > ’D(f) (x)’ = D(f) (x);

’D(f) (y)’ = D(f) (y);

ln(x) 1 + x (1 − x) (1 − x)2 ln(y) 1 + D(f )(y) = y (1 − y) (1 − y)2

D(f )(x) =

Look at how we do not indicate the fact that f is a function of some variable by giving an argument to f within the derivative operation, for example, as D(f(x)). Nor do we state with respect to which variable we are taking the derivative. In that we are not indicating that the derivative is with respect to x, Maple does not view x as a simple argument, but rather as a general variable or function that has yet to be defined. That being the case, if you ask D to differentiate f (x), it assumes x is some arbitrary function that may have a derivative of its own: > restart: f := (x) -> ln(x) / (1-x); > ’D( f(x) )’ = D( f(x) );

# Clear variables and redefine function # Take derivative

ln(x) 1−x D(ln(x)) ln(x) D(x) D(f(x)) = + 1−x (1 − x)2 f := x →

Likewise, since f (15) is just a number, D(f(15)) is the derivative of a constant, which of course is zero: > ’D( f(15) )’ = D( f(15) );

D(f(15)) = 0

As we have said, if you want to evaluate the derivative function for argument 15, then you should think of D(f) as the function: > ’D(f)(15)’ = D(f)(15);

D(f )(15) = −

1 1 + ln(15) 210 196

5.7.5 Second and Partial Derivatives: D Second derivatives with the D operator are formed either by applying D twice, or by using the composition operator @ (which repeats the action of a function, much like $ repeats the argument): > restart:

f := (x) -> ln(x) / (1-x);

125

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

# Second derivative on arrow function

> ’D( D(f) )’ = D( D(f) ); > ’( D@@2 )(f)’ = ( D@@2 )(f);

f := x →

# Another 2nd derivative on arrow function

ln(x) 1−x

D(D(f )) = (x → −

2 1 2 ln(x) + + ) x2 (1 − x) x (1 − x)2 (1 − x)3 # Second derivative on built-in function

> ’D( D(sin) )’ = D( D(sin) );

D(D(sin)) = −sin

With functions of several variables (multivariate functions) such as g(x, y) = sin x cos y , we form different second derivatives by taking the partial derivative with respect to either x or y . You indicate the desired operation to Maple by including a list [in square brackets] of the variable(s) by number that you desire to differentiate with respect to: # A function of two variables # Derivative with respect to 1st argument

> g := (x, y) -> sin(x) * cos(y); > ’D[1](g)’ = D[1](g);

g := (x, y) → sin(x) cos(y) D1 (g) = ((x, y) → cos(x) cos(y)) # Derivative with respect to 2nd argument # Derivative with respect to 1st & 2nd arguments

> ’D[2](g)’ = D[2](g); > ’D[1,2](g)’ = D[1,2](g);

D2 (g) = ((x, y) → −sin(x) sin(y)) D1, 2 (g) = ((x, y) → −cos(x) sin(y)) > ’D[2,1](g)’ = D[2,1](g); # Same answer, order does not matter > ’D[2,1](g)(Pi/4,Pi/4)’ = D[2,1](g)(Pi/4,Pi/4); # 2nd derivative, explicit

D2, 1 (g) = ((x, y) → −cos(x) sin(y)) −1 π π D2, 1 (g)( , ) = 4 4 2 Check how Maple indicates these second derivatives as arrow functions (mappings). If the function does not have an explicit definition, then the D operator yields an abstract form: # 2nd partial undefined function

> ’D[1,2](n)’ = D[1,2](n);

D1, 2 (n) = D1, 2 (n)

Unless the function is very unusual, partial derivatives are generally independent of order: > ’D[1,2](h)’ = D [1,2](h); ’D[2,1](h)’ = D[2,1](h);

D1, 2 (h) = D1, 2 (h),

# Independent of order

D2, 1 (h) = D1, 2 (h)

Clearly then, this option gives us another way to determine the second derivative, even for univariate functions:

126

CHAPTER 5

> ’D[1,1](g)’ = D[1,1](g); ’D[2,2] (g)’ = D[2,2](g); > ’D[1,1](sin)’ = D[1,1](sin);

D1, 1 (g) = ((x, y) → −sin(x) cos(y)) D2, 2 (g) = ((x, y) → −sin(x) cos(y)) D1, 1 (sin) = −sin

5.8 NUMERICAL DERIVATIVES* Normally you should be happy to have Maple calculate derivatives for you, especially if they are done exactly. If it cannot do it exactly, then let Maple calculate the derivative numerically. We now ask the question, “How does one calculate a derivative numerically?” This query is interesting, as it is useful to understand how Maple does things under cover, as well as being something we will need in Chapter 15, where we undertake a numerical study of projectile motion with friction. Determining a numerical derivative is essentially determining the slope of a function, namely, drawing a tangent to a curve. This is not hard. Say we want a numerical approximation to the derivative of f (x) = sin x. We know the exact answer, > f := (x) -> sin(x);

‘D(f(x))‘ = D(f)(x);

f := sin

D(f (x )) = cos(x)

The exact derivative is defined as the limit: df (x) def f (x + ∆x) − f (x) . = lim ∆x→0 dx ∆x

(5.16)

Numerical approximations use a similar approach, except that one skips the limiting process and simply does the division for a small but finite value of ∆: df (x) f (x + ∆x) − f (x) (5.17)  . dx ∆x So let us try this out on some typical values. We will start with x = 1.0 and ∆ = 0.1 (decimal points to ensure that Maple does numerical computations) and compare the approximation to the exact answer: > ’df[exact]/dx’ = cos(1.);

’df[approx]/dx’ = (sin(1.+0.1) - sin(1.))

/ 0.1;

df approx df exact = 0.5403023059 = 0.497363753 dx dx We see that the approximation is at least close. We make the approximation better by using smaller and smaller values for ∆: > ’df[approx]/dx’ = (sin(1.+0.01) - sin(1.)) / 0.01; > ’df[approx]/dx’ = (sin(1.+0.001) - sin(1.)) / 0.001;

127

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

df approx = 0.53608598 0.5398815 dx The numerical derivative rule we have just outlined is called the forward-difference approximation because it moves forward by ∆ to sample how the function changes. A more balanced rule that requires no extra computation is known as the central difference approximation. It evaluates the function on either side of x and takes that difference: ∆ df CenDif f f (x + ∆ 2 ) − f (x − 2 ) (5.18) = . dx ∆ We apply it with the same values of ∆ as used for the forward difference: > ’df[exact]/dx’= cos(1.); > ’df[CenDiff]/dx’ = ( sin(1.+0.05) - sin(1.-0.05) ) / 0.1;

# Exact answer

> ’df[CenDiff]/dx’ = ( sin(1.+0.025) - sin(1.-0.025) ) / 0.05; > ’df[CenDiff]/dx’ = ( sin(1.+0.012) - sin(1.-0.012) ) / 0.024;

df exact = 0.5403023059 dx df CenDiff = 0.540077208 0.54024603 0.54028933 dx We see that even the largest value of ∆ yields three places of precision, with the approximation still improving as ∆ is made smaller still. Notwithstanding the use of even more precise algorithms in scientific work, our exercise here should convince you that numerical derivatives are generally reliable.

5.9 ALTERNATE SOLUTION: MAXIMUM TOWER HEIGHT If we think of Nmax as a continuous variable and take a mathematical point of view, then the maximum height occurs at the point at which an infinitesimal change in n leads to no change at all in the tower’s height. We know from calculus that this occurs when the derivative vanishes, that is, when dH(n, t, a)/dn = 0: q: Use Maple’s derivative function diff to determine dH(n, t, a)/dn. ♠ > restart: Hf := (n, t, a) -> n*t - a*n*(n-1)/2; > Diff( Hf(n,t,a), n ) = diff( Hf(n,t,a), n );

normal(%);

1 Hf := (n, t, a) → n t − a n (n − 1) 2 a n (n − 1) a (n − 1) a n ∂ (n t − ) = t− − ∂n 2 2 2 ∂ 1 1 1 (n t − a n2 + a n) = t − a n + a ∂n 2 2 2

128

CHAPTER 5

q: Before we defined the “maximum” tower height as one for which H(Nmax + 1) = H(Nmax ), now we want to use calculus. Use Maple’s solve function to determine the value of n for which dH/dn = 0. ♠ # Back quotes for string

> ‘Nmax‘ = solve (diff( Hf(n,t,a), n), n );

Nmax =

2t + a 2a

q: Explain in your own words why this is not the same as Nmax . ♠ Our previous definition of Nmax was one for which Nmax and Nmax + 1 both gave the same height. That definition gave Nmax = t/a and Nmax + 1 = (t + a)/a as producing the same height. The derivative vanishes at (t + a2 )/a, which is right between our previous two answers. Neither one is really more correct, but they do correspond to different definitions of maximum. q: Plot up the results as a check. ♠ It is not possible to make plots of symbolic functions, so let us assign values to t and a. We know from our computations that the maximum, in some sense of the word, occurs near n = 1000, so we will narrow our plot to that region: > t := 15; a := 0.001*t; > plot( Hf(n,t,a), n = 0..1200 );

# Broad view shows growth, then curvature # Narrow view highlights maximum

> plot ( Hf(n,t,a), n = 998..1003 );

7507.5

7000 6000

7507.49

5000 7507.48

4000 3000

7507.47

2000 1000 0

7507.46 200

400

600 n

800

1000

1200

998

999

1000

n

1001

1002

1003

We do indeed see that the maximum height, as determined by the zero of the derivative, occurs at the true maximum of the graph, n = 1000.5, but that in terms of integer block numbers, there are maxima at n = 1000 and 1001. 5.10 ASSESSMENT AND EXPLORATION 1. How relevant is this model to building actual skyscrapers? Specifically, is it wise to use the same structural design on the top and bottom floors?

129

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

k

m

x

Figure 5.3 A mass m attached to a spring with constant k that is stretched a distance x from its equilibrium position.

2. It is not realistic to have the addition of levels make the tower shorter. Explain in words whether an assumption is at fault, or are we just applying the model beyond its range of applicability. 3. Rather than (5.1), a more realistic model might assume that the blocks get harder to compress at some point and so their thickness never becomes negative. An equation that describes this model may be t=

t0 1 + b i2

where t is the thickness of a single block supporting i blocks above it, and b  1/100 is a small number. Obtain an expression from Maple for the total height of a tower of N blocks. Hint: while the sum is simple, Maple may not be able to come up with a simple answer. However, numerical evaluation is possible. 4. Use the improved model to derive a Maple function that calculates the height of a tower containing N blocks. 5. Is there a maximum height for the tower now? Plot up the results to be sure. 6. The rate of increase of the tower height with number of blocks is given by the derivative dH/dn. Is there some value of n for which the rate of increase is a maximum? What is the numerical value for the maximum height now (use similar parameter values as before)?

5.11 AUXILIARY PROBLEM: NONLINEAR OSCILLATIONS A mass m = 2 kg is attached to a spring as in Figure 5.3. The mass is displaced from its rest position and then released so that it oscillates about the equilibrium position. When the mass is a small distance x from the equilibrium position, the restoring force f tending to bring the mass back to equilibrium is directly proportional to x: f = −k x, (5.19)

130

CHAPTER 5

where k is the spring’s force constant k = 25 Newton/meter. The equation of motion for this system is Newton’s second law of motion: m d2 x . (5.20) dt 2 As shown in the study of simple harmonic motion, a solution for the position x as a function of time t is a trigonometric function, −kx =

f = m a,



x(t) = sin(ω t),

ω=

k/m.

(5.21)

1. Write a Maple function that returns the position x(t) for any input argument t. (You are given numerical values for m and k , and therefore for ω .) 2. Plot the position for times in the range 0 ≤ t ≤ 12. As always, add labels and a title to your plot. 3. In mechanics we learn that the velocity v is the first derivative of x with respect to time, and that acceleration a(t) is the second derivative: dx dv d2 x (5.22) , a(t) = = 2. dt dt dt Use Maple’s diff command to determine analytic expressions for velocity and acceleration as functions of time. Determine whether (5.19) is true for this oscillator, that is, compare m a to the force −k x. Plot x(t), v(t), and a(t) in the same graph for t between 0 and 10, with different colors for each of the three functions. Take your plot and change some lines to dotted and some to bold. Which scheme do you think is clearest? Make a phase-space (parametric) plot of v(t) versus x(t) to produce v(x). A more realistic model for an oscillator has the restoring force acting upon the mass depend slightly upon whether the mass is to the right or to the left of the origin (nonlinear terms). For this case the position as a function of time might be described by v(t) =

4. 5. 6. 7. 8.

x(t) = sin ωt + b sin2 ωt,

where b (which controls the right-left asymmetry) is a small number compared to 1. 9. Modify your Maple function x(t) to a new one x(b, t) and test that you get the old answer for b = 0. 10. For this new oscillator, what are the velocity, acceleration, and force as functions of time? Make several plots for a range of b values between 0 and 1. Add comments to your worksheet indicating for what b values the functions no longer look sinusoidal. 11. Use a 3-D plot with axis and labels to examine x as a function of time for the entire range of b values. Examine the different options for displaying

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

131

your surface, as well as different viewing angles. Print out several of the best representations (assume that the printer prints grayscale only). Make sure one of your plots is of only the actual points that are calculated (no connecting lines) and that another one is of contours. 12. Examine the v(t) versus x(t) phase-space plot for this anharmonic oscillator for a number of b values. 13. Make a parametric plot of the force f (t) = ma(t) versus position x(t) for this anharmonic oscillator. Determining an analytic expression for f (x) would be more difficult. 14. Create two animations of this function, one in which the different frames correspond to different b values, and a second in which the different frames correspond to different times. Look at both and decide which produces the best visualization (animations are usually best to show time dependence). 5.12 KEY WORDS AND CONCEPTS analytic solution mapping roots

derivative maximum simultaneous equations

derivative operator numerical solution theory vs. model

limit partial derivative

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

Give three possible meanings of the words maximum height. Are mathematical equations always true? Must all equations have analytic solutions? If an equation has a numeric solution, must it also have an analytic solution? Do nonlinear equations occur in nature? How are simultaneous equations different from other types? Are all solutions to an equation equally valid? Are nonlinear equations harder to solve than linear ones? If so, why? Do you have to take a limit to evaluate a derivative? Does it make sense to take the derivative of a function of several variables? What is the difference in Maple between taking the derivative of an expression and a function? 12. Can you take the derivative of a number? 5.13 SUPPLEMENTARY EXERCISES 1. Use both solve and fsolve to find as many solutions as you are able of a. x3 + x2 + x = 1. Beware, this equation may have complex roots that fsolve will not show unless you include complex in the argument set: (fsolve(f(x),x,complex). b. x4 + c x3 + x2 + x + 1. Hint: you may need to make c numeric. c. sin(x)2 = x. Make a plot to check that your answer is reasonable. 2. Determine the limit . a. lim→0 sin(x+)−sin(x) 

132

CHAPTER 5

b. lim→0 sin(x+/2)−sin(x−e/2) .  c. γ(v) = limc→∞ √ 1 2 2 . d. γ(v) = limv→c √

1−v /c 1 . 1−v 2 /c2

3. Determine the first and second derivatives with respect to x of the following expressions: a. xn b. x(−n) c. x1 d. tan x e. sin(sin x) 2 2 f. (a √ x + b x + c)/(d x + e x + f ) g. 1 − x4 4. Nonlinear equations are more interesting, but more challenging to solve, than linear equations. They are called nonlinear because the unknown appears to a higher power than the linear first power. Find the solutions of the nonlinear, simultaneous equations: x2 − 4 y 2 = 1,

x − 8 y 3 = 3.

5. You are given the expression 10 x4 sin(k x). a. Enter this as a Maple expression and determine when it equals 0, and its first and second derivatives. b. Enter this as a Maple function and determine when it equals 0, and its first and second derivatives. 6. In mechanics, we often study the position x of an object as a function of time t. If we take the first derivative of the position with respect to time, we get the velocity v as a function of time. In turn, the acceleration a is the second derivative of x with respect to time. In the following exercises, calculate x(t), v(t), and a(t) for each case, and plot all the functions on the same graph as a function of time for 0 ≤ t ≤ 10. Use different colors for each function: 3 t2 + 5 t − 13 a. x(t) = √ b. x(t) = t3 c. x(t) = (et )t 7. Here is a real function of the variables x, y , and z : g(x, y, z) = 

x2

1 . + y2 + z2

Check whether g is a solution of Laplace’s differential equation by showing that the sum of these three derivatives equals 0: d 2 g d2 g d2 g + 2 + 2 = 0. dx2 dy dz

8. Find the first and second derivatives with respect to x of the functions:

SOLVING EQUATIONS, DIFFERENTIATION; BUILDING TOWERS

133

a. y = x/(x + 1) b. y = (4 x2 + 3 x)√ln(x) c. y = √ tanh(3 x) e 5 x d. y = x3 + 4 x2 + 7 x + 1 9. A rock is hurled upward, and its distance above the ground is given by y = 210 t − 34 t2 . Use Maple to calculate: a. the maximum height from the ground of the rock, b. the time at which the rock reaches the maximum height, c. the time at which the rock strikes the ground, d. the rock’s speed when it hits the ground. 10. The resistance R of two resistors in parallel is given by 1 1 1 = + . R R1 R2 Solve for R and take the limit as R2 approaches infinity. Does your answer make sense?

Chapter Six Integration; Power and Energy Usage (Also 14)

6.1 PROBLEM: RELATING POWER AND ENERGY USAGE The use of electrical power is characterized by variations of different sorts. There are fluctuations related to the day-night cycle, fluctuations related to the change of seasons (approximately monthly), fluctuations due to weather patterns (daily and long-term), and others [Energy]. As an instance, observe the hourly use of power for Australia during a one-year period shown in Figure 6.1 [D&A 00]. Generally, power consumption peaks in the winter due to heating demands, and peaks again in the summer due to cooling demands. Power usage also shows a year-to-year growth that may be related to population growth and economic development, as well as the seasonal variation that is the most evident, especially when the daily fluctuations are removed by taking averages over weekly periods. Problem: Determine the total energy used over a three-year period. 6.2 EMPIRICAL MODELS The data for power usage is just a series of numbers, each for a different time, and therefore not in an appropriate form for analytic integration.1 We model it approximately with three formulas: P (t) = 4 + [2 + sin(2 π t)] sin(π t/91),   t sin(2 π t)2 sin(π t/91) P (t) = 4 + (2 + + ), 365 2 2   t sin(π t/91) P (t) = 4 + (2 + e(− sin(2 π t)) ), + 365 2

(6.1) (6.2) (6.3)

where the power P (t) is in GW (109 watts) and the time t is in days. These formulas try to incorporate the various time dependencies of power usage. Since t is in days, a function of the form sin(2 π t/T ) is periodic with period T days. There1 In Chapter 14 we do this integration numerically with Java. Numeric integration can be used on tables of numbers as well as functions, and is often required for realistic problems.

135

INTEGRATION; POWER AND ENERGY USAGE (ALSO 14)

Figure 6.1 Electrical power use for Australia (left) and Mad del Plata (right) on an hourly basis over a one-year period.

fore sin(2 π t) in these models represents a daily variation of power. Likewise, sin(π t/91) has a period of 182 days (half of a year), and so represents a variation that peaks twice during the year. Models 2 and 3 have a term that grows linearly with time to account for year-to-year growth. Finally, model 3 incorporates an exponential time dependence of power use on a daily time scale. To see if these models look anything like the data, we plot them: > restart; with(plots): with(plottools): # Model 1 > plot(4 + (2 + sin(2*Pi*t)) * sin(Pi*t/91), t = 0..700, title=‘Power‘);

Power (GW) vs time(days)

7 6 5 4 3 2 1 0

>

100

200

300

t

400

500

600

700

# You plot models 2 and 3 here

136

CHAPTER 6

Indeed, there is a biyearly variation about a nonzero average, a daily variation, and, for models 2 and 3, a long-term growth of the average. 6.3 THEORY: POWER AND ENERGY DEFINITIONS In common usage, the words “power” and “energy” may differ from their scientific meanings (like paying the “power bill” when you are really buying energy). Power is defined as the rate at which work is done, or energy used: dW (6.4) . dt Consequently, if energy is measured in joules (the System International unit), then power is measured in joules/second or watts (W). P =

The power company charges you for kilowatt-hours. A kilowatt is 1,000 watts, an hour is 3,600 seconds, and so one kilowatt-hour = 1000 watts × 3600 sec = 3.6 × 106 joules. Typical costs for a kWh is between 2 and 20 cents, depending on where you live and politics. In any case, you are paying for energy, not power. To set the scale for some realistic numbers, one horsepower (hp) equals 746 W. One energy worker, the average output power of a human body, is 100 W, significantly less than a horse. A nuclear power plant produces energy at the rate of 1,150 megawatts, significantly more than a horse. In our problem we are given the power used as a function of time and asked to compute the energy used in three years. Rearranging the definition of power, (6.4) gives the energy used during infinitesimal time dt as dW = P(t) dt,

(6.5)

which gives he total energy used from some time 0 to T as  T

W(T ) =

P(t) dt.

(6.6)

0

This means that the solution to our problem is the integration of the three models for power usages, (6.1)–(6.3), which is equivalent to measuring the areas under the P (t) vs. t curves. If an analytic integration is possible, then we will have a function W (T ) to use for predicting future energy usage. Otherwise we just have to evaluate the integral numerically and plot the numbers up to see the trends. 6.4 MAPLE: TOOLS FOR INTEGRATION Integrals were invented to determine quantities such as the potential energy in the interior of a spherical planet:  R

V(R) =

4 ρ(r) π r dr. 0

(6.7)

137

INTEGRATION; POWER AND ENERGY USAGE (ALSO 14)

Calculus is still essential for solving scientific problems, and Maple is good at calculus. In the previous chapter we studied the derivative, here we study the integral, which is essentially the inverse of the derivative (antiderivative). An integral without explicit limits, such as f (x) dx, is an indefinite integral. An  integral with limits, such as ab f (x) dx, is a definite integral. The difference is important; an indefinite integral is still a function of x, while a definite integral is just a number. As we are about to see, Maple handles both types of integrals.

6.4.1 Indefinite Integration The Maple command for integration is int, with inert form Int: > int( y / sqrt(1+x), x ); Int( y / sqrt(1+x), x ); > Int( y / sqrt(1+x), x ) = int( y / sqrt(1+x), x );

2y



√ 1+x 





# Evaluate, display # Both together

y dx 1+x

√ y dx = 2 y 1 + x 1+x

Look at the second argument to int. It is the variable with respect to which we want the integration performed. Be careful, it does not have to be x: > Int( y / sqrt(1+x), y ) = int( y / sqrt(1+x), y );





# x = integration variable

y y2 dy = √ 1+x 2 1+x

where, we would have to include the integration constant by hand, if needed. We check the answer via differentiation: > Diff( Int( y / sqrt(1+x), y), y) = diff( Int( y / sqrt(1+x), y ), y);

∂ ∂y







y dy 1+x

= √

y 1+x

Beware, int does not write out a constant of integration for these indefinite integrals. You will need to include that by hand when appropriate.

6.4.2 Definite Integration Definite integration includes the integration limits as a second argument in the int command:

138

CHAPTER 6

> Int( x / sqrt(1+x), x = a..b) = int( x/sqrt(1+x), x = a..b );

 b a



√ √ x 2 (1 + b)(3/2) 2 (1 + a)(3/2) −2 1+b− +2 1+a dx = 3 3 1+x

In spite of Maple’s awesome powers for doing integrals, some functions cannot be integrated analytically: > Int(exp(-x)*sin(x)/sqrt(1+x), x = 0..1) = int(exp(-x)*sin(x)/sqrt(1+x), x = 0..1);

 1 (−x) e sin(x) 0



1+x

dx =

 1 (−x) e sin(x) 0



1+x

dx

We see that Maple just repeats the integral without evaluating it. This response is Maple’s way of saying that it is stumped without losing face. However, even if Maple cannot figure out an exact value for the integral, it should be able to evaluate it numerically. To do that, just tell Maple to evaluate with floats ( evalf): > Int( exp(-x)*sin(x) / sqrt(1+x), x = 0..1 ) = evalf( int( exp(-x)*sin(x) / sqrt(1+x), x = 0..1 ) );

 1 (−x) e sin(x) 0



1+x

dx = 0.1965185003

Now and then you may have to try different approaches to get a workable form for a needed integral, for example, S=

 

1 + b t + c t2 dt.

Have Maple evaluate this expression for S as an indefinite integral, and then have Maple evaluate this expression for S as the definite integral:  T

1 + b t + c t2 dt.

0

Even the definite integral is terribly complicated. See if substituting some numerical values for the constants helps: >

# Assign values to b, c, and T , reevaluate

6.4.3 Integrals as Functions It should be noted that int, like diff, produces an expression, not a function. Use the unapply command to make the indefinite integral I(x) = f(x) dx into a function of x: > myInt := unapply( int(y/sqrt( 1+x ), x ), x ); > myInt(z);

# Form function I(x) # Evaluate function for argument z

139

INTEGRATION; POWER AND ENERGY USAGE (ALSO 14)

# Differentiate integral

> D(myInt);

:= x → 2y √ 2y z + 1 y √ x → x+1

myInt



x+1

In view of the fact that definite integrals are just numbers, the unapply command does not work for them. However, let us imagine an integral as a function of both its upper and lower integration limits: > Int( x/sqrt(1+x), x = y..z ) = int( x/sqrt(1+x), x = y..z );

 z y



# Form I(x)

√  x 2 (z + 1)(3/2) 2 (1 + y)(3/2) −2 z+1− +2 1+y dx = 3 3 1+x

The integral clearly depends upon y and z , the endpoints of the interval. Thus we make the integral a function of the endpoints: # Define # Evaluate

> myInt := (a, z) -> int( x/sqrt(1+x), x = a..z ); > myInt( a, z );

myInt := (a, z) → 1)(3/2)

2 (z + 3

−2

> diff( myInt(a, z), z );





z+1 −

 z

a (3/2) a)

2 (1 + 3

x dx 1+x √ +2 1+a



# Test, simplify

simplify(%);

z+1− √

1 z+1



z z+1

6.5 PROBLEM SOLUTION: ENERGY FROM POWER We now know how to evaluate integrals, so let us do some! We will investigate models 1 and 3 here, and leave model 2 for you. For model 1, let us first define a function for the power, and then look at it to see if it looks OK: > P := (t) -> 4 + ( 2+sin(2*Pi*t) )* sin(Pi*t/91); > plot( P(t), t = 0..365, title = ‘model 1, P(t) vs t, for one year‘);

P := t → 4 + (2 + sin(2 π t)) sin(

1 π t) 91

140

CHAPTER 6

model 1, P(t) vs t, for one year

7 6 5 4 3 2 1 0

50

100

150

t

200

250

300

350

Now that we know that the integrand looks reasonable, let us integrate it: > Int( P(t), t ) = int( P(t), t );



4 + (2 + sin(2 π t)) sin(

πt ) dt = 91

181 π t 183 π t πt ) 91 sin( 91 ) 91 sin( 91 ) 91 4t − + − π 362 π 366 π The expression for the integral is neat and shows that the total energy used has some terms that oscillate as a function of time, as well as a term that increases linearly with time. However, it is missing the integration constant, which is clear since the t = 0 value of the integral is −182/π , when it should be 0. One way of eliminating the need to personally include the integration constant, is to evaluate the integral as a definite integral: 182 cos(

> Int( P(t), t = 0..T ) = int( P(t), t = 0..T );

 T

1 πT πt ) dt = (264984 π T − 12056772 cos( ) 91 66246 91 0 183 π T 181 π T ) − 16471 sin( ) + 12056772) /π +16653 sin( 91 91 The above equation is not as neat an expression as the indefinite integral, but it is correct (vanishes at T = 0). We define the energy function E(t) as the power integrated up to time T , and evaluate the energy used after one year: 4 + (2 + sin(2 π t)) sin(

> E := (T) -> int( P(t), t = 0..T ); > E(0); E(365);  T

E := T → 0,

P(t) dt 0

π π 2 −8281 sin( 91 ) + 24179790 π − 3014193 cos( 91 ) + 3014193 33123 π

141

INTEGRATION; POWER AND ENERGY USAGE (ALSO 14)

Maple is returning the one-year result as an exact number, which really does not make much sense here, since we want a floating-point number to tell the stockholders. Consequently, we modify the definition of E(t) to float the answer and look at the answers for zero, one, and two years: > E := (T) -> evalf( int(P(t), t = 0..T ) ); > E(0), E(365), E(370);

 T

E := T → evalf( 0.,

P(t) dt) 0

1460.029026,

1481.205658

The energy used does appear to be growing year by year, something we here check with a simple plot for 1,000 days: > plot( E(T), T = 0..1000, title = ‘Energy (GW) used after T days‘);

Energy (GW) used after T days 4000

3000

2000

1000

0

200

400

T

600

800

1000

We see a small fluctuation about a linear increase. This does not bode well for the planet! We now conduct a similar study for model 3. The power function is now: > P := (t) -> ( 4 + 1/365*t + sin(1/91*Pi*t)/2 ) * ( 2 + exp(-sin(2*Pi*t) ) ); > plot ( P(t), t = 0..30, title = ‘model 3 P(t), for one month‘ ); > plot ( P(t), t = 0..365, title = ‘model 3 P(t), for one year‘ );

P := t → (4 +

1 1 1 t + sin( π t)) (2 + e(−sin(2 π t)) ) 365 2 91

142

CHAPTER 6

model 3 P(t), for one month

model 3 P(t), for one year

24

20

22

18

20 18

16

16 14

14

12

12 10

10 0

5

10

15 t

20

25

30

0

50

100

150

t

200

250

300

350

This model looks good and shows an interesting daily and yearly variation. Now that we know that the integrand looks reasonable, let us integrate it: > Int(P(t), t) = int(P(t), t);

t 1 + + ... 365 2 Maple appears to give up on us (just repeating the input), so let us try the definite integral form: (4 +

> Int( P(t), t = 0..T ) = int( P(t), t = 0..T );

 T

(4 + 0

t + ··· 365

Again Maple fails us. Maybe numerical integration works? > Int( P(t), t = 0..365 ) = evalf( int( P(t), t = 0..365 ) );

 365

(4 + 0

t 1 πt + sin( )) (2 + e(−sin(2 π t)) ) dt = 5364.722015 365 2 91

Yes that works, so let us define an energy function equal to the integral of the power, and plot and evaluate the results: > E := (T) -> evalf( int( P(t), t = 0..T ) ); > E(0), E(365), E(730);  T

E := T → evalf(

0.,

P(t) dt) 0

5364.722015,

11921.61441

This calculation takes some computer time, since Maple must do a numerical integration, and Maple is slow with numerics. In the present case, we have a highly oscillatory integrand and Maple’s adaptive step sizing may have trouble. We are free to try Maple’s plot command, however, since it uses a minimum of 50 points, this also takes a real long time. Consequently, we use the resolution option to set the number of horizontal points to less than the default value of 200:

143

INTEGRATION; POWER AND ENERGY USAGE (ALSO 14)

> plot ( E(T), T = 0..1000, resolution = 20, title = ‘Energy Used, Model 3 T Days‘ );

Energy Used (GW), Model 3 after T Days 16000 14000 12000 10000 8000 6000 4000 2000 0

200

400

T

600

800

1000

We see, again, a rather linear increase in energy use for the next few years.

6.5.1 Your Solution to Model 2 1. Analyze model 2 in a manner similar to that used for models 1 and 3. 2. These models show a rather linear increase in integrated energy over time. This is a long-range concern, because there is no way for a finite amount of energy to sustain this growth. Regardless of our inability to dictate energyuse policy to the world, modify models 1 and 3 so that the total energy-use growth rate is approximately 10% per year. 3. Devise a model in which the total energy used becomes a constant in time. 6.6 KEY WORDS AND CONCEPTS definite integral integration limits

indefinite integral

integration constant

power vs. energy

1. If you buy a truck, do you want it to have lots of power or lots of energy? 2. If an integral represents the area under a curve, how does indefinite integration make sense? 3. How may an integral be thought of as a function? 4. If the definite integral of some expression exists, does that mean that the indefinite integral must also exist? 5. If the indefinite integral of some expression exists, does that mean that the definite integral must also exist? 6. Is it possible to integrate an integral? 7. If the numerical integral of some expression exists, does that mean that the analytic integral must also exist? 8. Is it possible to integrate a function that becomes singular (equal to infinity) within the region of integration?

144

CHAPTER 6

6.7 SUPPLEMENTARY EXERCISES Evaluate the following integrals: 1. 2. 3. 4. 5.

 2 √x − 5 x + 8 dx 3 x3 + 5 x dx  1 √x3 +5 x dx 0 e 1

dx 2  1x +2 1x+4 dx 2 −1  ∞ (x−2) 2 (−x ) 6. 0 e dx

7.

ln(x) dx

Chapter Seven Matrices and Vectors; Rigid-Body Rotation

7.1 PROBLEM: RIGID-BODY ROTATION The study of rotations in classical mechanics is an excellent example of the use of vectors and matrices in science. The problem we study is basically an exercise that we work out in full and ask you to repeat for another case. For comparison, we repeat this same problem using Java matrices in Chapter 18. Even if you are not familiar with the physics, we suggest that you work with us through these examples as a review of matrices and as a preview of what is to come; it should also make learning the physics easier in the future. Problem a (we work this one through for you): Consider Figure 7.1 showing a barbell composed of a mass m1 attached to mass m2 by a massless rigid rod of length 2a. The barbell is fixed at an angle θ with respect to the z axis, and is being rotated with an angular velocity vector ω along the z axis. a. Use Maple to determine the vector angular momentum L of this barbell by adding up the angular momenta of the two masses. b. Use Maple to evaluate the inertia tensor {I} for this barbell. c. Form the matrix product {I}ω of the inertia tensor and angular velocity for the barbell. Check that you get the same value for L as above. d. Make 3-D plots of L and ω for three different times, and thereby show that L

ω z θ

x

y

Figure 7.1 A barbell rotating about a fixed axis in the z direction. In Maple you can grab and rotate this pointplot3d figure.

146

CHAPTER 7 z

y 1

(-1, 1, 1) d

y

1

x (1, -1, -1) (1, 1, -1)

x

Figure 7.2 Left: A 2-D rectangular plate of side 2 with the origin at its center and a mass at each corner. Right: A cube of side 2 with the origin at its center and a mass at each corner. In Maple you can grab and rotate these pointplot3d figures.

rotates about a fixed ω . A changing L implies that there is an external torque being applied to the barbell. Problem b: Consider the 2-D square plate on the left of Figure 7.2 with a mass m at each corner. It is rotated with these three values for the angular velocity: 

ω=

1 0





,

0 1





,

1 1



.

1. Use Maple to determine the angular momentum vector L of the plate by adding up the angular momenta of the individual masses. 2. Use Maple to evaluate the inertia tensor {I} for this plate. Hint: if this were a solid plate, its inertia tensor would be 

I=

1 0 0 4



.

(7.1)

3. Form the matrix product {I}ω of the inertia tensor {I} and angular velocity ω for the plate, and check that you get the same value for L as before. 4. Make three 3-D plots of L and ω for different times, and thereby show that L rotates about a fixed ω . From this we deduce that there must be an external torque applied to the plate. 5. Determine the torque acting on the plate. (optional) Problem c: Consider the 3-D cube on the right of Figure 7.2 with a mass m at each corner. It is rotated with these three values for the angular velocity: 

ω=

1 0





,

0 1





,

1 1



.

1. Use Maple to determine the vector angular momentum L of the cube by adding up the angular momenta of the individual masses.

147

MATRICES AND VECTORS; RIGID-BODY ROTATION

2. Use Maple to evaluate the inertia tensor {I} for this cube. Hint: if this were a solid cube, its inertia tensor would be ⎡

+ 23 ⎢ 1 I = ⎣ −4 − 14

− 14 + 23 − 14



− 14 ⎥ − 14 ⎦ . + 23

3. Form the matrix product of the inertia tensor and angular velocity {I}ω for the cube and check that you get the same value for L as above. 4. Make some 3-D plots of L and ω for different times. Thereby show that L rotates about a fixed ω . From this we deduce that there must be an external torque applied to the cube. 5. Determine the torque acting on the cube. (optional) 7.2 MATH: VECTORS AND MATRICES Classical mechanics is an area of physics that studies the motion of particles and objects. Before we describe the motion of a particle, we must first develop a system to specify the location of a particle in space. As illustrated in Figure 7.3, we describe the location R of a point in space by drawing a set of perpendicular x and y axes, and then drawing a line from the origin O to the point R. An arrow head is usually added to this line segment to show its direction. An arrow in space is the most elementary representation of a vector. We view Figure 7.3 as showing a graphical representation of the displacement vector R. This vector has length or magnitude R = |R|, and a direction specified by giving the angle θ that the vector makes with the x axis. A vector is usually denoted by a bold symbol, such as R, or sometimes by a symbol with an arrow over it. By convention, the magnitude of a vector is denoted by the same symbol used to denote the vector, only without the bold font or arrows. Even though the direction and magnitude of a vector is a good way to visualize a vector, calculations on vectors are usually easier to perform with the individual components of the vector. For our vector R, its components are just its projections along the positive x and y axes: x = R cos(θ),

y = R sin(θ).

(7.2)

Thus, if we know R and θ, it is easy to use these equations to calculate x and y , or if we know x and y , R and θ: 

R=

x2 + y 2 ,

y θ = tan−1 ( ). x

(7.3)

Usually these components are put together inside some kind of parentheses to indicate that they are the components of a single object. Within the text of a document, they may be written in a horizontal row as (x, y), while in an equation they

148

CHAPTER 7

y y

y

R R x

x

z

x

z

Figure 7.3 Left: Representation of a vector as an arrow in 2-D space. Right: Representation of a vector as an arrow in 3-D space. The projection of the arrow along each axis equals the components of the vector. In Maple you can rotate the 3-D figure.

may be grouped as a vertical column:



R=

x y



.

(7.4)

Whether a vector is viewed as an abstract symbol R or as the collection of components, it is the same object being viewed. Multiple representations are a good thing, because sometimes it is easier to perform formal manipulations with abstract symbols rather than individual components, while other times it is easier to perform explicit calculations with the components. We have drawn the vector R as if it were in a plane. If you spent more time away from the computer screen, you might realize that the real world has three dimensions, and we really should be drawing our vectors in a 3-D space with components along the x, y , and z axes. As shown in Figure 7.3, while the trigonometry is more complicated for 3-D vectors than 2-D ones, working with components is much the same, only now R has three components: ⎡



x ⎣ R = y ⎦. z

(7.5)

Other vectors in addition to the displacement vector R occur regularly in science. Examples include velocity, acceleration, force, and electric and magnetic fields (so you may as well start to understand them now if you do not already). Fortunately, the rules for decomposition into components are the same for all vectors, as are the rules for various algebraic manipulations.

149

MATRICES AND VECTORS; RIGID-BODY ROTATION

7.2.1 Vector Operations and Matrices Operating upon vectors that are represented by components is really quite easy, since all you have to do is operate on the individual components using the ordinary rules of algebra. To illustrate, let us say we have vectors A and B represented by their three components, ⎡





Ax A = ⎣ Ay ⎦ , Ay



Bx B = ⎣ By ⎦ . Bz

Addition and subtraction are defined as ⎡





(7.6)



Ax − Bx A − B = ⎣ Ay − By ⎦ . Az − Bz

Ax + Bx A + B = ⎣ Ay + By ⎦ , Az + Bz def

def

(7.7)

Multiplication of vector A by a constant c is defined as ⎡



cAx cA = ⎣ cAy ⎦ . cAz def

(7.8)

Two vectors may multiply each other in two ways. The dot product is the sum of the product of their corresponding components: A · B = Ax Bx + Ay By + Az Bz ,

(7.9)

where the bar over the components of A indicates complex conjugate. In science, the asterisk * is usually used to indicate complex conjugation, yet is often left off, since the vectors are real. Geometrically, the dot product A·B is just the projection of A along the direction of B. In view of the fact that the result is just a scalar (a quantity with no direction), the dot product is also known as the scalar product. The second kind of vector multiplication is the cross product: ⎡



Ay Bz − Az By A × B = ⎣ Az Bx − Ax Bz ⎦ . Ax By − Ay Bx

(7.10)

Geometrically, the cross product A × B is a new vector perpendicular to the plane formed by A and B, with magnitude |A||B|sinθ, where θ is the angle between the vectors A and B. 7.3 THEORY: ANGULAR MOMENTUM DYNAMICS A rigid body is one in which the distance between the particles that compose the body do not change [M&T 88]. We imagine such an object rotating about a fixed point O (or about the center of mass of the body). In spite of it being true that each

150

CHAPTER 7

part of a rotating body has a different velocity, all parts have the same angular velocity ω . The velocity of part i is related to ω via: vi = ω × ri ,

(7.11)

where ri is a vector from the origin O to part i, and × represents the vector cross product. Angular momentum is introduced into mechanics as the rotational analog of linear momentum. It measures the tendency of a rotating object to remain rotating, and has a direction associated with it. For the highly symmetric cases usually studied in elementary physics, the angular momentum and angular velocity vectors are parallel to each other, L = Iω, (7.12) with the proportionality constant I called the moment of inertia. However, when an object rotates about an axis that is not one of high symmetry, then L and ω will not be parallel to each other, and a generalization of this equation is needed. The general relation between the angular momentum and the angular velocity follows from expressing the angular momentum of a group of particles as the sum of the angular momentum of each particle: L=



ri × pi ,

(7.13)

i

where pi = mi vi is the linear momentum of particle i. If we restrict ourselves to the rotation of a rigid body about a fixed point, then the velocity is related simply to the angular velocity, and we have L=



mi ri × (ω × ri ),

(7.14)

i

where the right-most vector cross product must be performed first. We now skip over the details of the derivation and give you the final result. For general problems, such as rotation of the cube in Figure 7.3 about axes along a side of the cube, the angular velocity ω and angular momentum vector L are related by what Maple calls the dot product of vector ω with the inertia tensor {I}: L = {I} ω.

(7.15)

Observe how in the above equation we have placed braces around the symbol I. The braces indicate a type of mathematical object, a tensor, that transforms one vector into another vector. If the vectors are represented by columns (the usual thing), then we represent the action of a tensor on a vector by the multiplication of that vector by a matrix: ⎡





⎤⎡



Lx Ixx Ixy Ixz ωx ⎣ Ly ⎦ = ⎣ Iyx Iyy Iyz ⎦ ⎣ ωy ⎦ . Lz Izx Izy Izz ωz

(7.16)

151

MATRICES AND VECTORS; RIGID-BODY ROTATION

We see by looking at this equation, and by applying the rules of matrix algebra, that this one matrix equation is an elegant way of presenting the three simultaneous linear equations relating the individual angular velocity and angular momentum components to each other: Lx = Ixx ωx + Ixy ωy + Ixz ωz , Ly = Iyx ωx + Iyy ωy + Iyz ωz , Lz = Izx ωx + Izy ωy + Izz ωz .

(7.17) (7.18) (7.19)

It is, of course, even easier to write this as one matrix equation L = ω . In addition, it is much easier to manipulate the matrix equation than the individual equations. The diagonal elements Ixx , Iyy , and Izz of the inertia tensor are called the moments of inertia about the x, y , and z axes, respectively. They are the same as those used in elementary physics. The off-diagonal elements, such as Ixy = Iyx , are negatives of what are called products of inertia, and they are responsible for making ω and L nonparallel. An explicit expression for the elements of I is [M&T 88]



Ii,j =



ρ(v) δij (

x2k ) − xi xj dv

(7.20)

k

for continuous mass, and Ii,j =







ma δij (

a

x2a,k )

− xa,i xa,j

(7.21)

k

for discrete masses. Here the symbol δij is the Kronecker delta function, 

δi,j =

1, 0,

if i = j otherwise.

(7.22)

7.4 MAPLE: LINEAR ALGEBRA TOOLS In computing and science, confusion often arises from speaking of arrays and matrices as much the same thing. An array refers to the bookkeeping device used to store items by placing them in designated rows and columns. A matrix also has its elements stored in rows and columns, but it has well-defined mathematical properties. We shall speak more about this in Chapter 17, where we discuss these objects within the Java framework of programming with a compiled language. Now we shall look at some of the facilities Maple has for handling matrices and their algebraic manipulations. The more elementary tone of the Java chapter on matrices may be more accessible to beginners than this chapter, where Maple’s mathematical sophistication requires a higher-level understanding of linear algebra. In mathematics, the row or column is usually given by a subscript, with the row and column numbers starting from 1. As a case in point, Mi,j for an element

152

CHAPTER 7

in row i and column j , with M1,1 the first element of the matrix. In computer science, the rows and columns are indicted by indices, for example, M[i][j], with M[0][0] the first element. There is essentially no limit to the number of indices an array may have, yet the maximum value that an index may have is limited by the finite amount of computer memory available.1 A vector in Maple is represented as a one-dimensional array V[i] with index i starting from 1. A matrix in Maple is represented as a two-dimensional array M[i,j] with row and column indices i and j starting from 1. The branch of mathematics called linear algebra deals with matrices. Maple has two packages for linear algebra. The older one, linalg, has been superseded by LinearAlgebra. The commands are similar, with those in LinearAlgebra capitalized, and those in linalg in lowercase. In an appendix we give a table showing the equivalent commands in each package. Loading either package shows the commands available: > with(LinearAlgebra);

[Add, Adjoint, BackwardSubstitute, BandMatrix, Basis, BezoutMatrix, BidiagonalForm, BilinearForm, CharacteristicMatrix, CharacteristicPolynomial, Column, ColumnDimension, ColumnOperation, ColumnSpace, CompanionMatrix, ConditionNumber, ConstantMatrix, ConstantVector, CreatePermutation, CrossProduct, DeleteColumn, DeleteRow, Determinant, DiagonalMatrix, Dimension, Dimensions, DotProduct, EigenConditionNumbers, Eigenvalues, Eigenvectors, Equal, ForwardSubstitute, FrobeniusForm, GaussianElimination, GenerateEquations, GenerateMatrix, GetResultDataType, GetResultShape, GivensRotationMatrix, GramSchmidt, HankelMatrix, HermiteForm, HermitianTranspose, HessenbergForm, HilbertMatrix, HouseholderMatrix, IdentityMatrix, IntersectionBasis, IsDefinite, IsOrthogonal, IsSimilar, IsUnitary, JordanBlockMatrix, JordanForm, LA Main, LUDecomposition, LeastSquares, LinearSolve, Map, Map2, MatrixAdd, MatrixInverse, MatrixMatrixMultiply, MatrixNorm, MatrixScalarMultiply, MatrixVectorMultiply, MinimalPolynomial, Minor, Modular, Multiply, NoUserValue, Norm, Normalize, NullSpace, OuterProductMatrix, Permanent, Pivot, PopovForm, QRDecomposition, RandomMatrix, RandomVector, Rank, ReducedRowEchelonForm, Row, RowDimension, RowOperation, RowSpace, ScalarMatrix, ScalarMultiply, ScalarVector, SchurForm, SingularValues, SmithForm, SubMatrix, SubVector, SumBasis, SylvesterMatrix, ToeplitzMatrix, Trace, Transpose, TridiagonalForm, UnitVector, VandermondeMatrix, VectorAdd, VectorAngle, VectorMatrixMultiply, VectorNorm, VectorScalarMultiply, ZeroMatrix, ZeroVector, Zip] 1 At times the largest value an index assumes is called the dimension of the array, while at other times the number of indices is called the dimension of the array. So, be careful! We call the number of indices the dimension.

153

MATRICES AND VECTORS; RIGID-BODY ROTATION

(The warnings that appear after loading a package do not indicate that anything is wrong. They just indicate that these packages redefine some of the meaning of existing mathematical operations, like norm.) Notwithstanding our preference for the LinearAlgebra package, there are some commands available only in linalg. When you run into that, you may run a linalg command within LinearAlgebra.

7.4.1 Defining Matrices and Vectors Matrices are input as two-dimensional arrays or with the Matrix command: > with(LinearAlgebra): > I plate := Matrix( 2, 2, [ [1,0], [0, 4] ] );



I plate :=

1 0 0 4

# Define 2 x 2 matrix & elements



> I sphere := Matrix( 3, 3, [[Ixx,Ixy,Ixz], [Iyx,Iyy,Iyz], [Izx,Izy,Izz] ] );





Ixx Ixy Ixz I sphere := ⎣ Iyx Iyy Iyz ⎦ Izx Izy Izz # Look at individual element

> ‘I sphere[2, 3]‘ = I sphere[2, 3];

I sphere[2, 3] = Iyz # Matrix elements = function (i,j)

> A := Matrix( 3, 3, (i, j) -> i*jˆ2 );





1 4 9 A := ⎣ 2 8 18 ⎦ 3 12 27 > ‘A[3, 2]‘ = A[3, 2];

# Look at individual element

A[3, 2] = 12

> B := Array( 1..3, 1..3, [ [-2,2,-3], [2,1,-6], [-1,-2,0] ] );





−2 2 −3 1 −6 ⎦ B := ⎣ 2 −1 −2 0 # The identity matrix of specified dimension

> ID := IdentityMatrix(6);

⎡ ⎢ ⎢ ⎢ ⎢ ID := ⎢ ⎢ ⎢ ⎣

1 0 0 0 0 0

0 1 0 0 0 0

0 0 1 0 0 0

0 0 0 1 0 0

0 0 0 0 1 0

0 0 0 0 0 1

⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦

154

CHAPTER 7

You see that the [i,j] elements of the matrices are assigned and accessed using the subscript notation A[i,j], just as the elements of a vector are accessed as V[i] or V[j]. The matrix I sphere is assigned with explicit elements; the matrix A is assigned via a functional form for the [i,j] element; the matrix B is assigned as a list of lists (recall order matters in lists), with each sublist being a row of the matrix, and the major list being a list of rows. The identity matrix ID is created just by giving its size. Now that we have created matrices row by row using list of lists, we show how to create them column by column using angle brackets < and > with vertical bars as separators: > A := < | | >;



A :=

a b c d e f

Separated symbols



> C := Matrix ( < | | > );



1 ⎢ 2 C := ⎢ ⎣ 3 4



5 9 6 10 ⎥ ⎥ 7 11 ⎦ 8 12

Once you have a matrix with numerical elements, it is easy to visualize it via Maple’s matrixplot command: > with(plots): > matrixplot(ID, axes = boxed, title =‘ Identity Matrix‘);

Identity Matrix

1 0.8 0.6 0.4 0.2 0 1

C Matrix

2

3 column

4

5

6

6

# Left plot

5

4

3 row

2

1

12 10 8 6 4 2 1 1.5 column

2 2.5 3

> matrixplot( C, axes = boxed, title = ‘C Matrix‘ );

4

3.5

3

2 2.5 row

1.5

1

# Right plot

Next we use the type function to see if some of the objects we have created are arrays or matrices. We shall see that while an array can be set up to also be a matrix, if the array uses indices that begin with 0, then it cannot be a matrix: > type(M, Matrix);

# Is M a Matrix, Array?

type(M, Array);

true

f alse

155

MATRICES AND VECTORS; RIGID-BODY ROTATION

7.4.2 Column and Row Vectors In physics we usually deal with vectors that represent objects having a magnitude and direction in space. We specify the velocity vector by giving its three components: v = (vx , vy , vz ). (7.23) We do this in Maple with the Vector command: > restart: with(LinearAlgebra): > V := Vector([Vx, Vy, Vz]);

# Enter column vector; displayed as row





Vx V := ⎣ V y ⎦ Vz > r := Vector([x,y,z]);





# Another vector

x r := ⎣ y ⎦ z

> V[1];

# First element of vector =?

Vx

The Vector command manifestly creates a column vector. We may also create a row vector as a matrix with one row. That is, a row vector is the same as a 1 × N matrix. Likewise, a column vector is the same as an N × 1 matrix. Remember, since explicit input of matrix elements are done row by row, a column matrix requires a list of lists, with each sublist containing the one item for that row: ⎡



> U col M := Matrix( [ [10],[20],[30] ] );

# Column vector via list of lists

10 U col M := ⎣ 20 ⎦ 30 # Look at element in matrix, OK

> U col M[2,1];

20 > U col M := Matrix( );





# Column vector via angle brackets

10 U col M := ⎣ 20 ⎦ 30 # Look at element in matrix; it is still OK

> ‘U col M[2, 1]‘ = U col M[2, 1];

U col M [2, 1] = 20 > U col := ;





10 U col := ⎣ 20 ⎦ 30

# Create column vector, not matrix

156

CHAPTER 7 # Look at element in matrix, not OK

> U col[2, 1]; Error, number of indices exceeds rank

.

# Look at element in vector, OK as not matrix.

> ‘U col[2]‘ = U col[2];

U col[2] = 20 # Create row in matrix

> U row M := Matrix( [a, b, c] );

U row M :=



a b c

 # Look at individual elements

> ‘U row M[1, 3]‘ = U row M[1, 3];

U row M [1, 3] = c # Look at element in vector; not OK for matrix.

> U row M[3]; Error, Matrix index out of range

7.4.3 Arrays Arrays in Maple are tables of data or variables with arbitrary numbers of subscripts. Matrices, in contrast, are restricted to just two indices and are mathematical objects. We can define an array in Maple with the array command in which we give explicit ranges for each index. However, for intermixing arrays with matrices from the LinearAlgebra package, we want Maple to implement the arrays as rtables, and so we use the Array command: > restart: with(LinearAlgebra): > A 1D := Array( 1..4 ); > Array( 1..2, 1..2, [ [1, 2], [3, 4] ] ); > A 3D := Array( 1..4, 1..3, 1..2 ); > type(A 1D, Array);

type(a 1D, Array);

> type(A 1D, array);

type(A 3D, Array);

A1 D := [0, 0, 0, 0]   1 2 3 4 ⎡

# Created as rtable (note commas in output) # Create Array and assign elements # Create 3-D Array, hard to print out



1 . . . 4 × 1 . . . 3 × 1 . . . 2 3 − D Array ⎢ ⎥ DataT ype : anything ⎥ A3 D := ⎢ ⎣ ⎦ Storage : rectangular Order : F ortran order true f alse f alse true

We see that these commands create the 1D array A 1D whose index can vary from 1 to 4, and the 3D array A 3D whose first index varies from 1 to 4, whose second index varies from 1 to 3, and whose third index varies from 1 to 2. These array commands created the two array structures, but have not put any values in them. We assign values explicitly, or with some procedures:

157

MATRICES AND VECTORS; RIGID-BODY ROTATION > A 1D [1] := A; > A 3D [3] := Pi;

A 1D [2] := 55; A 3D [2] := 55;

A 1D [3] := Pi;

> A 1D;

A 1D[1] := A A 1D[2] := 55 A 1D[3] := P i A 3D[3] := P i A 3D[2] := 55 [A, 55, π, 0]

7.5 MATRIX ARITHMETIC AND OPERATIONS 7.5.1 Multiplication Matrix multiplication is accomplished with the commands Multiply(A, B)

A.B

MatrixMatrixMultiply(A, B)

evalm(A &* B).

The basic mathematical rule showing the dimensions for matrix multiplication is AM × N = BM × L CL × N

(7.24)

This means that it is legal to multiply the M × L matrix B by the L × N matrix C only if the two inner dimensions are the same. The resulting matrix A will have dimension L × M , that is, the two outer dimensions of B and C: > restart: with(LinearAlgebra): > A := Matrix( 3, 3, (i,j) -> (i*x)ˆj );

# Define matrix A





x x2 x3 2 8 x3 ⎦ A := ⎣ 2 x 4 x 2 3 x 9 x 27 x3 > B := Matrix(3,3,(i,j) -> iˆj);





# Define matrix B

1 1 1 B := ⎣ 2 4 8 ⎦ 3 9 27

> A;

B;

> evalm(A &* B);







x x2 x3 ⎣ 2 x 4 x2 8 x3 ⎦ 3 x 9 x2 27 x3





1 1 1 ⎣ 2 4 8 ⎦ 3 9 27

# Check the matrices

# Matrix multiply of two square matrices



x + 2 x 2 + 3 x3 x + 4 x2 + 9 x3 x + 8 x2 + 27 x3 2 3 2 3 ⎣ 2 x + 8 x + 24 x 2 x + 16 x + 72 x 2 x + 32 x2 + 216 x3 ⎦ 3 x + 18 x2 + 81 x3 3 x + 36 x2 + 243 x3 3 x + 72 x2 + 729 x3 > A . B;

# Multiply two matrices

158

CHAPTER 7





x + 2 x2 + 3 x3 x + 4 x2 + 9 x3 x + 8 x2 + 27 x3 2 3 2 3 ⎣ 2 x + 8 x + 24 x 2 x + 16 x + 72 x 2 x + 32 x2 + 216 x3 ⎦ 2 3 2 3 3 x + 18 x + 81 x 3 x + 36 x + 243 x 3 x + 72 x2 + 729 x3 # Multiply two matrices in reversed order

> B . A;





6x 14 x2 36 x3 ⎣ 34 x 90 x2 250 x3 ⎦ 102 x 282 x2 804 x3 # Yet another command for multiply

> Multiply(A, B);





x + 2 x2 + 3 x3 x + 4 x2 + 9 x3 x + 8 x2 + 27 x3 2 3 2 3 ⎣ 2 x + 8 x + 24 x 2 x + 16 x + 72 x 2 x + 32 x2 + 216 x3 ⎦ 3 x + 18 x2 + 81 x3 3 x + 36 x2 + 243 x3 3 x + 72 x2 + 729 x3 > MatrixMatrixMultiply( A, B );

MatrixMatrixMultiply( B, A );





x + 2 x2 + 3 x3 x + 4 x2 + 9 x3 x + 8 x2 + 27 x3 2 3 2 3 ⎣ 2 x + 8 x + 24 x 2 x + 16 x + 72 x 2 x + 32 x2 + 216 x3 ⎦ 3 x + 18 x2 + 81 x3 3 x + 36 x2 + 243 x3 3 x + 72 x2 + 729 x3 ⎡



6x 14 x2 36 x3 ⎣ 34 x 90 x2 250 x3 ⎦ 102 x 282 x2 804 x3

The different forms of the command to multiply matrix A by B are seen to all give the same result. Yet the result from A × B is not the same as B × A (matrix multiplication is not commutative). The identity or unit matrix is always square and must be defined for a specific dimension. It is also worth checking that when multiplying from either side it yields the same answer: > ID := IdentityMatrix(3);





# Create 3 × 3 identity matrix

1 0 0 ⎣ ID := 0 1 0 ⎦ 0 0 1 > Multiply( A, ID );

# Check with R and L multiplications

ID . A;





x x2 x3 ⎣ 2 x 4 x2 8 x3 ⎦ 3 x 9 x2 27 x3 > evalm( A &* ID );

> Aˆ2; A . A;







x x2 x3 ⎣ 2 x 4 x2 8 x3 ⎦ 3 x 9 x2 27 x3 ⎤

x x2 x3 ⎣ 2 x 4 x2 8 x3 ⎦ 3 x 9 x2 27 x3

# Multiply by identity matrix

# Matrix raised to power

159

MATRICES AND VECTORS; RIGID-BODY ROTATION









x2 + 2 x3 + 3 x4 x3 + 4 x4 + 9 x5 x4 + 8 x5 + 27 x6 2 3 4 3 4 5 ⎣ 2 x + 8 x + 24 x 2 x + 16 x + 72 x 2 x4 + 32 x5 + 216 x6 ⎦ 2 3 4 3 4 5 3 x + 18 x + 81 x 3 x + 36 x + 243 x 3 x4 + 72 x5 + 729 x6 x2 + 2 x3 + 3 x4 x3 + 4 x4 + 9 x5 x4 + 8 x5 + 27 x6 ⎣ 2 x2 + 8 x3 + 24 x4 2 x3 + 16 x4 + 72 x5 2 x4 + 32 x5 + 216 x6 ⎦ 2 3 4 3 x + 18 x + 81 x 3 x3 + 36 x4 + 243 x5 3 x4 + 72 x5 + 729 x6

7.5.1.1 Matrix-Scalar Multiplication Multiplication of a matrix by a scalar is simple if the scalar is numeric: > 3 * A;



# Multiply matrix A by number



3 x 3 x2 3 x3 ⎣ 6 x 12 x2 24 x3 ⎦ 9 x 27 x2 81 x3

However, if the scalar is symbolic, the results may not be what you expect: > c * A;



x2

x3



# Multiply matrix A by symbol

x c ⎣ 2 x 4 x2 8 x3 ⎦ 3 x 9 x2 27 x3

The problem is that Maple does not know what type of an object c is unless you tell it. The most direct way is use the ScalarMultiply command: > ScalarMultiply(A, c);





cx c x2 c x3 2 ⎣ 2cx 4cx 8 c x3 ⎦ 3 c x 9 c x2 27 c x3

Once in a while, like above, Maple appears to be stubborn in granting our wishes. As is often our approach in these cases, we are patient and request help from the simplify command, possibly with symbolic or scalar as an option: > c * A;

# Distribute via simplify

simplify(%);





x x2 x3 2 8 x3 ⎦ c ⎣ 2x 4x 3 x 9 x2 27 x3 > c * A;





cx c x2 c x3 2 ⎣ 2cx 4cx 8 c x3 ⎦ 3 c x 9 c x2 27 c x3

simplify( %, symbolic);





x x2 x3 2 8 x3 ⎦ c ⎣ 2x 4x 3 x 9 x2 27 x3





cx c x2 c x3 2 ⎣ 2cx 4cx 8 c x3 ⎦ 3 c x 9 c x2 27 c x3

160

CHAPTER 7

> c * A;

simplify(%) assuming c::





symbolic;



x x2 x3 c ⎣ 2 x 4 x2 8 x3 ⎦ 3 x 9 x2 27 x3



cx c x2 c x3 ⎣ 2 c x 4 c x2 8 c x3 ⎦ 3 c x 9 c x2 27 c x3

7.5.1.2 Matrix-Vector Multiplication Now we explore the multiplication of vectors by matrices. Remember the rule showing the dimensions for matrix multiplication: AM ×N = BM ×L CL×N .

(7.25)

This means that the number of columns in B must match the number of rows in C. If C is a vector of dimension J, this means that C has J rows and one column (a J × 1 matrix), so L = J and N = 1 in the above equation. As a consequence, the matrix B that multiplies a vector of dimension J must be dimension J × J. The result of matrix-vector multiplication is a matrix of J × 1, that is, another a vector: ucol := Matrix(3, 1, [[a], [b], [c]]); # 2 vectors

> Vec := Vector([a, b, c]);







a V ec := ⎣ b ⎦ c

B := Matrix(3,3, (i,j)-> iˆj); # Row vector

> urow := Matrix(1,3, [d, e, f]);

urow :=

> Multiply( B, Vec );



d e f





a+b+c ⎣ 2a + 4b + 8c ⎦ 3 a + 9 b + 27 c



1 1 1 ⎣ B := 2 4 8 ⎦ 3 9 27



Multiply( B, ucol );





a ucol := ⎣ b ⎦ c



# 3 × 3 matrix times 3 × 1 vector



a+b+c ⎣ 2a + 4b + 8c ⎦ 3 a + 9 b + 27 c

> ’B * urow’ = Multiply(B, urow); # 1 × 3 times 3 × 3 not allowed Error, first matrix column dimension (3) second matrix row dimension (1) # Is 1 × 3 times 3 × 3 allowed?

> ’urow * B ’ = Multiply( urow, B );

B urow =



d + 2e + 3f

d + 4e + 9f

d + 8 e + 27 f



161

MATRICES AND VECTORS; RIGID-BODY ROTATION

7.5.2 Matrix Addition and Subtraction The LinearAlgebra package contains a number of ways to perform addition and subtraction of matrices. The MatrixAdd and VectorAdd methods do the actual work, yet they are called automatically by the more general Add command. In addition, there are the shortcuts + and - that work fine (as long as you remember what is a matrix). Beware: these commands are real smart and will do the “obvious” thing if you try to add together different combinations of vectors, matrices, and scalars; however, you must ensure that what is obvious to Maple is what you want. # Define matrices A, B B := Matrix( 3, 3, (i,j) -> iˆj );

> restart: with(LinearAlgebra): > A := Matrix( 3, 3, (i,j) -> (i*x)ˆj );







x x2 x3 2 ⎣ 2x 4x 8 x3 ⎦ A := 2 3 x 9 x 27 x3 > Vec1 := Vector( [a, b, c] );

Vec2 := Vector([1,2,3]);







a V ec1 := ⎣ b ⎦ c > A + B;

1 V ec2 := ⎣ 2 ⎦ 3





> Add( A, B );





x+1 x2 + 1 x3 + 1 ⎣ 2 x + 2 4 x2 + 4 8 x3 + 8 ⎦ 2 3 x + 3 9 x + 9 27 x3 + 27 > Add( Vec1, Vec2 );





x+1 x2 + 1 x3 + 1 ⎣ 2 x + 2 4 x2 + 4 8 x3 + 8 ⎦ 2 3 x + 3 9 x + 9 27 x3 + 27

Vec1 + Vec2;





A + 2;



# Three adds



x+2 x2 x3 ⎣ 2x ⎦ 4 x2 + 2 8 x3 2 3 3x 9x 27 x + 2

a+1 ⎣ b+2 ⎦ c+3

> ID := IdentityMatrix(3);



# Two adds

MatrixAdd( A, B );



a+1 ⎣ b+2 ⎦ c+3



x−1 x2 − 1 x3 − 1 ⎣ 2 x − 2 4 x2 − 4 8 x3 − 8 ⎦ 2 3 x − 3 9 x − 9 27 x3 − 27

x+1 x2 + 1 x3 + 1 ⎣ 2 x + 2 4 x2 + 4 8 x3 + 8 ⎦ 2 3 x + 3 9 x + 9 27 x3 + 27

> A+ 2*ID;

# Two 3-D vectors



A - B;







1 1 1 B := ⎣ 2 4 8 ⎦ 3 9 27





1 0 0 ID := ⎣ 0 1 0 ⎦ 0 0 1 # ID implied in 2nd

C := Add(A,-lambda);



x+2 x2 x3 2 ⎣ 2x ⎦ 4x + 2 8 x3 2 3 3x 9x 27 x + 2





x−λ x2 x3 2 ⎦ 8 x3 C := ⎣ 2 x 4 x − λ 2 3 3x 9x 27 x − λ

162

CHAPTER 7 # This not permitted

> Add(Vec1,1); Error, (in LinearAlgebra:-Add) invalid arguments > 2*A + 3*B;

# Scalar mult + add, 4 vec1 + vec2

Add(Vec1, Vec2, 4, 1);







2 x + 3 2 x2 + 3 2 x3 + 3 2 ⎣ 4 x + 6 8 x + 12 16 x3 + 24 ⎦ 6 x + 9 18 x2 + 27 54 x3 + 81



4a + 1 ⎣ 4b + 2 ⎦ 4c + 3

7.5.3 Other Matrix Operations Now we define a 3 × 3 and a 4 × 4 matrix. The matrix A has three rows, each proportional to the others, and the matrix ID is the 4-D identity matrix: > restart: with(LinearAlgebra): > A := Matrix([ [1,2,3], [2,4,6], [3,6,9] ]);



ID := IdentityMatrix(4);





1 ⎢ 0 ID := ⎢ ⎣ 0 0

1 2 3 A := ⎣ 2 4 6 ⎦ 3 6 9

0 1 0 0

0 0 1 0



0 0 ⎥ ⎥ 0 ⎦ 1

Before we go ahead and evaluate the determinant of each, we observe that the rows in A are not independent, and so its determinant should vanish, while the determinant of a diagonal matrix, such as the identity matrix, is just the product of the diagonal elements: > Determinant(A);

# Determinant vanishes if rows proportional

Determinant(ID);

0

1

> B := Matrix(3,3, (i,j)-> iˆj);

Determinant(B);



1 1 1 B := ⎣ 2 4 8 ⎦ 3 9 27

12 # Inverse exist if nonzero determinant

> Binv := 1/B;

⎡ ⎢ 3

⎢ ⎢ ⎢ −5 Binv := ⎢ ⎢ 2 ⎢ ⎣ 1

2 > B . Binv;

# B has independent rows



Binv . B;

−3 2 2 −1 2

1 3 −1 2 1 6

⎤ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎦ # B times its inverse, inverse times B

163

MATRICES AND VECTORS; RIGID-BODY ROTATION







1 0 0 ⎣ 0 1 0 ⎦ 0 0 1 > Transpose(A);

Trace(A);



1 0 0 ⎣ 0 1 0 ⎦ 0 0 1 Trace(ID);



# Row–column interchange, sum diagonals



1 2 3 ⎣ 2 4 6 ⎦ 3 6 9

14

4

> A := Matrix([[a,b,c], [aˆ2,bˆ2,cˆ4], [a,b,cˆ4]]);







a b c A := ⎣ a2 b2 c4 ⎦ a b c4 > A.Ainv;



Ainv := ⎢ ⎣

−c

3

(b−1) a 3 c (a−1) b 1 − (c3 −1) c

Ainv := 1/A; 1 (a−b) a 1 − (a−b) b

0

# Symbolic

− c a−b 3

⎤ ⎥

3 − −c b+a ⎥ ⎦

1 (c3 −1) c

# A mess, simplified

Map(simplify, %);





1 0 0 ⎣ 0 1 0 ⎦ 0 0 1

As we have just seen, the Map command applies a function to each element of its second argument and places the result back into the matrix. If the first argument is a matrix, then each element of the matrix has the function applied to it. If the function takes an argument of its own, that argument may be given as a third argument: > restart: with(LinearAlgebra): > Map(f, x + y*z);

# Use map on ordinary function

f(x) + f(y z) # Define matrix A again

> A := Matrix(3,3, (i,j)-> (i*x)ˆj);





x x2 x3 2 8 x3 ⎦ A := ⎣ 2 x 4 x 3 x 9 x2 27 x3 > Map(f,A);

# Apply f to each element in A, check

A;







f(x) f(x2 ) f(x3 ) 2 ⎣ f(2 x) f(4 x ) f(8 x3 ) ⎦ f(3 x) f(9 x2 ) f(27 x3 )

> A:= Matrix(3,3, (i,j) -> (sin(i*x))ˆj);

A :=

sin(x) sin(2 x) sin(3 x)

sin(x)2 sin(2 x)2 sin(3 x)2

sin(x)3 sin(2 x)3 sin(3 x)3



f(x) f(x2 ) f(x3 ) 2 ⎣ f(2 x) f(4 x ) f(8 x3 ) ⎦ f(3 x) f(9 x2 ) f(27 x3 )

sin(x) sin(2 x) sin(3 x)

Map(simplify,A); # Define, simplify 1 − cos(x)2 1 − cos(2 x)2 1 − cos(3 x)2

−sin(x) (−1 + cos(x)2 ) −sin(2 x) (−1 + cos(2 x)2 ) −sin(3 x) (−1 + cos(3 x)2 )

164

CHAPTER 7

> A:= Matrix(3,3, (i,j) -> (i*x)ˆj);





x x2 x3 A := ⎣ 2 x 4 x2 8 x3 ⎦ 3 x 9 x2 27 x3

Map(diff,A,x);



# Define, take derivative



1 2 x 3 x2 ⎣ 2 8 x 24 x2 ⎦ 3 18 x 81 x2

7.5.4 Vector Operations Not only does Maple deal with matrices, but it does vector operations, including calculus. We start by setting up a matrix and some vectors with which to experiment. We intermix symbolic and numeric objects: > with(LinearAlgebra):

A := Matrix([ [1,2,3], [2,4,6], [3,6,9] ]);





1 2 3 ⎣ A := 2 4 6 ⎦ 3 6 9 > ucol := ;

# Column and row vectors

urow := [a, b, c];





10 ucol := ⎣ 20 ⎦ 30

urow := [a, b, c] # A Maple vector

> vec := Vector( [x, y*xˆ2, sin(x)] );





x vec := ⎣ y x2 ⎦ sin(x)

As we discussed in §7.2 with the mathematics, there are a number of ways in which one matrix or vector gets multiplied by another. We define some vectors and look at a few ways here: > vec2 := Vector([1,2,3]);

vec3 := ;





1 vec2 := ⎣ 2 ⎦ 3 > dot := DotProduct( vec, vec2 );



vec3 := Vector([4, 5, 6]);



4 vec3 := ⎣ 5 ⎦ 6 # Maple dot product (NB complex conjugation)

dot := x + 2 (y x2 ) + 3 sin(x)

As you may recall, the dot product of vectors A and B equals the length of each

165

MATRICES AND VECTORS; RIGID-BODY ROTATION

vector multiplied by the cosine of the angle between them: A · B = |A||B|cos(θ) = ABcos(θ),

(7.26)

where A = |A| is the length (or magnitude or norm, or modulus) of vector A. To check if this relation holds, we will need to determine the length of a vector. Maple has the VectorNorm command. However, there are various norms with which mathematicians deal, and what we call the length of a vector is what Maple calls the 2-norm: 

VectorNorm(V, 2)

=

V x 2 + Vy 2 + Vz 2 .

(7.27)

An alternative is to take the dot product of a vector with itself and then take the square root. First we will determine the magnitude of the two vectors that we dotted into each other, and then check that the deduced value of cos(θ) is less than 1 in magnitude. For convenience, we define subscript variables L2 and L3 for the lengths: > L[2] := VectorNorm(vec2, 2);

L2 :=

L[2] := sqrt(vec2 . vec2);



14

> L[3] := VectorNorm(vec3, 2);

L3 :=

L2 :=



14

L[3] := sqrt(vec3 . vec3);



77

L3 :=



# Two ways

# Two ways

77

> cos theta := DotProduct(vec2, vec3) / L[2] / L[3];

√ √ 16 14 77 cos theta := 539

# Cosine angle

# Check that floating-point value < 1

> evalf(%);

0.9746318461

While we are talking about the magnitude of vectors, it is often useful to deal with vectors that have been normalized to unit length (“unit vectors”). This is done with Maple’s Normalize command: > NormVec := Normalize( < 1, 2, 3 >, Euclidean); ⎡ √ ⎤ 14 ⎢ 14 ⎥ ⎢ √ ⎥ ⎢ ⎥ N ormV ec := ⎢ 714 ⎥ ⎢ ⎥ ⎣ √ ⎦ 3 14 14 > DotProduct( NormVec, NormVec );

1

# Normalize to norm 1

# Check if normalized (= 1)

166

CHAPTER 7

Now we define two symbolic vectors and take their cross product to check that it agrees with the definition of cross product. After that, we form the cross product of our two numeric vectors, vec2 and vec3, and check that it is perpendicular to both of them (and thus to their plane): > A := Vector([Ax,Ay,Az]);

# 2 vectors

B := Vector([Bx,By,Bz]);



Ax





A := ⎣ Ay ⎦

Bx



B := ⎣ By ⎦

Az

Bz # General form of cross product

> CrossProduct( A, B );



Ay Bz − Az By



⎣ Az Bx − Ax Bz ⎦

Ax By − Ay Bx

# Numeric cross product of vec2 with vec3

> Vcross := CrossProduct( vec2, vec3 );





−3 V cross := ⎣ 6 ⎦ −3 > Vcross . vec2;

# Should get 0 if vectors are orthogonal

Vcross . vec3;

0

0

We also know that the cross product has a length equal to the product of the lengths of vec2 and vec3 times the sine of the angle between them. So let us compute sin(θ) and see if it agrees with the cos(θ) we computed from the dot product: > sin theta := VectorNorm(Vcross, 2)/L[2]/L[3];

√ √ √ 3 6 14 77 sin theta := 1078

> cos thetaˆ2 + sin thetaˆ2;

1

evalf(%);

# Exact or useful

0.4438141291 # Check via trig identity

7.5.5 Vector Calculus* Vector calculus may appear a little advanced for beginners, but you will have it here when needed. Because the LinearAlgebra package does not have all of the vector calculus commands of the linalg package, we sometimes must intermix the two packages. To illustrate, invoking Curl just returns its name, which means that the LinearAlgebra package does not know what it means. Hence, we use the linalg[curl] from the linalg package (with the derivatives taken with respect to the second argument): > vec := Vector([ x, y * xˆ2, sin(x)] );

167

MATRICES AND VECTORS; RIGID-BODY ROTATION





x ⎣ y x2 ⎦ vec := sin(x) # No exists, use linalg instead

> Curl(vec, [x,x,x]);

⎛⎡





x Curl ⎝⎣ y x2 ⎦ , [x, x, x]⎠ sin(x) > linalg[curl] (vec, [x,x,x]);

[cos(x) − 2 y x, 1 − cos(x), 2 y x − 1] > f := vector([xˆ2, yˆ3, zˆ4]): > linalg[diverge](f, v);

Exists

linalg[curl] (vec, [y,y,y]);

[−x2 , 0, x2 ]

v := vector([x, y, z]):

linalg[grad](xˆ3,[x,y,x]);

2 x + 3 y2 + 4 z3

# Two vectors # Operate on them

[3 x2 , 0, 3 x2 ]

7.5.6 Eigenvalues and Eigenvectors* The command Eigenvectors solves the eigenvalue problem for matrix A: [A]X = λX.

(7.28)

That is, Eigenvectors searches for a set of eigenvectors [X] and corresponding eigenvalues [λ] for which (7.28) holds. The eigenvectors are numbered by the index i, where 1 ≤ i ≤ D, (7.29) with D the dimension of the space spanned by the eigenvectors. As we shall see, the eigenvectors of A are returned as a matrix with vectors as columns, and so some manipulation is required to extract individual eigenvectors: >

# Enter A’s rows

A := Matrix([ [-2,2,-3], [2,1,-6], [-1,-2,0] ]);





−2 2 −3 1 −6 ⎦ A := ⎣ 2 −1 −2 0 # Find eigenvector and eigenvalues of A

> Eigenvectors( A );





−3 ⎣ −3 ⎦ , 5





−2 3 −1 ⎣ 1 0 −2 ⎦ 0 1 1

> (lambda, VecMatrix) := Eigenvectors(A);

# Place eigenvector in vecs, values in λ

168

CHAPTER 7







−3 λ, V ecM atrix := ⎣ −3 ⎦ , 5 > lambda[1];

lambda[2];



3 −2 1 ⎣ 0 1 2 ⎦ 1 0 −1 # Three eigenvalues

lambda[3];

−3

−3

5

Vec2 := Column(VecMatrix, 2); # Two eigenvectors

> Vec1 := Column(VecMatrix, 1);









−2 V ec2 := ⎣ 1 ⎦ 0

3 V ec1 := ⎣ 0 ⎦ 1

# Test first eigenvalue with first eigenvector

> A . Vec1 = lambda[1] . Vec1;

⎡ ⎣







−9 −9 0 ⎦=⎣ 0 ⎦ −3 −3 # You test 2nd & 3rd eigenvalues here!

>

This particular eigenvalue problem is a degenerate situation in which two of the eigenvalues are equal, and so we do not expect the degenerate eigenvectors to be orthogonal. (Because the matrix A is not Hermitian, we have no guarantee of orthogonality). We will check if they are orthogonal, and then use the GramSchmidt orthogonalization procedure to make them orthogonal: # Equals 0 if orthogonal

> DotProduct(Vec1,Vec2);

−6 > NewVecs := GramSchmidt([Vec1, Vec2, Vec3]);

⎡ ⎡ −1 ⎤ ⎡ ⎢ 3 5 ⎢ ⎢ ⎢ ⎢ ⎦ ⎣ N ewV ecs := ⎢ 0 , ⎣ 1 ⎣ 1 3

⎤ ⎥ ⎥, ⎦



4 7

⎢ ⎢ ⎢ ⎢ ⎣

8 7 −12 7

5

> DotProduct(NewVecs[1], NewVecs[2]);

# Extract 2

NewVecs[2];

⎤⎤ ⎥⎥ ⎥⎥ ⎥⎥ ⎥⎥ ⎦⎦

⎡ −1 5 ⎢ ⎢ ⎣ 1

⎤ ⎥ ⎥ ⎦

3 5 # Test if new vectors are orthogonal

0

These same matrix calculations are possible with symbolic matrix elements, although this usually take more time, as the matrices get bigger. As we see below, even with matrices, we sometimes have to coerce Maple into distributing the multiplication and then expanding the products so the answer looks the way we want it to: > restart: with(LinearAlgebra): > A := Matrix([ [x, -3], [3*x, 4*x] ]);



A :=

x −3 3x 4x



# Algebraic matrix A

169

MATRICES AND VECTORS; RIGID-BODY ROTATION

# Store eigenvalues in vector

> lambda := Eigenvalues(A);

⎡ ⎢

λ := ⎣

5x 2

+

3

5x 2



3

√ √



x2 −4 x 2

⎥ ⎦

x2 −4 x 2

# Store both eigenvalues and eigenvectors

> (lambda, Xs) := Eigenvectors(A);

⎡ ⎢

λ, Xs := ⎣

5x 2

+ −

5x 2

3 3

√ √

x2 −4 x 2 x2 −4 x 2

> X1 := Column (Xs,1);

⎡ ⎢ −

3x 2



⎡ ⎢ − ⎣

⎥ ⎦,

3x 2



3



x2 −4 x 2



3x

3



x2 −4 x 2

3x

X1 := ⎣



⎢ −

X2 := ⎣

3x 2

+

3



5x 2

3x

⎢ ⎣

⎤ ⎥ ⎦

# Test first eigenvalue with distributed mult

x2 −4 x − 2 √ 3 x2 −4 x 2





3

− ⎥ ⎢ ⎦=⎢ ⎣

( 52x +

3



√ 3 x2 −4 x ) ( 32x − ) 2 3x √ 3 x2 −4 x 2

x2 −4 x 2

5x 2

+

⎤ ⎥ ⎥ ⎦

# Expand each element as check

> Map(expand, %);



⎥ ⎦

# Test first eigenvalue



+



1

> A . X1 = Multiply(lambda[1], X1);

⎢ ⎣

x2 −4 x 2

3x

x2 −4 x 2

> A . X1 = lambda[1] . X1;

− x2 +



# Extract two eigenvectors



⎥ ⎦

3

1

1



+

1 X2 := Column (Xs,2);



3x 2

− x2 + 5x 2





+

x2 −4 x − 2 √ 3 x2 −4 x 2

3



⎥ ⎢ ⎦=⎣

− x2 + 5x 2





+

x2 −4 x − 2 √ 3 x2 −4 x 2

3

⎥ ⎦

As is true for other Maple calculations, if any one of the numbers in matrix A contains a decimal point, then Maple will interpret this as a request for a floatingpoint calculation. For matrix calculations, using floats means that the eigenvectors and eigenvalues will be computed numerically and that all elements should be of numeric type. If the calculation is really big, then we recommend a compiled language with a scientific matrix library (we do that in Chapter 17). Here is an example using a numerical form of the previous matrix: > A := Matrix( 10, 10, (i, j)-> (0.1*i )ˆj); > Eigenvalues(A); Eigenvectors(A); >

# Check precision of solutions via substitution

170

CHAPTER 7

7.5.7 Solution of Linear Equations* A set of simultaneous linear equations may be written in the matrix form AX = b.

(7.30)

If we have n equations for n unknowns, then A will be a square matrix of dimension n, b a vector of constants with length n, and X a vector containing the n unknowns we wish to solve for: ⎡



x1 ⎢ x2 ⎥ ⎥ X=⎢ ⎣ x3 ⎦ . xn

(7.31)

The command LinearSolve(A, b) finds the vector X that solves this equation: > A := Matrix([ [4,-2,1], [3,6,-4], [2,1,8]]);





b := Vector([12, -25,32]);



4 −2 1 6 −4 ⎦ A := ⎣ 3 2 1 8



12 b := ⎣ −25 ⎦ 32 # Solve AX = b for unknown vector X

> X := LinearSolve (A,b);





1 X := ⎣ −2 ⎦ 4 # Check your answer here

>

If you are solving a problem in which you have n equations in m unknowns, then A will have n rows and m columns. For a solution to exist, this situation requires b to be a vector of dimension n and X a vector of dimension m. If AX = b has no solution, or if Maple cannot find one, then the null sequence NULL is returned. If AX = b has multiple solutions, then Maple will describe the family of solutions parametrically: > A := Matrix([[1,2],[1,3],[1,2]]);



b := ;





1 2 A := ⎣ 1 3 ⎦ 1 2 X := >



1 b := ⎣ −2 ⎦ 1 

> X := LinearSolve(A, b);

# A = 3 × 2, B = 3 × 1

7 −3



# Solve

# Check your answer here

171

MATRICES AND VECTORS; RIGID-BODY ROTATION

Omega in black, L in colors

2 1.5 z

1

0 0.5

0.5 1 x

0 y

1.5 0

2

Figure 7.4 The angular velocity vector ω in black and the angular momentum vector L in gray. In Maple you can grab and rotate this figure.

7.6 SOLUTION: ROTATING RIGID BODIES We now apply the Maple linear algebra tools to the barbell problem of Figure 7.1. Seeing that the barbell is rotating means that the geometry of this figure is correct only for one instant of time, or for all times in a reference frame rotating with the barbell. We will use that rotating frame and then transform it to a stationary frame later. We are told that the angular velocity vector of the barbell ω lies along the z axis, as shown in Figure 7.4. This means that the barbell is rotating about the z axis. Clearly, then, this is a 3-D problem, so we define ω as a 3-D vector Ω with only a z component: > restart:with(LinearAlgebra):

Omega := Vector([0,0,omega]);





0 Ω := ⎣ 0 ⎦ ω

We want to use Maple to evaluate the angular momentum via (7.15), which for a barbell with two masses has the simple form: L = r1 × p1 + r2 × p2 = m1 r1 × ω × r1 + m2 r2 × ω × r2 .

(7.32)

We first enter the vectors using the trigonometry shown in Figure 7.3: > r1 := Vector([-a*sin(theta),0,a*cos(theta)]);



r1 :=

−a sin(θ) 0 a cos(θ)

r2 := Vector([a*sin(theta),0,-a*cos(theta)]);

r2 :=



a sin(θ) 0 −a cos(θ)

This makes sense, since we know that r1 = −r2 from the figure. Rather than have some very long expressions, we proceed in steps and compute the momentum of

172

CHAPTER 7

each particle next using the cross product: > p1 := m1* CrossProduct(Omega, r1);



p2 := m2* CrossProduct(Omega, r2);





0 p1 := m1 ⎣ −ω a sin(θ) ⎦ 0



0 p2 := m2 ⎣ ω a sin(θ) ⎦ 0

As expected, m1 is moving into the paper (negative y velocity), and m2 is moving out of the paper. We now compute the angular momentum arising from each mass and add the results: > L1 := CrossProduct(r1, p1);



L1 := > L := L1 + L2;

L :=

a2 cos(θ) m1 ω sin(θ) 0 a2 sin(θ)2 m1 ω

L2 := CrossProduct(r2, p2);



L2 :=

a2 cos(θ) m2 ω sin(θ) 0 a2 sin(θ)2 m2 ω

simplify(%);

a2 cos(θ) m1 ω sin(θ) + a2 cos(θ) m2 ω sin(θ) 0 a2 sin(θ)2 m1 ω + a2 sin(θ)2 m2 ω



a2 cos(θ) ω sin(θ) (m1 + m2) 0 −a2 (−1 + cos(θ)2 ) ω (m1 + m2)

We see that even though the angular velocity is always along the z axis, the angular momentum has an x component, in addition to a z component. Furthermore, since we are viewing the system in a rotating frame, an observer in a fixed frame would see the x component of L rotate (precess) continuously about a fixed z component. To visualize this solution, we need to assign numerical values to the masses, lengths, angular velocity, and angles: > m1 := 1;

m2 := 1;

m1 := 1, > L;

a := 1;

m2 := 1,

Omega;

omega := 2;

a := 1,

theta := Pi/4;

ω := 2,

θ :=

π 4

map(eval, Omega);





2 ⎣ 0 ⎦, 2





0 ⎣ 0 ⎦ ω





0 ⎣ 0 ⎦ 2

We now have variables to plot that are numbers and not symbols.

7.6.1 Visualization of Vectors with arrow Often it is valuable to view a vector as an arrow in space. Maple has the neat command arrow that does just that:

173

MATRICES AND VECTORS; RIGID-BODY ROTATION > with(plots):

Omega := Vector([0,0,omega]);





0 ⎣ Ω := 0 ⎦ 2 > arrow(Omega, color = black, shape = double arrow);

arrow(L); # Two arrows

So we now have visualizations of both L and Ω that may be grabbed and rotated. We place them on the same graph by assigning objects to each arrow and then displaying the objects together: > w := arrow(Omega, color = black, shape = double arrow); # Assign object to arrow > L1 := arrow( L ): # Assign object a2 to arrow of L > display( w, L1, axes = BOXED, scaling = constrained, labels = [x,y,z], title = ‘Omega in black, L in colors‘);

# Display both together

Omega in black, L in colors

2 1.5 z

1

0 0.5

0.5 1 x

0 y

1.5 0

2

Observe that when we created the arrows we gave some options to the arrow command to control the color and shape of the arrows (use Help arrow for other properties of arrows that can be controlled). We have chosen to make Ω black as a way to indicate that it does not move in time (you are free to grab it and move it as you please, however). Notwithstanding the limited number of options available for the arrow command, the display command permits the usual ones possible for 2-D

174

CHAPTER 7

and 3-D plots (see Chapter 4). For this reason, it is with the display command that we placed labels, titles, and constraints on the scaling so that the vertical and horizontal sizes are true. Now we would like to convey the notion that the angular momentum vector L is rotating about the Ω vector (which is fixed along the z axis). We do that by creating vectors with the components that L would acquire as it rotates without changing magnitude, and then displaying them all in one plot along with Ω: > L2 := arrow([0,2,2]): L3 := arrow([-2,0,2]): L4 := arrow([0,-2,2]): > display(w, L1, L2, L3, L4, scaling = constrained, # Three arrows on one graph axes = BOXED, labels = [x,y,z], title = ‘Omega in black, L in colors‘);

Omega in black, L in colors

2 z1 0 –2

–2 –1

–1 y

0

0 1

1 2

x

2

7.6.2 Moment of Inertia for the Barbell We have just evaluated the angular momentum of the barbell by using the elementary definition of angular momentum for each individual mass, and then adding up the two angular momenta. Even though it is possible to follow this approach for any rigid object, it is easier to get the angular momentum by just multiplying the angular velocity ω by the inertia tensor: L = {I}ω.

(7.33)

However, to do that we must first evaluate the inertia tensor for the object under study. We start with the basic definition and apply it to the barbell Ii, j =





ma (δij (

a

xa, k 2 ) − xa, i xa, j ),

(7.34)

k

where the sum over a is over the two masses, and i, j , and k take on the values x, y , and z . The x’s in this equation describe the location of the two masses in Figure 7.1 in terms of Cartesian coordinates (what we called r1 and r2 before): > Omega := Vector([0,0,omega]);

# Angular velocity vector

175

MATRICES AND VECTORS; RIGID-BODY ROTATION





0 Ω := ⎣ 0 ⎦ ω > x1 := Vector([-a*sin(theta), 0, a*cos(theta)]);





x2 := . . .;



−a sin(θ) ⎦ 0 x1 := ⎣ a cos(θ)

# Masses



a sin(θ) ⎦ 0 x2 := ⎣ −a cos(θ)

The angular velocity vector and the positions of the masses are now known. In order to apply the equation for the moment of inertia, we see that x has subscripts indicating both the mass it corresponds to and its component along the x and y axes. Accordingly, we form a matrix x that has two indices: > x := Matrix(2,3, [[-a*sin(theta),0, a*cos(theta)], [a*sin(theta),0,-a*cos(theta)]]);



x := > x[1,3];

−a sin(θ) 0 a cos(θ) a sin(θ) 0 −a cos(θ)



# Check answer, check agree?

x1[3];

a cos(θ)

a cos(θ) 

> m := Vector([m1,m2]);

m :=

m1 m2



To keep this treatment simple, we will explicitly write out the a index over particles and have Maple sum over i and j as subscripts. We start by determining the contribution of mass 1 to the moment of inertia: > B := Matrix(3,3, (i,j)-> iˆj);





# Test case from before

1 1 1 B := ⎣ 2 4 8 ⎦ 3 9 27 > delta := IdentityMatrix(3) ;



‘delta[1,2]‘ = delta[1,2];



1 0 0 ⎣ δ := 0 1 0 ⎦ 0 0 1

delta[1, 2] = 0

> Inertia := Matrix(3,3, (i,j) -> m1*(delta[i,j]*sum(’x1[k]ˆ2’, ’k’ = 1..3) -x1[i]*x1[j]));





m1 a2 cos(θ)2 0 m1 a2 sin(θ) cos(θ) 2 2 2 2 ⎦ 0 0 m1 (a sin(θ) + a cos(θ) ) Inertia := ⎣ 0 m1 a2 sin(θ)2 m1 a2 sin(θ) cos(θ) > Inertia := Matrix(3,3,(i,j) -> sum(’m[A]*(delta[i,j]*sum(’x[A,k]ˆ2’, ’k’=1..3) -x[A,i]*x[A,j])’, ’A’=1..2) );

176

CHAPTER 7





m1 a2 cos(θ)2 + m2 a2 cos(θ)2 , 0 , m1 a2 sin(θ) cos(θ) + m2 a2 sin(θ) cos(θ) ⎦ 0 , m1 (a2 sin(θ)2 + a2 cos(θ)2 ) + m2 (a2 sin(θ)2 + a2 cos(θ)2 ) , 0 Inertia := ⎣ 2 m1 a sin(θ) cos(θ) + m2 a2 sin(θ) cos(θ) , 0 , m1 a2 sin(θ)2 + m2 a2 sin(θ)2

To simplify this, we assign the numerical values we had before for the masses, lengths, angular velocity, and angles: > m1 := 1;

m2 := 1;

m1 := 1

a := 1;

m2 := 1

a := 1 ⎡

> Inertia;

> with(plots):

omega := 2;

ω := 2

theta := Pi/4;

θ :=



π 4

1 0 1 ⎣ 0 2 0 ⎦ 1 0 1 matrixplot(Inertia, axes = boxed, title = ‘Inertia tensor barbell‘);

Inertia tensor of barbell

2 1.5 1 0.5 0 1

1 1.5

1.5 column

2

2 2.5

2.5 3

> Inertia.Omega;

# Mult, then check if Inertia unchanged

Inertia;





2 ⎣ 0 ⎦ 2

row

3





1 0 1 ⎣ 0 2 0 ⎦ 1 0 1

7.7 EXPLORATION: PRINCIPAL AXES OF ROTATION* We have seen that when the inertia tensor contains off-diagonal elements (products of inertia), the angular momentum and angular velocity of a spinning body are not parallel to each other. It would clearly be simpler if we did not have those offdiagonal elements to deal with. In general, what is “simpler” in life depends upon one’s view of the world, and the inertia tensor is no different. If we rotate the coordinate system we use to calculate the moment of inertia (still keeping it fixed to the body at the center of mass), then it is usually possible to find an orientation for which the inertia tensor is diagonal. In terms of individual elements, a diagonal matrix is expressed as: Ii, j = Ii δi, j . (7.35)

177

MATRICES AND VECTORS; RIGID-BODY ROTATION

In terms of the matrix representation, this is: ⎡



I1 0 0 ⎣ 0 I2 0 ⎦ , {I} = 0 0 I3

(7.36)

where we are using (1, 2, 3) to label the (x, y, z) axes in this new system. The set of axes in which the inertia tensor becomes diagonal is called the principal axes of inertia. Finding the principal-axes of inertia is not hard. The whole idea is that the physical angular momentum vector does not change when we choose to view it from different coordinate systems. Therefore when we take the expressions for L in the two systems and set them equal, we obtain equations that Maple will solve for us. We assume that in the principal-axes system, the body is rotating along one of the principal axes, namely, along the 1, 2, or 3 directions. Then the angular momentum L and the angular velocity ω will be parallel, L = Iω,

(7.37)

where I is the moment of inertia along the principal axis of rotation (which we cannot yet calculate since we do not know where those axes point). Yet L is still the same vector we expressed previously in (7.17)–(7.19) in terms of the products and moments of inertia. By setting the two expressions equal we obtain three simultaneous linear equations for the unknowns I and ω : Lx = Iωx = Ixx ωx + Ixy ωy + Ixz ωz , Ly = Iωy = Iyx ωx + Iyy ωy + Iyz ωz , Lz = Iωz = Izx ωx + Izy ωy + Izz ωz .

(7.38) (7.39) (7.40)

We write these three equations in the matrix form: > Matrix([ [I[xx]-I, I[xy], I[xz]], [I[yx], I[yy]-I, I[yz]], [I[zx], I[zy], I[zz]-I] ]), Vector([omega[x], omega[y], omega[z]]) = 0;



⎤ ⎡



Ixx − I Ixy Ixz ωx ⎣ Iyx Iyy − I Iyz ⎦ , ⎣ ωy ⎦ = 0 Izx Izy Izz − I ωz

The trivial solution is ω = 0. For a nontrivial solution to exist, the matrix on the LHS of this equation must have no inverse. This requires that the determinant of the matrix vanish: > Determinant(Matrix([[I[xx]-I, I[xy], I[xz]], [I[yx], Determinant(I[yy]-I, I[yz]], [I[zx], I[zy], I[zz]-I]])); Ixx Iyy Izz + Iyz Izy I + Izx Ixz I − Ixx − Ixx Iyz Izy − Ixx Iyy I − Iyy − Izz + I

178

CHAPTER 7 +Iyx Ixy I + Iyx Izy Ixz − Iyx Ixy Izz − Iyy Izz I + Izx Ixy Iyz − Izx Ixz Iyy − Ixx Izz I.

When the determinant is expanded (something we dare you to try), we are left with a cubic equation in I . The three solutions of this cubic are the three principal moments of inertia, Ix , Iy , and Iz . Once these I ’s are known, they are substituted back into the matrix equation to determine the angular momenta via: Lx = Ix ωx , Ly = Iy ωy , Lz = Iz ωz .

(7.41) (7.42) (7.43)

We enter the inertia tensor for a cube given as part of the Problem: > restart: with(LinearAlgebra): > Inertia := Matrix([[2/3, -1/4, -1/4], [-1/4, 2/3, -1/4], [-1/4, -1/4, 2/3]]);

⎡ 2 ⎢ 3 ⎢ −1 Inertia := ⎢ ⎢ 4 ⎣ −1 4

−1 4

−1 4

2 3

−1 4

−1 4

2 3

⎤ ⎥ ⎥ ⎥ ⎥ ⎦

Now we form a diagonal matrix with the value Iprin along the diagonal: > Idiag := Iprin * IdentityMatrix(3);





simplify(%);



1 0 0 Idiag := Iprin ⎣ 0 1 0 ⎦ 0 0 1



Iprin 0 0 Iprin 0 ⎦ Idiag := ⎣ 0 0 0 Iprin

Next we form the matrix whose determinant must vanish. We build the matrix up explicitly from its elements: > Inertia - Idiag;⎡

⎢ ⎢ ⎢ ⎢ ⎣

2 3

−1 4

−1 4

−1 4

− Iprin 2 3

−1 4

−1 4

−1 4

− Iprin 2 3

− Iprin

⎤ ⎥ ⎥ ⎥. ⎥ ⎦

Now we calculate the determinant of this matrix and ask Maple to solve for the value of Iprin for which the determinant vanishes: > Determinant( Inertia - Idiag );

121 55 − Iprin + 2 Iprin2 − Iprin3 864 48 > ‘I[prin]‘ = solve( Determinant(Inertia - Idiag), Iprin );

1 11 11 , ) I[prin] = ( , 6 12 12

179

MATRICES AND VECTORS; RIGID-BODY ROTATION

We should see three principal moments, two of which are equal (a consequence of symmetry). The inertia tensor in the principal-axes frame is thus diagonal: > Idiag := Matrix([ [1/6,0,0], [0,11/12,0], [0,0,11/12] ]);

⎡ 1 ⎢ 6 ⎢ Idiag := ⎢ ⎢ 0 ⎣

0

0



0

11 12

⎥ ⎥ 0 ⎥ ⎥ ⎦

0

11 12

Although we cannot derive it all here, the indicial equation that we have deduced above is the same one that arises in the eigenvalue problem. For our problem, the eigenvalues are principal moments of inertia, and the eigenvectors are three values for the angular velocity vector ω . Because each eigenvector is associated with one eigenvalue, the principal moment of inertia, the eigenvectors correspond to values of ω for which L and ω are parallel. This causes the directions of the eigenvectors to be the same as the directions of the principal axes in space. To verify this for our cube problem: > Inertia := Matrix([[2/3, -1/4, -1/4], [-1/4, 2/3, -1/4], [-1/4, -1/4, 2/3]]);

⎡ 2 ⎢ 3 ⎢ −1 Inertia := ⎢ ⎢ 4 ⎣ −1 4

> Eigenvectors(Inertia);

⎡ 11 ⎢ 12 ⎢ 11 ⎢ ⎢ 12 ⎣ 1 6

−1 4

−1 4

2 3

−1 4

−1 4

2 3

⎤ ⎥ ⎥ ⎥ ⎥ ⎦



⎡ ⎤ ⎥ 1 0 1 ⎥ ⎥, ⎣ 0 1 1 ⎦ ⎥ ⎦ −1 −1 1

We see that Maple first returns a column vector with the same three values of the eigenvalues as we found above. The matrix contains the corresponding three eigenvectors as its columns. Here we separate off the eigenvalues and vectors: > (Is, Omegas) := Eigenvectors( Inertia );

⎡ 1 ⎢ 6 ⎢ 11 Is, Omegas := ⎢ ⎢ 12 ⎣ 11 12 > ‘Is[1]‘ = Is[1];

# Store both eigenvalues and eigenvectors



⎡ ⎤ ⎥ 1 −1 −1 ⎥ ⎥, ⎣ 1 1 0 ⎦ ⎥ ⎦ 1 0 1

omega1 := Column (Omegas,1); . . .

# Extract eigenvectors

180

CHAPTER 7



1 Is[1] = 6



11 Is[3] = 12



1 ω1 := ⎣ 1 ⎦ 1



11 Is[2] = 12 ⎤



−1 ω2 := ⎣ 1 ⎦ 0

−1 ω3 := ⎣ 0 ⎦ 1

We see that the third eigenvalue corresponds to rotations along the diagonal of the cube, while the first two are perpendicular to the diagonal. To actually visualize these eigenvectors, we represent them as arrows with Maple’s arrow command: > with(plots):

arrow(omega3, axes = BOXED, color=black, labels = [x,y,z]);

1 z 0

x

y 0

Next, as before, we represent the three omega’s as objects and display them together: > w3 :=arrow(omega3,axes=BOXED,color=black,labels=[x,y,z]): > w2 :=arrow(omega2,axes=BOXED): w1 :=arrow(omega1,axes=BOXED): > display(w1,w2,w3);

181

MATRICES AND VECTORS; RIGID-BODY ROTATION

1 z 0.5 0 –1

0 –0.5 0.4 y

0

x

0.5 1

1

7.8 KEY WORDS AND CONCEPTS angular momentum diagonalization* inertia tensor matrix arithmetic vector vector direction

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.

angular velocity dot product linear algebra moment of inertia scalar multiplication

columns & rows eigenvalues* linear equations polar coordinates vector components

determinant eigenvectors* inverse matrix rigid rotation vector magnitude

Is a vector also a matrix? Is a row vector a matrix? Is a column vector also a matrix? What is the difference between a row and a column vector? Why is the study of matrices and vectors also called linear algebra? How many coordinates are needed to locate a point in space? What meaning is there to a vector having more than three components? Can you add, subtract, and multiply matrices? Can you divide matrices? How are eigenvalues and eigenvectors related? What is the relation between simultaneous equations and matrix equations?

182

CHAPTER 7

7.9 SUPPLEMENTARY EXERCISES 1. Use Maple’s linear algebra commands to find the solution of the simultaneous equations: 3 x + 2 y + z = 11, 2 x + 3 y + z = 13, x + y + 4 z = 12.

2. Find the inverse of the matrix:





4 −2 1 6 −4 ⎦ . A=⎣ 3 2 1 8

3. Verify that the inverse A−1 found in question 2 works in both directions; that is, that AA−1 = A−1 A = 1. 4. For the same matrix A as in problem 2, find and verify the solution of: ⎡



12 B = ⎣ −25 ⎦ . 32

AX = B,

5. In quantum mechanics, the electron’s spin is represented by the three Pauli matrices: 

σ1 =

0 1 1 0





, σ2 =

0 −I I 0





, σ3 =

1 0 0 1



.

Many of the properties of angular momenta follow from the properties of these matrices. a. Enter these three matrices into Maple. b. Use Maple to show that σ1 σ1 = σ2 σ2 = σ3 σ3 = 1.

c. Use Maple to show that σ1 σ2 = Iσ3 ,

σ2 σ3 = Iσ1 ,

σ3 σ1 = Iσ2 .

d. Use Maple to show that for (i, j, k) in cyclic order namely, [(1, 2, 3), (2, 3, 1), (3, 1, 2)], σi σj − σj σi = 2Iσk . e. Use Maple to show the trace of each of these matrices vanishes, trace σi = 0. 6. (You do not have to understand the physics here to do this problem.) Consider a hydrogen atom in a magnetic field B that points in the y direction. If the electron within the atom is described by the Pauli matrices of problem

MATRICES AND VECTORS; RIGID-BODY ROTATION

183

5, then the Hamiltonian matrix for this system is a Hermitian matrix of the form:   h −I B H= , IB h where I is the imaginary number and h is the energy of the atom when there is no magnetic field. a. The energy of this system is the eigenvalues of this Hamiltonian matrix. Find an analytic expression for the two possible energies for an electron in a magnetic field; namely, find the two eigenvalues of this matrix. b. Determine the corresponding eigenvectors and show that they are complex yet orthogonal to each other. (Orthogonality is a consequence of the matrix being Hermitian). 7. Determine the relation between the angular momentum and the angular velocities of a uniform plate and cube. Compare that relation to the one we worked out for a plate and cube composed of discrete masses. 8. Consider the matrix:   α β A= . −β α Solve for the complex eigenvalues and eigenvectors of this matrix. Hint: your eigenvalues should be λ = α − β , λ = α + I β . 9. In quantum mechanics, a spin 1 particle is described with the three 3 × 3 matrices: ⎡ ⎤ ⎡ ⎤ 0 1 0 0 −I 0 ⎣ 1 0 0 ⎦ ⎣ I 0 −I ⎦ ⎡ ⎤ 1 0 0 0 1 0 0 I 0 √ √ , M2 = , M3 = ⎣ 0 0 0 ⎦ . M1 = 2 2 0 0 −1 a. Show that

def

[Mx , My ] = Mx My − My Mx = iMz ,

as well as for cyclic permutations of the indices, namely, for index orders yzx and zxy . b. Show that Mx2 + My2 + Mz2 = 2 1 where 1 is the 3-D identity matrix.

Chapter Eight Trial-and-Error Searching, Maple Programming; Dipstick Calibration

8.1 PROBLEM: VOLUME OF LIQUID IN SPHERICAL TANKS A spherical tank of radius R = 3 meters is to be used to hold gas. You are given a stick to dip into the tank, and you want to calibrate the scale on it such that it reads the volume of fluid in the tank (see Figure 8.1). Your problem is to determine the height of the fluid for a given volume. Hint: use an indirect approach by calculating the volume for a series of heights until some height gives you the volume you are looking for.

8.2 MATH: VOLUME INTEGRATION It seems straightforward to calculate V (H), the volume of the liquid in the tank as a function of height H . Once we have that, the hard problem is to invert the solution, that is determine H(V ). Consider Figure 8.1. The volume of a disk of differential thickness dh and radius r is dV = πr2 dh.

(8.1)

Figure 8.1 A spherical tank of radius R filled with a liquid to height H above its bottom.

TRIAL-AND-ERROR SEARCHING, MAPLE PROGRAMMING; DIPSTICK CALIBRATION

185

Here the height varies over the range 0 ≤ h ≤ H , and the radius r is related to the variable height h via Pythagoras’s theorem: R2 = r2 + (R − h)2



r2 = R2 − (R − h)2 .

(8.2)

Indeed, if h = 2R, then r = 0, as it should. We evaluate the volume by adding up the volumes of all the differential disks:  H

V (H) =

πr2 dh =

0



 H

0





π R2 − (R − h)2 dh

H3 = π RH − . 3 2

(8.3) (8.4)

Equation (8.4) gives the volume of fluid as a function of its height. As a check we see that for an empty tank V (0) = 0, and that for a full tank V (2R) = 43 πR3 . Our problem requires us to deduce the height for a known volume. To do that we rewrite (8.4) as the cubic equation H 3 − 3RH 2 +

3 V = 0. π

(8.5)

For R = 3 and a sample volume V = 43 π , the equation to solve is f (H) = H 3 − 9H 2 + 4 = 0.

(8.6)

Here (8.6) is in the standard f (h) = 0 form. We know from the geometry of the problem that the solution we want must lie in the range 0 ≤ H ≤ 2R = 6.

(8.7)

Because f (H = 0) = 4 and f (H = 2R = 6) = −105, we know that our function must pass through zero in this range. Regardless of the existence of closed-form expressions for the solutions of cubic equations, we shall solve it numerically (a procedure you would have to follow anyway if there were higher powers than the third). In spite of the fact that Maple’s fsolve command is applicable, we wish to understand what goes on inside that command and to get some programming experience. 8.3 ALGORITHM: BISECTION SEARCHES A traditional computational technique is “trial and error,” in which a program starts with a trial solution, determines how large the error is, and then makes a new guess based on the error. The procedure keeps repeating until the error becomes acceptably small. These trial-and-error programs are interesting to write because they must be intelligent, and interesting to run because you are not sure how long it will take to find a solution, or whether a solution will be found at all.

186

CHAPTER 8

Figure 8.2 A graphical representation of the steps involved in solving for a zero of f (x) using the bisection algorithm. In each step the algorithm takes the midpoint of the interval as its new guess for x, thereby reducing the interval’s size by one half. The first interval is (x1 , x−1 ), the second (x2 , x−2 ), etc.

The most elementary trial-and-error technique is the bisection algorithm. Its basis, shown in Figure 8.2, starts with two values, x− and x+ , between which we know a zero occurs. We assume that f (x) is negative at x− and positive at x+ : f (x− ) < 0,

f (x+ ) > 0.

(8.8)

This approach does not limit us in any way, since it just means that if the function changes from positive to negative as x increases, then x− will be greater than x+ . The algorithm bisects the interval to obtain the midpoint x=

x+ + x− , 2

(8.9)

and then checks if there is a sign change in the right half of the interval. If there is, then it limits the search to the right half-interval only; if not, then it limits the search to the left half-interval. In either case the regions to be searched decrease by one half each time. In Figure 8.2 the first interval extends from x− = x+1 to x+ = x−1 . We bisect that interval at x, and since f (x) < 0 at the midpoint, we set x− ≡ x−2 = x and label it x−2 to indicate the second step. We then use x+2 ≡ x+1 and x−2 as the next interval and continue the process. We see in the figure that only x− changes for the first three steps, but that for the fourth step, x+ finally changes. After that, the changes get too small for us to show.

TRIAL-AND-ERROR SEARCHING, MAPLE PROGRAMMING; DIPSTICK CALIBRATION

187

Table 8.1 Maple’s relational operators. < >=

less than greater than or equal to



greater than not equal to

8.4 PROGRAMMING IN MAPLE We have already done some Maple programming when we defined arrow functions to represent expressions. This is fine as long as the function definition fits on one line. However, sometimes we need to include a number of steps in a calculation, and we cannot do that with an arrow function. In these latter cases we need to write a multiline program, often with logic deciding which path to follow through the program. In the sections to follow we explore some of the features of Maple that are useful for programming. Notwithstanding the usefulness of programming with Maple for honing your programming skills, we recommend that you also experience the more powerful programming possible with compiled languages like Java and Fortran. We do that in Part 2 of this book. Even if you do not intend to be programming in Maple, the programming here will serve well as an introduction to general programming.

8.4.1 Logic The first step in having the computer make intelligent decisions is having it be able to tell if a statement is true or false. This is accomplished with Boolean variables, that is, variables that are either true or false. Objects that have true and false values may also be constructed from expressions. To construct a Boolean expression, you use relational and logical operators to combine ordinary variables. The relational operators in Maple are as shown in Table 8.1. To prove the point, 1 < 2 is true, as is 1 3. To determine what Maple believes to be true, we evaluate an expression as Boolean using the evalb command: # Returns true, false, or fail

> evalb(1 > 2);

false > T :=1;

Heat :=3;

> evalb(T if T if T > 0 then Heat := Heat - 1 end if;

Heat := 2

We accomplish the same goal in just one line by invoking the else if construct, that Maple calls elif: > T :=1;

Heat :=3;

T := 1

Heat := 3

> if T Sum(i, i = 1..100) = sum(i, i = 1..100); 100 

i = 5050

i=1

In this case, Maple has an internal program that loops over the sum. We do the same thing ourselves using Maple’s for construct: for from by to ,

where not all parts must be used at any one time, although do and end do are always required. So we write our own sum: # Initialize

> mySum := 0; > for i from 1 to 10 do

# Prints out intermediate execution

> mySum := mySum + i;

# Ends loop

> end do;

mySum := 0 mySum := 1 .. . mySum := 55

Here all the lines between the do and end do statements get repeated for as long as the condition specified by the for .. from .. to .. construct holds true. In the present case, the for construct increases i from 1 to 10, and the loop increases mySum by i each time the loop is repeated. For more involved computations there may be many lines between the beginning and end of the do loop. As is often the case with loops, some variables must be initialized before the loop begins, as we do here with mySum := 0;. Take note, you will avoid the 100 lines of intermediate output by replacing the “;” on the end do line with a “:”. For some applications you may wish to change the loop index (counter) by a number other than 1. (Normally you may leave out the from and by parts of the construct and let them have their default values of 1.) For loops that do not increment by 1, you use the by construct and pick what change you would like; for example, here we sum all the even number from 1 to 100: # Initialize

> mySum := 0;

mySum := 0 # Increase i by 2 # No printout as have :

> for i from 0 by 2 to 100 do > mySum := mySum + i;

# Ends loop

> end do: > ’mySum’ = mySum;

mySum = 2550

TRIAL-AND-ERROR SEARCHING, MAPLE PROGRAMMING; DIPSTICK CALIBRATION

191

8.4.4 while Loop You may not always know ahead of time just how often a loop must repeat before the necessary condition is met. In that case you repeat the loop while some condition is true: while do end do

To give an instance, let us say we want to keep summing until some sum is greater than 10,000, and then find out what the i value is: > restart: mySum := 0; > while mySum < 10000 do

# Initialize # Repeat while

i := 0;

> mySum := mySum + i; > i := i + 1; # Ends loop

> end do: > ’mySum’ = mySum; > ’i’ = i;

mySum := 0 i := 0 mySum = 10011 i = 142

The problem with the while loop is that if the Boolean condition is always true, then the loop is “infinite”; that is, it never end. By way of example, if you change mySum < 10000 to mySum > 0, be prepared to press the stop button! An addition to the while loop that will keep it from becoming infinite is to break the loop, that is, to stop it if some alternative condition is met (say, after 100,000 iterations). For example, here we break the previous while loop when i = 77: > mySum := 0;

# Initialize

i :=0;

mySum := 0

i := 0 # Do while

> while mySum < 10000 do > mySum := mySum + i; > i := i + 1;

# Break condition

> if i = 77 then break end if;

# Ends loop; no printout as have :

> end do: > ’mySum’ = mySum; > ’i’ = i;

mySum = 2926

i = 77

192

CHAPTER 8

8.4.5 Procedures (Methods, Multiline Functions) Though it is satisfying to write worksheets that have intelligence, the use of logic and looping tends to require many lines in order to handle all possible situations that may occur. If all the logic and possible cases are placed together, we may end up with a program that is so long and complicated that it is hard to follow. Consequently, it is useful to do some housekeeping and remove some of the utility commands out from the mainstream of our worksheets. Maple permits this by letting us define our own multiline procedures to be called from within a worksheet. Essentially, this is equivalent to defining a function, or what Java calls a method. Here we define a procedure MySum() that sums the values of i from iStart to iEnd, and then uses Maple’s printf command to print out the results in a general format (the %g symbol). The definition of a procedure uses the Maple key word proc, takes the arguments iStart and iEnd, and ends with the key word end. Check over the absence of a semicolon or colon on the line containing proc, or on the last line before end: > restart; > MySum := proc(iStart, iEnd) > local iQuit; > global i, mySum;

# Named procedure (argument list) # Local variable used only within procedure # Global variable used outside too

> iQuit := 77; > mySum := 0;

# Initialize for each call

> for i from iStart to iEnd do > mySum := mySum + i; > if i = iQuit then break end if; > end do;

# Ends loop

> printf("In MySum, at end of do loop, i = %g, mySum = %g", i, mySum) > end

MySum := proc(iStart, iEnd ) local iQuit; global i, mySum; iQuit := 77 ; mySum := 0 ; for i from iStart to iEnd do mySum := mySum + i; if i = iQuit then break end if end do; printf(“ In MySum, at end of do loop, i = %g, mySum =%g”, i, mySum) end proc

TRIAL-AND-ERROR SEARCHING, MAPLE PROGRAMMING; DIPSTICK CALIBRATION

193

Observe that Maple returns a statement of what it thinks our procedure is intended to do, with italics used to indicate variable names. Now we test our procedure by calling MySum, as you would any Maple function, with numerical values for the arguments iStart and iEnd. If the procedure works as intended, Maple should evaluate all the statements within the procedure and then return to the worksheet with values for the global variables: > MySum(0, 10000);

InM ySum, at end of do loop, i = 77, mySum = 3003 > ’mySum’ = mySum;

mySum = 3003

Here we have called our procedure by name and supplied it with the required two arguments. The values for the internal, or local, variables within a procedure are not visible from the outside the procedure: # Local to procedure

> iQuit;

iQuit > mySum,

i;

3003,

77

# Global, visible everywhere

We see that the internal test causes the loop to break at i = 77, and that the value of the variable mySum does get returned to the calling program (it should, since we made it global). Now let us tell the procedure to end at iEnd = 70 and not give it a chance to break: > MySum(0, 70); In MySum, at end of do loop, i = 71 , mySum = 2485 > ’mySum’ = mySum; > a := mySum;

mySum = 2485

a := 2485

In summary, the general form of a procedure is: proc () local options

end;

global

where the first and last lines must always be present. Exercise: Add the line > mySum: as the line before the > end; verify that now ♠ the value of mySum is returned.

194

CHAPTER 8

8.4.6 Conversion of Procedures to Compiled Code The code generation package CodeGeneration, and the older codegen, are used to convert Maple procedures and modules into compiled code. Although we give no examples, we tell you this for reference purposes. 8.5 SOLUTION: VOLUME FROM DIPSTICK HEIGHT The solution to our problem requires us to solve for the height h at which f (h) = h3 − 9 h2 + 12 = 0.

We start the solution by defining this function in Maple and then plotting it as a check that there are solutions in the region of interest: > restart; f := (h) -> hˆ3 - 9.*hˆ2 + 12.; > plot(f(x), x = 0..9, title = ‘Height of Liquid in Sphere‘); 3 2

f := h → h − 9. h + 12.

Height of Liquid in Sphere x 4 6

2

8

0 –20 –40 –60 –80

We observe two solutions, namely, two places where f (h) = 0. Yet as we discussed when we first set up this problem, the physical solution must lie in the range 0 < h < 2R = 6. Consequently only the root near h = 1.5 is physical. As we said, while we have the option of using Maple’s fsolve to find the root, we will find it from scratch by programming up the bisection algorithm. We set the level of desired precision to eps = 0.01, and the range to search in as hhi = 7, hlo = 0: > eps := 0.01;

hlo := 0.;

eps := 0.01

# Parameters

hhi := 7.;

hlo := 0.

hhi := 7.

We use a while loop to narrow down the h range until eps |(hhi − hlo)/(hhi + hlo)| is no longer true:



TRIAL-AND-ERROR SEARCHING, MAPLE PROGRAMMING; DIPSTICK CALIBRATION > while abs((hhi-hlo)/(hhi+hlo)) >= eps do > h := 0.5 * (hlo+hhi): > printf(‘ in loop h = %g‘, h);

195

# Bisect the interval # Print out # Decide if zero on right or left

> if ( f(hhi) * f(h) hlo := h; > else hhi := h; > end if; > end do;

h := 3.5 in loop h = 3.5

h := 1.75 in loop h = 1.75

h := 0.875 .. . in loop h = 1.244141

This tells us that for a dipstick height h = 1.244m we have a volume V = 4π/3 = 4.19 m3 of fluid in the tank. As a check, we apply Maple’s fsolve command > fsolve(f, h, 0..2);

1.243848483

Sure enough, we see that the two answers agree to one place in the fourth decimal place, just the precision we asked our algorithm to produce. 8.6 KEY WORDS AND CONCEPTS Boolean expressions procedure vs. function 1. 2. 3. 4. 5. 6. 7. 8. 9.

Boolean variables volume integration

local vs. global logical operator

flow control trial and error

Why would you ever solve a problem with a trial-and-error approach? Can you do arithmetic on Boolean variables? What is meant by symbolic logic? How might Boolean variables be related to artificial intelligence (the computer appearing to think)? Can all decision making be reduced to sets of Boolean expressions? Must there always be a relation between the height of a liquid in some vessel and the liquids’s volume? If the height of a liquid in some vessel doubles, does that imply that the volume of the liquid must have increased eightfold? Describe what is meant by an iterative solution to a problem. Can a program containing correctly programmed Boolean expressions ever give the wrong answer?

break loops

196

CHAPTER 8

10. Can logical conclusions ever be wrong? 11. Is a solution found in 10 steps better than one found in 100 steps? 12. Can a function use another function as part of its definition? 8.7 SUPPLEMENTARY EXERCISES 1. Use the bisection algorithm to calibrate the dipstick. Determine the h’s for which the tank is 1/4 full, 1/2 full, 3/4 full, and 99/100 full. 2. Use the if . . . end if syntax to make a plot of the tan θ for −2π ≤ θ ≤ 2π . Limit the ordinate to |tan θ| ≤ .75. 3. Write a loop that evaluates the power series for the exponential function  (−x)n e−x = N for x = 1, 10, 100. Continue the loop until the next n=0 n! term added to the sum is less than 10−6 of the value for the sum. 4. Create a loop that uses the bisection algorithm to find a solution, good to six places, of the equation x = e−x . 5. Consider the nonlinear equation for the function y(x) : x e−y . y= 1 + y2 2

Write a procedure that solves for and returns y(x). Plot y(x) for 0 < x < 12. Write a procedure that computes and returns the derivative dy/dx. Make a plot of dy/dx for 0 < x < 12, and check that the derivative does correspond to the slope in the previous graph. 6. (Adapted from [Zach 96]) If a is your age in years, w is your weight in pounds, and h is your height in inches, construct a Boolean expression that will be true only when the following statements are true: a. you are old enough to obtain a driver’s license and you do not weigh 1,000 pounds; b. you are not a teenager; c. you are either younger than 20 and less than 150 pounds, or older than 40 and greater than 6 feet; d. you are neither old enough to vote, tall enough to hit your head on a fivefoot door frame, nor the right weight to box as a 132–140 pounder. 7. Let A be your age, Y the number of years you have been in college, and D the number of dollars you have in the bank. Construct a Boolean expressions that will be true when the following conditions are met: a. you are a millionaire but you are not a senior; b. you are either too young to vote or you are not a freshman; c. you are either younger than 20 and broke, or older than 90 and have more than $100,000; d. you are 16 years old and your number of years in college is greater than the number of dollars you have in the bank. a. b. c. d.

Chapter Nine Getting Started with Java: Compiling, Interpreting, Executing

9.1 COMPILED LANGUAGES As we have seen in our work with Maple, a computer is an incredibly powerful and helpful device, when it does what you want. Maple is a fairly easy way to tell the computer what to do and that is why we started with it; you enter a command and Maple responds. If Maple cannot understand your command, or if its response is not what you like, then you just keep changing the command until all is well. In this line-by-line mode with a response after each command line, Maple is acting as an interpreter. Having the immediate response of an interpreter is useful, and when it is combined with a friendly graphical user interface (GUI), as in Maple, we have a powerful environment for scientific work. To provide you with a broader experience in scientific computing than is possible with Maple, we now look at a different approach. Rather than enter commands one at a time, we write a program containing all the commands, and then have the computer process (compile) them all in one fell swoop. Just as you probably have found it rather rare to be able to enter many Maple commands without making at least one error, it will also be rather rare that you are able to write a long program perfectly on the first try. However, there are many rewards to be had by trying, and once you get it right it, running it can be a breeze. But not to worry, we will start you with programs to modify rather than making you start from scratch. To understand the value of a compiled language, imagine telling a story to a bunch of friends at party in which you were restricted to speaking only one sentence at a time, and could not continue your story until someone else responded to each of your sentences. Even though you might be able to get through your story, you could probably weave a better tale if you did not have to stop all the time. Likewise it is with a compiled approach to computing. Though it may take

200

CHAPTER 9

Source Code Area.java

compile % javac Area.java

Compiled Code (byte code)

execute % java Area

Area.class

Figure 9.1 Steps in compiling and executing a Java program.

some more skill and time to weave a long program, you should be able to make it do exactly what you want, in exactly the way you want it done. And if the program gets used more than once, for instance to analyze different data sets, you do not have to repeat the process. Here are some reasons for using Java as your first compiled language: 1. Java is the closest a computer language has ever come to being universal in syntax and execution, secure, and highly flexible. 2. Java is modern in structure, much like C and C++, and essential for computing on the World Wide Web. 3. Java requires the user to be specific about the type of variables declared (strongly type-cast) and is very careful about the precision of mathematical functions. This helps a student learn the proper handling of floating-point variables (especially important in scientific computing). 4. Java incorporates the modern, and possibly best, view towards programming known as object-oriented programming. C++ is also object-oriented but harder to learn. 5. Java has automatic “garbage collection”; that is, it automatically frees up sections of computer memory once your program stops using them. In C, you must do this yourself (a pain). 6. In our view the error-handling capability of Java, both during compilation and execution, is excellent. It is demanding that its grammar be followed (a good thing) and informative when it is not. 7. Finally, Java is exciting. Much of the excitement has arisen from Java’s ability to do Web computing and from the availability of many classes (prepared utility programs to be incorporated into your program). We have not said that we like Java because it is fast. Compared to Fortran and C, Java is slow, although it has the promise of being much faster. However, since modern computers are so fast, its speed will not affect you until you get into large projects. Likewise, there are fewer scientific libraries of programs for Java than for Fortran and C. However, the availability of libraries is also improving, spurred on, in part, from the developers not having to have a different library for each operating system.

GETTING STARTED WITH JAVA: COMPILING, INTERPRETING, EXECUTING

Listing 9.1 Area.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16



201

17



// Area . j a v a : Area o f a c i r c l e , s a m p l e program p u b l i c c l a s s Area { p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) { double r a d i u s , circum , area , PI = 3.14159265359; / / I n i t i a l i z e i n t modelN = 1 ; radius = 1.; circum = 2. ∗ PI ∗ r a d i u s ; a r e a = r a d i u s ∗ r a d i u s ∗ PI ; // Screen Output System . o u t . p r i n t l n ( "Program number = " + modelN ) ; System . o u t . p r i n t l n ( "Radius = " + r a d i u s ) ; System . o u t . p r i n t l n ( "Circumference = " + c i r c u m ) ; System . o u t . p r i n t l n ( "Radius = " + r a d i u s +", Area = " + a r e a ) ; } }

9.2 JAVA PROGRAM PIECES Whether simple or complex, developing Java programs requires two basic elements. These are indicated in Figure 9.1. One is a source file that you have written in Java and has a name ending with .java. The other is the executable file that the computer compiles for only computers to read, and has a name ending with .class. For instance, in Listing 9.1 we give the contents of a source file containing a simple program Area.java that calculates the area of a circle. There are a number of actions involved in order for you to convert Area.java into something a computer will understand: 1. You issue a command to compile the Java source code from within a shell (a command-line interpreter). You know that you are “in” a shell by the commandline prompt, which may contain a > or a %, or something containing the name of the directory/folder in which you are located. 2. The conversion of your program into an executable file is done with the Java compiler at the shell prompt (the second step in Figure 9.1): > javac Area.java

Run Java compiler on file Area.java

When we illustrate commands like the one above, the first field of characters (>) is the shell’s prompt, the second field, in monospaced bold type, is the command that you enter, and the rightmost field is a comment to you that should not be entered.

202

CHAPTER 9

3. For these commands to work you must issue the javac command from within the directory in which the file Area.java is stored.1 If the compilation was successful and you get no error messages, Java will have written another file in the same directory ending with the extension .class. In our example, Area.class. 4. In Java terminology, the compiled program in the .class file contains byte code. The term byte indicates that the code is compiled to the byte level, and the term code is used synonymously with the word “program.” Java’s universality means that this same .class file runs on any operating system, be it Unix, Windows, or Mac. In other compiled languages like Fortran and C, the compiled code contains commands that only one particular computer is able to execute, but it does so immediately. In Java, the compiled code is universal, yet another, internal, step is required before execution. 5. The last processing step in Figure 9.1 is executing or running your program:2 > java Area

Execute byte code Area.class

When we issue this execution command java, the .class extender to Area is left off. Java just needs to know that Area is the name of the class for it to run the program Area.class. If you actually entered Area.class or Area.java as the program to run, you would get an error message. Go ahead and try java Area.class, it does no harm. 9.3 ENTERING AND RUNNING YOUR FIRST PROGRAM As a general rule, you will not learn to compute just by reading about it. You must do what is necessary to get a program running and giving correct answers before you understand it. Accordingly, before we try to explain what is inside a Java program, let us get some experience with the steps we have outlined so far. In the code Listing 9.1 is the Java source code for the program Area.java. The line numbers on the left are there to help explain the program to you; do not enter the line numbers in your program. The lines beginning with a //, or between /* and */, are comments not read by Java. You do not need to enter these comments into your program, but they do help with understanding and documentation, and you are free to put whatever you want there. 1. Use a text editor3 to enter Area.java into a file named Area.java in the direc1 If

you are using a programming environment package such as Code Warrior, some of this will be done automatically for you. Nevertheless, it is a good idea to check the location and modification dates of the files you create and use. 2 In a strict sense this is interpreting, because Java executes the Area.class file one line at a time. However, most modern Java installations actually recompile the byte code into a completely compiled object code, and then run the recompiled code. In any case, as far as you are concerned Java will follow the commands in your program from top to bottom. 3 An editor is a program like Notepad on a Windows PC, or nedit or Xemacs on Unix. Word processing

GETTING STARTED WITH JAVA: COMPILING, INTERPRETING, EXECUTING

203

tory for your work. By “enter” we mean key the program in rather than cut and paste it. You may view this as mindless clerical work, but reading and looking at what you are entering helps make this foreign language seem less alien. 2. After you have entered the entire program, Save it into a file named Area.java in your personal directory. 3. Open a shell in your directory and compile Area.java: > javac Area.java

Get help if this will not execute

We remind you: the > here is the computer’s prompt to you; the command you enter is in bold monospaced font, and the last field is a comment, not to be entered. 4. If compilation is successful, you will be told nothing (you only get spoken to when you do something wrong). However you can check that a file Area.class was created by listing all the files, with creation times, in your working directory: > ls -l > dir A*

From Unix/Linux prompt From Windows command prompt

5. If you do have to correct some error (presumably typographical since you are just coping over a running program), open your Area.java in an editor and fix it. It is a good idea to keep the editor open in a separate window while you compile in the shell’s window. Then Save Area.java in the same directory where you are doing the compiling and try compiling again. 6. Get into the habit of saving your files often, and sometimes as backup copies. Saving writes a copy to the hard disk for storage, while closing the file removes it from the grasp of the editor, without necessarily saving it. 7. If you like looking where you should not, use your editor to open and look at the Area.class file. Inasmuch as this file is not meant for human comprehension, what you see will not be illuminating. Alternatively, these files are viewable from the shell: > cat Area.class > type Area.class

Print out on screen for Unix Print out on screen for DOS

8. We find ourselves more patient waiting for the computer to do its work if we know what the computer is doing (this also alleviates anxieties that we have done something wrong). On this account we recommend that you include the -verbose option in your commands. We may leave it off in this book to keep things short, but it is fun to include it: programs like Word or Word Perfect are possible, but you must remember to save the file as Text to avoid control characters that will confuse the compiler.

204

CHAPTER 9

> javac -verbose Area.java

Tell me what javac is doing

9. Check again that you have created the byte code version of your Java program, namely, that there is a file Area.class: List files in Unix/linux List files in DOS

> ls -l > dir

10. As we see from Figure 9.1, once we have compiled code in Area.class, we get the computer to execute or run it by issuing the java command (no c) at the prompt with just the class name: Execute Area.class Wrong, cannot have .class extension

> java Area > java Area.class

You should get output of the form Program number = 1 Radius = 1.0 Circumference = 6.283186 Area = 3.141593

11. Now that you have a program that is running, check that it is actually doing as you intended by changing a line in it and seeing how the results change. In particular, try assigning a value to the variable radius: 6 radius = 1.;

12. Use your editor to change this line to radius = 10.;, being careful not to leave out the semicolon “ ;” at the end of the line. 13. Save the modified Java source code in the file Area.java. 14. Compile the modified file, correcting any errors if necessary. 15. Execute the new Area.class file with the command java Area, and check that you get 100 π as the area. 16. To see how Java responds to not doing things correctly, be adventurous and try below some unconventional variations on the theme: Left off .java after Area Forget to capitalize

> javac Area > javac area.java error:

Cannot read:

area.java

> java Area.java > java -verbose Area.java

The error message on Unix Left off c on javac Really get yelled at for that c!

GETTING STARTED WITH JAVA: COMPILING, INTERPRETING, EXECUTING

205

9.4 LOOKING INSIDE AREA.JAVA We now look at Area.java to get some idea of how a program goes about its business. Though a simple program, it is not trivial in that it contains a number of important parts. Admittedly it is hard to understand at first all that is going on; be patient, after seeing them a few times it begins to make more sense. At the top of Area.java is a // and some information about the program. Text following the // is a comment unread by Java and placed there as information for the user. The // comment may be placed on a line by itself, or after some Java statements. Multiline comments start with a /* and end with a */. They too are ignored by Java. We encourage the use of comments and blank lines to make code clearer. Nevertheless, we are frugal in our examples in order to space printed space.

// Comments and /* Comments */

This line declares the name of the class to be Area. You may think of the term class as the name of our program.4 Remember that this is exactly the same named used for the file Area.java containing the Java source. In fact, if the two do not agree, then Java will complain. The public on line 2 indicates that this class may be read by other programs. While speaking of area, notice on line 8 the statement area = radius * radius * PI. Here area is the name of a variable, and since Java is case sensitive, Java treats it as a completely different name from the class name Area. public class Area

public static void main(String[] argv) Line 3 contains the important word main and means that the main method is to follow. A class usually contains a

number of methods, the term method denoting a subprogram that behaves like a mathematical function. It is called a “method” to indicate that it is a method for handling data or variables. Every Java program that runs directly on a computer, that is, not via a Web browser, must contain a main method as the place where execution begins. This simple program contains only one method, the main method. The body of the main method is made up of all the lines of code between the first open brace { and the last close brace }. Usually the main method is used as the control unit or administrator for a program; it does not do much in the way of work itself, but instead calls other methods to get the computation done and then collects and organizes their results. It is probably best for first-time readers to just accept the phrase public as the way to declare a main method, and ignore for now our explanation of what the modifiers and arguments to main mean.

static void main(String[] argv)

4 In a formal sense, a class is a collection of data (variables) and methods (functions) that act on those data. In the present case, our class file has only the main method.

206

CHAPTER 9

Nevertheless, we now give some explanation for completeness and for later-time readers. The modifier static before main indicates that main is a static method, and not dynamic like an object. We talk about objects in Chapters 16 and 18, and suggest you ignore it for now. The modifier void before main indicates the type of output produced by main, in this case nothing. Likewise, the parentheses in main(String[] argv) contain the input or arguments supplied to main. In this case there is a single argument named argv and it is a String. The square brackets [ ] indicate that argv is an array, that is, a subscripted variable. double radius, circum, ...; int modelN = 1; Before you are permitted to use a variable in Java, you must declare its name and the type of variable it is. This creates instances or records of them so that they may be used later. The first line declares the variables r, area and PI to be double, that is, double precision. The next line declares the variable modelN to be an int, that is, an integer. (We describe the various data types in the next chapter).

Instead of assigning values later, lines 4 and 5 serve double duty by assigning initial values to the variables PI and modelN. Inspect how in both of these lines there is first a data type (double or int) and then a series of one or more variables that will be stored with this data type. Any number of variables are permitted on these lines, although it is probably best to add additional declaration lines rather than have one line so long that its end gets hidden beyond the edge of the screen. A semicolon ; is used to end these two statements, as is true for most “action type” statements in Java. radius = 1.; Lines 6, 7, and 8 are assignment statements. They compute the numerical value of whatever expression is on the RHS of = and assign that number to the variable named on the LHS of =. Here, “assign” means to store a numerical value in the memory location reserved for that variable. (This is the same as the assignment operator := in Maple, except Java deals only with numerical values.)

Arithmetical operations are denoted by the standard +, -, *, and / symbols. However, the symbol ˆ, often used for exponentiation, is used for a logical operation xor in Java. Instead, use the power function in the Math class, Math.pow(x,2). In this example we squared the variable radius by simply using radius * radius, but we could also have said Math.pow(radius,2). System.out.println(. . . );

These lines look worse than they really are and may be used as given until you know better. If you learn to read the System.out.println part backwards, you could believe that it says to print a line out using Java’s (the System’s) methods to do this. The periods in the command denote the access route. Starting from the top level class, the System class, we then ac-

GETTING STARTED WITH JAVA: COMPILING, INTERPRETING, EXECUTING

207

cess a subclass out and one of its methods, the println(String s) method. (As an example of another such object-oriented method access, say you make a class MyClass with a method myMethod(). If you wish to access this method, then call MyClass.myMethod();.) Getting back to these output lines, the argument ("Program Number = " + modelN) means take the string Program Number = and append another string containing the numerical value of the variable modelN to the end of it. The appending is done by the + modelN part. Likewise, the output on line 11 starts with a string Radius = and appends the numerical value of radius to it. Once these strings are built up (concatenated), they get printed out to your screen, each on a new line. If you do not want Java to place your output on a new line, but instead to continue with the last line, then you would leave off the ln part: System.out.print(" Program Number = " + modelN);

You are allowed to make the string that gets printed as long as you like by concatenating more and more substrings to it. To cite an instance, on line 13 System.out.println("Radius = " + radius +", Area = " + area);

after the value of radius is added to the string, we add in the string “, Area = ,” and then add on the value of area after that. (When we say “add on the value of,” what really is happening is that Java is converting a double to a string, and then adding that string to the end of the previous string). 9.5 KEY WORDS assignment statement compile methods source file/code string

byte code execution objects type-cast main method

class garbage collection program text editor running

comments interpreter shell print

9.6 SUPPLEMENTARY EXERCISES 1. Modify Area.java into a new program Volume.java that calculates the volume of a liquid in a spherical tank of radius R, when the liquid is a height H above the bottom of the tank. We have already studied this problem with Maple in Chapter 8, where we derived that the volume V = πH 2 (R − H/3). Make sure to test your program for H = 0, R, and 2R, namely, for an empty, a half-full, and a full tank. 2. Explain in just a few words: a. what the Java interpreter does; b. what the Java compiler does; c. how Java differs from an operating system; d. what is a shell.

Chapter Ten Data Types, Limits, Methods; Rocket Golf

10.1 PROBLEM AND THEORY (SAME AS CHAPTER 3) Michele, our golf fanatic in Figure 3.1 is still in pursuit of the world’s record for the fastest golf ball. Recall, she hits her golf balls from a rocket moving to the relative to observer Ben on earth, and sees her ball right with a velocity v = c/2 √ travel with a speed of U = c/ 3 at an angle θ = 30o with respect to the moving rocket. She observes her drive to remain in the air for hang time T  = 2.6 × 107 seconds. 1. How would Ben, watching Michele’s drive from the earth, describe the golf ball in terms of its speed, angle φ, and hang time T ? 2. How would the answer to 1. change if Michele hit her ball to the left, that is, in a direction opposite to the rocket’s velocity? 3. If Michele hit the ball with a speed U = c, how would the answers change? The formulas we need from special relativity relate the description of the same moving object as it appears to observers in different frames. If Michele in O’ sees her golf ball having velocity components: U  = (Ux , Uy ),

(10.1)

then Ben in O, who sees Michele moving to the right with velocity v , will see her golf ball move with velocity components U = (Ux , Uy ), Ux

+v , 1 + vUx /c2 1 γ= . 1 − v 2 /c2

Ux =

(10.2) Uy =

Uy γ(1 + vUx /c2 )

(10.3) (10.4)

10.2 JAVA’S PRIMITIVE DATA TYPES Before we begin to calculate with Java, it is in your best interest to explore the limitations of the numerical capabilities of Java. It all arises from the schemes

209

DATA TYPES, LIMITS, METHODS; ROCKET GOLF

Table 10.1 Java’s basic data types and their sizes in bytes (B).

Name

long

Type logical string integer integer integer integer

float

floating point

double

floating point

boolean char byte short int

Bits 1 16 8 16 32 64

Bytes 2 1 2 4 8

32 64

4 8

Range & Precision true or false ’\u0000’ ↔ ’\uFFFF’ (ISO Unicode) −128 ↔ +127 −32, 768 ↔ +32, 767 −2, 147, 483, 648 ↔ +2, 147, 483, 648 −9, 223, 372, 036, 854, 775, 808 ↔ 9, 223, 372, 036, 854, 775, 807 ±1.401298 × 10−45 ↔ ±3.402923 × 10+38 ±4.94065645841246544 × 10−324 ↔ ±1.7976931348623157 × 10+308

Table 10.2 The use of three bits a, b, and c, to represent the eight integers from 0 to 23 − 1 = 7.

Binary abc 000 001 010 011 100 101 110 111

= a × 22 0 0 0 0 1 1 1 1

+ b × 21 0 0 1 1 0 0 1 1

+ c × 20 0 1 0 1 0 1 0 1

= Decimal 0 1 2 3 4 5 6 7

computers use to store information. As you have seen in Area.java’s statements containing int and double, Java is specific about the types of variable or data with which it deals. Variables are stored as any of Java’s primitive data types, or as ones you create yourself. The primitive data types and the amount of memory they occupy are described in Table 10.1. These are rather standard for most modern computer languages. Variables of these types are stored in the computer’s memory in small blocks of memory locations called words. The amount of memory used to store a variable depends upon what you want to store in the variable, and one of the chores you have in programming is deciding how long you want your variables to be. So, now a word about words.

210

CHAPTER 10

10.2.1 Integers The most elementary unit of memory is a little magnet, like a compass needle that points up or down. If we associate “down” with 0 and “up” with number 1, then we have a physical device that stores representations of 0’s and 1’s. It should then be no surprise to hear that all numbers on the computer are ultimately represented in binary form, namely, in terms of the binary digits (abbreviated bits) 0 and 1. Just like the digits in the decimal system indicate the number of times that 100 , 101 , 102 , . . . , are each contained in a number, so it is with the binary system. As an instance, in Table 10.2 we see the three bits abc representing the eight numbers from 0 to 7. Likewise, N bits are used to represent integers up to 2N . In practice, since the first bit is used to represent the sign of the integer, we effectively lose one bit, which means that it is possible to represent integers only up to 2N −1 with N bits. Long strings of 0’s and 1’s are fine for computers but awkward for people. Consequently, binary strings are converted to octal, decimal, or hexadecimal numbers before results are communicated to people. Octal and hexadecimal numbers are fine, but our decimal rules of arithmetic do not work for them, and so they are hard for humans to work with. Converting to decimal numbers makes the numbers easier for us to work with but usually causes some loss in precision. The point of all this discussion about what goes on in the “guts” of memory is that in one way or another, you must tell Java how many bits you want to have assigned to store the value of each variable. The number of bits is called word length and is often expressed in bytes, where a byte is a “mouthful of bits”:

def

1 byte ≡ 1 B = 8 bits.

(10.5)

Conventionally, storage size is measured in bytes or kilobytes. For this reason, while one bit is adequate to represent a 0 or 1, two bytes are required to represent a single character, like the letter “a” or “b.” In practice, 8 bits or 1 byte are used to store an integer, in which case the integers lie in the range 1–27 or 1–128. More usually, 4 B = 32 bits are used for integers, which means that the maximum positive integer is 231  2 × 109 . In spite of this seeming to be a large range for numbers, it really is not compared to the range of sizes encountered in the physical world. To illustrate, the ratio of the size of the universe to the size of a proton is 1040 . Though Java integers have limited range, they are stored exactly on the computer if they are within this range.

211

Overflow

Overflow

truncation

DATA TYPES, LIMITS, METHODS; ROCKET GOLF

Underflow

0 −10

+38

−10

−45

10−45

+10+38

Figure 10.1 An illustration of the limits of single-precision floating point numbers and the consequences of exceeding those limits.

10.2.2 Floating-Point Numbers Scientific work primarily uses floating-point numbers. In floating-point notation, the number x is stored as a sign, a mantissa, and an exponent: xfloat = (−1)s × mantissa × 2exp .

(10.6)

Here the mantissa contains the significant figures of the number, s is the sign bit, and the exponent permits very large, as well as very small, numbers.1 To prove the point, a single-precision 32-bit word may allocate 8 bits of computer memory for the exponent in (10.6), which leaves 23 bits for the mantissa and 1 for the sign. This 8-bit integer “exponent” has the range [−127, 128]. In practical terms this means that single-precision (4-byte) numbers have 6–7 decimal places of precision (1 part in 223 ) and magnitudes typically in the range 2−149  10−45 ≤ single precision ≤ 2128  1038 .

(10.7)

These ranges are represented schematically in Figure 10.1. If you write a program requesting double precision, then 64-bit (8-byte) words will be used in place of the 32-bit (4-byte) words. With 11 bits used for the exponent and 52 for the mantissa, double-precision numbers have about 16 decimal places of precision and typically have magnitudes in the range 10−324 ≤ double precision ≤ 10308 .

(10.8)

1 In practice a number called the bias is subtracted from the exponent exp so that the stored exponent is always positive.

212

CHAPTER 10

10.2.3 Naming Convention We have already seen with the program Area.java being stored in the file of the same name, that the name of a class file must match the name of the class. It is also conventional to have the name of the class start with a Capital letter, as we did with Area.java. Regular variables in Java usually are written with lowercase letters. Even though a variable may start with a lowercase letter, it may contain capital letters within, such as hiMass and loMass. Variable names may contain alphanumeric (letters and numbers) characters and underscore characters ( ) but must start with a letter or an underscore. All names are case sensitive, so IloveYou and ILOVEYou are completely unrelated. We have also seen PI spelled with all capital letters. If a piece of variable does not change as the program runs, then it is a constant or a FINAL and is denoted by all capital letters. Finally, it is illegal to use as names those words that Java has reserved for itself:

abstract class extends import new short throws

boolean const final implements null static transient

break continue finally instanceof package super try

byte double float int private switch void

Reserved Words case catch char default do else for goto if interface long native protected public return synchronized this throw volatile while

Examples of acceptable names are: AlbertEinstein, alEinstein, F B I, Herr Prof Dr, Geo3rd

Examples of unacceptable (space, special character, reserved word, beginning number) names are: Al Einstein, Al*Einstein, final, 3rdGeo

10.2.4 Machine Precision One consequence of computers using the floating-point representation to store numbers is that most numbers are stored with only a limited precision. We see a schematic representation of this in Figure 10.1, where you are to imagine floatingpoint numbers being stored along the vertical hash marks. If you call for a number

213

DATA TYPES, LIMITS, METHODS; ROCKET GOLF

that lies between the hash mark, the computer will move you to the closest number. The corresponding loss of precision is called truncation error. The exact precision obtained in a calculation depends on the program. For a single operation, single precision usually yields 6–7 decimal places for a 32-bit word, and double precision 15–16 places. To understand how machine precision affects calculations, what would you guess is the result of the simple addition of two single-precision numbers: 7 + 2.0 × 10−8 ? (b) 9 × 10−8 ,

(a) 7.00000002,

(c) 7,

(d) 7.02

The correct answer is (c). Because there is no more room left to store the 2.0 × 10−8 , it is lost or truncated from the answer, and the addition gives 7. This loss of precision is categorized by defining the machine precision m as the maximum positive number that may be added to the number stored as 1 without changing the number stored as 1 on the computer: 1c + m = 1c .

(10.9)

Here the subscript c is a reminder that the variable is the number stored in the computer’s memory. Likewise, xc , the computer’s representation of an arbitrary number x, and the actual number x, are related by xc  x(1 + ),

Remember, m 

10−7

|| ≤ m .

for single precision and m 

10−16

(10.10) for double precision.

10.2.5 Under- and Overflows If a single-precision number x is larger than 2128 , an overflow occurs. If x is smaller than 2−149 , an underflow occurs. We visualize this in Figure 10.1 by observing that any number that tries to get closer to 0 than 10−45 in magnitude falls in the gray region at the center, and leads to underflow. Likewise, any number whose magnitude exceeds 10+38 falls in the gray region at the ends of the line, and leads to overflow. The number xc that the computer stores may end up being NAN (not a number), or a noncomputable infinity, or zero. Because underflow is a loss of information, a scientific programmer should be sensitive to its occurrence. Because the only difference between the representations of positive and negative numbers on the computer is the one sign bit for negative numbers, the same considerations hold for negative numbers. In our experience, serious scientific calculations almost always require double precision (8B). And if you need double precision in one part of your calculation, you probably need it all over, and that also means double-precision library

214 Listing 10.1 Limits.java

 1 2 3 4 5 6 7 8 9 10

11 12 13 14



CHAPTER 10

15

/∗ Limits . java : Determine machine p r e c i s i o n ∗ / public class Limits { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { f i n a l int N = 60; / / Declaration int i ; / / Declaration double eps = 1 . 0 , o n e P l u s E p s ; / / Declaration f o r ( i = 0 ; i < N; i = i +1) { eps = eps / 2 . ; one Plus Eps = 1.0 + eps ; System . o u t . p r i n t l n ( "one + eps = " + o n e P l u s E p s + ", eps = " +eps ) ; } } } / / End main , end c l a s s /∗ Output : one + e p s = 1 . 5 , e p s = 0 . 5 one + e p s = 1 . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 , e p s = 2 . 2 2 0 4 4 6 0 4 9 2 5 0 3 1 3 E−16 one + e p s = 1 . 0 , e p s = 1 . 1 1 0 2 2 3 0 2 4 6 2 5 1 5 6 5 E−16 ∗ /

routines. If you want to be a scientist or an engineer, learn to say “no” to singles and floats.

10.2.6 Experiment: Determine Your Machine’s Precision Listing 10.1 contains the small, but significant, program Limits.java for you to “test-drive” during your “break-in” period with Java. By repeatedly comparing 1 + m to 1 as m is made smaller, Limits.java determines the machine precision as the value of m for which the variable onePlusEps equals 1: 1. Enter Limits.java by hand into a file of the same name, compile it, and run it to determine the machine precision m for doubles. If you are going to save the output, or show it to someone else, it is good practice to edit the output file to eliminate the large number of noninformative lines. Once you understand how this program works, you may want to modify it so that it starts closer to the final answer, or so that it runs longer in order to get to the final answer. As is true for many of the programs we give you, they are meant as models for you to modify and extend. 2. Modify Limits.java to determine the machine precision m for singleprecision numbers (floats). To modify it correctly, you need to change the word double to float in the declaration statements, as well as change numbers like 1.0 to 1.0F throughout the program. The reason for the latter change is that Java automatically assumes that any decimal number is a double, yet by affixing the F you force Java to save the number as a float. 3. Underflow: Modify this program to determine the smallest positive single-



215

DATA TYPES, LIMITS, METHODS; ROCKET GOLF

Java a Class:

Golf

Class Variables b & Class C Constants

Java Class: (Golf.java)

Golf

Class Variables & Class

main

main method Uy( Up, theta, v)

method gamma(v)

Figure 10.2 Left: The structure of a Java class with only a main method. Right: The structure of a Java class with two methods.

4.

5. 6.

7.

precision numbers that Java handles. Hint: Continuous division of eps by 2 will determine the smallest number within a factor of 2. So, modify the line onePlusEps = 1.0 + eps to a division and increase the value of N so the program repeats for a longer time. Overflow: Modify this program to determine the largest positive singleprecision numbers that Java handles. Hint: Rather than divide by 2, multiply by 2. Determine the underflow and overflow limits (smallest and largest numbers) for double precision. Modify this program to determine the largest and most negative integers. Hint: Change eps to an integer (int) and keep subtracting 2 to determine the most negative integer; add 2 to determine the most positive integer. Determine Java’s value for 3(1/3) using double-precision values for 1 and 3, and then integer values for 1 and 3.

10.3 METHODS (FUNCTIONS) AND MODULAR PROGRAMMING On the left of Figure 10.2 we show a simple Java program. The class’s name is Golf and in it is the class definition and the main method. The only conceptual advance beyond the Area.java program of Chapter 9 is that we have included the definition of class variable in the box indicating the class definition public class Golf. Class variables are defined just like any other variable, except by doing it at the class level, they may be used by all methods. Variables defined within methods are local to that method. In some ways, having a small program like Area.java do all its computing

216

CHAPTER 10

in the main method makes sense. Because execution begins there, you may as well put all the work there so you do not have to look elsewhere to find it. As programs get called on to do more, they tend to get more complicated and harder to follow or modify. Then it makes sense to adopt a modular approach in which a program is divided into several relatively small subprograms, with each subprogram having its own separate function. In Java, the separate subprograms are separate methods and are usually placed within a class of related methods. Often each class has its own main method to begin execution, but that is not necessary. This collection of data (variables) and methods used to manipulate those data is the abstract definition of class. It is a great idea to save time and not repeat yourself, and so it is also possible for methods in one class to call methods from other classes. As an example, the mathematical functions, to be discussed shortly, are in the class named Math. We call the method sin in that class just by adding a prefix to the method name; for example, Math.sin. On the right of Figure 10.2 we show an example where we have enlarged the class Golf to now contain the methods Uy and gamma as well as main. The main method may call either of the other methods, and the other methods can call each other as well, but not main. Data is transferred in these calls by means of the argument list and the value of the method (input and output), as well as through the class variables, which may be shared by all methods. There are many good reasons for writing a program in which there are multiple methods with each method performing a single task (modular programming). This lets us build up into complexity with individual modules that are to be written and tested separately, reused in a number of programs, and in which the logic of the program is easy to follow. In good modular programming design, the main method is the control unit, or administrator, of the program, and it calls other methods to get the work done without doing any of the work itself. So you get a good idea of what an entire program does just by reading through its main method; the details are left to the other methods. Listing 10.2 Method.java

 1 2 3 4 5 6 7 8 9 10

// Method . j a v a : Example o f c a l l i n g a method p u b l i c c l a s s Method { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) / / Main method { double fVal ; int i ; f o r ( i =1 ; i = 0; i = i-1) System.out.println("i equals " + i);

13. A class consists of a main method with the statement y = f(x,n). The method f is defined just once, beginning with the line double f(double x, int y). Indicate whether each of the following is true or false: a. the main method must call f with variables named x and y ; b. the method f may change and return new values for x and n; c. the method f returns a value for f (y, n); d. the method f returns an int; e. the method f should have only one argument.

Chapter Eleven Visualization with Java, Classes, Packages

We have already seen in Maple how helpful visualizations are in understanding results and debugging the computation, and in adding some fun to your work. Fortunately, there is an excellent Java plotting package called PtPlot that is both free and easy to use, and in §11.1 we will show you how to use it.1 In order to use PtPlot, you will need to import a package and work with multiple classes. This is no worse than what we did in Maple. Not only will this use of PtPlot give you some beautiful graphs, it will also give you some experience with objects. Truly understanding Java’s packages, classes, and naming conventions is rather advanced for beginners, and so we will first just give you a sample program to modify, and then tell you about packages and such. We recommend that all users read §11.1, but maybe scan the other sections and treat them as reference materials for the present. In particular, §11.4 on gnuplot will be needed to create 3-D surfaces from data. 11.1 2-D GRAPHS WITHIN JAVA: PTPLOT One of the exceptional things about Java is that it contains commands that permit you to draw graphs on your computer screen—regardless of your operating system. Yet these commands are rather low level, as we shall see in Chapter 21, and you must attend to a number of details if you want to use them. To avoid all that fuss, we recommend the use of PtPlot [PtPlot] as a basic plotting package for 2-D graphs.2 PtPlot is free, supported by the University of California, written in Java (and thus runs under Unix, Linux, Mac OS, and MS Windows), is easy to use for 2-D data plotting, and produces attractive graphs.3 It may be incorporated right into your programs or applets (applets are discussed in Chapter 21), or used as a stand-alone application or applet. We include on the disk the PtPlot Java library; alternatively, you may download the most recent version over the Web. 1 PtPlot

is actually part of Ptolemy, an entire computing environment that is powerful, free, and Java-based. of this chapter were prepared with the help of Connelly Barnes. 3 For our research publications we use AceGr/Xmgr for 2-D plotting and gnuplot for 3-D surfaces. 2 Parts

VISUALIZATION WITH JAVA, CLASSES, PACKAGES

233

Figure 11.1 Sample output from advanced use of PtPlot in which two plots are placed side by side (see TwoPlotExample.java).

11.1.1 EasyPtPlot.java Listing 11.1 EasyPtPlot.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16



17

// EasyPtPlot . java : simple f ( x ) plot import p t o l e m y . p l o t . ∗ ; / / P l o t t i n g package public class EasyPtPlot { p u b l i c s t a t i c f i n a l d o u b l e Xmin = −5. , Xmax = 5 . ; / / y automatic public s t a t i c f i n a l i n t Npoint = 500; p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { P l o t p l o t O b j = new P l o t ( ) ; / / Create p t P l o t o b j e c t p l o t O b j . s e t T i t l e ( "f(x) vs x" ) ; p l o t O b j . s e t X L a b e l ( "x" ) ; p l o t O b j . s e t Y L a b e l ( "f(x)" ) ; / / Use : a d d P o i n t ( i n t d a t a S e t , d o u b l e x , d o u b l e y , b o o l e a n c o n n e c t ) d o u b l e x S t e p = ( Xmax − Xmin ) / N p o i n t ; f o r ( d o u b l e x = Xmin ; x javac EasyPtPlot.java > java EasyPtPlot

// Compile EasyPtplot.java // Run EasyPtplot.class

2. You should get a pretty window on your screen just like Figure 11.1. You may move it anywhere on your screen by grabbing the bar on top, and resize it by grabbing a corner and pulling. Try that! 4 More specifically, you are copying a subdirectory ptolemy, which, in turn, contains the subdirectory plot. The * is a wild-card character, and its use here means to copy all the files (class files) in that subdirectory. 5 Objects

are discussed in Chapters 16 and 18.

VISUALIZATION WITH JAVA, CLASSES, PACKAGES

235

Figure 11.2 Sample output from PtPlot in which three data sets are placed on one plot. Observe the error bars on two sets.

3. By grabbing a corner and pulling, make the window fill about one quarter of your screen. 4. Examine the Edit pull-down menu. Select Edit and pull it down. (Alternatively, the E indicates that this is possible with keystrokes alone. In this case, Alt + E also pulls down the menu.) 5. From the Edit pull-down menu select Format. You should get a window like that in Figure 11.3 that lets you control most of the options in your graph. 6. Experiment with the Format menu. In particular, change the graph so that only points are plotted, with the points being pixels and black, and so that your name is in the title. 7. Select a central portion of your plot and zoom in on it by drawing a box (with mouse button depressed) starting from the upper left corner and then moving down before you release the mouse button. You zoom out by drawing a box from the lower right corner and moving up. You may also resize your graph by selecting Special/Reset Axes or by resetting the x and y ranges. And of course, you always have the option of starting over by closing the Java window and running the java command again. 8. Scrutinize how you are able to print your graphs under the File menu, as well as write them to file in postscript (.ps format). You may also export plots in various formats.

236

CHAPTER 11

Figure 11.3 The Format submenu located under the Edit menu in a PtPlot application. This submenu controls the plot’s basic features.

11.1.3 PtPlot Options

Calling PtPlot from Your Program Plot myPlot = new Plot(); Name and create plot object myPlot PlotApplication app = new PlotApplication(myPlot); Display myPlot . setTitle(“f(x) vs x”); Add title to plot myPlot . setXLabel(“x”); Label x axis myPlot . setYLabel(“f(x)”); Label y axis myPlot . addPoint(0, x, y, true); Add (x, y) to set 0, connect points myPlot . addPoint(1, x, y , false); Add (x, y) to set 1, no connect points myPlot . addLegend(0, “Set 0”); Label data set 0 in legend myPlot . addPointWithErrorBars(0, x, y, yLo, yHi, true); Plot, (x, y - YLo), (x, y + yHi) + error bars myPlot . clear(0); Remove all points from data set 0 myPlot . clear(false); Remove data from all sets myPlot . clear(true); Remove all points, default options myPlot . setSize(500, 400); Set plot size in pixels (optional) myPlot . setXRange(-10., 10.); Set an x range (default fit to data) myPlot . setYRange(-8., 8.); Set a y range (default fit to data) myPlot . setXLog(true); Use log scale for x axis myPlot . setYLog(true); Use log scale for y axis myPlot . setGrid(false); Turn off the grid myPlot . setColor(false); Color in black and white myPlot . setButtons(true); Display zoom-to-fit button on plot myPlot . fillPlot(); Adjust x, y ranges to fit data myPlot . setImpulses(true, 0); Lines from points to x axis, set 0 myPlot . setMarksStyle(“none,” 0); Draw none, points, dots, various, pixels myPlot . setBars(true); Display data as bar charts String s = myPlot . getTitle(); Extract title (or other properties)

VISUALIZATION WITH JAVA, CLASSES, PACKAGES

237

The program TwoPlotExample.java and its data file data.plt, taken from the PtPlot Web site, shows how to place two plots side by side, and how to read in a data file containing error bars and various symbols for the points. Here we list some of the options available. More are to be found in the description of the commands (methods) on the PtPlot Web site [PtPlot]. At various times you may have your data in a file, possibly because it came from another application or because you wanted to get it all computed or measured before you started looking at it. If you write your data in a format that PtPlot recognizes, then you will be able to plot it and replot at your pleasure. This may sound like a lot of work, yet most reasonable formats will work. However, you have the option of including some PtPlot formatting commands in the data file. In the simplest form, a PtPlot Data Format is just a text file with a single x, y point per line. For example, Figure 11.2 was produced from a data file with the x and y values separated by spaces, tabs, or commas: Sample PtPlot Data file PtPlotdat.plt

 1 2 3 4 5 6 7 8



9



# T h i s i s a comment : Sample d a t a f o r P t P l o t T i t l e T e x t : Grade I n f l a t i o n XRange : 0 , 5 YRange : −5, 6 G r i d : on XLabel : Y e a r s i n C o l l e g e YLabel : GPA Marks : v a r i o u s NumSets : 3 C o l o r : on D a t a S e t : D a t a S e t 0 L i n e s : o f f 0 , −5.4 1 , −4.1 2 , −3.2 3 , −2.3 4 , −2 D a t a S e t : D a t a S e t 1 L i n e s : on 0 , − 3 . 6 , −4,−3 1 , − 2 . 7 , −3, −2.5 2 , − 1 . 8 , −2.4 , −1.5 3 , − 0 . 9 , −1.3 , −0.5 4 , 0 . 6 , 0 , 1 . 1 D a t a S e t : D a t a S e t 2 0 , 0 . 5 , −1 ,2 1 , 1 . 5 , 0 . 5 , 2 2 , 2 . 5 , 1 . 5 , 4 3 , 3.5 , 2.5 , 5 4 , 4.5 , 3 , 6

To plot up your data files, enter > java ptolemy.plot.PlotApplication dataFile

// Plot data in file dataFile

This causes the standard PtPlot window to open and display your data. If this does not work, then your CLASSPATH variable may not be defined properly, or PtPlot may not be installed. See “Installing PtPlot” in §C.5. Reading in your data from the PtPlot window is an alternative. Either use a window that is already open, or issue Java’s run command without a file name: > java ptolemy.plot.PlotApplication

// Open PtPlot window

To look at your data from the PtPlot window, choose File → Open → YourDataFile. By default, PtPlot will look for files with .plt or .xml suffixes. However, you may enter any name you want, or pull down the Filter menu and select * to see all of your files. The same holds for the File → SaveAs option. In addition, you may Export your plot as an Encapsulated PostScript (.eps) file, a format useful for inserting in documents.

238

CHAPTER 11

As with any good plot, you should label your axes, add a title, and customize other parts of your plots to make them informative and clear. To do these things, include PtPlot commands with your data, or work with the pull-down menus under Edit and Special in the PtPlot window. The options are essentially the same as ones you would call from your program. Here is a useful list:

TitleText: f(x) vs. x XLabel: x YLabel: y XRange: 0, 12 YRange: -3, 62 Marks: none Marks: points Lines: on/off Impulses: on/off Bars: on/off Bars: width (, offset) DataSet: string x, y move: x, y x, y, yLo, yHi

Add title to plot Label x axis Label y axis Set x range (default: fit to data) Set y range (default: fit to data) (Default) No marks at points, lines connects points or: dots, various, pixels Do not connect points with lines; default: on Lines down from points to x axis; default: off Bar graph (turn off lines) default: off Bar graph; bars of width and (optional) offset Specify data set to plot; string appears in legend Specify a data point; comma, space, tab separators Do not connect this point to previous Plot (x, y - yLo), (x, y + yHi) with error bars

If commands appear before DataSet directives, then the command will apply to all data sets. If commands appear after DataSet directives, then it will apply to that data set only.

11.2 INSTALLING PTPLOT: SEE APPENDIX C* 11.3 CLASSES AND PACKAGES* Up until this point your programs have contained a single class contained in a file with a .java or .class extension. That class contained a main method and possibly some other methods as well. Just as we encourage you to modify old programs rather than writing all programs from scratch, so we encourage you to include methods you and others have already written and debugged into your new programs. Java contains a large number of libraries consisting of collections of methods for various purposes, and you should think of these libraries as toolboxes from which you extract individual tools needed for your programs. In fact, the object-oriented approach of Java is specifically designed to make reuse of components easier and safer.

VISUALIZATION WITH JAVA, CLASSES, PACKAGES

239

Table 11.1 Some of Java’s packages and the classes they contain.

Java Package java.lang java.util java.awt java.applet java.beans java.io

Classes for Basic elements of Java language Utilities; random-number generators, date, time, etc. Abstract Windowing Toolkit; creates graphical interfaces Creates applets and interacts with browsers Creates reusable software components Data input and output

11.3.1 Including Packages* We have already referred to a collection of related methods as a class and, for a broader collection, as a library (like the Math library). In strict Java naming convention, each method would be in a class file, and the libraries would be called packages. In general, there are two types of packages: the standard Java packages that constitute the Java language, and user-defined packages that extend standard Java. The PtPlot package is an example of a user-defined package. Some of the standard Java packages are given in Table 11.1. Because these Java packages contain hundreds or thousands of classes, some organization is necessary to keep track of what each method does. Java does this with a hierarchical directory structure in which there are parent packages containing subpackages, with the subpackages containing more subpackages or various classes. To make the name of each class unique, one precedes it with the names of the package and subpackages that contain this class. As an example, consider the command System.out.println

that we have been using to print a line of output on the screen. Here System is the name of the class (classes begin with capital letters) containing many of Java’s methods. When we give the combination System.out, we are referring to an object representing the standard output stream. The final println that gets affixed to System.out is the name of the method that prints lines. To summarize, by convention, the names of classes are capitalized, while the names of packages and methods are lowercase. This is relevant here because the System.out.println command is in the java.lang package, and so the proper full name of the command is actually java.lang.System.out.println

which contains the package name as well. Because java.lang is the most ba-

240

CHAPTER 11

sic package, the java compiler automatically looks there to find the methods we invoke. This means we can, fortunately, leave off the java.lang prefix. Technically, java is the main package and lang is a subpackage, but it is easier to just say java.lang is the package. We must admit that we sometimes find Java’s naming conventions overwhelming. Nevertheless, we will use them when importing packages and in using methods from other classes, so some familiarity is helpful. You include the classes from a package with the Import command. It may be given in one of two forms: import . import .*

// Import specific classes from packageName // Import all classes from packageName

The import command tells the Java compiler to look in the package packageName for methods that it might not find otherwise. However, for the importation to work the compiler must know where the package of classes and their methods are stored on your particular computer. In other words, the compiler needs to know the path to follow through the local disk memory to get to the directory or folder where the classes are stored. For this purpose each computer system, be it Windows, Unix, or Mac OS, has an environmental variable named CLASSPATH that contains the explicit path to where the classes are stored on that particular computer. As we show in §C.5 on installing PtPlot, you will need to modify this variable before a package is imported. Even though what follows is more advanced programming than we do in this book, for completeness we indicate how you could create your own packages. This is done by placing several classes in the same .java file and then including a package command at the beginning of the file:

 1 2 3 4



5

package < mypackage name >; public c l a s s < myclass1 name > { < n o r m a l c l a s s s t r u c t u r e , m u l t i p l e m e t h o d s OK > } p u b l i c c l a s s { < n o r m a l c l a s s s t r u c t u r e , m u l t i p l e m e t h o d s OK > }

Your package may be a collection of methods without any main method, for example, mathematical subroutines that are called from all the programs you write. However, there must be one main method someplace if the program is to run, since execution always begins in a main method. Likewise, the main method must be a public class (other classes are read only the public classes). 11.4 GNUPLOT BASICS Gnuplot is a versatile 2-D and 3-D graphing package that makes Cartesian, polar, surface, and contour plots. In spite of PtPlot being fine for 2-D plotting with Java,



241

VISUALIZATION WITH JAVA, CLASSES, PACKAGES 30 ' dat a. gr aph' ' 2. dat ' ' 3. dat '

20 10 0 - 10 - 20 - 30

-6

-4

-2

0

2

4

6

Figure 11.4 A gnuplot graph for three data sets with impulses and lines.

we still have a need for gnuplot’s surface plots of numerical data. Gnuplot is open software, available for free on the Web, and produces many output formats. You begin gnuplot with a file of (x y) data points, say, graph.dat. You then issue the gnuplot command from a shell or from the Start menu. You then get a new window with the gnuplot prompt gnuplot>.You construct your graph by entering gnuplot commands at the gnuplot prompt or by using the pull-down menus in the gnuplot window: // Start gnuplot program Type of terminal for Unix

> gnuplot Terminal type set to ’x11’

The gnuplot prompt

gnuplot> gnuplot>

plot "graph.dat"

Plot data file graph.dat

In Figure 11.4 we plot a number of graphs on the same plot using several data files and the command: gnuplot>

plot ’graph.dat’ with impulses, ’2.dat’, ’3.dat’ with lines

The general form of the 2-D plotting command is: plot {ranges} function {title} {style} {, function ...}

with points with lines with linespoint with impulses with dots

Default. Plot symbol at each point Plot lines connecting the points Plot lines and symbols Plot vertical lines from x axis to points Plot small dot at each point (scatter plots)

It is necessary to place all file or directory names in single or double quotes and to separate file names with a comma. Explicit values for the x and y ranges are set with the plot options: gnuplot> plot [xmin:xmax] [ymin:ymax] "file"

Generic

242

CHAPTER 11 Im T(E)

30 15 0 -15 -30

-100 -80 -60 Im E (Me V)

-40

1600 1500

-20 0

1300

1400 Re E (MeV)

Figure 11.5 Gnuplot’s surface plot of a scattering amplitude ImT as a function of complex energy E.

gnuplot> plot [-10:10] [-5:30] "graph.dat"

Explicit

11.4.1 Printing Plots Gnuplot supports a number of printers including PostScript ones. The basic method of printing plots is: 1. 2. 3. 4. 5.

Set “terminal type” for your printer. Send the plot output to a file. Replot figure for new output device. Quit gnuplot (or get out of gnuplot window). Print file.

You could also import the file into a word processor to have it appear in a document or into a drawing program to fix it up just right. To see what type of printers and other output devices are supported by gnuplot, enter the set terminal command without any options into a gnu window for a listing of available terminal types. Here is an example of creating a PostScript figure and printing it: gnuplot> set terminal postscript Terminal type set to ’postscript’ gnuplot> set term postscript eps gnuplot> set output "plt.ps" gnuplot> replot gnuplot> quit % lp plt.ps

Choose local printer type Gnuplot response Another option Send figure to file Plot again so file is sent Or get out of gnu window Unix print command

VISUALIZATION WITH JAVA, CLASSES, PACKAGES

243

11.4.2 Gnuplot Surface (3-D) Plots The surface (3-D) plot command is splot, and it is used in the same manner as plot—with the obvious extension from (x, y) to (x, y, z). As discussed in Chapter 4, a surface, such as that in Figure 11.5, is specified by giving just the z values for successive rows, with different rows separated by blank lines: Data for 3-D plot

-4.043764 ... -11.000000

Because there are no explicit x and y values given, gnuplot labels the x and y axes with the row and column number. At present gnuplot does not have the capability to rotate 3-D plots interactively. Instead, you adjust your plot with the command: gnuplot>

set view rotx, rotz, scale, scalez

where 0 ≤ rotx ≤ 180o and 0 ≤ rotz ≤ 360o are angles in degrees, and the scale factors control the size. Any changes made to a plot are made when you redraw the plot using the replot command. To see how this all works, here we give a sample gnuplot session that we will use in Chapter 20 to plot a 3-D surface from numerical data. Listing 20.1 contains the actual code used to output data in the form for a gnuplot surface plot. > gnuplot

Start gnuplot system from a shell

Then, a special gnuplot shell starts up with the prompt gnuplot>, and you are ready to give gnuplot your subcommands.6 gnuplot> set hidden3d gnuplot> set nohidden3d gnuplot> splot ’Laplace.dat’ with lines gnuplot> set view 65,45 gnuplot> replot gnuplot> set contour

Hide surface whose view is blocked Show surface though hidden from view Surface plot of Laplace.dat with lines Set x and y rotation viewing angles See effect of your change Project contours onto x-y plane

gnuplot> set cntrparam levels 10

10 contour levels

gnuplot> set terminal PostScript

Output in PostScript format for printing

gnuplot> set output "Laplace.ps"

Plot output to be sent to file Laplace.ps

gnuplot> splot ’Laplace.dat’ w l

Plot again, output to file

gnuplot> set terminal x11 gnuplot> set title ’Potential V(x,y) vs x,y’

To see output on screen again Title graph

gnuplot> set xlabel ’x Position’

Label x axis

gnuplot> set ylabel ’y Position’

Label y axis

gnuplot> set zlabel ’V(x,y)’; replot

Label z axis and replot

6 Under Windows, there is a graphical interface that is friendlier than the gnuplot subcommands. The subcommand approach we indicate here is reliable and universal.

244

CHAPTER 11 Tell me more

gnuplot> help gnuplot> set nosurface

Do not draw surface, leave contours Look down directly onto base

gnuplot> set view 0, 0, 1 gnuplot> replot

Draw plot again; may want to write to file Get out of gnuplot

gnuplot> quit

For this sample session, the default output for your graph is your terminal screen. To print a paper copy of your graph, we recommend first saving it to a file as a PostScript document (suffix .ps), and then printing out that file to a PostScript printer. You create the PostScript file by changing the terminal type to Postscript, setting the name of the file, and then issuing the subcommand splot again. This plots the result out to a file. If you want to see plots on your screen again, you need to set the terminal type to x11 again (for Unix’s X Windows System), and then plot it again. 11.5 JAVA ARCHIVES: JAR* The standard Java Development Kit provides an archive tool jar that permits you to compress and decompress files using the zip file format. Even if you do not feel the urge at this moment to save disk space by using it, jar also lets you create an archive of all your work saved in the directory structure you have already created, and then unpack the archive into a directory with the same structure. This permits you to efficiently and safely save and transfer entire directory structures from one place on the computer to another, or from one computer to another, without worrying about leaving out some important files. What is more, since it is Java, it will work on all computers. The basics operations with jar, made from the command line, are [SunJ]: > jar cf archive file1 file2 ... > jar cvf archive file1 file2 ...

Create archive Archive.jar A verbose creation Archive everything into Archive.jar

> jar cf archive * > jar tf archive.jar

View table contents of file Archive.jar

> jar xf archive.jar

Extract contents of a file Archive.jar Extract file1, file2

> jar xf archive.jar file1 file2 > java -jar archive.jar

Run application in Archive.jar



Run applet in Archive.jar

Here the options to the jar command follow it directly, with several options concatenated (placed end to end). The c option indicates that you want to create a jar file. The f option indicates that you want to use the file that follows the f (rather than the standard input or output). The t option indicates that you want a table of contents. The x option indicates that you want to extract some files. The v option may also be included with any of these commands to obtain a verbose version of

245

VISUALIZATION WITH JAVA, CLASSES, PACKAGES

the action in which jar tells you what it is doing as it does it (we recommend that). Check too that we have followed the convention of using a .jar extender to jar file names, though this is not required. We have indicated the input files (those to be archived) explicitly as file1 and file2. You are also permitted to use the wild-card symbol * to archive all the files and subdirectories in the current directory. A particularly valuable feature of jar is that if any of the indicated files are directories, then the entire contents of those directories are added to the archive recursively. In turn, when archived directories are extracted, the entire directory and subdirectory structures are extracted. Here we give an example in which we issue the jar command from a directory containing the files EasyPtPlot.class and OOPlanet.java, and the directory Plot, that itself contains a whole bunch of files. We create the archive All.jar: > jar cf All.jar *

Create archive All.jar with everything

Whereas there is no indication that this was successful, we check what is in All.jar by looking at its table of contents: > jar tf All.jar EasyPtPlot.class

Produce table of contents Class file Source File

OOPlanet.java

Directory

plot/ plot/Plot1.class

File in working directory

plot/Plot2.class

If we ask for a verbose form of creation, we would see a list of the file names as they are added to the archive: > jar cvf All.jar * added manifest

Verbose archive creation

adding:

EasyPtPlot.class(in = 807) (out = 563)(deflated 30%)

adding:

OOPlanet.java(in = 3141) (out = 1008)(deflated 67%)

adding:

plot/(in = 0) (out= 0)(stored 0%)

adding:

plot/Plot1.class(in = 687) (out = 436)(deflated 36%)

adding:

plot/Plot2.class(in = 782) (out = 474)(deflated 39%)

We see that we also get an indication of how much compression is made for the archived files. The source code is deflated by roughly 2/3, the compiled code by roughly 1/3. Extraction of a single file or of the entire archive is simple: > jar xvf All.jar OOPlanet.java extracted: OOPlanet.java > jar xvf All.jar * extracted: EasyPtPlot.class extracted:

OOPlanet.java

Extract OOPlanet.java jar’s response Extract everything in All.jar

246 created:

CHAPTER 11 plot/

extracted:

plot/Plot1.class

extracted:

plot/Plot2.class

Creates directory to place files

Chapter Twelve Flow Control via Logic; Projectiles

In this chapter we begin using some of the control structures that make programming so interesting and that make your programs so intelligent. We will have you write your own program to simulate projectile motion, a standard problem of elementary physics. In Chapter 15, “Differential Equations with Java and Maple” we solve this same problem including air resistance. We start with a review of the kinematic equations, which may be scanned by those familiar with it, and then go to describe program design and control structures. 12.1 PROBLEM: FRICTIONLESS PROJECTILE MOTION Figure 12.1 is the result of a computer simulations showing the trajectory of a projectile fired from a cannon at time t = 0, with initial velocity V0 , at an angle θ relative to the horizon. The projectile remains in the air for a total hang time of T and hits the ground a horizontal distance or range x = R away from the origin. Problem: write a program to simulate the motion of this projectile. Have it: 1. store the initial values V0 , g , and θ as constants; 2. compute the “hang time” T from the equations of kinematics; 3. compute the range R and height H from the equations of kinematics;

P ro je c tile w ith o u t/w ith Air R e s is ta n c e 8 4

yy

2

V0

0 -2 -4 -6

(x, y)

0

6

Voy

θ

5

Vo

4 t=T

t=0 0

y

10

15

x

20

x

25

30

35

40

45

q Vox x

Figure 12.1 Left: The trajectory of a projectile fired with initial velocity V0 in the θ direction. The nonparabolic curve includes air resistance. Right: The x and y components of V0 .

248

CHAPTER 12

4. compute the position (x, y) of the projectile for 100 times starting at −T /2 and running in uniform steps to 3T /2. 12.2 THEORY: KINEMATICS If we ignore air resistance, a projectile has only the force of gravity acting on it. This force causes the projectile to fall towards the center of the earth with a constant acceleration g = 9.8m/s2 . The resulting solutions to the equations of motion are x(t) = V0x t, y(t) = V0y t − 12 gt2 . (12.1) Here V0x and V0y are the horizontal and vertical components of the initial velocity, and, as we see from Figure 12.1, V0x = V0 cos θ,

V0y = V0 sin θ.

(12.2)

Likewise, the x and y components of the velocity as functions of time are: vx (t) = V0x ,

vy (t) = V0y − gt.

(12.3)

Although the equations of motion yield x and y as functions of time, they do not tell us the shape of the trajectory. For these simple equations it is easy to derive that the shape is a parabola. We will, instead, determine the shape by plotting up the solution, a simpler approach as the equations become more realistic. We determine the hang time T by observing that at the time of firing t = 0, and landing t = T , the projectile has a height y = 0. If we set y = 0 in (12.1), we obtain an equation for the time at which the height is zero:



y(t) = V0y t − 12 gt2 = 0, ⇒ 2V0y T = t= , g

V0y t = 12 gt2

(12.4) (12.5)

where we ignore the t = 0 solution. Once we know T we find the range R as the horizontal distance x(T ): R = x(T ) = V0x

2V0y 2V 2 sin θ cos θ = 0 . g g

(12.6)

The projectile reaches its maximum height H at the midpoint of the trajectory at time t = T /2 = V0y /g : 

V0y 1 V0y T − 2g H = y( ) = V0y 2 g g 2 2 V sin θ ⇒ H= 0 . 2g

2

(12.7) (12.8)

FLOW CONTROL VIA LOGIC; PROJECTILES

249

12.3 COMPUTER SCIENCE: DESIGNING STRUCTURED PROGRAMS Now that you are into the program construction business, it is a good idea to understand not only the grammar of the language you are writing in, but also the general structure that you should be building into your programs. Books have been written on program design, but then again, it is a good idea not to believe everything you read! Yet, as seems to be true in much of life, it is helpful to follow the rules until you know better. Those who have truly mastered a subject may go on and make new rules. We view programming as a written art that blends elements of science, mathematics, and computer science into a set of instructions to permit a computer to accomplish a scientific goal. Good programs should: • • • • • •

give the correct answers; be clear and easy to read, with the action of each part easy to analyze; document themselves for the sake of readers and the programmer; be easy to use; be easy to modify and robust enough to keep giving the right answers; be passed on to others to use and develop further.

One way to make your programs clearer is to structure them. You may have already noticed that sometimes we show our coding examples with indentation, skipped lines, and braces placed strategically. This is done to provide visual clues as to the function of the different program parts (the “structures” in structured programming). In spite of the Java compiler ignoring these visual clues, human readers are aided in understanding and modifying the program by having it arranged in a manner that not only looks good, but also makes the different parts of the program manifest to the eye. The placement of text on the lines and the skipping of lines to make the purpose of the programming clearer is known as structured programming. We recommend it highly. It is a valuable approach when dealing with control structures, and particularly so when dealing with one control structure nested within another. The basic idea is simple: blocks of code performing specified tasks should be indented and physically isolated to set them apart. Even though, in order to conserve printed space, we do not skip as many lines as we would like to, we recommend that you do!

12.3.1 Flowcharts and Pseudocode In Figure 12.2 we present a flowchart that illustrates a possible program to compute projectile motion. A flowchart is not meant to be a detailed description of a program, but rather a graphical aide to help visualize its logical flow. As such, it is independent of computer language and is useful for developing and understanding the basic structure of a program. We recommend that you draw some type of

250

CHAPTER 12 Store g, V0, θ

Calculate R, T

Initialize Constants Loop over time

Basic Calculations

Y

0 H . e. End loop over time. 4. End program. One way of viewing program design is in terms of data flow. Your program starts with some input data, decides what to do with it, does it, and then outputs the results. Usually boxes in flowcharts like Figure 12.3 show actions, while elongated or angled boxes show decisions to be made. The direction of logical flow of the program is shown by the arrows, with flow moving down, except when arrows redirect it. For our projectile problem, we need to calculate the position for 100 different times. The easiest way to do this is to have the same block of code repeated

251

FLOW CONTROL VIA LOGIC; PROJECTILES do

action 1

then

action 2

and then

action 3

T then action 1

Figure 12.3 Left: Sequential or linear programming. Right: several used in nonlinear programming.

if true

F else action 2

The if-then-else structure, one of

100 times, with only the value of the time t changing. Such a structure is called a loop. Like a clock, it will run for a total time −T 3 = 2T, Ttotal = Tf − Ti = T − 2 2

(12.9)

in steps (ticks) of

2T T = . 100 50 Whence, the time increases by ∆t for each repetition of the loop: ∆t =

t = t + ∆t,

(12.10)

(12.11)

with the program using its intelligence to decide what to compute and print out depending upon the particular value of t. 12.4 FLOW CONTROL VIA LOGIC One of the most satisfying aspects of programming is getting the computer to do just what you want. Getting it to do what you want is sometimes direct, as when you evaluate functions in a “calculator” mode, and sometimes subtle, as when you try to make the computer act intelligently and “think” logically about what it should do next. Making a program think logically is done by including symbolic logic with Boolean variables. A Boolean variable is a primitive (built-in) Java data type that is either true or false. A Boolean expression is a combination of variables containing logical operators that evaluate to either true or false. When Boolean expressions are used with control structures in your program, you are able to affect the order in which statements are executed, as well as which statements actually get executed.

12.4.1 Relational and Logical Operators Conditional operators act between two variables or expressions to form a Boolean expression that is either true or false. There are two types of conditional operators, relational operators and logical operators. Relational operators start with variables like double x and double y, and create a logical relationship between

252

CHAPTER 12

Table 12.1 Relational operators of Java.

Operator > >= < y x >= y x y is true if the numerical value of x is greater than the numerical value of y. These relational operators are never used in assignment statements. Indeed, for this reason, we introduce the double equal sign == as a relational operator; x = y is an assignment statement, while x == y is a Boolean variable. Logical operators combine Boolean variables to create more complex expressions, for example, (x > 3) & (y > 4). Table 12.2 shows Java’s logical operators, examples of their use, their logical names, and the conditions under which they evaluate to true results. The basic logic behind logical operators is that if we have a compound statement constructed from two simpler statements, then the truth table, Table 12.3, determines the truth of the compound statement. We see in the truth table that the logical and is represented by both & and &&, and that the logical or is represented by both | and ||. Either form will work for you. The difference is that the single operator form, such as x | y, always evaluates y even if the result is foretold just

253

FLOW CONTROL VIA LOGIC; PROJECTILES

Table 12.3 Truth table.

True true and true true or true true or false !false

False true and false false and false false or false !true

by evaluating the first variable x. As a case in point, if x is true, then x | y is true regardless of y. The double operator form evaluates y only if the answer is not foretold by evaluating just x: 1 > 4 (1 > 4) | (3 > 2) (1 == 4) & (2 < 1) (1 != 3) & (2 !> 4)

false true false true

Believe it or not, what makes the use of logical expressions so powerful is that it is legitimate to combine the different logical expressions to describe the basis for any logical decision. Exercise: If a is your age in years, w your weight in pounds, and h your height in inches, construct Boolean expressions that will be true only when the following statements are true: 1. you are old enough to obtain a driver’s license but too young to retire, ( (a > 16) & (a < 65)); 2. you are not a teenager; 3. you are either younger than 20 and less than 150 pounds, or you are older than 40 and more than 6 feet; 4. you are neither old enough to vote, tall enough to hit your head on a five-foot door frame, nor heavy enough to box in the 132–140 pound division. ♠

12.4.2 Control Structures The combination of Boolean expressions and control structures permits you to construct programs that make decisions based on the data at hand. Table 12.4 contains a complete list of the control structures in Java. The type of logic that is possible with these Boolean expressions is shown in the flowcharts of Figures 12.3– 12.5. If you look at both the command in the table and the action in the figure together, you should get a good idea of how the command works.

254

CHAPTER 12

Table 12.4 Logical flow control structures in Java. Name if-then if-then-else if-then-else-if

for do

do while switch

Example

Comment

if ( (x < 3) && (y == 12) ) z = y*x; if (x = 90) {grade=’A’;} else if (score >= 80) {grade=’B’;} else if (score >= 70) {grade= ’C’;} for (count=0; count 0.001. Be that as it may, be warned, round-off error makes it unreliable to demand true equality between two floating-point numbers, or between a float and an integer. By way of example, an ill-advised condition might occur in the for loop starting with for ( double x = 0.; x != 100; x = x + 1.

)

// May never be satisfied

where x may never be precisely equal to 100, and so this loop may never end!

1 Hoping not to confuse the reader, we note that the Do loop in Fortran executes the action after the test, and is thus equivalent to Java’s for loop, not the do loop.

257

FLOW CONTROL VIA LOGIC; PROJECTILES

Table 12.5 Java’s increment and decrement operators.

Shorthand i++ i-++i --i

Equivalent Expression i=i+1 i=i-1 i=i+1 i=i-1

Note Evaluate i before increment Evaluate i before decrement Evaluate i after increment Evaluate i after decrement

To make sure that the loop will eventually stop, it is better to use the greater-than and less-than operators. As case in point, we can always satisfy for ( double x = 0.; x 0. Modify the program so that it will work for negative x, and check your results for x = −0.5, −0.1, and −0.9.



Chapter Thirteen Java Input and Output*

Some of the computing in the chapters to follow is made easier by being able to write output to files, and being able to read from the keyboard or from a file. Even though we will give you the commands needed to do that in those chapters, we will not explain just how they go about doing it. In this optional chapter we do explain some of the theory and details of Java I/O, for those who care to or need to know about them. As we have indicated before, we consider the difficulty of Java’s I/O a major weakness of the language for scientific computing, a weakness that is being overcome as this book goes to press. Part of this difficulty arises from the increased security needed to be able to use Java over the Web, and part from Java being designed more for graphical output than text. Already Java 1.5 has added scanner, a version of scanf, and we suspect that printf will follow shortly [Jgran]. You have seen in Chapter 9, “Getting Started with Java,” that we printed out a line of output with the basic Java command: System.out.println("R = " + r + ", A = " +A);

Even though this seems rather ungainly for such a simple operation, we advised you to just use it and not worry about its form or meaning. As we get on with more serious computing, it would be nice to have some more control over the input and output (I/O) within our programs. In particular, it is useful to enter input from the keyboard or from a file and to place our output into files that are stored for later use. In the first part of this chapter we discuss the standard I/O streams of Java and how to use them to read and write files. Rather than give all details of the theory of I/O, we primarily give examples of how these things are done, with little discussion of the various steps that Java must undertake to get them done. In the second part of this chapter we give examples of the use of a freely available package that provides convenient reading and writing of formatted data. That package contains Java versions of the well known C-language printf and scanf commands.

263

JAVA INPUT AND OUTPUT*

13.1 BASIC INPUT WITH SCANNER As this book was going to press, Sun announced their latest version of Java, j2se/1.5.0. It contains the simple scanner command that reads input and converts (parses) it into primitive data types and strings, without the user having to deal with the complication of buffers. While its introduction was too late for us to include in our sample codes, in Listing 13.1 we give ScannerIn.java containing examples of its use with various data types. Listing 13.1 ScannerIn.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21



22

// S c a n n e r I n : J a v a 1.5+ i n p u t v i a S c a n n e r command import j a v a . i o . ∗ ; / / Import I /O l i b r a r y import j a v a . u t i l . ∗ ; / / Import u t i l i t i e s public class ScannerIn { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) { d o u b l e r , A; / / Connect Scanner to standard i n p u t ( o t h e r a l s o p o s s i b l e ) S c a n n e r s c = new S c a n n e r ( System . i n ) ; System . o u t . p r i n t l n ( "Enter your name & r on 1 or more lines" ) ; S t r i n g name = s c . n e x t ( ) ; / / Read S t r i n g r = sc . nextDouble ( ) ; / / Read d o u b l e System . o u t . p r i n t l n ( "Hi "+name ) ; System . o u t . p r i n t l n ( "radius = "+ r ) ; System . o u t . p r i n t l n ( "Key in your age as integer " ) ; i n t age = sc . n e x t I n t ( ) ; / / Read i n t System . o u t . p r i n t l n ( a g e +"years old, you don’t look it!" ) ; sc . close ( ) ; / / Close i n p u t A = Math . P I ∗ r ∗ r ; System . o u t . p r i n t l n ( "Area = "+A) ; }}

13.2 STREAMS: STANDARD OUTPUT, INPUT, AND ERROR The most basic I/O in Java is that which comes packaged as part of the Java “system.” It consists of System.in System.out System.err

standard input stream (keyboard) standard output stream (monitor screen) standard error stream (varies)

Here the word stream represents a one-way path connecting your program and a device, such as a file. You may issue system-level commands that redirect where the input comes from and where the output goes, but unless you do so, the default input is the keyboard and the default output is the screen. To prove the point, when in Listing 13.2:



264

CHAPTER 13

Table 13.1 Control characters for formatting output.

Control Character \n \r

Effect new line return (no line feed)

\b \t

Effect backspace tab

Listing 13.2 Area2.java

 1 2 3 4 5 6 7 8 9



Control Character

10

p u b l i c c l a s s Area2 { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) / / B e g i n main method { d o u b l e r , C , A, P I = Math . P I ; / / Declare ( i n s t a n t s ) , assign r = 1.; / / Assign r C = 2 .∗ PI ∗ r ; / / Calculate C A = r ∗ r ∗ PI ; / / Calculate A System . o u t . p r i n t l n ( "R = " + r ) ; / / Print r System . o u t . p r i n t l n ( "C = " + C) ; / / Print C System . o u t . p r i n t l n ( "\n R = " + r +", A = " + A) ; } } / / End main and c l a s s

we issue the System.out.println command, we actually are using the println method to send a line of data to be printed on the standard output stream System.out. The method is in the out classes that are part of Java’s System. The original version of Java (1.0) was written so that it transfers data one byte at a time. Java 1.1 and beyond permit I/O using characters as well. Both systems are still part of Java, and commands from each tend to get intermixed. Though you as a programmer are free to go through all the effort of converting your input and output to byte-size chucks, use of the println method does this for you. However, println takes only a single string as its argument, which is why we use the + operator to convert doubles and ints to strings that are then concatenated into a single, long string. Here are a few examples:

13.2.1 Screen Output; Area2.java For beginning work we usually output to the screen. In Listing 13.2 we have a modified version of our old friend Area. It produces the output:

Area2.java,

R = 1.0 C = 6.283185307179586 R = 1.0, A = 3.141592653589793

Survey some new things here. First, how the third println command includes a



265

JAVA INPUT AND OUTPUT*

Table 13.2 String to primitive type conversion methods.

Conversion Method Double.parseDouble(String str) Double.valueOf(String str).doubleValue() Integer.parseInt(String str) Integer.valueOf(String str).integerValue() Long.parseLong(String str) Long.valueOf(String str).longValue() Float.parseFloat(String str) Float.valueOf(String str).floatValue() Boolean.valueOf(String str).booleanValue()

Converts to double double int int long long float float boolean

\n

control character before the symbol R. This causes a “new” or blank line to be inserted in the output, in this case, before the symbol R is printed. (The indentation occurs because there is a blank space after the \n.) Other control characters that you may use to control the format of the output are given in Table 13.1. Another new thing in Area2 is our printing both r and A on the same line: System.out.println("\n R = " + r +", A = " + A);

We did this by the concatenation of a string + data conversion + string + data conversion. Some care is needed here in keeping track of where the double quotes begin and end, although you may rest assured that Java will tell you when you do not get it quite right!

13.2.2 Keyboard Input, Screen Output The keyboard is the standard stream for input, much as the screen is the standard stream for output. We may accomplish all the conversions and transfers needed for output with a single command like println, yet an additional step is needed for input, because we must both transfer a stream of bytes into our program and then convert these bytes into the appropriate data type (such as double, int, or string). Java provides the methods to look after the details, but you must tell Java what it is you want done. In fact, there are a number of different methods to accomplish this task, as you shall see in the examples. Data read into your program enters as a long string. If you want to assign that long string to a string variable, then no conversion is necessary. If you want to convert that string into double, int, or boolean data types, then you may use the type conversion methods indicated in Table 13.2 [Chap 04, Bron 03, Dav 99]. Observe how the first part of the method name is actually the name of the class to

266

CHAPTER 13

which it belongs, while the second part, after the decimal point, is the method’s proper name. So, for example, when we say Double.parseDouble(String str)

the method name is parseDouble, and it is contained in the class Double. This agrees with our convention that class names are capitalized, while method names are not. The method’s argument is in the parentheses, (String str), with String indicating the data type. In contrast to primitive data types, which we declare with lowercase key words like double, Strings are variable-length objects that we create, and are therefore declared with the capitalized String. Before we lose you with all the talk, look at ReadStnd.java in Listing 13.3: Listing 13.3 ReadStnd.java

 1 2 3 4 5 6 7

8 9 10 11 12 13 14 15



16

// ReadStnd : standard ( keyboard ) i n p u t stream import j a v a . i o . ∗ ; / / Import I /O l i b r a r y p u b l i c c l a s s ReadStnd { p u b l i c s t a t i c f i n a l d o u b l e P I = Math . P I ; / / Constants p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) throws I O E x c e p t i o n / / main { d o u b l e r , A; / / throws exception B u f f e r e d R e a d e r b = new B u f f e r e d R e a d e r ( new I n p u t S t r e a m R e a d e r ( System . i n ) ) ; System . o u t . p r i n t l n ( "Please enter your name" ) ; / / Input // Read & p r i n t 1 s t l i n e ; r e a d & c o n v e r t 2 nd l i n e t o d o u b l e S t r i n g name = b . r e a d L i n e ( ) ; System . o u t . p r i n t l n ( "Hi "+name+" \n Please enter r" ) ; S t r i n g rS = b . r e a d L i n e ( ) ; r = Double . v a l u e O f ( r S ) . d o u b l e V a l u e ( ) ; A = PI ∗ r ∗ r ; / / The c o m p u t a t i o n System . o u t . p r i n t l n ( "\n Bye "+name+"\t R= " + r +", A=" +A) ; } }

This code uses the standard input stream (keyboard) and produces the output: Please type in your name Loren B Hi Loren B Please enter r 10 Bye Loren B R = 10.0, A = 314.1592653589793

This example also illustrates a step in the input process known as buffering. As a general rule, you do not want your program (software) to interact directly with an I/O device (hardware), as then you may have your fast program waiting for some slow hardware. On these grounds, the data in the input stream gets placed in a buffer where it accumulates until there is a full line ready for transmission to your program. So it follows that, like a shock absorber on an automobile smoothing out



267

JAVA INPUT AND OUTPUT*

the ride, a buffer in the I/O streams smooths out the data flow. To include a buffer in the program, on line 7 we have the command BufferedReader b = new BufferedReader(new InputStreamReader(system.in));

This means that the standard java.io package creates BufferedReader objects to buffer the data in the input stream. The method readLine() used on line 10 reads the line in the buffer and returns it to the program as a String. The reading of the data from the input stream is done by the object InputStreamReader (line 7), which is passed to BufferedReader. We see (line 10) that we have read in a string name with the user’s name and stored it as a string. Then (line 12) we read in String rS, which we converted to the double r with the command Double.valueOf(rS).doubleValue(). However, we do not need to convert the string name to a string, as it already is one. Now in Listing 13.4 we give ReadStnd2.java, a version of this program that still uses the same BufferedReader object but uses the parse conversion of the string rather than the valueOf form: Listing 13.4 ReadStnd2.java

 1 2 3 4 5 6 7 8

9 10 11 12 13 14 15 16



17

// ReadStnd2 : parse i n p u t f o r i n t , double import j a v a . i o . ∗ ; / / i m p o r t I /O l i b r a r y p u b l i c c l a s s ReadStnd2 { p u b l i c s t a t i c f i n a l d o u b l e P I = Math . P I ; p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) / / main throws I O E x c e p t i o n , F i l e N o t F o u n d E x c e p t i o n { d o u b l e r , A; / / Variables instants B u f f e r e d R e a d e r b = new B u f f e r e d R e a d e r ( new I n p u t S t r e a m R e a d e r ( System . i n ) ) ; System . o u t . p r i n t l n ( "Please enter your age" ) ; / / Input S t r i n g ageS = b . r e a d L i n e ( ) ; / / Read i n t a g e = I n t e g e r . p a r s e I n t ( ageS ) ; / / Convert to i n t System . o u t . p r i n t l n ( "Hi at "+ a g e +" yrs,\n Please enter r" ) ; S t r i n g rS = b . r e a d L i n e ( ) ; / / Read r = Double . p a r s e D o u b l e ( r S ) ; / / Convert to double A = PI ∗ r ∗ r ; System . o u t . p r i n t l n ( "\n r= " + r +", A= " +A) ; / / Screen }}

This program produces the output: Please type in your age 101 Hi at 101 years Please enter r 1.

Done, R = 1.0, A = 3.141592653589793

We see in ReadStnd2.java that Integer.parseInt() is used to convert a String to an int, and Double.parseDouble() is used to convert a String to a double.



268

CHAPTER 13

13.2.3 Keyboard Input via Command Line: CommandLineIn.java Recall how the main method is always declared with a statement containing void main(String[] argv). The main method gets called by the Java interpreter when you issue the java command, and since it is a method, it takes arguments (parameter list) and returns values. The word void here means that no argument gets returned to the command that calls main, while String[] argv means that the argument argv is an array (indicated by the []) of the data type String. Although we have not done it yet, you are permitted to pass arguments (data) to the main method from the command line, that is, from the shell command java that you use to run your program. As an example, the program CommandLineIn.java in Listing 13.5 accepts and then uses arguments from the command line. Consequently, if you issue the command: > java CommandLineIn Manuel 1.0

the string Manuel and the double 1.0 will be transferred to the main program for use, and will produce the output: Please enter your name & r as command line arguments 1st argument: Manuel, 2nd argument: 1. How’s life Manuel?

radius = 1.0

Done Manuel, A = 3.141592653589793

Here you are free to use any name and number, or any number of arguments, and the program will have that information transferred to it. Listing 13.5 CommandLineIn.java

 1 2 3 4 5 6

7

8 9 10 11 12 13



14

// CommandLineIn : i n p u t v i a command−l i n e p u b l i c c l a s s CommandLineIn { p u b l i c s t a t i c f i n a l d o u b l e P I =Math . P I ; / / Constants p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) { double r a d i u s , A; System . o u t . p r i n t l n ( "Please enter your name and r as command line arguments" ) ; System . o u t . p r i n t l n ( "1st argument: "+ a r g v [ 0 ] + ", 2nd argument: " +argv [ 1 ] ) ; System . o u t . p r i n t l n ( "How’s life "+ a r g v [ 0 ] + "?" ) ; S t r i n g s = argv [ 1 ] ; / / String to String r a d i u s = Double . v a l u e O f ( s ) . d o u b l e V a l u e ( ) ; / / s t r n g −Double−d o u b l e System . o u t . p r i n t l n ( "radius = "+ r a d i u s ) ; A = PI ∗ r a d i u s ∗ r a d i u s ; System . o u t . p r i n t l n ( "Done "+ a r g v [ 0 ] + ", A = "+A) ; } }

Look at CommandLineIn.java to see how to create a command-line interpreter in Java. We see first that the main-method declaration on line 4 looks like



JAVA INPUT AND OUTPUT*

269

the usual one (which declares argv to be an array of strings). Line 6 reminds the user that she should have entered arguments on the command line, which will be useful the next time the program is run. Line 7 prints out the two strings that are fed to the program through the command line. Seeing that the input is always an array of strings, no conversion is necessary if you want to use elements of the array as a string. This is, in fact, what we do on line 7 with the printouts, and on line 9 with the second argument s = argv[1]. However, if you want to use the input string s for numerical computation, then you must convert it to the appropriate data type. This is done on line 10, where we first convert the string s to a double object with the Double.valueOf(s) command, and then convert that object to a static double by appending the .doubleValue() method to the object.

13.2.4 File Input and File Output: AFileIO.java, FT.java Assume you need to store the output of your program in a file or read data from a file. As a case in point, you may want to store data to be plotted by a graphics program, or you may want to read in the results from an experiment so that you may analyze them. The simplest way to do this is by using command-line redirection from within the shell in which you are running your program: % java A outfile.dat

redirect standard output

redirects the standard output stream from the screen to the file outfile.dat. Or, for complete redirection, you could put both together: % java A outfile.dat

redirect standard I/O

You may also read and write formatted and nonformatted files from within your Java program without any shell commands. Nonformatted files are useful when creating large databases, while formatted files, being simpler to deal with, is all we shall deal with. Our discussion of the need for buffering when dealing with I/O streams is also true for file I/O. On that account, examine AFileIO.java in Listing 13.6. You see that reading a file also uses the BufferedReader method. Likewise, we also use the println method, but now with a buffer. What is new with file I/O is that we need to create an object that is associated with a physical file on our computer.

270

CHAPTER 13

Table 13.3 File readers and writers.

Reader BufferedReader FileReader

PrintWriter FileWriter BufferedWriter

Listing 13.6 AFileIO.java

 1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19



Writer

20

// AFileIO : Area w i t h F i l e I /O import j a v a . i o . ∗ ; / / Import I /O l i b r a r y public c l a s s AFileIO { p u b l i c s t a t i c f i n a l double PI = 3 . 1 4 1 5 9 3 ; / / Constants p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) throws IOException , FileNotFoundException { / / Main w i t h t h r o w d o u b l e r , A; / / declare variables / / Ask u s e r t o p r e p a r e i n p u t f r o m f i l e System . o u t . p r i n t l n ( "Enter name and r in Name.dat" ) ; B u f f e r e d R e a d e r b = new B u f f e r e d R e a d e r ( new I n p u t S t r e a m R e a d e r ( new F i l e I n p u t S t r e a m ( "Name.dat" ) ) ) ; / / Open f i l e System . o u t . p r i n t l n ( "Hi "+b . r e a d L i n e ( ) ) ; / / Read , p r i n t 1 s t l i n e String s = b . readLine () ; / / Read 2 nd l i n e , c o n v e r t d o u b l e r = Double . v a l u e O f ( s ) . d o u b l e V a l u e ( ) ; / / S t r i n g to double System . o u t . p r i n t l n ( "r = "+ r ) ; A = PI ∗ r ∗ r ; / / Do c o m p u t a t i o n System . o u t . p r i n t l n ( "Done, look in A.dat" ) ; / / Screen p r i n t P r i n t W r i t e r q = new P r i n t W r i t e r ( new F i l e O u t p u t S t r e a m ( "A.dat" ) , true ) ; q . p r i n t l n ( "r = " + r ) ; / / File output q . p r i n t l n ( "A = "+A) ; }}

Take note of how in AFileIO.java, we open the input file NameAndR.dat and associate it with the object b via the logical but not succinct command: BufferedReader b = new BufferedReader( new InputStreamReader( new FileInputStream( "NameAndR.dat" ) ) );

We now open output file A.dat and associate it with object q: PrintWriter q = new PrintWriter( new FileOutputStream( "A.dat" ), true );

Here the words in quotes, "NameAndR.dat" and "A.dat" are the names of physical files on our computer, and unless a more complete path is indicated, they are assumed to reside in the directory from which the java command is issued. The variables FileInputStream and FileOutputStream are objects in which large amounts of data may accumulate. If the data come from an input device, they



271

JAVA INPUT AND OUTPUT*

are accumulated one byte at a time and then passed to the InputStreamReader in a large chunk. If the data are to be sent to an output device, it is accumulated in a large chuck from the FileOutputStream and then passed to the file, one byte at a time. Java does the same thing in multiple ways. Other options are in Table 13.3. Once these admittedly awkward commands are issued, reading and writing files is simply accomplished by modifying the objects b and q. For instance, examine the AFileIO.java code in Listing 13.6. Check out how the declaration of the main method on lines 5–6 includes the throws IOException phrase, as needed when dealing with files. After that, we create an object b (line 11) that represents the input file, and then a second object q (line 19) that represents the output file. After that it is easy to read and write files just by affixing a read or write line method to the object, for example, lines 14 and 20: String s = b.readLine(); q.println("r = " +r);

In Listing 13.7 we present the code FT.java that uses standard Java to read data from a file until it runs out of data (instring is null), and then places those data into an array for processing. The array is then processed and written to another file. The input file contains data of the form: 0.026643 0.017807 0.008962 0.000107 -0.008756 -0.017628 -0.026508 -0.035396

These data are positions y(ti ) of a nonlinear oscillator as a function of time t, measured in discrete steps of h. In other words, the data are y(0), y(h), y(2h), . . ., with one y per line, and with each line corresponding to t increasing by h (the actual value of h is not given). The program computes the discrete Fourier transform [CP 05] of these data via: Yn =

N −1   k=0



2πnk 2πnk ) + i sin( ) y(tk ), cos( N N

n = 0, . . . , N − 1 (13.1)

where N is the number of times at which the input signal was measured. The code is given in Listing 13.7. It produces a file with lines of the form:

0 1 2 3 4 5

-425.183626 0.0 128.80130855519593 964.4441383691654 -1.3054384301614006 5.840535198361542 31.463783796425304 11.33636196280638 12.11948839663997 -0.7004454432124283 8.944525602799267 -0.7686320522517022

In §13.5 we give another version of this code, which uses formatted output for an improved listing.

272 Listing 13.7 FT.java

 1 2 3 4 5 6 7

8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25



CHAPTER 13

26



// FT . j a v a : DFT w i t h f i l e IO import j a v a . i o . ∗ ; p u b l i c c l a s s FT { s t a t i c f i n a l i n t MAX = 1 0 0 0 0 ; / / Global Constants p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) throws I O E x c e p t i o n , F i l e N o t F o u n d E x c e p t i o n { B u f f e r e d R e a d e r i n = new B u f f e r e d R e a d e r ( new F i l e R e a d e r ( "Data.in" ) ); / / Create BufferedReader P r i n t W r i t e r o u t = new P r i n t W r i t e r ( new F i l e O u t p u t S t r e a m ( "Data.out ") , true ) ; / / Create PrintWriter S t r i n g i n s t r i n g = "" ; / / Define variables d o u b l e imag , r e a l ; d o u b l e i n p u t [ ] = new d o u b l e [MAX + 1 ] ; i n t i =0 , j , k ; / / Place data i n t o i n p u t array while ( i n s t r i n g = i n . r e a d L i n e ( ) != n u l l ) / / Read t i l l n u l l { Double i n s = new Double ( i n s t r i n g ) ; / / S t r i n g to double input [ i ] = ins . doubleValue ( ) ; / / Double t o d o u b l e i = i + 1; f o r ( j = 0 ; j soltn := dsolve( {diff eq2, init con}, y(t) ); > diff( soltn, t ); diff( soltn, t, t );

1 soltn := y(t) = − g t2 + Vyo t 2

# Solve ODE with initial conditions # 1st, 2nd derivatives

300

CHAPTER 15

d2 y(t) = −g dt2

d y(t) = −g t + Vyo dt

15.6.1 Extract Right-Hand Side: rhs In order to plot the solution, all parameters must be assigned numerical values. In addition, since the expression for the solution is an equation with y(t) on the LHS, we must use Maple’s rhs() function to extract the RHS. We extract the velocity and acceleration from the solutions by taking time derivatives: > g := 9.8;

Vyo := 20;

g := 9.8

Vyo := 20 # y(t), left plot # Vy(t), right plot

> plot( rhs(soltn), t = 0..4.2, title = ‘y(t) vs t‘ ); > plot( rhs(diff(soltn, t)), t = 0..5, title = ‘v(t) vs t‘ );

> plot( rhs(diff(soltn, t, t) ), t = 0..5, title = ‘a(t) vs t‘ ); # a, right plot

y(t) vs t

v(t) vs t

20

20

a(t) vs t –9

15

10

10

0

1

2

t

3

–20 0

1

2

t

3

5

–9.5

–10

–10

5

4

–10.5

4 0

1

2

t

3

4

5

Check over how the velocity starts off positive and then gets more and more negative, and that the acceleration is constant at g = 9.8 in the negative y direction.

15.6.2 Second-Order ODE with Plot Maple has the DEplot command that both solves a differential equation and plots up the solutions. As before, while our solution was analytic and contained initial conditions as parameters, to plot a graph we need to have a completely numeric answer, and so we must assign values to all parameters. The value of the parameter g is assigned first, and the values of the initial conditions are placed within the DEplot command: > restart: with(DEtools): > diff eq := diff( y(t), t, t ) = - g ; d2

diff eq :=

dt2

y(t) = −g

g:= 9.8;

# Initialize, load DEplot # Enter ODE

g := 9.8

> DEplot( diff eq, y(t), t = -0..4.2, [[y(0)=0,D(y)(0)=20 ]] );

# [init conds]

DIFFERENTIAL EQUATIONS WITH JAVA AND MAPLE; PROJECTILE MOTION WITH DRAG*

301

20 15 y(t) 10 5

0

1

3 t

Observe how we include the initial conditions as a list for the third argument.

15.6.3 System of ODEs When the systems in an environment depend on each other, we usually have simultaneous differential equations to solve. As a case in point, in our projectile problem we have simultaneous equations for the x and y motions (we solved only for the y motion so far). The same commands and procedures are used for a set of equations as for a single equation. The additional step is to define the variable that gave the equation to solve to now be a set of equations (“set” because the order does not matter): > diff eqs := ( D@@2 ) (y)(t) = -g,

(D@@2) (x)(t) = 0;

diff eqs := (D(2) )(y)(t) = −g,

(D(2) )(x)(t) = 0

Here we use D to define the equations, although diff would do. The solution is obtained by specifying the set of equations {diff eqs} as the first argument to dsolve, and the set {y(t), x(t)}, the solution we desire, as the second argument: > dsolve( {diff eqs}, {y(t), x(t)} );

# Maple will generate integration constants

g t2 + C3 t + C4 x(t) = C1 t + C2 } 2 We see that Maple does find the expected forms of the solution (15.16), with the constants still to be determined. As before, we include the initial conditions into the set of equations we give as the first argument to dsolve, this time with four conditions: {y(t) = −

> init cons := y(0)=0,

init cons := y(0) = 0,

D(y)(0)=Vyo,

x(0)=0,

D(x)(0)=Vxo ;

D(y)(0) = Vyo, x(0) = 0,

> dsolve( {diff eqs, init cons}, {y(t), x(t)} );

D(x)(0) = Vxo # Includes initial condition

1 {y(t) = − g t2 + Vyo t, x(t) = Vxo t} 2

302

CHAPTER 15

Indeed, we get the familiar solutions as a set within braces. If we want numeric values for x and y , we must assign numeric values to the parameters. 15.7 MAPLE SOLUTION: DRAG ∝ VELOCITY We now return to our projectile-with-drag problem, applying some of the Maple tools we have just learned. If the frictional force is proportional to the first power of the velocity, the equations to solve are: d2 x d2 y = −k v , = −g − k vy . (15.17) x dt2 dt2 Because the x and y motions are independent, there is no mixing of the x motion into the y equations, and vice versa, and we could actually solve each equation separately. However, simultaneous equations generally are coupled, and we will solve them as if they were. We enter the equations into dsolve as a set with elements separated by commas. We use a set because order does not matter, and later we will place brackets around the elements. We define a variable diff eqs as the set of equations, and then solve the set. Because the equations involve the second derivatives, there will be a diff with respect to t$2 and t: > diff eqs := diff( x(t), t$2 ) = -k * diff( x(t), t), diff( y(t), t$2) = -g -k * diff( y(t), t ); d2 d d2

diff eqs :=

dt2

x(t) = −k (

dt

x(t)),

dt2

y(t) = −g − k (

d y(t)) dt

Observe that we must indicate the t dependence of x and y as x(t) and y(t), because otherwise Maple would treat them as constants whose derivatives vanish. Now we see if Maple is smart enough to find an analytic solution. We enter the set of equations as the first argument to dsolve with braces around diff eqs to indicate that it is a set: # Solve differential equation

> dsolve({diff eqs});

C1 e(−k t) + g t − C2 k } k We see that Maple returns a set of solutions with four constants. Good, we are on our way. Rather than solving for the constants in terms of the initial conditions, we define a set of initial conditions, and include them as a second argument to dsolve. We specify initial velocities as the initial values for the first derivatives, and use the D() operator (diff does not work for initial conditions): {x(t) = C3 + C4 e(−k t) ,

y(t) = −

> InitConds := x(0) = 0, D(x)(0) = Vox, > soltn := dsolve( {diff eqs,InitConds} );

InitConds := x(0) = 0,

D(x)(0) = Vox ,

y(0) = 0,

y(0) = 0,

D(y)(0) = Voy;

D(y)(0) = Voy

DIFFERENTIAL EQUATIONS WITH JAVA AND MAPLE; PROJECTILE MOTION WITH DRAG*

⎧ ⎨

soltn :=

Vox e(−k t) Vox − , x(t) = ⎩ k k

y(t) = −

(g+Voy k) e(−k t) k

303

+gt− k

This appears to be the analytic solution we want, and it is worth investigating its properties. Yet before we do that, we need to see if the velocities exhibit reasonable behaviors, that is, if they attain a terminal speed and then stop increasing. We do that by taking the derivative of our solution: > diff(soltn,t); > diff(y(t),t) = -(-(g+Voy*k)*exp(-k*t)+g)/k};

# Take derivative of y(t) to get velocity

−(g + Voy k) e(−k t) + g d d x(t) = Vox e(−k t) , y(t) = − } dt dt k Indeed, we see that the x component of velocity decreases exponentially as a function of time from its initial value and approaches zero for long times. This is a consequence of the frictional force always opposing the velocity in the x direction, but with no other force present to increase the velocity in the x direction. In contrast, the y velocity approaches a terminal value -g/k for long times. This is a consequence of the force of gravity F = −mg exactly balancing the frictional force F = mkv when v = −g/k : {

> Vox := 22*cos(Pi/4);

Vox := 11



2

Voy := 22*sin(Pi/4);



Voy := 11 2

:= 9.8;

g := 9.8

k := 1.0;

k := 1.0

Once the parameters are assigned, we check the solution: √ 2 − 11.0000 2 e(−1.0 t) , √ √ y(t) = −1.00000 (9.8 + 11.0 2) e(−1.0 t) − 9.80000 t + 9.80000 + 11.0000 2}

> soltn;

{x(t) = 11.0000



g+Voy k ⎬ k



We see our solution in its numeric form, as a set with a comma separating the equation for x(t) from that of y(t). Though they make the equations hard to read, the zeros after the decimal point indicate the Maple’s level of precision for this floating-point calculation (it became floating point when we made g and k floatingpoint numbers). 15.8 EXTRACT OPERANDS To plot up the results we need to extract the RHSs of both equations. For a simple expression this was done with the rhs command. Now that soltn is a set of equations, we need to indicate which equation it is in the set that we want the RHS of. For this purpose Maple has the op command to extract operands (pieces) from an expression. Consequently, rhs(x) is equivalent to op(2, x). First we test that we know how to extract each equation, and then we take RHSs:



304

CHAPTER 15

> op(1,soltn);

# Extract operand 1, 2 from soltn

op(2,soltn);

√ √ x(t) = 11.000 2 − 11.0000 2 e(−1.0 t) √ √ y(t) = −1.00000 (9.8 + 11.0 2) e(−1.0 t) − 9.800000 t + 9.80000+11.0000 2

Now that we have a way to separate out each equation, we take the RHSs of each: > rhs(op(1,soltn));

rhs(op(2,soltn));

# Extract RHSs of x(t), y(t) solution

√ √ 11.00000000 2 − 11.00000000 2 e(−1.0 t) √ √ −1.00000 (9.8 + 11.0 2) e(−1.0 t) − 9.80000 t + 9.80000+11.0000 2

At last we have what we need to make our plots. We plot the position and velocity (time derivative) by entering lists (in square brackets) as the first argument to the plot command (a list because order matters for the plot): > plot( [rhs(op(1,soltn)), rhs(op(2,soltn))], t = 0..3, legend = [‘x(t)‘, ‘y(t)‘] ); > plot( [diff(rhs(op(1, soltn)), t), diff(rhs(op(2, soltn)), t )], t = 0..3, legend = [‘Vx(t)‘, ‘Vy(t)‘]);

15

15

10

10 5

5

0 0

1

2

t

3

–5

1

2

t

3

–5 x(t) y(t)

Vx(t) Vy(t)

These plots show an initial linear increase of x(t) with time t, as expected for constant velocity. However, as time increases, the action of the frictional force in the x direction is to keep decreasing Vx until, for large time, Vx vanishes (except that the ground gets in the way). Then the x motion ceases and x remains constant. In turn, y(t) shows a parabolic dependence on time initially, as expected for uniform acceleration, but only a linear increase for later times. The linear increase of y with time indicates that vy has attained its terminal value as the drag force exactly balances that of gravity. As expected from the analytic expressions, and our discussion in the previous paragraph, we see that Vx approaches zero and Vy approaches a constant value for large times (or would if the ground did not get in the way). The usual plot of a trajectory is a plot of y(t) as the ordinate versus x(t) as

DIFFERENTIAL EQUATIONS WITH JAVA AND MAPLE; PROJECTILE MOTION WITH DRAG*

305

the abscissa. As you will recall from our discussion of visualization in Chapter 4, this type of plot is known as a parametric plot. A parametric plot is constructed by moving the time limits into the list that is used as the first argument (the list contains {x(t), y(t)} and the range of t): > plot([rhs(op(1, soltn)), rhs(op(2, soltn)), t = 0..3 ], labels=[‘x(t)‘, ‘y(t)‘]);

6 4 y(t) 2 0

2

4

6

8 x(t)

10

12

14

–2 –4

We see a trajectory that is much distorted from the symmetric parabola that occurs for frictionless flight, and looks similar to that computed with Java and shown in Figure 12.1. In fact, this looks very much like the type of trajectory seen for a golf ball or a baseball in which the ball rises at first and then appears to “drop out of the sky” at the end of its trajectory.

15.8.1 Solution for R and T We now want to determine if we can solve for the range R and hang time T for the general projectile-with-friction problem. We start by again placing our solution in symbolic form: > diff eqs := diff(x(t), t$) = # DE for x, y motion -k * diff(x(t), t), diff(y(t), t$) = -g -k * diff(y(t), t); d2 d d d2

diff eqs :=

dt2

x(t) = −k (

dt

x(t))

dt2

y(t) = −g − k (

dt

y(t))

> InitConds := x(0) = 0, D(x)(0) = Vox, y(0) = 0, D(y)(0) = Voy;

InitConds := x(0) = 0, D(x)(0) = Vox , y(0) = 0, D(y)(0) = Voy > soltn := dsolve( { diff eqs, InitConds } );

⎧ ⎨

Vox e(−k t) Vox − , y(t) = − soltn := x(t) = ⎩ k k

(g+Voy k) e(−k t) k

+gt− k



g+Voy k ⎬ k



306

CHAPTER 15

The range R corresponds to the value of x at which the height y = 0 (in addition to the initial firing). We start our solution for the hang time T at which y(T ) = 0 by extracting x(t) and y(t) : > X := rhs(op(1, soltn));

Y := rhs(op(2, soltn)); (g+Voy k) e(−k t)

Vox e(−k t) Vox k − Y := − X := k k Now we use solve to find the time T at which Y (T ) = 0:

# Extract RHS x(t), y(t)

+gt− k

g+Voy k k

> solve(Y, t);

0

This just tells us the initial time, which we know, but is not what we want. Instead, let us be more specific and ask for the time at which the height y just becomes negative, namely, when it just hits the ground: > solve(Y < 0, t);

We see that Maple returns nothing. It apparently has given up. If we look at the expression for y(t), we see that it contains the time t in both an exponential and a linear term. Apparently, setting Y = 0 leads to a transcendental equation that has no analytic solution. The only solution, then, is obtained numerically, and we shall do that with Java (although Maple will also work). 15.9 DRAG ∝ V 2 (EXERCISE) Modify the analysis performed for a drag force proportional to the first power of the velocity so that it is appropriate for a force proportional to the square of the velocity. Maple should be able to solve this analytically as well, and you should be able to follow all of the steps we have for Model 1. Note: since v 2 does not change sign when v does, you will need to add a v/|v| factor in the frictional force for the y motion (it is not needed for the x motion because the x component of velocity is always positive). 15.10 DRAG ∝ V 3/2 We now want to solve the projectile-with-friction problem for a drag force proportional to the 3/2 power of the velocity. The equations we need to solve are: d2 x = −k vx3/2 , dt2

vy d2 y = −g − k vy3/2 . dt2 |vy |

(15.18)

The acute reader might notice that there is a problem with this last equation; namely, if vy is negative, then the square root operation, which is part of raising vy to the 3/2 power, will return an imaginary number. This is clearly not what

DIFFERENTIAL EQUATIONS WITH JAVA AND MAPLE; PROJECTILE MOTION WITH DRAG*

307

we want. We solve that problem by taking the absolute value of vy before raising it to a power: ( (3/2 ( dy ( d2 y vy = −g − k (( (( (15.19) . 2 dt dt |vy | This expression is simpler to read and easier to enter into Maple. (An alternative approach would be to use the sign function, which extracts the sign of its argument, to keep track of the sign of the velocity and adjust the frictional force appropriately.) We set about solving this model just as we did the problem with drag proportional to the first power. First we define the set of differential equations and give it the name diff eqs: > restart; with(DEtools): # Clean the slate # DE > diff eqs := diff(x(t),t$2) = -k * diff(x(t), t)ˆ(3/2), diff(y(t),t$2) = -g - (diff(y(t),t)/abs(diff(y(t),t))) *k*abs(diff(y(t), t))ˆ(3/2);

d2 d d2 d diff eqs := 2 x(t) = −k ( x(t))(3/2) , y(t) = −g−( y(t)) 2 dt dt dt dt

)( ( (d ( ( y(t)( k ( dt (

This looks fine, and so we go on to look for a general solution by first entering the initial conditions: > InitConds := x(0) = 0, D(x)(0) = Vox, y(0) = 0, D(y)(0) = Voy;

InitConds := x(0) = 0,

D(x)(0) = Vox , y(0) = 0, D(y)(0) = Voy

Warning: You may have to stop the following command by hand. We now ask Maple to solve the differential equations labeled diff eqs with the initial conditions labeled InitConds: > soltn := dsolve( [diff eqs, InitConds], [x(t), y(t)] );

soltn := · · · RootOf · · ·

We see from the time it takes Maple to search for a solution, from the volume of output that Maple produces, and from the RootOf command being returned, that Maple is having trouble finding a simple solution to our problem. In any case, an analytic solution with this level of complexity is not illuminating, and probably not even good for computation (the numerous subtractions and evaluations of the multivalued tan−1 and ln functions is error-prone). Now we try a numerical approach. We start it by assigning values to the initial conditions and parameters: > Vox := 22.*cos(Pi/4);

Vox := 15.556349186

Voy := 22.*sin(Pi/4);

Voy := 15.556349186

g := 9.8;

g := 9.8

k := 1/5.;

k := 0.2000

308

CHAPTER 15

We get a numeric solution using the same procedures as before, only now by including the type=numeric argument to dsolve: > soltn := dsolve( [diff eqs, InitConds], [x(t), y(t)], type=numeric );

soltn := proc(x rkf45 ) . . . end proc

Regardless of this not appearing to be a clear signal that a solution is in hand, this is Maple’s way of telling us that it has written a procedure named soltn that will produce a numerical solution to the equations. Recall, a procedure in Maple is like a function that requires more than one line to define. It is like a method in Java or a subroutine in Fortran. In the present case the argument to the procedure tells us that the procedure takes an argument and then returns the solution. Since Maple assumes we are solving for y(x), the argument is indicated generically as x. In our case, the second argument to the solve command was the list [x(t), y(t)], and so we are solving for x and y as functions of the time t. Therefore the argument to the procedure is the time t. (The subscript rkf45 to x is meant for aficionados; it indicates that a fourth order Runga-Kutta numerical method is used, and that it uses adaptive step size to obtain fifth-order precision.) We now have a procedure soltn(t) that numerically solves the differential equations for a single input value of time t and returns [x(t), y(t)] in some form. To see how this works, let us look at the solution returned for time 0 (which should just be the initial conditions we entered): > soltn(0);

d d x(t) = 15.556349186, y(t) = 0., y(t) = 15.556349186] dt dt OK, the solution appears to be working properly and returning [t,x(t),Vx(t),y(t),Vy(t)]. So we try some nonzero times: [t = 0., x(t) = 0.,

> soltn(0.1);

soltn(0.5);

d x(t) = 14.3981782622785151, dt d y(t) = 1.44943382860698944, y(t) = 13.47199594152410] dt d [t = 0.5, x(t) = 6.49693016854386762, x(t) = 10.8534715090049546, dt d y(t) = 6.980924610035919] y(t) = 5.458067069137789, dt We use Maple to pick out the individual pieces of the solution: [t = 0.1, x(t) = 1.496606466606704,

> Soltn := soltn(0.5);

# Store list of solutions in Soltn

d x(t) = 10.8534715090049546, Soltn := [t = 0.5, x(t) = 6.49693016854386762, dt d y(t) = 5.45806706913778950, y(t) = 6.98092461003591946] dt > op(1, Soltn);

op(2, Soltn); op(3, Soltn); op(4, Soltn); op(5, Soltn);

309

DIFFERENTIAL EQUATIONS WITH JAVA AND MAPLE; PROJECTILE MOTION WITH DRAG*

d x(t) = 10.8534715090049546 dt d y(t) = 5.45806706913778950 y(t) = 6.98092461003591946 dt

t = 0.5

x(t) = 6.49693016854386762

In order to plot the solutions, we pick off just the RHS of these expressions: > rhs(op(2, Soltn));

# Extract RHS of x(t), y(t) in Soltn

rhs(op(4, Soltn));

6.49693016854386762

5.45806706913778950

15.10.1 Defining Functions from Procedures It seems like we should now be able to plot up the solution. However, Maple’s plot command accepts expressions and functions as input but not procedures. (We discussed Maple programming and procedures in Chapter 8.) This means we cannot have plot call our procedure and plot it. It is not much work to define a function from a procedure and then to plot up the function; essentially, you just define an arrow function as a call to your procedure. Here we do that for the x and y positions and velocities: > X := (t) -> rhs(op(2, soltn(t))); > Vx := (t) -> rhs(op(3, soltn(t)));

Y := (t) -> rhs(op(4, soltn(t))); Vy := (t) -> rhs(op(5, soltn(t)));

X := t → rhs(op(2, soltn(t))) Vx := t → rhs(op(3, soltn(t)))

Y := t → rhs(op(4, soltn(t))) Vy := t → rhs(op(5, soltn(t)))

> plot([X,Y], 0..3, title = "x(t) & y(t), 3/2-power friction"); > plot([Vx,Vy], 0..3, title = "Vx(t) & Vy(t), 3/2-power friction");

# Plot 1 # Plot 2

> plot([X,Y,0..3], title = "y(t) vs x(t) for 3/2-power friction");

# Plot 3

x(t) & y(t), 3/2-power friction

Vx(t) & Vy(t), 3/2-power friction

y(t) vs x(t) for 3/2-power friction

15

20

6 15

10

10

5

5

0

0 –5

4

–5 0.5

1

1.5

2

2.5

3 –10

2 0.5

1

1.5

2

2.5

3

0

5

10

15

20

–2 –4

We see similar results to Model 1, only now with an even more drastic “drop out of the sky effect” at the end of the trajectory.

310

CHAPTER 15

y

.N

(x, y)

.O .

H

x

Figure 15.1 Left: The gravitational force on a planet a distance r from the sun. The x and y components of the force are indicated. Right: Output from the applet PlanetRHL showing the precession of a planet’s orbit when the gravitational force ∝ 1/r4 .

15.11 EXPLORATION: PLANETARY MOTION* Newton’s explanation of the motion of the planets in terms of a universal law of gravitation is one of the great achievements of science. He was able to prove that the planets traveled along elliptical paths with the sun at one vertex, and with periods that agree with observation. All Newton needed to postulate is that the force between a planet of mass m and the sun of mass M is given by GmM , (15.20) r2 where r is the distance between the planet of mass m and sun of mass M , and G is a universal constant. The minus sign indicates that the force is always attractive and lies along the line connecting the planet and sun, as indicated on the left of Figure 15.1. The hard part for Newton was solving the resulting differential equations, since he had to invent calculus to do it. Whereas the analytic solution is complicated, the numerical solution is not. F =−

Even for planets, the basic equation of motion is d2 x , (15.21) dt2 with the force now given by (15.20). If we look at Figure 15.1 we see that  x y Fy = F sin θ = F , r = x2 + y 2 , Fx = F cos θ = F , r r (15.22) with F given by (15.20). If we write the equation of motion in component form and substitute (15.22) for the force components, we obtain the differential equations we need to solve: d2 x x d2 y y = −GM 3 , = −GM 3 . (15.23) 2 dt r dt2 r

F = ma = m

DIFFERENTIAL EQUATIONS WITH JAVA AND MAPLE; PROJECTILE MOTION WITH DRAG*

311

15.11.1 Implementation: Planet.java On the CD you will find the applet Planet. We suggest that you run the class file to see how the solution behaves. To keep the calculation simple without changing the physics, assume that the units we use are such that GM = 1, and that the initial conditions are [Feyn 63]: x(0) = 0.5,

y(0) = 0,

vx (0) = 0.0,

vy (0) = 1.63. (15.24)

1. Modify projectileAir.java so that it solves (15.23) as functions of time. 2. Make the number of time steps large enough so that the planet’s orbit repeats on top of itself. 3. You may need to make the time step small enough so that the orbit closes upon itself (it should) and just repeats. This should be a nice ellipse. 4. Experiment with initial conditions until you obtain the ones that produce a circular orbit (a circle is a special case of an ellipse). 5. Once you have good precision, see the effect of progressively increasing the initial velocity until the orbit opens up and becomes an hyperbola. 6. Use the same initial conditions as produced the ellipse. This time investigate the effect of the power in (15.20) being 1/r4 rather than 1/r2 . You should find that the orbital ellipse now rotates (precesses), as in Figure 15.1. 15.12 KEY WORDS equations of motion numerical ODE solution

Euler’s method numerical differentiation

forward difference ODE

15.13 SUPPLEMENTARY EXERCISES 1. Use Maple, Java, or both to find the solution of the differential equation dn(t) = −λn(t), dt

(15.25)

with the initial condition n(0) = 100. Plot up the answer for λ = 0.3. 2. In Chapter 16 you will encounter an RLC electrical circuit and the differential equation describing it. We consider here the same circuit without a capacitor and described by the differential equation dI (15.26) . dt Solve this equation for the current in the circuit for the same values of R and L as in the problem, and for a constant V (t) (same magnitude as in problem). Plot your solution. V (t) = RI + L

312

CHAPTER 15

3. Consider exponential growth starting with an initial population N (0) = 2. a. Solve the equation describing exponential growth dN (t) = λN (t). (15.27) dt Assign the integration constants to correspond to n(0) = 100, and λ = 0.3, and plot up the solution. b. Solve for exponential growth with a modulated growth parameter 



dy(t) 10000 − N (t) =λ × N (t). (15.28) dt 10000 This is the differential-equation version of the logistics map studied in Chapter 19, “Discrete Math, Arrays as Bins.” The term in square brackets is insignificant for small N (t). 4. Solve for and plot up the solution to the differential equation dy(x) (15.29) = sin(xy), dx with initial condition y(0) = 1. a. Verify that Maple cannot find an analytic solution to this equation. b. Have Maple find a numerical solution to this equation. 5. Solve for and plot up the solution to the differential equation d2 y(x) = −y(x)3 , (15.30) dx2 subject to the initial conditions y(0) = 1, y  (0) = 0. Hint: If Maple cannot find an analytic solution, you may need to try a numerical one.

Chapter Sixteen Object-Oriented Programming, Abstract Data; Complex Currents

Note to the instructor: As an alternative to the full version of this chapter, you may skip the study of the RLC circuit and just focus on the mathematics of complex numbers and their representation in terms of objects. And even with that, you may defer the use of nonstatic (object-oriented) methods to a later time. 16.1 PROBLEM: RESONANCE IN RLC CIRCUIT We are given the circuit shown on the left of Figure 16.1 containing a resistor of resistance R, an inductor of inductance L, and a capacitor of capacitance C . All three elements are connected in series to an alternating voltage source V (t) = V0 cos ωt.

(16.1)

Problem: Determine the magnitude and time dependence of the current in this RLC circuit as a function of the frequency of the external voltage. We will solve the RLC circuit problem for you within this chapter. Your problem is to repeat the calculation for a circuit in which there are two RLC circuits in parallel, as shown on the right of Figure 16.1. You may assume a single value for inductance and capacitance, and three values for resistance: 1000 1000 1000 , , Ω. (16.2) 1.5 2.1 5.2 √ Consider frequencies of applied voltage in the range 0 < ω < 2/ LC = 2/s. L = 1000 H,

C=

1 F, 1000

R=

16.2 MATH: COMPLEX NUMBERS Try to remember your first exposure to square roots in elementary school. Though 2 it was √straightforward to understand that 5 = 25, it was a challenge to understand that 25 = ±5, and more of a challenge to understand what was the true value of √ √24. For many of us, it was downright impossible to understand the true value of −1. Mathematicians, being a rather clever and proud bunch, have handled this

314

CHAPTER 16

R

R

2R

L

L

2L

C

C

2C

Figure 16.1 Left: An RLC circuit connected to an alternating voltage source. Right: Two RLC circuits connected in parallel to an alternating voltage. Observe that one of the parallel circuits has double the values of R, L, and C as does the other.

affront to their abilities by inventing the number i as the answer,1 def √ i = −1, i2 = −1,

(16.3)

where the “def” over the equal sign indicates a definition. In this way mathematicians have a way of going ahead with their calculations, even if they do not know what i means. Whereas defining away one’s ignorance may appear to be a swindle, mathematicians are an honest bunch at heart and so tell the world that they have really just made up the answer by calling i the imaginary number. “Imaginary” is a good name for i, since there is no way to measure this number in the real world, but, then again, there is no law forbidding us from imagining such a number. In common mathematical usage, i is called the imaginary number, while any multiple of i is called an imaginary number. So, for example, 2i, i, and 100i are all imaginary numbers. Once we have extended our minds to imagine an imaginary number, it is not much of a stretch to imagine adding a real number to an imaginary number to form something more complex. To name an instance, 1 + i, 1 + 2i, and 100 + 76i. These numbers with both real and imaginary parts are called complex numbers.2 The term “complex” indicates that these numbers have a number of parts, and not that they are hard to understand! Complex numbers are very useful in mathematics and science since they let us double our work output with only the slightest increase in input. This is accomplished by employing the familiar operations of algebra and calculus on complex numbers, and then separating off the real and imaginary parts of the answer at the end. By way of example, even if z is a complex number, z 3 /z still equal z 2 , without our having to express the individual numbers in terms of their real and imaginary parts. 1 The 2 The

invention is credited to Girolama Cardana (1501–1576). term, as well as much in applied mathematics, is credited to Carl Friedrich Gauss (1777–1855).

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

315

Im z = y

z(x,y)

r

θ Re z = x

Figure 16.2 Representation of a complex number as a vector in space.

To do algebra with complex numbers, we define the symbol z to represent a number with both real and imaginary parts: z = x + iy.

(16.4)

In turn, the complex nature of z is indicated by giving its real and imaginary parts: Re z = x,

Im z = y.

(16.5)

In a strict sense, y is the magnitude of the imaginary part of z , and iy is the imaginary part. Yet y is usually called “the imaginary part.” Because complex numbers have independent real and imaginary parts, a useful way to visualize them is to imagine a coordinate system in which the ordinate points off into imaginary space and the abscissa points off into real space. As seen in Figure 16.2, we then visualize z = x + iy as a point with y projection along the imaginary axis and x projection along the real axis. This is analogous to a vector in a 2-D space, except that part of this vector lies in an imaginary space. The analogy between complex numbers and 2-D vectors is taken one step further by applying the polar coordinate representation of a vector to complex numbers. On account of this, the point z in Figure 16.2 may be located not only by giving its Cartesian coordinates x and y , but also by specifying it polar coordinates r, the length of the vector from the origin to point z , and θ, the angle that the vector makes with the abscissa. The complex number is the same in either case, and, indeed, the two representations are related via simple trigonometry: r = x =



x2 + y 2 , r cos θ,

θ = tan−1 (y/x), y = r sin θ.

(16.6)

316

CHAPTER 16

16.2.1 Complex Arithmetic Review The essence of the computing aspect of our problem is the programming of the rules of arithmetic for complex numbers. This is an interesting chore because while Java contains all the rules for real numbers, you must educate Java as to the rules for complex numbers. Indeed, since complex numbers are not primitive data types like doubles and floats, we will construct complex numbers as objects.3 We start with two complex numbers, which we distinguish with subscripts: z1 = x1 + i y1 , z2 = x2 + i y2 .

(16.7) (16.8)

The rules of arithmetic follow by applying algebra to the real and imaginary parts: z1 + z2 = (x1 + x2 ) + i(y1 + y2 ), (16.9) z1 − z2 = (x1 − x2 ) + i(y1 − y2 ), (16.10) z1 × z2 = (x1 + iy1 ) × (x2 + iy2 ), (16.11) = (x1 x2 − y1 y2 ) + i(x1 y2 + x2 y1 ), x1 + iy1 x2 − iy2 z1 = × , (16.12) z2 x2 + iy2 x2 − iy2 (x1 x2 + y1 y2 ) + i(y1 x2 − x1 y2 ) = . x22 + y22

Addition: Subtraction: Multiplication: Division:

In deducing the rule for complex division we employed the fact that a complex number z multiplied by its complex conjugate z ∗ = x − iy,

(16.13)

z × z ∗ = (x + iy)(x − iy) = x2 + y 2 .

(16.14)

always yields a real number:

We observe that this result is the same as the square of the length r defined in (16.6). The length r is commonly referred to as the modulus or magnitude of the complex number z , and is expressed by using the absolute value symbol: r = |z|. The product of the complex number z and its complex conjugate z ∗ is then zz ∗ = |z|2 = r2 .

(16.15)

Exercise: Consider the complex numbers b = 1 + 2i,

c = 4 + i.

What are the values of b + c, b − c, b × c, |c|, and b/c?

(16.16) ♠

3 Because complex numbers are used so often in science and engineering, there is a move afoot to have some future versions of Java incorporate complex numbers as a primitive data type in order to speed up execution and make them easier to use.

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

317

We end our little review with an examination of functions of complex numbers. The new idea here is an ingenious theorem, derived by Euler, that if you raise e, the base of the natural logarithm system, to a purely imaginary power, then you get a complex number with a sine and cosine as its real and imaginary parts: eiθ = cos θ + i sin θ

(Euler’s theorem).

(16.17)

If the angle θ is real and in radians, then cos θ and sin θ are identified with the projection of exp iθ along the real and imaginary axes, respectively. So if we look at both Figure 16.2 and Eqs. (16.4) and (16.6), we see that it is also possible to express a complex number z in terms of its polar representation: z ≡ x + iy = reiθ = r cos θ + i r sin θ.

(16.18)

An important application of Euler’s theorem is to define what it means to raise a number to a complex power z , for example, ez = ex+iy = ex eiy = ex (cos y + i sin y).

(16.19)

We shall find (16.19) useful in our exercises. 16.3 THEORY: RESISTANCE BECOMES IMPEDANCE The basic rules of circuit theory are called Kirchoff’s laws [R & M 93], and we now apply one of them to the circuit on the left of Figure 16.1. We work our way around the circuit, setting the external voltage V (t) equal to the sum of the voltage drops across the resistor, the inductor, and the capacitor. If I(t) is the current in the circuit, we end up with the basic differential equation of circuit theory dV (t) dI d2 I I =R +L 2 + , dt dt dt C

(16.20)

where we have taken an extra derivative to eliminate an integral. The solution to our problem follows by solving (16.20) when the voltage has the form V (t) = V0 cos ωt. An elegant way to do that is to recognize that V0 cos ωt = Re V0 e−iωt = Re (V0 cos ωt − iV0 sin ωt) .

(16.21)

Because (16.20) is a linear equation (only first power of I occurs), the law of linear superposition holds. This means that if we imagine the circuit being driven by a complex voltage source, whose real part is the physical voltage, then the resulting current I(t) will also be complex, with its real part the physical current. Thus we assume that the current has the form I(t) = I0 e−iωt .

(16.22)

If we substitute this and the complex V (t) into (16.20), we obtain V0 e−iωt = ZI0 e−iωt .

(16.23)

318

CHAPTER 16

Here Z is called the impedance and is the complex expression 



1 Z =R+i − ωL . ωC

(16.24)

Equation (16.23) is the generalization of Ohm’s law, V = IR, to alternating current circuits.4 We see that the real part of the impedance is just the resistance R, while the imaginary part (called the reactance) is 1/ωC − ωL. As we shall see, the imaginary part of the impedance determines the phase of the current.

16.3.1 Solution for Complex Current If we now solve (16.23) for the complex current, we obtain I(t) =

1 V0 e−iωt V0 e−iωt = . Z R + i (1/ωC − ωL)

(16.25)

Eq. (16.25) is more illuminating if the complex impedance is expressed in polar form:

|Z| =



Z = |Z|eiθ , R2 + (1/ωC − ωL)2 ,

θ = tan−1





(16.26)

1/ωC − ωL . (16.27) R

We see now that the complex current is I(t) =

V0 −i(ωt+θ) , e |Z|

(16.28)

which means that the physical current is its real part: V0 Iphysical (t) = ReI(t) = cos(ωt + θ). |Z|

(16.29)

Equation (16.29) states that the amplitude of the current in the circuit is given by the amplitude of the voltage divided by the magnitude of the complex impedance, and that the phase of the current, relative to that of the voltage, is given by θ. If the phase θ > 0, then the current leads the voltage in time, that is, the current reaches its maximum before the voltage reaches its maximum. If θ is negative, then the current lags the voltage. Finally, what is to be done if we have two such RLC circuits in parallel, as shown on the right of Figure 16.1. The analysis is the same as that done with ordinary resistors. If two impedances are in series, then they have the same current passing through them. If two impedances are in parallel, then they have the same 4 Some elementary texts may refer to |Z| as the impedance and then use the concept of phasors to describe the effect of the impedance on the phase. We view the use of complex impedance as more direct and elegant.

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

319

Figure 16.3 An abstract drawing, or what?

voltage across them. If two impedances are connected in series, then the voltages add, and this leads to: Z = Z1 + Z2

(series connection).

(16.30)

If the impedances are connected in parallel, then the currents add, and this leads to 1 1 1 = + Z Z1 Z2

(parallel connection).

(16.31)

Hence in the parallel case, the impedances add in inverse, with all the steps of the calculation being performed with complex arithmetic. 16.4 CS: ABSTRACT DATA TYPES, OBJECTS What do you see when you look at the abstract object in Figure 16.3? Some readers may see a face in profile, others may see some parts of human anatomy, and others the total absence of artistic ability. This figure is abstract in the sense that it does not try to present a true or realistic picture of the object, but rather uses a symbol to suggest more than meets the eye. Abstract or formal concepts pervade mathematics and science because they make it easier to describe nature. For example, we often use the symbol v(t) to denote the velocity of an object as a function of time. Despite velocity being a familiar concept, it is actually abstract in the sense that we cannot see it. What we see is the position of the object as a function of time, and then we infer from that the velocity by determining how rapidly that position is changing. In computer science we create an abstract object by using a symbol to describe a collection of items. We have already seen that data, or variables in Java and Maple may be integers, floating-point numbers, Booleans, or strings. These types of variables are built into the languages and therefore are called primitive data types. In addition, computer languages let the user define abstract data types of their own by combining primitive data types into more complicated structures called objects. These objects are abstract in the sense that they are named with a single symbol, yet they represent a number of parts.

320

CHAPTER 16

For instance, one might create an object with parts that contain a student’s school record (ID, grades, etc.), as well as other parts that contain methods to calculate the grade point average, etc. Of course, one would need many such objects because there are many students. To distinguish between the general structure of this student-record object and specific record objects for individual students, the general object is called a class, while the object for specific cases is called an instance of the class, or just an object. In this chapter our objects will be complex numbers, while in other chapters they may be plots, vectors, or matrices. The classes that we form will be combinations of abstract data types and associated methods for modifying those data. The entire class may also be thought of as objects. In a formal sense, computer science requires abstract data types to possess the three properties [Zach 96]: Typename: procedure to construct the new data type from elementary pieces. Set values: mechanism for assigning values to the defined data type. Set operations: rules that permit operations on the new data type (you would not have gone to all the trouble of declaring a new data type unless you were interested in doing something with it). In terms of these properties, when we declare a variable to be complex we satisfy property (1). When we declare Rez = x and Imz = y as doubles, we satisfy (2). When we define the rules of arithmetic and trigonometry for complex numbers (as reviewed in § 16.2.1), we satisfy (3). Before we examine how these properties are applied in our programs, let us review the structure we have been using in our Java programs. When we start off our programs with a declaration statement such as double x. This tells the Java compiler the kind of variable x is, so that Java will store it properly in memory and use proper operations on it. The general rule is that every variable we use in a program must have its data type declared. For primitive (built-in) data types, we declare them to be double, float, int, char, long, short, or boolean. If our program employs some user-defined, abstract data types, then they too must be declared. This declaration must occur even if we do not define the meaning of the data type until later in the program (the compiler checks on that). Consequently, when our program refers to a number z as complex, the compiler must be told at some point that there is both a real part x and an imaginary part y that makes up a complex number. To actually create objects in your Java program, you need class variables and methods that are nonstatic. This means we leave off the word static in declaring the class and variables. If the class and class variables are no longer static they may be thought of as dynamic. Likewise, methods that deal with objects may

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

321

be either static or dynamic. The static ones take objects as arguments, much like conventional mathematical functions. In contrast, dynamic methods accomplish the same end by modifying or interacting with the objects. Regardless of our starting our dealings with objects using static methods, you must use dynamic (nonstatic) methods to enjoy the full power of object-oriented programming.

16.4.1 Object Declaration and Construction Before we start working with objects in a program, it is necessary to understand that even though we may assign a name like x to an object, because objects have multiple components, you do not assign one explicit value to the object. It follows then, that when Java deals with objects it does so by reference. In plain English this means that the name of the variable refers to the location in memory where your object is stored, and not to the explicit values of the object’s parts. To see what this means in practise, the class file Complex.java in Listing 16.1 adds and multiplies complex numbers, with the complex numbers represented as objects. Exercise: 1. Enter the program Complex.java by hand, trying to understand it as best you are able. (Yes, we know that you can just copy it, but then you do not become familiar with the constructs.) 2. Study how the word Complex is a number of things in this program. It is the name of the class (line 2), as well as the name of two methods that create the object (lines 7 and 11). Methods, such as these, that create objects are called constructors. In spite of neophytes viewing these multiple uses of the name Complex as confusing (Landau’s second rule), more experienced users often view it as elegant and efficient. Look closely and take note that this program has nonstatic variables (no static on line 3). 3. Compile and execute this program, and check that the output agrees with the ♠ results you obtained in the exercises in §16.2.1. The first thing to notice about Complex.java is that the class is declared on line 2 with the statement 2 public class Complex

The main method is declared on line 23 with the statement 23 public static void main(String[] argv)

These are the same techniques we have employed before (it is good when some things stay the same in life). However, on line 3 we see that the variables re and im are declared for the entire class with the statement

322

Listing 16.1 Complex.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

29 30 31 32 33 34 35 36 37 38



CHAPTER 16

39

/ / Complex . j a v a : C r e a t e s ” Complex ” c l a s s w i t h s t a t i c members p u b l i c c l a s s Complex { p u b l i c d o u b l e r e , im ; / / Nonstatic , for entire class p u b l i c Complex ( ) / / Default constructor { re = 0; im = 0 ; } p u b l i c Complex ( d o u b l e x , d o u b l e y ) / / Full constructor { re = x ; im = y ; } / / S t a t i c method a d d s 2 c o m p l e x numbers , r e t u r n s sum p u b l i c s t a t i c Complex add ( Complex a , Complex b ) { Complex temp = new Complex ( ) ; / / C r e a t e Complex temp temp . r e = a . r e + b . r e ; temp . im = a . im + b . im ; r e t u r n temp ; } / / S t a t i c Method m u l t i p l i e s 2 c o m p l e x numbers , r e t u r n s p r o d u c t p u b l i c s t a t i c Complex m u l t ( Complex a , Complex b ) { Complex temp = new Complex ( ) ; / / C r e a t e Complex temp temp . r e = a . r e ∗ b . r e − a . im ∗ b . im ; temp . im = a . r e ∗ b . im + a . im ∗ b . r e ; r e t u r n temp ; } / / Main method f o r Complex o b j e c t s w i t h S t a t i c m e t h o d s p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) { Complex a , b ; / / D e c l a r e 2 Complex o b j e c t s a = new Complex ( ) ; / / Create the o b j e c t s b = new Complex ( 4 . 7 , 3 . 2 ) ; Complex c = new Complex ( 3 . 1 , 2 . 4 ) ; / / Declare , c r e a t e in 1 System . o u t . p r i n t l n ( "a,b = ("+ a . r e +","+a . im+"),("+b . r e +","+b . im+")," ) ; System . o u t . p r i n t l n ( "c = ("+c . r e +", "+c . im+")" ) ; a = add ( b , c ) ; / / Perform a r i t h m e t i c System . o u t . p r i n t l n ( "b+c = (" + a . r e + ", " + a . im+"), " ) ; a = mult ( b , c ) ; System . o u t . p r i n t l n ( "b*c = (" + a . r e + ", " + a . im+")" ) ; }} / / EXPECTED OUTPUT a ,b = (0.0 ,0.0) , (4.7 ,3.2) , c = (3.1 , 2.4) b+c = ( 7 . 8 0 0 0 0 0 0 0 0 0 0 0 0 0 1 , 5 . 6 ) , b∗ c = ( 6 . 8 9 0 0 0 0 0 0 0 0 0 0 0 0 1 , 2 1 . 2 0 0 0 0 0 0 0 0 0 0 0 0 0 3 )



OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

323

3 public double re, im;

Although this is similar to the declaration we have seen before for class variables, observe that the word static is absent. This indicates that these variables are interactive or dynamic, namely, parts of an object. They are dynamic in the sense that they will be different for each object (instance of the class) created. That is, if we define z1 and z2 to be Complex objects, then the variables re and im will be different for z1 and z2. We extract the component parts of our complex object by a projection operation. For those readers who know some vector analysis, think of this as similar to extracting the components of a vector in space by taking dot products with the unit vectors to project the vector onto different axes. As with space vectors, the projection operation is denoted by a dot operation: z1.re z1.im z2.re z2.im

real part of object z1 imaginary part of object z1 real part of object z2 imaginary part of object z1

This same dot convention is used to access the methods of objects, as we will see below. On line 4 we see a method Complex declared with the statement 4 public Complex()

On line 7 we see a method Complex declared, yet again, but with a somewhat different statement: 7 public Complex(double x, double y)

Some explanation is clearly in order! First notice that both of these methods are nonstatic (no word static). In fact, they are the methods that construct our complex number object, which we call Complex. Second notice that the name of each of these methods is the same as the name of the class, Complex.5 Indeed, you know that they are special since by convention they are spelled with their first letters capitalized, rather than the lowercase letters usually used for methods, and because these objects have the same name as the class they are in. The two Complex methods are used to construct the object, and for this reason are called constructors. The first Complex constructor on line 4 is seen to be a method that takes no argument and returns no value (yes, this appears rather weird, but be patient). When Complex gets called with no argument, as we see on line 25, the real and imaginary parts of the complex number (object) are set to zero. This method is called the default constructor, since it does what Java would 5 These two nonstatic methods are special as they permit the parameters characterizing the object that they construct to be passed during a new call. However, you probably will not understand this statement until we describe the new call.

324

CHAPTER 16

otherwise do automatically (“by default”) when first creating an object, namely, set all of its component parts initially to zero. We have explicitly included it for pedagogical purposes. Exercise: Remove the default constructor (the one on line 4 that takes no argument) from Complex.java and check that you get the same result for the call to Complex(). ♠ The Complex method on line 7 implements the standard way to construct complex numbers. It is seen to take the two doubles x and y as input arguments, to set the real part of the complex number (object) to x, the imaginary part to y, and then return. This method is an additional constructor for complex numbers but differs from the default constructor by taking arguments. Inasmuch as the nondefault constructor takes arguments while the default constructor does not, Java does not confuse it with the default constructor, even though both methods have the same name. Okay, let us now take stock of what we have up to this point. On line 3 has defined the variables re and im that will be the two separate parts of the created object. As each instance of each object created will have different values for the object’s parts, these variables are referred to as instance variables. Because the name of the class file and the names of the objects it creates are all the same, it sometimes is useful to use yet another word to distinguish one from the other. Hence the phrase instance of a class is used to refer to the created objects (in our example, a, b, and c). This distinguishes them from the definition of the abstract data type. Complex.java

Now let us look at the main method to see how to go about creating objects using the constructor Complex. On line 24, in the usual place for declaring variables, we have the statement 24 Complex a, b;

Because the compiler knows that Complex is not one of its primitive (built-in) data types, it assumes that it must be one we have defined. In the present case, the class file contains the nonstatic class named Complex, as well as the constructors for Complex objects (data types). This means that the compiler does not have to look very far to know what you mean by a complex data type. By reason of this, when the statement Complex a, b; on line 24 declares the variables a and b to be Complex objects, we know that they are manifestly objects since the constructors are not static. Recall that declaring a variable type, such as double or int, does not assign a value to the variable, but, instead, tells the compiler to add the name of the variable to the list of variables it will encounter. Likewise, the declaration statement

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

325

Complex a, b lets the compiler know what type of variables these are without as-

signing values to their parts. To actually create objects we have to place numerical values in the memory locations that have been reserved for them. Seeing that an object has multiple parts, we cannot give all its parts initial values with a simple assignment statement like a = 0;, so something fancier is called for. This is exactly why the constructor methods are used. Specifically, on line 25 we have the object a created with the statement 25 a = new Complex();

and on line 26 we have the object b created with the statement 26 b = new Complex(4.7, 3.2);

Look at how the creation of a new object requires the command new to precede the name of the constructor (Complex in this case). Also note that line 25 uses the default constructor method to set both the re and im parts of a to zero, while line 26 uses the second constructor method to set the re part of b to 4.7 and the im part of b to 3.2. Just as we have done with the primitive data types of Java, it is possible to both declare and initialize an object in one statement. Indeed, line 27 does just that for object c with the statement 27 Complex c = new Complex(3.1, 2.4);

Monitor how the data type Complex precedes the variable name c in line 27 because the variable c has not previously been declared; future uses of c should not declare or create it again.

16.4.2 Static and Nonstatic Methods Once our complex-number objects have been declared (added to the variable list) and created (assigned values), it is easy to do arithmetic with them. For those readers seeing objects for the first time, we suggest that you get some experience with object arithmetic using the familiar static methods that we have been using up until now. That is what we do in this section. In the next section §16.4.2.1, which we consider optional for object neophytes, we show how to perform the same object arithmetic using nonstatic methods. Undeniably, nonstatic methods are elegant and powerful; however, they do their work in a different way and so may take some getting used to before making sense. We recommend that even the object neophytes read §16.4.2.1 before getting on to the exercises. After that it should be possible to make more sense out of the nonstatic methods and even to repeat the exercises using nonstatic methods (an approach we encourage). We have now declared and created objects that represent complex numbers.

326

CHAPTER 16

We know from §16.2.1 all the rules of complex arithmetic and complex trigonometry, so next we will write Java methods to implement these rules. It makes sense to place these methods in the same class file that defines the data type since these associated methods are needed to manipulate objects of that data type. This is an interesting task since it is analogous to the one faced by those people who originally wrote the Java language and had to program up all the methods to do arithmetic with real numbers. On line 30 in our main program we see the statement 30 a = add(b, c);

This statement says to add the complex number b to the complex number c and then to store the result “as” (in the memory location reserved for) the complex number a. You may recall that we initially set the re and im parts of a to zero in line 25 using the default Complex constructor. This statement will replace the initial zero values with those computed in line 30. Nevertheless, it often helps the debugging process to have zero initial values. The method add that adds two complex numbers is defined on lines 11–15. It starts with the statement 20 public static Complex add(Complex a, Complex b)

The method is declared to be static and takes as its input arguments the two Complex objects (numbers) a and b. The fact that the word Complex precedes the method’s name add signifies that the method will return a Complex number object as its result. We had the option of defining other names like complex add or plus for this addition method, but we opted to keep things simple instead. The calculational part of the add method starts on line 12 by declaring and creating a temporary complex number temp that will contain the result of the addition of the complex numbers a and b. As indicated before, the dot operator convention with objects means that temp.re will contain the re part of temp and that temp.im will contain the imaginary part. Thus the statements on lines 13 and 14, 13 temp.re = a.re + b.re; 14 temp.im = a.im + b.im;

add the complex numbers a and b by extracting the real parts of each, adding them together, and then storing the result as the re part of temp. Line 20 determines the imaginary part of the sum in an analogous manner. Finally, the statement 21 return temp;

returns the object (complex number) temp as the value of add(Complex a, Complex b). Because a complex number has two parts, both parts must be re-

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

327

turned to the calling program, and this is what return temp does.

16.4.2.1 Nonstatic Methods* In this section we present a nonstatic approach for dealing with complex objects. If you have just read the section on static methods for dealing with objects and feel somewhat confused by it, we recommend that you jump ahead to the exercises to get some experience with the more elementary aspects of objects before using the nonstatic methods described here. Then come back here to see what nonstatic methods are all about. The program ComplexDyn.java in Listing 16.2 at the end of this section also adds and multiplies complex numbers as objects, but it uses what are called dynamic, nonstatic, or interactive methods. This is more elegant and powerful, but less like the procedural programming we have been doing up till now. To avoid confusion and to permit you to run both the static and nonstatic versions without them interfering with each other, the nonstatic version is called ComplexDyn, in contrast to the Complex used for the static method. Inspect how the names of the methods in ComplexDyn and Complex are the same, although they go about their work differently. Exercise: 1. Enter the ComplexDyn.java class file by hand, trying to understand it in the process. If you have entered Complex.java by hand, you may modify that program to save some time (but be careful!). 2. Compile and execute this program, and check that the output agrees with the ♠ results you obtained in the exercises in §16.2.1. Nonstatic methods go about their work by modifying the properties of the objects to which they are attached (for the present case the objects are complex numbers). In fact, we shall see that nonstatic methods are literally appended to the name of objects much as the endings of verbs are modified when their tenses change. In other words, the method gets appended to the object and in so doing becomes part of the object. To cite an instance, on line 27 we see the operation // Nonstatic addition

27 c.add(b);

Study the way this statement says to take the complex number object c and modify it using the add method that adds b to the object. This results in new values for the parts of object c. Because object c gets modified by this action, line 27 is equivalent to the static operation c = add(c,b);

//

Static method equivalent

Regardless of the approach, since c now contains the sum c + b, if we want to use

328

CHAPTER 16

c again we must redefine it, as we do on line 30. On line 31 we take object c and multiply it by b with, 31 c.mult(b);

// Nonstatic multiplication

This method changes c to c * b. Thus line 31 has the static-method equivalence c = mult(c, b);

// Static method equivalent

We see from these two examples that nonstatic methods are called using the same dot operator that is used to refer to instance variables of an object. On the other hand, static methods take the object as arguments and do not use the dot operator. Thus we called the static methods using add(c,b) and mult(c,b) and called the nonstatic methods using c.add(b) and c.mult(b). The static methods here do not need a dot operator, since they are called from within the class Complex or ComplexDyn that defined them. However, they would need a dot operator if called from another class. As an instance, you have already seen what we called the method Math.sqrt(x). This is actually the static method sqrt from the class Math. You could call the static add(c,b) method of class Complex by using Complex.add(c,b). This works within Complex as well, but is not required. It is required, however, if add is called from other classes. Observe now how the object-oriented add method has the distinctive form: public void add(ComplexDyn other) {this.re = this.re + other.re; this.im = this.im + other.im;}

Line 11 tells us that the method add is nonstatic (the word static is absent), that no value or object is returned (the void), and that there is one argument other of the type ComplexDyn. What is clearly unusual about this nonstatic method is that it is supposed to add two complex numbers together, yet there is only one argument given to the method and no object returned! Indeed, the assumption is that since the method is nonstatic, it will only be used to modify the object that called it. Hence it “goes without saying” that there is an object around for this method to modify, and the this reference is used to refer to “this” calling object. In fact, the reason the argument to the method is conventionally called other, is to distinguish it from the this object that the method will modify. (We are being verbose for clarity’s sake: the word “this” may be left out of these statements without changing their actions.) Consequently, when the object addition is done in line 12 with 12 this.re = this.re + other.re;

// Addition of re parts of this and other

it is understood that re will refer to the current object being modified (this), while other will refer to the “other” object that is being used to make the modifications.

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

Listing 16.2 ComplexDyn.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

21 22 23 24 25

26 27 28 29 30 31 32



329

33



// ComplexDyn . j a v a : non− s t a t i c members c l a s s p u b l i c c l a s s ComplexDyn { p u b l i c double r e ; / / Nonstatic variables : entire class p u b l i c d o u b l e im ; p u b l i c ComplexDyn ( ) / / Default constructor { re = 0; im = 0 ; } p u b l i c ComplexDyn ( d o u b l e x , d o u b l e y ) / / Constructor { re = x ; im = y ; } p u b l i c v o i d add ( ComplexDyn o t h e r ) / / Dynamic o t h e r + t h i s { this . re = this . re + other . re ; t h i s . im = t h i s . im + o t h e r . im ; } p u b l i c v o i d m u l t ( ComplexDyn o t h e r ) / / Dynamic o t h e r ∗ t h i s { ComplexDyn a n s = new ComplexDyn ( ) ; / / Intermediate a n s . r e = t h i s . r e ∗ o t h e r . r e − t h i s . im ∗ o t h e r . im ; a n s . im = t h i s . r e ∗ o t h e r . im + t h i s . im ∗ o t h e r . r e ; t h i s . re = ans . re ; / / Copy v a l u e i n t o r e t u r n e d o b j e c t t h i s . im = a n s . im ; } p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) / / S t a t i c Main : o b j e c t indep { ComplexDyn a , b ; / / D e c l a r e 2 Complex o b j e c t s a = new ComplexDyn ( ) ; / / Create the o b j e c t s b = new ComplexDyn ( 4 . 7 , 3 . 2 ) ; ComplexDyn c = new ComplexDyn ( 3 . 1 , 2 . 4 ) ; / / Declare , c r e a t e System . o u t . p r i n t l n ( "a,b = ("+ a . r e +","+a . im+"),("+b . r e +","+b . im+")," ) ; System . o u t . p r i n t l n ( "c = ("+c . r e +", "+c . im+")" ) ; c . add ( b ) ; / / Non− s t a t i c a d d i t i o n a = c; System . o u t . p r i n t l n ( "b+c = (" + a . r e + ", " + a . im+")," ) ; c = new ComplexDyn ( 3 . 1 , 2 . 4 ) ; c . mult ( b ) ; / / Non− s t a t i c m u l t i p l i c a t i o n System . o u t . p r i n t l n ( "b*c = (" + c . r e + ", " + c . im+")" ) ; }}

16.5 JAVA SOLUTION: COMPLEX CURRENTS 1. Extend the class Complex.java or ComplexDyn.java by adding new methods to subtract, take the modulus, take the complex conjugate, and determine the phase of complex numbers. 2. Test your methods by checking that the following identities hold for a variety of complex numbers: z + z = 2z, z + z∗ = 2 Rez z − z = 0, z − z∗ = 2 Imz 2 = r2 zz∗ = |z| , zz∗

(16.32) (which is real)

330

CHAPTER 16

Hint: Compare your output to some cases of pure real, pure imaginary, and simple complex numbers that you are able to evaluate by hand. 3. Equations (16.29) and (16.27) are the solution for the current in a single RLC circuit. It tells us that the magnitude of the current is given by ( ( ( V0 ( (, Z(

|I| = ((

(16.33)

and that the phase of the current (relative to the cos ωt time dependence) is θI = tan

−1





1/ωC − ωL . R

(16.34)

Modify the given complex arithmetic program so that it performs the complex arithmetic required by (16.33). Hint: You do not have to solve this from scratch! Instead, use the techniques you have already programmed for determining the magnitude to determine |I|. 4. Compute and then make a plot of the magnitude and the phase of the current in the circuit as a function of frequency ω of the external voltage source. For our problem, a good range is 0 ≤ ω ≤ 2. 5. Assessment: You should notice a resonance peak in the magnitude at the same frequency for which the phase vanishes. The smaller the resistance R, the sharper should the circuit pass through resonance. These types of circuits were used in the early days of radio to tune to a specific frequency. The sharper the peak, the better the quality of reception. 6. The second part of the problem dealing with the two circuits in parallel is very similar to the first part. You need to change only the value of the impedance Z used. To do that, explicitly perform the complex arithmetic implied by (16.31), deduce a new value for the impedance, and then repeat the calculation of the current. 16.6 MAPLE SOLUTION: COMPLEX CURRENTS Recall from way back in Chapter 3 that Maple knows all about complex numbers and complex arithmetic. Indeed, you have probably already seen an occasional I pop up as the solution to some equations. The point here is that Maple reserves the √ symbol I as the −1, and this lets us use its I at our pleasure: > restart:

‘sqrt (-1)‘ = sqrt (-1);

# What’s returned?

sqrt ( −1 ) = I > expand((a + I*b)ˆ2);

a2 + 2 I a b − b2

# See if Maple uses I as



−1

In §16.3 we applied circuit theory and complex analysis to the RLC circuit and found that if the exciting voltage is the real part of V (t) = V0 e(−I ω t) ,

(16.35)

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

331

then the current in the circuit is I(t) =

V0 cos(ω t − θ) . |Z|

(16.36)

Here Z is the complex impedance, > Z

:= R + I * (1/(omega*C) - omega*L);

Z := R + (

1 − ω L) I. ωC

We will start our Maple investigation by trying to determine magnitude |Z| and phase θ of Z using Maple’s capabilities for complex analysis. Whereas needing to know the magnitude and phase of Z is just another way of saying that we need to know Z in polar notation, we have Maple calculate these for us: > Re(Z);

Im(Z);

# ReZ, ImZ

1 1 − ω L) I) − ω L) I) Re(R + ( Im(R + ( ωC ωC This is just a fancy way of giving us back the input. The problem is that Maple does not know that ω , R, L, and C are real, and so it cannot do the complex arithmetic. To tell Maple that the constants are real, we tell it what to assume: > assume (R, real); assume(L, real); assume(C, real); assume (omega, real); > Re(Z); Im(Z); # Now check again

1 − ω˜ L˜ ω˜ C ˜ It has taken some work, but now we are ready for some complex arithmetic. Look at the polar form: R˜

> polar(Z);

*

polar( R˜2 + (

1 − ω˜ L˜)2 , ω˜ C ˜

argument(R˜ + (

1 − ω˜ L˜) I)) ω˜ C ˜

This is giving us the correct magnitude, but Maple appears unable to compute the phase. This seems to be a Maple failure. However, we get it to work by combining the map and evalc commands: > Polar := map( evalc, polar(Z) );

*

# Polar is variable name

1 1 − ω˜ L˜)2 , arctan( − ω˜ L˜, R˜)) ω˜ C ˜ ω˜ C ˜ Here map applies the procedure evalc to each element of polar(Z) and returns the polar form (|Z|, θ). This simplifies the elements into the form needed. [It should not be this hard!] We get separate expressions out for the real and imaginary parts Polar := polar( R˜2 + (

332

CHAPTER 16

by using the command op(numb,expression) to extract the numb-th operand from expression: > op(1,Polar);

# Extract operands

op(2,Polar);

*

1 1 − ω˜ L˜)2 − ω˜ L˜, R˜) arctan( ω˜ C ˜ ω˜ C ˜ The expressions we have just derived are the standard ones for the magnitude and the phase of the impedance Z . However, since the current is proportional to 1/|Z|, we will plot 1/|Z|. We could compute 1/Z and work with that as well. R˜2 + (

16.6.1 Maple’s Surface Plots of Complex Impedance We want to examine the current that occurs in the RLC circuit as a function of the driving frequency. We have already discussed in Chapter 4 some of Maple’s commands for plotting complex functions. Irrespective of them being illuminating, for the problem given here, we first use plot3d to make the familiar z(x, y) surface plot of the magnitude and phase of the current as functions of both the frequency of the external voltage ω and of the resistance R: Zinv := 1/(R + I * (1/(omega*C) - omega*L)); # Compute 1/Z C :=1/1000; # Assign numerical values

> restart: > L := 1000;

Zinv :=

1 R + ( ω1C − ω L) I

L := 1000

> assume (R, real); assume (omega, real); > Polar := map( evalc, polar(Zinv) );

C :=

1 1000

# Tell Maple the constants are real # Convert 1/Z to polar form

> polar(1/((Rˆ2+(1000/omega-1000*omega)ˆ2)ˆ(1/2)), > arctan(-1000/omega + 1000*omega, R));





1

Polar := polar ⎝

2

R˜ + > mag := op(1, Polar);

mag := 

( 1000 ω˜

− 1000 ω˜)2

R˜ +

( 1000 ω˜

− 1000 ω˜)2

1000 + 1000 ω˜, R˜)⎠ ω˜ # Extract magnitude, phase

op(2, Polar);

1 2

, arctan(−

arctan(−

1000 + 1000 ω˜, R˜) ω˜

Check out √ how the magnitude has a maximum when the external frequency ω = 1/ LC . This is the resonance frequency. For our choice of constants this corresponds to ω = 1. We now make some plots to see if this is true. > op(1, Polar); with(plots): > plot3d(op(1, Polar), omega = 0..2, R = 200..1000, axes = BOXED); # 1/Z vs ω

333

OBJECT-ORIENTED PROGRAMMING, ABSTRACT DATA; COMPLEX CURRENTS

0.005 0.004 0.003 0.002 0.001 0 200

0 0.5

400 R~

1

600 1.5

800 1000

omega~

2

# θ vs ω

> plot3d(op(2,Polar), omega=0..2, R=200..1000, axes = NORMAL);

omega~

1 0.5 200 0.5

400 –1

2

600 R~ 800 1000

0.016 0.012 0.008 0.004 0 –1

0 0.5

–0.5 y

1

0 1.5

0.5 1

x

2

Sure enough, the plot of 1/|Z| shows that the magnitude of the current has a maximum at ω = 1. (It helps to grab and rotate these plots to see them better.) We also see that as the resistance R in the circuit is made smaller, the maximum current becomes progressively larger. The second plot of the phase shows that below resonance, ω < 1, the current lags the voltage, while above resonance the current leads the voltage. Another way to visualize complex functions is with the command complexplot3d. It makes a 3-D visualization of a complex function of a complex argument. Here we do it by treating the frequency ω = x + iy as a complex number: > with(plots): R := 1000; > complexplot3d( 1/(R+I*(1/(w*C)-w*L) ), w = 0-I..2+I, axes = BOXED);

# 1/Z

We see in the right plot above that there is a sharp peak at x = Re(ω) = 1, as expected. The color change indicates where Im(1/Z) changes sign. If we look closely at this graph we will also see that there is a maximum for a negative imaginary value of ω . This is related to how long the resonance stays excited, a statement we do not try to prove here.

334

CHAPTER 16

Superposition of Two Sine Waves 2 1 0 -1 -2 0.0

0.5

1.0

1.5

2.0

2.5 x

3.0

3.5

4.0

4.5

5.0

Figure 16.4 Superposition of two waves with similar wave numbers (a PtPlot).

16.7 EXPLORATIONS: OOP WORKED EXAMPLES* Creating object-oriented programs (OOP) requires a transition from a proceduralprogramming mindset in which functions take arguments as input and produce answers as output, to one in which objects are created, probed, transferred, and modified. To assist you in the transition, we present here (courtesy of Manuel P´aez) two sample procedural programs and their OOP counterparts. In both cases the OOP examples are longer but presumably easier to modify and extend.

16.7.1 OOP Beats Listing 16.3 Beats.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19



20

// Beats . java : plots beats import p t o l e m y . p l o t . ∗ ; public class Beats { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) { d o u b l e y1 , y2 , y3 , x ; int i ; x = 0.; // Initial position P l o t m y P l o t = new P l o t ( ) ; m y P l o t . s e t T i t l e ( "Superposition of Two Sine Waves" ) ; m y P l o t . s e t X L a b e l ( " x" ) ; f o r ( i = 1 ; i = 0 . 0 ; t += 0 . 0 2 ) { d o u b l e KEcm = myBaton . getKEcm ( t ) ; m y P l o t . a d d P o i n t ( 0 , t , KEcm , t r u e ) ; }



Compile the modified Baton.java and check that your plot is physically reasonable. The translational kinetic energy should decrease and then increase as the baton goes up and then comes down. 5. Write a method in the Baton class that computes the kinetic energy of rotation about the CM, KEr = 12 Iω 2 . Call getI to extract the moment of inertia of the baton, and check that all classes still compile properly. 6. The potential energy of the baton P E(t) = mgycm (t) is that of a point particle with the total mass of the baton located at the CM. Write a method in the Baton class that computes P E . Use getM to extract the mass of the baton, and use path.g to extract the acceleration due to gravity g . To determine the height as a function of time, write a method path.getY(t) that accesses the path object. Make sure that the methods getKEcm, getKEr, and getPE are in the Baton class. 7. Plot on one graph: the translational kinetic energy, the kinetic energy of rotation, the potential energy, and the total energy. The plots may be obtained with commands such as:  1 2 3 4 5 6 7 8



9

f o r ( d o u b l e t = 0 . ; myPath . getY ( t ) >= 0 . ; t += 0 . 0 2 ) { d o u b l e KEcm = myBaton . getKEcm ( t ) ; / / KE o f CM d o u b l e KEr = myBaton . g e t K E r ( t ) ; / / KE o f r o t a t i o n d o u b l e PE = myBaton . g e t P E ( t ) ; // Potential d o u b l e t o t a l = KEcm + KEr + PE ; / / T o t a l Energy m y P l o t . a d d P o i n t ( 0 , t , KEcm , t r u e ) ; / / To d a t a s e t 0 m y P l o t . a d d P o i n t ( 1 , t , KEr , t r u e ) ; / / To d a t a s e t 1 m y P l o t . a d d P o i n t ( 2 , t , PE , t r u e ) ; / / To d a t a s e t 2 myPlot . a d d P o i n t ( 3 , t , t o t a l , true ) ; } / / To d a t a s e t 3

Check that all the plotting commands are object-oriented, with myPlot being the plot object. Label your data sets with a myPlot.addLegend command outside of the for loop, and check that your graph is physically reasonable. The total energy and rotational energies should both remain constant in time. However, the gravitational potential energy should fluctuate.



ADVANCED OBJECTS; BATON PROJECTILES*

369

18.3.7 Tutorial: Inheritance and Object Hierarchies Up until this point we have built up our Java classes via composition, namely, by placing objects inside other objects (using objects as arguments). As powerful as composition is, it is not appropriate for all circumstances. For example, you may want to modify the Baton class to create similar, but not identical, objects such as 3-D batons. A direct approach to extending the program would be to copy and paste parts of the original code into a new class, and then modify the new class. However, this is error-prone and leads to long, complicated, and repetitive code. The OOP approach applies the concept of inheritance to allow us to create new objects that inherit the properties of old objects but have additional properties as well. This is how we create entire hierarchies of objects. As an example, let us say we want to place red balls on the end of the baton. We make a new class RedBall that inherits properties from Ball by using the extend command:  1



2

public c l a s s RedBall extends Ball { . . .



As written, this code creates a RedBall class that is identical to the original Ball class. The keyword extends tells Java to copy all of the methods and variables from Ball into RedBall. In OOP terminology, the Ball class is the parent class or superclass, and the RedBall class is the child class or subclass. It follows then that a class hierarchy is a sort of family tree for classes, with the parent classes at the top of the tree. As things go, children beget children of their own and trees often grow high. To make RedBall different from Ball, we add the property of color:  1 2 3 4



5

public c l a s s RedBall extends Ball { String getColor () { r e t u r n "Red" ; } double getR ( ) { return 1 . 0 ; } }

Now we append the getColor method to a RedBall to find out its color. Consider what it means to have the getR method defined in both the Ball and RedBall classes. We do this because the Java compiler assumes that the getR method in RedBall is more specialized, so it ignores the original method in the Ball class. In computer science language, we would say that the new getR method overrides the original method.



370

CHAPTER 18

18.3.8 Application: Baton with a Lead Weight As a second example, we employ inheritance to create a class of objects representing a baton with a weight at its center. We call this new class LeadBaton.java and make it a child of the parent class Baton.java. Consequently, the LeadBaton class inherits all of the methods from the parent Baton class, in addition to having new ones of its own: 

 1 2 3 4 5 6 7



8

/ / LeadBaton : c l a s s i n h e r i t a n c e o f methods from Baton p u b l i c c l a s s LeadBaton { p u b l i c d o u b l e M; / / Non− s t a t i c c l a s s v a r i a b l e s L e a d B a t o n ( P a t h p , B a l l b , d o u b l e L1 , d o u b l e w1 , d o u b l e M1) { s u p e r ( p , b , L1 , w1 ) ; / / Baton c o n s t r u c t o r M = M1; } p u b l i c d o u b l e getM ( ) r e t u r n s u p e r . getM ( ) + M; } / / C a l l getM i n B a t o n

Here the nondefault constructor LeadBaton(...) takes five arguments, while the Baton constructor takes only three. For the LeadBaton constructor to work, it must call the Baton constructor in order to inherit the properties of a Baton. This is accomplished by use of the key word super, which is shorthand for look in the superclass, and tells Java to look in the parent, or superclass for the constructor. We may also call methods with the super key word; for example, super.getM() will call the getM method from Baton, in place of the getM method from LeadBaton. Finally, because the LeadBaton class assigns new values to the mass, LeadBaton overrides the getM method of Baton.

Exercise: 1. Run and create plots from the LeadBaton class. Start by removing the main method from Baton.java and placing it in the file Main.java. Instead of creating a Baton, now create a LeadBaton with: LeadBaton myBaton = new LeadBaton(myPath, myBall, 2.5, 15., 10.);

Here the 10. argument describes a 10 kg mass at the center of the baton. 2. Compile and run the main method, remembering to use the “-classpath .” option if needed. You should get a plot of the energies of the lead baton versus time. Compare its energy to an ordinary baton’s and comment on the differences. 3. You should see now how OOP permits us to create many types of batons with only slight modifications of the code. You switch between a Baton and a LeadBaton object with only a single change to main, a modification that would be ♠ significantly more difficult with procedural programming.

ADVANCED OBJECTS; BATON PROJECTILES*

371

18.3.9 Encapsulation to Protect Classes In the previous section we created the classes for Ball, Path, and Baton objects. In all cases the Java source code for each class had the same basic structure: class variables, constructors, and get methods. Yet classes do different things, and it is common to categorize the functions of classes as either: Interface: how the outside world manipulates an object; all methods that are applied to that object; Implementation: the actual internal workings of an object; how the methods make their changes. As applied to our program, the interface for the Ball class includes a constructor and the getM, getR, and getI methods. The interface for the Path class includes a constructor Path and the getX and getY methods. Ball,

Pure OOP strives to keep objects abstract and manipulate them only through methods. This makes it easy to follow and to control where variables get changed, and thereby makes modifying an existing program easier and less error-prone. With this purpose in mind, we separate methods into those that perform calculations and those that cause the object to do things. In addition, to protect your object from being misused by outsiders, we invoke the private (in contrast to public) key word when declaring class variables. This ensures that these variables may be accessed and changed only from inside the class. Outside code may still manipulate our objects, but it will have to do so by calling the methods we have tested and know will not damage our objects. Once we have constructed the methods and made the class variables private, we have objects whose internal codes are entirely hidden to outside users, and thereby protected. As authors, we may rewrite the objects’ codes as we want and still have the same working object with a fixed interface for the rest of the world. Furthermore, since the object’s interface is constant, even though we may change the object, there is no need to modify any code that uses the object. This is a great advance in the ability to reuse code and to use other’s people’s codes properly. This two-step process of creating and protecting abstract objects is known as encapsulation. An encapsulated object may be manipulated only in a general manner that keeps the irrelevant details of its internal workings safely hidden within. Just what constitutes an “irrelevant detail” is in the eye of the programmer. In general, you should place the private key word before every nonstatic class variable, and then write the appropriate methods for accessing the relevant variables. This OOP process of hiding the object’s variables is called data hiding.

372

CHAPTER 18

18.3.10 Encapsulation Exercise 1. Place the private key word before all class variables in Ball.java. This accomplishes the first step in encapsulating an object. Print out myBall.m and myBall.r from the main method. The Java compiler should complain, because the variables are now private (visible to Ball class member only), and the main method is outside the Ball class. 2. Make methods that allow us to manipulate the object in an abstract way; for example, to modify the mass of a Ball object and assign it to the private class variable m, include the line command myBall.setM(5.0). This is the second step in encapsulation. We already have the methods getM, getR, and getI, and the object constructor Ball, but they do not assign a mass to the ball. Insofar as we have used a method to change the private variable m, we have kept our code as general as possible and still have our objects encapsulated. 3. When we write getM() we are saying that M is the property to be retrieved from a Ball object. Inversely, the method setM sets the property M of an object equal to the argument that is given. This is part of encapsulation, because with both get and set methods on hand, you do not need to access the class variables from outside of the class. The use of get and set methods is standard practice in Java. You do not have to write get and set methods for every class that you create, but you should create these methods for any class you want encapsulated. If you look back at Chapter 11, “Visualization with Java,” you will see that the classes in the PtPlot library have many get and set methods, for example, getTitle, setTitle, getXLabel, and setXLabel. 4.

Java’s interface key word BallInterface defines an interface

 1 2 3



4

allows us to specify an interface. for Ball-like objects:

Here

public interface B a l l I n t e r f a c e { p u b l i c d o u b l e getM ( ) ; p u b l i c double getR ( ) ; p u b l i c double g e t I ( ) ; }

This interface does not do anything by itself, but if you modify Ball.java so that public class Ball is replaced by public class Ball implements BallInterface then the Java compiler will check that the Ball class has all of the methods that are specified in the interface. The Java interface and implements commands are useful for having the compiler check that your classes have all of the required methods. 5. Add an arbitrary new method to the interface and compile Ball. If the method is found in Ball.java, then the Ball class will compile without error.



373

ADVANCED OBJECTS; BATON PROJECTILES*

Listing 18.4 KomplexInterface.java



 1 2 3 4 5 6 7 8 9 10 11



12

// KomplexInterface : c o m p l e x numbers v i a i n t e r f a c e public interface KomplexInterface { p u b l i c double getRe ( ) ; p u b l i c double getIm ( ) ; p u b l i c double setRe ( ) ; p u b l i c double setIm ( ) ; / / type = 0: polar r e p r e s e n t a t i o n ; other : rectangular p u b l i c v o i d add ( Komplex o t h e r , i n t t y p e ) ; p u b l i c v o i d s u b ( Komplex o t h e r , i n t t y p e ) ; p u b l i c v o i d m u l t ( Komplex o t h e r , i n t t y p e ) ; p u b l i c v o i d d i v ( Komplex o t h e r , i n t t y p e ) ; public void conj ( i n t type ) ; }

18.3.11 Extension: Complex Number Objects, Komplex.java In Listing 18.4 we display our design KomplexInterface.java of an interface for complex numbers. To avoid confusion with the Complex objects of Chapter 16, we call the new objects Komplex. We include methods for addition, subtraction, multiplication, division, negation, and conjugation, as well as get and set methods for the real, imaginary, modulus, and phase. We include all methods in the interface and check that javac compiles the interface without error. Remember, an interface must give the arguments and return type for each method. We still represent complex numbers in Cartesian or polar coordinates: z = x + iy = reiθ .

(18.17)

Insofar as the complex number itself is independent of representation, we should be able to switch between a rectangular or polar representation. This is useful because certain mathematical manipulations are simpler in one representation than the other, for example, z1 a + ib r1 eiθ1 r1 ac + bd + i(bc − ad) = = = ei(θ1 −θ2 ) . = 2 2 iθ 2 z2 c + id c +d r2 e r2

(18.18)

374

CHAPTER 18

Listing 18.5 Komplex.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

26 27 28

29

30 31 32 33 34 35

36

37 38 39

40

41 42 43 44

// Komplex : C a r t e s i a n / p o l a r c o m p l e x v i a i n t e r f a c e ’ t y p e = 0 ’ −> p o l a r r e p r e s e n t a t i o n , e l s e r e c t a n g u l a r p u b l i c c l a s s Komplex implements K o m p l e x I n t e r f a c e { p u b l i c d o u b l e mod , t h e t a , r e , im ; p u b l i c Komplex ( ) / / Default constructor { mod = 0 ; theta = 0; re = 0; im = 0 ; } p u b l i c Komplex ( d o u b l e x , d o u b l e y , i n t t y p e ) / / Constructor { i f ( t y p e == 0 ) {mod = x ; t h e t a = y ; } e l s e { r e = x ; im = y ; } } p u b l i c d o u b l e g e t R e ( ) r e t u r n mod∗ Math . c o s ( t h e t a ) ; p u b l i c d o u b l e g e t I m ( ) r e t u r n mod∗ Math . s i n ( t h e t a ) ; p u b l i c double setRe ( ) { r e = mod∗Math . c o s ( t h e t a ) ; return re ;} p u b l i c double setIm ( ) { im = mod∗Math . s i n ( t h e t a ) ; r e t u r n im ; } p u b l i c v o i d add ( Komplex o t h e r , i n t t y p e ) { d o u b l e tempMod = 0 . ; i f ( t y p e == 0 ) { tempMod = Math . s q r t ( Math . pow ( t h i s . mod , 2 ) +Math . pow ( o t h e r . mod ,2) + 2∗ t h i s . mod∗ o t h e r . mod∗Math . c o s ( t h i s . t h e t a −o t h e r . t h e t a ) ) ; t h i s . t h e t a = Math . a t a n 2 ( t h i s . mod∗ Math . s i n ( t h i s . t h e t a ) + o t h e r . mod∗Math . s i n ( o t h e r . theta ) , t h i s . mod∗Math . c o s ( t h i s . t h e t a ) + o t h e r . mod∗Math . c o s ( o t h e r . theta ) ) ; t h i s . mod = tempMod ; } else { this . re = this . re + other . re ; t h i s . im = t h i s . im + o t h e r . im ; } } p u b l i c v o i d s u b ( Komplex o t h e r , i n t t y p e ) { i f ( t y p e == 0 ) { t h i s . mod = Math . s q r t ( Math . pow ( t h i s . mod , 2 ) +Math . pow ( o t h e r . mod , 2 )− 2∗ t h i s . mod∗ o t h e r . mod ∗ ( Math . c o s ( t h i s . t h e t a ) ∗ Math . c o s ( o t h e r . theta )+ Math . s i n ( t h i s . t h e t a ) ∗ Math . s i n ( o t h e r . t h e t a ) ) ) ; this . theta = Math . a t a n ( ( t h i s . mod∗Math . s i n ( t h i s . t h e t a )−o t h e r . mod∗ Math . s i n ( other . theta ) ) / ( t h i s . mod∗ Math . c o s ( t h i s . t h e t a )−o t h e r . mod∗Math . c o s ( o t h e r . t h e t a ) )); } e l s e { t h i s . r e = t h i s . r e −o t h e r . r e ; t h i s . im = t h i s . im−o t h e r . im ; } } p u b l i c v o i d d i v ( Komplex o t h e r , i n t t y p e ) { i f ( t y p e == 0 ) { t h i s . mod = t h i s . mod / o t h e r . mod ;



ADVANCED OBJECTS; BATON PROJECTILES* 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65



66

375

t h i s . t h e t a = t h i s . t h e t a −o t h e r . t h e t a ; } e l s e { t h i s . r e = ( t h i s . r e ∗ o t h e r . r e + t h i s . im∗ o t h e r . im ) / ( Math . pow ( o t h e r . r e , 2 ) +Math . pow ( o t h e r . im , 2 ) ) ; t h i s . im = ( t h i s . im∗ o t h e r . r e −t h i s . r e ∗ o t h e r . im ) / ( Math . pow ( o t h e r . r e , 2 ) +Math . pow ( o t h e r . im , 2 ) ) ; } } p u b l i c v o i d m u l t ( Komplex o t h e r , i n t t y p e ) { i f ( t y p e == 0 ) { t h i s . mod = t h i s . mod∗ o t h e r . mod ; this . theta = this . theta+other . theta ; } else { Komplex a n s = new Komplex ( ) ; a n s . r e = t h i s . r e ∗ o t h e r . r e −t h i s . im∗ o t h e r . im ; a n s . im = t h i s . r e ∗ o t h e r . im+ t h i s . im∗ o t h e r . r e ; t h i s . re = ans . re ; t h i s . im = a n s . im ; }} public void conj ( i n t type ) { i f ( t y p e == 0 ) { t h i s . mod = t h i s . mod ; t h i s . t h e t a = −t h i s . t h e t a ; } else { this . re = this . re ; t h i s . im = − t h i s . im ; } } }

Here is our implementation of an interface that permits us to use either representation when manipulating complex numbers. There are three files, Komplex, KomplexInterface, and KomplexTest, all given in the listings. Because these classes call each other, each must be in a class by itself. However, for the compiler to find all the classes that it needs, all three classes must be compiled with the same javac command: % javac Komplex.java KomplexInterface.java KomplexTest.java % java KomplexTest

// Run test

We see that KomplexInterface requires us to have methods for getting and setting the real and imaginary parts of Komplex objects, as well as adding, subtracting, multiplying, dividing, and conjugating complex objects. (In comments we see the suggestion that there should also be methods for getting and setting the modulus and phase.) The class Komplex contains the constructors for Komplex objects. This differs from our previous implementation Complex by having the additional integer variable type. If type = 0, then the complex number(s) are in polar representation, else they are in the Cartesian representation. So, for example, the methods for arithmetic, such as the add method on line 22, is actually two different methods depending upon the value of type. In contrast, the get and set methods for real and imaginary parts on lines 14–21 are needed only for the polar representation, and so the value of type is not needed.

376 Listing 18.6 KomplexTest.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

17

18 19 20



CHAPTER 18

21



// KomplexTest : t e s t KomplexInterface p u b l i c c l a s s KomplexTest { p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) { Komplex a , e ; e = new Komplex ( ) ; a = new Komplex ( 1 . 0 , 1 . 0 , 1 ) ; Komplex b = new Komplex ( 1 . , 2 . , 1 ) ; System . o u t . p r i n t l n ( "Cartesian: Re a = "+a . r e +", Im a = "+a . im+"" ) ; System . o u t . p r i n t l n ( "Cartesian: Re b = "+b . r e +", Im b = "+b . im+"" ) ; b . add ( a , 1 ) ; e = b; System . o u t . p r i n t l n ( "Cartesian: e=b+a="+e . r e +" "+e . im+"" ) ; / / P o l a r V e r s i o n , u s e s g e t and s e t m e t h o d s a = new Komplex ( Math . s q r t ( 2 . ) , Math . P I / 4 . , 0 ) ; / / Polar via 0 b = new Komplex ( Math . s q r t ( 5 . ) , Math . a t a n 2 ( 2 . , 1 . ) , 0 ) ; System . o u t . p r i n t l n ( "Polar: Re a = "+a . g e t R e ( ) +", Im a = "+a . g e t I m ( ) +"" ) ; System . o u t . p r i n t l n ( "Polar: Re b = "+b . g e t R e ( ) +", Im b = "+b . g e t I m ( ) +"" ) ; b . add ( a , 0 ) ; e = b; System . o u t . p r i n t l n ( "Polar e=b+a = "+e . g e t R e ( ) +" "+e . g e t I m ( ) +"" ) ; } }

18.3.12 Polymorphism, Variable Multityping Polymorphism allows a variable name that is declared as one type to contain other types as a program runs. The idea may be applied to both the class and the interface. Class polymorphism allows a variable that is declared as one type to contain types it inherits. To illustrate, if we declare myBaton of type Baton, Baton myBaton;

then it is valid to assign an object of type Baton to that variable, which is what we have been doing all along. However, it is also permissible to assign a LeadBaton object to myBaton, and, in fact, it is permissible to assign any other class that inherits from the Baton class to that variable: myBaton = Baton(myPath, myBall, 0.5, 15.); myBaton = LeadBaton(myPath, myBall, 0.9, 20., 15.);

// Usual // OK too

myBaton = LeadBaton(myPath, myBall, 0.1, 1.5, 80.);

// Also OK

Polymorphism applies to the arguments of methods as well. If we declare an argument as type Baton, we are saying that the class must be a Baton, or else some class that is a child class of Baton. This is possible because the child classes

377

ADVANCED OBJECTS; BATON PROJECTILES*

will have the same methods as the original Baton class (a child class may override a method or leave it alone, but it may not eliminate it). 18.4 KEY WORDS abstraction data hiding inheritance object hierarchies polymorphism subclass translation

center of mass dynamic methods interface OOP private subobjects rotation

child class dynamic variables multityping overriding programming superclass

composition encapsulation nonstatic variables parent class reference pass templates

18.5 SUPPLEMENTARY EXERCISES Use a Java interface to introduce another object corresponding to the polar representation of complex numbers: r = x =



x2 + y 2 , r cos θ,

θ = tan−1 (y/x) y = r sin θ.

(18.19)

1. Define a constructor Complex (r, theta, 1) that constructs the polar representation of a complex number from r and θ. (The 1 is there just to add a third argument and thereby to make the constructor unique.) 2. Define a method (static or nonstatic) that permits conversion from the Cartesian to the polar representation of complex numbers. 3. Define a method (static or nonstatic) that permits conversion from the polar to the Cartesian representation of complex numbers. 4. Define methods (static or nonstatic) for addition, subtraction, multiplication, and division of complex numbers in polar representation. [Hint: multiplication and division are a snap for complex numbers in polar representation, while addition and subtraction are much easier for complex numbers in Cartesian representation.]

Chapter Nineteen Discrete Math, Arrays as Bins; Nonlinear Bug Dynamics*

This chapter gives an example of how computations are used to model the population dynamics of biological systems. It employs some fairly simple discrete mathematics that leads to some unusual nonlinear behaviors to explore. Onedimensional arrays are used to bin store the results of the simulations, with the arrays then written to a file and visualized with PtPlot. This chapter introduces no new Java tools, but instead reviews a number of previously introduced techniques. So even though there is no advanced material in it, we mark this chapter as optional since it may be skipped without interrupting the logical flow. 19.1 PROBLEM: VARIABILITY OF BUG POPULATIONS As is true with much in nature, insect populations do not appear to follow any simple patterns. At times they appear stable, at other times they vary periodically, and at still other times they appear chaotic, only to settle down to something simple again.1 Problem: Deduce and explore a simple model of the population as a function of time that seems capable of producing complicated behaviors. 19.2 THEORY: SELF-LIMITING GROWTH, DISCRETE MAPS Imagine a bunch of insects reproducing, generation after generation, in your garden. We start with N0 of them, in the next generation we have to live with N1 bugs, and after i generations there are Nn of them to bug us. We want a model for how Nn varies with the discrete generation number n. We look to the simple model of radioactive decay and growth for guidance. 1 We actually use chaos as a technical term meaning complicated behavior in which there does not appear to be any order, but in which there is some simple mathematical description [CP 05].

DISCRETE MATH, ARRAYS AS BINS; NONLINEAR BUG DYNAMICS*

379

There we know that an exponential time dependence, N (t) = N (0)e±λt ,

(19.1)

follows from the simple discrete law [CP 05]: ∆Nn = ±λNn ∆t.

(19.2)

Here λ is a rate constant, Nn is the number of particles present in generation n, and ∆t is the time for one generation. The model states that the change in the number of particles in one generation is proportional to the number of particles present and to the length of that generation. We know that a discrete equation such as (19.2) produces growth or decay for positive or negative values of λ respectively, but not both. If bugs just bred, then an exponential-growth model might make sense; yet bugs cannot live on love alone. As their numbers grow the bugs start running out of food and this leads to their numbers growing only to some maximum value N∗ . We modify the simple growth model by introducing a growth rate λ that decreases as the population number reaches some maximum: λ = λ (N∗ − Nn ),



∆Nn = λ ∆t(N∗ − Nn )Nn .

(19.3)

We expect that when Nn is small compared to the maximum N∗ , the bugs will grow exponentially, but as Nn becomes comparable in magnitude to N∗ , the growth rate should decrease and possibly become negative if it exceeds N∗ . To make the mathematics of this model clearer, we change to dimensionless variables xn =

λ ∆t Nn Nn  ,  1 + λ ∆tN∗ N∗

µ = 1 + λ ∆tN∗ .

(19.4)

In terms of these, our model assumes the simple form xn+1 = µxn (1 − xn ).

(19.5)

Here xn is essentially the fraction of the maximum population attained in generation n, and µ is a constant we call the survival rate. The range of variation of these dimensionless parameters are limited to: 0 ≤ xn ≤ 1,

0 ≤ µ ≤ 4.

(19.6)

Notwithstanding the simplicity of (19.5) with its one variable x and one parameter µ, it leads to surprisingly complicated behaviors. This is a consequence of its being a nonlinear equation in which the variable x occurs to the second power. It is called the logistics map [Rash 90], and your problem is now reduced to exploring the properties of (19.5).

380

xn

CHAPTER 19 1.0

1.0

0.8

0.8

0.6

xn

0.4

0.4

0.2 0.0

0.6

0.2

A 0

10

20

0.0

30

B 0

10

n

xn

1.0

1.0

0.8

0.8

0.6

xn

0.4

30

0.6 0.4

0.2 0.0

20

n

0.2

C 0

10

20

30

0.0

D 0

10

n

20

30

n

Figure 19.1 The insect population xn versus generation number n for various survival rates: (A) µ = 2.8, a period-one cycle; (B) µ = 3.3, a period-two cycle; (C) µ = 3.5, a periodfour cycle; (D) µ = 3.8, a chaotic regime.

19.3 ASSESSMENT: PROPERTIES OF NONLINEAR MAPS Rather than do some fancy mathematical analysis [Rash 90] to determine properties of the logistics map (19.5), study it directly on the computer. Write a simple program that uses the logistics map to produce a sequence of population values xn as a function of the generation number n. Start with a seed population x0 = 0.75 and plot up xn versus n for µ = (0.4, 2.8, 3.3, 3.5, 3.8). The last four simulations should yield results similar to those in Figure 19.1. Explore other nearby values of µ and identify on printed copies of your graphs the following characteristics: Transients: Irregular behaviors before reaching a steady state. Extinction: If the survival rate is too low, the population dies off. Stable states: Single-population stable states for µ < 3. Two and three cycles: At two-cycle fixed points, the population jumps back and forth between two semistable x∗ values. At three-cycle fixed points the population

DISCRETE MATH, ARRAYS AS BINS; NONLINEAR BUG DYNAMICS*

381

jumps between three x∗ values. These attractors occur only for µ > 3. Intermittency: Try to find solutions for 3.8264 < µ < 3.8304 in which the system appears stable for a while but then jumps all around, only to become stable again. Chaos: The system’s behavior in the chaotic region is critically dependent on the exact value of µ and x0 . Systems may start out much the same but end up quite different. Compare the long-term behaviors of starting with the two essentially identical seeds: x0 = 0.75, x0 = 0.75(1 + ), (19.7) where   2 × 10−14 for a double-precision calculation. Repeat the experiment with x0 = 0.75, but with what should essentially be two identical survival parameters: µ = 4.0, µ = 4.0(1 − ). (19.8) 19.4 EXPLORATION: BIFURCATION DIAGRAM, BUGSORT.JAVA* Listing 19.1 BugSort.java

 1 2 3 4 5 6 7

8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24



25

// BugSort : B i f u r c a t i o n d i a g r a m f o r l o g i s t i c map ( s o r t e d ) import j a v a . i o . ∗ ; public c l a s s BugSort { s t a t i c f l o a t m min = 0 . 0 f ; / / Minimum f o r m s t a t i c f l o a t m max = 4 . 0 f ; / / Maximum f o r m static float step = 0.1 f ; / / Stepsize for m p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g v ) throws I O E x c e p t i o n , FileNotFoundException { f l o a t m; f l o a t x [ ] = new f l o a t [ 1 0 0 0 ] ; int i ; / / Save data i n F i l e BugSort . dat P r i n t W r i t e r w = new P r i n t W r i t e r ( new F i l e O u t p u t S t r e a m ( "BugSort. dat" ) , t r u e ) ; f o r (m = m min ; m

A p p l e t c a l l e d from t h i s HTML f i l e .







guage used to create hypertext documents. The “markup” aspect of the language refers to marks, analogous to those used by human typesetters to “mark up” a manuscript in preparation for typesetting, which indicate things like font type and spacings. As we shall see, the marks in HTML are tags like enclosed in angle brackets.1 All HTML files end with the suffix .html (or sometimes .htm on Windows machines). Browsers read HTML files and both format the text it contains to look like a printed page, and display the referenced multimedia content appropriately. If the browser is modern, it should be equipped with a Java plug-in (an addition) that permits it to execute .class files. However, browsers cannot compile .java files (only developers, like you with SDK, are capable of doing that). This is actually quite simple with applets. The Java programs that we have been writing so far are referred to as applications, in recognition of the fact that they apply computer science to something useful. Yet if you add two lines and modify one other, you will change an application program into an applet whose .class file runs via the Web via a browser. These files are called “applets,” since it is envisioned that they will be small applications that will not overload someone else’s computer when run over the Web. All applets run on the computer reading the Web page, and not on the server where the page is stored (that is a good thing because educational institutions are terribly underfunded and cannot afford to supply computer time to the whole world.) An important thing about Java is that the language is written with a high degree of attention to security, which in this case means restricting which areas of memory a program may access. This is a good thing if you are to let someone else’s program (applet) run on your computer, and it is one example of how 1 We do not do much with HTML in this chapter. However, in Part 3 of this book we do study another markup language, LATEX .



396

CHAPTER 21

Figure 21.2 A screen dump of the computer screen showing output from Applet1.

Java is designed for Web computing. For this reason, programs that read and write files will not work as applets, since they will not be permitted to write (and possibly damage) files on someone else’s computer. Likewise, we have seen that Java methods deal with arrays via reference calls in which the address of the array in memory is passed to the method. There are no commands in Java that permit you to manipulate these addresses or go to some other address in memory where you do not belong. This is in contrast to the use of pointers in a language like C, where you manipulate memory locations to do some very powerful programming—or do tremendous harm, even if that was not your intention. The use of applets also protects you, the code provider. On the one hand, you are transmitting only your .class file to the Web computer, and so your source file cannot be damaged, or plagiarized, by the Web user. On the other hand, the .class file is running on their computer, not yours, and so regardless of how it fails, it cannot harm you.

21.2 IMPLEMENTATION: GET THIS TO WORK FIRST If you are on a computer that has been set up to serve documents to the Web (a “Web server”), then the files you place in the directory or folder public html are viewable by anyone on the Web. It follows that if the .class file from your applet program is in public html, and if this class file is called by some .html file, then anyone’s browser is used to run your applet on their computer, wherever and whatever their computer may be. To see how this all works (and make more sense of all these words), examine It is the HTML that calls the applet after being read by a browser, and produces an image on your screen like Figure 21.2. The executeable applet is the compiled version of Applet1.java in Listing 21.2.

CallApplet.html in Listing 21.1.

WEB COMPUTING, APPLETS, PRIMITIVE GRAPHICS

397

1. Save a copy of CallApplet.html in your public html directory/folder. If you do not have such a directory, then create one. You can copy CallApplet.html from the CD, from the Web, or from Listing 21.1. Listing 21.2 Applet1.java

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40



41

/∗ Applet1 : applet to plot f ( x ) ∗/ import j a v a . a p p l e t . A p p l e t ; / / Add A p p l e t c l a s s e s import j a v a . awt . ∗ ; / / A b s t r a c t Windows T o o l k i t public c l a s s Applet1 extends Applet / / Class d e f i n i t i o n { d o u b l e mx , my , bx , by ; / / Class v a r i a b l e s f o r a l l methods / / p a i n t method . E x e c u t i o n b e g i n s h e r e ( no main ) g : G r a p h i c s c o n t e x t ( g r a p h o b j e c t ) p a s s e d by b r o w s e r public void p a i n t ( Graphics g ) { double x , y ; / / World c o o r d i n a t e s i n t gOldX , gOldY , gNewX , gNewY ; / / Window c o o r d i n a t e s g . d r a w S t r i n g ( "In Applet1.class, f(x) vs x" , 4 0 , 4 0 ) ; // Title System . o u t . p r i n t l n ( "In Applet1.class, about to draw graph" ) ; world2win ( 0 . 0 , 1 . 0 , 1 . 0 , 0 . 0 ) ; / / D e t e r m i n e mapping params x = 0.; / / First point y = f (x) ; / / f ( x ) = function to plot gOldX = ( i n t ) ( mx∗x+bx ) ; / / C o n v e r t t o window c o o r d s gOldY = ( i n t ) ( my∗y+by ) ; f o r ( x = 0 . 0 1 ; x window ( i n t ) // x win = m x ∗ x + b x , y win = m y ∗ y + b y d o u b l e maxx =350 , maxy =250 , rm , lm , tm , bm ; lm = 0 . 1 ∗ maxX ; / / Margins , a l l a r o u n d rm = 0 . 9 ∗ maxX ; bm = 0 . 9 ∗ maxY ; tm = 0 . 1 ∗ maxY ; mx = ( lm−rm ) / ( x l −x r ) ; / / Mapping params w r t m a r g i n s bx = ( x l ∗rm−x r ∗lm ) / ( x l −x r ) ; my = ( tm−bm ) / ( y t −yb ) ; by = ( yb ∗tm−y t ∗bm ) / ( yb−y t ) ; } }

2. Point your browser to the Web page CallApplet.html. When opened, this file prints out some lines on the browser’s screen and runs the program in the file Applet1.class. It knows to run the code via the statement on line 8:



398

CHAPTER 21

8

Call applet

This command tells the browser to load and run Applet1.class and to place the output of the program (a pretty graph) within a window in your browser of size width = 350 pixels × maxY = 250 pixels. A pixel stands for “picture element,” namely, a single dot on your screen. A typical screen may have an area of 1024 × 768 pixels, so a 350 × 250 graph should appear as a box of about 1/10 the screen size. 3. Normally your browser will run the applet and print out line 6 someplace on your browser’s screen (but not if you call the HTML file with the soon-to-bediscussed appletviewer command). 4. If you modify an applet that you are developing and want your browser to reload it, you need to depress the shift + reload keys simultaneously. Conversely, if you want to stop the applet from running, then pushing the back arrow on the browser seems safest. (Closing the browser window may still leave the applet running.) 5. appletviewer: The SDK developer’s kit provides a shell command that activates an applet without a browser: > appletviewer CallApplet1.html

Execute applet from within shell

The applet does not look quite the same within appletviewer as it does with a browser, but this is a convenient and fast way to develop applets. 6. Examine the graph drawn by your applet. Not bad for something we have done from scratch? One of the amazing things about Java is that this class file produces an identical graph on every Java-enabled computer system. a. Use the View/Page Source button on your browser to see the source HTML file that the browser is rendering on the screen for you. Look at line 8 calling the Java program Applet1.class. b. Instruct your browser to save this Web page in the appropriate directory for this chapter (File/Save As . . . button on your browser). The file should have the name CallApplet.html. It should be similar to the file in Listing 21.1. 7. Point your browser to Applet1.java and open it (the file is on the CD). Study This is the Java source code, given in Listing 21.2, for an applet to draw a graph. Your browser will be able to read this file as text, list it on your screen, but not compile or execute it. Examine how the variable g represents a Graphics object that ultimately becomes the graph on your screen. The paint method draws the graph, and the world2win method determines the parameters needed to scale the graph so that it fits in the window on your computer screen. Applet1.java.

399

WEB COMPUTING, APPLETS, PRIMITIVE GRAPHICS

You will modify the paint method as part of your exercise. 8. Tell your browser to save Applet1.java in the appropriate directory for this week’s work. (Doing that with the File>Save As commands is safer than cut and paste.) 9. Applet1.class: This is a compiled byte code to be called by your browser as it reads CallApplet.html. Feel free to look at it, but it will not tell you much, as it is not meant to be read by humans. 10.

Create your own personal copy of Applet1.class by compiling

Applet1.java.

11. Open your copy of CallApplet.html with your browser (File > Open Page > If you did all the copying and compiling correctly, this should plot the graph again.

Browse).

12. Try out the alternate method for executing applets, issuing appletviewer from a shell: > appletviewer CallApplet.html

View applet from a shell

13. Try using the command java Applet1 to execute Applet1.class.

21.2.1 Understanding Applet1.java Look inside Applet1.java to see what makes it go. On lines 2 and 3 we find: 2 import java.applet.Applet; 3 import java.awt.*;

// Add Applet classes // Add Abstract Windows Toolkit

These import commands tells the Java compiler to include packages of class files that extend Java beyond the basic classes. The java.applet package contains the class Applet that is needed to create applets. Consequently, when on line 4 we have the statement: 4 public class Applet1 extends Applet

// Class definition

the word “extends” means that Applet1 inherits all the general properties of an Applet, as defined in java.applet. These include an applet’s ability to be displayed in a window, its ability to respond to events like the click of a mouse, and its being killed when no longer needed. Line 4 is how we declare our Applet1 to be an Applet. The compiler understands what an Applet is after the importation of line 2. What is new in this program, aside from it being an applet, is that it draws

400

CHAPTER 21

graphical objects on your computer screen. Regardless of our graphics not being as fancy as what is possible with Java, the mere fact that we are able to write graphic commands that will run on most every computer is a major advance towards system-independent computing. The Abstract Windows Toolkit java.awt contains the classes for creating graphical objects such as lines, strings, rectangles, and ovals.2 Other than lines 2–4, the rest of the program has the same structure as the applications you have been writing up until now. However, they start their lives differently. As a consequence of applets beginning their execution from a Web browser, which is inherently a graphical interface, the applet must be “painted” onto a browser’s Web page. Thus, an applet’s execution begins in a method named paint, in contrast to the main method where execution begins for an application. If we had written an application, line 8 might declare our main method. Instead, execution of the applet begins with the call to the paint method: // Execution begins here

8 public void paint(Graphics g)

You are free to put whatever you want in the paint method, but you must call it paint and it must have a graphical object as its argument. It is through this graphics context, g in our case, that the browser and applet communicate.

21.2.1.1 Transformation of Coordinates We want to plot x + 0.2x sin(33 x), for 0.0 ≤ x ≤ 1.0. (21.1) 2 Because f (0) = 0 and f (1)  0.7, we need to plot ordinates in the range 0 ≤ y ≤ 1. These ranges for x and y are called actual or world values or coordinates. The plotting in our applet is done with basic Java graphics that deal with the display screen’s pixels. A typical screen, indicated on the left of Figure 21.3, may be 1,024 pixels wide and 768 pixels high. To draw our graph we need to convert the x and y values from (21.1) into pixel counts or window coordinates that fit on the screen. We assume a linear mapping between the two, f (x) =

xwin = mx x + bx ,

ywin = my y + by ,

(21.2)

where the m’s and b’s are constants that we need to determine. The method world2sc solves for these constants and in the process as converts the values of x and y in doubles into ints needed for pixel values. To be safe, we leave a 10%

margin around the graph. 2 A useful reference on this subject is the free textbook by D. Eck [Eck 02]. Other books often examine older versions of Java, even if they call them Java2, Java3, or Java4. Eck uses JFC (Java Foundation Class) and swing.

WEB COMPUTING, APPLETS, PRIMITIVE GRAPHICS

401

Monitor screen

Window (0,0)

x

y (400,350)

Figure 21.3 Left: The coordinate system used to define the window (pixel) coordinates. The upper left corner corresponds to the point (0,0), the width of the window is 400 pixels, and the height is 350 pixels. The ywin coordinate increases downwards. Right: An image of the computer screen showing Applet2.

Rather than try to draw a smooth curve through 100 points (possible using splines [CP 05]), we draw 100 little straight lines connecting old points, (gOldX, gOldY) to new points (gNewX, gNewY) and hope that your eyes are not good enough to notice the steps. The for loop that generates the 100 line segments starts on line 18, and the actual drawing is done on line 23. Seeing that pixel coordinates are all integers, on lines 16–17 and 20–21 we use the cast operator (int) to convert or “cast” a double into an integer (recall, we must do this explicitly since Java will not let us reduce precision unless we insist). 21.3 EXPLORATION: MODIFY APPLET1.JAVA 1.

Copy the files CallApplet.html and Applet1.java into new files and Applet2.java respectively.

Applet2.html

2. Modify both these files so they will work as the applet Applet2.java. You will need to make the names of the Java source and class file consistent. This also means that you will have to compile Applet2.java to produce Applet2.class, since a direct renaming of Applet1.class will not work. 3. Check that opening Applet2.html in your browser draws the graph again. 4. Modify the program Applet2.java so that it also draws an x axis and a y axis with the graph. It should look like the right drawing in Figure 21.3. a. Hint 1: Draw a long line using the g.drawLine command on line 31, only now draw it from the origin to the edge of the box. b. Hint 2: It is easy to draw an axis outside of the piece of real estate you have reserved on the screen, and then not see the line at all (what a bummer). Accordingly, it is a good idea to make your program change

402

CHAPTER 21

progressively so that at first you draw the axes well within the reserved box and then see how close you can get the axes to the edges of the boxes before they disappear. 5. Modify the source code to plot the function f (x) = tan(5x). To keep your curve within the boundary, like Figure 21.3, you may have to divide f (x) by 100. 6. Make the straight-line nature of the drawing more evident by experimenting with the number of steps used in the program and observing the results. For example, try using 1/10 the number of steps, and 10 times the number of steps. Is there a limit to how smooth a graph you are able to create? 21.4 EXTENSION: PTPLOT AS APPLET* Listing 21.3 PlotFourierSeries.java defines a class PlotFourierSeries derived from PlotApplet that configures and constructs a PtPlot.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

23 24 25 26 27 28 29



30

/ / P l o t F o u r i e r S e r i e s . j a v a by Edward A . Lee package p t o l e m y . p l o t . demo ; import p t o l e m y . p l o t . P l o t ; import p t o l e m y . p l o t . P l o t A p p l e t ; public c l a s s P l o t F o u r i e r S e r i e s extends PlotApplet { p u b l i c S t r i n g g e t A p p l e t I n f o ( ) { r e t u r n "PlotFourierSeries\n" ; } public void i n i t ( ) / / I n i t i a l i z e the applet { super . i n i t ( ) ; Plot plot = ( Plot ) plot () ; p l o t . s e t T i t l e ( "Fourier Series for Square Wave" ) ; p l o t . setXRange ( 0 , 400) ; p l o t . s e t M a r k s S t y l e ( "none" ) ; p l o t . addLegend ( 0 , "ideal" ) ; p l o t . addLegend ( 1 , "1 sinusoid" ) ; f o r ( i n t j = 2 ; j 12}. 23.7 MATH FUNCTIONS The basic mathematical functions are obtained by entering a backlash \ before the name. To cite an instance, x3 sin y 2 is obtained from xˆ3 \sin yˆ2. Look at how you need to leave a space after the function name as a separator, unless another backslash follows. Available functions include: \arccos

\arcsin

\arctan

\arg

\cos

\cosh

\cot

\csc

\deg

\det

\dim

\exp

\gcd

\hom

\coth \inf

\ker

\lg

\lim

\liminf

\limsup

\ln

\log

\max

\min

\Pr

\sec

\sin

\sinh

\sup

\tan

\tanh

Other mathematical function names are obtained by switching to Roman math font, and then writing the function name; for example, cosec(A) as $\mathrm{cosec}(A)$.

23.8 FRACTIONS To make a fraction with num over denom, use the \frac{num}{denom} command: v=

dx dt

v = \frac{dx}{dt}

433

LATEX FOR MATHEMATICS

Even though this LATEX command works fine, we prefer the simpler command: v=

dx dt

\over

TEX

v = {dx \over dt}

23.9 ROOTS √ N is obtained with the \sqrt{N} command. The square root symbol LATEX automatically adjusts the height and lengths of root symbols to fit into the equation and to span the argument: √

x=

√ a± b2 −4ac z

\sqrt{x} = {a \pm \sqrt{bˆ2-4ac} \over z}

Higher roots are obtained by including an optional order parameter in square brackets to \sqrt[n]{}: √ 3

x=

√ a+ 5 b2 −4ac z

\sqrt[3]{x} = {a + \sqrt[5]{bˆ2-4ac} \over z}

23.10 BRACKETS (DELIMITERS) Except for braces, which are used for commands, most normal-sized brackets or delimiters are entered into mathematics as typed: ((

))

[[

]]

{ \{

As an instance: {a} = (b) ∗ [c] + |d| × &e&

} \}

\{a\} =

||

&

\|

(b)*[c] + |d| \times \|e\|

Just as it does for roots, LATEX scales the size of these delimiters to match the size of the rest of the equation. However, since an equation may have a number of nested brackets, LATEX needs some help in deciding just which brackets you want to match. Thus, you give it help by tacking on the commands \left and \right to the delimiters that you wish to match up: ( (  ( 1 ( ( √ ( = {a + ℵ} ( z( \left[ \left|{1\over\sqrt{z}} \right|= \left\{ a + \aleph \right\} \right].

In order for LATEX to do all the work for you in sizing and placing these limiters, it demands that all the \rights be balanced by an equal number of \lefts. LATEX does not demand that the types match up, just the number of rights and lefts. So, for example, \left( may be paired off with \right]. Although this all makes

434

CHAPTER 23

sense, it introduces a problem if you try to create something like  b 2 d f a

dx

dx = 2

(

df ((b , dx (a

where there is no delimiter to match the left |. For this purpose, LATEX contains the null delimiters \left. and \right. that count as delimiters but do not show up when typeset. As an instance, the above formula was obtained with: $\int_aˆb {dfˆ2\over dxˆ2} dx = \left. {df\over dx} \right|_aˆb$

23.11 MULTILINE EQUATIONS Having LATEX arrange multiline equations is essentially the same as working with the tabular environment for text. You place each equation on a separate line (row), and then align the equal signs in each equation by placing them in the same column. To cite an instance, Sample.tex on the CD creates −¯h2 d2 ψ1 (x) + V (x)ψ1 (x) + U (x)ψ2 (x) = Eψ1 (x) 2m1 dx2 −¯h2 d2 ψ2 (x) + V (x)ψ2 (x) + U (x)ψ1 (x) = Eψ2 (x) 2m2 dx2

(23.2) (23.3)

\begin{eqnarray} {-\hbarˆ2 \over 2m_1} {dˆ2 \psi_1 (x) \over dxˆ2} + V(x)\psi_1(x) + U(x)\psi_2(x) &=& E \psi_1(x)\\ {-\hbarˆ2 \over 2m_2} {dˆ2 \psi_2 (x) \over dxˆ2} + V(x)\psi_2(x) + U(x)\psi_1(x) &=& E \psi_2(x) \end{eqnarray}

Observe here that we display these equations with the equation-array environment eqnarray rather than the equation environment that we used for single-line equations. We begin with \begin{eqnarray} and end with \end{eqnarry}. These commands produce numbered equations. If unnumbered, multiline equations are desired, they are produced with the \begin{eqnarray*} and \end{eqnarry*} forms. The equations are entered into an eqnarray environment in the same way as they would be with {equation}, except that the equal signs are enclosed by ampersands &=&, and each line of the equation ends with a double backslash \\. As also occurs in the tabular environment, this produces a column with all the equal signs aligned, and permits there to be as many lines with equations as you enter. Because eqnarray is just a tabular environment, it is possible to align symbols other than the equal sign, or to use this environment to split a very long equation onto two lines, with the continuation being indented: −

d2 ψ1 (x) d2 ψ2 (x) − + V (x)ψ1 (x) + V (x)ψ2 (x) dx2 dx2

435

LATEX FOR MATHEMATICS

+ U (x)ψ2 (x) + U (x)ψ1 (x) = Eψ1 (x) + Eψ2 (x) \begin{eqnarray*} - {dˆ2 \psi_1 (x)\over dxˆ2} - {dˆ2 \psi_2 (x)\over dxˆ2} &+& V(x)\psi_1(x) + V(x)\psi_2(x) \\ &+& U(x)\psi_2(x) + U(x)\psi_1(x) \\ = && E \psi_1(x) +E \psi_2(x) \end{eqnarray*}

23.12 MATRICES AND MATH ARRAYS The array environment is used to produce matrices and mathematical arrays: ⎛







α−µ β Γ x x2 y √ ⎝ y xy z ⎠ ⇒ ⎣ δ β3 α + γ ⎦ −x y λ z a z −1 i \left( \begin{array}{ccc} x & xˆ2 & y \\ \sqrt{y} & xˆy & z \\ zˆa & zˆ{-1} & i \end{array} \right) \Rightarrow \left[ \begin{array}{ccc} \alpha - \mu & \beta & \Gamma \\ \delta & \betaˆ3 & \alpha + \gamma \\ -x & y & \lambda \end{array} \right]

This is essentially identical to how tables are constructed for text. All that is new is the use of large right and left delimiters to produce a matrix symbol that fits the array. The alignment characters {ccc} are the same as with tabular. Another approach to matrices, which we personally find easier and more compact than the LATEX standard, is the TEX command \pmatrix: ⎛ mb2 12 ⎜ ⎝ 0

0

0 2

ma 12

0



0 0 2

2

+b m a 12

⎛ 1 ⎟ ⎝ ⎠= 0

0

0 4 0



0 0⎠ 5

\pmatrix{{mbˆ2\over 12} & 0 & 0 \cr 0 & {maˆ2\over 12} & 0 \cr 0 & 0 & m{aˆ2+bˆ2\over 12}} = \pmatrix{1 & 0 & 0 \cr 0 & 4 & 0 \cr 0 & 0 & 5}

We see that pmatrix takes care of its own delimiters and uses the carriage return command \cr to end the rows.

436

CHAPTER 23

The array environment is also used to produce an equation containing cases: 

θ(x) =

1, if x ≥ 0, 0, if x < 0.

\theta(x) = \left\{ \begin{array}{ll} 1 & \mbox{if $x \geq 0$};\\ 0 & \mbox{if $x < 0$}.\end{array} \right.

Here too, our preference is the TEX command \cases, which we find simpler: 

θ(x) =

1, 0,

if x ≥ 0, if x < 0.

\theta(x) = \cases{1, & if $x \geq 0$, \cr 0, & if $x < 0$. \cr}

23.13 INCLUDING GRAPHICS Most drawing and graphing programs have a number of options to determine the file format in which to save graphics. Because LATEX is used to produce publication quality typesetting, if you include graphics, then they too should be publication quality. PostScript (.ps) and Encapsulated PostScript (.eps) figures are of publication quality, so it should not be a surprise that there are nice packages to include these formats in LATEX documents. We will discuss how to include .eps figures, with the same commands used for .ps figures. Given a choice, use encapsulated figures, since it may be easier to adjust their size to fit the document. If your figure is of a different type, you should be able to open it in a drawing program and save it as an .eps figure. In addition, we recommend that you embed the fonts used in your figure with the figure; this does make for a larger file, but also avoids the possibility of some inappropriate local font being substituted when the document is printed (especially likely if you scale the figure). We recommend the graphics package for including figures, although we have also had success with the psfig package, especially for placing figures at desired points on the page. If a package is not part of your LATEX installation, it may be added. Before you use a package, you must include it in your document’s preamble (the part before the \begin{document}command): \usepackage[dvips]{graphicx, color}

On the CD is the sample graphics file Latex_Compile.eps used to create Figure 22.1. It is redrawn on this page as Figure 23.1 with the commands

437

ex la t Pa

pe

r.t e

x

%

So C urc od e e

Pa

pe

co m pi

le

C

Pa

pe

r.p s,

Pa

pe

r.p

df

Pr in ips dv d f ip dv

r.t om ex C pi (D od le ev e d ic e In de Pa pe pe nd r.d en vi t)

xd y a vi p

t

Vi ew

LATEX FOR MATHEMATICS

Figure 23.1 A scaled-down and rotated Figure 9.1.

\begin{figure} \begin{center} \includegraphics[angle=45, scale=0.35]{Latex_Compile.eps} \caption{A scaled-down and rotated Figure˜\ref{javacompile.eps}. \label{sample.fig}} \end{center} \end{figure}

The figure environment is standard LATEX. It is also used to draw pictures with LATEX, or to leave room for and label figures to be pasted in later (use \vspace to make the room). The \includegraphics command is placed within the figure environment. To ensure that the figure is centered on the page, we place the figure in a center environment. The angle=45, scale=0.25 specification in square brackets is optional and leads to a figure that is slanted and scaled to 25% of its original size.1 The name of the figure Latex_Compile.eps is given in curly braces, with a path indication if it is not in current directory. Scrutinize how after the name of the figure we issue the \caption command containing the caption as its argument. Within the caption’s argument (still within the curly braces) we have placed the label command \label{sample.fig}. We then refer to this figure as \ref{sample.fig}, as we do in the sample \includegraphics command above, and let LATEX take care of its actual number. parts:

As an example of \psfig, examine Figure 15.1 (p. 310) with its multiple

\begin{figure} \psfig{file=FIGS/planet.eps, height=1.85in} \hspace{6ex} \psfig{file=FIGS/PlanetApplet.eps, width=1.5in} \caption{\emph{Left: } The gravitational ....)} \label{planet.eps} \end{figure}

1 We are able to scale and rotate with no loss in quality because PostScript figures are not bit maps, but instead contain a text description of the figure and the fonts.

438

CHAPTER 23

23.14 EXERCISE: PUTTING IT ALL TOGETHER 1. Enter Sample.tex as given in §22.3. Use a text editor and save the file as Sample.tex. We recommend WinEdt and MikTex for Windows, and gnu xemacs for Unix/Linux. 2. Compile the file by pressing the LATEX button or by issuing a command from a shell: > latex Sample.tex

Compile latex source Sample.tex

If there are no errors in your file, you should get a fairly verbose response with statements of the sort This is TeX, Version 3.14159 (MiKTeX 2 UP 1) 2 NOV 2003 10:44 1478 **Sample.tex (Sample.tex LaTeX2e Babel ... C:\Program Files\MiKTeX\tex\latex\base\article.cls Document Class: article No file Sample.aux. LaTeX Warning: Reference ‘eq.1’ on page 1 undefined on input line 37. LaTeX Warning: Reference ‘eq.2’ on page 1 undefined on input line 37. [1] (sample.aux) Output written on sample.dvi (1 page, 2676 bytes).

3. The warnings that there is No file Sample.aux and that Reference ‘eq.1’ (‘eq.2’) undefined are to be expected on first compilation. The Sample.aux file contains the information LATEX needs to cross-reference the source Sample.tex. Yet LATEX does not write it until the end of compilation, and, consequently, it does not yet know that you have defined references eq.1 and eq.2. 4. Compile the source file again. This time the compiler should find a Sample.aux file to use for cross-references and not warn you about undefined references. 5.

The last part of LATEX’s message tells you that the files sample.aux and sample.dvi were written, and that the typeset document is one page long. If you have changed some labels or references, then LATEX may request that you compile the source file again in order to incorporate the updated .aux file. 6. If there were no errors, your working directory should now contain the files Sample.tex sample.aux sample.log sample.dvi

source file auxiliary working file with reference numbers and such log of LATEX messages; similar to messages flashed on screen device-independent, viewable version of typeset document

7. If you are lucky enough to get a sample.dvi file, then look at it to admire your work. On Unix you do this with: > xdvi sample.dvi

Preview .dvi file

439

LATEX FOR MATHEMATICS

On Windows use Yap (yet another previewer) or the DVI button. 8. If you get complaints that LATEX was not happy with the way a line fits on the page, just ignore them unless you are really going to publish the paper. If there are real errors, correct them one at a time and proceed. If you cannot figure out where the error is, try forcing LATEX to skip it by continually entering Enter. Alternatively, you isolate the location of an error by moving the %\end{document} up in your document until LATEX compiles with no errors. Then move it down gradually, recompiling at each stage until the error is uncovered. 9. To see how LATEX responds to errors, make these mistakes: Correct

Change to incorrect

\item

\iten

$N=6$

$N=6

The first error should illicit the response ! Undefined control sequence. l.19 \iten Use this sequence to simulate a random walk and ! LaTeX Error: Something’s wrong--perhaps a missing \item.

This error message indicates with a carriage return where LATEX notices something is wrong, in this case, that it has never heard of \iten. Below that, LATEX indicates that it thinks you meant to write \item. In the second case where a $ is left off, LATEX responds with ! LaTeX Error: Bad math environment delimiter. l.17 \[ 3, 7, 3, 1, 8, 2 \]

We see that LATEX knows something is wrong in the math environment (we left off the second $, but it does not know it until the next math environment begins with a \[ on line 17. Consequently, you need to look at line 17 in the source file, and work back from there to see where there is a missing math delimiter. 10. Once you have produced a complete .dvi file, you convert it into the PostScript file sample.ps for printing with the dvips command: > dvips -o sample.ps sample.dvi > dvips -o sample.ps sample

Convert .dvi file to .ps file Also converts .dvi file to .ps file

Here the -o option places the output into the file whose name follows. If you leave off the -o option, then the .ps file might well be sent directly to some printer. In WinEdt, the dvips command is issued by pushing the dvi %→ ps button. You should get an output statement showing you all the page numbers that have been

440

CHAPTER 23

converted, and indicating that the output is placed in the file sample.ps. There is also the pslatex version of LATEX that uses PostScript fonts in place of LATEX’s standard Computer Modern fonts. This does produce a smaller .ps file in the end. 11. If you want a .pdf file, you may issue the pdfLATEX command in the first place, or use the dvipdf command to convert your .div file. However, if the .eps figures in your file do not get placed in the .pdf file properly, we recommend the use of Adobe Distiller to convert the entire .ps file to a .pdf one. 12. If your document does not contain any embedded PostScript figures, then you may be able to print it successfully from the .dvi previewer. Otherwise, we have found it best to create a .ps file of your document and to print that. 13. Now print your sample.ps file. You do that from a GhostScript viewer, such as GSview, or with Unix printer commands: > lpr -Pps497 sample.ps > lp -d ps497 sample.ps

Print to printer ps497 Print to device (printer) ps497

14. Now let us go back and add some more features to Sample.tex: a. Include the sample PostScript figure disk or Web into your document.

Latex_Compile.eps

from the

b. Include Table 22.1 into your document (the commands are all given in the text). First keep the table in one place by using just the tabular environment, and then let it float by placing the tabular environment within a table environment. c. Include a line in your document that uses the \ref command to refer to the floating table by its label. d. Create a title page by placing your personalized versions of these commands into the preamble: \title{My Title} \author{My Name \and My Friend\\ Our Institution} \date{Someday} \thanks{To those who have supported this noble effort with cash.}

After placing these commands in the preamble, you need to create the title in the body of the document with: \maketitle

Appendix A Glossary

absolute value Value of a quantity expressed as a positive number, e.g., |f (x)|. accuracy The degree of exactness provided by a description or a theory. Accuracy usually refers to an absolute quality, while precision usually refers to the number of digits used to represent a number. address The numerical designation of a location in memory. An identifier, such as a label, that points to an address in memory or a data source. algorithm A set of rules for solving a problem in a finite number of steps. Usually independent of the software or hardware. allocate To assign a resource for use, often memory. alphanumeric The combination of alphabetic letters, numerical digits, and special characters, such as %, $, and /. analog The mapping of a continuous physical observable to numbers. As an instance, a car’s speed to its speedometer. animation A process in which motion is simulated by presenting a series of slightly different pictures (frames) in succession. append To add on, especially to the end of an object or word. application A self-contained executable program containing tasks to be performed by a computer, usually for a practical purpose. architecture The overall design of a computer in terms of its major components: memory, processor, I/O, and communication. archive To copy programs and data to an auxiliary medium or file system for long-term and compact storage. argument A parameter passed from one program part to another, or to a command. arithmetic unit Part of the central processing unit that performs arithmetic.

442

APPENDIX A

array (matrix) A group of numbers stored together in rows and columns that may be referenced by one or more subscripts. Each number in an array is an array element. assignment statement Command that sets a value to a variable or symbol. B Abbreviation for byte (8 bits). b Abbreviation for bit or baud (1 bit/sec). background (1) A technique of having a programming run at low priority (“in background”) while a higher-priority program runs “in foreground.” (2) The part of video display not containing windows. base The radix of a number system. (10 is the radix of the decimal system.) basic machine language Instructions telling the hardware to do basic operations such as store or add binary numbers. batch The running of programs without user interaction; often in background. baud 1 bit per second. binary Related to the number system with base 2. BIOS Basic Input/Output System. bit Contraction of “binary digit”; the digits 0 or 1 used in binary representation. Boolean algebra A branch of symbolic logic dealing with logical relations as opposed to numerical values. boot To “bootstrap”; to start a computer by loading the operating system. branch To pick a path within a program based on the value of variables. bug A mistake in a computer program or operating system; a malfunction. bus A communication channel (bunch of wires) used for transmitting information quickly among computer parts. byte Eight bits of storage. Java uses two bytes to store a single character in extended unicode. byte code Compiled code that is read by all computer systems, but still needs to be interpreted (or recompiled) on each system. Contained in class file. cache Small, very fast part of memory used as temporary storage between the very fast CPU registers and main memory. calling sequence The data and setup needed to call a method or a subprogram.

GLOSSARY

443

central processing unit (CPU) The part of a computer that accepts and acts on instructions; where calculations are done and communications controlled. checkpoint A statement within a program that stops normal execution and provides some output to assist in debugging. checksum The summation of digits or bits used to check the integrity of data. child Object created by presently existing parent object. class A group of objects or methods having a common characteristic. Collection of data types and associated methods. An instance of an object. Byte code version of a Java program. clock Electronics that generate the periodic signal to begin execution. code A program or the writing of a program. column The vertical line of numbers in an array. column-major order The method used by Fortran to store matrices in which the leftmost subscript varies most rapidly and attains its maximum value before the subscript to the right is incremented. (Java uses row-major order.) command A computer instruction. A control signal. command key A keyboard key, or combination of keys, that performs a predefined function. compilation Translation of a program written in a high-level language into (more) basic machine language. compiler A program that translates source code from a high-level computer language to machine language or object code. concatenate To join together two or more strings, head to tail. concurrent processing Same as parallel processing; simultaneous execution of several related instructions. conditional statement Statement to be executed only under certain conditions. control character A character that modifies or controls the running of a program (e.g., control +c). control statement A statement within a program that transfers control to another section of the program. copy To transfer data without removing the original. CPU See central processing unit.

444

APPENDIX A

crash The abnormal termination of a program or a piece of hardware due to some malfunction. cycle time (clock speed) Time it takes CPU to execute simplest instruction. data Information stored in numerical form; plural of datum. data type Definitions that permits proper interpretation of character string. debug To detect, locate, and remove mistakes in a program or hardware. default The assumption made when no specific directive is given. delete To remove and leave no record. digital Representation of quantities in discrete form; contrast analog. dimension of array Number of subscripts needed to access single array element. directory A collection of files given their own name. disc, disk A circular magnetic medium used for storage. discrete Related to distinct elements. double precision Use of two memory words to store a number. download To transfer data from a remote computer to a local computer. driver A set of instructions needed to transmit data to/from external device. dump Data resulting from listing all information in memory. dynamic RAM Computer memory that must be refreshed at frequent intervals. E A symbol for exponent. To illustrate, 1.97E2 = 1.97 × 102 . element An item of data within an array; a component of a language. enable To make a computer part operative. ethernet A high-speed local area network (LAN) composed of specific cable technology and communication protocols. executable program A set of instructions that can be loaded into the computer’s memory and executed. executable statement A statement that causes some computational action, such as assigning a value to a variable. fetch To locate and retrieve information from storage. floating point Representation of numbers as mantissa times base raised to a power. Scientific notation.

GLOSSARY

445

FLOP Floating Point Operation. foreground Running high-priority programs before low. Fortran An acronym for formula translation. fragmentation File storage in many small, dispersed pieces. G Abbreviation for giga; one billion in USA; 109 . garbage Meaningless numbers, usually the result of error or improper definition. Obsolete data in memory waiting to be removed (“collected”). giga Prefix indicating one billion, 109 , of something (USA). GUI Graphical user interface; a window environment. hard disk A circular, spinning, storage device using magnetic memory. hardware Physical components of a computer system. hashing A transformation that converts keystrokes to data values. heuristic A trial-and-error approach to problem solving. hexadecimal Base 16; {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}. hidden line surface Part of a graphics object normally hidden from view. high-level language Programming language similar to normal language. host computer A central computer providing services to terminals. icon Small on-screen symbol that activates an application. increment The amount added to a variable, especially an array index. index The symbol used to locate a variable in an array, the subscript. A reference table kept in memory. infinite loop The endless repeating of a set of instructions. input Introduction of data from an external device into main storage. instructions Orders to the hardware to do basic things such as fetch and add. instruction stack Group of instructions currently in use. interpolation Finding values between known values. interpreter A language translator that converts each line of source code into machine code and immediately executes each line. interrupt A command that stops execution of a program when some abnormal condition is encountered.

446

APPENDIX A

iterate To repeat a series of steps automatically. jump A departure from the linear processing of code; branch, transfer. just-in-time compiler A procedure that takes a Java class file that would ordinarily be interpreted and recompiles it into a more efficient machine code. K Abbreviation for KILO, one thousand, 103 . kernel The central part of a large program or operating system that does not get significantly modified when run on different computers. kill To delete or stop a process. language Rules, representations, and conventions used to convey information. library (lib) A collection of programs or methods usually on a related topic. linking The connecting of separate pieces of code to form an executable program. literal A symbol that defines itself, such as the letter A. load To read information into the computer’s memory. load module A program that is loaded into memory and run immediately. log in (on) To sign onto the computer, to begin a session. loop A set of instructions executed repeatedly as long as some condition is met. low-level language Machine-related programming not easy for humans to read. machine language The set of instructions understood by the computer hardware. machine precision The maximum positive number that, when added to the number stored as 1, does not change it. macro A single, higher-level statement resulting in several lower-level ones. main method Part of application program where execution begins; may call other methods but cannot be called by them. main storage The fast, electronic memory; physical memory. mantissa The significant digits in a floating-point number; e.g., 1.2 in 1.2E3. mega, M A prefix denoting a million, or 1, 048, 576 = 220 . metalanguage A language used to define other languages. method A subroutine used to calculate a function or manipulate data. modular programming The technique of writing program with many, reusable, and independent parts.

GLOSSARY

447

modulo (mod) Function that yields only remainder after division of numbers. multiprocessors Computers with more than one processor. multitasking The system by which several jobs reside in a computer’s memory simultaneously; may run in parallel or sequentially. nesting Embedding a group of statements within another group. object A software component with properties like physical objects. A combination of data (variables, properties) and methods (behaviors) to interact with the data; and abstract data type containing multiple parts. object-oriented programming A modular programming style focused on classes of data objects and associated methods to interact with the objects. object program (code) A program in basic machine language produced by compiling a high-level language. octal Base 8; easy to convert to or from binary. operating system (OS) The program that controls the computer and runs applications, processes I/O, and shells. optimization The modification of a program to make it run more quickly. overflow A number that is larger than the largest number a computer can store accurately. package A collection of related programs or classes. page A segment of disk memory that gets read into central memory in one block. parallel (concurrent) processing Simultaneous or independent processing in different CPUs. parallelization Rewriting an existing program to run on a parallel computer. partition The section of memory assigned to a program during its execution. physical memory The fast, electronic memory of a computer; main memory; contrast to virtual memory. physical record The physical unit of data for input or output that may contain a number of logical records. pipeline (segmented) arithmetic units Assembly-line approach to central processing in which CPU simultaneously gathers, stores, and processed data. pixel A picture element, a dot on the screen. See also voxel.

448

APPENDIX A

portable document format, pdf A document format developed by Adobe that is of high quality and still readable within a Web browser. PostScript, ps A standard language developed by Adobe for sending text and graphics to printers. precision The degree of exactness with which a quantity is presented. Highprecision numbers are not necessarily accurate. program A set of instructions that a computer interprets and executes. protocol A set of rules or conventions. pseudocode A mixture of normal language and coding that provides a symbolic guide to a program. queue An ordered group of items waiting to be acted upon in turn. radix The base number in a number system that gets raised to powers. RAM Random access (central) memory that is reached directly. random access Reading or writing memory independent of storage order. record A collection of data items treated as a unit. recurrence (recursion) The use of a loop to produce new values of a variable computed in previous iterations. registers Very high-speed memory used by the central processing unit. reserved words Words that cannot be used in an application program. RISC Reduced Instruction Set Computer; a CPU design that increases arithmetic speed by decreasing the number of instructions the CPU must follow. row-major order The method used by Java to store matrices in which the rightmost subscript varies most rapidly and attains its maximum value before the subscript to the left is incremented. run To execute a program. scalar A data value or number, for example, π . serial/scalar processing Calculations in which numbers are processed in sequence. Contrast to vector and parallel processing. shell The command line interpreter; the part of the operating system in which the user enters commands. simulation The modeling of a real system by a computer program. The use of one system to represent or model another one.

GLOSSARY

449

single precision The use of one computer word to store a variable. software Programs or instructions. source code Program in high-level language needing compilation to run. stochastic A process in which there is an element of chance. string A connected sequence of characters treated as a single object. structure The organization or arrangement of a program or a computer. subprogram The part of a program invoked by another program unit, a method. supercomputer The class of fastest and most computers available. syntax The rules governing the structure of a language. telnet Protocols for computer–computer communications. tera, T 1012 , or 230 = 1, 073, 741, 824. top-down programming Designing a program from the most general view of the problem, down to the specific subroutines. unary An operation that uses only one operand; monadic. underflow A result that is smaller than the smallest number that a computer stores properly. unit A device having a special function. upload To transfer data from a local to a remote computer; opposite of download. utility programs Programs to enhance other programs or do chores. vector A group of N numbers in memory arranged in one-dimensional order. vector processing Calculations in which an entire vector of numbers is processed with one operation. virtual memory Memory on the slow, hard disk and not in fast RAM. visualization The production of two- and three-dimensional pictures or graphs of the numerical results of computations. volume A physical unit of a storage medium, such as a disk. word A unit of main storage, usually 1, 2, 4, 6, or 8 bytes. word length The amount of memory used to store a computer word.

Appendix B Maple Quick Reference, Debugging Help

From Unix Prompt # Start X Windows Maple in background # Start text Maple in background

% xmaple % maple

Select Modes T, ‘‘text’’ input > Maple input Σ Calculation

# Useful for notes or reports # To enter mathematics # Insert and edit math into text

Execute Commands > restart; > 3 + 7; then Enter

# De-assign all variables # Enter and Return may be different keys

Basic Operations and Representations > (4/2)*3 - 7ˆ8 + 7**8; > 2485 / 3479; > 2485.

# Decimal point implies float; limited precision

/ 3479;

# Save floats with 40 digits

> Digits := 40; > evalf( 2485/3479, 20); > 1.0*10ˆ(-6); > Float( 1, -6 );

# Evaluate rational number as 20-digit float # Floating-point number in scientific notation # Alternative form of above # Alternative form of above, not e

> 1E-6;

# Variable E -6

> 1.*E-6; > log(1.*E-6);

# Test exponential # Syntax error without the* or with the.

> 1.E-6; > exp(1.);

# This should be 6 # Reduces to 5/7

exp(1);

# Two versions of e, base of natural logs

451

MAPLE QUICK REFERENCE, DEBUGGING HELP

# Test of log

> log( exp(1) );

# Maple even simplifies for you

> sqrt(10!);

# Name for 3.1415. . .

> Pi; > ln(x);

log10(x);

cos(Pi);

# Math functions

cos(x);

# Complex numbers

> (2+3*I)/(5-4*I);

# Polar form of complex number

> convert( %, polar );

# Polar form of complex number

> polar(2+3*I); map(evalc,polar(Z)); > op(1,Polar);

# Extract first, second operand from Polar

op(2,Polar);

# String within quotes

> String := "whatever I put here";

# Help on command

> ?command;

Sums and Products > sum( (-1)ˆi* xˆ(2*i)/(2*i)!, i = 0..2 ); > sum( (-1)ˆi * xˆ(2*i)/(2*i)!, i = 0..infinity );

# First 3 terms in cosine series # All terms

> Sum( (-1)ˆi * xˆ(2*i)/(2*i)!, i = 0..infinity ); > value(%); expand(%); factor(%); expand(%);

# Inert sum

> allvalues(%);

eval(%);

collect(%);

simplify(%); # Manipulate! # Manipulate!

normal(%);

# Form product of symbols

> Product( (iˆ2+3*i-11)/(i+3), i = 0..10 ); > sum( i,i = 1 ..

# Should be n(n + 1)/2

n );

> convert( sin(x), exp );

# Express as exponents (many variants)

> seq(2*n-1, n = 1..4); > list1 := [4,4,8,6]; list2 := [seq(2*n-1, n = 1..4)]; > NiceSet := 0,2,4,6;

# Create sequence # Create lists # Create set

Statements, Expressions, and Functions > 1+4; exp(x)-1; > y := 3* zˆ2 + 1; > eval( y, z = 2 ); > f :=(x) -> x * sin(x); > R :=(x,y,z) -> sqrt( x*x + y*y + z*z ); > g := unapply( 3*zˆ2 + 1, z ); g(2); > x := ’x’;

# Expressions (evaluated or stored) # Assignment statement, has side effects # Evaluate y for z = 2 # Define function f (x) equal to x sin x # Define function of 3 variables # Convert expression into function, evaluate # De-assign x

Solving Single Equations > solve( a*xˆ2 + b*x +c = 0, x ); > y := a* xˆ2 + b*x + c = 0; > solve( y, x ); > eval( y, x = a/2 ); > roots( xˆ2-1 );

Find x that solves # Symbol y represents equation # Solve equation y # Verify answer # Real roots and multiplicity of polynomials

452

APPENDIX B # Find complex roots

> roots( xˆ2 + 1, I ); > ans := solve( a* xˆ2 + b*x +c = 0, x); > ans[2];

# ans equals sequence # Second answer or element in sequence

> fsolve( x*sin(x) - 1, x ); > fsolve( x*sin(x) - 1, x = 0..6 );

# Floating point solve for x # Floating point solve for 0 < x < 6

Simultaneous Equations > eqn1 := a +3*b +4*c = 4; eqn2 := 5*a +6*b +7*c = 2; # 2 equations > solve( eqn1, eqn2, a, b ); # Solve for a, b in terms of c > x := Linsolve(A,b) mod 5;

# Solve matrix equation [A]x = b

Plotting Along > with(plots); with(plottools); > plot( tan(x), x = -7...3 );

# Load the plotting package # Basic plot

> plot(tan(x), x = -7..7, y = -5..5, labels=[‘x‘,‘tan(x)‘], title=‘tan‘); > plot({cos(x), sin(x), xˆ2}, x = -Pi..Pi );

# Multiple functions in 1 plot

> plot( max(0, cos(x)), x = -2*Pi..2*Pi ); # Eliminate y < 0 > plot([sin(x), cos(x), x = 0..2*Pi], scaling = constrained); # Parametric plot > implicitplot(xˆ2+yˆ2 = 1, y = exp(x),x=-2..2, y=-2..2); # Implicit 2 variable eqtn > plot3d( x*(xˆ2-3*yˆ2), x = -1..1, y = -1..1,title = ’saddle’ ); > polarplot( [ r, theta, theta = 0..2*Pi ] );

# 3-D plot # Polar plot

# Contour plot

> contourplot(V(x,y), x = -4..4, y = -4..4);

# Vector field plot

> fieldplot( [Ex(x,y), Ey(x,y)], x = -3.5..3.5, y = -3.5..3.5, color = Ex(x,y), arrows = THICK ); > fieldplot3d( [Ex(x,y,z), Ey(x,y,z), Ez(x,y,z)], >

x = -3.5..3.5, y = -3.5..3.5, z = -3.5..3.5, arrows = THICK);

> animate( cos(t*x) *sin(t*x), x = 0..Pi, t = 1..20 ); > animate3d(cos(t*x) * sin(t*y), x=0..Pi, y=0..Pi, t=1..2 ); > complexplot3d( [xˆ2 - yˆ2, 2*x*y], x =-2..2, y =-2..2); > complexplot3d( zˆ2, z = -2-2*I..2+2*I, axes = framed );

# 2-D animation # 3-D animate # Separate Re and Im # Complex function

Plotting Numerical Data > with(statplots); with(stats); > Xdata : = [1, 2, 3, 6, 10]; > Ydata := [1, 4, 9, 36, 100]; > scatterplot( Xdata, Ydata, color = RED); > pointplot(List, connect = true); > display( plot1, plot2 );

# Need statistics packages (not math!) # A list of all x values # A list of corresponding y values # Can have several plots # Plot points in a list or set # Display two plots in one figure

453

MAPLE QUICK REFERENCE, DEBUGGING HELP

Differentiation # df (x)/dx # Second derivative ($ not ˆ)

> diff( f(x), x ); > diff( f(x), x$2 );

# Partial derivative

> diff( (sin(x))ˆy, x ); > diff( (sin(x))ˆy, x, y );

# Second partial derivative wrt x and y # Inert derivative

> Diff( f(x), x );

# Function = derivative of f (x)

> D(f)(x);

# Differentiate multivariate function

> Diff( f(x,y,z), x, z ); > g := (x) -> D(f)(x); > h := (x, y, z) -> sin(x*y*z); > g := D [1] (h);

# Define function as derivative of function # Define multivariate function # Differentiate h wrt its first argument # Limit

> limit( (1+x/n)ˆn, n = infinity );

# New function = derivative of cos

> D (cos); > g := (x) -> D(f)(x);

# New function = derivative

Integration # Indefinite integral # Check answer by differentiation

> int ( x/sqrt(1+x), x ); > diff ( %, x );

# Definite integration

> int ( x/sqrt(1+x), x = 0..1 );

# Integrate to produce a function

> g := unapply ( int( f(x), x ),x );

# Function of y after integrate out x

> g := (y) -> int( f(x,y), x = a..b );

Differential Equations > with( DEtools ): # Load differential-equation tools # ODE of simple harmonic motion > diff eq := D( D(x) ) (t) = -w*w*x; # Alternate form of above > diff eq := ( D@@2 ) (x) (t) = -w*w *x; > DEplot([diff(theta(t),t) -omega = 0, diff(omega(t), t) + sin(theta) = 0], [t, theta, omega], 0..12, [0,0,1.0], scene = [t, omega] ); > diff eq1 := D ( D(y) ) (x) + 5*D(y)(x) + 6*y(x) = 0;

# PDE # Initial conditions

> init con := y(0)=0, D(y)(0)=1; > dsolve( {diff eq1, init con},{y(x)} );

# Solve equation

> sys := (D@@2)(y)(x) = z(x), (D@@2)(z)(x) = y(x); > dsolve( {sys}, {y(x), z(x)});

# Solve and plot

# System of ODEs

# Maple generates initial condition parameters

Linear Algebra > with(LinearAlgebra): with (linalg): > N := matrix(2, 2, [2, x, 1, yˆ3]); > A := matrix(3, 3, (i,j)-> i*j);

# Load, list packages # Assign 2 × 2 matrix with symbols # Matrix with function as elements

454 > ID := IdentityMatrix(3); > v := vector([1, 2, 3]);

APPENDIX B # Identity matrix # Enter column vector with list

> ucol := matrix(3, 1, [1, 2, 3]);

# Explicit column vector

> urow := matrix(1, 3, [1, 2, 3]);

# Explicit row vector, NB no commas!

> print (A); > evalm( A &* B ); > evalm( Aˆ2 ); > evalm( 1/B ); > det( A ); > inverse( A ); > P := multiply(A, B); > eigenvals( B ); > eigenvects( B ); > x := linsolve (A, b);

# View matrix # Matrix multiply of two square matrices # Square of matrix # inverse of nonsingular matrix # Determinant of matrix # Inverse, another way # Matrix multiplication, another way # Eigenvalues of matrix B # Eigenvectors of 3 × 3 matrix # Solve Ax = b for x

LinearAlgebra vs. linalg Commands N := Matrix(2,2, [[2,x],[1, yˆ3]]) N := matrix(2,2, [[2,x],[1, yˆ3]]) A := Matrix(3,3, (i,j)-> i*j) A := matrix(3,3, (i,j)-> i*j) B := Array(1..2, 1..2, [[2,2,3],[2,1,6]]) B := array(1..3, 1..3,[[2,2,3],[2,1,6]]) type(A, Matrix) type(A,matrix) Vec := Vector( [ 1/M[1, 2], M[1, 1], B[3, 3] ]) evalm(A &* B) evalm(A &* B) A.B ID := IdentityMatrix(4) Multiply(A,B) multiply(A, B) map(f, {a,b,c}) Map(f, {a,b,c}) ID := matrix( 3, 3, [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ] ) Determinant(A) det(A) Binv := 1/B Binv := evalm(1/B) B . Binv evalm (B &* Binv) Transpose(A) transpose(A) Ainv := MatrixInverse(A) inverse(A) vec := Vector([x, y*xˆ2,y]) vec := vector([x, y*xˆ2,y]) DotProduct(vec,vec2) dotprod(vec,vec2) CrossProduct(vec,vec2) crossprod(vec,vec2) Curl(vec,[x,x,x]) curl(vec, [x,x,x]) linalg[diverge](vec, [x,y,z]) diverge(vec, [x,x,x]) linalg[grad](xˆ3,[x,y,x]) grad(xˆ3,[x,y,x]) Eigenvectors(A) eigenvectors(A) Eigenvalues(A) eigenvalues(A) X := LinearSolve (A, X := linsolve (A,

455

MAPLE QUICK REFERENCE, DEBUGGING HELP

Generating Java, Matlab, Fortran, and C Code > with (codegen): > CodeGeneration (Java);

# Code generation; also for Fortran, C, Matlab

Statistics and Fitting > with ( stats ); > with ( fit );

# Load the package # Load the fitting package

> X := [2,4,6,8];

# Define independent variable

> Y := [0,20,28,44];

# Define dependent variable

> leastsquare [ [x,y], y = a*xˆ2 + b*x + c, a, b, c ] ( [X,Y] ); # Fit quadratic > with ( describe ); # Load the package for means > mean X := mean ( X );

# Mean

> median ( X ); > variance ( X ); > dev := standarddeviation ( X ); > with(statplots); > scatterplot( X, Y, color = black);

# Load the statistical plotting package # Make scatterplot

Maple Procedures Chebyshev := proc( n ) local p, k; p[0] := 1; p[1] := x; if n a := Chebyshev(5); a[3];

# Call procedure

456

APPENDIX B

Maple Debugging Built in debuggers: tracelast, mint, trace, printlevel 1. Check example in help file for proper use of options. 2. restart: try if something that was just working no longer does. 3. Maple confused: delete command or line and start again; there may be hidden control characters. 4. Check against backup copy (see Edit/Preferences for automatic backups). 5. Check if used = when wanted := (or vice versa). 6. "; unexpected": possibly unbalanced parentheses. 7. empty plot: numerical values for parameters may not be assigned. 8. . . . instead of [. . . ], or vice versa. 9. wrong number (or type) of parameter, invalid arguments a symbolic variable may be numeric, or vice versa. 10. No response from Maple: Maple gave up. Try numerical evaluation, if that works then the problem is not you. May need to assign variable type to assist Maple. 11. No action: forgotten ”;” to end line.

457

MAPLE QUICK REFERENCE, DEBUGGING HELP

Maple’s Standard Library Functions about addproperty AiryAi algsubs AngerJ applyrule arccsc arcsinh array assemble assuming bernoulli BesselK branches CheckArgs coeff comparray conjugate convert cosh csch CylinderD DefaultOverflow denom Diff Dirac DistDeg Eigenvals EllipticCPi EllipticNome erfc eval evalf evaln exists expandon Factor fdiscont fixdiv fopen freeze Fresnelg

abs addressof AiryAiZeros alias antihermitian arccos arccsch arctan ArrayDims assign asympt bernstein BesselY cat Chi coeffs compiletable constant indfcn coords cot csgn CylinderU DefaultUnderflow depends diff disassemble Divide eliminate EllipticE EllipticPi erfi evala evalfint evalr Expand(inert) exports factor feof float forall frem FresnelS

add AFactor AiryBi allvalues antisymm arccosh arcsec arctanh ArrayElems assigned attributes BesselI BesselYZeros ceil chrem coeftayl COMPILE OPTIONS

Content copy coth currentdir CylinderV define DESol diffop discont divide ellipsoid EllipticF elliptic int euler evalapply evalhf evalrC Expand ExternalCalling Factors fflush floor forget fremove FromInert

addcoords AFactors AiryBiZeros anames apply arccot arcsech argument ArrayIndFns asspar band BesselJ Beta changecoords Ci collect compoly content CopySign coulditbe curry dawson define external Det Digits discrim dsolve EllipticCE EllipticK entries eulermac evalb evalindets events expand extract factors FFT fnormal fprintf FresnelC frontend

additionally AIrreduc algebraic andmap applyop arccoth arcsin Array ArrayOptions assume Berlekamp BesselJZeros BivariatePolynomial

charfcn close combine CompSeq convergs cos csc CustomWrapper Default0 degree diagonal dinterp dismantle efficiency EllipticCK EllipticModulus erf Eval evalc evalm Excel expandoff extrema fclose filepos fold frac Fresnelf fscanf

458

APPENDIX B

Maple’s Standard Library Functions (cont) fsolve Gaussjord gcdex GF has heap Hessenberg IEEEdiffs igcdex Im indets ininame intat invfunc iratrecon is isolate isqrt JacobiCN JacobiNC JacobiSN JacobiZeta KelvinKei LambertW lcm LegendreQ Li ln LommelS2 maptype MatrixOptions member ModifiedMeijerG mods msolve Nextprime Normal numer numerics open patmatch pdsolve

galois gc genpoly Greek hasassumptions Heaviside hfarray ifactor ilcm implicitdiff index initialcondition interface invztrans irem iscont isolve issqr JacobiCS JacobiND JacobiTheta1 KelvinBei KelvinKer last name eval Lcm length Limit lnGAMMA lprint match max membertype modp Modular mtaylor nextprime normal NumericClass NumericStatus order pclose pi

GAMMA Gcd getenv HankelH1 hasfun Hermite history ifactors ilog10 ImportMatrix indexed initialize Interp iostatus iroot isdifferentiable ispoly ithprime JacobiDC JacobiNS JacobiTheta2 KelvinBer KummerM latex lcoeff LerchPhi limit log map MatlabMatrix maximize min modp1 module mul nops nprintf NumericEvent numeric type OrderedNE PDEplot options piecewise

GaussAGM gcd GetResultDataType

Gausselim Gcdex GetResultShape harmonic hastype hermitian identity igcd ilog[b] in inifcn int Inverse iquo irreduc IsMatrixShape isqrfree JacobiCD JacobiDS JacobiSD JacobiTheta4 KelvinHer

HankelH2 hasoption HermiteH icontent iFFT ilog2 ImportVector indices insert interp iperfpow Irreduc isdir isprime JacobiAM JacobiDN JacobiSC JacobiTheta3 KelvinHei KummerU latex filter leadterm lexorder Linsolve log10 map2 Matrix maxnorm minimize modp2 MOLS NextAfter norm Nullspace

lattice LegendreP lhs listdir LommelS1 Maple floats matrix maxorder mkdir modpol mserver Nextpoly Normal(inert) numboccur

NumericEventHandler

NumericException

odetest ormap pdesolve plot

op parse pdetest plot3d

459

MAPLE QUICK REFERENCE, DEBUGGING HELP

plotsetup polylog powmod Prevprime print procmake protect quo randomize ratinterp readbytes realroot release requires rhs RootOf rsolve rtable indfns savelib scanf select setattribute Shi signum singular solve spline Sqrfree ssystem sturm substring Svd table testfloat timelimit trigsubs unapply unprotect vector WeierstrassSigma with writeline zip

pochhammer polynom Prem prevprime printf Product Psi radfield Randpoly rationalize readdata Record Rem residue RiemannTheta Roots rtable rtable options scalar Searchtext selectfun SFloatExponent showprofile sigpipe sinh solvefor spreadsheet sqrfree storage sturmseq subtype symmdiff tan TEXT ToInert trunc unassign UseHardwareFloats verify WeierstrassZeta worksheet writestat ztrans

pointto polytools prem Primitive ProbSplit product psqrt radnormal randpoly Ratrecon readlib Reduce rem RESol rmdir roots rtable algebra rtable printf Scale10 searchtext selectremove SFloatMantissa showtime Simplify sinterp sort SPrem sqrt string subs Sum symmetric tanh thaw TopologicalSort

type undefined userinfo WeberE whattype WRAPPER writeto

poisson Power Preprocessor Primpart procbody proot queue radsimp Randprime ratrecon readline references remove Resultant root round rtable dims rtable scanf Scale2 sec seq shake Si simplify smartplot3d sparse sprem sscanf StruveH subsindets sum syntax taylor thiele traperror typematch unit value WeierstrassP WhittakerM writebytes zero

polar Powmod Prevpoly primpart ProcessOptions property Quo rand range Re readstat Regular Expressions repository resultant rootbound Rounding rtable elems SampleRTable scan sech series SharedLibrary sign sin Smith spec eval rules sprintf Ssi StruveL subsop surd system testeq time triangular unames Unordered Vector WeierstrassPPrime

WhittakerW writedata Zeta

460

APPENDIX B

Maple Packages algcurves CodeGeneration combstruct CurveFitting diffalg Domains GaussInt geom3d Groebner inttrans LibraryTools linalg LinearFunctionalSystems

ListTools Maplets Matlab networks numtheory OrthogonalSeries padic plots PolynomialTools process RationalNormalForms

Rif simplex SNAP SoftwareMetrics Spread StringTools student SumTools tensor Units VectorCalculus XMLTools

algebraic curves translate Maple combinatorial structures functions for curve fitting differential algebra create computation domains gaussian integers Euclidean 3-D geometry Groebner basis calculations integral transforms library manipulation utilities array-based linear algebra linear functional equations manipulating lists graphical user interfaces MATLAB Link graph networks number theory orthogonal poly series p-adic numbers graphics polynomial tools (Unix)-multi-processing rational normal forms nonlinear ODEs and PDEs linear optimization symbolic-numeric algorithms code complexity spreadsheets string manipulations student calculus closed form sums tensors, Gen Relativity unit conversions vector calculus XML tools

codegen combinat context DEtools difforms finance genfunc geometry group LargeExpressions liesymm LinearAlgebra LinearOperators LREtools MathML MatrixPolynomialAlgebra

numapprox Ore algebra orthopoly PDEtools plottools powseries RandomTools RealDomain ScientificConstants Slode Sockets SolveTools stats Student Student[Calculus1] sumtools TypeTools VariationalCalculus Worksheet

translate Maple combinatorial functions context sensitive menus differential equations tools differential forms financial mathematics rational generating functions Euclidean geometry permutation and finite groups computation sequences Lie symmetries rtable-based linear algebra linear functional equations linear recurrence relations import and export MathML polynomial matrices numerical approximation algebras of linear operators orthogonal polynomials PDE tools basic graphical objects formal power series random objects real number context scientific constants ODE series solution network communication solution tools statistics undergraduate math calculus education indefinite/definite sums extending type tools Calculus of Variations worksheet tools

Appendix C Java Quick Reference and Installing Software

C.1 JAVA ELEMENTS Compilation and Interpretation of Application Compile Hello.java Run Hellow.class

> javac Hello.java > java Hello

Application Program Structure public class Hello { public int a = 2; public static void main(String[] args) { double b = 3., c; int three; final double HBARC = 197.32; three = add(1,a); c = Math.sin(b); System.out.println("sin(b)= "+ c); } // -end main, begin addpublic static int add(int x, int y) {return x + y;} } /* Comment field (multiple lines OK) */ // One line comment /** Documentation comment **/ public static double f(double x) {return x*x ;}

Class definition, in file Hello.java Beginning brace; must have mate Class variable a main method Opening brace for main Variables local to main Integer variable Unchangeable constant Call to method Call to math xlibe Print to screen Closing brace for main Comment Silly method to add to int’s Returns value of “add” Ending brace for class Comment in field One line comment Documenting comment Method (function) f (x) Multiple lines within brace OK too

Data (Variable) Types Description Integer Floating Point Single Character Logical

Type byte, short, int, long float, double char boolean

Size/Format 1B (8b), 2B, 4B, 8B Single, Double precision (8B) 16-bit (2B) 1 bit, true or false

462

APPENDIX C

Sample Data Representations Representation i = 10 L; i = 10 l; i = 10.; i = 10.0 F;, 10.0 f;

Meaning long integer decimal (double) float (single)

Representation i = 3.1e+8, 3.1E+08 i = OxA; i = O17;

Meaning Scientific Hexidecimal Octal

Naming Convention variable, variableName; ClassName, Classname; CONSTANT. Reserved Words abstract boolean break byte case catch char class

double else extends final finally float for goto *

int interface long native new null package private

static super switch synchronized this throw throws transient

do short default return continue public const * protected

instanceof while import volatile implements void if try

Arrays int [ ] i; double [ ] x = new double[10]; double [ ][ ] y = new double [8][9]; arrayname [i][j] = i*j; int a[3] = {1, 2, 3}; int size = arrayname.length;

Declare integer array Declare & create (allocate memory) array Declare & create 2D array Assign value to array element Assign values to array elements Extract length of array (any array)

Arithmetic Operators Operator + * / %

Example x + y x - y x * y x / y x % y

Description Add x to y (also concatenates strings) Subtract y from x Multiply x by y Divide x by y Remainder from x/y; the modular op

Unary Operators Operator + ( )

Example +x -x x = (double) 1

Description Promotes x to int if it is a byte, short, or char Arithmetically negates x Cast (converts data types)

463

JAVA QUICK REFERENCE AND INSTALLING SOFTWARE

Shortcuts Operator ++ ++ ---

Example x++ ++x x---x

Description Use x, then set x = x + 1 Set x = x + 1, use x Use x, then set x = x -1 Set x = x -1, then use x

Relational Operators Operator > >= < y x >= y x < y x > y x >> y x & y x | y x ˆ y ˜y

Operation Shift bits of x right by distance y Shift bits of x left by distance y Shift bits of x right by distance y (unsigned) Bitwise and Bitwise or Bitwise xor Bitwise complement

Bitwise Operators Operator >> >> & | ˆ ˜

Compound Assignment Operators Operator += -= *= /= %= &= |= ˆ= =

Example x += y x -= y x *= y x /= y x %= y x &= y x |= y x ˆ= y x = y

Equivalent to

x x x x x x x x x x

= = = = = = = = = =

x + y x - y x * y x / y x % y x & y x | y xˆy x > y

Order of Precedence 1. unary then binary ops 5. x++ 9. +

2. left to right 6. ++x 10. >>>

3. assignment ops 7. cast 11. ==

4. RHS then LHS 8. * 12. =

464

APPENDIX C

Mathematical Function Library [Use: Math.sin(b)] E log

PI pow(x,3.)

sin sqrt

cos random

tan abs

asin max

acos min

atan ceil

atan2(y,x) floor

exp rint

Flow Control while (x = 80 ) { grade = ’B’; } else if ( score >= 70 ) { grade = ’C’; } switch (month) { case 1: s = "Jan"; break ... case 12: s = "Dec"; break;} for ( i = 0; i < 100; i++ ) { } do { } while ( ) : x = x*y; ... break ; if ( i==99 ) continue;

Evaluate as long as true; Second line to be evaluated, etc. Evaluate once if true Can have one or more lines in {} Only one else permitted (catchall) The “if” condition Any number of else if’s OK Inaccessible if earlier else satisfied Fall through if no break Can have many cases (initial value; repeat for; increment) Multiline code block Goes through at least once Break send control back here Use continue within loops for new iteration Unlabelled continue, jump to loop end Break out of loop if no label

Input and Output, Screen and Keyboard System.out.println ( "count = " + j ); import java.io.*; main(String[] argv) throws IOException, FileNotFoundException BufferedReader b=new Buffered Reader (new InputStreamReader(System.in)); String s = b.readLine(); r = Double.valueOf(s).doubleValue();

Screen output Include input/output package Main method to handle exceptions; the exception Read via 3 filters; ” Line read stored as string Convert string to double

Input and Output, Files import java.io.*; main(String[] argv) throws IOException, FileNotFoundException BufferedReader b = new BufferedReader (new InputStreamReader (new FileInputStream("radius.dat"))); String s = b.readLine(); double x = Double.parseDouble(s); int i = Integer.parseInt(s); PrintWriter q = new PrintWriter (new FileOutputStream("area.dat", true); PrintWriter q = new PrintWriter (new FileOutputStream("area.dat", false); q.println("radius = " + radius); q.close();

Need for all but screen & keyboard

main with exception throwing Open file with JDK 1.1; ” ” Read 1 line, save as string Convert string to double Convert string to integer Open output file with JDK 1.1; appends file Open output file with JDK 1.1; overwrites file Output word radius and its value Closes file

JAVA QUICK REFERENCE AND INSTALLING SOFTWARE

465

Compiling Options javac [options]?source files @ -g Generate all debugging info -g:none Generate no debugging info -g:lines,vars,source Generate only some debugging info -O Optimize; may hinder debugging or enlarge class files -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath ?path@ Specify where to find user class files -sourcepath ?path@ Specify where to find input source files -bootclasspath ?path@ Override location of bootstrap class files -extdirs ?dirs@ Override location of installed extensions -d ?directory@ Specify where to place generated class files -encoding ?encoding@ Specify character encoding used by source files -target ?release@ Generate class files for specific VM version -cp -classpath ? directories and zip/jar files@ -cp -classpath dir Use files in directory dir set search path for application classes and resources -D?name@=?value@ set a system property -verbose[:class|gc|jni] enable verbose output -showversion print version of Java being used -? print help message -help print help message -X print help on non-standard options

Execution > java [-options] class [args...] > java -jar [-options] jarfile [args...]

To execute a class To execute a jar file

C.2 TRANSFERRING FILES FROM THE CD The Maple and Java worksheets and programs given on the CD do not need any special installation. You just need to pick a directory/folder on your computer where you want them stored, and copy the appropriate folder from the disk into that directory. Of course you will need to insert the CD into the CD drive, and open that drive to see the contents of the CD. (On Windows computers you get to that drive by clicking on the My Computer icon.)

466

APPENDIX C

C.3 USING OUR MAPLE WORKSHEETS You open one of our Maple worksheets by starting Maple on your computer and then opening the worksheet from within Maple. Of course you must have Maple installed on your computer for this to work, and that is done with the installer that comes on the Maple CD. We have written the Maple worksheets with Maple Versions 6–9.5. We have not tried to specialize the worksheets to just the latest version, so most every command should work with any recent version of Maple. You may get a complaint from Maple when you first load the worksheet that it was created with a more recent version (that the Maple people would not mind you running out and purchasing), but that should cause no problems. You may find that the response shown in the text does not quite match that given by your version of Maple. In any case, learning how to cope with ever-advancing software and hardware is part of the learning experience we wish to present. C.4 USING OUR JAVA PROGRAMS The Java programs on the CD are almost all source (.java) files and therefore need to be compiled before they will execute. There are a number of ways in which you may work with and execute our Java programs. The least painful is probably to use a complete programming workbench like Code Warrior or the Forte Suite. With these you need only punch some buttons to compile, debug, and execute programs. We view these programming environments as powerful tools that will increase the productivity of professional programmers. However, we believe that the more basic approach is preferable for the purpose of this introductory book. The basic approach to Java programming that we prefer is to use an editor to modify the Java source file, save the file, and then execute the resulting class file from the prompt (command line) of a shell. In this way the beginning programmer learns that there are just two basic types of files to deal with, and that they are universal for all operating systems. In contrast, the programming environments often produce an assortment of files containing formatting and other such information, with the user not sure just what is truly needed to write and run a program. On Windows computers you may use a text editor such as Notepad to modify Java source file. You should not, in contrast, use a word processor such as MS Word, since it formats text by inserting control characters that are invisible to you but stop the Java compiler from getting its job done. We prefer using the WinEdt [WinEdt] editor on Windows, which also provides immediate access to a shell in the same directory as the source (it is also excellent for LATEX). On Unix systems we recommend the free gnu emacs (especially xemacs)

JAVA QUICK REFERENCE AND INSTALLING SOFTWARE

467

editor [Gnu]. It is powerful and permits compilation and execution from within the editor. It is excellent too with LATEX. In addition, the jEdit Java editor is an excellent and free Java editor that is written in Java. Thus, it runs identically on all operating systems and provides some very useful color coding of codes. Once you have your .java file open in an editor, you need a shell or command line from which to issue the javac and java commands. On Unix this is easy if you first change to the appropriate directory and open your editor from there. It is equally easy on Windows computers if you open a shell from your editor. However, if you use the Start > Run > Open > Command route, then you will have to navigate the Window’s file directory structure with the dir and cd commands. C.5 INSTALLING PTPLOT (OR OTHER) PACKAGES The first step in setting up PtPlot is to download its latest version and then uncompress it.1 The plotting package we call PtPlot is part of a larger Java project called Ptolemy [PtPlot], all free for downloading. After you have properly unzipped or untarred the PtPlot package, a directory such as ptplot5.2 should be created. The number 5.2 here is the version number of the PtPlot package that we are now using; there may be a newer version when you do your download. For our examples, we have renamed the directory in which PtPlot resides as simply ptplot, with no version number attached. On Unix, we assume that your ptplot directory is ˆ/java packages/ptplot, where the ˆ indicates your home directory. On Windows, we assume that your ptplot directory is C:\ptplot.2 Advanced users may prefer to keep the version number in the directory name, or use a different organizational system. However, if this is the first time that you have installed a Java package, we recommend that you use the same directory names as we have. Now that we have placed the PtPlot package in its own directory, we need to tell Java where to find it. As a matter of convention, the Java compiler javac and interpreter java assume that the value of a variable named CLASSPATH contains the information on where packages such as PtPlot are stored. This type of variable that controls the environment in which programs run is called an environment variable. On account of the programs in the packages having already been compiled into class files, the variable that directs Java to the classes is called the 1“1 CLASSPATH. To get PtPlot to work under Java you need to modify the CLASSPATH variable to include the location where PtPlot is stored: 1 If you have never done this before, you may want to do it with a friend who has. In Windows, you use WinZip [WinZip] to unzip files. In Unix, you try double-clicking on an icon of the file, or decompress it from within a shell with gunzip and tar -xvf. 2 If you use the Windows automatic installer, you should install to C:\ptplot, rather than C:\ptolemy\ptplot5.2, if you want to follow our examples verbatim.

468

APPENDIX C

Windows 95: Open the autoexec.bat file (usually C:\autoexec.bat) in a text editor such as Notepad. At the end of the file, add the line SET CLASSPATH=%CLASSPATH%;C:\ptplot

Save the autoexec.bat file and restart your computer. Windows 98/ME: Click Start and then Run under that. Key in the command name msconfig, and press Enter. The System Configuration Utility should appear. Select the tab named Autoexec.bat and look for a line that says SET CLASSPATH. If you cannot find that line, click New, and enter SET CLASSPATH = C:\ptplot. If the SET CLASSPATH line already exists, select it, choose Edit, add a semicolon ; to the end of the line, and then add C:\ptplot after the semicolon. The semicolon is a separator that tells Java that it should look in more than one location for class files. Make sure that the checkbox by the CLASSPATH line is checked, and click OK. Answer Yes when Windows asks you whether you want to restart the computer. Windows NT/2000/XP Open the Control Panel (Start, Settings, Control Panel in NT/2000, or Start, Control Panel in XP). Open the System icon (you may need to switch to the Classic View in Windows XP). Under the System Properties window, select the Advanced tab, and choose Environment Variables. Two lists should be shown. One contains environment variables just for you, and one contains environment variables for all users on the system. In your personal environment variable list, look for CLASSPATH. If you cannot find the CLASSPATH variable, click New, enter CLASSPATH for the variable name, and C:\ptplot for the value. If the CLASSPATH variable already exists, select it, choose Edit, add a semicolon ; to the end of the current value, and then add C:\ptplot after the semicolon. The semicolon is a separator that tells Java that it should look in more than one location for class files. Click OK until you get back to the Control Panel, and then restart your machine. Solaris: We assume that you do not have system authority for your computer, and so will install PtPlot in your home directory. We suggest that you make a subdirectory called java packages in your home directory and install PtPlot there: > cd > mkdir java packages > mkdir java packages/ptplot

Change to my home directory Create subdirectory in present directory Create subdirectory in java packages

If the ˜ is used to represent your home directory, this assumes that you will be installing the PtPlot package in ˜/java packages/ptplot/ Your CLASSPATH variable that needs updating is contained in the initiation (init) file .cshrc in your home directory. Because this file name begins with a . it is usually hidden from view. Beware; it is easy to mess up your .cshrc file and end up in a quagmire in which you cannot enter commands. For this reason we suggest that you first create a backup copy of your .cshrc file, in case anything

469

JAVA QUICK REFERENCE AND INSTALLING SOFTWARE

goes wrong: > cp .cshrc .cshrc bk

Make a backup, just in case

Next, open the .cshrc file in a text editor. Because this file may contain some very long lines that must be kept intact, if your text editor has an “automatic word wrap” feature, make sure it is turned off. Next look for a line that starts with setenv CLASSPATH. If you cannot find that line, add setenv CLASSPATH ˜/java packages/ptplot on its own line at the end of your .cshrc file. If the setenv CLASSPATH line already exists, add a colon : to the end of the existing line, and then add ˜/java packages/ptplot after the colon. The colon is a separator that tells Java that it should look in more than one location for class files. Save your .cshrc file, then close and reopen all shells that you have open (or log off and then back on). Once you have the CLASSPATH variable set, you should make sure that it is working. To check, go to a command prompt in a shell and enter > echo %CLASSPATH% > echo $CLASSPATH

Windows check Unix check

The complete value for the CLASSPATH variable should be printed to the screen, for example: /home/jan/java/classes:/home/jan:/home/jan/mpiJava:/usr/local/mpiJava/ lib/classes:/home/jan/java packages:/home/jab/java packages/ptplot:

If your changes do not take, carefully follow the directions again, then ask for help. At this point, you are ready to try out PtPlot. Get the file EasyPtPlot.java containing a sample plot program from the disk or the Web and enter > javac EasyPtPlot.java > java EasyPtPlot

Compile sample plot program Run sample plot program

If the PtPlot package was installed correctly, you should get a nice graph on your screen. If this does not work, ask for help. C.6 INSTALLING JAVA DEVELOPER’S KIT Sun Microsystem’s Java Web site [SunJ] contains the latest, and free, version of JDK, the Java Developer’s Kit (presently j2sdk1.4.1). Though most operating systems have all that is needed to run Java programs, you need the developer’s kit to compile Java programs from a source file. The actual JDK tools occupy about 7 MB, with its (optional) documentation occupying more than ten times this space (the documentation is in HTML). This means that you may want only the tools if space is an issue.

470

APPENDIX C

On Windows computers, JDK is usually placed in C:\jdk1.2\bin, while on Unix it is in /usr/local/jdk1.2. Once installed, you need to update the PATH and the CLASSPATH environment variables so that the shell knows where to find the Java compiler and classes you make. This is essentially the same procedure we used to include the PtPlot classes in the CLASSPATH. Under Windows you need to go to the System control panel and then select the Environment tab. You then need to add ;C:\jdk1.2\bin (or whatever file JDK was installed into) to the PATH variable. Likewise, the CLASSPATH need to be added, for example, > set CLASSPATH=C:\...

Finally, you need to reboot for the changes to take effect.

Bibliography

MAPLE AND MATHEMATICA [Corl 02] C ORLESS , R. M. (2002), Essential Maple 7, An Introduction for Scientific Programmers, Springer, New York. [Gass 98] G ASS , R. (1998), Mathematica for Scientists and Engineers, Prentice-Hall, Upper Saddle River, NJ. [Hass 03] H ASSANI , H. (2003), Mathematical Methods Using Mathematica, Springer, New York. [Heck 03] H ECK , A. (2003), Introduction to Maple, Third Ed., Springer, New York. [Meissner 95] M EISSNER , L. P. (1995), Maple, Fortran 90, PWS Publishers, Boston. [N&W 96] N ICOLAIDES , R., AND N. WALKINGTON (1996), Maple, A Comprehensive Introduction, Cambridge University Press, Cambridge. [R&M 01] ROMANO , A., AND A. M ARASCO (1996), Mathematica Navigator, Second Ed., Elsevier/Academic Press, New York. [Rusk 04] RUSKEEPAA , H. (2001), Scientific Computing with Mathematica, (ODEs) Birkh¨auser, Boston. [Wolf 99] W OLFRAM , S. (1999), The Mathematica Book, Wolfram Media, Champaign, and Cambridge University Press, Cambridge. [Zimm 02] Z IMMERMAN , R. L., AND F. I. O LNESS , (2002), Mathematica for Physicists, Addison-Wesley, San Francisco.

JAVA AND FORTRAN [Bron 03] B RONSON , G. J. (2003), Java for Engineers and Scientists, Thompson, Brooks/Cole, Toronto.

472

BIBLIOGRAPHY

[Chap 04] C HAPMAN , S. J. (2004), Java for Engineers and Scientists, Second Ed., Pearson, Prentice Hall, Upper Saddle River, NJ. [ChapF 04] C HAPMAN , S. J. (2004), Fortran 90/95 for Engineers and Scientists, Second Ed., McGraw-Hill, New York. [Dav 99] DAVIES , R. (1999), Introductory Java for Scientists and Engineers, Addison-Wesley, Harlow, UK. [Eck 02] E CK , D. (2002), Introduction to Programming Using Java, Version 4.0, (a free textbook), http://math.hws.edu/javanotes. [Flan 97] F LANAGA , D. (1997), Java in a Nutshell, Desktop Quick Reference, Second Ed., O’Reilly, Sebastopol, CA. [F90 CSEP] T HE C OMPUTATIONAL S CIENCE E DUCATION P ROJECT, Fortran 90 and Computational Science, http://csep1.phy.ornl.gov/pl/pl.html. [F90 UL] M ARSHALL , A. C. The University of Liverpool Fortran 90 Course Notes, http://www.liv.ac.uk/HPC/F90page.html. [Smit 91] S MITH , D. N. (1991), Concepts of Object-Oriented Programming, McGraw-Hill, New York.

COMPUTATIONAL SCIENCES ´ (1997), Computational Physics, [CP 05] L ANDAU , R. H., AND M. J. P AEZ Problem Solving with Computers, Wiley, New York; ´ , AND C. C. B ORDEIANU (2005), ComputaL ANDAU , R. H., M. J. P AEZ tional Physics, Problem Solving with Computers, Second Ed., Wiley, New York. [G&T 96] G OULD , H., AND J. T OBOCHNIK (1996), An Introduction to Computer Simulation Methods, Second Ed., Addison-Wesley, Reading, PA. [Gar 00] G ARCIA , A. L. (2000), Numerical Methods for Physics, Prentice Hall, Upper Saddle River, NJ. [Knuth 86] K NUTH , D. E. (1986), The TeXbook, Addison-Wesley, Reading, PA. [L&F 93] L ANDAU , R. H., AND P. J. F INK (1993), A Scientist’s and Engineer’s Guide to Workstations and Supercomputers, Wiley, New York. [Lap 85] L AMPORT, L., (1986), LATEX: A Document Preparation System, Second Ed. Addison-Wesley, Reading, PA. [LAP 95] A NDERSON , E., Z. BAI , C. B ISCHOF, J. D EMMEL , J. D ONGARRA , J. D U C ROZ , A. G REENBAUM , S. H AMMARLING , A. M C K ENNEY, S. O S TROUCHOV, AND D. S ORENSEN (1995), Lapack Users’ Guide, Second Ed., SIAM, Philadelphia; http://netlib.org.

473

BIBLIOGRAPHY

[Press 94] P RESS , W. H., B. P. F LANNERY, S. A. T EUKOLSKY, AND W. T. V ETTERLING (1994), Numerical Recipes, Cambridge University Press, Cambridge, UK. [UCES] U NDERGRADUATE C OMPUTATIONAL E NGINEERING http://www.krellinst.org/UCES/. [Wilk 95] W ILKINS , D. R.,

AND

S CIENCE,

Getting Started with LaTeX, Second Ed., (1995).

http://www.maths.tcd.ie/˜dwilkins/LaTeXPrimer/,

[Zach 96] Z ACHARY, J. L. (1993), Introduction to Scientific Programming, Computational Problem Solving using Maple and C, Springer-Telos, Santa Clara, CA.

MATHEMATICS [A&S 64] A BRAMOWITZ , M., AND I. A. S TEGUN (1964), Handbook of Mathematical Functions, U.S. Govt. Printing Office, Washington. [B&R 92] B EVINGTON , P. R., AND D. K. ROBINSON (1992), Data Reduction and Error Analysis for the Physical Sciences, McGraw-Hill, New York. [Fral 76] F RALEIGH , J. B. (1976), A First Course in Abstract Algebra, Second Ed., Addison-Wesley, Reading, PA. [Krey 88] K REYSZIG , E. (1988), Advanced Engineering Mathematics, Sixth Ed., Wiley, New York.

SCIENCE [D&A 00] D EGAUDENZI , M. E., AND C. M. A RIZMENDI, Wavelet-Based Fractal Analysis of Electrical Power Demand, Fractals, 8, no. 3 (2000) 239–245. [Feig 79] F EIGENBAUM , M. J. (1979), J. Stat. Physics 21, 669. [Fein 76] G. F EINBERG, Phys. Rev. 159 (1976) 1089–1105. [Feyn 63] F EYNMAN , R. P., R. B. L EIGHTON , Feynman Lectures on Physics §9.7.

AND

M. S ANDS , (1963), The

[M&T 88] M ARION , J. B., AND S. T. T HORNTON (1988), Classical Dynamics of Particles and Systems, Third Ed., Harcourt Brace Jovanovich, Orlando, FL. [R & M 93] R EITZ , J. R., F. J. M ILFORD , AND C HRISTY, R. W. (1993), Foundations of Electromagnetic Theory, Fourth Ed., Addison-Wesley, Reading, PA.

474

BIBLIOGRAPHY

[Rash 90] R ASBAND , S. N. (1990), Chaotic Dynamics of Nonlinear Systems, Wiley, New York. [Ser 00] S ERWAY, R. A., AND R. J. B EICHNER (2000), Physics for Scientists and Engineers, Fifth Edition, Saunders, Orlando, FL. [Smith 65] S MITH , J. H. (1965), Introduction to Special Relativity, Benjamin, New York. [USN&WR] US News & World Report, 1/28/99.

WEB RESOURCES Web pages change so often that we expect some of these URLs to fail. If so, we suggest searching on the name. [Black] The Java-Linux Porting Project, http://www.blackdown.org/. [CPlets] Interactive plets,

Computational

Physics

Ap-

http://www.physics.orst.edu/˜rubin/nacphy/CPapplets/, http://www.physics.orst.edu/˜rubin/TALKS/CPtalk/DEMO/samples.html.

[Dislin] H. Michels, MPI fuer Sonnensystemforschung, Dislin Scientific Plotting Software, http://www.mps.mpg.de/dislin/. [DX] Open DX,

Data http://www.opendx.org/.

Explorer

(formerly

IBM’s

DataExplorer),

[Energy] Edison Electrical Institute, Climatron Research Technical Reports, http://www.climatron.com; California Independent System Operator, http://www.caiso.com; Platte River Power Authority, http://www.prpa.org. [Game] Gamelan Repository of Java Code, http://www.gamelan.com/. [Gnu] The GNU Project, Emacs, http://www.gnu.org/. [Gnuplot] A command-line interactive datafile and function plotting utility, http://www.gnuplot.info/. [Grace] A WYSIWYG 2D plotting tool (descendant of ACE/gr, Xmgr), http://plasma-gate.weizmann.ac.il/Grace/. [Jama] H ICKLIN , J., C. M OLER , P. W EBB , R. F. B OISVERT, B. M ILLER , JAMA: Java Matrix Library, R. P OZO , AND K. R EMINGTON, http://math.nist.gov/javanumerics/jama/.

[Jampack] Java

Matrix

Package,

berlin.de/ˆlamour/software/JAVA/Jampack/Doc/00 Manual.html.

[jEdit] jEdit, http://www.jedit.org.

http://www.mathematik.hu-

475

BIBLIOGRAPHY

[JF] NPAC JavaForce, http://www.npac.syr.edu/projects/javaforce/. [Jgran] Java Grande Forum, http://www.javagrande.org/. [JHPC] Java for High Performance Computing, http://www.jhpc.org/. N UMERICS, Java http://www.vni.com/products/imsl/jmsl.html.

Numerical

[JNL] V ISUAL

Library,

[Jopt] Java Optimization, http://www-2.cs.cmu.edu/˜jch/java/optimization.html. [JsimS] Physics Simulation with Java, http://www.particle.kth.se/˜fmi/kurs/PhysicsSimulation/. [Jtut] Sun’s Java Tutorial, http://java.sun.com/docs/books/tutorial. [MapWeb2] Indiana Maple WWW Tutorial, http://www.indiana.edu/˜statmath/math/maple. [MMM] Mathematica,

Maple,

Matlab,

IDL,

Translations,

http://amath.colorado.edu/computing/mmm/.

[Net] Netlib, http://www.netlib.org/. [NIST] Java

Numerics

at

National

Inst

of

Sci

and

Tech,

http://math.nist.gov/javanumerics/.

[OSS] Open Source Software, http://www.opensource.org/. [Printf] B ENGTSSON , H., Java Printf Package, http://www.braju.com/. [PtPlot] Ptolemy plotting project, http://ptolemy.eecs.berkeley.edu/java/ptplot/. [SunJ] Sun Microsystems Java Home Page, http://java.sun.com/. [UnixWeb] L ANDAU , R. H., P. J. F INK , AND M. J OHNSON, Unix Survival Guide, http://www.physics.orst.edu/˜rubin/nacphy/UNIX/. [Visad] VisAD, Java Component Library for Interactive and Collaborative Visualization and Analysis of Numerical Data, http://www.ssec.wisc.edu/˜billh/visad.html. [WinEdt] S IMONIC , A., WinEdt, http://www.winedt.com/. [WinZip] WinZip Extractor and Compactor, http://www.winzip.com/.

This page intentionally left blank

Index

( ), 62 *, xxi ˆ, Maple, 15 !=, 251 **, 15 .dvi, 413 .java, 201 .mws, 10 .pdf, 413 .ps, 244, 413 .tex, 413 /*, 202 //, 202 :=, 34 , 187, 251 Graphics, 399 [>, 14 [], 63, 64, 90, 344 %, 201 ++, 257 --, 257 ->, 39 { }, 63, 65, 66, 113, 150, 205 Abstract data, see Data types Abstraction, 32, 63, 125, 148, 216, 294, 319, 344, 359, 363

Algebraic numbers, 10, 28, 32, 35, 39, 110, 112, 151 Angular momentum, 145, 149, 150, 165, 171, 176, 341, 343 Animations, 76–78, 102, 441 Applets, 232, 238, 244, 309, 311, 356, 358, 394, 396, 402, 403, 405 buttons, 403 appletviewer, 358, 394, 398, 399, 408 Archive, Java, 244, 441 Argument coercion, see Coercion Arrays, 151, 225, 269, 271, 343–345, 348, 388, 396, 442 as arguments, 225, 348 Java, 344 LATEX, 435, 436 length, 348 Maple, 52, 65, 90, 94, 151, 153, 156 arrow, 83, 90, 172, 180 Arrow functions, 39, 123, 187, 309 Assignment statements, see Statements, 258 awt, 238, 399, 400, 405, 407 Bifurcation, 381, 382 Binary, 29, 210, 442 operators, 258

Binning, 383 Bisection algorithm, 185, 194 Boolean expressions, 187, 252, 253 variables, 188, 208, 251 Bottom-up programs, 220 boxplot, 93 Break, 5, 44, 191, 193, 254, 257, 392, 419 Browser, 205, 238, 274, 394, 396, 400, 408 Byte code, see Compile Capacitors, 385 Cast, 222, 227, 383, 401 CD, xxiii, 10, 77, 95, 223, 274, 415, 465, 466 Central difference, 127 Chaos, 378 Classes, 200, 205, 216, 232, 238, 239, 264, 275, 294, 320, 328, 339, 356, 372 import, 399 multiple, 360, 375, 408 subclass, 339 superclass, 369 variables, 215, 216, 285, 294, 320, 362, 363, 371 Coercion, 224, 226, 228 Combine, 41, 187, 252, 253, 363 Comments Java, 201, 202, 205, 274, 461

478 LATEX, 415 Maple, 15, 96 PtPlot, 237 Compile, 199, 201, 202, 217, 240, 361, 399, 442, 465 Compiler, 4, 201, 217 Complex numbers, see Numbers Composition, 124, 363, 369 Computational science, xxi, 1, 472 Constants, 32, 212, 221, 294, 299, 302, 331 Constructors, 277, 321, 324, 325, 336, 362, 365, 370, 371, 375, 407 Containers, 405 Control structures, 188, 234, 249, 251, 253, 257 Cross product, 149, 150, 166, 172 D (derivative), 117, 121,

123, 124, 298, 302 Data, 5, 32, 91, 93, 101, 102, 200, 205, 216, 250, 265, 269, 359 conversion, 228 hiding, 371 Maple, 52 types, 52, 54, 63, 65, 206, 208, 217, 227, 316, 319, 320, 344, 366 Daughter class, 339 DEplot, 300 Derivative, 108, 117, 128, 387 as function, 121 Maple, see D and diff numeric, 126, 292 partial, 119, 120, 124 second, 119, 124 vector, 166 diff (derivative), 117, 118, 121, 122, 128, 137, 300 Differential equations Java, 290

INDEX

Maple, 290, 297 Digits, 30

Directories, 9, 11, 97, 103, 202, 239, 245, 360, 365, 444 Documentation, Java, 274 Dot operator, 149, 150, 165, 234, 279, 323, 326, 328, 348, 391 Double precision, see Doubles Doubles, 26, 206, 207, 209, 211, 213, 214, 223, 226, 265, 267, 276, 366, 400 Drag, 6, 290, 291, 297, 302, 306 Eigenvalues, 167, 179, 183, 351 Electric field, 56, 82, 385 Encapsulation, 359, 371 Equals, 34, 36, 109, 204, 251, 252, 434 Equations differential, 292, 300, 301 discrete, 379 finite-difference, 388 LATEX, 427, 434 meaning of, 36, 109 motion, 130, 248, 292, 310 nonlinear, 115 parametric, 81, 105 partial-differential, 132, 385, 387 simultaneous, 65, 113, 151, 170 simultaneous ODE, 290, 301 solution, see Solutions transcendental, 110 waves, 106 Equipotential surface, 71, 391 Errors round-off, 26, 44, 219, 256, 283, 292

truncation, 44, 213 Euler’s rule, 283, 293 eval, evaln, 42, 43, 111, 115 Exceptions, 44, 46, 272 Execution groups, 14, 21 expand, 40, 41, 111, 168, 330 Exponent, 29, 30, 211, 444 Exponentiation Java (Math.pow), 206 Maple, 15, 30 Expressions, 32, 34, 41–43, 118, 121, 187, 309, 332 Boolean, see Boolean Extract part, 303, 323, 332, 348, 351, 361, 367 factor, 23, 40, 116 fieldplot, 82 final, 212, 285, 294

Finite differences, 387 Floating-point numbers, 27, 29, 30, 38, 44, 49, 169, 200, 211, 213, 277 floats, 26, 214, 223 Flow control, see Control structures Flowcharts, 249, 408 Folders, see Directories Forward difference, 127, 292 fsolve, 110, 112 Functions, 37 built-in, 39 complex, 85, 317, 332, 340 Java, 215, 218 LATEX, 432 Maple, 28, 36, 37, 43, 138, 192, 299, 309 user-defined, 39, 219 Gnuplot, 6, 94, 240, 242–244, 391 Graphics context, 407 histogram, 93

479

INDEX

HTML, 274, 394, 469 I/O, see Input/output if-then-else, 254, 256

Impedance, 317, 318 import, 234, 275, 360, 399

Inertia tensor, see Moment of inertia Inheritance, see Objects Input/output command line, 268 exceptions, 272 files, 269 formatted, 275, 279 keyboard, 265 screen, 264 streams, 263, 265, 267, 270, 274, 276, 279 Installing software, 465 Instances, 206, 320, 324 Instructional guide, 1 Integers, 27, see Numbers Integration, 137 definite, 137 indefinite, 137 Maple, 136 numerical, 282 volume, 184 weights, 283 Interface, 372 Interpreter, 199, 201, 202, 268, 445 Irrational numbers, 27 Jama matrix library, 341, 349 JApplet, 405, 406, 409 jar, 244–246, 465 javadoc, 274 Kernel, 3 Landau’s rules, 4 Language basic machine, 3, 4 compiled, xxii, 4, 10, 169, 187, 194, 199,

341, 394, 413, 414, 442, 443 computer, 2 high-level, 3 interpreted , 4 Laplace’s equation, 387 LATEX, xxii, 413 accents, 431 arrows, 430 environments, 416, 422 fonts, 420 footnotes, 426 fractions, 432 functions, 432 graphics, 436 Greek letters, 428 lists, 422 math, 421, 427, 428, 440 matrices, 435 parentheses, 430, 433 quotations, 426 sections, subsections, 425 special characters, 417 super/subscripts, 431 tables, 423 lhs (left-hand side), 36 limit, 46, 117 Linear algebra, 90, 94, 145, 147, 151, 181, 341, 344, 349 Linear program, 254 listplot, see Plots, 92 Lists, 63–65, 88, 94, 96, 101, 154, 325, 422 Logic, see Boolean Logical operators, 188, 251 Logistics map, 312, 378 Loops counting, 255, 256 do, 256 for, 189, 255 index, 256 nested, 286 while, 191 Machine precision, 212 Main method, 205, 217 Mantissa, 29, 211

Mapping, 121 Maps, 378 Markup language, 394, 414 Math., 218 Math library, see Functions Matrices, 147, see Linear algebra, 151, 344 dimension, 152 library, 349 multiplication, 157 plots, 97 matrixplot, see Plots Memory, see Words Methods, 36, 215, 216 array arguments, 348 dynamic, 206, 361 main, 205 Maple, 192 overloading, 226, 228 static/nonstatic, 206, 325 Modular program, 215, 216 Moment of inertia (tensor), 150, 151, 174 Mother class, 339 Naming convention, 212 Nonlinear systems, 115, 378, 380 Nonstatic, see Static/nonstatic normal, 41 Numbers, 31 algebraic, 39 complex, 40, 85, 110, 149, 313, 314, 316, 320, 330, 332, 373 floating-point, 27, 29, 211 imaginary, 31, 112, 314 integers, 27, 208, 210, 227, 256, 265, 276, 277 rational/irrational, 27 real, 29, 31, 110, 316, 326 Object-oriented programs, 200, 334, 355, 359 Objects, 63, 218, 316, 319 hierarchies, 369

480 inheritance, 369, 399 ODE, see Equations OOP, see Object-oriented programs op, 303 Operating system, 3, 4 Operators conditional, 187, 251 Java, 206, 257, 258, 320 Maple, 16 relational, 187, 251 Optional materials, xxi Oscillator harmonic, 130 nonlinear, 129, 278 other.object, 328 Output, see Input/output Overflow, 44, 213 Overloading, 224 Packages, 218, 232, 238, 239 Parameters, 36, 80 Parent class, 369 PDE, see Equations PI, 29, 32, 205 Math.PI, 218 Pixel, 398 Planetary motion, 310 plot, 58 Plots, 55, 58, 69 complex, 85, 243, 314, 332, 333 contour, 71, 99, 100, 240, 243, 385, 391 data, 91, 93, 94, 96, 98, 100, 102, 232, 240 field, 55, 70, 82, 385, 392 implicit, 81 list, 88, 91, 94, 96, 103 matrix, 97, 98, 154, 176 parametric, see phase-space phase-space, 78, 81, 106, 130, 305, 337 polar, 84 PtPlot, 232 scatter, 93

INDEX

set, 88 surface, 69, 77, 94, 100, 102, 240, 243, 332, 391 surfdata, 100 vector, 90 pointplots, 88 Polymorphism, 359, 376 Polynomial roots, 112 PostScript, 244, 413 Powers complex, 317 Maple, 15, 30, 51 Math.pow, 206 Primitive data types, 208 Principal axes, 176 printf, 275 Private, 371 Problem-solving environment, xxii, 10 Procedures, Maple, 192 Programs, 199 design, 249, 250 Maple, 187 Projectile motion, 247, 290, 302, 356 Prompt gnuplot, 241 Maple([>), 14 shell (%), 201 Pseudocode, 249 PtPlot, 219, 232, 234, 236, 238, 467 Public, 217 Quotes Java, 270 LATEX, 419 Maple, 16, 18, 62, 265 Reexpressing answers, see Simplifying answers Reference pass, 217, 294, 321, 396 Relaxation techniques, 388 Reserved words, 52, 152, 153, 212, 457 Resonance, 87, 313, 332 restart, 32

rhs (right-hand side), 36,

300 Rigid body, 145 RLC circuits, 313 Roots, see Solutions Rotations, 145, 171, 341 Rules of precedence, 16, 24 Scalar product, 149 scanf, 279 scatterplot, 91 Self similarity, 383 Sequence, 64 Sequential program, 254 Sets, 54, 63–65, 89, 100, 113, 115, 167 Shell, 3, 201, 243, 398 Shorthand notations, 257 Side effects, 34 Significant figures, 30, 211 simplify, see Simplifying answers Simplifying answers, 23, 34, 39, 41, 52, 109, 159, 163, 164, 176 Simpson’s Rule, 282, 287 Solutions analytic, 63, 110 numeric, 94, 105, 112, 114, 185 roots, 112 simultaneous, 113 verification, 111 Source file, 201 Stability, 56 Statements, 32 assignment, 33–35, 206, 252, 285, 442 Boolean, 252 compound, 252 declaration, 214 Java, 202, 205, 206, 226, 234, 251, 252, 255, 285 Maple, 32, 34 Static/nonstatic, 206, 217, 218, 294, 313, 320, 323–325, 328, 361, 362, 371

481

INDEX

Streams, 239, 263, 265, 267, 270, 274, 276, 279 Strings Java, 207, 264, 265, 267, 276, 319, 405, 464 Maple, 18, 128 Subroutines, 36 sum, Sum, 16 Swing, 405 Symbolic logic, see Boolean Tachyons, 50 Templates, 361 Tensor, 150 Text editor, 6, 202, 420, 438, 466, 467, 469 this.object, 328 3-D plots, see Plots Top-down programs, 219 Trapezoid rule, 282 Trial and error, 185

Truth table, 187, 252 Type cast, see Cast Typename, 320, see Data unapply, 43, 118, 122,

138 unassign, 32

Underflow, 44, 213 Value pass, 217, 294, 321 Variables, 32 class, 215, 216, 285, 294, 320, 362, 363, 371 dependent, 36 dynamic, 361 global, 193 independent, 36 instance, 324 local, 193, 215, 225 multitype, 376 static/nonstatic, 361 Vectors, 56, 65, 82, 83, 145, 147, 151, 152, 170,

291, 314, 323, 343, 344, 347, 349, 409, 449 calculus, 166 column, row, 155 eigen, 167 Java, 341 Maple, 154 multiplication, 160 operations, 149, 164 plots, 90, 97, 172 Verlet algorithm, 297 Visualization, value of , 55 Window coordinates, 400, 409 Words (memory), 20, 30, 209–212, 292 World coordinates, 400, 409 World Wide Web, xxii, xxiii, 9, 200, 274, 394, 396