Below we
point out a few more characteristics in some Mathematica commands
that you will use during the year. Execute each command, notice its output,
and read the explanation that follows.
y
Here we have defined y as
we had earlier. Now consider
xy
Mathematica considers xy as
a new variable, since we "forgot" to put a space between x and y.
Now
execute:
x y
Mathematica remembers the definition of y and
substitutes it.
Expand[%]
In this Expand command, the percent sign
(%) stands for "the last result". Therefore, the product x y is expanded
here to get a fourth degree polynomial.
Factor[%]
Likewise, this
command factors the last result. Finally, a bit more subtle command:
y
/. x -> 2
The effect of this command is to evaluate y at x =
2.
- It illustrates a very important feature of
Mathematica. You should read this command as "evaluate y subject to
the rule: x is set to 2". The operator /. is made up by a slash / followed
by a period (.) and the arrow is a "hyphen" followed by a "greater than"
sign (>). The spaces after the y, before the x, and before the 2 are NOT
necessary here, but they help in the readability of the command.
Defining a function in Mathematica
As you probably
noticed, the last example is an awkward way to evaluate y at x = some
value. An easier way to do such evaluations is to use the "function"
notation. The following is an example of a function definition for f :
f[x_] := x^2 + 1
There are several items to note in the above
command that are peculiar to Mathematica and that you must include
in this definition to make f a legitimate function:
- square brackets must be used to enclose the
argument of f,
- an underscore (shift on the hyphen) follows the
argument inside the brackets, and
- the assignment operator :=
replaces the usual equal sign.
Examples using a defined
function
Some advantages of the function notation are shown in the
examples below. These examples give you an idea of the power you have in
Mathematica when f is defined as an actual function. The original
argument, x, can be replaced by any numerical value or symbol. EXECUTE the
following commands, one at a time, starting with the definition of f .
f[x_] := x^2 + 1
f[x]
f[3]
f[-3]
f[-12]
f[p]
f[cat]
f[t^3+1]
Expand[%]
As you can see, the original
argument, x, can be replaced by ANYTHING! For example, with the argument
t^3 + 1 we obtained a sixth degree polynomial (the "composition" of the
functions x^2 + 1 and t^3 + 1).
Working with a Rational
Function
You can form a so-called "rational function" by taking the
quotient of y and f[x]. Execute the following command to define the ratio
of y and f[x], and then execute the second command to plot the function.
r = y/f[x]
Plot[r, {x, -20, 20}, PlotRange -> All]
You can see
here that r has the same general shape, for "small" values of x, as did y.
(Why is this?) Also notice that for "large" values of x the graph of r
looks linear. (Why is this?) To explore the second question graphically we
plot functions r and x-1 on the same graph as follows. Execute the command:
Plot[{r, x-1}, {x, -40, 40}, PlotRange -> All]
In this tutorial,
you have seen demonstrated two phenomena that are quite common:
- Most functions look linear if you "zoom in"
very close; that is, if you look at the function over a very small (domain)
interval.
- Some (but not all) functions look linear if you look at
them over a large (domain) interval, or "zoom out".
The concepts
of "very small" and "very large" play central roles in calculus, as you
will see this semester.
http://math.colgate.edu/mathlab/additional.html
Revised: March 1, 1996.
Questions to:
valente@colgate.edu
Copyright 1996 © Colgate University. All rights reserved.