You are on page 1of 54

Introduction to Mathematica's basic philosophy

ü What is Mathematica?

ü One view...

ü Use dichotomy: application and language

ü An application (use raw, immediately, yourself)

ü A language (encapsulated, extend use, for you or others).

But really...what is Mathematica?

ü A Complete system
This esample shows how different technologies and Mathematica features can be combined with a minimum effort and high
impact result.
Here I have a time series of tickers from the Stock exchange. I have all series in a database, then I use the front end features
to simplify the query and visualization of data.
2 MathTour2008-NorwayAndDenmark.nb

ü NOTE: this example doesn't work without the database (not provided). It has been left just
as a code example.
In[1]:= << DatabaseLink`;
conn = OpenSQLConnection@JDBC@"odbc", "Ticker"DD;

MyButtonFunction@label_StringD :=
Module@8<,
Get@ToFileName@"C:\\temp\\milano",
"D" <> SQLExecute@conn, "SELECT ∗ FROM Ticker WHERE nomeTicker = '" <>
ToString@labelD <> "'"D@@1, 2DD <> ".mx"DD;

8"First report" −>


TabView@

Manipulate@
DateListPlot@8Tooltip@Take@Matrice@@4DD, −mDD, Tooltip@Take@Matrice@@3DD, −mDD,
Tooltip@Take@Matrice@@2DD, −mDD, Tooltip@Take@Matrice@@5DD, −mDD<,
82006, 8, 15<, ImageSize → 300, Filling → 82 → 81<<, FillingStyle → Blue,
PlotMarkers −> 8" ", " ", Style@"− ", Blue, 12, BoldD, Style@" −", Blue, 12, BoldD<D,
8m, 10, Length@Matrice@@4DDD, 10<D,
"Second report" → Manipulate@
ListPlot@Take@Matrice@@9DD, −daysD, Joined → True, ImageSize → 300,
PlotLabel → "123−days mean − values for the last " <> ToString@daysD <> " days"D,
8days, 50, Length@Matrice@@9DDD, 50<D,
"Third report" → Manipulate@
ListPlot@Take@Matrice@@10DD, −daysD, Joined → True, ImageSize → 300,
PlotLabel → "21−days mean − values for the last " <> ToString@daysD <> " days"D,
8days, 50, Length@Matrice@@10DDD, 50<D,
"Fourth report" → Manipulate@
ListPlot@Take@Matrice@@11DD, −daysD, Joined → True, ImageSize → 300,
PlotLabel → "13−days mean − values for the last " <> ToString@daysD <> " days"D,
8days, 50, Length@Matrice@@11DDD, 50<D,
"Show together" → Manipulate@
ListPlot@8Take@Matrice@@9DD, −daysD, Take@Matrice@@10DD, −daysD,
Take@Matrice@@9DD, −daysD, Take@Matrice@@11DD, −daysD<,
ImageSize → 300, Joined → TrueD, 8days, 50, Length@Matrice@@9DDD, 50<D,
"Overall" →
Manipulate@

8Tooltip@Take@Matrice@@4DD, −daysDD, Tooltip@Take@Matrice@@3DD, −daysDD,


DateListPlot@

Tooltip@Take@Matrice@@2DD, −daysDD, Tooltip@Take@Matrice@@5DD, −daysDD,


Tooltip@Take@Matrice@@9DD, −daysDD, Tooltip@Take@Matrice@@10DD, −daysDD,
Tooltip@Take@Matrice@@9DD, −daysDD, Tooltip@Take@Matrice@@11DD, −daysDD<,
82006, 8, 15<, ImageSize → 300, PlotMarkers −> 8"‡", "Ê", Style@"− ", Blue, 12, BoldD,
Style@" −", Blue, 12, BoldD, "Á", "Û", "ı", "Ï"<, Joined → TrueD,
8days, 50, Length@Matrice@@9DDD, 50<, 8c1, 8False, True<<D<DD

Manipulate@

Map@Button@ @@2DD, CreateDocument@MyButtonFunction@ @@2DDDDD &, Take@SQLExecute@ conn,


Column@8"Select a ticker and click the button to see the graphic", Spacer@100D,

"SELECT ∗ FROM Ticker WHERE IDMercato = " <> ToString@nDD, 10DD êê TableForm<D,
8n, 81, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14<<, ControlType → SetterBarD

ü Fully integrated across all functionality


As a straightforward example of data import and processing, I will show an example of working with image data.
Import a JPEG image showing a picture of bacteria and limestone accretions in the runoff from a geothermal hot spring.
MathTour2008-NorwayAndDenmark.nb 3

SetDirectory@NotebookDirectory@DD;

Show@ Import@"Bacteria.JPG", "JPG"D, ImageSize → 300D

Extract a part of the image data from the Graphics object. The image data consists of a matrix of triples giving the values for
each color channel at each pixel.

In[3]:= imagedata = Import@"Bacteria.jpg", "Data"D@@1 ;; 100, 51 ;; 150DD

A very large output was generated. Here is a sample of it:

888161, 135, 86<, 8150, 121, 65<, 8140, 111, 45<, 8137, 104, 33<,
8125, 91, 19<, 8130, 94, 20<, 8147, 109, 38<, 8154, 116, 45<, 8151, 113, 42<,
8155, 117, 46<, 8159, 123, 49<, 8163, 127, 53<, 8166, 133, 56<,
8170, 137, 60<, 8173, 139, 65<, 8182, 148, 74<, 8179, 145, 71<, 8182, 148, 74<,
, 8198, 161, 93<, 8199, 160, 93<, 8201, 162, 95<, 8201, 161, 91<,
8198, 158, 88<, 8193, 151, 77<, 8190, 148, 74<, 8190, 148, 74<, 8194, 152, 78<,
Out[3]= 65

8199, 157, 83<, 8198, 156, 82<, 8196, 157, 82<, 8194, 155, 80<, 8193, 153, 81<,
8192, 152, 80<, 8194, 156, 83<, 8201, 163, 90<<, 98 , 8 1 <<

Show Less Show More Show Full Output Set Size Limit...

Show the part of the data that was extracted with the true colors.
4 MathTour2008-NorwayAndDenmark.nb

In[4]:= imgdetail = Graphics@Raster@imagedata ê 256.DD

Out[4]=

What I would like to do is get the size and location of the limestone accretion. There are many ways to approach this. The
idea I show below is certainly not the best, but it demonstrates well the ability to explore using the mathematics and interactiv-
ity of Mathematica.
Compute the mean color of all the pixels shown in the image.

In[5]:= meancolor = Mean@Mean@N@imagedataDDD

Out[5]= 8193.49, 155.8, 78.1182<

I am interested in highlighting those points with color different than the most prevalent dull yellow. Thinking of the color as
a vector in 3-space, I want to highlight the points with different color directions. This can be done by taking the norm of the
cross product with the mean color.
Compute the norm of the cross product of the color vector at each point with the mean color and normalize it so that the
maximum value is 1.
orthogonal = Map@Norm@Cross@meancolor, DD &, imagedata, 82<D;
orthogonal ê= Max@orthogonalD;
In[6]:=

Show a surface plot of the resulting data.


MathTour2008-NorwayAndDenmark.nb 5

In[8]:= ListPlot3D@orthogonal, PlotRange → All, Mesh → FalseD

Out[8]=

Define a Gaussian model function.

In[9]:= Gaussian@a_, 8x0_, y0_<D@8x_, y_<D := −a IHx−x0L2 +Hy−y0L2 M

Compute a nonlinear least-squares best fit for a Gaussian to the data.


Here I just add coordinates to each value.

In[10]:= xydata = Flatten@MapIndexed@Flatten@8 2, 1<D &, orthogonal, 82<D, 1D

A very large output was generated. Here is a sample of it:

881, 1, 0.322325<, 81, 2, 0.0694745<, 81, 3, 0.177929<, 81, 4, 0.342394<,


81, 5, 0.483023<, 81, 6, 0.498972<, 81, 7, 0.330704<, 81, 8, 0.266964<,
8100, 93, 0.0544211<, 8100, 94, 0.127035<, 8100, 95, 0.15502<, 8100, 96, 0.155275<,
9984 ,

8100, 97, 0.125922<, 8100, 98, 0.19546<, 8100, 99, 0.177795<, 8100, 100, 0.160276<<
Out[10]=

Show Less Show More Show Full Output Set Size Limit...

In[11]:= fit = FindFit@xydata, Gaussian@a, 8x0, y0<D@8x, y<D, 88a, .1<, 8x0, 50<, 8y0, 60<<, 8x, y<D

Out[11]= 8a → 0.0094613, x0 → 59.0501, y0 → 53.4534<

In[12]:= Gaussian@a, 8x0, y0<D@8x, y<D ê. fit

−0.0094613 IH−59.0501+xL2 +H−53.4534+yL2 M


Out[12]=

Show contours for the data and the fit.


6 MathTour2008-NorwayAndDenmark.nb

ContourPlot@Evaluate@ Gaussian@a, 8x0, y0<D@8y, x<D ê. fitD, 8x, 0, 100<,


In[13]:= Show@imgdetail,

8y, 0, 100<, Contours −> 80.5<, ContourShading → False, PlotRange −> AllD,
ListContourPlot@orthogonal, Contours −> 80.5<, ContourShading → False, PlotRange −> AllDD

Out[13]=

Why "The New Era"?

ü A Complete system
ü Fully integrated across all functionality
ü "Instantly dynamic": new, unique and important

ü Means a whole new era of modelling, simulation and development.

ü Analogy: what can be done with DOS v Vista/MacOSX

(...)

ü Revolutionary concept of "personal development environment"

ü Taking non-developers and enabling development

ü Analogy: Google making everyone personal librarian


MathTour2008-NorwayAndDenmark.nb 7

ü Completing the "fully integrated" vision


In[14]:= parts = 8"Programming", "Numerics", "Symbolics", "Documents", "Text", "Graphics"<;
Apply@Mouseover,
GraphPlot@ , DirectedEdges → True, VertexLabeling → True, Method → "CircularEmbedding",
MultiedgeStyle → 0.04, SelfLoopStyle → None, ImageSize → 400D & ê@
8Join@DeleteCases@Flatten @ Outer@Rule, parts, partsD,
HHoldPattern@"Graphics" → _D » HoldPattern@"Documents" → "Graphics"DLD,
8"Interfaces" → "Programming", "Programming" −> "Interfaces"<D,
AppendTo@parts, "Interfaces"D; Flatten @ Outer@Rule, parts, partsD<D

Symbolics

Numerics

Documents

Out[15]= Programming

Text

Interfaces

Graphics

Mathematica's basic concept: everything is an expression


Mathematica is built on a simple and efficient concept: everything is (internally) represented as an expression having the
form Name[arguments, options].
All objects we can use inside Mathematica is converted to an expression or expression of expressions (nesting). This makes
Mathematica an extremely powerful and flexible language, and at the same time this is the "trick" for an immediate under-
standing of how the system works.
The version 6 has fully integrated this concept also for those elements, such graphics, that apparently have a different shape.
To explore the internal representation of Mathematica objects, we have some functions like FullForm, InputForm TreeForm
and so on.
Here in the internal form of a rational number
8 MathTour2008-NorwayAndDenmark.nb

F
1
In[16]:= FullFormB
3
Out[16]//FullForm=
Rational@1, 3D

F
1
In[17]:= TreeFormB
3
Out[17]//TreeForm=

Rational

1 3

A binomial

In[18]:= FullForm@a + b xD

Out[18]//FullForm=
Plus@a, Times@b, xDD

In[19]:= TreeForm@a + b xD

Out[19]//TreeForm=

Plus

a Times

b x

A graphic
MathTour2008-NorwayAndDenmark.nb 9

In[20]:= gr = Graphics@8Blue, Circle@80, 0<D<D

Out[20]=

In[21]:= FullForm@grD

Out[21]//FullForm=
Graphics@List@RGBColor@0, 0, 1D, Circle@List@0, 0DDDD

In[22]:= TreeForm@grD

Out[22]//TreeForm=

Graphics

List

RGBColor Circle

0 0 1 List

0 0

Knowing the internal form is also useful to manage our results without to find complex alternatives or re-write pieces of
code. If I want to replace the circle with a disk, I can use a replacement rule specifying that he objects Circle have to be
replaced by the object Disk.
10 MathTour2008-NorwayAndDenmark.nb

In[23]:= ReplaceAll@gr, Circle → DiskD

Out[23]=

Even the front end (the Mathematica user interface module) manages objects with the same philosofy.

Here is a dynamic object enabling the manipulation of a polynomial expansion

In[24]:= man = ManipulateAExpandAHx − 2Ln E, 8n, 2, 10, 1<E

Out[24]=

4 − 4 x + x2

I can ask for the InputForm and the TreeForm as well

In[25]:= FullForm@manD

Out[25]//FullForm=
Manipulate@Expand@Power@Plus@x, −2D, nDD, List@n, 2, 10, 1DD
MathTour2008-NorwayAndDenmark.nb 11

In[26]:= TreeForm@manD

Out[26]//TreeForm=

Manipulate

Expand List

Power n 2 10 1

Plus n

x −2

So, if I want to change the x variable, I can use ReplaceAll once again

In[27]:= ReplaceAll@man, x → Hz − 1L ^ 2D

Out[27]=

1 + 4 z + 2 z2 − 4 z3 + z4

Another example.

Here is a list of points of the function Sin and the ListPlot image

In[28]:= data = Table@8x, Sin@xD<, 8x, 0, 6 Pi, 0.02<D;


ListPlot@dataD
1.0

0.5

Out[29]=
5 10 15

-0.5

-1.0

With an easy replacemente rule, I generate a list of triple, where starting from the couple {x, y} the third value is a function
of the first two
12 MathTour2008-NorwayAndDenmark.nb

In[30]:= data = ReplaceAll@data , 8x_, y_< −> 8x, y, Sin@x yD<D;


ListPointPlot3D@dataD

1.0

0.5
Out[31]= 1.0
0.0
-0.5 0.5
-1.0
0 0.0

5
-0.5
10

15
-1.0

A more useful example

In astronomical field, sometimes it is needed to plot functions with reversed x axes (x values decreases from left to right).
The flexibility of Mathematica allows to obtain this kind of result without to increase the complexity or the number of
funtions already built-in.

In[32]:= s = FullGraphics@Plot@Cos@xD + x, 8x, 0, 10<DD

Out[32]=
4

2 4 6 8 10
MathTour2008-NorwayAndDenmark.nb 13

In[33]:= ReplaceAll@s, 8x_Real, y_Real< → 8−x, y<D

Out[33]=
4

10 8 6 4 2

Dynamic interaction: a completely new feature in Mathematica


6

ü Examples
Here I generate two results, apparently identical: the solution of an equation with three parameters and the "Dynamic"
version of the same solution

In[34]:= PanelAColumnA9SolveAa x2 + b x + c 0, xE, Spacer@50D, DynamicASolveAa x2 + b x + c 0, xEE=EE

::x Ø >, :x Ø >>


-b- b2 -4 a c -b+ b2 -4 a c
2a 2a
Out[34]=

88x Ø -0.372678 - 0.726483 Â<, 8x Ø -0.372678 + 0.726483 Â<<

Now we can assign any value to one or more of the variable included in the solution, and see how the above cell change
accordingly

In[35]:= 8a = 0, b = 1, c = −2<

Out[35]= 80, 1, −2<

As you have seen, the first result is static, while the second one changes when a/b/c are changed. We can use some facilities
to set their values. For instance a list of SetterBar managing tha parameters values
14 MathTour2008-NorwayAndDenmark.nb

In[36]:= Panel@

8Row@8"Set a ", SetterBar@Dynamic@aD, Range@−5, 5DD<D,


Column@

Row@8"Set b ", SetterBar@Dynamic@bD, Range@−5, 5DD<D,


Row@8"Set c ", SetterBar@Dynamic@cD, Range@−5, 5DD<D<DD

Set a -5 -4 -3 -2 -1 0 1 2 3 4 5

Out[36]= Set b -5 -4 -3 -2 -1 0 1 2 3 4 5

Set c -5 -4 -3 -2 -1 0 1 2 3 4 5

Manipulate is a "macro" that allow an immediate generation of a special box where parameters can be manipulated by
controllers.

In[37]:= Manipulate@
Style@Column@8Row@8"Discriminant → ", ToString@b ^ 2 − 4 a cD<D,
Row@8"Solutions → ", Reduce@a x ^ 2 + b x + c 0, x, RealsD<D<, Spacings → 2D, LargeD,
8a, −5, 5, 1, Appearance → "Labeled"<, 8b, −5, 5, 1, Appearance → "Labeled"<,
8c, −5, 5, 1, Appearance → "Labeled"<D

a -5

b -5

c -5

Out[37]=
Discriminant → −75

Solutions → False

Another nice and useful new feature is Mouseover. It allow to overlap two objects and shows the first or the second depend-
ing on the position of the mouse pointer (outside or inside the object's box).
Here is a double face graphic, representing a ListPlot of a random data set and the BinCounts of its values. Mmove the mous
pointer inside/outside the image to see the "side B"
MathTour2008-NorwayAndDenmark.nb 15

In[38]:= data = RandomReal@80, 10<, 1000D;

Mouseover@ListPlot@dataD, BarChart@BinCounts@data, 80, 10, 1<DDD


Needs@"BarCharts`"D;

