SCCC Mathematica Tutorial, © 2007-2020, Seattle Central Community College Math Dept., contact: Greg.Langkamp@seattlecolleges.edu
Version 1.9/ March 2020
Version 1.9/ March 2020
Important!
Click "Make your own Copy" in the black ribbon above to make this document interactive. You will also need to be logged into Wolfram Cloud
Click "Make your own Copy" in the black ribbon above to make this document interactive. You will also need to be logged into Wolfram Cloud
Lesson 1 Numerical Computation
Lesson 1 Numerical Computation
1.1 How to enter a simple numerical calculation
1.1 How to enter a simple numerical calculation
Using Mathematica to do numerical calculations is very straightforward. Just enter the expression and then press (hold down the Shift key, then press Enter). We will refer to pressing as "evaluating" the input. The result of the calculation will appear in a math output cell.
◼
Enter 2 + 2 in the math input cell below and then press (hold down the Shift key, then press Enter).
Out[]=
4
Another way to evaluate an inputSome large keyboards have an Enter key at the extreme right of the keyboard, next to a numeric keypad. This Enter key can be pressed without pressing Shift.
◼
Click anywhere in the input cell below and then press the Enter key on the extreme right of the keyboard. (If you only have one Enter key, press Shift-Enter.)
1+2+3+4+5
Modifying an input to create a new inputNote that any Mathematica input cell can be changed and reevaluated. Change the 5 in the input above to 500 and then evaluate the revised input. Observe that the output automatically updates to reflect the new input.
Creating a new input cellIf you pass your mouse vertically over the screen you will see that the edit cursor is vertical (looks like an I-beam) when passing over text cells and existing input and output cells, but switches to a horizontal edit cursor in the spaces between these cells. At any point where the cursor is horizontal you can click on the screen and Mathematica will draw a faint horizontal line. Just start typing and a new math input cell is automatically created. Try doing this now above the gray box below.
Enter a new math input cell above this gray box ↑↑↑↑
Deleting math input cellsOccasionally you will create a few extra math input cells that you want to get rid off. To delete cells, click on the cell bracket icon on the far right side of the screen. Then press the Delete key on your keyboard. Practice this now by deleting the two math input cells below.
Below are two exercises for practice. Give them a try!
Exercise 1.1 A
Evaluate the expression +. Enter this just as you would on a calculator (use " / " for division). Don't forget to enclose the entire numerator in parentheses.
2
3.56
4
4.53
34.87
Answer to Exercise 1.1A
(3.56^2+4.53^4)/34.87
Exercise 1.1 B
Create a new input cell with the expression 5 + 9 between the two text cells below. Move the mouse from one text cell down to the other and click and start typing when the cursor is horizontal.
Put your new input cell below this text cell ↓↓↓↓↓↓↓↓↓ ...
..and above this text cell ↑↑↑↑↑↑↑↑↑
Answer to Exercise 1.1B
This is what it should look like when you are done.
Put your new input cell below this text cell ↓↓↓↓↓↓↓↓↓
....
5+9
... and above this text cell ↑↑↑↑↑↑↑↑↑
1.2 Exact vs. numerical approximations -- using the "N" command
1.2 Exact vs. numerical approximations -- using the "N" command
Calculate the value of .In the input cell below, enter the expression 34^19, then press .
19
34
Note that Mathematica produces the exact answer here; all thirty digits! Mathematica will always display the exact value when doing a calculation if it is possible to do so. However, if we really just want a numerical approximation, then we can use the "N" command as shown below. Here "N" stands for "numerical".
◼
Click anywhere in math input cell below and press . (Note the use of square brackets in the command below. Square brackets are used in all Mathematica commands).
N[34^19]
Here the answer is expressed using scientific notation with a default of 6 significant digits. To get more digits, we just add a second argument to the command. For example, if we want 25 digits we enter N[34^19, 25].
◼
Click anywhere in the math input cell below and press .
N[34^19,25]
Note that the command above has two "arguments" (or inputs) namely 34^19 and 25. These are separated by a comma. This is standard syntax for Mathematica commands that have more than one argument.
When entering a command it's okay to put in some extra spaces as in the next input cell.
N[34^19,25]
1.3 Adding fractions
1.3 Adding fractions
◼
Add the fractions + by evaluating the input cell below.
2
3
5
7
2/3+5/7
Again Mathematica gives us the exact answer rather than a decimal approximation.
Exercise 1.3 A
Find a 100 digit approximation for the sum of the fractions +.
2
3
5
7
Answer to Exercise 1.3A
N[2/3+5/7,100]
Math fact: The fraction is by definition a rational number. Recall that rational numbers are always expressible as either terminating decimals (e.g. =.75) ... or repeating, non-terminating decimals (e.g. =.333333...). What can you say about ?
29
21
3
4
1
3
29
21
1.4 Another way to apply the N command
1.4 Another way to apply the N command
In Section 1.2 you learned about the N command which produces numerical approximations. Recall that Mathematica will produce the exact answer for a calculation whenever possible. This sometimes produces surprising results.
◼
Execute the input below.
(987^17-85^20)/1452
Well, there it is, the exact answer! This is the exact, reduced fraction which is equal to our input. In cases like this we almost always prefer Mathematica to give us a numerical approximation.
◼
Earlier we applied the N command to our original expression as in the input cell below. Execute this cell.
N[(987^17-85^20)/1452]
◼
In the input cell below, we introduce an alternative way to achieve the same result. This is called "postfix" (as contrasted to "prefix") form, since here the command follows, rather than precedes, the argument. Note the use of the double forward slash " // ". Execute this cell.
(987^17-85^20)/1452//N
Prefix vs. Postfix Postfix form is often convenient, however it does have one significant limitation. It is not possible to specify options for a command when using the postfix form. So, for example, if you want π displayed with 20 digits then you will need to use . There is no way to use //N and at the same time specify that you want 20 digits.
N[π,20]
1.5 Representing the numbers π and in Mathematica
1.5 Representing the numbers π and in Mathematica
In Mathematica we can enter the constant π by using the keyboard shortcut p . (You will find the key at the upper left-hand corner of your keyboard).
◼
Enter π in the input cell below by using the keyboard shortcut p . (Observe that when you press the key it appears as , then you enter p, and when you press the key the second time, the π symbol magically appears).
Exercise 1.5 A
Answer to Exercise 1.5A
Exercise 1.5 B
Answer to Exercise 1.5B
Exercise 1.8 A
Answer to Exercise 1.8A
Or we could use the shorter form.
Exercise 1.8 B
Create a list that consists of all the even numbers from 12 to 56.
Answer to Exercise 1.8B
Exercise 1.8 C
Answer to Exercise 1.8C