CHAPTER 1 Getting Started 7下0 Here we see the use of the built-in square root function Sqrt Entering Sqrt[27] is the same as entering 27(1/2). In the first computation we entered the number 27 exactly, so obtained an exact answer. In the second calculation we still entered 27 exactly but Mathematica provided an approximate answer. We forced this to happen by using the numerical evaluation function N. This function will convert any number into a decimal representation. Many functions in Mathematica have optional arguments and the numerical evaluation function N is one of them. By adding the optional argument n, N[x, n] will estimate x with n-digit precision. If we want to know a certain number of digits in the decimal representation of T for example, we can use N as in the first line of Example 1.7.2. Here we use Pi to stand for T, the ratio of the circumference to the diameter of any circle. Math ematica has special symbols for a number of important mathematical constants, including E, the base of the natural logarithm, and I, the imaginary number whose square Is-1 Example 1.7.2 n3=N[Pi,100 N[Pi/10.0,30] o33.141592653589793238462643383279502884197169 3993751058209749445923078164062862089986280 34825342117068 o3=0.314159265358979323846264338328 ou35=0.314159 The second and third lines of Example 1.7.2 illustrate an important feature of N In both cases we have asked Mathematica for 30 digits in the expansion of /10 In the first case we get it, but in the second case we do not. This is because in the second case we have already moved to an approximation by using 10.0 instead Here are some sample calculations involving the constants E and I Amazingly, in November of 2005, Chao Lu recited the first 67890 digits in the decimal expansion from ory!Checkouthttp://www.pi-world-ranking-list.com/index.html
CHAPTER 1 Getting Started 7 Here we see the use of the built-in square root function Sqrt. Entering Sqrt[27] is the same as entering 27ˆ(1/2). In the first computation we entered the number 27 exactly, so obtained an exact answer. In the second calculation we still entered 27 exactly but Mathematica provided an approximate answer. We forced this to happen by using the numerical evaluation function N. This function will convert any number into a decimal representation. Many functions in Mathematica have optional arguments and the numerical evaluation function N is one of them. By adding the optional argument n, N[x, n] will estimate x with n-digit precision. If we want to know a certain number of digits in the decimal representation of π, for example, we can use N as in the first line of Example 1.7.2.4 Here we use Pi to stand for π, the ratio of the circumference to the diameter of any circle. Mathematica has special symbols for a number of important mathematical constants, including E, the base of the natural logarithm, and I, the imaginary number whose square is −1. Example 1.7.2 In[33]:= N[Pi, 100 ] N[Pi / 10, 30] N[Pi / 10.0 , 30] Out[33]= 3.141592653589793238462643383279502884197169 3993751058209749445923078164062862089986280 34825342117068 Out[34]= 0.314159265358979323846264338328 Out[35]= 0.314159 … … The second and third lines of Example 1.7.2 illustrate an important feature of N. In both cases we have asked Mathematica for 30 digits in the expansion of π/10. In the first case we get it, but in the second case we do not. This is because in the second case we have already moved to an approximation by using 10.0 instead of 10. Here are some sample calculations involving the constants E and I. 4You may remember that π is an irrational number, one whose decimal representation never ends and never repeats. Amazingly, in November of 2005, Chao Lu recited the first 67890 digits in the decimal expansion from memory! Check out http://www.pi-world-ranking-list.com/index.html
Mathematica Demystified Example 1. 7.3 n21=Sqrt[-16] ou21=4 ou222.718281828 The last calculation is one of the more amazing identities in all of mathematics! It follows from Euler's formulas = cos e If we substitute 0=z. we obtain eiT==1 In the last computation we tried to take the square root of -16 and mathemat responded with the imaginary number 4i Mathematica is perfectly happy using complex numbers a bi where a and b are real numbers and i is the imaginary num- berv-1. The numbers a and b are called the real and imaginary parts of a+bi, re- spectively. Mathematica has several built-in functions that deal especially with com plex numbers. Two of the more important ones are the functions Re and lm which re- turn the real and imaginary parts, respectively, of a complex number. Another impor- tant function is the absolute value function abs which works not only for real num- bers but for complex numbers too. In the case of a complex numbera bi, its abso- lute value is defined as va2+b2. Example 10.3. 10 gives a few more calculations Example 1.7.4 n24}=(2+4工)(6-3工) Im[6-3工 Aba[-23] Abs[3+4工 out2424+18i u25=2 Leonhard Euler(1707-1783)was one of the greatest mathematicians of all time. He published his famous ormula in 1748
8 Mathematica Demystified Example 1.7.3 In[21]:= Sqrt [– 16] N[E, 10] E ^ (I Pi) Out[21]= 4 Out[22]= 2.718281828 Out[23]= – 1 The last calculation is one of the more amazing identities in all of mathematics! It follows from Euler’s formula5 eiθ = cos θ + i sin θ. If we substitute θ = π, we obtain eiπ = −1. In the last computation we tried to take the square root of −16 and Mathematica responded with the imaginary number 4i. Mathematica is perfectly happy using complex numbers a + bi where a and b are real numbers and i is the imaginary number √−1. The numbers a and b are called the real and imaginary parts of a + bi, respectively. Mathematica has several built-in functions that deal especially with complex numbers. Two of the more important ones are the functionsRe and Im which return the real and imaginary parts, respectively, of a complex number. Another important function is the absolute value function Abs which works not only for real numbers but for complex numbers too. In the case of a complex number a + bi, its absolute value is defined as √a2 + b2. Example 10.3.10 gives a few more calculations. Example 1.7.4 In[24]:= ( 2 + 4 I) ( 6 – 3 I) Re [ [ 2 6 + – 4 3 [ – 23 I I ] ] ] Im Abs Abs[3 + 4 I] Out[24]= 24 + 18 Out[25]= 2 5Leonhard Euler (1707–1783) was one of the greatest mathematicians of all time. He published his famous formula in 1748
CHAPTER 1 Getting Started 90 Example 1.7.4( Continued) ou26-3 Out(27=23 Out(28 5 Mathematica has all the common mathematical functions built-in These include the trigonometric functions and their inverses, the hyperbolic trigonometric func- tions and their inverses, and the logarithm and exponential function. Mathematica also has many special, more esoteric functions too. In this book we will be primarily interested in the more common mathematical functions There are two very important features of all built-in Mathematica functions First, all built-in functions in Mathematica begin with capital letters. Some, like the inverse cosine, Arc Cos, may even have multiple capital letters. Second, square brackets are always used to surround the input, or arguments, of a function. So we type Abs[-12 ] not Abs(-12), if we want to compute the absolute value of -12 Moreover, this is the only use of square brackets in Mathematica.(Actually, the only use of single square brackets. We'll see shortly that double square brackets, I[ and l], are used with lists ) Parentheses, and ) are used to group terms in algebraic expressions. One other set of delimiters that will be extremely important are the"curly braces, I and. These are used to delimit lists, something that we will be introducing shortly. The three sets of delimiters, [], (, and are used for functions, algebraic expressions, and lists, respectively, and only for these purposes This can be hard to get used to at first, but leads to a great system 1.8 Using variables We may introduce variables and give them values using the equals sign. Here are Example 1. 8.1 In(29=a= 2 Out(29=2 ou31=5
CHAPTER 1 Getting Started 9 Example 1.7.4 (Continued) Out[26]= –3 Out[27]= 23 Out[28]= 5 Mathematica has all the common mathematical functions built-in. These include the trigonometric functions and their inverses, the hyperbolic trigonometric functions and their inverses, and the logarithm and exponential function. Mathematica also has many special, more esoteric functions too. In this book we will be primarily interested in the more common mathematical functions. There are two very important features of all built-in Mathematica functions. First, all built-in functions in Mathematica begin with capital letters. Some, like the inverse cosine, ArcCos, may even have multiple capital letters. Second, square brackets are always used to surround the input, or arguments, of a function. So we type Abs[-12], not Abs(-12), if we want to compute the absolute value of −12. Moreover, this is the only use of square brackets in Mathematica. (Actually, the only use of single square brackets. We’ll see shortly that double square brackets, [[ and ]], are used with lists.) Parentheses, ( and ), are used to group terms in algebraic expressions. One other set of delimiters that will be extremely important are the “curly braces,” { and }. These are used to delimit lists, something that we will be introducing shortly. The three sets of delimiters, [ ], ( ), and { } are used for functions, algebraic expressions, and lists, respectively, and only for these purposes. This can be hard to get used to at first, but leads to a great system. 1.8 Using Variables We may introduce variables and give them values using the equals sign. Here are some examples. Example 1.8.1 In[29]:= a = 2 b = 3 a + b Out[29]= 2 Out[30]= 3 Out[31]= 5
10 Mathematica Demystified Now that we have defined a to be equal to 2, it will remain equal to 2 unless re set it equal to something else, or use the Clear function to clear its value. This is extremely important and can sometimes lead to a great deal of frustration! If we forget that we have given a value to a certain variable and then try to use the variable later as if it had no value, we can run into unexpected results. Example 1.8.2 shows how the Clear function works. Remember that previous to evaluating the cell. a =2 and b= 3 Example 1.8.2 In(32:=Clear [al Clear [b] a+ b ou3]=3+a ut[35]=a+b Variables that are given values retain those values until we quit Mathematica or use Clear. It is very important to remember this! Also, once we use Clear the variable will continue to not have a value until we give it one. Thus, if we reenter he above cell we will not get the same output! The second time we enter it, both a and b will have been cleared and we will not get the output of 3+a. Clear can bo used with a number of options. A useful construction is Clear["Global"] which will clear everything! The real power of Mathematica is that it can manipulate abstract expressions rather than just specific numbers. So we will often use variables that are, well, variable! That is, they have not been set equal to any specific value. The following ample illustrates this. We'll talk a lot more about the Expand function in Chap. 4, but for now you can probably guess what it does Example 1.8.3 In(36)-Expand[(x+y)10] out36}=x20 +10x9y+4 y+120x7 210x6y4+252x5y5+210x4y6 120x3y7+45x2y8+10xy9+y20
10 Mathematica Demystified Now that we have defined a to be equal to 2, it will remain equal to 2 unless we set it equal to something else, or use the Clear function to clear its value. This is extremely important and can sometimes lead to a great deal of frustration! If we forget that we have given a value to a certain variable and then try to use the variable later as if it had no value, we can run into unexpected results. Example 1.8.2 shows how the Clear function works. Remember that previous to evaluating the cell, a = 2 and b = 3. Example 1.8.2 In[32]:= Clear [a] a + b Clear [ b] a + b Out[33]= 3+a Out[35]= a + b Variables that are given values retain those values until we quit Mathematica or use Clear. It is very important to remember this! Also, once we use Clear the variable will continue to not have a value until we give it one. Thus, if we reenter the above cell we will not get the same output! The second time we enter it, both a and b will have been cleared and we will not get the output of 3+a. Clear can be used with a number of options. A useful construction is Clear[“Global`*”] which will clear everything! The real power of Mathematica is that it can manipulate abstract expressions rather than just specific numbers. So we will often use variables that are, well, variable! That is, they have not been set equal to any specific value. The following example illustrates this. We’ll talk a lot more about the Expand function in Chap. 4, but for now you can probably guess what it does. Example 1.8.3 In[36]:= Expand [ ( x + y) ^ 10] Out[36]= x10 + 10 x9 y + 45 x8 y2 + 120 x7 y3+ 210 x6 y4 + 252 x5 y5 + 210 x4 y6+ 120 x3 y7 + 45 x2 y8 + 10 x y9 + y10
CHAPTER 1 Getting Started Variables are case sensitive. Thus s and s are two different variables. Exam- ple 1.8.4 illustrates this point Example 1. 8.4 n29=a=2 s=a+b+c Out 31F 4 Out(32=9 Out[33]=S Since s(for sum) is the sum of a, b, and c, we see 9 for the fourth output line. But because S is not the same variable as s, the fifth output line contains the name of the variable S. This variable has no value since we have not set it equal to You can use almost anything as a variable name except that variable names cannot start with a number. Thus x2 can be used but 2x cannot. Moreover words or letters that already have meaning in Mathematica cannot be used. For example, we cannot use E as a variable name because E is already being used by mathematica to stand for the base of the natural logarithm. Other reserved words and letters exist too. If you try to use one, Mathematica will simply tell you that you are not allowed to. Here is an example of this Example 1. 8.5 n42}=c=12 Set: wrsym: Symbol C is Protected. ou42=12 Since c is reserved we cannot use it for a variable. and Mathematica warns us that this is the case by typing the rather crytptic"Set: wrsym: Symbol C
CHAPTER 1 Getting Started 11 Variables are case sensitive. Thus s and S are two different variables. Example 1.8.4 illustrates this point. Example 1.8.4 In[29]:= a = 2 b = 3 c = 4 s = a + b + c S Out[29]= 2 Out[30]= 3 Out[31]= 4 Out[32]= 9 Out[33]= S Since s (for sum) is the sum of a, b, and c, we see 9 for the fourth output line. But because S is not the same variable as s, the fifth output line contains the name of the variable S. This variable has no value since we have not set it equal to anything. You can use almost anything as a variable name except that variable names cannot start with a number. Thus x2 can be used but 2x cannot. Moreover, words or letters that already have meaning in Mathematica cannot be used. For example, we cannot use E as a variable name because E is already being used by Mathematica to stand for the base of the natural logarithm. Other reserved words and letters exist too. If you try to use one, Mathematica will simply tell you that you are not allowed to. Here is an example of this. Example 1.8.5 In[42]:= C = 12 Set::wrsym : Symbol C is Protected. >> Out[42]= 12 Since C is reserved we cannot use it for a variable, and Mathematica warns us that this is the case by typing the rather crytptic “Set::wrsym: Symbol C is