Math 161 Lab: Functions and Their Graphs in Mathematica  



In this lab you will be introduced to Mathematica. You will learn how to work with Mathematica expressions, and how to define and plot functions. Work in groups of two at one computer and turn in out set of solutions to the exercises for your group (with both names on it).

1  An Introduction to Mathematica Functions

We have mentioned several times already the importance of functions for calculus. Mathematica is a computer program for doing mathematics and functions play a very important role in Mathematica, too.

1.1  Built-in Functions

Of course, Mathematica knows about some of the most common mathematical functions, like sin(x), cos(x), ln(x), Öx, ex, and many more. When using Mathematica's built-in functions there 2 very important things to remember:
A
ll built-in functions are spelled with the first letter CAPITALIZED.
T
he inputs to Mathematica functions are placed inside SQUARE BRACKETS.
So, for example, if you want to know the square root of 19, you can type Sqrt[19]. Sqrt is the Mathematica name for the square root function (notice the capital S), and the input (19) is placed inside square brackets.

1.2  Built-in Constants

Mathematicaalso knows a number of constants like p (Pi) and e (E). It even knows about infinity (Infinity) and the complex number i (I). Notice that built-in constants are also capitalized.

1.3  Arithmetic Operations

The arithmetic operations in Mathematica are probably similar to what you have used on a graphing calculator:
+, -, *, /
are used for addition, subtraction, multiplication and division.
R
ound parentheses - ( and ) - are used for grouping.
T
he caret symbol - ^ - is used for exponents
P
arenthesis and the multiplication symbol (*) can be omitted in the same situations were we omit them in mathematics, but it never hurts to use them just to make sure Mathematica knows just what you mean.

Examples

Try these examples. Note that you should see a horizontal line across the notebook before you begin (if not, click on the notebook) and that you must type ENTER (not return) to get a cell to execute. (Shift-return works, too.)

Math              Mathematica
Ö{4+5 p}              Sqrt[4 + 5 Pi]
sin(p/3)              Sin[Pi / 3]
ln(e2 + e)              Log[E^ 2 + E]

Exercises

Copy down exactly (with pencil on paper) the output Mathematica gives to each of the examples above. Is the output what you expected? In just a moment you will learn how to get the output you probably expected.

Copy down exactly (with pencil on paper) the result Mathematica gives when evaluating the following: cos(p2), ([3/4])5, (0.75)5.

Note that the last two answers are different. Why do you think this is?

2  Some Special Functions

2.1  N[]

When it is able to, Mathematica will generally give exact answers rather than approximate answers. If you want a decimal approximation and Mathematica has given you an exact value, you can use a built-in function to request an exact value from Mathematica. The name of this function is N[]. For example, to get an approximation for [55/34], enter N[55/34]. Try it.

If you get an exact value and want a decimal approximation, you can use a short-cut: N[%] will give the numerical approximation to the last output. (% always stands for the last output in Mathematica.)

Exercise

Use N[] to get decimal approximations for each of the items in problem 1. (Hint: you can go back and edit the input cells you entered there; that saves some retyping.)

2.2  ?

The question mark can be used to get information about functions. If you type ?N, for example, Mathematica will tell you something about the function N[]. ??N will give somewhat different information. And ?N* will give you a list of Mathematica functions that begin with the letter N. If none of these methods give you the information you need, you can also look in the extensive online help menus.

Exercises

What is the fourth function in the list of Mathematica functions beginning with the letter A? What does it do?

Use the information from ?N and ??N to determine the 496th through 500th digits of p (after the decimal point).

The list of the Fibonacci numbers begins 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,89 ... Each term is the sum of the preceding two. These numbers have many interesting properties.

F
ind numerical approximations to the quotients of consecutive terms [8/5], [13/8], [21/13], [34/21], and [55/34]
W
hat do you notice about these numbers?
D
oes Mathematica have a built-in function to compute Fibonacci numbers? Check by entering ?Fib* to see if any functions begin with those letters. If there is such a function, compute the ratio of the 26th Fibonacci number divided by the 25th Fibonacci number. How does this ratio compare to the ones above?