10

Out[40]=
4

200 400 600 800 1000

Another example

In[41]:= data = RandomReal@1, 8100, 2<D;


Mouseover@ListPlot@dataD,
Tooltip@ListLinePlot@data@@Last @ FindShortestTour@dataDDDD, "Shortest Tour"DD

1.0

0.8

0.6

Out[42]=
0.4

0.2

0.2 0.4 0.6 0.8

Note that in just one line of code we have used random number generation, a graphic function, a quite complex algorithm
finding the shortest tour from a set of points and an interactivy function controlling the mouse position. This is an easy and
intuitive example of Mathematica as an integrated system. The same philosophy for several different tasks and operations.

2D and 3D graphics: some examples and new feaures


In[43]:= Plot@Sin@ x
D, 8x, 0, 4<D

1.0

0.5

Out[43]=
1 2 3 4

-0.5

-1.0
16 MathTour2008-NorwayAndDenmark.nb

We can easily manipulate graphics using parameters and manipulators

In[44]:= Manipulate@Plot@Sin@x H1 + a xLD, 8x, 0, 6<D, 8a, 0, 2<D

1.0

0.5

Out[44]=

1 2 3 4 5 6

-0.5

-1.0

Mathematica’s plot functions are now fully numeric and as a result allow the specification of WorkingPrecision . This
will help clean up plots that would otherwise display some noise.
In this example the imaginary part of the WeierstrassZeta function is zero on the specified interval but numerical error
creeps in leading to a lot of noise in the plot.

