Brief Introduction to Mathematica 
Thursday, January 16, 2014
Instructor: Michael Pelsmajer
Teaching Assistant: Adam Rumpf

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

I do not expect you to memorize much, if any of this!  The idea is that today you will familiarize your self with the basic functionality of Mathematica, and later you this document will serve as a quick reference sheet.  In the future, you can return here, copy similar examples, and modify them to suit your needs.

Example:

See the line “2+2” written below?  Move the cursor onto it  (using the arrow keys, or by mouse + left-click).
Now press Shift-Enter on the keyboard.  You should see a “4” appear.

2+2

!!!IF YOU DON’T SEE “4”, ASK FOR HELP NOW!!!

Continue through the rest of this “Notebook” in the same way:
Whenever you see a line written in  This Font, move the cursor into that line, press Shift-Enter, and see what happens.  Got it?  
Oh, and read all the text!

(There will be further instructions later, as the lesson proceeds.)

Arithmetic

Try these calculations.  (Move the cursor to each line, press Shift-Enter, etc.)

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 appear.)  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.
Like before, move the cursor below this line so that it’s in between two lines.
But this time, look at the left end of the gray line and you’ll see a “+“; click on it with the mouse, then select “Plain Text”.
Then 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?

Getting Help

If you want help with a particular command, like the one we just discussed, “N”, then you can learn about it this way:

?N

You’ll see a quick summary and then “>>”.  
Try clicking on the “>>”.  A new window should pop up with more details and a few examples.  

The examples are very useful: copy them and modify them to suit your needs.

Try it with “Plot3D”, copying an example to here.

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 correctly.

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.  The problem is that some equations actually don’t have nice “closed form” solutions, in which case, you obviously won’t be able to find a clean answer.
In this situation, we can look for approximate decimal solutions instead:

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

Two of those are complex numbers.  Here’s how to 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.

Calculus: Limits, Derivatives, Integrals

Try these:

Limit[(2x^3+100x)/(7x^3-20x^2),{x->Infinity}]

D[Sin[x^100],x]

Integrate[x^2,x]?Integrate

Integrate[x^2,{x,1,Pi}]

If you remember your Calculus I, you can probably figure out how they work.  (If not, try “?Integrate”, or just ask!)

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 it can be quite convenient, and you’ll probably see it being used.

(3)  Remember to save 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).

Want more?

Want to learn more?  Start here:
	http://reference.wolfram.com/mathematica/tutorial/GettingStartedOverview.html
To better understand the working environment, look here:
     	http://reference.wolfram.com/mathematica/tutorial/WorkingWithTheNotebookInterfaceOverview.html

Or if you prefer to watch a video, start here:
     	http://www.wolfram.com/broadcast/screencasts/first-ten-minutes/
 and continue here:
	http://www.wolfram.com/broadcast/screencasts/handsonstart/
 
 If you’re still reading, you probably also need to be aware of potential problems with any sort of computing technology.  A decent starting point:
	http://www.stewartcalculus.com/data/default/upfiles/Calc_7E_liesmycomp_stu.pdf

Exercise

Which is bigger in the long term: exponential functions (with base a >1) or polynomials?  We study this question via a few computer experiments.

(We already know how to compare two polynomials. For example, in Calculus I you learned how to find the infinite limit of a function like   ((3 (x^4) - 5(x^2))/(100 x + 7 (x^4))).  Which is really just a question about comparing the numerator with the denominator for very large values of x.)

(a)  The functions f(x)=(2^x) and g(x)=(x^2) intersect in three places.  Find or estimate all three points of intersection.  Then plot both functions on the same graph, with all three points of intersection clearly visible.

(b)  This time you’ll compare f(x)=(2^x) and h(x)=(x^5).  
       - Which one is bigger for small x-values like x = 2, 3, 4, 5?  (Support your answer by computing some values and illustrating with a plot.)  
       - Find the largest value of x such that  (2^x) =(x^10) .  
       - To the right of that final intersection point, which function is bigger?  (Support your answer with computaions and/or a plot.)  

(c) Repeat part (b) using (((1.05))^x) and (x^2).
 
(c) Repeat part (b) using (((1.01))^x) and (x^50).

Once you have performed all of the above experiments, write a brief lab report, in which you organize your answers and discuss anything interesting you observed.  (If you didn’t notice any interesting pattern at all, you are doing it wrong - ask the TA or instructor for help!)  You will be graded on correctness, language, and clarity.

You can work with one other person, handing in a single lab, and sharing a single grade.  However, anyone who does not physically attend the lab (or who is more than a few minutes late) is not allowed to collaborate in this way.  
Outside of this partnership, you cannot share any written work with anyone else.  Discussion, however, is unlimited and encouraged.

The exercise is due in one week, at the start of class on Thursday.