Essential Mathematica for Students of Science

Tutorial Approach to Mastery of Mathematica

James J. Kelly
Department of Physics
University of Maryland
College Park, MD 20742

Click here to jump to the notebook listings.

Preface

Mathematica is a fully integrated system for technical computing.  Among the capabilities it offers are:

an interactive front end with notebook interface

numerical calculations with (practically) unlimited precision

symbolic manipulation

special functions

graphics

typesetting

extensibility

Within a Mathematica notebook you can develop solutions to complex problems that combine symbolic derivations, numerical calculations, and graphical displays in an interactive document.  The tools provided by Mathematica will help you focus more on conceptual development and visualization than on details of algebra or procedural programming.  Your solution can be documented and presented in a very appealing format using the typesetting tools provided by the front end interface.

Once you have acquired mastery of the basic tools, you will probably find yourself using Mathematica for an ever increasing share of the work you used to perform by hand or with the aid of several separate software tools.  For example, I once had a very bright student who performed some very complex derivations by hand, using thousands of pages of scrap paper and many drafts of his final report, over a period of about 2 years.  When several years later I had to verify some of his results and extend his work to other applications, it would have taken me as long or longer to verify that derivation by hand.  Fortunately, I had learned Mathematica in the meantime and with it was able to reproduce and extend that derivation in a period of about a week.  Instead of consuming reams of scrap paper and repeating many steps whenever errors were found, I developed a set of live documents in which changes and corrections propagate with computer assistance.  The computer requires human guidance, but executes mathematical and numerical operations much more efficiently and without the trivial but critical errors to which humans are prone.  These living notebooks evolve as new results are developed and can be transmitted to colleagues without further transcription.  
Mathematica has made many of the tedious derivations and calculations required for my research much more efficient, and I am sure that it will do the same for you, whether you use it for research, course work, or business.  Although for many numerical computations it still cannot compete in sheer speed with programs using compiled languages, I use it for development and testing of symbolic derivations and numerical methods before writing such programs.  Furthermore, as Mathematica continues to improve in speed for both numerical and symbolic calculation, I expect that it (and programs like it) will replace traditional programming languages for all but the most computationally intensive tasks.

The purpose of this course is to provide mastery of the Mathematica tools and techniques most relevant to scientists and engineers.  No prior familiarity with Mathematica is needed. The course consists of a collection of tutorials that present concepts and practical techniques  in a systematic fashion.  Many people, including many faculty, attempt to learn Mathematica in a very haphazard fashion, learning just enough of the basic syntax to evaluate a few integrals, perform simple numerical calculations, and make some basic graphs, without ever understanding how the system works.  Unfortunately, this method often leads to frustration because it provides no basis for interpreting errors or unexpected output.  It also tends to produce rather inefficient and clumsy code.  Effective use of Mathematica requires training the mind to formulate problems in new ways, not just to apply new syntax to old methods.  If you take the time to work through the tutorials and exercises, you will understand the system well enough to use it both efficiently and creatively.  You will be amazed at how much you can accomplish with so little code, especially if you are a veteran programmer in traditional languages.

Course materials

The materials for this course are organized into two types of notebooks.  Language notebooks present the basic concepts and techniques of the Mathematica language using relatively simple examples.  Exercises within the body of these notebooks should be done in class, while some of the more challenging exercises at the end of the notebooks will be assigned for homework.  Application notebooks present solutions to more interesting problems at considerably greater depth.  These notebooks are intended to illustrate the use of Mathematica for research in the form of polished documents which introduce a problem, outline the strategy for its solution, implement that solution, discuss interesting examples, and draw conclusions.  Many of these notebooks include exercises also.

The language notebooks are designed to present Mathematica in a nearly linear fashion, building an edifice one block at a time, whereas the application notebooks use whatever tools are needed to solve a particular problem or class of problems.  Therefore, applications may use features of Mathematica before they are covered in the course, but links to related course material or to on-line documentation are provided where appropriate.  After all, in research one does not choose problems according to the tools at hand, but rather acquires the tools needed for a problem as those needs are realized!  The student, and the instructor, are encouraged to jump back and forth between application and language notebooks.  The style of reading from page 1 to the end is passé.

