Beruflich Dokumente
Kultur Dokumente
edu/curgus/
http://faculty.wwu.edu/curgus/Courses/Mathematica/Mathematica_12.html
This file is in fact for any Mathematica version from 8 to 12.1
Mathematica at WWU
notes by
Branko Ćurgus
Mathematica at WWU
These notes are written for
Getting started
You need to find a campus computer with Mathematica 12 installed on it (for
example computers in BH 215). You will find Mathematica 12 as follows (this
sequence might differ on different campus computers)
Start -> Programs -> Math Applications -> Mathematica 12
When Mathematica starts up, it gives you a blank Mathematica file. A
Mathematica file is called a notebook. As you start writing in a blank notebook
Mathematica forms a cell; a thin blue frame will appear on the right of the
notebook window. This is a cell bracket.
Start writing something simple, 2+2 for example. After writing 2+2, hold
down the Shift key and press Enter (this is abbreviated as Shift+Enter). This
tells Mathematica to process your input. The result will appear in an output
cell. Notice that the thin blue brackets of the input and output cells have
differently designed tops.
To add a new cell place the pointer in the notebook window below the last cell.
Make sure that the pointer becomes a horizontal I-beam. Then click and a cell
insertion bar will appear. Only then start typing. By default, new cells are input
cells. To change the style of a cell: click the cell bracket; this highlights the
bracket; then choose style from the menu item Format -> Style
Selected
Processing math: 0%
videos from wolfram.com
http://faculty.wwu.edu/curgus/Courses/Mathematica/Mathematica_12.html[8/17/2020 12:41:53 PM]
Mathematica at WWU
A good place to start with learning Mathematica are the following videos from
wolfram.com. Please read my comments as well. One general comment: In the
movies below please ignore everything that relates to free-form input. Your
homework notebooks should not contain free-form linguistic input. Only
Mathematica language is allowed for calculations on Assignments.
Movies below are Wolfram movies posted on YouTube. The reason for linking to
YouTube is that I liked these movies and I could not find them at the Wolfram site.
However, you can find many more useful movies at the Wolfram site Getting Started
with Mathematica.
f[x_]:=x^2+x+1
My recommendation is to use more complicated names for functions to
avoid duplication and to always clear the string that you are using as the
name. So, a better way here would be to write
Clear[myf,x]; myf[x_]:=x^2+x+1
Brackets in Mathematica
Before presenting a few examples of how to use Mathematica it is important to
notice that there are five types of brackets in Mathematica:
(The double square brackets will appear nicer if you type Esc[[Esc
and Esc]]Esc.)
(* *) -- starred parentheses -- for comments in Mathematica code --
(* this is a comment *)
Each example below uses some (or all) of these brackets. Please pay
attention and make each example a learning experience.
Arithmetic in Mathematica
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.
For more see Mathematica's
Arithmetic page.
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[
http://faculty.wwu.edu/curgus/Courses/Mathematica/Mathematica_12.html[8/17/2020 12:41:53 PM]
Mathematica at WWU
{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:
Manipulate[
Plot[Evaluate[Sin[x + s]], {x, 0 , 2 Pi},
PlotRange->{{0 , 2 Pi},{-1.2,1.2}}] ,
{s, 0, 2 Pi, Pi/20, ControlPlacement -> Top} ]
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_]:= 1- 2 Exp[(x^2)(Cos[x]-1)]
But much more complicated functions are common. For example functions that
include graphing commands.
The zeros of the function cannot be determined exactly. The first positive zero
is close to 1. To find its numerical approzimation we use
Usually we do not need this level of precision, but it is nice to know how to
achieve it. Other positive zeros come in pairs around even positive multiples of
\pi. Here is the pure function combined with Map[] which finds the first 12
pairs
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[];
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
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:
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
I believe that I am the first one to come up with a closed form expression for
this sequence. 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]])