-
You can use Mathematica as a calculator. For example to get the circumference of a circle circumscribed about a square with side 1, you type
Pi*2^(1/2)
and Shift+Enter. To get a numeric approximation type N[Pi*2^(1/2)]
and, as always, Shift+Enter.
-
Mathematica has hundreds of built in functions. Function names are always capitalized, for example the standard ones
Exp[x], Log[x], Sin[x], Cos[x], Abs[x], ...
or some which are for some mysterious reason absent from our curriculum:
Sign[x], Floor[x], Ceiling[x], UnitStep[x], ...
Please see the menu item
Help -> Built-in Functions -> Mathematical Functions
-
I view all Mathematica commands as functions with several variables. Some examples are below.
-
To plot one period of the sine function
Plot[Sin[x],{x,0,2*Pi}]
Here Sin[x] is one variable, and the domain {x,0,2*Pi} is another variable. But this function can have many more variables. Options[Plot] will give you all.
-
Plotting two functions with several options:
Plot[
{Sin[x],Cos[x]}, {x,0,2*Pi},
PlotStyle->{{Thickness[0.007],Blue},{Thickness[0.007],Green}},
PlotRange->{-2,2}
]
-
Plotting a function of two variables:
Plot3D[ Sin[x]*Cos[y], {x,0,2*Pi}, {y,0,2*Pi} ]
plots a function of two variables.
-
Find a derivative
D[Sin[3 x]^2 + Cos[4 x], x]
finds the derivative with respect of x.
-
Simplify[D[Sin[3 x]^2 + Cos[4 x], x]]
finds an algebraically simpler form of the previous answer. Sometimes
FullSimplify[] gives more desirable results.
-
Integrate[-4 Sin[4 x] + 3 Sin[6 x], x]
finds an antiderivative.
-
Table[Sin[x + s],{s, 0, 2 Pi, Pi/20}]
produces a list of forty-one shifts of the sine function.
-
To plot all the functions from the previous item we need to use Evaluate[]:
Plot[ Evaluate[Table[Sin[x + s],{s, 0, 2 Pi, Pi/20}]], {x, 0 , 2 Pi} ]
-
Notice that exchanging the positions of Plot[] and Table[] gives a different but useful result:
Table[ Plot[Evaluate[Sin[x + s]],{x, 0 , 2 Pi}], {s, 0, 2 Pi, Pi/20} ];
Also notice ; at the end of the input above. This suppresses Mathematica telling us that the outputs are Graphics cells.
-
Often it is useful to define our own functions. It is a good practice to clear the name that you use for the function and the name of the variable before the definition:
Clear[ff,x];
ff[x_]:= Exp[(x^2)(Cos[x]-1)]
But much more complicated functions are common. For example functions that include graphing commands.
-
Notice the use of semicolon (; ) in a cell. Its purpose is to separate different commands used in the same cell. Contrast this with the use of comma (, ) which is used to separate variables in a command which takes several variables.
-
Also notice the use of colon (: ) in the definition of function. For the meaning of := check SetDelayed in the help file. Basically, if you use :=, then Mathematica will remember the formula and evaluate it only when we use the function later on. I prefer to use := in definitions of functions, in particular complicated ones. Often, like in the simple example above, := and = are interchangeable. However, this is not always the case. Here is one example in which = yields a meaningful function while := does not make sense: Say I want to graph three random lines with y-intercepts and slopes in the interval [0,1]. I can define
Clear[f1, f2, f3, x];
f1[x_] = Random[] + x*Random[];
f2[x_] = Random[] + x*Random[];
f3[x_] = Random[] + x*Random[];
Now you are thinking that I defined the same function each time. Try graphing:
Plot[
{f1[x],f2[x],f3[x]}, {x,-5,5},
PlotStyle->{{Thickness[0.007],Red},
{Thickness[0.007],Green}, {Thickness[0.007],Blue}},
PlotRange->{-5,5}, AspectRatio->Automatic
]
and you will see that these are different functions since in each definition Mathematica assigned different random numbers as slopes and y-intercepts. I am writing all of this to point out that
Clear[g,x];
g[x_]:= Random[] + x*Random[];
does not make sense! With this definition Mathematica selects random numbers as slope and y-intercept whenever you calculate, for example, g[1]. Try evaluating it twice. You get different numbers! Thus, as defined, g[x] is not a function.
-
The sinc
function is not in Mathematica's library of functions. (In fact, it has been added in version 6.0.) To define it set
Clear[Sinc,x];
Sinc[x_]:= If[x==0, 1, Sin[x]/x]
Clearly Sinc is not a bijection. However, in the spirit of inverse trigonometric functions one can define ArcSinc[x] for certain real values of x. This is a nice exercise is using Interpolation[] function in Mathematica.
-
By default Mathematica warns us of similar spelling. I do not find this useful. Therefore I switch this feature off by Off[General::"spell"]. I just copy the first part of the warning message, that is General::"spell" and paste it into Off[] command.
-
Functions whose domain is the set of positive integers can be defined recursively. Here is an example. Why I choose this long name Repeat will be clear after you define and explore this function.
Clear[Repeat, n];
Repeat[1] = 1;
Repeat[n_]:= Repeat[n - Repeat[n - 1]] + 1
But much more complicated functions are common. For example functions that include graphing commands.
-
If you try to calculate Repeat[100] you will notice that Mathematica takes quite a long time to calculate this number. Try
Timing[Repeat[60]]
On my computer it takes 2.735 seconds to calculate that Repeat[60] = 11. The reason for this is that as defined above the function Repeat[] does not
remember any values that it calculates. We can make Mathematica remember all the function values it finds by changing the syntax in the definition:
Clear[Repeat, n];
Repeat[1] = 1;
Repeat[n_]:= Repeat[n] = Repeat[n - Repeat[n - 1]] + 1
You will easily notice that this definition is much faster.
- Test what this function does using
Table[Repeat[k], {k, 1, 106} ]
or by plotting its graph (which consists of points with integer coordinates)
Show[
Graphics[{
{PointSize[0.015], Table[Point[{k, Repeat[k]}], {k, 1, 106}]}
}],
PlotRange -> {{0, 107}, {0, 15.5}},
Frame -> True, ImageSize -> 400
];
But sometimes we want to control more details of the plot:
Show[
Graphics[{ (* the start of the graphics, it is a list *)
{ (* the grid lines are in this list *)
Thickness[0.003], GrayLevel[0.8],
Table[Line[{{0, j}, {120, j}}], {j, 1, 15}],
Table[Line[{{j(j + 1)/2, 0}, {j(j + 1)/2, 16}}], {j, 1, 15}]
}, (* the next list are the points on the graph*)
{PointSize[0.015], Blue, Table[Point[{k, Repeat[k]}], {k, 1, 106}]}
}], (* the end of the graphics *)
PlotRange -> {{0, 107}, {0, 15.5}},
Frame -> True,
FrameTicks -> {Table[j(j + 1)/2, {j, 1, 15}], Range[1, 15], {}, Range[1, 15]},
ImageSize -> 400
];
Please notice the indentation of the parts of the command. That helps me keep me identify the code for each part of the picture. You can copy-and-paste (^C then ^V) this gray box into your Mathematica notebook.
-
I used the Repeat[] function above as an illustration for a recursive definition. But there are explicit formulas that define the same function. Such formulas are called "closed form expressions" for a recursive sequence. Here are few closed form expressions for Repeat[]:
Clear[Rf1, Rf2, Rf3, n];
Rf1[n_]:= Round[Sqrt[2*n]];
Rf2[n_]:= Floor[(1/2) + Sqrt[2*n]];
Rf3[n_]:= Ceiling[(-1/2) + Sqrt[2*n]];
The following table is a quite convincing evidence of the above claim:
Table[
{k, Repeat[k], Rf1[k], Rf2[k], Rf3[k]},
{k, 1, 100}
]//TableForm
-
I just want to mention that the determining a closed form expression for a recursive sequence is a hard problem. For example, the creator of Mathematica Stephen Wolfram in 2002 introduced the following slight modification of the Repeat[] sequence:
Clear[W, n];
W[1] = 1; W[2] = 1;
W[n_]:= W[n] = W[n - W[n - 1]] + 2
No closed form expression for this sequence is known. This is no longer true. On February 10, 2011 I found the following closed form expression for the above sequence
Clear[Wf1, n];
Wf1[n_]:= 2*Floor[Sqrt[n]]-(-1)^(n+Ceiling[Sqrt[n]])
-