Mathematica in Action Wagon

Mathematica in Action

Problem Solving Through Visualization and Computation

Third edition

Stan Wagon Department of Mathematics and Computer Science Macalester College 1600 Grand Avenue St. Paul, MN 55105 USA [email protected]

Wolfram Mathematica ® is a registered trademark of , Inc.

ISBN 978-0-387-75366-9 e-ISBN 978-0-387-75477-2 DOI 10.1007/978-0-387-75477-2 Springer New York Dordrecht Heidelberg London

Library of Congress Control Number: 2010928640

© Springer Science+Business Media, LLC 2010 All rights reserved. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer Science+Business Media, LLC, 233 Spring Street, New York, NY 10013, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

Printed on acid-free paper

Springer is part of Springer Science+Business Media (www.springer.com) Contents

Preface ix

0 A Brief Introduction 1 0.1 Notational Conventions 2 0.2 Typesetting 3 0.3 Basic Mathematical Functions 5 0.4 Using Functions 7 0.5 Replacements 12 0.6 Lists 13 0.7 Getting Information 15 0.8 Algebraic Manipulations 17 0.9 Customizing Mathematica 19 0.10 Comprehensive Data Sets in Mathematica 19

1 Plotting 23 1.1 Plot 24 1.2 An Arcsin Curiosity 26 1.3 Adaptive Plotting 28 1.4 Plotting Tables and Tabling Plots 31 1.5 Dealing with Discontinuities 34 1.6 ListPlot 37 1.7 ParametricPlot 42 1.8 Difficult Plots 48

2 Prime Numbers 53 2.1 Basic Number Theory Functions 54 2.2 Where the Primes Are 60 2.3 The Prime Number Race 66 2.4 Euclid and Fibonacci 70 2.5 Strong Pseudoprimes 73

3 Rolling Circles 77 3.1 Discovering the Cycloid 78 3.2 The Derivative of the Trochoid 82 3.3 Abe Lincoln’s Somersaults 84 3.4 The Cycloid’s Intimate Relationship with Gravity 90 3.5 Bicycles, Square Wheels, and Square-Hole Drills 98 vi Contents

4 Three-Dimensional Graphs 113 4.1 Using Two-Dimensional Tools 114 4.2 Plotting Surfaces 119 4.3 Mixed Partial Derivatives Need Not Be Equal 130 4.4 Failure of the Only-Critical-Point-in-Town Test 134 4.5 Raising Contours to New Heights 137 4.6 A New View of Pascal’ s Triangle 139

5 Dynamic Manipulations 141 5.1 Basic Manipulations 142 5.2 Control Variations 144 5.3 Locators 148 5.4 Fine Control 151 5.5 Three Case Studies 157

6 The Cantor Set, Real and Complex 169 6.1 The Real Cantor Set 170 6.2 The Cantor Function 173 6.3 Complex Cantor Sets 175

7 The Quadratic Map 179 7.1 Iterating Functions 180 7.2 The Four Flavors of Real Numbers 187 7.3 Attracting and Repelling Cycles 192 7.4 Measuring Instability: The Lyapunov Exponent 199 7.5 Bifurcations 202

8 The Recursive Turtle 209 8.1 The Literate Turtle 210 8.2 Space-Filling Curves 215 8.3 A Surprising Application 223 8.4 Trees, Mathematical and Botanical 233

9 Parametric Plotting of Surfaces 235 9.1 Introduction to ParametricPlot3D 236 9.2 A Classic Torus Dissection 243 9.3 The Villarceau Circles 250 9.4 Beautiful Surfaces 254 9.5 A Fractal Tetrahedron 261 Contents vii

10 Penrose Tiles 267 10.1 Nonperiodic Tilings 268 10.2 Penrose Tilings 270 10.3 Penrose Rhombs 274

11 Complex Dynamics: Julia Sets and the (by Mark McClure) 277

