mathematica11€¦ · mathematica11.3 what is mathematica? mathematica is a fully integrated...
TRANSCRIPT
Mathematica11.3
What is Mathematica?Mathematica is a fully integrated environment for technical computing. It was written by Stephen Wolfram 25 years ago, when he was 29 years old.
The website www.wolfram.com gives a wealth of information, tutorials, demonstrations, code, benchmarks, cloud services, machine learning applications, etc.
Mathematica as a Calculator
Type in questions, and get back answers
A simple computation.
In[1]:= 6^20
Out[1]= 3 656 158 440062 976
Numbers of any size.
In[2]:= 6^200
Out[2]= 426 825 223812 027 400 796 974 891518 773 732 342 988 745354 489 429 495 479 078935 112 929 549
619 739 019072 139 340 757 097 296812 815 466 676 129 830954 465 240 517 595 242384 015 591 919
845 376
Standard mathematical notation, using palettes or from the keyboard (type: <escape>^<escape>, ^ ).
In[3]:= 6200
Out[3]= 426 825 223812 027 400 796 974 891518 773 732 342 988 745354 489 429 495 479 078935 112 929 549
619 739 019072 139 340 757 097 296812 815 466 676 129 830954 465 240 517 595 242384 015 591 919
845 376
Calculate the eigenvalues of a matrix.
In[4]:= Eigenvalues1 2 34 5 67 8 9
Out[4]= 3
25 + 33 ,
3
25 - 33 , 0
Handle formulas as well as numbers.
Solve an equation.
In[5]:= Solve x + a == 2 x, x
Out[5]= x →1
81 + 4 a - 1 + 8 a , x →
1
81 + 4 a + 1 + 8 a
Compute an integral.
In[6]:= x a + x ⅆx
Out[6]=1
4 x a + x a + 2 x - a2 Log x + a + x
Manipulate
In[7]:= Expand(x + y)2
Out[7]= x2 + 2 x y + y2
In[8]:= ManipulateExpand(x + y)n, {n, 1, 100, 1}
Out[8]=
n
x + y
Mathematica is very precise:
In[9]:= N[π, 1 000 000]
In[10]:= N2
27 + 3 69
1/3
+1
3
1
227 + 3 69
1/3 30 000
, 6000
Note the many consecutive zero's in this remarkable Pisot number.
2D and 3D graphics.Mathematica can generate many different types of plot.
A 2D plot of a simple function.
2 IntroductionMathematica11.nb
In[11]:= Plot[Sin[x] + Sin[1.6 x], {x, 0, 40}]
Out[11]=10 20 30 40
-2
-1
1
2
A 3D plot.
In[12]:= Plot3D[Sin[x] Sin[y], {x, 0, 2 π}, {y, 0, 2 π}, PlotPoints → 30]
Out[12]=
A closely related syntax will generate a contour plot.
IntroductionMathematica11.nb 3
In[13]:= ContourPlot[Sin[x] Sin[y], {x, 0, 2 π}, {y, 0, 2 π}, PlotPoints → 30]
Out[13]=
0 1 2 3 4 5 6
0
1
2
3
4
5
6
Similar commands in Mathematica have a closely related syntax.
In[14]:= ClebschSurf = 16 x3 + 16 y3 - 31 z3 + 24 x2 z - 48 x2 y - 48 x y2 + 24 y2 z - 54 3 z2 - 72 z;
4 IntroductionMathematica11.nb
In[15]:= clebschgr = ContourPlot3D[ClebschSurf, {x, -2.5, 2.5},
{y, -2.5, 2.5}, {z, -2.5, 2.5}, Contours → {1}, Mesh → None, PlotPoints → 50,
ContourStyle → {{SurfaceColor[RGBColor[.6, .4, .1], White, 200]}},
Background → Black, Boxed → False, Axes → False, ImageSize → 500,
ViewPoint → {2.043, -2.684, 0.262}, SphericalRegion → True, Mesh → All]
Out[15]=
GUISquare brackets [ ] for functions only. Round brackets for grouping (x + y)2. Curled brackets for lists: {1,2,3,4,5}
Type Greek letter, mathematical symbols with the escape key before and after: a →α
Every Mathematica factory function is with a capital: Sin[x], own functions start with lower case letter.
Prefix or Postfix functions:
In[16]:= N[π]
Out[16]= 3.14159
In[17]:= π // N
Out[17]= 3.14159
IntroductionMathematica11.nb 5
MapThe function Map does a function on every element of the list:
In[18]:= Map[f, {a, b, c, d, e}]
Out[18]= {f[a], f[b], f[c], f[d], f[e]}
Pattern matchingPattern matching (/. is the Replace function, the underscore stands for anything):
In[19]:= {a, b, d, t, k + w, r} /. p_ + q_ → pq
Out[19]= a, b, d, t, kw, r
Close cell groups by double click on the cell bracket.
CameraStart and stop the webcam, capture images:
In[20]:= ImageCapture[]
Continuously capture a stream of images
In[21]:= Dynamic[CurrentImage[]]
Capture an image with the built-in camera:
Dynamic capturing and processing:
In[22]:= Dynamic[EdgeDetect[CurrentImage[], 3]]
Time-lapse recording of image frames:
In[23]:= Table[ Pause[1]; CurrentImage[], {5}]
Create an animation from a series of captured images:
In[24]:= ListAnimate[CurrentImage[30]]
A simple dynamic motion detector:
In[25]:= Dynamic[ImageDifference @@ CurrentImage[2]]
Interactive face detection:
In[26]:= Dynamic[HighlightImage[CurrentImage[], FindFaces[CurrentImage[]]]]
FunctionsThere are over 2500 functions in Mathematica. They are all designed carefully to work together. This makes Mathematica much more than a collection of different functions.
Mathematica is numerically fast:
6 IntroductionMathematica11.nb
In[27]:= denseMatrix = RandomReal[{0, 1}, {1000, 1000}];
Dimensions[denseMatrix]
Out[28]= {1000, 1000}
In[29]:= Timing[Inverse[denseMatrix];]
Out[29]= {0.078125, Null}
Data Analysis
With Mathematica least squares fitting of some data is very straight-forward. First some data is generated.
In[30]:= data = Tablei, RandomReal[] + 4 Sin i , {i, 0.`, 20, 0.1`};
A plot of the discrete data:
In[31]:= p1 = ListPlot[data]
Out[31]=
5 10 15 20
-4
-2
2
4
A fit of the data:
In[32]:= Fitdata, 1, x, x2, x3, x4, x5, x
Out[32]= 1.90958 + 2.21506 x - 0.594047 x2 + 0.0559482 x3 - 0.0024374 x4 + 0.0000408601 x5
Plot the fitted function:
In[33]:= p2 = Plot[%, {x, 0, 20}, PlotStyle → Red]
Out[33]=
5 10 15 20
-2
2
4
Plot the fit and the data together:
IntroductionMathematica11.nb 7
In[34]:= Show[p1, p2]
Out[34]=
5 10 15 20
-4
-2
2
4
After carrying out simple data fitting one might wish to do more sophisticated analysis using the statistics functionality that Mathematica provides.
Fourier
We generate a collection of data.
In[35]:= data = Table[RandomReal[] + 50 Sin[2 x], {x, 0., 300}];
It is easy to make a plot of this data.
In[36]:= ListPlot[data]
Out[36]=50 100 150 200 250 300
-40
-20
20
40
Now we can take the Fourier transform of the data and plot the absolute value.
8 IntroductionMathematica11.nb
In[37]:= ListPlot[Abs[Fourier[data]], Joined → True]
Out[37]=
50 100 150 200 250 300
2
4
6
8
10
12
14
Solving Differential Equations
Solve a nonlinear differential equation ⇒ interpolating function which implicitly represents the whole solution.
In[38]:= NDSolve[{x''[t] + x[t]^3 == Sin[t], x[0] == x'[0] == 0},
x, {t, 0, 50}]
Out[38]= x → InterpolatingFunctionDomain: {{0., 50.}}Output: scalar
A phase space plot of the solution.
IntroductionMathematica11.nb 9
In[39]:= ParametricPlot[Evaluate[{x[t], x′[t]} /. %], {t, 0, 50}]
Out[39]=-2 -1 1 2
-2
-1
1
2
Note how easy it is to take the result of the differential equation, differentiate it and plot the result. This is an example of good design.
Mathematica NotebooksMathematica notebooks are general purpose technical electronic document that collect computa-tions, text, pictures, formulae, tables and animations in a single interactive document.
The document is arranged with a hierarchy of different types of cells. There are title cells, section cells, text cells, graphics cells, and input and output cells.
These are input and output cells.
In[40]:= TrigToExp[ ArcSinh[ω]]
Out[40]= Logω + 1 + ω2
Each cell has a particular appearance which is set by a style sheet. There are many different types of style sheet. Clicking on the different buttons here demonstrates a few of the different styles that are available.
Article Default Textbook
Click here to restore style.
10 IntroductionMathematica11.nb
Palettes
Mathematica notebooks can contain interactive hypertext buttons which do various types of things. The BasicInput palette allows entries of greek letters and complete mathematical templates.
In[41]:= 1 + (α + λ)6
Out[41]= 1 + (α + λ)6
The Help System
One large application of Mathematica notebooks is the help system. This can look up some specific function to get detailed information including examples:
In[42]:= ? LinearSolve
LinearSolve[m, b] finds an x that solves the matrix equationm.x == b.
LinearSolve[m] generates a
LinearSolveFunction[…] that can be applied repeatedly to different b.
In addition you can look up some general topic such as PDE, to find out what Mathematica has to offer. Whenever you want to see what Mathematica can do the help system is a good place to start.
ManipulateA pretty interactive graphic:
IntroductionMathematica11.nb 11
In[43]:= Manipulate[
Graphics[Polygon[Table[r^n {-Sin[n * a], Cos[n * a]}, {n, 0, L}]], PlotRange → P],
{P, 1, 0}, {{r, .98}, 1, .95}, {{a, 2.6}, 0, 2 Pi}, {{L, 300}, 1, 1000, 1}]
Out[43]=
P
r
a
L
ManipulateInteractivity is done with Manipulate. This function creates a Java applet, and any parameter can be interactively studied. See for many examples the Documentation Center (i.e. Help Browser), and the many examples on the website "Wolfram Demonstration Project": www.wolfram.com.
In[44]:= ManipulateExpand(x + y)n, {n, 1, 100, 1}
Out[44]=
n
x + y
You can make many sliders:
12 IntroductionMathematica11.nb
In[45]:= Manipulate[Plot[a Sin[ω t + ϕ], {t, 0, 2 π}, PlotRange → {-2, 2}],
{{a, 1, "Amplitude"}, 0, 2}, {{ω, 2, "Frequency"}, 0, 20}, {{ϕ, 0, "Phase"}, 0, 2 π}]
Out[45]=
Amplitude
Frequency
Phase
1 2 3 4 5 6
-2
-1
1
2
Check the Help browser for the function Manipulate and Locator.
Demonstrations project: http://demonstrations.wolfram.com/
Designing Optical SystemsEverything can be made interactive in Mathematica, it is ideal for design. Here follows a more advanced application, the design of an optical system. Run the definitions of the required functions first.
Run the code below, and play with the ray tracing plot and the lenses. Every point can be moved with the mouse.
Documentation CenterThe Documentation Center gives many tutorials, pointers and explanations. See for an efficient introduction also the many screencasts (streaming videos): http://www.wolfram.com/broad-cast/screencasts/.
Below are some additional examples of the functionality of Mathematica.
IntroductionMathematica11.nb 13
Transparency in 3D graphics
In[46]:= Graphics3D[{EdgeForm[], Opacity[.5], Cylinder /@ RandomReal[5, {20, 2, 3}]}]
Out[46]=
More interactive visualizations (examples by Wolfram)
Chemical dataAnother example is the database with chemical data:
In[76]:= ChemicalData["Caffeine"]
Out[76]=
O
O NNN
NNNNNN
NN
Study the plot interactively in 3D:
14 IntroductionMathematica11.nb
In[77]:= ChemicalData["Caffeine", "MoleculePlot"]
Out[77]=
In[78]:= ChemicalData["*athion"]
Out[78]= methyl parathion , parathion , methidathion ,
malathion , dioxathion , amino-parathion , isoxathion
In[79]:= Map[ChemicalData[#, "MoleculePlot"] &, ChemicalData["*athion"]]
Out[79]= , ,
, ,
IntroductionMathematica11.nb 15
,
,
In[80]:= Grid[Map[{#, ChemicalData[#, "MoleculePlot"]} &, ChemicalData["*athion"]], Frame → All]
16 IntroductionMathematica11.nb
methyl parathion
parathion
methidathion
IntroductionMathematica11.nb 17
Out[80]=
malathion
dioxathion
amino-parathion
18 IntroductionMathematica11.nb
isoxathion
See ChemicalData for more information.
In[81]:= TabView[
Show[CountryData[#, "Shape"], ImageSize → {40, 40}] → DateListPlot[CountryData[#,
{{"GDP"}, {1970, 2005}}], PlotLabel → #] & /@ CountryData["GroupOf8"]]
Out[81]=
1970 1980 1990 20000
2.0×1011
4.0×1011
6.0×1011
8.0×1011
1.0×1012
1.2×1012Canada
IntroductionMathematica11.nb 19