david c. johnston - physics 304 text supplement - basics of mathematica [2013] [p22]

Upload: galeotto-marzio

Post on 02-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 David C. Johnston - Physics 304 Text Supplement - Basics of Mathematica [2013] [p22]

    1/22

    Physics 304 Text Supplement: Basics of Mathematica

    David C. JohnstonDepartment of Physics and Astronomy

    Iowa State University, Ames, Iowa [email protected] 2013

    This tutorial is known to be compatible with Mathematicaversions 7 and 8, but not

    version 6, and is not comprehensive. The instructions are sufficient for many pur-

    poses and are illustrated by examples. The examples can be used as templates for

    other uses by copy and paste-ing code or complete cells into other Mathematica

    notebooks.

    Free video tutorials for learning the basics of using Mathematica are available at:http://www.wolfram.com/support/learn/students.html

    Getting Started

    To open a new notebook (.nb), choose New Notebook from the File menu. It will be

    a blank page. Click anywhere on the page and type your formula. A "cell" will form

    that has a bracket "]" on the right side. On the left side will be an input cell number

    "In[n]". To execute the contents of a cell, put the cursor anywhere in the cell, hold

    down the shift key and press the return key. An output cell will be created containingthe output of the input cell, with the notation "Out[n]" on the left side, where "n" is

    the same as in the input cell number if the input cell has one calculation in it. The n

    values are incremented for each new input cell. The input and output numbers disap-

    pear when you close a notebook.

    Variables, Comments and Functions in Mathematica

    Variable names should start with a lower case English or Greek letter followed by any

    sequence of numbers and/or upper or lower-case letters. A variable name can also bea single Greek or lower case English letter. Some upper case single letters and

    sequences of letters starting with a capital English letter are reserved by Mathematica

    for special uses, such as E, D, N, Cos and Sinh.

  • 8/10/2019 David C. Johnston - Physics 304 Text Supplement - Basics of Mathematica [2013] [p22]

    2/22

    ClearAll@xD H* Clears the definition of the variable x*Lx

    x

    From the output, x has no number assigned to it yet.

    Assign a numerical value to the variable x:

    x= 3

    3

    To suppress the output of a calculation, put a semicolon after thecalculation. The calculation is still done and only the printed output issuppressed.

    x= 4;

    Check to see what the value of x is :

    x

    4

    To put a comment into a cell use the format (* comment *). A commentis not executed.

    x= 5

    H* Defining the value of a variable overwrites theprevious value if it was already previously assigned a value. *L

    5

    Defining a function of previously defined constants

    A function name can be any combinations of letters and numbers starting with a

    lower-case letter. The arguments of a function are enclosed in square brackets and

    separated by commas.

    Example: first define the values of the independent variables and then define the func-

    tion of those.

    x= 3; y= 4;H* Hit the return key to go to the next line. *Lfcn@x, yD= x ^ 2 + y ^ 225

    2 Basics of Mathematica8.0.nb

  • 8/10/2019 David C. Johnston - Physics 304 Text Supplement - Basics of Mathematica [2013] [p22]

    3/22

    To simplify inputting complicated functions and to make formulas looklike they should, use theBasic Math Assistant Pallettefrom thePalettes Menu

    fcn@x, yD= x2 + y225

    x= 3; y= 4;

    fcn2@x, yD= x2+ y2

    x

    y+

    -xy

    25

    3

    4+

    1

    34

    Decimal Values and Recalling Output Values

    To get a decimal value from an exact value, use the command N[x,n], where n is the

    number of digits. Recall the output of the previous output cell by using %:

    N@ED H* Here, E is a reserved number E = 2.718... .The default value of n is 6 if not specified. *L

    2.71828

    N@E, 10D2.718281828

    N@%, 5D H* The % sign is reserved. When usedby itself it is the last Out@D value or expression *L

    2.7183

    Log@ED H* Log is the natural logarithm *L1

    To recall any previous output Out[n], use %n. This is not good practice, though,

    because the input and output numbers change if they are executed more than once,

    and they disappear when you close a notebook or use the Quit[]command (see below).

    Built - In Functions

    Built-in function names start with a capital letter. For example, the trig functions areSin[x], Cos[x], Tan[x]. All trig functions use radian measure of angle. The exponen-

    tial function is E^x or Exp[x] or, using the Basic Math Assistantpalette, x. Hyper-

    bolic functions are Sinh[x], Cosh[x], Tanh[x]. Inverse trig or hyperbolic functions

    are, e.g, ArcCos[x] or ArcCosh[x]. The factorial function of n is "n!".

    Basics of Mathematica8.0.nb 3

  • 8/10/2019 David C. Johnston - Physics 304 Text Supplement - Basics of Mathematica [2013] [p22]

    4/22

    Help pages for Built-in Functions or Operations

    To bring up the help page for a built-in function or operation, type the Mathematica

    function name (e.g., Sin) into a new cell, or use one that exists within an existing cell,

    highlight it with the cursor, then press down and hold the shift and the command (on a

    Mac) (or alt on a PC?) keys and hit the "F" key. This brings up the help page for thatfunction. Extensive help is available in the Mathematica Help menu by typing in

    function names or keywords or phrases.

    e u omman

    The command "Quit[]" clears the Mathematica memory of all functions and variable

    values. Use it as the first command of the solution to a new problem. It also resets

    the input and output counters to [1].

    Defining a function of variables or functions of functions of variables,etc. ("patterns").

    These are not executed until later, after the functions and variables are defined. A

    symbol such as w_ below is a place holder into which any value or function can be

    inserted. The symbol := assigns the function on the right side to the function symbols

    on the left side.

    Quit@D

    Define a function:fcn@w_, b_D:= w Sin@bD H* Don't put w_ or b_ on the right side, only w and b. *L

    Define another function:

    z@x_, y_D:= [email protected] x + yD

    ow insert the value a into the first independent variable position of fcn[w,b] and the

    function z[x,y] into the second variable position:

    fcn@a, z@x, yDD H* This shows the power of using functions like f@w_,b_D *La Sin

    A-0.16 x+y

    ECalculate a numerical value for this function:

    a= 3; x= 5; y= 1;H* assign numerical values to the variables a, x and y *Lfcn@a, z@x, yDD2.81874

    4 Basics of Mathematica8.0.nb

  • 8/10/2019 David C. Johnston - Physics 304 Text Supplement - Basics of Mathematica [2013] [p22]

    5/22

    Generate a list (table) of values of a function

    Quit@Dfcn@x_D:= Exp@-x ^ 2Dfcn@x_D:= -x2

    H* This is the same function written using the Basic Math Assistant Palette*

    Ldatafile=Table@8x, fcn@xD

  • 8/10/2019 David C. Johnston - Physics 304 Text Supplement - Basics of Mathematica [2013] [p22]

    6/22

    datafile@@1DD@@1DD81., 0.367879