11.1 Complex Dynamics 278 11.2 Julia Sets and Inverse Iteration 284 11.3 Escape Time and the Mandelbrot Set 292

12 Solving Equations 301 12.1 Solve 302 12.2 Diophantine Equations 306 12.3 LinearSolve 310 12.4 NSolve 312 12.5 FindRoot 314 12.6 Finding All Roots in an Interval 315 12.7 FindRoots2D 318 12.8 Two Applications 322

13 Optimization 329 13.1 FindMinimum 330 13.2 Algebraic Optimization 333 13.3 and Its Cousins 334 13.4 Case Study: Interval Methods for a SIAM Challenge 346 13.5 Case Study: Shadowing Chaotic Maps 353 13.6 Case Study: Finding the Best Cubic 360

14 Differential Equations 363 14.1 Solving Differential Equations 364 14.2 Stylish Plots 367 14.3 Pitfalls of Numerical Computing 376 14.4 Basins of Attraction 382 14.5 Modeling 385

15 Computational Geometry 399 15.1 Basic Computational Geometry 400 15.2 The Art Gallery Theorem 404 15.3 A Very Strange Room 406 15.4 More Euclid 413 viii Contents

16 Check Digits and the Pentagon 423 16.1 The Group of the Pentagon 424 16.2 The Perfect Dihedral Method 427

17 Coloring Planar Maps 431 17.1 Introduction to Combinatorica 432 17.2 Planar Maps 437 17.3 ’s Formula 441 17.4 Kempe’ s Attempt 445 17.5 Kempe Resurrected 449 17.6 Map Coloring 458 17.7 A Great Circle Conjecture 468

18 New Directions for Π 473 18.1 The Classical Theory of Π 474 18.2 The Postmodern Theory of Π 480 18.3 A Most Depressing Proof 483 18.4 Variations on the Theme 488

19 The Banach-Tarski Paradox 491 19.1 A Paradoxical Free Product 492 19.2 A Hyperbolic Representation of the Group 495 19.3 The Geometrical Paradox 499

20 The Riemann Zeta Function 505 20.1 The Riemann Zeta Function 506 20.2 The Influence of the Zeros of Ζ on the Distribution of Primes 512 20.3 A Backwards Look at Riemann’ s x 519

21 Miscellany H L 523 21.1 An Educational 524 21.2 Making the Alternating Harmonic Series Disappear 525 21.3 Bulletproof Prime Numbers 528 21.4 Gaussian Moats 530 21.5 Frobenius Number by Graphs 536 21.6 Benford’ s Law of First Digits 542

References 557 Mathematica Index 566 Subject Index 572 Preface

This book is an example-based introduction to techniques, from elementary to advanced, of using Mathematica, a revolutionary tool for mathematical computation and exploration. By integrating the basic functions of mathematics with a powerful and easy-to-use , Mathematica allows us to carry out projects that would be extremely laborious in traditional programming environments. And the new developments that began with version 6 — allowing the user to dynami- cally manipulate output using sliders or other controls — add amazing power to the interface. Animations have always been part of Mathematica, but the new design allows the manipulation of any number of variables, an important enhancement. Mathematica in Action illustrates this power by using demonstrations and anima- tions, three-dimensional graphics, high-precision number theory computations, and sophisticated geometric and symbolic programming to attack a diverse collection of problems..

It is my hope that this book will serve a mathematical purpose as well, and I have interspersed several unusual or complicated examples among others that will be more familiar. Thus the reader may have to deal simultaneously with new mathe- matics and new Mathematica techniques. Rarely is more than undergraduate mathematics required, however.

An underlying theme of the book is that a computational way of looking at a mathematical problem or result yields many benefits. For example:

Ÿ Well-chosen computations can shed light on familiar relations and reveal new patterns.

Ÿ One is forced to think very precisely; gaps in understanding must be eliminated if a program is to work.

Ÿ Dozens (or hundreds or thousands) of cases can be examined, perhaps showing new patterns or phenomena.