Hyperlinks to course materials

You will probably find it helpful to open this notebook at the beginning of a session and to use the hyperlinks listed below to navigate among the available notebooks.  Unfortunately, these hyperlinks may not work properly if you open the notebooks directly instead of accessing them through the links in this table of contents.  I find it helpful to create a shortcut to this notebook on the desktop.

Related material may be found through the course home page EssentialMathematica.

Contents

Primary language concepts and techniques

Getting started : introduces the front end, help sources, input conventions, and some important functions.

Functions : techniques for constructing Mathematica functions.  Includes implicit and explicit functions, pure and skeletal functions, recursive techniques, scoping commands, loop commands, conditionals, etc.

Expressions  : presents the central concepts of the Mathematica language and its internal representation of expressions.

Lists : manipulation of the basic data structure and efficient programming.

Plotting : the basic visualization tools are presented.

Algebra : simplification of algebraic expressions, solution of equations, eigensystems.

Calculus : differentiation, integration, Taylor series.

Differential equations : symbolic and numerical solutions to ordinary differential equations.

Additional language topics and tools

Dimensional Analysis : symbolic and numerical computations involving dimensioned quantities and their units.

Files : storing and retrieving information from files.

Complex : subtleties of algebra with complex variables.

Recurrence relations : recursive functions, dynamic programming, and stability of recurrence relations.

InPolygonQ : develops a query for determining whether a point is within a closed polygon.

Packaging : the strategy and mechanics of developing packages is illustrated with a specific example based upon earlier exercises about fractals.

Applications

Algebra

cicada : presents a model describing the synchronized emergence of some insect populations

collisions : studies kinematics of nonrelativistic binary collisions, including inelasticity and mass transfer, and makes useful vector diagrams.

Paschen : investigates the effects of magnetic interactions between the electron and proton in hydrogen and their interactions with an external magnetic field, known as the Zeeman effect for weak fields or Paschen-Back effect for strong fields.

Markov : presents a program for computing the equilibrium state of Markov processes in which the evolution of a system is governed by a transition probability matrix.

Fractals and deterministic chaos

logistic map : a nonlinear discrete model for the population of species with nonoverlapping generations that illustrates the phenomena of stable cycles, chaos, and order within chaos.  Entropy and Lyapunov exponents are introduced.

cobweb : functional programming is used to produce cobwebbing diagrams for iterated function systems, such as the logistic map.

Koch : fractal properties of Koch curves

Henon : the two-dimensional Hénon map produces a strange attractor.

Julia : escape-time algorithm for Julia and Mandelbrot sets

JuliaIIM : inverse iteration methods for Julia sets

frequency analysis : discrete Fourier analysis of time series.  General properties are discussed and applications are made to the van der Pol oscillator and to the logistic map.

Differential equations

home run : uses numerical integration of a differential equation to investigate projectile motion with air resistance

pendulum : phase diagrams for driven pendulum with nonlinear restoring force and linear damping.

Duffing : phase diagrams for driven anharmonic oscillator with linear damping.  The Poincaré map is used to study patterns within apparently chaotic behavior.

Volterra : uses predator-prey models to introduce linear stability analysis.  We begin with the simple Lotka-Volterra model, a conservative system with periodic orbits, then study a more realistic model with limit-cycle oscillations.

van der Pol : limit cycle oscillation in nonlinear differential equation using linear stability analysis.

FN model for BZ reaction : Belousov-Zhabotinskii reaction, the prototypical chemical oscillator

Data analysis

MathLite : enough Mathematica for data analysis in an introductory physics lab.  This is an abbreviated version of getting started, functions, and files.

DataAnalysis : introduces the theory of linear and nonlinear least-squares fitting and develops several useful programs.  These are also compared to the built-in function NonlinearRegress.

Curfit : a package that provides improved versions of the Linfit and Curfit programs developed in DataAnalysis.

Physics problems

PDE relaxation : relaxation method is used to compute electrostatic potential near systems of electrodes in two-dimensions.  Major example: fringe fields, surface charge density, and capacitance for parallel-plate capacitor.

