Brief Introduction to Mathematica 
Michael Pelsmajer, January 2012

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

General Instructions:

Read the text. 
Every time you see a line that is written in this font: Move the cursor to that line (mouse-click on it, or use the arrow keys), and then press Shift-Enter on the keyboard.  
When you have questions, raise your hand and ask!!

Arithmetic

Try these:

2+5

2-5

2*5

2/5

2^5

Sqrt[9]

9^(1/2)

9^(1/4)

Abs[2-5]

Sin[Pi/3]

Capitalization matters: x does not equal X, and Mathematica will not work correctly with these two:

sin[Pi/3]

Sin[pi/3]

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, these three:

Sin(Pi/3)

Sin{Pi/3}

[6-3]^2

Entering two numbers separated by a space is assumed to be multiplication. Try it!

Hey you!  Click on the blank line above, and then, on the keyboard hit three keys: 2, then [spacebar], then 5.  Then Shift-Enter.  (Do it now!)
You should see “2 x 5” and then “10”.

Exact vs. approximate 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

(When you click and Shift-Enter, you are telling Mathematica what f(x) should be, but you won’t see much happen - perhaps you will notice a small color change.)

Now try these:

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

Now try these:

dog[5]

f[dog[t]]

Plotting functions

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

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

Curly braces “{}” are used for the domain and also for multiple functions.

A more flexible way to combine plots is to 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.  So, let’s go for approximate decimal solutions:

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

Again, but ignoring the complex 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.
Below “Basic Commands”, you should see “2D”.  (If not, click on Basic Commands.)
Click on "2D".  Now you should see "Plot”.
Next, click in the open space below this line, and then click on "Plot".  
(Do it now!)

You should see “Plot[function,{var,min,max}]” appear above, with “function” highlighted.
Click on the square root within the Basic Math Assistant window, then click on ArcCos, and then type: x.  
Now replace var,min,max with x, -1, 1  (by clicking and typing).
And now Shift-Enter.  
(Do 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 other people use it a lot.

(3) I do not expect you to memorize everything in this worksheet.  Instead, keep it handy, and in the future refer back to it.

Exercises

Exercise 1: 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.

Through the use of the Solve command.

Through the use of the Reduce command.

Through the use of the NSolve command.

Exercise 2: 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 3: Secant lines

Find the slope of the line that intersects y=(Sqrt[5-x]) at x=4 and...

and at x=4.5

and at x=4.1

and at x=4.01

and at x=4.000178

and at x=3.9

and at x=3.99

and at x=3.9999723

Exercise 4: Recommended Movies

Get some headphones and/or a laptop and watch this movie:
http://www.wolfram.com/broadcast/screencasts/first-ten-minutes/

Optional: If you are interested in learning more, these videos expand on those topics: 
http://www.wolfram.com/broadcast/screencasts/handsonstart/