+ Im@WeierstrassZeta@z, 83, 4<DD, 8z, 0, 1<F


z
In[45]:= PlotB
1015

1.5 μ 10-15

1. μ 10-15

Out[45]=
5. μ 10-16

0.2 0.4 0.6 0.8 1.0

-5. μ 10-16

Setting the WorkingPrecision to 32 corrects the situation.

In[46]:= PlotAz ë 1015 + Im@WeierstrassZeta@z, 83, 4<DD, 8z, 0, 1<, WorkingPrecision → 32E

1. μ 10-15

8. μ 10-16

6. μ 10-16
Out[46]=
4. μ 10-16

2. μ 10-16

0.2 0.4 0.6 0.8 1.0


MathTour2008-NorwayAndDenmark.nb 17

Plot: Singularity Removal


If a function has non-removable singularities at points xi , the vertical lines that would normally appear in the plot can be
avoided by including those points in the plot specification. This is similar to having NIntegrate pay special attention to
individual points when integrating.

This function has singularities at 0 and Å12 .

, 8x, −1, 1<F


1

x Ix − M
In[1]:= PlotB
1
2

40

20

Out[1]= -1.0 -0.5 0.5 1.0

-20

-40

If we specify the exclusion values, we can also specify a style to use for those values. Giving the two colors, blue and
orange, creates a blue line with orange points at the end.

Here we specify the locations of the singularities. In this case the excluded values are indicated by blue lines with
orange points at the minimum and maximum values.

, 8x, −1, 1<, Exclusions → :x >,


1 1 1

Ix + M Ix − M
In[2]:= PlotB − ,x
1 1 4 2

ExclusionsStyle → 8Blue, Orange<, PlotStyle → 88Red, Thick<<F


4 2

30

20

10

Out[2]=
-1.0 -0.5 0.5 1.0

-10

-20

-30
18 MathTour2008-NorwayAndDenmark.nb

Similar functions are available for 3D graphics

, 8x, −4, 4<, 8y, −4, 4<, Mesh → None, PlotRange → 5, Exclusions → 9y x2 =F
2
In[3]:= Plot3DB
y − x2

Out[3]=

, 8x, −4, 4<, 8y, −4, 4<, Mesh → None, PlotRange → 5,


2
In[4]:= Plot3DB
y − x2
Exclusions → 9y x2 =, ExclusionsStyle → 8Directive@Opacity@.5DD<F

In[5]:=

Plot: Automatic Removal of Non-Real Values


The visualization functions in Mathematica are defined to work with real-valued functions. Hence if a function becomes
complex- or other-valued at some point, those values are excluded. With the adaptive sampling algorithm built into these
functions, you will usually get fairly close to the real-valued barrier.
MathTour2008-NorwayAndDenmark.nb 19

Here is an example of a function that returns complex values on the specified domain.

In[6]:= TableB 1 − x2 , 8x, −2, 2, 0.1<F

Out[6]= 90. + 1.73205 , 0. + 1.61555 , 0. + 1.49666 , 0. + 1.37477 , 0. + 1.249 ,


0. + 1.11803 , 0. + 0.979796 , 0. + 0.830662 , 0. + 0.663325 , 0. + 0.458258 , 0.,
0.43589, 0.6, 0.714143, 0.8, 0.866025, 0.916515, 0.953939, 0.979796, 0.994987, 1.,
0.994987, 0.979796, 0.953939, 0.916515, 0.866025, 0.8, 0.714143, 0.6, 0.43589,
0. + 2.10734 × 10−8 , 0. + 0.458258 , 0. + 0.663325 , 0. + 0.830662 , 0. + 0.979796 ,
0. + 1.11803 , 0. + 1.249 , 0. + 1.37477 , 0. + 1.49666 , 0. + 1.61555 , 0. + 1.73205 =

In[7]:= PlotB 1 − x2 , 8x, −2, 2<, PlotStyle → 88Red, Thick<<, AspectRatio → AutomaticF

1.0
0.8
0.6
Out[7]= 0.4
0.2

-2 -1 1 2

Plot: Adaptive Refinement


Mathematica uses an adaptive algorithm to determine how many points should be included in creating the plot. One of the
determining factors is how many recursions are used to locate points used in the plot. As with other numeric functions,
recursion leads to higher precision results.

This shows all of the points used in the adaptive sampling routine for Plot.
In[8]:= Plot@Sin@tD, 8t, 0, 2 π<, Mesh → AllD

1.0

0.5

Out[8]=
1 2 3 4 5 6

-0.5

-1.0
20 MathTour2008-NorwayAndDenmark.nb

Adaptive refinement requires repeated sampling of the function which is done recursively. This Manipulate
illustrates the process.

Plot@Sin@x D, 8x, −π, π<,


In[9]:= Manipulate@

MaxRecursion → n,
Mesh → All, MeshStyle → 8Red, PointSize@0.015D<, PlotPoints → 8D, 8n, 0, 10, 1<D

1.0

0.5

Out[9]=

-3 -2 -1 1 2 3

-0.5

-1.0

Here is the same for 3D

Plot3D@Sin@x yD, 8x, 0, π<, 8y, 0, π<, MaxRecursion → r, PlotPoints → 8, Mesh → AllD,
In[10]:= Manipulate@

88r, 0, "MaxRecursion"<, 0, 10, 1<D

MaxRecursion

Out[10]=
MathTour2008-NorwayAndDenmark.nb 21

Plot: Filling
A new option, Filling , can be used to shade regions of a plot. This option essentially replaces the functionality that was
present in the pre-Version 6 package, Graphics`FilledPlot`.

In[11]:= Plot@8Sin@xD, Cos@xD<, 8x, 0, 2 π<, Filling → AutomaticD

Out[11]=

You can fill to the horizontal axis, or the top, or bottom of the plot.
TabView@8"Top" −> Plot@8Sin@xD, Cos@xD<, 8x, 0, 2 π<, Filling → TopD,
"Axis" −> Plot@8Sin@xD, Cos@xD<, 8x, 0, 2 π<, Filling → AxisD,
In[12]:=

"Bottom" −> Plot@8Sin@xD, Cos@xD<, 8x, 0, 2 π<, Filling → BottomD,


"Inside" −> Plot@8Sin@xD, Cos@xD<, 8x, 0, 2 π<, Filling → 81 → 82<<D<D

Top Axis Bottom Inside

Out[12]=
22 MathTour2008-NorwayAndDenmark.nb

We add a different style in this example with the option FillingStyle . The colors change depending on which
function value is greater.
In[13]:= Plot@8Sin@xD, Cos@xD<, 8x, 0, 2 π<, Filling → 81 → 82<<, FillingStyle → 8Yellow, Green<D

1.0

0.5

Out[13]=
1 2 3 4 5 6

-0.5

-1.0

Here is a 3D equivalent
In[14]:= Plot3DBSin@x yD, 8x, 0, π<, 8y, 0, π<,

RegionFunction → 0.3 ≤ K 1 − O +K 2− O ≤ 2 & , Filling → BottomF


π 2 π 2

2 2

Out[14]=

Tooltip and MouseOver


There is a new function, Tooltip , which can be used as a wrapper for the plot arguments. The argument to Tooltip will
display when the mouse passes over the curves in the plot. You can use Tooltip to distinguish between vectors as in this
example.
MathTour2008-NorwayAndDenmark.nb 23

In[1]:= Plot@8Tooltip@Sin@xDD, Tooltip@Cos@xDD<, 8x, 0, 2 Pi<D

1.0

0.5

Out[1]=
1 2 3 4 5 6

-0.5

-1.0

Plot@Evaluate@Map@Tooltip@ , TraditionalForm@ DD &,


8Sin@xD, Cos@xD, 3 Sin@xD, Sin@xD + 2, Cos@xD + 2<DD, 8x, 0, 2 π<D
In[2]:=

Out[2]=
1 2 3 4 5 6

-1

-2

-3

ContourPlot show automatically Tooltips for contour lines

In[3]:= ContourPlot@Sin@2 x D Sin@2 yD, 8x, 0, π<, 8y, 0, π<, ContourLabels → NoneD

Out[3]=
24 MathTour2008-NorwayAndDenmark.nb

Plot3D@Evaluate@Table@Tooltip@Sin@x + n yD, Plot@Sin@n xD,


8x, 0, 2 Pi<, Filling → Axis, FillingStyle → Pink, ImageSize → 200DD, 8n, 3<DD,
In[4]:=

8x, 0, 3<, 8y, 0, 3<, PlotStyle → Table@Opacity@.7, Hue@i ê 6DD, 8i, 4<DD

Out[4]=

Grid@Table@Mouseover@data = Table@1 + Sin@i r ê 10D, 8r, 1, 100<D + RandomReal@t ê 5, 100D;


ListPlot@data, PlotRange → All, AxesOrigin → 80, 0<D, ListPlot@Abs@Fourier@dataDD,
In[5]:=

PlotRange → All, Joined → TrueD, ImageSize −> AllD, 8i, 1, 2<, 8t, 1, 2<DD

2.0
2.0

1.5
1.5

1.0
1.0

0.5 0.5