bswf : shooting methods are used to find bound-state energies and wave functions for Schrödinger equation.  Major example: symmetric double-well potential.  Exercises extend the method to central potentials and study hydrogen atom, nuclear shell model, mexican hat potential, etc.

Note to students

The notebooks which comprise this book are intended to teach Mathematica by example and by practice.  Ideally the course should be studied in a tutorial format in which an instructor guides students through a progression of exercises which develop skill and understanding through practice.  However, if you are studying Mathematica independently, please work the exercises — do not just read the text and skip the exercises.  Do not be discouraged if you have had negative experiences in the past with an instructor in one of your courses who expected you to learn Mathematica after just one or two lectures.  It is only through practice that one can train the mind to approach problem solving in new ways and this takes time and patience.  Mathematica provides many tools which will help you to solve and visualize problems in new ways, and these tools are well worth the effort needed to master them.  The notebooks in this book will guide you toward mastery of the most important tools in Mathematica slowly, with exercises you can work at your own pace.  By the time you complete the two core notebooks, getting started and  functions, you will have acquired a solid foundation that will allow you to easily learn and apply the particular tools most appropriate to your needs.  Take the time needed to understand the material presented in these notebooks well.

Note to instructors

This book arose from a course that I teach at the University of Maryland.  Although I am a professor of physics, the course is intended for advanced undergraduate and beginning graduate students in any field of science, engineering, or mathematics.  Thus, although many of the examples are naturally from physics, many examples are also drawn from mathematics (iterated functions and fractals), biology (population dynamics), and chemistry (chemical oscillators).  The prerequisites are calculus and introductory physics.

Too often students receive their first introduction to Mathematica in just one or two class periods of a course which uses Mathematica as one of the tools employed in the presentation of its primary subject.  The unfortunate student is then expected to master new concepts, syntax, and programming techniques in a couple of hours.  This impossible burden often alienates the student so completely that he or she abandons Mathematica once that course is finished.  If any of your students have suffered through that experience, please reassure them that the tools provided by Mathematica are worth a considerable investment of effort and that your course will offer sufficient time and practice to acquire mastery of them.  Mastery of Mathematica is like mastery of a second language — it requires time, patience, practice, immersion, repetition, and more patience.  Any second language becomes much easier when a person begins to think in that language without having to translate every thought.  Languages cannot be learned by passively listening to lectures, but require active immersion and practice.

My course was taught in a teaching theater using a tutorial format.  The teaching theater provides each student with a computer and the instructor with equipment to project the instructor's monitor, transmit the instructor's screen to each student monitor, and to view and/or project any monitor in the room.  Networking software permits communication or collaboration between students and the instructor.  Assignments were distributed and collected electronically.  All materials for the course were presented in the form of Mathematica notebooks.

Most classes began with a brief introduction to the lesson of the day, with the instructor's monitor transmitted to the students.  I would work through and discuss the appropriate section of a notebook, and then the students would work some of the exercises in that notebook as I circulated through the room offering help and suggestions where needed.  If several students were experiencing similar problems, I might give hints to the entire class or might return to my computer and show how a particular problem might be solved.  Occasionally, I would project the solution produced by a student and comment on it or allow the student to present it.  After students have completed the exercises for one topic, a new topic would be introduced by the instructor and then the students would perform the exercises for that section.

I found that it was very important to allow students to work through the materials for themselves as much as possible.  Students are very anxious to place their hands on the keyboard and to dive right in; students tend to have relatively little patience for dry lectures.  Most students learn relatively little from listening, but learn a lot from working the embedded exercises and discussing strategies for the solution of specific problems.  The tutorial format requires more time than lecturing, but is much more effective.  Do not try to go too fast!  It is more important for students to begin to think in Mathematica style than to hear or read the syntax for thousands of functions.

This book contains much more material than can be covered in a one semester course.  In fact, I anticipate dividing this material into two volumes in an expanded next edition.  The first volume will concentrate upon the Mathematica language and is intended for the general student of science, while the second will present mathematical methods intended primarily for students of physics.

