Mathematica 入門
解方程式
1
| Solve[x^2 - 3 x + 2 == 0, x]
|
輸出
在整數範圍內求方程式的解
1
| Solve[x^2 - 3 x + 2 == 0 && 0 <= x <= 2, x, Integers]
|
輸出
解聯立方程式
1
| Solve[{x + y == 3, x - y == 1}, {x, y}]
|
輸出
解不等式
1
| Reduce[x^2 - 3 x + 2 > 0, x]
|
輸出
微分
輸出
積分
輸出
求極限
1
| Limit[1/x, x -> 0, Direction -> "FromAbove"]
|
輸出
x 從右側趨近 0 時,極限為 Infinity。雙側實數極限不存在。可用 Direction 指定單側趨近方向。
求級數
1
| Sum[1/n^2, {n, 1, Infinity}]
|
輸出
建立矩陣
求矩陣乘積
輸出
求反矩陣
輸出
輸出
1
| {{(5 + Sqrt[33])/2, (5 - Sqrt[33])/2}, {{2, (3 + Sqrt[33])/2}, {2, (3 - Sqrt[33])/2}}}
|
此矩陣的特徵值為 (5±√33)/2。特徵向量的順序或倍數可能不同,但每組值與向量都滿足 m.v=λv。
求向量內積
輸出
求向量外積
1
| Cross[{1, 2, 3}, {4, 5, 6}]
|
輸出
求向量的大小
輸出
求向量的角度
1
| ArcCos[{1, 2} . {3, 4}/(Norm[{1, 2}] Norm[{3, 4}])]
|
輸出
求向量的投影
1
| Projection[{1, 2}, {3, 4}]
|
輸出
將 {1,2} 正交投影到 {3,4} 時,係數為內積比 11/25,結果是 {33/25,44/25}。
求向量的旋轉
1
| RotationMatrix[π/2].{1, 0}
|
輸出
求向量的平移
1
| TranslationTransform[{1, 2}][{3, 4}]
|
輸出
求向量的縮放
1
| ScalingTransform[{2, 3}][{1, 1}]
|
輸出
求向量的反射
1
| ReflectionTransform[{0, 1}][{1, 1}]
|
輸出
ReflectionTransform 的第一個引數是鏡面的法向量。法向量 {0,1} 代表 x 軸,因此 {1,1} 會映射為 {1,-1}。
下方亂數輸出僅供示例,每次計算都會改變。RandomInteger[] 會回傳 0 或 1。SeedRandom 可在相同產生器與版本下重現亂數序列。
產生亂數
輸出
產生亂數(整數)
輸出
產生亂數(指定範圍)
輸出
產生亂數(整數、指定範圍)
輸出
產生亂數(指定分佈)
1
| RandomVariate[NormalDistribution[0, 1]]
|
輸出
產生亂數(指定分佈、指定數量)
1
| RandomVariate[NormalDistribution[0, 1], 10]
|
輸出
1
| {0.123456, 0.234567, 0.345678, 0.456789, 0.567890, 0.678901, 0.789012, 0.890123, 0.901234, 0.012345}
|
產生亂數(指定分佈、指定數量、指定種子)
1
2
| SeedRandom[12345]
RandomVariate[NormalDistribution[0, 1], 10]
|
輸出
1
| (* Output intentionally omitted: exact sequence depends on the Wolfram Language version. *)
|
將函數套用至陣列元素
1
2
3
| Map[Sqrt, {1, 4, 9}]
Sqrt /@ {1, 4, 9}
Map[#^(1/2)&, {1, 4, 9}]
|
輸出
1
2
| f = Function[x, x^2]
f[3]
|
輸出
合成函數
1
2
3
4
| f = Function[x, x^2]
g = Function[x, x + 1]
h = Function[x, f[g[x]]]
h[3]
|
輸出
參考前一次的計算結果
輸出
純函數
輸出
擷取陣列
1
2
| Select[{1, 2, 3, 4, 5}, EvenQ]
Select[{1, 2, 3, 4, 5}, Mod[#,2]==0&]
|
輸出
函數官方文件
Solve · Reduce · D · Integrate · Limit · Sum · Inverse · Eigensystem · Projection · RotationMatrix · ReflectionTransform · RandomReal · RandomInteger · RandomVariate