Math 615 - 09/16/2015
Introduction to Mathematica

Evaluating expressions

Begin by evaluating a simple arithmetic expression.  You will type the following, and then you will evaluate it by hitting “shift” and “return” at the same time.  Try doing other calculations.

1+(17-5)*2/5

Mathematica’s built-in commands all start with a capital letter.  This includes built-in constants such as π and e.  Try evaluating an expression containing one of these.

5Pi^2 - 1/E

By default, Mathematica will keep these answers exact.  To get a numerial approximation, use the function N.
As a general rule, parentheses () are used when you are writing algebraic expressions, brackets [] are used for functions, and braces {} are used for lists, matrices, and ranges.

N[5Pi^2 - 1/E]
N[5Pi^2 - 1/E, 20]

There are two other ways to do this, and they generalize to other functions.
The symbol % refers to the last output.
() // ()  will make mathematica input the expression on the left into the function on the right.

5Pi^2 - 1/E
N[%]

5Pi^2 - 1/E // N

5Pi^2 - 1/E
% // N

Be careful, because % refers to the expression evaluated most recently, not simply the expression immediately preceding within the text.  To see this, make the following all distinct cells, and then evaluate them in different orders.

17^(1/2)

N[%]

E^(2Pi)

Expressions and functions

Complicated expressions can be easily stored as variable names.  Suppose that we were solving the equation 3x^2 - 14x - 174π.  The solutions are a bit cumbersome, but we can do the following.

a1 = (14 + Sqrt[14^2 - (4)(3)(-174Pi)])/(2(3))
a2 = (14 - Sqrt[14^2 - (4)(3)(-174Pi)])/(2(3))

Let’s check to see how this behaves.

a1

a1+a2

N[a1]

3a1^2 - 14a1 - 174Pi

3a1^2 - 14a1 - 174Pi // N

Simplify[3a1^2 - 14a1 - 174Pi]

Functions are defined using the following conventions.

f[x_] := 3x^2 - 14x - 174Pi

f[0]

f[x+h]

f[a1]

% // Simplify

You can also replace variables occuring within an expression using the command /. and the arrow ->

g = 2x^2 - 4

g /. x-> 4

g /. x-> (x-2)

You may be tempted to do something like the following, but let’s see what goes wrong.

x = 4

g

h = 120 Sin[x] - 1

Try opening a new notebook and creating an expression with x.  Arghh!  Mathematica will automatically replace x by 4 everywhere, even when we move on to another expression or a new window.  To stop this from happening, we can clear the variable by

Clear[x]

x

If things get bad, you can clear everything from Mathematica’s memory by quitting the kernel, located in the Evaluation menu.

Matrix basics

Mathematica works with matrices as lists of lists.  Fortunately, there are nice input/output features.
(At least in the newer versions of Mathematica), the easiest way for a novice to create a matrix is to use the “Classroom Assistant,” which can be found under the “Palettes” menu.  Under Basic Commands, select the matrix looking button, and then add rows and columns to create the following matrix

A =(({{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 100}}))

MatrixForm[A]

RowReduce[A] // MatrixForm

Be careful with MatrixForm.  You always want to store matrices as matrices (lists) and display them using MatrixForm, but you do not want to store them in MatrixForm.

B = {{2, 1}, {1, 4}} // MatrixForm
B
RowReduce[B] // MatrixForm
(B = {{2, 1}, {1, 4}}) // MatrixForm
B
RowReduce[B] // MatrixForm

Matrix algebra is easy.  Addition uses “+“, multiplication uses “.”, and there are several built-in functions such as Det[], Transpose[], IdentityMatrix[n], and MatrixPower[A,n].
(Try using capital C for the matrix below, and you will see why it is a good idea to start variable names with lower-case letters.)

c = (({{1, -1}, {0, 4}}))
(A + B ) // MatrixForm
(B + c) // MatrixForm
(B.c.B) // MatrixForm
(MatrixPower[B,3]) // MatrixForm
(Det[c]) // MatrixForm
(Transpose[A]) // MatrixForm
(B + IdentityMatrix[2])// MatrixForm

Before quitting.

If you save right now, Mathematica will save all of the output data.  For what we have done, this is not very much.  However, once we introduce graphs or dynamic objects, this makes the file sizes unnecessarly bloated.  It is best to click “Delete All Output” from the Cell menu and then save your file.  

In fact, if you intend to use this notebook at a later date or turn it in, you should check that it will rerun correctly by deleting all the outputs and re-evaluating all the cells, which can be done quickly by clicking “Evaluate Notebook” in the Evaluation menu.  This is what I will do when I grade your notebooks.

Mathematica Homework 1

1.  Evaluate the following expression to a few decimal places: 3 sin(.5) - (Sqrt[17])

2.  Evaluate π to 142 decimal places.

3.  Consider the quadratic equation 5x^2 + (17(e^(-2))) x - 2 cos(1) = 0.
Store the two solutions as “a1” and “a2”.  Verify that your a1 and a2 are actually solutions to the equation.  

4.  Find rref for the following matrices.

(({{2, -3, 6, 1, -2}, {-4, -1, 0, 1, 6}, {7, 3, -1, 0, 7}, {-2, -3, 2, 5, 8}})) , (({{-4, -3, 6, -6, 3, -4}, {1, -4, -7, 3, 2, 5}, {7, 2, 6, -4, -1, 2}, {8, 6, -3, -6, -4, -1}, {-3, 6, -1, 3, 8, 9}}))


Save your file as “Lastname1.nb” and upload it to my Dropbox account at https://dbinbox.com/Math615
For example, I would save my file as Redden1.nb