The essential core of an introductory course in Mathematica is contained within the two notebooks getting started and functions, each of which requires approximately three weeks to cover.  The student who has mastered these lessons is already quite proficient in the use of Mathematica and should be able to identify and learn the additional tools needed for his/her purposes with relatively little help from the instructor.  Therefore, the selection of topics for the remainder of a standard one semester course permits flexibility to meet the needs of the class.  A sample curriculum is given below with estimated times.

The notebook on dimensional analysis, or parts of it, can be assigned as homework soon after completion of getting started.  Within functions is a problem on Julia sets that is easy and fun; I assigned it between two classes and then presented julia.nb as an example of modular programming after the students turned in their solutions.  Depending upon the pace of the class, plotting can either be covered in class or parts of it assigned as homework; I used the diffraction problem as a homework assignment that reinforced techniques of modular programming as well as covering plotting features.  I chose to omit lists to permit more time for differential equations.  I also used frequency analysis to shed additional insight upon limit cycles (van der Pol oscillator) and the logistic map.  A miniproject based upon Koch was also a lot of fun.

Version dependencies

These notebooks employ Mathematica Version 5.2, but can be viewed using Version 4 with only a few minor formatting problems; simply ignore warning messages if you open a notebook with an earlier version.  Although Version 5 provides some new features, most of the code presented here will work properly under Version 4 also.  However, there are some version dependencies:

Prior to Version 5.1, master packages are accessed with statements like Needs["Graphics`Master`'"], but after Version 5.0 one uses just Needs["Graphics`"] for the same purpose.  Similar abbreviations are used for other families of packages.

Occasionally simplification functions produce version-dependent results and it may be necessary to rearrange those functions to obtain the desired output.

The ordering of roots also varies by version, so it may be necessary to adjust references to specific solutions from a list.

Changes in FindRoot and FindMinimum around Version 5.1 affect equations based upon user-written functions.  It is now necessary to express arguments in explicitly numerical form using patterns like x_?NumericQ.

Symbolic integration is probably the most difficult task performed by Mathematica.  Unfortunately, some integrals that are performed correctly in one version become incorrect in a later version, and then may be corrected again in the next version.  I maintain a small list of problematic integrals, but the user must be aware that it is important to check symbolic integration whenever possible — no software is perfect!

Style sheet

To create notebooks with the style sheet designed for this course, copy the style-sheet notebook TutorialJJK to your working directory.  Then select this style using the Format ↦ StyleSheet menu.  Alternatively, the standard Classroom style sheet, upon which this was based, gives a similar appearance.  If you would like to modify this style sheet or to use it for notebooks in many directories, you may prefer to copy it into the Configuration\FrontEnd\StyleSheets subdirectory of the Mathematica folder.  TutorialJJK should then appear in the list of standard style sheets.

Selected bibliography

There are many good books on Mathematica and on mathematical and computational methods.  A selection of useful references is listed below with brief descriptions.

Mathematica references

S. Wolfram, The Mathematica Book, (Wolfram Media and Cambridge University Press, Cambridge, 1996)

The bible according to Wolfram is the definitive reference for the language and philosophy of Mathematica.  Although designed primarily as a reference book rather than a textbook, it includes much practical advice and many instructive examples.  The entire contents is available on-line with standard installations of Mathematica 3.0.

W.M. MacDonald, Mathematica for Scientists and Engineers, (University of Maryland, 1996, unpublished)

Prof. MacDonald taught an earlier version of this course at the University of Maryland and prepared an extensive collection of notebooks and other course materials.  I have drawn heavily upon these materials  during the preparation of my course and this book.

P.T. Tam, A Physicist's Guide to Mathematica, (Academic Press, San Diego, 1997)

This is a Mathematica textbook designed primarily for physicists and advanced students of physics.  The first part contains a comprehensive presentation of the language, while the second presents selected applications to a wide variety of problems in physics.  The book comes with a CD, but the notebooks do not take full advantage of the front end.  The applications are often insightful, but I would not accept these notebooks from my students.  Nevertheless, I still find the detailed presentation of syntax and built-in functions to be well organized and quite useful.

R. Gass, Mathematica for Scientists and Engineers, (Prentice Hall, Upper Saddle River, 1998)