20 40 60 80 100 20 40 60 80
Out[5]=

2.0
2.0

1.5
1.5

1.0
1.0

0.5 0.5

20 40 60 80 100 20 40 60 80
MathTour2008-NorwayAndDenmark.nb 25

Graphics fully integrated into the whole system, just like any
other object
Coin tossing experiments...

In[1]:= coins = 8Import@ToFileName@NotebookDirectory@D, "EuroA.jpg"DD,


Import@ToFileName@NotebookDirectory@D, "EuroB.jpg"DD<

Out[1]= : , >

In[2]:= Button@"Toss the coin", Print@coins@@RandomChoice@81, 2<DDDDD

Out[2]= Toss the coin

In[3]:= t = ApplyAPlus, Hcoins − 4L2 E


2 2

Out[3]= −4 + + −4 +

In[4]:= t = Expand@%D
2 2

Out[4]= 32 − 8 + −8 +

In[5]:= s = ApplyAPlus, coins2 E


2 2

Out[5]= +

In[6]:= Solve@8t 0, s 1<, coinsD

:: >,
33 31 33 31
Out[6]= → − , → +
16 16 16 16

: >>
33 31 33 31
→ + , → −
16 16 16 16
26 MathTour2008-NorwayAndDenmark.nb

Instantly dynamic

ü Interact with anything that makes sense


In[7]:= ListPlot@Sort@RandomReal@StudentTDistribution@5D, 8100<DD, PlotRange → 8−4, 4<D

Out[7]=
20 40 60 80 100

-2

-4

Adding interaction:

Manipulate@ListPlot@Sort@RandomReal@StudentTDistribution@5D, 8points<DD,
PlotRange → 8−4, 4<D, 8points, 100, 1000, 10<D
In[8]:=

points

Out[8]=

20 40 60 80 100

-2

-4

A random walk
MathTour2008-NorwayAndDenmark.nb 27

In[9]:= ListLinePlot@Accumulate@RandomReal@8−1, 1<, 100DDD

Out[9]=
1

20 40 60 80 100
-1

-2

In[10]:= Manipulate@ListLinePlot@Accumulate@RandomReal@8−1, 1<, pointsDDD, 8points, 100, 1000, 10<D

points

20 40 60 80 100

-5
Out[10]=

-10

-15

Dynamic@Plot@Cos@xD, 8x, −2, 2<, ImagePadding → 1, Axes → assi,


Epilog → Inset@Panel@Column@8"Draw axes", Checkbox@Dynamic@assiDD<DD, 81.5, .8<DDD
In[1]:=

Draw axes

Out[1]=
28 MathTour2008-NorwayAndDenmark.nb

Fast response is needed for instant interactivity


ü Real-time lighting changes
myspheres = Table@8RGBColor@Random@D, Random@D, Random@DD, Specularity@White, 128D,
Sphere@8x, y, z<, 1D<, 8x, 0, 10, 4<, 8y, 0, 10, 4<, 8z, 0, 10, 4<D;
In[2]:=

Manipulate@Graphics3D@8White, PointSize@.02D, Point@8t@@1DD, t@@2DD, 5<D, myspheres<,


Background → Black, Boxed → False,
Lighting → 8RGBColor@.3, .3, .3D, 8White, 888t@@1DD, t@@2DD, 5<, 80, 0, 0<<, 2<<<,
PlotRange → 88−1, 10<, 8−1, 10<, 8−1, 10<<, ImageSize → 300D,
8t, 8−15, −15<, 820, 20<<, SaveDefinitions → TrueD

Out[2]=
MathTour2008-NorwayAndDenmark.nb 29

ü Draft quality for expansive calculations


Manipulate@fn@Re@Zeta@x + I yDD, 8x, −t, 1<, 8y, −6, 6<, ImageSize → 300D,
8fn, 8ContourPlot, Plot3D<<, 8t, 1, 20<D
In[4]:=

fn ContourPlot Plot3D

Out[4]=

Integrated data sources


Mathematica gives seamless immediate access to an ever-growing library of carefully curated and continually updated data
maintained by Wolfram Research—all with a unified interface allowing complete interoperability and tight integration into
Mathematica and its computational and data presentation capabilities.
30 MathTour2008-NorwayAndDenmark.nb

In[5]:= Column@8Tooltip@CountryData@"Denmark", "Flag"D, "Denmark"D,


Tooltip@CountryData@"United Kingdom", "Flag"D, "United Kingdom"D,
Tooltip@CountryData@"Italy", "Flag"D, "Italy"D<D

Out[5]=

In[11]:= GraphPlot@Flatten@Thread@ −> CountryData@ , "BorderingCountries"DD & ê@


CountryData@"SouthAmerica"DD, VertexLabeling → True, ImageSize → 300D

Suriname Guyana

FrenchGuiana Venezuela
Panama

Brazil Colombia
Out[11]=

Uruguay Paraguay
Peru Ecuador
Bolivia
Argentina

Chile

Graphics@8Gray, CountryData@ , "SchematicPolygon"D & ê@ CountryData@"Europe"D,


Thick, Red, Line@ @@Last@FindShortestTour@ DDDD &@
In[7]:=

Reverse@CountryData@ , "CenterCoordinates"DD & ê@ CountryData@"Europe"DDD<D

Out[7]=
MathTour2008-NorwayAndDenmark.nb 31

In[8]:= Button@"Country data notebook",


CreateDocument@CellGroup@8TextCell@CountryData@ , "Name"D, "Section"D,

Quiet@DateListPlot@CountryData@ , 88"Population"<, 81970, 2005<<DDD<,


ExpressionCell@GraphicsRow@8CountryData@ , "Shape"D,

Frame → All, ImageSize −> 450D, "Output"D<, ClosedD & ê@ CountryData@"Europe"D,


StyleDefinitions → "CreativeêNaturalColor.nb"D, Method → "Queued"D

Out[8]= Country data notebook

Graphics@Tooltip@Inset@CountryData@ , "Flag"D, 8Log@10, CountryData@ , "Population"DD,


Log@10, CountryData@ , "GDP"DD<, Center, 0.2D, D & ê@
In[9]:=

DeleteCases@CountryData@"Europe"D, "VaticanCity" » "Svalbard"D, Frame → True,


PlotRangePadding → .2, FrameLabel → 8Style@"Population", 20D, Style@"GDP", 20D<D

Out[9]=

In[13]:= ManipulateB
H∗Select the subset of the data according to the checkboxes set by the user∗L
pms = If@showconservative, , DeleteCases@ , 8___, "Conservative"<DD & @
If@showlabour, , DeleteCases@ , 8___, "Labour"<DD & @
If@shownationallabour, , DeleteCases@ , 8___, "National Labour"<DD & @
If@showliberal, , DeleteCases@ , 8___, "Liberal"<DD & @ PrimeMinisterData;

H∗Create the whole graphic∗L


Deploy @ Graphics@Table@PrimeMinisterGraphic@pms, i, If@stack, i, 1D, stack, TrueD,
8i, Length@pmsD − 1<D, AspectRatio → 0.7, Axes → 8True, False<,
FrameTicks → 8880, 1900<, 825, 1925<, 850, 1950<, 875, 1975<, 8100, 2000<<, None<,
Frame → True, Axes → False, GridLines → None,
PlotRange → If@stack, 880, 120<, 80.5, Length@pmsD + 0.5<<, 880, 120<, 81, 2<<D,
AxesOrigin → 80, 0<, ImageSize → 300D,

H∗Interface specification∗L
88stack, True, "show names"<, 8True, False<<,
88showconservative, True, "Conservative Party"<, 8True, False<<,
88showlabour, True, "Labour Party"<, 8True, False<<,
88showliberal, True, "Liberal Party"<, 8True, False<<,
32 MathTour2008-NorwayAndDenmark.nb

88shownationallabour, True, "National Labour Party"<, 8True, False<<,

Initialization

DateToValue@8m_, yr_<D := m ê 12 + yr − 1900;


H∗Create one line of the graphic∗L
PrimeMinisterGraphic@pmdata_, pm_, pos_, txt_, tooltip_D :=

Column@8pmdata@@pm, 4DD, Rasterize@pmdata@@pm, 1DDD<, CenterDDD, D &@


If@tooltip, Tooltip@ , If@txt, pmdata@@pm, 1DD,

8Switch@pmdata@@pm, 5DD, "Conservative", Blue, "Labour", Red,


"Liberal", Yellow, "National Labour", OrangeD,

8DateToValue@pmdata@@pm, 2DDD, pos<,


Polygon@8

8DateToValue@pmdata@@pm, 3DDD, pos<,


8DateToValue@pmdata@@pm, 3DDD, pos + 1<,
8DateToValue@pmdata@@pm, 2DDD, pos + 1<,
8DateToValue@pmdata@@pm, 2DDD, pos<<D, Black, If@txt, Text@" " <> pmdata@@pm, 4DD,
8DateToValue@pmdata@@pm, 3DDD, pos + 0.5<, 8−1, 0<D, 8<D<D;

H∗ The historical data on all the PMs ∗L

PrimeMinisterData = :: , 87, 1902<, 812, 1905<, "Arthur Balfour", "Conservative">,

: , 812, 1905<, 84, 1908<, "Sir Henry Campbell−Bannerman", "Liberal">,

: , 84, 1908<, 812, 1916<, "H. H. Asqith", "Liberal">,

