Mathematica: A Practical Approach (2nd Edition)
Preface As scientists, engineers, and mathematicians, you spend a great deal of time performing mathematical calculations and manipulations by hand. Many of these calculations can be handled by a software program called Mathematica. This book offers a tutorial introduction to Mathematica.  Mathematica performs three basic types of computation: numerical, symbolic, and graphical. It works with numbers of arbitrary magnitude and precision, as well as with polynomials, power series expansions, matrices, and graphs. Mathematica provides the standard symbolic operations of algebra and calculus, including integration and differentiation. It can also plot functions and data in two or three dimensions.  Even though they included hundreds of functions, the developers of Mathematica were aware that they could not anticipate the needs of all users. Therefore, Mathematica was designed to be extensible by including its own programming language. It is flexible and extremely useful software for anyone who regularly performs complicated mathematical computations.  Although we wrote this book for people new to Mathematica, it also contains information of interest to those who already have experience with the program. For those who do not have Mathematica, this text describes the capabilities of the software in sufficient detail to enable you to decide whether it suits your needs.  Our book does not teach mathematics. It assumes you understand the theory behind what you want to do and just need to be told what Mathematica commands to use to make it happen. It teaches Mathematica by showing some common patterns of usage. It also shows you how to find the commands you need to solve your problems, how to use Mathematica interactively, how to manipulate expressions, how to visualize functions and data, how to write functions and packages, and how to import and export data. Each chapter ends with a set of exercises designed to give you practice with the material presented.  Why did we write this book? Because there wasn't such a book when we were learning Mathematica. Can't you learn Mathematica from Stephen Wolfram's book Mathematica: A System for Doing Mathematics by Computer? Yes, but it's a bit like learning English from a dictionary. As the definitive reference for Mathematica, Wolfram's book describes all the functions built into the program. On the other hand, our book focuses on how to use Mathematica.  It provides examples of useful constructs and functions, illustrative problem sets and complete solutions to half the exercises.  In our book, we strive to show the versatility of the program as well as its limitations. This book is intended to get you up to speed quickly.  Mathematica: A Practical Approach grew out of an undergraduate course we have been teaching at Stanford University since 1990. Although it was originally designed to be used for teaching courses and giving workshops in Mathematica, the book can also be used for independent study. All readers are encouraged to work through the exercises and to practice their programming skills.  Organization of this Book This book is divided into three parts. The first discusses how to use Mathematica interactively, the second focuses on programming, and the third describes how to build more complicated applications.  Part I: Introduction to Mathematica explains how to use Mathematica interactively, and is intended for those who have little or no experience with the program. It starts by describing how to start Mathematica, how to create a new Notebook, how to make Mathematica perform a calculation, how to access the on-line help.  It then details Mathematica's the numerical, symbolic, graphical, data manipulation and data analysis capabilities.  Part II: Programming is intended for those who have used Mathematica interactively and who are interested in learning how to write their own functions and packages.  It discusses rule-based, functional and procedural techniques for programming and describes several tips for speeding up your code.  It describes how to write your own packages and how to import, export, and format data and expressions.  The final chapter focuses on programming the user interface, i.e., creating buttons, palettes, and special notation.  Part III: Building Larger Applications describes how to write complicated Mathematica programs that integrate symbolic, numeric, graphical, data manipulation, and data analysis capabilities.  We emphasize principles of good Mathematica programming that lead to reliable, robust, efficient and elegant code.  We also discuss how to interface Mathematica with other programs, e.g., by creating HTML pages suitable for display on any World Wide Web browser.  The Appendix offers additional information on Mathematica. It contains answers to all the odd-numbered exercises and gives an extensive list of Mathematica books that cover more specialized areas.  At the end of each chapter is a problem set to give you practice with the material presented. We strongly urge you to work through the examples in the chapter and the exercises at the end of each chapter. Only through practice will you become proficient at Mathematica.  Although this book was developed using Version 3 of Mathematica (on a Macintosh G3 and a Pentium-based Micron) it illustrates general techniques that can be used with other versions of the software. Be aware, however, that earlier versions of Mathematica do not have some of the capabilities described, and later versions have capabilities that are not addressed. This book focuses on general techniques and thus should be of interest to most Mathematica users regardless of the version of the software they are using. We welcome feedback from our readers.  Please feel free to email us at colin@solstice.jpl.nasa.gov, cpw@cs.stanford, and nancy@variablesymbols.  Nancy's Background Nancy Blachman heard about Mathematica in the summer of 1988 when numerous articles started appearing in the press.  She first saw Mathematica in action in August of 1988 at SIGGRAPH, a computer graphics conference.  She was pleased to see that the program lived up to much of the hype that it had received.  In the fall of 1988, Nancy joined Wolfram Research, where she gave talks, seminars, and workshops on Mathematica.  Realizing that corporations, colleges, and universities want training in the use of Mathematica, in 1989, she founded Variable Symbols, Inc. to serve their needs.  She has traveled around the world giving hands-on workshops on Mathematica at industrial institutions including: Boeing, Chevron, Hewlett Packard, IBM, MITRE, Naval Research Laboratory, Shell, Xerox, and Wolfram Research, Inc., the developer of Mathematica. She has also given workshops at academic institutions including: Boston University, Massachusetts Institute of Technology, the University of California Berkeley Extension, and the University of California Santa Cruz Extension.  In Winter quarter of 1990, Nancy gave a course at Stanford University entitled "Problem Solving with Mathematica". Stanford has invited her back to teach it again every year since then.  Nancy has also given workshops at Mathematica Conferences sponsored by Wolfram Research.  Nancy has written or co-authored several books on Mathematica including "The Mathematica Quick Reference", "The Mathematica Graphics Guidebook", and "CalcLabs with Mathematica" for Stewart's "Calculus Concepts and Contexts".  Before getting involved with Mathematica Nancy was at Stanford University, where she was the instructor for a course on computer graphics and a teaching assistant for Donald Knuth's course in discrete mathematics.  In addition to teaching, she has developed software at Bell Laboratories, Resonex, and the Research Institute for Advanced Computer Science at NASA Ames.  Nancy holds a B.S. in mathematics from the University of Birmingham in the United Kingdom, an M.S. in operations research from the University of California at Berkeley, and an M.S. in computer science from Stanford University.  Colin's Background Colin is research computer scientist who works at the Jet Propulsion Laboratory in the areas of quantum computing and quantum communications.  He also teaches courses in quantum computing and Mathematica at Stanford University.  Colin first saw Mathematica in January 1990.  He had previously written several algebraic manipulation systems in Prolog and LISP and was immediately impressed by the power and expressiveness of the Mathematica system.  His first programming project was to simulate how coordination can arise from purely local adaptation rules in a society of mini-robots.  The simulation was up and running in just two hours of programming effort!  Overnight, Mathematica became Colin's programming language of choice for all mathematical, data analysis and graphical needs. Since that time, Colin has used Mathematica daily and has given numerous workshops and tutorials on Mathematica to engineering research laboratories, banks and government agencies.  He has recently published the first book on quantum computing, entitled "Explorations in Quantum Computing", with Scott Clearwater, that comes equipped with a multi-platform CD-ROM packed full of Mathematica simulations that illustrate the ideas of quantum computing in visually appealing ways.  Colin earned a Ph.D. in artificial intelligence from the University of Edinburgh, Scotland, in 1989.  He was also awarded a M.Sc. degree in Atmospheric Physics and Dynamics, from Imperial College of Science and Technology, University of London, and a B.Sc. in Mathematical Physics, from the University of Nottingham. Prior to going to Edinburgh, Colin was a research assistant to Prof. Stephen Hawking of Cambridge University. After completing his Ph.D., Colin joined the Dynamics of Computation Group at the Xerox Palo Alto Research Center, where he worked as a research scientist, linking ideas from physics, economics and computer science.  In early 1995 Colin joined Variable Symbols, Inc. and developed new workshops on financial and engineering applications of Mathematica. He has taught the Stanford Mathematica class "CS50: Problem Solving with Mathematica" since 1995.  Colin now leads a research group in quantum computing and quantum communications at the Jet Propulsion Laboratory, California Institute of Technology. He recently finished another book with Scott Clearwater entitled "Ultimate Zero and One: Computing at the Quantum Frontier".  Acknowledgments Nancy thanks her mother and father, Nelson and Anne Blachman, and Colin thanks his beautiful, talented, supportive, and most importantly, patient wife, Patricia, for their encouragement, and invaluable suggestions.  This book enlarges greatly (!) upon the first edition of "Mathematica: A Practical Approach".  It covers even more practical problem solving tips, ideas and suggestions in the light of the excellent feedback we received from several scientists, engineers, mathematicians and analysts.  In particular, we would like to thank Paul Abbott, Arnold O. Allen, Helmer Aslaksen, Peter Altenberg, Eric Apgar, Sheldon Axler, Rick Beldin, Susan Blachman, Robert Campbell, John Carroll, Rolan Christofferson, Anne Coleman, Shawn Ewing, Richard Fateman, Harold Falk, Yossi Friedman, Richard Gaylord, Bruce Herman, Thomas Highes, David Jacobson, Pat Lampert, Matthew Lutzker, Rafael O. Marrero, Wanda Martinez, Eddie Mizzi, Julia Olkin, Larry Seiford, Sha Xin Wei, Malcolm Slaney, Cameron Smith, Anton Tran, Ilan Vardi, Roger W. Vidal, Jim Wendel, Stephen Wolfram, and Eran Yehudai.  We used a draft of this book to teach our Mathematica class at Stanford University.  Several students gave us great feedback. We are especially grateful to Lyndell L.Asbenson, Greg Baker, Ed DeGrange, Derek Fong, Bonnie Gorsic, Steve Greidinger, Jeff Gruda, Jim Hwang, Blair Ireland, Stan Isaacs, Phil Isubaki, Richard O. Kahn, Karen Kraemer, Bing Kongmebhol, Eric Lim, David G. Lowell, Paul R. McGill, Motegi Tsuyoshi, Wentao Pan, Chris Phoenix, Dave Raisin, Kevin W. Rudd, Haydee Saffari, Valter Sorana, Phil Tsubaki, and Qinping Yang.  We also wish to thank Peter Altenberg for designing the cover for the first edition of the book, Kimberly for adapting the illustration for the second edition, and Kerry Reardon of KR Publishing for helping with production.  Nancy Blachman Colin P. Williams Princeton, NJ San Francisco, CA November, 1998 November, 1998