Ÿ Methods of verifying the results must be worked out, again adding to one's overall understanding.

Ÿ Different proofs of the same theorem can be compared from the point of view of algorithmic efficiency. x Preface

Ÿ One can examine historically important ideas from the varied perspectives provided by Mathematica, often obtaining new insights.

The reader will find examples of these points throughout the book. Here are two specific cases: Chapter 17 contains a discussion of the four-color theorem that seeks to turn Kempe's false proof of 1879 into a viable for four-coloring planar graphs. And Chapter 15 shows how a certain published construction in computa- tional geometry — the construction of a three-dimensional room that contains a point invisible to guards placed at every vertex — must be changed if it is to be correct.

Another point worth mentioning is that a detailed knowledge of some of Mathemati- ca's internal workings can lead to novel solutions to programming problems. As an example, Chapter 12 shows how an understanding of the data computed by ContourPlot can lead to a simple and effective routine for finding all solutions in a rectangle to a pair of simultaneous transcendental equations.

The chapters are written so that browsing is possible, but there is certainly a progres- sion from elementary to advanced techniques, and the novice is encouraged to read the chapters in order. Even advanced users will benefit from a careful reading of the first few chapters. Chapter 5 is devoted to the Manipulate command, which is used throughout the book. The output cannot be appreciated very well on the printed page and the reader is encouraged to load the files in the electronic supple- ment and work through the demonstrations in an interactive way.

From its beginning, twenty years ago, the pleasure and power of using Mathematica arose from the two somewhat separate features: the kernel and the front end. The kernel does the underlying computations and the front end is the device through which the user communicates with the kernel. There has always been some commu- nication between the two, especially after version 3 with its many front end enhance- ments, but the two interfaces had the feel of separate entities. With version 6 the connection between the two became much stronger, as one can create Manipulate output that runs on its own. All the code in this book has been designed to work in both versions 6 and 7, with some small exceptions where use has been made of functions that are new in version 7. All the timings in the book are from a Macin- tosh laptop running a 2.16 gigaHertz Intel chip.

There have been important enhancements to the kernel as well. Some are minor, but together they contribute to making programming smoother, faster, and just more fun. A brief sampling: Table and Do commands now accept iterators that vary over a list, as in Do this, x, a, b, , where x takes on the values a, b, and c; RandomChoice s generates a random choice from the list s; Total s sums the @ 8 8 <

This book includes a CD with Mathematica files containing all the code that appears in the book, and much code that is not in the book. In some of the chapters one needs to load code from the disk to enable various sophisticated functions.

From a personal point view I have to emphasize the huge impact that Mathematica has had on my teaching and research. I have been using the program for almost 20 years and it has led to many new ideas at the forefront of research, in a wide variety of fields. Math and science are much more fun when one can visualize concrete examples of the objects being studied; Mathematica gives us the chance to see even very abstract constructions, and thus to understand them more deeply.

Acknowledgments I am grateful to the many people who have shared with me their expertise in mathematics and Mathematica. Of particular help with this edition have been several members of the Wolfram Research staff, including: Rob Knapp, Danny Lichtblau, Brett Champion, Ulises Cervantes, Lou D'Andria, Adam Strzebon- ski, Jeff Bryant, and Oleksandr Pavlyk. For helpful consultation on technical and style issues, I thank Joan Hutchinson, Ellen Gethner, Mark McClure, and Rob Pratt. Mark also contributed Chapter 11 on Julia sets. Michael Rogers shared his consider- able knowledge of the subtleties of Manipulate. I am grateful to Wayne Hayes (shadowing), Rachel Fewster (Benford's Law), and Matthias Weber (three-dimen- sional surfaces) for sharing their expertise on various points. And finally I thank Ed Packel, with whom I have for fifteen years taught a summer course about Mathemat- ica in the mountains of Colorado.

Stan Wagon, Macalester College, St. Paul, Minnesota December 2009