Brief Introduction to Mathematica

Instructor: Michael Pelsmajer
Teaching Assistant: Francisco Hernandez
Monday August 25 or Thursday August 28, 2014

Mathematica is a powerful tool for calculation.  Today, we will see only a little bit, just to get us started with it.

Example:

Click anywhere on the line below.  Or, use the arrow keys to move the cursor into it.  
Then press Shift-Enter on the keyboard.

2+2

You should see a “4”.  (If you don’t something is wrong - ask for help.)

Continue below in the same way: move the cursor into each line which is written in  This Font, press Shift-Enter, and see what happens.
And read all the text!
There will be further instructions later.

Arithmetic

Try these:

2+5

2-5

2*5

2/5

2^5

Sqrt[9]

9^(1/2)

9^(1/4)

“Cells” for your own calculations

You’ll notice that on the right side of the screen there are brackets.  Each bracket defines a “cell”.  

If you move the cursor in between two cells and start typing, it will create a new cell where you can do calculations.
You can move the cursor with the mouse(click), or you can use arrow keys on the keyboard.

Try it now:

Move the cursor slightly below this cell.  (You should see a grey horizontal line with a “+“ on the left end.)
Then type 2+3, and press Shift-Enter.   (You should see a “5” appear, of course.  Ask for help if you don’t see a 5.)

Writing Legible Text - Try it now:

Move the cursor between cells, but this time, click on the + at the left end of the gray line.  
Then, select “Plain Text”.  Now write a funny sentence about a cow, or whatever.

Whenever you want to write legible, normal text in Mathematica, do it that way.

Open closed cells

The rest of cells in this notebook are “closed”, so you can’t see what’s inside them.  
To open a closed cell, go the far right bracket, and double-click it.

Test: open this cell now!

It worked, you opened the cell!  
Now, double click the bracket again on the right to close it back up.

(If you don’t see the color pink, ask for help.)
Continue, opening cells as you need them.

More calculations

Try these:

Abs[2-5]

Sin[Pi/3]

sin[Pi/3]

Sin[pi/3]

7x-4X

7x-4x

Capitalization, Parenthesis/bracket type, etc.

Three of the previous calculations failed, because capital letters and lower-case letters are not the same to Mathematica.  Capitalization matters.  
So “x” is different than “X”.
Special words like “sin” and “pi” must have the right letters capitalized (usually the first letter of each word).
Capitalization matters!

Parentheses/bracket type matters, too.  For algebra, use normal parentheses “( )”.  For Mathematica-defined commands (including Sqrt, Sin, Plot, and more) use “[]” and sometimes you also need “{}”. If you use the wrong type, Mathematica will not work correctly.  
For example, try these three:

Sin(Pi/3)

Sin{Pi/3}

[6-3]^2

Tip: For multiplication, you can use a “*” or you can just leave a blank space.  
Try multiplying two numbers below without using “*”.

Try it now!  (Mathematica should have added in the multiplication symbol as you typed.  It only does this for pure numerical multiplication, not with algebraic expressions.)
Ask if it didn’t work right.

Exact vs. approximate calculations

From now on, whenever you see Mathematica code, run it (using Shift-Enter).

224/5928

If you want a decimal approximation:

N[224/5928]

You can also specify the number of decimal places of accuracy:

N[224/5928,30]

Hey you!  Replace “224/5928” in each of the previous three calculations by “Pi”, and run them again (press Shift-Enter again).

Did it work?  
How about 100 digits of Pi? Try it!

Functions

Defining a function looks like this:

f[x_] := x^2 - x +1

You must press Shift-Enter on the above line.  That is how, you tell Mathematica to remember what f(x) means. 
(You won’t see much happen - perhaps you will notice a small color change.  But if you don’t do it, stuff won’t work later.)

