Introduction to Mathematica
For Math 75, Mathematical Analysis I

Instructions for Math 75A:
We will spend two class periods working with Mathematica. To get full credit (20 points) for this lab assignment, you must perform all the operations described below and do all the exercises in sections Getting started throught Limits (section Derivatives is for Math 75B). Try to complete at least half of these sections (or as much as you have time to) during the first lab. Save your file and send it to the instructor. Your work will be checked and you will receive comments as well as your "current" grade (which will indicate how much you've completed correctly). If your grade is less than 10 points (which is half of the full amount), I suggest that you go to the computer lab at some point during the semester and work on your own to catch up. (Feel free to send your file to the instructor at any point for comments and grade update.) Then you'll be able to finish it during our second lab hour. In any case, you have to complete the assignment before the end of the semester to get full credit. When you are done, save your file and send to the instructor.

The number of points each section is worth is given below:
Getting started - 1 pt
Arithmetic calculations - 4 pts
Functions - 4 pts
Equations - 2 pts
Graphics - 3 pts
Limits - 6 pts

Instructions for Math 75B:
We will spend two class periods working with Mathematica. Each part is worth 10 points.
First class: do everything in section Derivatives except the last exercise. Save your file and send it to the instructor, within 1 week of the lab. Your work will be checked and you will receive comments as well as your grade. If your grade is less than 10 points, you may (and are encouraged to) fix all mistakes and resubmit your file by the last day of classes. Your grade will be updated.
Second class: do the last exersice in section Derivatives. As instructed in the exercise, you have to do all the calculations by hand (on a piece of paper) first, and then use Mathematica to check your derivatives, roots, asymptotes, etc. If you find that you made a mistake in your calculations, fix it. Submit both your paper and your file, by the last day of classes.


Getting started

Open a Mathematica window.
Try typing 2+3 and hitting "Enter" - nothing happens? Mathematica allows more than one line of input, so it preserves "Enter" for getting to the next line. So instead, to process your input, you have to press Shift and Enter (simultaneously). Now you should see (wait a few moments if it's slow at first) something like:
In[1]:= 2+3
Out[1]= 5

Arithmetic calculations

Use +, -, *, / for addition, subtraction, multiplication, and division,
x^y denotes "x raised to the power y".

Try 1/3+2/7. The output is a fraction.
If you want the answer to be given as a decimal, type N[1/3+2/7] ("N" stands for "Numerical result").
To compute the answer to 20 decimal places accuracy, type N[1/3+2/7,20].

Exercise: Find the cubic root of 2 accurate to 25 decimal places.

Exercise: Calculate as an ordinary fraction, and also as a decimal:
math1
Note: You have to enter all necessary parentheses! You should get -22/15 and -1.46667.

Building computations: To use the last result in a new expression use %, and to use the nth to last result use %%...% where there are n % symbols.
In[10]:= 4*13
Out[10]= 52
In[11]:= 66-% - this computes 66 minus last answer, i.e. 66-52
Out[11]= 14
In[12]:= (-2)^5
Out[12]= -32
In[12]:= 35+% - this computes 35 plus last answer, i.e. 35+(-32)
Out[12]= 3
In[12]:= %%%*% - this computes 3rd to last result times last result, i.e. 14*3
Out[12]= 42
and so on.

Exercise: Finish the calculation of the above expression doing only one operation at a time.

You can assign values to variables and then evaluate expressions of variables:
In[20]:= x=5
Out[20]= 5
In[21]:= y=6
Out[21]= 6
In[22]:= x*y
Out[22]= 30

To save space, you can define variables on one line:
x=5; y=6;
The ";" will suppress the output.
Also, if you need to evaluate several expressions, you can put them in a list. A list is specified with set braces {...} and the elements of the list are separated by commas.

Exercise: Execute the following: { 2x, y-1, x*y, x y, xy, x^y }.

Notice that xy was not evaluated. That's because Mathematica thinks it is one variable with name xy and not the product of x and y. You do not need to use a space or "*" when multiplying a variable by a number, but you do need a space or "*" when multiplying 2 variables or 2 numbers since xy can be another variable, just as, say, 56 is another number and not a product of 5 and 6.

Note: When you finish working with variables you should "remove" them (from the computer memory). The variables x and y used above are defined and have values. If you use x or y again later on, forgetting about the above, unexpected things can happen, e.g. the software will evaluate them using the above values when you don't mean to work with the above values any more. Alternatively you can remove the symbols just before you use them again.

To find out the value of x, type ?x.

To remove x, type Remove[x].

Functions

Here is a list of the most commonly used functions: Sqrt[x], Abs[x], Exp[x], Log[x], Sin[x], Cos[x], Tan[x], ArcSin[x], ArcCos[x], ArcTan[x], n!.
Notice that all math function names begin with capital letters (sometimes names made from more than one name, like ArcSin have capitals in the middle too) and the arguments in functions are enclosed in square brackets. Use Pi, E, and I for pi=3.1415926..., e=2.7182818..., and the imaginary number i.

Exercise: What is tan of 45 degrees? arccos(1)? arcsin(1/2)? Use Mathematica to check your answers.

Exercise: What happens if you type Sqrt[-4]?

If you need to calculate the values of the same function at several points, you can define the function, and then plug in the points:
f[x_]=x^3-7x+3;
f[1]
f[2]
f[3]
f[4]

Again, to save space, you can use a list:
{f[1], f[2], f[3], f[4]}.

Here are some useful packages for working with polynomial and rational functions: Expand[...], Factor[...], Simplify[...], Together[...] (puts over a common denominator).

Exercise: try the following.
Expand[x(x-5)(x^2+1)]
Factor[x^16-1]
Simplify[(x+2x^2)/(1-x^2)+(1-2x)/(1-x)]
Together[1/(x-1)+1/(x+1)]

Equations

Try Solve[x-3==2x+5,x] and Solve[x^3-x==0,x]. Note the two "=". "x" at the end means that we want to solve the equation for x.

Exercise: Solve the equation 1/(x-2)=x+1. Use Mathematica to check your answer.

Graphics

Plot[f,{x,a,b}] graphs the function f from a to b.

For example, to graph sin(x) from 0 to 2pi, type Plot[Sin[x], {x,0,2*Pi}].

Alternatively, you can define a function f(x) and then plot f(x):
f[x_]=Sin[x];
Plot[f[x],{x,0,2*Pi}]

Exercise: Plot the function math2 on the interval [-4,4].

To plot several functions simultaneously, list them in "{ }":
Plot[{Sin[x],Cos[x]},{x,0,2*Pi}].

Exercise: Above we solved the equation 1/(x-2)=x+1. Now plot 1/(x-2) and x+1 on the same screen for x from 0 to 4. You will see that the two graphs intersect. However, it appears that they intersect again outside of our interval. Increase the range to see both intersection points.

Limits

To evaluate the limit, use Limit[function,variable->value that the variable approaches]. You can define a function f(x) first and then use f[x] inside the limit:

Remove[f];
f[x_]=(x^2-x-2)/(x^2-4);
Limit[f[x], x->2]

or you can write the expression inside the limit:

Limit[(4-x)/(2-Sqrt[x]), x->4]

Exercise: Try the above limits and also plot the graphs of those functions. Check that the answers to the limits agree with the graphs.

You have to be careful when the limit does not exist or when it is infinite. Try the following:
Remove[f];
f[x_]=1/x;
Limit[f[x], x->0]
Now plot the graph of f(x)=1/x. Does the limit as x approaches 0 exist? Try the one-sided limits:
Limit[f[x], x->0, Direction->-1] is the limit as x approaches 0 from the right (notice that when x approaches a number from the right, x is decreasing, so x is changing in the negative direction. That's why we have to use Direction->-1).
Limit[f[x], x->0, Direction->1] is the limit as x approaches 0 from the left (so x is increasing, i.e. x is changing in the positive direction).

To evaluate limits as x approaches infinity or negative infinity, use Limit[f[x], x->Infinity] and Limit[f[x], x->-Infinity].

Exercise: Evaluate the following limits. Also, plot the graphs of the function and see how the answers you are getting agree with the graphs.
math3
Note: As you know, the last limit does not exist, and the limit of ln(x) only exists as x approaches 0 from the right since ln x is only defined for positive x. But try to compute all the limits and see what happens.

Derivatives

As with the limits, one way to compute the derivative of a function is to define the function f(x) and then use f'[x]:
f[x_] = 5x Sin[x];
f'[x] computes the first derivative,
f''[x] is the second derivative, and so on...
f'''''[x] is the fifth derivative...

Note: The "prime" notation will not work with something like y':
y=5x Sin[x];
y'

Another way to compute derivatives is to use D[expression, variable]:
D[5x Sin[x], x] or
D[f[x], x]
This notation is more convenient if you want to compute higher derivatives. The nth derivative is D[expression, {variable,n}]. E.g. to find the fifth derivative of f, use D[f[x], {x,5}].
You can also use y with this notation: define y=5x Sin[x], and then use D[y,x] to find y', and D[y,{x,5}] to find the fifth derivative of y.

Exercise: Compute the derivative of math4 and then simplify it.

Exercise:

  • Plot the graph of
math5 from -8 to 8.
  • Based on the graph of the function, sketch (by hand, on a piece of paper) the graph of its derivative. (Recall that at each point, the derivative is the slope of the graph of the original function.)
  • Use Mathematica to compute the derivative of f(x), and plot the graph of the derivative. Compare your graph and the graph you obtained using Mathematica.
  • Do the same for the second and third derivatives.

Exercise: Let math6 .
Do the following by hand first, and then use Mathematica to check your answers (whenever this is possible):
This page was last revised on October 26, 2009.