bàeinführung in mathematica (4) 8
TRANSCRIPT
Einführung in Mathematica (4)
Manipulate und graphische Elemente
Michael O. Distler, Computer in der Wissenschaft, SS 2010 (Vorlage von L. Tiator)
Grafik - Initialisierung
In[1]:= SetOptions@8Plot, ListPlot, ParametricPlot, Plot3D, Graphics<,BaseStyle ® 818, FontFamily ® "Times", Italic<D;
SetOptions@Plot, PlotStyle ® [email protected]<D;SetOptions@ListPlot, PlotStyle ® 8Red, [email protected]<D;
dünne Linien und Punkte :
SetOptions@8Plot, ListPlot, Graphics<,BaseStyle ® 8Medium, FontFamily ® "Times"<D;
SetOptions@Plot, PlotStyle ® [email protected]<D;SetOptions@ListPlot, PlotStyle ® 8Red, [email protected]<D;
andere mögliche Fonts :
BaseStyle ® 818, FontFamily ® "Helvetica"<BaseStyle ® 8Large, FontFamily ® "Helvetica", Italic, Bold<
Beispiele mit Manipulate
� Manipulate einen parameter in diskreten Schritten :
Manipulate@Factor@x^n - 1D, 8n, 10, 100, 1<D
n
H-1 + xL H1 + xL I1 - x + x2 - x3 + x4M I1 + x + x2 + x3 + x4M
ManipulateBà xn ã-x2 âx, 8n, -10, 10, 1<F
n
-
ã-x2 I105 - 30 x2 + 12 x4 - 8 x6 + 16 x8 + 16 ãx2 Π x9 Erf@xDM
945 x9
� Table Grids
Manipulate@Grid@Table@8i, i^m<, 8i, 1, n<D,Alignment ® Left, Frame ® AllD, 8n, 3, 20, 1<, 8m, 2, 100, 1<D
n
m
1 1
2 4
3 9
ManipulateBà xn ã-x2 âx, 8n, -10, 10, 1<F
n
-
ã-x2 I105 - 30 x2 + 12 x4 - 8 x6 + 16 x8 + 16 ãx2 Π x9 Erf@xDM
945 x9
� Table Grids
Manipulate@Grid@Table@8i, i^m<, 8i, 1, n<D,Alignment ® Left, Frame ® AllD, 8n, 3, 20, 1<, 8m, 2, 100, 1<D
n
m
1 1
2 4
3 9
2 Mathematica_4.nb
� Locator
Manipulate@Graphics@8Line@Table@88Cos@tD, Sin@tD<, pt<, 8t, 2. Pi�n, 2. Pi, 2. Pi�n<DD<,
PlotRange ® 1D, 88n, 30<, 1, 200, 1<, 88pt, 80, 0<<, Locator<D
n
� multiple locators
Strg - Alt - Click zum Erzeugen von neuen locators oder zum Löschen
Mathematica_4.nb 3
Manipulate@Module@8x<,Plot@Fit@points, Table@x^i, 8i, 0, order<D, xD, 8x, -2, 2<,PlotRange ® 2, ImageSize ® 400, Evaluated -> TrueDD,
88order, 3<, 1, 10, 1, Appearance ® "Labeled"<,88points, RandomReal@8-2, 2<, 85, 2<D<,Locator, LocatorAutoCreate ® True<D
order 3
-2 -1 1 2
-2
-1
1
2
� 3D Graphik
Manipulate kann auch für 3 D Graphiken benutzt werden.
Allerdings muss das Leistungsvermögen des Rechners berücksichtigt werden.
Betrachten wir folgendes einfaches Beispiel :
4 Mathematica_4.nb
Manipulate@Plot3D@Sin@n x yD, 8x, 0, 3<, 8y, 0, 3<, Mesh ® NoneD, 8n, 1, 5<D
n
0
1
2
30
1
2
3
-1.0
-0.5
0.0
0.5
1.0
Mathematica_4.nb 5
� Elektrostatisches Potential von zwei Punktladungen :
Manipulate@ContourPlot@q1� Norm@8x, y< - p@@1DDD + q2� Norm@8x, y< - p@@2DDD,
8x, -2, 2<, 8y, -2, 2<, Contours ® 10D,88q1, -1<, -3, 3<, 88q2, 1<, -3, 3<, 88p, 88-1, 0<, 81, 0<<<,
8-1, -1<, 81, 1<, Locator<, Deployed ® TrueD
q1
q2
-2 -1 0 1 2
-2
-1
0
1
2
6 Mathematica_4.nb
� Stehende Wellen
Manipulate@Plot@Sin@n Π xD Cos@ tD,8x, 0, 1<, PlotRange ® 8-1, 1<, Filling ® AxisD,
TextCell@"Stehende Wellen", "Subtitle"D, Delimiter,
88n, 4<, 1, 10, 1, ImageSize ® Large<,8t, 0, 2 Π, ImageSize ® Large<D
Stehende Wellen
n
t
0.2 0.4 0.6 0.8 1.0
-1.0
-0.5
0.5
1.0
Manipulate Elemente
� Slider
ã kontinuierlich
88dist, 2.37, "distance"<, 1, 3<
Mathematica_4.nb 7
ã diskret in Schritten von 0.1
Manipulate@dist,88dist, 2.5, "distance"<, 1, 3, .1< D
distance
2.5
ã options
Appearance ® "Labeled" oder : Appearance ® "Open",
ImageSize ® Small oder : ImageSize ® Large, ImageSize ® Tiny
ControlPlacement ® Left oder : Right, Top, Bottom
� VerticalSlider
Manipulate@plotrange,88plotrange, 100, "PlotRange"<, 81, 10, 20, 50, 100, All<,ControlType ® VerticalSlider, ControlPlacement ® Left< D
PlotRange 100
8 Mathematica_4.nb
� Dropdown Box (PopupMenu)
Manipulate@year,88year, 2010, "Year"<, Table@i, 8i, 2010, 2030<D< D
Year 2010
2010
Die angezeigten Werte müssen nicht unbedingt auch die tatsächlichen Werte in der Funktion sein.
Manipulate@month,88month, 1, "Month"<, 81 -> "January", 2 -> "February",
3 -> "March", 4 -> "April", 5 -> "May", 6 -> "June",
7 -> "July", 8 -> "August", 9 -> "September",
10 -> "October", 11 -> "November", 12 -> "December"<< D
Month January
1
Die Erzeugung einer Dropdown Box kann auch erzwungen werden.
Manipulate@plotrange,88plotrange, 100, "PlotRange"<,
81, 10, 100, All<, ControlType ® PopupMenu< D
PlotRange 100
100
Mathematica_4.nb 9
� Setter
Manipulate@plotrange,88plotrange, 100, "axes ratio"<, 81, 10, 100, All<< D
axes ratio 1 10 100 All
100
Manipulate@plotrange,88plotrange, 100, "axes ratio"<,
81, 5, 10, 50, 100, 500, 1000, All<, ControlType ® Setter<D
axes ratio 1 5 10 50 100 500 1000 All
100
Manipulate@Grid@88plotrange, star1<<, Frame ® AllD,88star1, -26, "object 1"<,
8-26 ® "Sun", -12 ® "Moon", 0.1 ® "Vega", -1.5 ® "Sirius",
-0.9 ® "Canopus", 0.1001 ® "Alpha Centauri",
1.3 ® "Regulus", 2.1 ® "Polaris"<, ControlType ® Setter<,88plotrange, 100, "axes ratio"<,
81, 5, 10, 50, 100, 500, 1000, All<, ControlType ® Setter<D
object 1 Sun Moon Vega Sirius Canopus Alpha Centauri Regulus Polaris
axes ratio 1 5 10 50 100 500 1000 All
100 -26
10 Mathematica_4.nb
� Radio Button
Manipulate@plotrange,88plotrange, 100, "PlotRange"<,
81, 10, 100, All<, ControlType ® RadioButtonBar< D
PlotRange 1 10 100 All
100
� InputField
Manipulate@plotrange,88plotrange, 100, "PlotRange"<,ControlType ® InputField , FieldSize ® 5<D
PlotRange 100
100
� Animator
Manipulate@Prime@nD,88n, 1, "Primzahlen"<, 1, 100, 1, ControlType ® Animator <D
Primzahlen
271
standardmäßig beginnt eine Animation automatisch, im nächsten Beispiel muss sie von Hand
gestratet werden.
Mathematica_4.nb 11
Manipulate@Prime@nD,88n, 1, "Primzahlen"<, 1, 100, 1,
ControlType ® Animator , AnimationRunning ® False<D
Primzahlen
2
auch uneigentliche Bereiche bis ¥ sind möglich
Manipulate@Prime@nD,88n, 1, "Primzahlen"<, 1, Infinity, 1 <D
Primzahlen
30119
� Trigger
Manipulate@Prime@nD,88n, 1, "Primzahlen"<, 1, Infinity, 1, ControlType ® Trigger<D
Primzahlen
107
12 Mathematica_4.nb
� Checkbox
Manipulate@Plot@Sin@xD, 8x, 0, 2 Π<, Frame ® frameD,88frame, False, "Plotrahmen"<,
8True, False<, ControlType ® Checkbox<D
Plotrahmen
1 2 3 4 5 6
-1.0
-0.5
0.5
1.0
� Button
clicks = 0;
Button@"throw", clicks++; Print@clicksDDthrow
Mathematica_4.nb 13
In[7]:= Manipulate@If@NewNumb, rand = Table@Random@D, 8n<D; NewNumb = FalseD;ListPlot@rand, PlotRange ® 80, 1<,PlotStyle ® 8Hue@hD, PointSize@pointsizeD<D,
88n, 10<, 85, 10, 20, 50, 100, 500, 1000<,ControlType ® RadioButton, ControlPlacement ® Bottom<,
88pointsize, 0.035, "size"<, 0.001, 0.1,
ControlType ® VerticalSlider, ControlPlacement ® Right<,Button@"repeat with new numbers",
NewNumb = True, Background ® YellowD,88h, 0, "Farbe HHueL"<, 0, 1, Appearance ® "Labeled"<D
Out[7]=
repeat with new numbers
Farbe HHueL 0
0 2 4 6 8 10
0.2
0.4
0.6
0.8
1.0
size
n 5 10 20 50 100 500 1000
Wird h von 0 nach 1 variiert, ändert sich die zu Hue@hD gehörige Farbe von rot,
über gelb, grün, cyan, blau, magenta wieder zurück nach rot.
14 Mathematica_4.nb
� Locator
Manipulate@Graphics@Line@880, 0<, p<D, PlotRange ® 2D,88p, 81, 1<<, Locator<D
Manipulate@ListPlot@points,PlotRange ® 880, 2<, 80, 2<<, Joined ® True, Axes ® NoneD,
88points, 881, 1<<<, Locator, LocatorAutoCreate ® True<D
Mathematica_4.nb 15
� ControlPlacement
mit ControlPlacement können die Elemente an verschiedenen Stellen platziert werden: Top(de-
fault), Bottom, Left and Right
ControlPlacement ® Bottom
ControlPlacement ® Bottom
Manipulate@dist,88dist, 2.5, "distance"<, 1, 3, .1<, ControlPlacement ® Left D
distance2.5
die Elemente können auch rundum verteilt werden:
ManipulateA"abc = " <> ToString@a^b^cD,88a, 2<, 1, 10, 1, ImageSize ® Tiny, ControlPlacement ® Left<,88b, 2<, 1, 10, 1, ImageSize ® Tiny, ControlPlacement ® Right<,88c, 2<, 1, 10, 1, ImageSize ® Large, ControlPlacement ® Bottom<
E
aab
c= 16
b
c
� ControlActive
Manche Berechnungen oder Grafiken benötigen zuviel Zeit, was beim Manipulieren als störend
empfunden wird. In solchen Fällen kann man während der Manipulationen mit gröberen Parame-
tern rechnen und sobald alle Parameter eingestellt sind, kann die Rechnung bzw der Plot mit
gewünschter Genauigkeit und Auflösung erfolgen.
PlotPoints ® ControlActive@5, 50D
16 Mathematica_4.nb
Manipulate@Plot@Sin@1� Hx - aLD, 8x, 0, 1<,PlotPoints ® ControlActive@3, 200D, PlotRange ® 1D, 8a, 0, 1<D
a
0.2 0.4 0.6 0.8 1.0
-1.0
-0.5
0.5
1.0
� SaveDefinitions
Jedes Manipulate sollte als letzte Aktion die Option:
SaveDefinitions->True
enthalten. Dann werden alle notwendigen Definitionen (Funktionen etc) mit dem Manipulate
abgespeichert und die Manipulation kann später direkt ausgeführt werden.
SaveDefinitions ® True
SaveDefinitions ® True
Graphics Elemente
� Objekte
ã Punkt
el@1, 1D = Point@80, 0<D;el@2, 1D = [email protected], Point@80, 0<D<;el@3, 1D = 8Lighter@Red, 0.9D, [email protected], Point@80, 0<D<;
Mathematica_4.nb 17
ã Kreislinie
el@1, 2D = Circle@80, 0<, 0.7D;el@2, 2D = 8Thick, Blue, Circle@80, 0<, 0.7D<;el@3, 2D = 8Thick, Blue, Dotted, Circle@80, 0<, 80.8, 0.4<D<;
ã Kreisscheibe
el@1, 3D = Disk@80, 0<, 0.6D;el@2, 3D = 8Darker@Green, 0.4D, Disk@80, 0<, 0.6D<;el@3, 3D = 9Green, DiskA80, 0<, 0.8, 920 °, 260 °=E=;
ã Rechteck
el@1, 4D = [email protected], -0.8<, 80.8, 0.8<D;el@2, 4D = 8Disk@80, 0<, 0.2D,
Magenta, [email protected], [email protected], -0.8<D<;el@3, 4D = 8Disk@80, 0<, 0.2D, Yellow,
[email protected], [email protected], -0.6<, 80.9, 0.5<D<;ã Linie
el@1, 5D = [email protected], 0.9<, 8-0.3, 0<, 80.2, 0<, 80.9, -0.9<<D;el@2, 5D = [email protected], Dashed, Red,
[email protected], 0.9<, 8-0.3, 0<, 80.2, 0<, 80.9, -0.9<<D<;el@3, 5D = [email protected], .2<D, Red, Thick,
[email protected], 0.9<, 8-0.3, 0<, 80.2, 0<, 80.9, -0.9<<D<;ã Polygon
el@1, 6D = [email protected], -0.5<, 80, 0.8<, 80.8, -0.5<<D;el@2, 6D = 8EdgeForm@ThickD, Pink,
[email protected], -0.5<, 80, 0.8<, 80.8, -0.5<<D<;el@3, 6D = Table@8EdgeForm@BlackD,
Hue@RandomReal@DD, Polygon@RandomReal@1, 83, 2<DD<, 820<D;ã graphische Tabelle
18 Mathematica_4.nb
ã
graphische Tabelle
Grid@Table@Graphics@el@i, jDD, 8i, 1, 3<, 8j, 1, 6<D, Frame ® AllD
Mathematica_4.nb 19
Grid@Table@Graphics@el@i, jD, ImageSize ® 880, 80<, PlotRange ® 2D,8i, 1, 3<, 8j, 1, 6<D, Frame ® AllD
Text in Graphics
ã Text im Standardformat
txt@1, 1D = Text@"einfacher Text"D;
20 Mathematica_4.nb
txt@2, 1D := 8Blue, Text@RandomReal@1D, 80, 0.3<, CenterD<;ã Formattierter Text mit Style
txt@1, 2D = Text@Style@"Text in Fontgröße 14", 14DD;txt@2, 2D = Text@Style@"Bold, Italic", 18, Bold, ItalicDD;
ã Text und Zahlen
txt@1, 3D = Text@Style@"Π = " <> ToString@N@Π, 3DD, 18, BoldDD;txt@2, 3D = TextBStyleBToStringBTraditionalFormB Π FF <> "=" <>
ToStringB NB Π , 4FF, 18, Bold, Background ® YellowFF;ã reine Text Tabelle
Grid@Table@txt@i, jD, 8i, 1, 2<, 8j, 1, 3<D, Frame ® AllDeinfacherText Text in Fontgröße 14 Π = 3.14
8RGBColor@0, 0, 1D, [email protected], 80, 0.3<, CenterD< Bold, Italic Π =1.772
ã graphische Tabelle
Grid@Table@Graphics@txt@i, jD, ImageSize ® 8150, 70<D,8i, 1, 2<, 8j, 1, 3<D, Frame ® AllD
einfacher Text Text in Fontgröße 14 Π = 3.14
0.435337 Bold, Italic Π =1.772
Mathematica_4.nb 21