28.12.2014 Views

Mathematica Basics

Mathematica Basics

Mathematica Basics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Mathematica</strong> <strong>Basics</strong> <br />

<strong>Mathematica</strong> notebooks for this Intro are kept online at <br />

http://sitelic.colorado.edu/mathematica/graphs <br />

When you are new to <strong>Mathematica</strong>, look through the Getting Started items from the Welcome <br />

Screen; "Get a quick overview", "Learn with guided examples", "Short video tour". <br />

1. Resources for learning <strong>Mathematica</strong>: <br />

• Download software to your computer from http://sitelic.colorado.edu/mathematica. Be sure <br />

to use your CU (ucdenver.edu) email address for requesting an activation code <br />

• Watch screencasts online at http://www.wolfram.comlbroadcast/screencasts/. <br />

• Run <strong>Mathematica</strong> and experiment with it while viewing screencasts <br />

• Choose and explore "Documentation Center" from <strong>Mathematica</strong>'s Help menu. <br />

• Press the Fl key for quick access to documentation. This is very useful! <br />

Visit and explore the "Learning Center" at http://www.wolfram.com/learningcenter/. <br />

• Consulting help at CU; Bruce Fast, sitelic@colorado.edu, 303-­‐492-­‐8995. <br />

2. Syntax, palettes -­‐ how we tell <strong>Mathematica</strong> what to do <br />

Get acquainted with these syntax rules for <strong>Mathematica</strong>'s "standard <br />

form": <br />

• Capitalization -­‐ all predefined functions/commands are <br />

capitalized <br />

• Square brackets -­‐ f[x] -­‐ enclose function parameters <br />

• Curly brackets -­‐ {a,b,c} -­‐ are used for lists / vectors / matrices <br />

• Parentheses -­‐ (x+1)(x-3) -­‐ are only for algebraic grouping <br />

• Double-­‐square-­‐brackets -­‐ v[[3]] -­‐ are for indexing lists <br />

• Double-­‐equal-­‐signs (==) in equations, e.g., eq= x+y==5 <br />

• Semicolons separate/end expressions -­‐ a=3 ; b=4 ; c=a+b <br />

• Postfix notation is an alternative: Pi // N instead of N [Pi] <br />

• Asterisk (*) optional for multiplication: 4Pi==4*Pi, n<br />

Pi==n*Pi <br />

• Dot (.) for matrix multiplication -­‐ A.B or A.v or v.w <br />

All input can be typed from a standard keyboard, but palettes can make some input easier (and <br />

prettier!). Palettes can be selected and opened from the Palettes menu. <br />

3. Overview <br />

A <strong>Mathematica</strong> notebook can include not only mathematical input and output, but also formatted <br />

text, to make a structured document with headers and sections. Contents can be formatted (fonts, <br />

colors, alignment, etc.) as in any word processor. Choose a Stylesheet (from the Format menu) to set <br />

the overall formatting scheme for the notebook. <br />

To evaluate the expressions in one cell, put the cursor anywhere inside the cell and press Shift-­‐<br />

Return. To evaluate all the cells in a notebook, in order from top to bottom, choose "Evaluate <br />

Notebook" from the Kernel menu. <br />

Just press the Fl key for complete and convenient documentation for all the thousands of <br />

<strong>Mathematica</strong> commands/functions. This includes tutorials and examples for all topics and <br />

commands. You can copy the examples directly from the documentation and paste them (and edit) <br />

into your own notebook!


4. Lists <br />

Vectors, matrices, sets, arrays of data in an Excel spreadsheet -­‐-­‐ all are lists, and learning to deal with <br />

them appropriately makes everything else easier. <br />

• Create lists either manually (remember the curly brackets), or using Table or Range, or <br />

reading in data from files using Import. <br />

• Add or delete or select portions of a list using the double-­‐bracket notation, e.g., <br />

m[[5,2 ]], or using specialized commands like Append, Take, Part, Extract, Drop, First, <br />

Last, Rest <br />

• Do arithmetic with lists without using programming loops. For simple calculations just <br />

treat lists as single entities; for more complicated functions use Map. Also be aware of basic <br />

functions specifically defined for use with lists, e.g., Total, Tally, Accumulate, Min, Max, <br />

Differences, Norm, Mean, Median, Commonest. And there are many functions for <br />

matrices. <br />

• Rearrange or restructure lists using Flatten, Transpose, Partition, Reverse, Sort, Riffle <br />

... <br />

• Display lists graphically using ListPlot or (for 3D data) ListPlot3D, ListContourPlot <br />

• Display lists textually using TableForm or MatrixForm. <br />

5. Graphics vs. Graphs, two different kinds of objects <br />

All the commands for graphing functions or data include the word "Plot", e.g., Plot, ListPlot, Plot3D, <br />

ContourPlot, etc. For a complete list, evaluate *Plot*. The command GraphPlot will produce a <br />

Graphic (diagram) of a graph. On the other hand, the commands Graph and TreeGraph produce an <br />

actual Graph object, which one can work with in different ways than just pictures of graphs. <br />

Graph[{1->2, 2->3, 3->4, 2->4, 1->5}] produces a Graph object. By itself it gets <br />

displayed in a standard node/edge diagram, but it can also be used in commands like VertexCount, <br />

EdgeList, and AdjacencyMatrix. <br />

6. Functional Programming <br />

For those with experience with procedural programming languages like Fortran and C, it is worth <br />

"unlearning" the reliance on loops. There is almost always a simpler and more elegant way to <br />

accomplish anything in <strong>Mathematica</strong>, by learning a functional programming approach. <br />

• Sums and products have their own functions, Sum and Product. Or, use the Palettes menu <br />

to use the traditional mathematical symbols ∑ and Π . No looping involved! <br />

• Defining your own functions is easy and practical using <strong>Mathematica</strong>'s notation, and makes <br />

subsequent programming simpler. Usually it is easiest to use pattern-­‐matching notation, <br />

such as f[x_]:=Sin[x^2]+3, but the same can be accomplished using "pure function" <br />

notationlike f=Function[x,Sin[x^2]+3], or even shorter, f=(Sin[#A2]+3)& <br />

• Applying functions to lists can be done elegantly using Map (/@) and Apply (@@) <br />

7. Saving results <br />

<strong>Mathematica</strong> notebooks themselves can be read by anyone using the free <strong>Mathematica</strong> Player (free <br />

download from www.wolfram.com), or any evaluated expressions can be saved to files of any <br />

appropriate format (e.g., XLS, CSV, PDF, PNG, JPEG) using Export. (See $ExportFormats !) <br />

Bruce Fast, January 2012

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!