: , 812, 1916<, 810, 1922<, "David Lloyd George", "Liberal">,

: , 810, 1922<, 85, 1923<, "Andrew Bonar Law", "Conservative">,

: , 85, 1923<, 81, 1924<, "Stanley Baldwin", "Conservative">,

: , 81, 1924<, 810, 1924<, "Ramsay MacDonald", "Labour">,


MathTour2008-NorwayAndDenmark.nb 33

: , 810, 1924<, 86, 1929<, "Stanley Baldwin", "Conservative">,

: , 86, 1929<, 88, 1931<, "Ramsay MacDonald", "Labour">,

: , 88, 1931<, 86, 1935<, "Ramsay MacDonald", "National Labour">,

: , 86, 1935<, 85, 1937<, "Stanley Baldwin", "Conservative">,

: , 85, 1937<, 85, 1940<, "Neville Chamberlain", "Conservative">,

: , 85, 1940<, 87, 1945<, "Winston Churchill", "Conservative">,

: , 87, 1945<, 810, 1951<, "Clement Attlee", "Labour">,

: , 810, 1951<, 85, 1955<, "Winston Churchill", "Conservative">,

: , 85, 1955<, 81, 1957<, "Sir Anthony Eden", "Conservative">,

: , 81, 1957<, 810, 1963<, "Harold Macmillan", "Conservative">,

: , 810, 1963<, 810, 1964<, "Sir Alec Douglas Home", "Conservative">,

: , 810, 1964<, 86, 1970<, "Harold Wilson", "Labour">,


34 MathTour2008-NorwayAndDenmark.nb

: , 86, 1970<, 83, 1974<, "Edward Heath", "Conservative">,

: , 83, 1974<, 84, 1976<, "Harold Wilson", "Labour">,

: , 84, 1976<, 85, 1979<, "James Callaghan", "Labour">,

: , 85, 1979<, 810, 1990<, "Margaret Thatcher", "Conservative">,

: , 810, 1990<, 85, 1997<, "John Major", "Conservative">,

: , 85, 1997<, 812, 2007<, "Tony Blair", "Labour">, Null> ;

, ControllerLinking → TrueF
MathTour2008-NorwayAndDenmark.nb 35

show names

Conservative Party

Labour Party

Liberal Party

National Labour Party

1900 1925 1950 1975 2000

John Major
Margaret Thatcher
Edward Heath
Sir Alec Douglas Home
Out[13]=
Harold Macmillan
Sir Anthony Eden
Winston Churchill
Winston Churchill
Neville Chamberlain
Stanley Baldwin
Ramsay MacDonald
Stanley Baldwin
Stanley Baldwin
Andrew Bonar Law
David Lloyd George
H. H. Asqith
Sir Henry Campbell-Bannerman
Arthur Balfour

1900 1925 1950 1975 2000

Example of data analysis and dynamic interaction


Thanks to the complete integration of all Mathematica's functionalities, we can analyse, manipulate and change data in just
one box.
In this example there is a InterpolatingPolynomial applied on some starting points, then its graphic result and an interactive
panel which let us to change "on the fly" pre-existing values or add new ones.
36 MathTour2008-NorwayAndDenmark.nb

Plot@Evaluate@InterpolatingPolynomial@pts, xDD, 8x, −2, 2<,


In[15]:= Manipulate@

PlotRange → 88−2, 2<, 8−10, 10<<, Filling → Axis, FillingStyle → LightGreen,


ImageSize → 300D, 88pts, Table@8Random@D, Random@D<, 85<D<,
8−2, −10<, 82, 10<, Locator, LocatorAutoCreate → True<D

10

Out[15]=
-2 -1 1 2

-5

-10

Here there is also the FindShortestTour included

Manipulate@ListLinePlot@pts@@Last@FindShortestTour@ptsDDDD, ImageSize → 300, Axes → FalseD,


88pts, Table@8Random@D, Random@D<, 810<D<,
In[17]:=

8−2, −10<, 82, 10<, Locator, LocatorAutoCreate → True<D

Out[17]=

Some "traditional" aspects

ü FindFit examples
A first example
MathTour2008-NorwayAndDenmark.nb 37

data = TableB:t, μ t ≥ 0 + RandomReal@8−0.01`, 0.01`<D>, 8t, −5, 150<F;


Sin@0.1` tD
In[1]:= 0.04` t

F;
Sin@b tD
model = WhichBt < 0, 0, True,
−a t

In[3]:= sol = FindFit@data, model, 88a, 1<, 8b, 1<<, 8t<, MaxIterations → 200D

Out[3]= 8a → −0.0399919, b → 0.0999763<

This shows the final result compared with experimental data

Show@Plot@model ê. sol, 8t, −5, 150<, PlotStyle → Red, PlotRange → AllD,


ListPlot@data, Joined → False, PlotStyle → 8PointSize@0.008`D<D, PlotRange → AllD
In[4]:=

0.6

0.5

0.4

0.3

Out[4]= 0.2

0.1

50 100 150
-0.1

Another example.

In[5]:= MyFunction@a_, b_, c_, x_D := a If@x > 0, Cos@b xD, Exp@c xDD

Here I make a list of values adding also a random component

Block@8ε = 0.1`, a = 1.2`, b = 3.4`, c = 0.98`<,


data = Table@8x, MyFunction@a, b, c, xD + ε RandomReal@8−0.5`, 0.5`<D<, 8x, −5, 5, 0.1`<DD;
In[6]:=

FindFit is used to find the data fitting

In[7]:= fit = FindFit@data, MyFunction@a, b, c, xD, 88a, 1<, 8b, 3<, 8c, 1<<, xD

Out[7]= 8a → 1.19136, b → 3.39983, c → 0.958732<

This is the graphical result

Plot@MyFunction@a, b, c, xD ê. fit, 8x, −5, 5<, PlotStyle → RGBColor@0, 1, 0DD<D


In[8]:= Show@8ListPlot@dataD,

1.0

0.5

Out[8]=
-4 -2 2 4

-0.5

-1.0

How we can discover intermediate steps? The option EvaluationMonitor does this for us
38 MathTour2008-NorwayAndDenmark.nb

In[9]:= Options@FindFitD

Out[9]= 8AccuracyGoal → Automatic, Compiled → Automatic, EvaluationMonitor → None,


Gradient → Automatic, MaxIterations → Automatic, Method → Automatic, NormFunction → Norm,
PrecisionGoal → Automatic, StepMonitor → None, WorkingPrecision → Automatic<

88a, 1<, 8b, 3<, 8c, 1<<, x, EvaluationMonitor


In[10]:= Reap@FindFit@data, MyFunction@a, b, c, xD,
Sow@8a, b, c<DDD
88a → 1.19136, b → 3.39983, c → 0.958732<,
8881., 3., 1.<, 80.618519, 3.28841, 0.376618<, 81.10779, 3.49663, 0.888822<,
Out[10]=

81.15118, 3.39636, 0.928168<, 81.19131, 3.39994, 0.959294<, 81.19136, 3.39983, 0.958726<,


81.19136, 3.39983, 0.958732<, 81.19136, 3.39983, 0.958732<<<<

There are many other ways to further investigate the intermediate steps and internal algorithms behaviour. Mathematica
provides many tools and in some cases additional documentation and/or packages to allow users "to explore and drive" the
computation of their data.
This is an example of additional functions provided by the package Optimization`UnconstrainedProblems`

In[11]:= Needs@"Optimization`UnconstrainedProblems`"D

In[12]:= FindMinimumPlot@x Sin@x + 1D, 8x, 2<D

Out[12]= :8−3.83922, 8x → 3.95976<<, 8Steps → 4, Function → 9, Gradient → 9<,

>
2

3 4 5 6 7

-2

-4

In[13]:= FindMinimumPlot@x Sin@x + 1D, 8x, 20<D

Out[13]= :8−22.5841, 8x → 22.6062<<, 8Steps → 4, Function → 7, Gradient → 7<,

20

10

22 24 26 28 30 >

-10

-20

-30

Another example of EvaluationMonitor within the NIntegrate.


This is the function we want to integrate
MathTour2008-NorwayAndDenmark.nb 39

Plot3DAAbs@x − yD3ê2 , 8x, 0, 1<, 8y, 0, 1<,


PlotPoints → 850, 50<, ViewPoint → 80.906, −1.672, 1.590<E
In[14]:=

Out[14]=

This is the graphic of points used by NIntegrte when the method used is the default Multidimensional

ListPlotA
xys = ReapANIntegrateAAbs@x − yD3ê2 , 8x, 0, 1<, 8y, 0, 1<, EvaluationMonitor
In[15]:=

Sow@8x, y<DEE@@
2, 1DD, AspectRatio → Automatic,
PlotLabel → "Number of points:" <> ToString@Length@xysDDE

Number of points:1530
1.0

0.8

0.6

Out[15]=

0.4

0.2

0.2 0.4 0.6 0.8 1.0

Here is the same for the GaussKronrod method


40 MathTour2008-NorwayAndDenmark.nb

In[16]:= ListPlotAxys = ReapANIntegrateAAbs@x − yD3ê2 , 8x, 0, 1<, 8y, 0, 1<,


Method → GaussKronrod, EvaluationMonitor Sow@8x, y<DEE@@2, 1DD,
AspectRatio → Automatic, PlotLabel → "Number of points:" <> ToString@Length@xysDDE