Here the features of the language are introduced as interesting and challenging problems are solved.  The book concentrates upon the use of Mathematica without attempting to present the language as comprehensively as Tam.  This book assumes a rather high level of mathematical sophistication that seems to be most appropriate for graduate students of physics or mathematics.  Despite the title, I doubt that it would appeal to engineering students or to students of life sciences.  A disk is included and makes better use of the notebook interface than Tam.

J.W. Gray, Mastering Mathematica, (Academic Press, San Diego, 1998).

Presents a comprehensive course in programming using Mathematica that is designed primarily for graduate students of mathematics and computer science.  The complete book is on the accompanying CD and can be linked into the Help browser.  Topics such as objected-oriented programming, pattern analysis and graph theory, and differential mapping are covered in considerable depth.

T.B. Bahder, Mathematica for Scientists and Engineers, (Addison-Wesley, Reading MA, 1995)

This is an early book on Mathematica Version 2 that remains quite useful despite recent advances in Mathematica.  It features a truly comprehensive presentation of the language elements and the internal workings of the patterns, rules, and expressions.  This is the best book available for understanding how Mathematica works, not just how to use it.

R.L. Zimmerman and F.I. Olness, Mathematica for Physicists (Addison-Wesley, Reading MA, second edition 2002)

After a brief introduction to Mathematica, the undergraduate physics curriculum is reviewed using Mathematica to solve a wide variety of problems with greater insight and depth than is possible with chalkboard techniques.  The Mathematica language and techniques are learned along the way, including a full range of both symbolic and numerical methods. Many interesting exercises are given.

Mathematical and computational references

J.D. Murray, Mathematical Biology, (Springer-Verlag, Berlin, 1989)

This textbook on mathematical biology covers a wide variety of topics in population biology, linear stability analysis, biological and chemical oscillators, diffusion, pattern formation, etc.  I used this reference to develop several Mathematica applications.

H.-O. Peitgen and P.H. Richter, The Beauty of Fractals, (Springer-Verlag, Berlin, 1986)

This book presents a series of articles on fractals that arise from iterated function systems and is a useful reference for the material presented in this course on the logistic map, Mandelbrot and Julia sets, and Koch curves.  It is also filled with lovely pictures.

W.H. Press, B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, Numerical Recipes — The Art of Scientific Computing, (Cambridge University Press, Cambridge, 1989).

This compendium of numerical methods and algorithms is a valuable reference for anyone who does scientific computation, whether using the compiled languages considered by its authors or an interpreted language like Mathematica.

B. Carnahan, H.A. Luther, and J.O. Wilkes, Applied Numerical Methods, (Wiley, New York, 1969).

An insightful survey of numerical methods complete with Fortran programs, test cases, and examples.  Each chapter includes many instructive exercises.  Although the programming style is a little dated, the discussions and examples are among the best available.

S. H. Strogatz, Nonlinear Dynamics and Chaos, (Perseus Books,  Reading MA, 1994).

Covers many topics in nonlinear science with applications to physics and life sciences.  This is a good place to look to more background material and greater depth for some of the examples considered in this course.

A. Gray, M. Mezzino, and M.A. Pinsky, Introduction to Ordinary Differential Equations with Mathematica, (Springer-Verlag, New York, 1997).

This book provides a comprehensive introduction to both symbolic and numerical solution of ordinary differential equations.  Although also suitable for a traditional mathematics course, it uses the symbolic, numerical, and graphical features of Mathematica quite effectively to enhance both conceptual understanding and practical skills.  The collection of methods implemented in the package ode is worth the price of the book by itself.

Acknowlegments

I was first introduced to Mathematica in 1994 by William M. MacDonald, Emeritus Professor of Physics at the University of Maryland, who taught a workshop for faculty.  Bill also developed the first version of the course that I now teach after his retirement.  Although I redesigned the course around a tutorial approach, I have benefitted considerably from the material that Bill developed for his version of the course and from his insight and guidance during my development of the present tutorial approach.  I have retained much of his organization of the language concepts, including his classification of functions into implicit, explicit, and skeletal forms.  The notebooks files and lists remain very similar to his versions.

I would also like to thank all of the students who have patiently road-tested these materials.