Post on 04-Feb-2021
Mathematica
EinleitungWas ist Mathematica:
(i) Nummerisches und symbolisches Rechenprogramm
(ii) Datenvisualisierungsprogramm
(iii) höhere Programiersprache, etc.
Struktur:
(1) Kernel
(2) Front end (notebook)
Hilfe:
Help-window (Tour)
News group und web sites
http://www.wolfram.com
http://www.mathsource.com
comp.soft-sys.math.mathematica
http://www.mathematica.ch
Bücher (Wolframs Buch ist über das Help Menü zugänglich)
http://store.wolfram.com/catalog/books/ zum Beispiel:
An Introduction to Programming with Mathematica
by Richard J. Gaylord, Samuel N. Kamin, and Paul R. Wellin
Programming in Mathematica by Roman Maeder
Introduction to Scientific Programming: Computational Problem Solving Using Mathematica and C
by Joseph L. Zachary
Starten des Kernel: (shift-return oder Enter)
In[1]:= 1 + 1
Out[1]= 2
Einige einfache Befehle
Einige einfache Befehle= Definition Beispiel: In[1]:= 3
In[2]:= f = 3
:= verzögerte Definition
== ist gleich
=!= ist ungleich
(* ... *) zwischen diesen Klammern kann man man Komentare setzen
+ Addition
- Subtraktion
* Multiplikation (kann weggelassen werden) Beispiel: 2 x = x + x
Achtung: x2 ist der Name der Variablen x2, und nicht 2 x !!!
2x ist 2*x
/ Division Beispiel: a/b
^ Exponentiation Beispiel: x^2 = x*x
% vorheriger output
%% vorvorheriger output
%4 output von Out[4]
; Unterdrückung von output
I = Sqrt[-1] = imaginäre Einheit
Pi = 3.14159...
( ) runde Klammern um Terme anzuordnen
Beispiel: (a + b)(a - b)
[ ] eckige Klammern für Argumente in Funktionen
Beispiel: f[x], Sin[x], h[x,y]
{ } geschwungene Klammern für Listen
Beispiel: {a,b,c}, {x,0,2Pi}
Clear["@"]
In[2]:= 2 x
Out[2]= 2 x
mmaintro.nb 2
In[3]:= % ê. x Ø 2
Out[3]= 4
Mathematica als Taschenrechner
Sqrt@xD square root H x LExp@xD exponential H ex LLog@xD natural logarithm H loge x L
Log@b, xD logarithm to base b H logb x LSin@xD, Cos@xD, Tan@xD trigonometric functions Hwith arguments in radiansL
ArcSin@xD, ArcCos@xD, ArcTan@xD inverse trigonometric functionsn! factorial Hproduct of integers 1, 2, …, n L
Abs@xD absolute valueRound@xD closest integer to xRandom@ D pseudorandom number between 0 and 1
In[4]:= 1/2 + 1/3
Out[4]=5
6
In[5]:= %
Out[5]=5
6
In[6]:= f = %
Out[6]=5
6
mmaintro.nb 3
In[7]:= f
Out[7]=5
6
In[8]:= %4
Out[8]=5
6
In[9]:= 2^1000
Out[9]= 10 715 086 071 862 673 209 484 250 490 600 018 105 614 048 117 055 336 074 437 503 Ö883 703 510 511 249 361 224 931 983 788 156 958 581 275 946 729 175 531 468 251 Ö871 452 856 923 140 435 984 577 574 698 574 803 934 567 774 824 230 985 421 074 Ö605 062 371 141 877 954 182 153 046 474 983 581 941 267 398 767 559 165 543 946 Ö077 062 914 571 196 477 686 542 167 660 429 831 652 624 386 837 205 668 069 376
In[10]:=
H2 ê 5L^5 + 25
5
Out[10]=
64
3125
In[11]:=
0.2^5
Out[11]=
0.00032
In[12]:=
N[%9]
Out[12]=
1.07151 µ 10301
mmaintro.nb 4
In[13]:=
N[%9, 20]
Out[13]=
1.0715086071862673209 µ 10301
In[14]:=
%9 //N
Out[14]=
1.07151 µ 10301
In[15]:=
%
Out[15]=
1.07151 µ 10301
In[16]:=
%7 + %8
Out[16]=
5
3
In[17]:=
f = x
Out[17]=
x
In[18]:=
f^2 + 2f
Out[18]=
2 x + x2
mmaintro.nb 5
In[19]:=
f = Pi
Out[19]=
p
In[20]:=
1.1 f
Out[20]=
3.45575
In[21]:=
N[Pi]
Out[21]=
3.14159
In[22]:=
Cos[Pi/6]
Out[22]=
3
2
In[23]:=
Cos[Pi/8]
Out[23]=
CosB p8F
In[24]:=
N[%, 25]
Out[24]=
0.9238795325112867561281832
mmaintro.nb 6
In[25]:=
Cos[%]
Out[25]=
0.6027290430100575892342310
In[26]:=
Exp[I Pi/4]
Out[26]=
‰Â p
4
In[27]:=
N[%]
Out[27]=
0.707107 + 0.707107 Â
In[28]:=
(1 + I)/Sqrt[2]
Out[28]=
1 + Â
2
In[29]:=
%^4
Out[29]=
-1
In[30]:=
N[%%]
Out[30]=
0.707107 + 0.707107 Â
mmaintro.nb 7
In[31]:=
2 p + Pi
Out[31]=
3 p
In[32]:=
N@%, 100DOut[32]=
9.4247779607693797153879301498385086525915081981253174629248337769234Ö49218858626995884104476026351204
In[33]:=
f = %;
In[34]:=
f
Out[34]=
9.4247779607693797153879301498385086525915081981253174629248337769234Ö49218858626995884104476026351204
In[35]:=
‰2
Out[35]=
‰2
In[36]:=
InputForm@%DOut[36]//InputForm=
E^2
mmaintro.nb 8
Einige einfache symbolische Rechnungen
Expand@exprD multiply out products and powers, writing the result as a sum of termsFactor@exprD write expr as a product of minimal factors
Simplify@exprD try to find the simplest form of expr by applying various standard algebraic transformationsFullSimplify@exprD try to find the simplest form by applying a wide range of transformations
ExpandAll@exprD apply Expand everywhereTogether@exprD put all terms over a common denominator
Apart@exprD separate into terms with simple denominatorsCancel@exprD cancel common factors between numerators and denominators
In[37]:=
f = (a + b) (a - b)
Out[37]=
Ha - bL Ha + bLIn[38]:=
g = Expand[f]
Out[38]=
a2 - b2
In[39]:=
Factor[g]
Out[39]=
Ha - bL Ha + bLIn[40]:=
g/(a - b)
Out[40]=
a2 - b2
a - b
mmaintro.nb 9
In[41]:=
Factor[%]
Out[41]=
a + b
In[42]:=
g = (a^3 - b^3)/(a - b)
Out[42]=
a3 - b3
a - b
In[43]:=
Simplify[g]
Out[43]=
a2 + a b + b2
In[44]:=
Factor[g]
Out[44]=
a2 + a b + b2
In[45]:=
g = (a^7 - b^7)/(a - b)
Out[45]=
a7 - b7
a - b
In[46]:=
Simplify[g]
Out[46]=
a7 - b7
a - b
mmaintro.nb 10
In[47]:=
Factor[g]
Out[47]=
a6 + a5 b + a4 b2 + a3 b3 + a2 b4 + a b5 + b6
Vor ca. 100 Jahren wurde durch folgendes Beispiel widerlegt, dass bei der Zerlegung von (a^n-b^n) nur
Einsen als Koeffizienten in den Faktoren auftreten könnenIn[48]:=
FactorAa105 - b105EOut[48]=
Ha - bL Ia2 + a b + b2M Ia4 + a3 b + a2 b2 + a b3 + b4MIa6 + a5 b + a4 b2 + a3 b3 + a2 b4 + a b5 + b6M Ia8 - a7 b + a5 b3 - a4 b4 + a3 b5 - a b7 + b8MIa12 - a11 b + a9 b3 - a8 b4 + a6 b6 - a4 b8 + a3 b9 - a b11 + b12MIa24 - a23 b + a19 b5 - a18 b6 + a17 b7 - a16 b8 + a14 b10 - a13 b11 +
a12 b12 - a11 b13 + a10 b14 - a8 b16 + a7 b17 - a6 b18 + a5 b19 - a b23 + b24MIa48 + a47 b + a46 b2 - a43 b5 - a42 b6 - 2 a41 b7 - a40 b8 - a39 b9 + a36 b12 +
a35 b13 + a34 b14 + a33 b15 + a32 b16 + a31 b17 - a28 b20 - a26 b22 -a24 b24 - a22 b26 - a20 b28 + a17 b31 + a16 b32 + a15 b33 + a14 b34 + a13 b35 +a12 b36 - a9 b39 - a8 b40 - 2 a7 b41 - a6 b42 - a5 b43 + a2 b46 + a b47 + b48M
In[49]:=
h = g /. b -> 3
Out[49]=
-2187 + a7
-3 + a
In[50]:=
g
Out[50]=
a7 - b7
a - b
mmaintro.nb 11
In[51]:=
x = 3
Out[51]=
3
In[52]:=
f = (x + a)^2
Out[52]=
H3 + aL2In[53]:=
Clear[x]; f = (x + a)^2
Out[53]=
Ha + xL2
Out[54]=
a2 + 2 a x + x2
Übung:(1) Man dividiere x^9 - a^9 durch x - a; und stelle das Ergebnis als Polynom dar.
(2) Man zerlege f = 24 - 14 x - 13 x^2 + 2 x^3 + x^4 in Faktoren und bringe das Resultat anschliessend
auf die ursprüngliche Form
(3) Man berechne den Binomialkoeffizienten direkt K 12n
Ooder mittels der Entwicklung von (x + y)^12.
Graphik Einfache Plots
Plot@ f, 8x, xmin, xmax< D plot f as a function of x from xmin to xmaxPlot@ 9 f 1 , f 2 ,… = , 8x, xmin, xmax< D plot several functions together
mmaintro.nb 12
In[55]:=
Plot[x^3 - x + 1, {x,-2,2}]
Out[55]=
-2 -1 1 2
-2
-1
1
2
3
mmaintro.nb 13
In[56]:=
ParametricPlot[ {Cos[t]^2,Sin[t]^3}, {t,0,2Pi}]
Out[56]=
0.2 0.4 0.6 0.8 1.0
-1.0
-0.5
0.5
1.0
Plot3D@ f, 8x, xmin, xmax< , 8y, ymin, ymax< D make a three-dimensional plot of f
mmaintro.nb 14
In[57]:=
Plot3D[ Sin[x y], {x,-2,2}, {y,-2,2}]
Out[57]=
mmaintro.nb 15
In[58]:=
ParametricPlot3D@82 Cos@tD Cos@pD, 3 Cos@tD Sin@pD, 4 Sin@tD
In[59]:=
Show@%, ViewPoint Ø 81, .5, -3
In[62]:=
Simplify@w3DOut[62]=
-I576 ‰8 t+2 x I‰560 t + 12 960 000 ‰20 x + 6 480 000 ‰16 H4 t+xL + 38 880 000 ‰16 H13 t+xL +907 200 ‰10 H28 t+xL + 15 000 ‰8 H36 t+xL + 97 200 ‰8 H42 t+xL + 90 000 ‰8 H54 t+xL +75 ‰4 H88 t+xL + 50 ‰504 t+2 x + 1800 ‰496 t+4 x + 4800 ‰344 t+6 x + 28 800 ‰488 t+6 x +90 000 ‰128 t+12 x + 2 430 000 ‰224 t+12 x + 2 160 000 ‰272 t+12 x +720 000 ‰72 t+14 x + 17 280 000 ‰216 t+14 x + 25 920 000 ‰56 t+18 xMM ë
I‰288 t + 43 200 ‰12 x + 10 800 ‰8 H8 t+xL + 7200 ‰6 H36 t+xL + 900 ‰4 H56 t+xL +72 ‰280 t+2 x + 600 ‰72 t+6 x + 21 600 ‰8 t+10 xM2
In[63]:=
w@x_, t_D :=-I576 ‰8 t+2 x I‰560 t + 12 960 000 ‰20 x + 6 480 000 ‰16 H4 t+xL + 38 880 000 ‰16 H13 t+xL +
907 200 ‰10 H28 t+xL + 15 000 ‰8 H36 t+xL + 97 200 ‰8 H42 t+xL + 90 000 ‰8 H54 t+xL +75 ‰4 H88 t+xL + 50 ‰504 t+2 x + 1800 ‰496 t+4 x + 4800 ‰344 t+6 x + 28 800 ‰488 t+6 x +90 000 ‰128 t+12 x + 2 430 000 ‰224 t+12 x + 2 160 000 ‰272 t+12 x +720 000 ‰72 t+14 x + 17 280 000 ‰216 t+14 x + 25 920 000 ‰56 t+18 xMM ë
I‰288 t + 43 200 ‰12 x + 10 800 ‰8 H8 t+xL + 7200 ‰6 H36 t+xL + 900 ‰4 H56 t+xL +72 ‰280 t+2 x + 600 ‰72 t+6 x + 21 600 ‰8 t+10 xM2
In[64]:=
kdv@v_D := D@v, tD - 6 v D@v, xD + D@v, 8x, 3
In[67]:=
Manipulate@Plot@-w@x, tD, 8x, -10, 10
In[68]:=
D[ Sin[a x], x]
Out[68]=
a Cos@a xDIn[69]:=
D[ Sin[ax], x]
Out[69]=
0
In[70]:=
D[ Sin[a x], {x,3}]
Out[70]=
-a3 Cos@a xDIn[71]:=
D[ Sin[a x] Cosh[b y], {x,3}, {y,5}]
Out[71]=
-a3 b5 Cos@a xD Sinh@b yD(1) Man leite f = x^2 - 5x + 1 nach x ab .
(2) Man leite die Funktionen {E^(2a x^2) Cos[c x], Tan[E^(3 x)], Sin[a x] Cos[b y]} nach x ab .
(3) Man leite (x/2)^2 (y/3)^4 nach x und y ab.
IntegrationUnbestimmtes Integral:
Integrate@ f, xD the indefinite integral Ÿ f d xIntegrate@ f, x, yD the multiple integral Ÿ d x d y f
Integrate@ f, 8x, xmin, xmax< D the definite integral Ÿxminxmax f d xIntegrate@ f, 8x, xmin, xmax< , 8y, ymin, ymax< D the multiple integral Ÿxminxmaxd x Ÿyminymaxd y f
mmaintro.nb 20
In[72]:=
Integrate[ Exp[a x], x]
Out[72]=
‰a x
a
In[73]:=
Integrate[ Exp[-a x] Cos[b y], x, y]
Out[73]=
-‰-a x Sin@b yD
a b
In[74]:=
‡ 1x4 - 1
„x
Out[74]=
-ArcTan@xD
2+1
4Log@-1 + xD - 1
4Log@1 + xD
Bestimmtes Integral:In[75]:=
Integrate[ Sin[a x], {x,0,Pi/2}]
Out[75]=
2 SinB a p4F2
a
In[76]:=
Together[%]
Out[76]=
2 SinB a p4F2
a
mmaintro.nb 21
In[77]:=
Simplify[%]
Out[77]=
2 SinB a p4F2
a
In[78]:=
NIntegrate[Sin[3x], {x,0,0.45}]
Out[78]=
0.260331
Übung:Man berechne folgendes Integral auf mehrere Arten und vergleiche die Resultate
Û01 x20 ex dx
a) Numerische Integration.
b) Analytische Integration, und dann Einsetzen der Randwerte mit einer Genauigkeit von 20 Stellen.
c) Reihenentwicklung (10 Terme) und gliedweiser Integration.
Listen und Matrizen
Vektoren werden in Mathematica als Listen betrachtet
In[79]:=
u = 81, 2
In[80]:=
3 u + 2 v
Out[80]=
89, 14<
c u multiply by a scalaru . v dot product
Cross@u, vD vector cross product Halso input as u ä vLAnalog werden Matrizen dargestellt
In[81]:=
M = 881, 2
Um einzelne Elemente aus einer Matrix zu bekommen benutzt man den Part-operation (wie bei allen Listen)
In[84]:=
M@@1DDOut[84]=
81, 2<In[85]:=
MP1TOut[85]=
81, 2<In[86]:=
M@@1DD@@2DDOut[86]=
2
Matrizen können mt Hilfe folgender Operationen erzeugt werden
Table@ f, 8i, m< , 8 j, n< D build an män matrix by evaluating f with i ranging from 1 to m and j ranging from 1 to nArray@a, 8m, n< D build an män matrix with i, j th element a@i, jD
IdentityMatrix@nD generate an nän identity matrixDiagonalMatrix@listD generate a square matrix with the elements in list on the diagonal
Zum Beispiel
In[87]:=
Table@i + j, 8i, 3
In[88]:=
Array@a, 83
In[91]:=
Solve[ sys, {x,y}]
Out[91]=
::x Ø 1119
, y Ø2
19>>
In[92]:=
sol = % // Flatten
Out[92]=
:x Ø 1119
, y Ø2
19>
In[93]:=
sys /. sol
Out[93]=
8True, True<In[94]:=
Solve[ {5 x + 1 y == 3., 4 x - 3 y == 2}, {x,y}]
Out[94]=
88x Ø 0.578947, y Ø 0.105263
In[95]:=
ma = {{5,1}, {4, -3}} ; MatrixForm[ma]b = {3,2}mx = {x,y}ma . mx == b
Out[95]//MatrixForm=
K 5 14 -3
OOut[96]=
83, 2<Out[97]=
8x, y<Out[98]=
85 x + y, 4 x - 3 y< ã 83, 2<In[99]:=
ma . mx == b //Thread
Out[99]=
85 x + y ã 3, 4 x - 3 y ã 2<In[100]:=
Inverse[ma] . b
Out[100]=
: 1119
,2
19>
Singuläre SystemeIn[101]:=
Solve[ {x - y == 1, 3 x - 3 y == 3}, {x,y} ]
Solve::svars : Equations may not give solutions for all "solve" variables. àOut[101]=
88x Ø 1 + y
In[102]:=
Solve[ {x - y == 1, 3 x - 3 y == 2}, {x,y} ]
Out[102]=
8<In[103]:=
Solve@Abs@x ê 2 - 1D ã x, xDOut[103]=
::x Ø 23>>
Quadratische Gleichung
In[104]:=
eqn = x2 + 2 a x + b ã 0
Out[104]=
93 + 2 a x + x2, 2 + 2 a x + x2= ã 0
Man beachte dass der Set operator (=) schwächer bindet als der Equal operator (==).
In[105]:=
sol = Solve@eqn, xDOut[105]=
8<Das Ergebnis ist als Liste von Regeln gegeben. Durch Einsetzen kann das Ergebnis verifiziert werden.
In[106]:=
Simplify@eqn ê. solDOut[106]=
93 + 2 a x + x2, 2 + 2 a x + x2= ã 0
Übung(1) Man löse das folgende lineare Gleichungssytem:
x + y + z = 1, x + 2y + 3z = 4, x + 3y + 6z = 10.
(2) Man löse das folgende lineare Gleichungssytem auf zwei Arten:
a x + b y = e, c x + d y = f.
mmaintro.nb 28
(2) Man löse das folgende lineare Gleichungssytem auf zwei Arten:
a x + b y = e, c x + d y = f.
FunktionenUnterstrichene ("_") Variable sind Platzhalter. Die anderen Variable sind global definiert.
In[107]:=
Clear@f, x, y, cD;f@x_, y_D = x^2 + y^ 2 - c
Out[108]=
-c + x2 + y2
In dieser Funktion sind x und y Platzhalter, c ist global. Es ist wichtig, dass noch keine Zuweisungen zu den Variablen der Funktion gemacht wurden.
In[109]:=
f@a, bDOut[109]=
99 + a2 - c, 4 + a2 - c=In[110]:=
f[2,5]
Out[110]=
29 - c
In[111]:=
c = 13.3
Out[111]=
13.3
In[112]:=
f[2,5]
Out[112]=
15.7
mmaintro.nb 29
In[113]:=
f[a,b]
Out[113]=
9-4.3 + a2, -9.3 + a2=In[114]:=
Clear[c]f[a,b]
Out[115]=
99 + a2 - c, 4 + a2 - c=In[116]:=
f[x_] = x^3 + x - 1
Out[116]=
-1 + x + x3
Eine Funktion kann auch rekursiv definiert werden. EIn Beispiel dafür sind die Legendre Polynome
Pn(x) die folgende Rekursion erfüllen
(n + 1) Pn+1(x) - (2 n + 1) x Pn(x) + n Pn-1(x) = 0
und wie folgt erzeugt werden können.In[117]:=
p[0,x] = 1; p[1,x] = x;p[n_Integer,x]:=p[n,x]=((2n-1)/n x) p[n-1,x] + p[n-2,x] (n-1)/n;
In[119]:=
p[5,x]//Apart
Out[119]=
15 x
8+35 x3
4+63 x5
8
mmaintro.nb 30
In[120]:=
p[.5,x]
Out[120]=
p@0.5, xDRekursionen können auf zweierlei Art definiert werden. Das zweite Beispiel unten ist ein Beispiel für dynamische Programmierung und ist auch wesentlich schneller. Die folgende Rekursion erzeugt die Fibonacci-Zahlen
In[121]:=
Clear@fibD; fib@0D = fib@1D = 1;fib@n_D := fib@n - 1D + fib@n - 2D;nt = Table@8k, fib@kD< êê Timing, 8k, 2, 25
In[125]:=
ListPlot@tp, PlotRange Ø All, AxesLabel Ø 8"n", "time @sD"
In[129]:=
dm = Table@8k, Fibonacci@kD
In[137]:=
f@2.2Df expects a positive integer argument
SubstitutionenSubstitutionen werden nach folgenden Regeln durchgeführt
exp /. var -> value
exp /. list
list = {var1 -> val1, var2 -> val2, ... }
Beispiele
In[138]:=
h = z
Out[138]=
z
In[139]:=
h1 = h /. z -> 44
Out[139]=
44
In[140]:=
h
Out[140]=
z
In[141]:=
h1
Out[141]=
44
mmaintro.nb 34
In[142]:=
Clear[x,y,f]f[x_,y_] = x^2 + y^ 2 - c
Out[143]=
-c + x2 + y2
In[144]:=
h = f[x,y]
Out[144]=
-c + x2 + y2
In[145]:=
h1 = h /. c -> 13.3
Out[145]=
-13.3 + x2 + y2
In[146]:=
h
Out[146]=
-c + x2 + y2
In[147]:=
h1
Out[147]=
-13.3 + x2 + y2
In[148]:=
h2 = h1 /. {x -> a, y -> b}
Out[148]=
9-4.3 + a2, -9.3 + a2=
mmaintro.nb 35
In[149]:=
x = 4y = 5
Out[149]=
4
Out[150]=
5
In[151]:=
h
Out[151]=
41 - c
In[152]:=
h1
Out[152]=
27.7
In[153]:=
h2
Out[153]=
9-4.3 + a2, -9.3 + a2=In[154]:=
f[p,q]
Out[154]=
-c + p2 + q2
mmaintro.nb 36
In[155]:=
f[x,y]
Out[155]=
41 - c
Substitutionen mit BedingungenIn[156]:=
Clear@m, n, kDIn[157]:=
GammaBm + 52F ê. Gamma@n_ + k_ ê; OddQ@2 kDD Ø H2 k + 2 n - 2L!! p
2k-
12+n
Out[157]=
2-2-m p H3 + 2 mL!!In[158]:=
% ê. m -> 4Out[158]=
10 395 p
64
In[159]:=
%% ê. m -> 4.3Out[159]=
575.696
Differentialgleichungen
' (Apostroph) bezeichnet die Ableitung (oder D[y[x],x])
DSolve@eqns, y@xD, xD solve a differential equation for y@xD, taking x as the independent variable
mmaintro.nb 37
In[160]:=
Clear@x, yDIn[161]:=
DSolve[ y''[x] + k y[x] == 0, y[x], x]
Out[161]=
::y@xD Ø C@1D CosB k xF + C@2D SinB k xF>>
In[162]:=
DSolve[ y''[x] + 2 y'[x]/x - l (l + 1)/x^2 y[x] + k^2 y[x] == 0, y[x], x]
Out[162]=
88y@xD Ø C@1D SphericalBesselJ@l, k xD + C@2D SphericalBesselY@l, k xD
SoundIn[165]:=
Play@Sin@1000 ê tD, 8t, 0, 2
In[168]:=
aussage2@p_, m_, n_D := Hp == Ÿ mÏ m == Ÿ nÏ n == HŸ mÏ Ÿ pL LIn[169]:=
wahrheitstafel@aussage2@p, m, nD, 8p, m, n