Number of points:2662
1.0

0.8

0.6

Out[16]=

0.4

0.2

0.2 0.4 0.6 0.8 1.0

And finallt the Montecarlo method

ListPlotAxys = ReapANIntegrateAAbs@x − yD3ê2 , 8x, 0, 1<,


8y, 0, 1<, Method → MonteCarlo, EvaluationMonitor
In[17]:=

Sow@8x, y<DEE@@2, 1DD,


AspectRatio → Automatic, PlotLabel → "Number of points:" <> ToString@Length@xysDDE

Number of points:9200
1.0

0.8

0.6

Out[17]=

0.4

0.2

0.2 0.4 0.6 0.8 1.0

There is also another intereting function named Monitor, that provides a temporary image of what is happening during a
calculation
MathTour2008-NorwayAndDenmark.nb 41

values = 8<;
Monitor@NIntegrate@Sqrt@x H1 − xLD, 8x, 0, 1<,
In[18]:=

EvaluationMonitor HPause@0.025D; AppendTo@values, xD;LD, ListPlot@valuesDD

Out[19]= 0.392699

Monitor NDSolve using an extrapolation method:

values1 = 8<; values2 = 8<;


Monitor@NDSolve@8x ''@tD + Sin@x@tDD 0, x@0D 1, x '@0D 0<, x, 8t, 0, 2 π<,
In[20]:=

EvaluationMonitor HAppendTo@values1, 8t, x@tD<D;


AppendTo@values2, 8t, x '@tD<D; Pause@0.05D;L, Method → "Extrapolation"D,
ListPlot@8values1, values2<DD

Out[20]= 88x → InterpolatingFunction@880., 6.28319<<, <>D<<

Steps in parameter space for a nonlinear fit:

data = 880.18, −0.13<, 80.84, −0.06<, 80.05, 0.88<, 80.24, −0.63<, 80.67, 0.93<,
80.05, 0.88<, 80.65, 0.92<, 80.01, 0.99<, 80.17, −0.04<, 80.23, −0.55<<;
In[1]:=

lp = ListPlot@data, PlotRange → AllD;

In[3]:= model@8a_, k_, w_, p_<D = a Exp@−k xD Sin@w x + pD;

Monitor the evolution of the model over the steps with a graph:

Monitor@FindFit@data, model@8a, k, w, p<D, 8a, k, w, p<, x,


StepMonitor Hvars = 8a, k, w, p<; Pause@0.3D;LD, Show@lp, Plot@model@varsD, 8x, 0, 1<DDD
In[4]:=

Out[4]= 8a → 0.997279, k → 0.104408, w → −9.41643, p → 1.56093<

Implement a simple LU decomposition with a slowdown parameter p:

LU@A_, p_: 0D := Block@8m, n, L, U<, 8m, n< = Dimensions@AD; 8L, U< = 8IdentityMatrix@nD, A<;
Do@Pause@pD; L@@k ;; n, kDD = U@@k ;; n, kDD ê U@@k, kDD;
In[5]:=

U@@Hk + 1L ;; n, k ;; nDD = U@@Hk + 1L ;; n, k ;; nDD − L@@Hk + 1L ;; n, 8k<DD.U@@8k<, k ;; nDD;


, 8k, 1, n − 1<D;
8L, U<D

Monitor an algorithm animation using MatrixForm or MatrixPlot :


42 MathTour2008-NorwayAndDenmark.nb

In[6]:= Monitor@LU@RandomInteger@81, 10<, 810, 10<D, 1D, MatrixForm ê@ 8L, U<D

::81, 0, 0, 0, 0, 0, 0, 0, 0, 0<, 81, 1, 0, 0, 0, 0, 0, 0, 0, 0<, :


1 40
Out[6]= , , 1, 0, 0, 0, 0, 0, 0, 0>,
9 9

: , 1, 0, 0, 0, 0, 0, 0>, :
1 22 19 8 31 25 69
, , ,− ,−
, 1, 0, 0, 0, 0, 0>, ,−
9 9 16 9 9 16 295

: ,− , 1, 0, 0, 0, 0>, : ,
7 17 17 61 2749 7 28 13 9 541 1938
,− ,− , , , ,− , , 1, 0, 0, 0>,
9 9 16 295 1886 9 9 16 295 1886 5399

:
10 32 64 5191 11 584 5737
,− , −2, − , , , , 1, 0, 0>,
9 9 295 1886 5399 8664

: , , ,
1 7 3 204 1799 3620 1561 655
, , , , , 1, 0>,
3 3 4 295 1886 5399 17 328 887

: , ,− ,−
2 2 3 204 1018 4847 2369 6133 42 577
, , , ,− ,− , 1>>,

:89, 5, 10, 2, 1, 4, 5, 1, 7, 1<, 80, 1, −1, 2, 9, 1, −4, 7, −3, 1<,


3 3 4 295 943 5399 17 328 3548 40 254

:0, 0, >,
16 55 280 10 173 272 122 14
,− ,− , , ,− , ,−
3 9 9 9 9 9 99

:0, 0, 0, >,
295 131 77 77 605
205 175
, ,− ,− , ,− ,
48 6 24 48 3 24 24

:0, 0, 0, >,
1886 2028 700 3244 332 639
0, − , , ,− , ,
295 295 59 295 59 59

:0, 0, 0, >,
5399 6526 2517 1248 26 065
0, 0, − ,− , , ,−
943 943 943 943 1886

:0, 0, 0, >,
17 328 22 160 3916 153 549
0, 0, 0, , ,− ,
5399 5399 5399 10 798

:0, 0, 0, >,
3548 13 069 44 233
0, 0, 0, 0, − ,− ,−
1083 2166 5776

:0, 0, 0, >, :0, 0, 0, 0, 0, 0, 0, 0, 0, >>>


20 127 135 311 2 008 391
0, 0, 0, 0, 0, ,
3548 28 384 644 064

In[7]:= Monitor@LU@RandomReal@1, 8100, 100<D, .3D;, MatrixPlot ê@ 8L, U<D


MathTour2008-NorwayAndDenmark.nb 43

Everything fits with everything


Manipulate@DynamicModule@8lastpt = 80, 1<<,
Graphics@8PointSize@0.1D, Point@Dynamic@Refresh@If@pt =!= lastpt,
AppendTo@contents, 8pt, 80, 0<<D; lastpt = ptD, TrackedSymbols → 8pt<D;
contents = Map@If@ @@1, 2DD ≥ 0, 8 @@1DD − @@2DD, @@2DD + 80, 0.001<<,
88 @@1, 1DD, 0<, 81, −0.8< @@2DD<D &, contentsD; Map@First, contentsDDD<,
PlotRange → 880, 1<, 80, 1<<, ImageSize → 300DD, 88pt, 80, 1<<, 80, 0<,
81, 1<, Locator, Appearance → None<,
88contents, 8<<, 80, 0<, ControlType → None,
Appearance → None<,
SynchronousUpdating → TrueD

Out[8]=
44 MathTour2008-NorwayAndDenmark.nb

Other examples (from demonstrations.wolfram.com )

Mama Papa Baby


Area 11.22 25.24 1.58
BC 0.96 1.44 0.36
Area
HLinear ElementL2
Ratio of is the same!!! 12.21 12.21 12.21

Linear Element AB BC CA Perimeter A Bisector

Vertex A

Vertex B

Vertex C

Geometric objects that have the same shape and same size are called congruent. When they have the same shape but
different sizes they are called similar. Here are three similar triangles called Mama Triangle, Papa Triangle, and Baby
Triangle. You can change the position of their vertices with the sliders. Since their angles all change by the same amount
they remain similar.

But notice that even though they are different sizes, the ratio of their areas to the square of ANY linear element is the same
for all three triangles (watch the pink line in the table). A teenage Albert Einstein made use of this quality in his proof of the
Pythagorean theorem.
MathTour2008-NorwayAndDenmark.nb 45

—CAB = 75.9431±
—ABC = 74.295±
—BCA = 29.7619±
total = 180.±

74±
C
30±

76±

A
46 MathTour2008-NorwayAndDenmark.nb

angle type acute right obtuse straight reflex full

choose an angle

show radians

straight angle

180∞

1
Hx − 1L Ix2 − 4M Ix2 + 1M
In[9]:= poly =

1
H−1 + xL I−4 + x2 M I1 + x2 M
Out[9]=

In[10]:= Apart@polyD

1+x
20 H−2 + xL 6 H−1 + xL 60 H2 + xL
1 1 1
10 I1 + x2 M
Out[10]= − + +
MathTour2008-NorwayAndDenmark.nb 47

In[11]:= ManipulateB

GridB:

:StyleB , "TraditionalForm", FontSize → 24F>, 8<,


1
Hx + aL Ix2 + bM Ix2 + cM

:StyleBApartB F, "TraditionalForm", FontSize → 24F>>F,


1
Hx + aL Ix2 + bM Ix2 + cM

8a, −5, 5, 1<, 8b, −5, 5, 1<, 8c, −5, 5, 1<F

H1+xL I−5+x2 M I−2+x2M


Out[11]=
1

4 H1+xL 12 I−5+x2M 3 I−2+x2M


1 + −1+x + 1−x
48 MathTour2008-NorwayAndDenmark.nb

In[12]:= TabViewB

, 8x, −5, 5<F,


1
Hx + aL Ix2 + bM Ix2 + cM
"Plot" → ManipulateBPlotB