2.3  Plot[]

To see the graph of a function, use the Mathematica function Plot[]. Here is an example:

Plot[Sin[x],{x, -2*Pi, 2*Pi}]
Type it in and see what you get. Remember to hit ``Enter'', not ``return''.

Plot[] is considerably more complicated than any function we have seen so far. It can take several inputs, but the basic form is like the example above:

T
he first input provides an expression to plot - Sin[x] in our example.
T
he second input tells what variable to use for the input to the function and what portion of the graph to display. In our example, the input variable is x and the the plot includes all values of x between -2p and 2p.
There are a number of other options to the Plot[] command controlling things like colors, thickness of lines, aspect ratio, range of output values to display (in case you don't like the choice Mathematica makes for you), etc. We will introduce some of these later as we need them. Consult your Mathematica Quick Reference Sheet, ??Plot, or the help menus to learn more.

Exercise

Make a plot of the function defined by the equation f(x) = x2 + 3x -1 on the interval [-3,3]. Click on the bar to the right of the graph. This selects that cell of the notebook. Now print out just this graph by using ``Print Selection'' from the print menu.

3  Defining Your Own Functions

You can also define your own functions in Mathematica. This is one of the most important things to learn to do correctly. The syntax is not difficult, but must be followed precisely to avoid errors. Read the section from your Quick Reference on defining functions. Notice especially
Always clear the function before defining it.
This will avoid bad interactions with previous definitions.
An underscore must follow the input variable.
This is how Mathematica identifies the input variable.
A
s always with Mathematica functions, be sure to use square brackets.
U
sually you will want to use ``colon-equals'' (:=) between as shown to define a function. We won't discuss here the other kinds of equals signs in Mathematica, but don't use another kind of equals sign unless you are sure you know what that would mean.
Y
ou do not need to use 1-letter names. You don't need to capitalize (In fact, it is a good idea not to for your functions; that way you can tell yours from Mathematica's.) You do not need to use x as the input variable. It is perfectly acceptable to do something like
area[r_] := Pi * r^ 2

I
f you have trouble with a function, try the following two diagnostics first:
[
a)] Type ?functionname to see what Mathematica is thinking about your functions.
[
b)] Enter a particular value (e.g., f[1]) and see if the results are reasonable

Exercises

Only the last item of this problem requires you to turn anything in, but you will need to do the other parts to get to that point.
D
efine the function g(x) = 3x2 - x6.
T
ype ?g to see what Mathematica tells you about g. Check to make sure everything is in order.
H
ave Mathematica compute g(1.1) and g(11/10). How do the answers compare?
P
lot g(x) on the interval [-1,2]. Hint: use
Plot[g[x], {x,-1,2}]
Be sure you understand how this works.

C
lick on the graph. Your cursor should change to the graphics cursor. Now hold down the ``apple key'' as you move the mouse around on the graph; the coordinates for the point the cursor is over are displayed at the bottom of the screen. Use this to approximate when g(x) = 0. We will learn better ways to have Mathematica help us determine when a function has a zero-value in a future lab.
Now let \ds g(x) = [(x7 -6x5 + 11x3 - 6x)/(1+x2)]. (Be sure to use Clear[] to get rid of the old definition of g.) Use Mathematica plots to estimate the values of x for which g(x) = 0. Such a value is called a root of the function. Zoom in until you can estimate each root to within 0.001.

Hints: Try an initial interval of [-10,10]. There are 7 roots of this function. In order to get within 0.001, you will need to zoom in on some of the roots. Others you can probably just guess and check. For example, it looks like 1 is a root. Enter g[1] to check if this is indeed the case.




File translated from TEX by TTH, version 2.78.
On 10 Sep 2001, 16:39.