Brief Introduction to Mathematica 
Instructor: Michael Pelsmajer
Monday, August 19, 2013

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)

Making your own calculation

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.

Try it.  Move the cursor so that it is directly below this line...

...and directly above this line.  (You should see a horizontal line.)  Then type some calculation and press Shift-Enter to do it.  (Ask for help if it doesn’t work.)

Making your own text

If you want to write a discussion, you should use a text cell, rather than a calculation cell.  (Trust me.)
Like before, move the cursor below this line so that it’s in between two lines.
But this time, click on the + which is at the left end of the line, then select “Plain Text”.

Now try typing some normal text.

More calculations

Try these:

Abs[2-5]

Sin[Pi/3]

sin[Pi/3]

Sin[pi/3]

Note that capitalization matters!  The last two didn’t work because the capitalization is wrong.  
And “x” does not equal “X”, more generally.

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 “*”.

(Mathematica should have added in the multiplication symbol as you typed.  It only does this for pure numerical multiplication, not with algebraic expressions.)

Exact vs. approximate calculations

Try this one. (I won’t keep writing this every time; just try all the calculations).

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 above calculations by “Pi”, and run them again (press Shift-Enter again).
(Do it now!)
Did it work?  How about 100 digits of Pi?

Functions

Create a function 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.

(Did you press Shift-Enter on that line?  If not, 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

Important: To create a function, you need the underscore "_", square brackets "[]", and the colon-equals ":=" just as shown! Otherwise, it won't work.

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.

There is a bar at the top with "File", "Edit", etc.  Click on "Palettes", then select "Basic Math Assistant".  (Do it now!)  It should open a new window.
Click on “Basic Commands” a couple times to show/hide that subpanel.  
Under Basic Commands, click on the “2D” tab.
We want to try a plot.  But don’t click “Plot” yet.  
Move the cursor between cells (below this line), to start a new calculation.  Now you can click “Plot”.

You should see “Plot[function,{var,min,max}]” appear, with “function” highlighted.
Click on its square root within the Basic Math Assistant window, then click on ArcCos, and then type: x.  
Now replace its var,min,max with x, -1, 1 (by clicking and typing).
And now Shift-Enter.  (Try it all now!)

(You should see a plot of ArcCos(x) on the interval [0,10].)

A few tips

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

x=6

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

Solve[x^2==25]

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 be careful with it.)
For example:

3+4

%

%+10

%/2

You don’t ever need 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.)

Exercises

Remember to save your work often while you work.
Before logging out of your 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: Why you can’t turn off your brain when you hit the “On” button

We shouldn’t trust blindly in calculators, or even in Mathematica.  Solve is one example: it doesn’t always find the solutions you are looking for (in a usable form).  Here we see another example, which shows that graphs can be misleading.  
Try these:

f[x_]:=((((1/2)))^(((((500*x))^2))))+Sin[x]

Plot[f[x],{x,-10,10}]

In the plot, it looks like f(0)=0. (Right?)  But it’s not true!!

Question 1: Find the correct value of f(0).
Question 2: Plot f(x) over a smaller interval which contains x=0, so that we can easily see the correct value of f(0).

Exercise 2: Solving a system of equations

Given the system of equations:

y=2x+2
y=2(x^3)+7(x^2)-x-2

Find the three solutions via the following four methods, and compare your results:

Graphically, by plotting the two expressions and estimating the points of intersection.

(You’ll want to mess around with the domain, to see the big picture, and to narrow in on the exact intersection points.  You might want to use more than one plot.)

Through the use of the Solve command.

Through the use of the Reduce command.

Through the use of the NSolve command.

Exercise 3: Optional Recommended Movies

http://www.wolfram.com/broadcast/screencasts/first-ten-minutes/
Although it covers similar ground as we did in this notebook, but having an alternative perpective can help a lot.  

If you wish to learn more Mathematica and you like the video, try these next:
http://www.wolfram.com/broadcast/screencasts/handsonstart/

Finishing up

These exercises are due next Monday afternoon.  You print the solutions and hand in the paper.  But before you do that:
1. Delete extraneous stuff, like my examples and instructions.  Also delete any failed attempts (unless you are angling for partial credit, I guess).  
---However, be careful to not delete function definitions and the like.  For example, if you delete the code that says “f[x_] : = x^2”, then your “f[2]” will no longer equal 4.
2. Make sure that your answers are written in proper English, and that they are in normal text cells (so that it looks nice, essentially).