8a, −5, 5, 1<, 8b, −5, 5, 1<, 8c, −5, 5, 1<F,

"Formula" → ManipulateBGridB:

:StyleB , "TraditionalForm", FontSize → 24F>, 8<,


1
Hx + aL Ix2 + bM Ix2 + cM

:StyleBApartB F, "TraditionalForm", FontSize → 24F>>F,


1
Hx + aL Ix2 + bM Ix2 + cM

8a, −5, 5, 1<, 8b, −5, 5, 1<, 8c, −5, 5, 1<F>, 2, ImageSize → AutomaticF

Plot Formula

0.2

Out[12]=

0.1

-4 -2 2 4

-0.1

-0.2
MathTour2008-NorwayAndDenmark.nb 49

ü Conic sections: move foci


In[3]:= Manipulate@

dist@pt1_, pt2_D := Sqrt@Hpt1 − pt2L.Hpt1 − pt2LD;


midpt@pt1_, pt2_D := Hpt1 + pt2L ê 2;
labsty@str_D := Style@str, Italic, 20, BoldD;

8h, k< = midpt@foc1, foc2D;


cntr = 8h, k<;
c = dist@cntr, foc1D;
a := Switch@conictype, ellipse, c ∗ amult, hyperbola, c ê amult, parabola, cD;
b := Switch@conictype, ellipse, Sqrt@a ^ 2 − c ^ 2D, hyperbola, Sqrt@c ^ 2 − a ^ 2D, parabola, cD;
ang = ArcTan@foc1@@1DD − h, foc1@@2DD − kD;

vert1 = 8h, k< + a ∗ Normalize@Hfoc1 − cntrLD;


vert2 = 8h, k< + a ∗ Normalize@Hfoc2 − cntrLD;
bvert1 = 8h, k< + b ∗ 880, 1<, 8−1, 0<<.Normalize@Hfoc1 − cntrLD;
bvert2 = 8h, k< + b ∗ 880, 1<, 8−1, 0<<.Normalize@Hfoc2 − cntrLD;

ellipse, 88a Cos@tD, b Sin@tD<<,


type := Evaluate@Switch@conictype,

hyperbola, ControlActive@88a Sec@tD, b Tan@tD<<, 88a Cosh@tD, b Sinh@tD<,


8−a Cosh@tD, b Sinh@tD<, 8a Cosh@tD, −b Sinh@tD<, 8−a Cosh@tD, −b Sinh@tD<<D,
parabola, 88c t ^ 2, 2 c t<, 8c t ^ 2, −2 c t<<
DD;

Deploy@
Column@8Text @ Style@
Column@8Row@8Switch@conictype, ellipse, "ellipse", hyperbola,
"hyperbola", parabola, "parabola"D, " : ", Style@"e", Italic, 16D,
" = ", If@conictype === parabola, "", Row@8Style@"c", Italic, 16D,
"ê", Style@"a", Italic, 16D, " = "<DD, Round@c ê a, 0.01D<D,
If@conictype === parabola, Row@8Style@Sqrt@Style@"a", Italic, 16D ^ 2 −

" = ", Round@c, 0.01D<D, If@showmeas, 8Purple, Bold<, GrayDD,


Style@"c", Italic, 16D ^ 2D, WhiteD, Style@Row@8Style@"c", Italic, 16D,

Style@Sqrt@Style@"a", Italic, 16D ^ 2 − Style@"c", Italic, 16D ^ 2D, WhiteD<D,


Row@8Style@Row@8Style@"c", Italic, 16D, " = ", Round@c, 0.01D<D,
If@showmeas, 8Purple, Bold<, GrayDD, ", ",
Style@Row@8Style@"a", Italic, 16D, " = ", Style@"c", Italic, 16D,
Switch@conictype, ellipse, " ∗ ", hyperbola, " ê ", parabola, ""D,
Round@amult, 0.01D, " = ", Round@a, 0.01D<D,
If@showmeas, 8Darker@Green, 0.2D, Bold<, GrayDD, ", ",
Style@Row@8Style@"b", Italic, 16D, " = ", Switch@conictype, ellipse,
Sqrt@Style@"a", Italic, 16D ^ 2 − Style@"c", Italic, 16D ^ 2D,
hyperbola, TraditionalForm@Sqrt@Style@"c", Italic, 16D ^ 2 −
Style@"a", Italic, 16D ^ 2DD, parabola, ""D, " = ", Round@b, 0.01D<D,
If@showmeas, 8Lighter@Blue, 0.3D, Bold<, GrayDD<DD
<, Alignment → CenterD, Gray, 14D,

Show@

Map@HTake@RotationTransform@ang, cntrD@@1DD, 81, 2<, 81, 2<D.Evaluate@ D + 8h, k<L &,


ParametricPlot@

typeD, 8t, 0, 2 Pi<, MaxRecursion → ControlActive@0, 1D, AxesOrigin → 80, 0<D,


50 MathTour2008-NorwayAndDenmark.nb

If@conictype === hyperbola && showasymp,

Take@RotationTransform@ang, cntrD@@1DD, 81, 2<, 81, 2<D.810 @@1DD, 10 @@2DD< +


Graphics@Map@H8Dashing@MediumD, Orange, Line@88h, k<,

8h, k<<D<L &, 88a, b<, 8−a, b<, 8a, −b<, 8−a, −b<<DD, 8<D,

If@conictype === parabola && showdirect,

Line@88foc2, H8−Hfoc1 − foc2L@@2DD, Hfoc1 − foc2L@@1DD<L 20 + foc2<,


Graphics@8Dashing@MediumD, Darker@Green, 0.2D,

8foc2, H8Hfoc1 − foc2L@@2DD, −Hfoc1 − foc2L@@1DD<L 20 + foc2<<D<D, 8<D,

If@conictype === parabola, 8<,


If@showmeas, Graphics@8

88Darker@Green, 0.2D, Thin, 8Line@8cntr, vert1<D, Line@8cntr, vert2<D<<,


Text@Style@labsty@"a"D, Darker@Green, 0.2DD, midpt@vert1, cntrD + 80, −0.4<D,
Text@Style@labsty@"a"D, Darker@Green, 0.2DD, midpt@vert2, cntrD + 80, −0.4<D,
8Darker@Green, 0.2D, PointSize@0.02D, Point@vert1D, Point@vert2D<,
8Lighter@Blue, 0.2D, Thin, 8Line@8cntr, bvert1<D, Line@8cntr, bvert2<D<<,
Text@Style@labsty@"b"D, Lighter@Blue, 0.3DD, midpt@bvert1, cntrD + 80.4, 0<D,
Text@Style@labsty@"b"D, Lighter@Blue, 0.3DD, midpt@bvert2, cntrD + 80.4, 0<D,
8Lighter@Blue, 0.3D, PointSize@0.02D, Point@bvert1D, Point@bvert2D<<D,
8Purple, Thick, Dashing@SmallD, Line@8foc1, foc2<D<,
Text@Style@labsty@"c"D, PurpleD, midpt@foc1, cntrD + 80, 0.4<D,
Text@Style@labsty@"c"D, PurpleD, midpt@foc2, cntrD + 80, 0.4<D<D, 8<D,

8Purple, PointSize@0.02D, Point@foc2D<, 8Red, PointSize@0.02D, Point@8h, k<D<


Graphics@88Purple, PointSize@0.02D, Point@foc1D<,

<D,

Frame → False, Axes → True, PlotRange → 7,


GridLines → 8Table@8n , Lighter@Gray, 0.7D<, 8n, −7, 7, 1<D,
Table@8n, Lighter@Gray, 0.7D<, 8n, −7, 7, 1<D<, BaseStyle → 812<, ImageSize → 300
D
<, Alignment → CenterD
D,

88conictype, ellipse, ""<,


8ellipse → "ellipse", hyperbola → "hyperbola", parabola −> "parabola"<,
ControlType → RadioButtonBar, ControlPlacement → Left, Appearance → "Vertical"<,

88showmeas, False, "measurements"<, 8True, False<, ControlPlacement → Right<,


88showasymp, True, "asymptotes"<, 8True, False<,
ControlPlacement → Right, Enabled → If@conictype === hyperbola, True, FalseD<,
88showdirect, True, "directrix"<, 8True, False<, ControlPlacement → Right,
Enabled → If@conictype === parabola, True, FalseD<,

88foc1, 81, 0<<, 8−6, −6<, 86, 6<, Locator, Appearance → Style@"Ê", Opacity@0DD<,
88foc2, 8−1, 0<<, 8−6, −6<, 86, 6<, Locator<,

88amult, 1.5, Row@8" ", "adjust ", Text @ Style@"a", Italic, 14D<D<,
1, 3, Enabled → If@conictype === parabola, False, TrueD<,

TrackedSymbols 8foc1, foc2, amult, conictype, showasymp, showdirect, showmeas<,


AutorunSequencing → Automatic, ControllerLinking → True
D
MathTour2008-NorwayAndDenmark.nb 51

adjust a

ellipse : e = cêa = 0.67


ellipse measurements

hyperbola asymptotes

parabola c = 1., a = c * 1.5 = 1.5, b = a2 - c2 = 1.12 directrix

Out[3]=
2

-6 -4 -2 2 4 6

-2

-4

-6

In[14]:= Manipulate@
DynamicModule@8h, k, type, label<,
h := center@@1DD;
k := center@@2DD;

