chapter 1 - bryn mawr · pdf filemany functions in mathematica have optional arguments and the...

Download CHAPTER 1 - Bryn Mawr · PDF fileMany functions in Mathematica have optional arguments and the numerical evaluation function N is one of them. By adding the optional argument n, N

If you can't read please download the document

Upload: duongdat

Post on 07-Feb-2018

218 views

Category:

Documents


3 download

TRANSCRIPT

  • CHAPTER 1

    Getting Started

    1.1 Starting Mathematica When we start Mathematica a fresh window, or notebook, will open. This is where we will do all of our mathematical calculations and graphics. At the top of the window we see the title of the window, which initially is "untitled-I." Later, when we see how to save our work as a file, we can give a name to the file, and that name will appear in the title bar of the window.

    1.2 Entering Expressions Let's do our first calculation! If we type 1+1 and then press Shift-Return (i.e., hold down the Shift key and then the Return key)' Mathematica computes the sum and places the answer on the next line in the window. This is called evaluating or entering the expression. The window now contains

    10n Mac keyboards, Shift+Return is the same as Enter. With either Windows or Mac OS, using the Return key will simply move the cursor to the next line, allowing us to type more.

  • Example 1.2.1 } , ln[1]:= 1 + 1

    Out[1]= 2 ~] Notice that Mathematica has placed "In [ 1] : =" and "Out [ 1] =" labels to the

    left of 1 + 1 and 2, respectively. To the right of the input and output, Mathematica has placed a set of brackets. The two innermost brackets enclose the input and output, respectively, and the larger bracket groups the input and output together. Each bracket contains what is known as a cell. All of the calculations that we do in this notebook will be organized into cells and the brackets that surround the cells will come in handy for organizing our work. We'll have a lot more to say about this in Chap. 11, so don't worry too much about the brackets now. In fact, until we get to Chap. 11 we will be omitting the brackets most of the time when we display Mathematica input and output.

    1.3 Editing Cells Let's change l+l to 1+2. Mathematica supports all the usual mouse-driven text- editing features of word processors. We can simply use the mouse to place the cursor in the input cell and edit the entry so that it reads 1 + 2. To redo the calculation, we now reenter the cell by once again pressing Shift+Return. The result is

    Example 1.3.1 ln[2]:= 1 + 2

    ou1[21= 3

    Notice that the the In and Out labels have changed to "In [ 2] : =" and "Out [ 2 ] = . " Each time we reevaluate a cell, the numbers in the In and Out labels will change.

    To create a new cell with a new calculation, simply start typing. Mathematica will place the input in a new cell. When many cells are present we can use the mouse to place the cursor between existing cells and click the mouse button to insert a new cell at that location. Notice how the cursor changes from a vertical bar when located inside a cell, to a horizontal bar when located between cells. With the cursor between cells, click the mouse button and then start typing. Mathematica will create a new cell at the desired location.

  • istified

    J Finally, we can also click on the bracket which encloses a cell to select it. After selecting a cell we can reevaluate it by pressing Shift+Return or treat it just like any . ... ' selected item in a text document and cut, copy, or paste as usual. Try deleting an entire cell 'by clicking on its bracket and then choosing Edit ... Cut from the menu . bar, ( or using the equivalent keyboard shortcut). There are lots of ways that we can change the appearance of cells, changing the

    font, fontsize, color, and the like. We'll explore these topics in Chap. ll. ibels to the -uhemauca : input and it together. atwedo in td the cells > say about ct, until we we display

    1.4 Basic Arithmetic Mathematica can do all the basic operations of addition, subtraction, multiplication, division, and exponentiation (raising one number to another) which are denoted by the symbols+,-,*,/, and A.2 We can also use parenthesis for grouping as usual. Here is an example involving the arithmetic operations.

    driven text- .e the cursor .ulation, we

    Example 1.4.1 in[3]:= 2*3 + 4A2

    Out[3]= 22

    Here the exponentiation was done first; giving 2 * 3 + 16, then the multiplication, which leads to 6+ 16, and finally the addition. Mathematica follows the .standard order of operations, first performing all exponentiation (from left to right), then all multiplications and divisions (again from left to right), and finally, all additions and subtractions (from left to right). If we want to override these conventions we need to use parenthesis to group terms.

    One nice feature of Mathematica is that of implied multiplication. We do not 1 need to use the multiplication sign * in order to multiply. Instead, a blank space between things that can. be multiplied (numbers, variables, expressions) will be treated as multiplication. The blank space can even be omitted if parenthesis are used to indicate multiplication. If we do leave a blank space for multiplication, sometimes Mathematica will fill in the space with the multiplication symbol x. Basically, we can type calculations pretty much the way we would write them. Here are several examples, all contained in a single input cell.

    2]: =" and d Out labels

    1athematica can use the se button to 1 vertical bar :lls. With the 'nathematica

    2When computers were first introduced, exponentiation was denoted by the "up-arrow" t. The shaft of the arrow was eventually lost and we were left with only the arrowhead.

  • Example 1.4.2 ln[4J:= 5 X 6

    2 (3 + 4) (2 - 3 + 1) (1 + 2 / 3) - 5 A {-1) 6!

    Out[4]= 3 0

    Out[5]= 14

    1 Out[6]=- - s Out[7]= 720

    Here we entered four separate calculations in a single input cell. (This is when you use the Return key-to type a new line in the input cell.) Notice that each result is placed in its own output cell: We didn't use the multiplication sign for 5 * 6 in the first calculation and instead left a blank space. After entering the blank space and the 6, Mathematica inserted the x. In the second and third calculation, .because of the parenthesis, there is no confusion caused by leaving out the multiplication sign, so it is easier not to use it. The fourth calculation illustrates the factorial symbol !. We read 6! as "six factorial" rather than shouting SIX. By definition, n! is the product of all integers from 1 ton. Thus 6! = 6 5 4 3 2 1 = 720.

    1.5 Using Previous Results Quite often we will perform a calculation and then want to use the output of this calculation for our next calculation. We can use the percent symbo], %, to refer to the output of the previous cell. Here is an example.

    Example 1.5.l ln{11]:= 2 A 5

    Out[11]= '3 2

    ln[12]:= % + 10 0

    Out[12]= 13 2

    Notice that the first cell gave output of 32 and that the next cell added 100 to this to give 132. In this case the % symbol referred to the previous output. We

  • stified

    is is when eachresult 5*6 in the space and

    oecause of ation sign, 'symbol!. , n! is the

    put of this to refer to

    Jed 100 to mtput. We

    can even use%% to refer to the result before the last result, or even%%% for the result before that. Sometimes using the % symbol can be quite handy. However, it is important'to remember that % always refers to the last output: This can sometimes lead to unexpected results! In this book, we will rarely use the % symbol. .

    1.6 Exact versus Approximate One of the truly amazing features of Mathematica is that it will work things out exactly whenever possible. Sometimes this is just what we need, but sometimes it would be nicer to get an approximate answer. Consider the following example.

    Example 1.6.1 Jn{1Q]:= 3 A 20 / 2 A 21

    3 486 784 401 Out[iOJ= 2 097 152

    It's pretty hard to get a feel for the fraction 3~~6;}1t~01 and it might be nicer to approximate it with a decimal representation. We can force Mathematica to do this in two important ways. The first is to use decimal representations from the very beginning. If we replace 320 With 3.020 (or 3200, or even 3.0200) look what happens.

    Example 1.6.2 Jn[11]:= 3 o O A 2 0 / 2 A 21

    Out[11J= 1662. 63

    Mathematica always views decimal representations as approximations. Thus Mathematica considers 3.0 to be an approximate number rather than an exact num- ber. If we ever do a calculation that involves approximate numbers, Mathematica will give an approximate answer. On the other hand, if we use exact numbers in the input, Mathematica will do its best to provide exact numbers .in the output Here are several more examples that illustrate this point.

  • Example 1.6.3 ln[12J:= 3 / 4

    3 . o I 4.0 12 A ( l / 2) 12 A ,5

    3 Out[12]= -

    4

    Out[13]= 0, 7 5

    Out[14]= 2 Y3 Out[15]= 3. 4641

    Notice that 1 r ( 1 / 2 ) is the square root of 12 and that this is exactly equal to 2./3. So Mathematica has not only given us an exact answer, it has also simplified the input. On the other hand, by replacing the exponent of 1/2 by the "approxi- mation" of .5 we have forced Mathematica to give us an approximate answer in decimal form.

    The second important way to force Mathematica to give approximate answers is to use the numeric evaluation function N. We describe this function in the next section.

    1.7 Using Functions Mathematica has thousands of built-in functions. Fortunately, we only have to know a few dozen3 of the more important ones to do lots of neat calculations. We will be introducing the most important and useful functions in this book as we go. The next example uses the square root function Sqrt and the numeric evaluation function N.

    Example 1.7.1 ln[16J:= Sqrt [27]

    N [Sqrt [27]]

    Out[16]= 3 Y3 Out[17]= 5 .196:).5

    30K, I lied. Knowing a hundred functions would be nice. Actually, memorizing the names of most functions is not so hard. How hard can it be to remember Cos fo