Mathematica Demystified Example 1. 2.1 in1=1+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. ll, so don't worry too much about the brackets now. In fact, until we get to Chap 1l we will be omitting the brackets most of the time when we display Mathematic 1.3 Editing Cells Lets change 1+1 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 Notice that the the In and Out labels have changed toIn[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
2 Mathematica Demystified Example 1.2.1 In[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 1+1 to 1+2. Mathematica supports all the usual mouse-driven textediting 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 In[2]:= 1 + 2 Out[2]= 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
CHAPTER 1 Getting Started 30 Finally, we can also click on the bracket which encloses a cell to select it. After ecting 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 p Cut from the menu bar, (or using the equivalent keyboard shortcut) font,fontsize, color, and the like. We'll explore these topics in Chap. Il.g the There are lots of ways that we can change the appearance of cells, chan 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.2 We can also use parenthesis for grouping as usual Here is an example involving the arithmetic operations. Example 1. 4.1 n=2*3+4"2 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 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 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 ith 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 uters were first introduced, exponentiation was denoted by the"up ↑. The shaft of the as eventually lost and left with only the arrowhead
CHAPTER 1 Getting Started 3 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. 11. 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 ˆ.2 We can also use parenthesis for grouping as usual. Here is an example involving the arithmetic operations. Example 1.4.1 In[3]:= 2*3 Out[3]= 22 + 4^2 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 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 ×. Basically, we can type calculations pretty much the way we would write them. Here are several examples, all contained in a single input cell. 2When computers were first introduced, exponentiation was denoted by the “up-arrow” ↑. The shaft of the arrow was eventually lost and we were left with only the arrowhead.
Mathematica Demystified Example 1.4.2 n4=5x6 2(3+4) (2-3+1)(1+2/3)-5^(-1 ou4=30 ou5=14 o6= u7=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 didnt 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 I to n. Thus 6=6.54 32-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 symbol, % to refer to the output of the previous cell. Here is an example Example 1.5.1 n1=2"5 n12}=%+100 o2-132 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
4 Mathematica Demystified Example 1.4.2 In[4]:= 5 6 2 3 4 (2 3 1 5^ 1 6 ! Out[4]= 30 Out[5]= 14 Out[6]= – 1 5 Out[7]= 720 × – + + ) ( ) (1 + /) ( ) 2 3 –– 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 ×. 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 to n. 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 symbol, %, to refer to the output of the previous cell. Here is an example. Example 1.5.1 In[11]:= 2^5 Out[11]= 32 In[12]:= 100 Out[12]= 132 + 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
CHAPTER 1 Getting Started can even use o% to refer to the result before the last result. or even %0%% for the result before that. Sometimes using the %o symbol can be quite handy. However, it is important to remember that %o 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 n1lp=3^20/2^21 3486784401 2097152 It's pretty hard to get a feel for the fraction 348060721401 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 320., or even 3. 020.0)look what Example 1.6.2 n113.020 outt1662.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
CHAPTER 1 Getting Started 5 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 In[10]:= 3 ^ 20 / 2 ^ 21 Out[10]= 3 486 784 401 2 097 152 It’s pretty hard to get a feel for the fraction 3486784401 2097152 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 320.0, or even 3.020.0) look what happens. Example 1.6.2 In[11]:= 3.0 ^ 20 / 2 ^ 21 Out[11]= 1662.63 Mathematica always views decimal representations as approximations. Thus Mathematica considers 3.0 to be an approximate number rather than an exact number. 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
Mathematica Demystified Example 1.6.3 n12=3/4 3.0/4.0 12^(1/2) 12-.5 Out 12 ou13=0.75 ou142 u15=3.4641 Notice that 12(1/2)is the square root of 12 and that this is exactly equal to V3. 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 secto 1.7 Using Functions Mathematica has thousands of built -in functions. Fortunately, we only have to know a few dozen 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 In[16]:=Sart [27] N[Sqrt [27]] 叫16}= u17=5.19615 3OK, I lied. Knowing a hundred functions would be nice. Actually, I is not so hard. How hard can it be to remember Cos for cosine, Abs for absolute g the names of most functions alue and Total for well total? The real work is going to be remembering the syntax needed to use these functions. a good strategy will be to get good at using the built-in documentation
6 Mathematica Demystified Example 1.6.3 In[12]:= 3/4 3.0 / 4.0 12 ^ (1/2) 12 ^ .5 Out[12]= 3 4 Out[13]= 0.75 Out[14]= 2 3 Out[15]= 3.4641 Notice that 12ˆ(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 “approximation” 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 In[16]:= Sqrt [27] N[Sqrt [27]] Out[16]= 3 3 Out[17]= 5.19615 3OK, 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 for cosine, Abs for absolute value, and Total for, well, total? The real work is going to be remembering the syntax needed to use these functions. A good strategy will be to get good at using the built-in documentation