ellipse, Hx − hL ^ 2 ê a ^ 2 + Hy − kL ^ 2 ê b ^ 2 1,
type := Evaluate@Switch@conictype,

parabolax, Hy − kL ^ 2 4 c Hx − hL,
parabolay, Hx − hL ^ 2 4 c Hy − kL,
hyperbolax, Hx − hL ^ 2 ê a ^ 2 − Hy − kL ^ 2 ê b ^ 2 1,
hyperbolay, −Hx − hL ^ 2 ê a ^ 2 + Hy − kL ^ 2 ê b ^ 2 1
DD;
labels = 88"ellisse", "ellipse"<, 8"parabola", "parabola"<,
8"iperbole", "hyperbola"<, 8" centro ", " center "<, 8" fuochi ", " foci
8" direttrici ", " directrices "<, 8" asintoti ", " asymptotes "<<;
"<,

labelselect@i_D := labels@@i, Switch@language, "it", 1, "en", 2DDD;

ellipse, HoldForm@Hx − Style@"h", ItalicDL ^ 2 ê Style@"a", ItalicD ^ 2 +


label := Evaluate@Switch@conictype,

Hy − Style@"k", ItalicDL ^ 2 ê Style@"b", ItalicD ^ 2 = 1D,


parabolax, HoldForm@Hy − Style@"k", ItalicDL ^ 2 =
4 Style@"c", ItalicD Hx − Style@"h", ItalicDLD,
parabolay, HoldForm@Hx − Style@"h", ItalicDL ^ 2 =
4 Style@"c", ItalicD Hy − Style@"k", ItalicDLD,
hyperbolax, HoldForm@Hx − Style@"h", ItalicDL ^ 2 ê Style@"a", ItalicD ^ 2 −
Hy − Style@"k", ItalicDL ^ 2 ê Style@"b", ItalicD ^ 2 = 1D,
hyperbolay, HoldForm@−Hx − Style@"h", ItalicDL ^ 2 ê Style@"a", ItalicD ^ 2 +
Hy − Style@"k", ItalicDL ^ 2 ê Style@"b", ItalicD ^ 2 = 1D
52 MathTour2008-NorwayAndDenmark.nb

DD;

Deploy@
Grid@8

8Text @ Style@TraditionalForm@Evaluate@labelDD, 20, Darker@Blue, 0.3DD<,

If@conictype === parabolax »» conictype === parabolay,


8Text @ Style@Row@8Style@"c", ItalicD, " = ", c, ";
" = ", h, ", ", Style@"k", ItalicD, " = ", k<D, GrayD<, 8Text @ Style@
", Style@"h", ItalicD,

Row@8Style@"a", ItalicD, " = ", a, ", ", Style@"b", ItalicD, " = ", b, "; ",
Style@"h", ItalicD, " = ", h, ", ", Style@"k", ItalicD, " = ", k<D, GrayD<D,

8Dynamic@Show@

8x, −7, 7<, 8y, −7, 7<, MaxRecursion → 2, ImagePadding → 0D,


ContourPlot@Evaluate@typeD,

If@showasymp && Or@conictype === hyperbolax, conictype === hyperbolayD,

Line@888h, k<, 81000 a + h, 1000 b + k<<, 88h, k<, 8−1000 a + h, 1000 b + k<<,
Graphics@8Dashing@MediumD, Orange,

88h, k<, 81000 a + h, −1000 b + k<<, 88h, k<, 8−1000 a + h, −1000 b + k<<<D<D, 8<D,

If@showcenter, Graphics@8Red, PointSize@0.02D, Point@8h, k<D<D, 8<D,

If@showfoci,
Graphics@8Purple, PointSize@0.02D,

hyperbolax, Point@88Sqrt@a ^ 2 + b ^ 2D + h, k<, 8−Sqrt@a ^ 2 + b ^ 2D + h, k<<D,


Switch@conictype,

hyperbolay, Point@88h, Sqrt@a ^ 2 + b ^ 2D + k<, 8h, −Sqrt@a ^ 2 + b ^ 2D + k<<D,


ellipse, If@a ≥ b, Point@88Sqrt@a ^ 2 − b ^ 2D + h, k<, 8−Sqrt@a ^ 2 − b ^ 2D + h, k<<D,
Point@88h, Sqrt@b ^ 2 − a ^ 2D + k<, 8h, −Sqrt@b ^ 2 − a ^ 2D + k<<DD,
parabolax, Point@8c + h, k<D, parabolay, Point@8h, c + k<DD
<D, 8<D,

If@showdirect,
Graphics@8Darker@Green, 0.2D, Dashing@MediumD,

hyperbolax, Line@888−a ^ 2 ê Sqrt@a ^ 2 + b ^ 2D + h, −10<, 8−a ^ 2 ê Sqrt@a ^ 2 + b ^ 2D + h,


Switch@conictype,

10<<, 88a ^ 2 ê Sqrt@a ^ 2 + b ^ 2D + h, −10<, 8a ^ 2 ê Sqrt@a ^ 2 + b ^ 2D + h, 10<<<D,


hyperbolay, Line@888−10, −b ^ 2 ê Sqrt@a ^ 2 + b ^ 2D + k<, 810, −b ^ 2 ê Sqrt@a ^ 2 + b ^ 2D +
k<<, 88−10, b ^ 2 ê Sqrt@a ^ 2 + b ^ 2D + k<, 810, b ^ 2 ê Sqrt@a ^ 2 + b ^ 2D + k<<<D,
ellipse, If@a ≥ b, Line@888−a ^ 2 ê Sqrt@a ^ 2 − b ^ 2D + h, −10<,
8−a ^ 2 ê Sqrt@a ^ 2 − b ^ 2D + h, 10<<, 88a ^ 2 ê Sqrt@a ^ 2 − b ^ 2D + h, −10<,
8a ^ 2 ê Sqrt@a ^ 2 − b ^ 2D + h, 10<<<D,
Line@888−10, −b ^ 2 ê Sqrt@b ^ 2 − a ^ 2D + k<, 810, −b ^ 2 ê Sqrt@b ^ 2 − a ^ 2D + k<<,
88−10, b ^ 2 ê Sqrt@b ^ 2 − a ^ 2D + k<, 810, b ^ 2 ê Sqrt@b ^ 2 − a ^ 2D + k<<<DD,
parabolax, Line@88h − c, −10<, 8h − c, 10<<D, parabolay,
Line@88−10, k − c<, 810, k − c<<DD
<D, 8<D,

Frame → False, Axes → True, PlotRange → 7,


GridLines → 8Table@8n , Lighter@Gray, 0.7D<, 8n, −7, 7, 1<D,
Table@8n, Lighter@Gray, 0.7D<, 8n, −7, 7, 1<D<, BaseStyle → 812<, ImageSize → 300DD
<,

8Row@8Checkbox@Dynamic@showcenterD, 8False, True<D,


Style@Dynamic@labelselect@4DD, "Label"D, Checkbox@Dynamic@showfociD,
MathTour2008-NorwayAndDenmark.nb 53

8False, True<D, Style@Dynamic@labelselect@5DD, "Label"D,


Checkbox@Dynamic@showdirectD, 8False, True<D,

Checkbox@Dynamic@showasympD, 8False, True<D, Style@If@conictype === hyperbolax »»


Style@Dynamic@labelselect@6DD, "Label"D,

conictype === hyperbolay, Dynamic@labelselect@7DD, " −−−−−−−−"D, "Label"D<D<

<, Alignment → 8Center, Top<, ItemSize → 8"Rows" → 84, 0.5, Automatic<<D


D
D,
88language, "it", "Language"<, 8"it", "en"<,
ControlPlacement → Left, ControlType → PopupMenu<,

88conictype, ellipse, ""<, 8ellipse → Dynamic@labelselect@1DD, parabolax → "parabola−x",


Delimiter,

parabolay → "parabola−y", hyperbolax → Dynamic@StringJoin@labelselect@3D, "−x"DD,


hyperbolay → Dynamic@StringJoin@labelselect@3D, "−y"DD<,
ControlType → RadioButtonBar, ControlPlacement → Left, Appearance → "Vertical"<,

88center, 80, 0<, Dynamic@labelselect@4DD<,


Delimiter,

8−2, −2<, 82, 2<, ControlType → Slider2D, ControlPlacement → Left<,


88b, 3<, 0.01, 6, Enabled → Not@Or@conictype === parabolax, conictype === parabolayDD,
ControlType → Slider, ControlPlacement → Top<,
88c, 1<, −6, 6, Enabled → Or@conictype === parabolax, conictype === parabolayD,
ControlPlacement → Top, ControlType → Slider<,
88a, 2, "a"<, 0.01, 6, Enabled → Not@Or@conictype === parabolax, conictype === parabolayDD,
ControlPlacement → Top, ControlType → Slider<,

8a, b, c, center, conictype, showcenter, showfoci, showdirect, showasymp<,


TrackedSymbols

ControllerLinking → True
D
54 MathTour2008-NorwayAndDenmark.nb

Hx-hL2 Hy-kL2
Language it

a2
+ =1
ellisse
b2
a = 2, b = 3; h = 0, k = 0
parabola-x

parabola-y
6
iperbole-x

iperbole-y
4
Out[14]=

2
centro

-6 -4 -2 2 4 6

-2

-4

-6

centro fuochi direttrici --------

You might also like