Try the following.  (Did you press Shift-Enter on the previous line like you should have?  If you didn't, the following lines won’t do anything.)

f[5]

f[a+5]

Expand[f[a+5]]

Here’s another function:

dog[x_] := Sqrt[x] +Pi/x

To create the previous functions, note the use of
   the underscore "_", 
   square brackets "[]", and 
   the colon-equals ":=".
You need to have them all, just like that.  Otherwise, it won't work.  (This is perhaps the most common mistake I see.)

Now try these:

dog[5]

f[dog[t]]

Plotting functions

Plot[f[x],{x,-2,6}]

Plot[{f[x],dog[x]},{x,-2,6}]

Note that curly braces “{}” are needed for the domain and also for multiple functions.

There is another, more flexible way to combine plots: Name each one, and use “Show” to combine them:

plot1= Plot[f[x],{x,-2,6}];
plot2= Plot[dog[x],{x,1,4}];
Show[plot1,plot2]

Solving equations: Solve, NSolve, and Reduce

Solve[(x-1)^2 == 5]

Important: You need a double equal-sign. Also, square brackets “[ ]”.

If there is more than one variable, you need to say which one you are solving for:

Solve[(x-3y)^2 == 5,x]

Solve[(x-3y)^2 == 5,y]

Try this one:

Solve[f[x]==dog[x]]

Riiiiight.  Not so helpful.  (Some equations don’t have nice “closed form” solutions.)
So, let’s ask for approximate decimal solutions instead.

NSolve[f[x]==dog[x]]

Two of those are complex numbers.  Here’s how you ask for real solutions:

NSolve[f[x]==dog[x], Reals]

Reduce is similar to Solve, but its output is different:

Solve[{x-Abs[x]==0},{x}]

Reduce[{x-Abs[x]==0},{x}]

Generally, Reduce is more powerful than Solve, but Solve's output (when it produces output) is more immediately useful.

Using “Palletes : Basic Math Assistant”

Here you will see an alternative way to work with Mathematica, which is maybe easier: using “Palettes”.
To demonstrate, we’ll do something a bit fancy: create a plot of ArcCos(x).

First, move the cursor between two cells (and click), the way you would to create a new cell for a calculation.

Next, at the bar at the top of this window, click on "Palettes", then select "Basic Math Assistant".  (A new window will open.)
Click on “Basic Commands” a few times to show/hide that subpanel.  
Within the “Basic Commands” subpanel, click on the “2D” tab.
Then click “Plot”.  

You should see “Plot[function,{var,min,max}]” appear.  
The first field, function, will be highlighted by the cursor, but just in case it isn’t, mouse-click on function.

Plot[function,{var,min,max}]

Next, click on the “square root” within the Basic Math Assistant window, then click on “ArcCos”, and finally, type “x”.  
Now replace its var,min,max with x, -1, 1 (by clicking and typing).
And now press Shift-Enter!

(You should see a plot of ArcCos(x) on the interval [0,10].  Ask for help if you don’t.)

A few tips

(1) Never assign a number to a common variable, like this:

x=6

(Try it!)  Now x is 6, so you won’t be able to use x in the normal way.

Solve[x^2==25]

(Do you understand why it returns no solutions?  If not, ask!)

If this happens, use Clear to change x back to a variable.

Clear[x]

Solve[x^2==25]

(2) The percentage sign % represents the quantity you computed most recently (which could be anywhere in the window, or even in another Mathematica window, so take care).

For example, try these in order:

3+4

%

%+10

%/2

You’ll never be required to use %, but sometimes it’s convenient, and you’ll see other people using it a lot.

(3) I do not expect you to memorize everything in this worksheet.  I expect you to know where to find it, when you need to remember how to do something. 

Mathematica also has built-in Help, which you can access by clicking “Help” at the top of the window.
If you want help with a particular command, say, “Plot3D”, then you can learn about it this way:

?Plot3D

You’ll see a quick summary.  For full details, including examples(!), click on the “>>” and a new window will pop up.  (Try it now.)

Exercises

Create a new Mathematica notebook for your answers.  (From the menu bar at the top, click “File”, then “New”, “Notebook”.)  

Only the exercises, your answers, and necessary work should go in the notebook.  (Delete typos, etc.)  Use text cells and calculation (aka “input”) cells appropriately - it must look nice.  At the top of the workbook, put your name, class, and section (151-01 or 151-02).  You will be graded on your ability to follow these directions.

Remember to save your work often.  Before logging out of an IIT workstation, save your work on a thumbdrive or in the cloud.  When you log back in, your work will not be there anymore (as far as I know).

Exercise 1

Use Mathematica to calculate the square root of Tan ((7 π)/12).

Exercise 2

Make a plot of the graph of y = Tan(x) over the interval [ 0, (π/3) ].

Exercise 3

Define the function h(x) = (x^2) -3 x+4.5`.  
Then plot the graph of y = h(x) over the interval [ -1.5, 4.5 ].

Exercise 4

Plot Tan(x) and h(x) together over the interval [0,5].

Exercise 5

Plot Tan(x) and h(x) together over the interval [0,5], but this time, add a “legend” to identify which curve is which.  (To get started, try: “?Plot”.)

Exercise 6

Part A:
Run the following code, which will produce a plot which is interactive, with sliders that you can move to change the parameters.
After you create the plot, try dragging the sliders to see what happens.

Manipulate[Plot[Sin[a x+b],{x,0,6} ,PlotLabel -> "y ="Sin[a x + b]],{{a,2,"Multiplier"},1,4},{{b,0,"Phase Parameter"},0,10}]

Part B:
Modify the code so that the domain (the interval of x-values) is [-(π/2), (π/2)] .  (Critical Hint: Do not try to understand the whole thing!  Instead, just figure out which part of it must be determining the domain, and change only that, very carefully so you don’t mess up the rest of it.)