ebook img

Mathematica as a Tool: An introduction with practical examples PDF

437 Pages·1994·8.08 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Mathematica as a Tool: An introduction with practical examples

Stephan Kaufmann Institute of Mechanics ETH Zentrum HGF38.4 8092 Zurich Switzerland e-mail: [email protected] Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Birkhauser was aware of a trademark claim, the designations have been printed in initial caps or all caps. Birkhiiuser and Stephan Kaufmann have used their best efforts in preparing this book. These efforts include the development and testing of the code which appears in this book. We make no warranty of any kind, expressed or implied, with regard to the code or the documentation contained in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of information appearing in this book. A CIP catalogue record for this book is available from the Library of Congress, Washington D.C., USA Deutsche Bibliothek Cataloguing-in-Publication Data Kaufmann, Stephan: Mathematica as a tool: an introduction with practical examples / Stephan Kaufmann. - Basel; Boston; Berlin: Birkhauser, 1994 Dt. Ausg. u.d.T.: Kaufmann, Stephan: Mathematica als Werkzeug ISBN 3-7643-5031-8 (Basel...) ISBN 0-8176-5031-8 (Boston) This work is subject to copyright. All rights are reserved, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, re-use of illustrations, recitation, broadcasting, reproduction on microfilms or in other ways, and storage in data banks. For any kind of use permission of the copyright owner must be obtained. © 1994 Birkhiiuser Verlag Basel, P.O. Box 133, CH-4010 Basel Camera-ready copy prepared by the author Printed on acid free paper Cover design: Markus Etterich, Basel ISBN 3-7643-5031-8 ISBN 0-8176-5031-8 987654321 • Stephan Kaufmann Mathematica as a tool An introduction with practical examples Birkhauser Verlag Basel' Boston' Berlin • Preface More than ten years ago, I wanted to carry out coordinate transformations for Hamiltonian systems, in order to discuss the stability of certain equilibrium posi tions. Basically, the calculations only involved rational expressions, but they turned out to be extremely complicated, because the third and fourth order terms had to be included. After several months of filling whole blocks of paper with for mulas, I was close to resignation. But, by a lucky incident, I met a colleague who showed me the computer algebra package Reduce. It still required a lot of patience and tricks, but Reduce finally did produce the desired results. After this experience, I wondered, why only a few engineers and scientists were aware of the strengths of such computer algebra programs. The mathematical treatment of scientific problems often leads to calculations which can only be solved "by hand" with a considerable investment of time, while a suitable com puter algebra program produces the solution within a couple of seconds or min utes. Even if a closed symbolic solution is not possible, such programs can often simplify a problem, before the cruder tool of numerical simulations is applied. Subsequently, I turned to other work, and put the computer algebra aside. But in 1988, the new program Mathematica instantly impressed me by its combination of computer algebra, graphics, animations, an elegant programming language, and numerical tools. I decided to offer a lecture for the students of mechanical and civil engineering, mathematics, and physics at the Swiss Federal Institute of Tech nology (ETH Zurich), where I showed them how to use Mathematica as a tool for their work. In the course of three years, the corresponding lecture notes became quite bulky. They were inspired by the book by Stephan Wolfram [WoI88], and this relationship still shines through. When Thomas Hintermann from Birkhauser Verlag asked me to write a Ger man book based on my lecture notes, I decided to take him up on it. During my work with Mathematica users, I had noticed that few of them really had studied the book by Stephen Wolfram. This has a positive and a negative side: On the one hand, it shows that Mathematica is designed in a very intuitive way. But on the other hand, the full strengths of the tool can only be used if one knows them. Many tasks can be simplified by small Mathematica programs; others require a detailed knowledge. This book presents a compact introduction to Mathematica. Some nontrivial examples are used as a motivation to study several aspects of the program. Read ers who are not interested in the mathematical or physical aspects of these exam- vi pIes, can simply skip these, and concentrate on the treatment of the equations with Mathematica. There is no intention to present a complete list of all the features and applications of Mathematica. I do not see much need for such a book because the documentation which comes with the program is well written, complete, and always up to date-and the field of applications is infinite. My intention is rather to bring you to the point where you know enough to help yourself and to solve your own problems. This English translation of my book contains several improvements to the first German edition. The major changes concern the Sections 1.8.2, 3.2.3, and 3.3.1, while minor adjustments and improvements have been made at many places. o Acknowledgments Many people have helped to bring this to a good end. They all deserve my grateful acknowledgment. • First comes my wife Brigitta. Without her patience and understanding, I never could have tackled such a time-consuming task. • Paul Wellin corrected my English and added many valuable suggestions. • Leszek Sczaniecki from Wolfram Research, Inc. provided some useful hints. • Mahir Sayir always supported my work very generously. • The discussions with my colleagues at the Institute of Mechanics (ETH), espe cially Stefan Messmer, have added many fruitful thoughts. • Valuable criticism and constructive suggestions have come from the very patient students of my lectures. • All people I met at Birkhauser Verlag are highly motivated. My special thanks go to Thomas Hintermann for the general and Justin Messmer for the technical support. • And finally, I would like to thank the Napier families in Menard, Keller, and Dallas (Texas). They were my hosts when I was an AFS foreign exchange stu dent and gave my a unique chance to learn at least some English. Stephan Kaufmann Zurich, Switzerland July 1994 • Table of Contents Introduction ....................................................................................... 1 Part 1. Basics 1.1 Getting Started ........................................................................... 9 1.1.1 Starting and Quitting .......................................................... 9 1.1.2 Interrupting and Quitting ...................... .............................. 12 1.2 Help! ........................................................................................... 14 1.3 Numerical Calculations...... ...................... ...................... ............. 17 1.3.1 Simple Examples .......... .......................... .................. ......... 17 1.3.2 External Packages: Units ................................................... 25 1.3.3 Large Numbers: The RSA Public-Key Cryptosystem ......... 32 1.4 Symbolic Calculations ........... ....................... ...................... ........ 44 1 .4.1. Polynomials .............. ...................... ........ ................ ............ 44 1.4.2 Equations ............... ...................... ...................... ................ 54 1 .4.3 Calculus ..... ............... .......................... ................... ............ 66 1.5 Plots: Different Pendulums......................................................... 81 1.5.1 Two-Dimensional Plots ...................................................... 81 1.5.2 Example: The Triple Pendulum .......................................... 89 1.5.3 Three-Dimensional Graphics and Animation: A Rotating Double Pendulum ............................ ................. 106 1.5.4 Parametric Plots: A Rotating Double Pendulum ................. 121 1.5.5 Plotting Data: The Random Generator .............. ................. 130 1.6 Lists ............................................................................................ 138 1 .6.1 Creating and Formatting Lists .......................... .................. 138 1.6.2 Calculations with Lists, Pure Functions ............................. 143 1.6.3 List Manipulation: The Cross Product ................................ 148 1.6.4 Vectors, Matrices, Tensors: The Jacobian ......................... 158 1 .6.5 Eigenvectors and Eigenvalues: An Oscillator ............ ........ 164 1.7 Graphics Programming .............................................................. 172 1.7.1 The Structure of Graphics .................................................. 172 1.7.2 Example: Animation of the Triple Pendulum ...................... 178 1.7.3 Example: The Oscillator ..................................................... 184 1.8 More Selected Tools...... ................................ .................. ........... 190 viii 1.8.1 Graphics and Sound .......................................................... 190 1.8.2 Complex Numbers ................ .................... ..... .................... 195 1.8.3 Sums, Products, Series ............................... ...................... 198 1.8.4 Data Analysis, Interpolation ............................................... 201 1.8.5 Transformations ................................................................. 207 1.8.6 Mathematical Functions ..................................................... 213 1.8.7 Polynomials ........................................................................ 214 1.8.8 Linear Algebra, Linear Programming ................................. 216 1.8.9 File Manipulation ................................................................ 217 1.8.10 Custom Configuration ......................................................... 219 1.8.11 Resources .......................................................................... 222 Part 2. Structure 2.1 Expressions ................................................................................ 229 2.1.1 The Structure of Expressions ............................................. 229 2.1.2 Working with Parts of Expressions ..................................... 231 2.2 Patterns ...................................................................................... 233 2.2.1 Simple Patterns .................................................................. 233 2.2.2 Restricted Patterns ............................................................ 237 2.2.3 More Complicated Patterns ............................................... 242 2.2.4 Example: A Simple Integrator ............................................. 248 2.3 Transformation Rules and Definitions ........................................ 251 2.3.1 Transformation Rules ......................................................... 251 2.3.2 Definitions .......................................................................... 255 2.3.3 Attributes ............................................................................ 263 2.4 Evaluation and Tools for Programming ...................................... 268 2.4.1 Standard Evaluation ........................................................... 268 2.4.2 Special Evaluation ............................................................. 271 2.4.3 Conditionals, Loops, and Control Structures ..................... 277 2.5 Modularity ................................................................................... 290 2.5.1 Local Variables and Constants ........................................... 290 2.5.2 Contexts and Packages ..................................................... 297 2.6 Strings, Text, Messages ............................................................. 308 2.6.1 Printing Text ....................................................................... 308 2.6.2 Arranging Text .................................................................... 309 2.6.3 Built-in Formats .................................................................. 310 2.6.4 Formats for Numbers ......................................................... 313 2.6.5 Subscripts and Superscripts .............................................. 315 ix 2.6.6 Custom Formats ................................................................ 318 2.6.7 Strings ................................................................................ 321 2.6.8 Messages ........................................................................... 323 Part 3. Programming 3.1 Programming Methodologies ..................................................... 331 3.1.1 Procedural Programming ................................................... 331 3.1.2 Recursive Programming .................................................... 332 3.1.3 Functional Programming .................................................... 335 3.1.4 Rule-Based Programming .................................................. 336 3.1.5 Programming with Constraint-Propagation ........................ 337 3.1.6 Data Types, Object-Oriented Programming ....................... 340 3.2 Developing Programs ................................................................. 342 3.2.1 Debugging .......................................................................... 342 3.2.2 Example: NonNegati veQ \ ............................................... 348 3.2.3 Example: AbsArg \ ............................................................ 353 3.2.4 Example: RSA \ .................................................................. 355 3.2.5 Example: Perturbation \ ............................................... 359 3.3 Numerics .................................................................................... 368 3.3.1 Compiling Expressions: Fractals ........................................ 368 3.3.2 Precision and Accuracy ...................................................... 376 3.3.3 Controlling Numerical Built-in Functions ............................ 378 3.4 Long Calculations: RSA ............................................................. 384 3.4.1 Primes for RSA .................................................................. 384 3.4.2 Saving Results in a File ..................................................... 386 3.4.3 External Programs, Calculations in the Background .......... 389 References ....................................................................................... 395 Index ................................................................................................. 399 I Color Plate 1: The potential of a rotating double pendulum (page 112) Color Plate 2: The potential of a rotating double pendulum in configuration space (page 124) II f 1 r f f 1 7 ? ? ) s c Color Plate 3: Some positions of the triple pendulum (page 183)

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.