Featured image of post Mathematica 入門

Mathematica 入門

Mathematica 入門

解方程式

1
Solve[x^2 - 3 x + 2 == 0, x]

輸出

1
{{x -> 1}, {x -> 2}}

在整數範圍內求方程式的解

1
Solve[x^2 - 3 x + 2 == 0 && 0 <= x <= 2, x, Integers]

輸出

1
{{x -> 1}, {x -> 2}}

解聯立方程式

1
Solve[{x + y == 3, x - y == 1}, {x, y}]

輸出

1
{{x -> 2, y -> 1}}

解不等式

1
Reduce[x^2 - 3 x + 2 > 0, x]

輸出

1
x < 1 || x > 2

微分

1
D[x^2, x]

輸出

1
2 x

積分

1
Integrate[x^2, x]

輸出

1
x^3/3

求極限

1
Limit[1/x, x -> 0]

輸出

1
Infinity

求級數

1
Sum[1/n^2, {n, 1, Infinity}]

輸出

1
π^2/6

建立矩陣

1
m = {{1, 2}, {3, 4}}

求矩陣乘積

1
m . m

輸出

1
{{7, 10}, {15, 22}}

求反矩陣

1
Inverse[m]

輸出

1
{{-2, 1}, {1.5, -0.5}}

求特徵值與特徵向量

1
Eigensystem[m]

輸出

1
{{5, 0}, {{1, 1}, {1, -1}}}

求向量內積

1
{1, 2} . {3, 4}

輸出

1
11

求向量外積

1
Cross[{1, 2, 3}, {4, 5, 6}]

輸出

1
{-3, 6, -3}

求向量的大小

1
Norm[{1, 2, 3}]

輸出

1
√14

求向量的角度

1
ArcCos[{1, 2} . {3, 4}/(Norm[{1, 2}] Norm[{3, 4}])]

輸出

1
ArcCos[11/(√5 √25)]

求向量的投影

1
{1, 2} . {3, 4}/Norm[{3, 4}] {3, 4}/Norm[{3, 4}]

輸出

1
{11/5, 22/5}

求向量的旋轉

1
RotationMatrix[π/2].{1, 0}

輸出

1
{0, 1}

求向量的平移

1
TranslationTransform[{1, 2}][{3, 4}]

輸出

1
{4, 6}

求向量的縮放

1
ScalingTransform[{2, 3}][{1, 1}]

輸出

1
{2, 3}

求向量的反射

1
ReflectionTransform[{1, 1}][{1, 1}]

輸出

1
{0, 0}

產生亂數

1
RandomReal[]

輸出

1
0.123456

產生亂數(整數)

1
RandomInteger[]

輸出

1
123456

產生亂數(指定範圍)

1
RandomReal[{1, 10}]

輸出

1
5.6789

產生亂數(整數、指定範圍)

1
RandomInteger[{1, 10}]

輸出

1
5

產生亂數(指定分佈)

1
RandomVariate[NormalDistribution[0, 1]]

輸出

1
0.123456

產生亂數(指定分佈、指定數量)

1
RandomVariate[NormalDistribution[0, 1], 10]

輸出

1
{0.123456, 0.234567, ..., 0.987654}

產生亂數(指定分佈、指定數量、指定種子)

1
2
SeedRandom[12345]
RandomVariate[NormalDistribution[0, 1], 10]

輸出

1
{0.123456, 0.234567, ..., 0.987654}

將函數套用至陣列元素

1
2
3
Map[Sqrt, {1, 4, 9}]
Sqrt /@ {1, 4, 9}
Map[#^(1/2)&, {1, 4, 9}]

輸出

1
{1, 2, 3}

使用 Lambda 運算式定義函數

1
2
f = Function[x, x^2]
f[3]

輸出

1
9

合成函數

1
2
3
4
f = Function[x, x^2]
g = Function[x, x + 1]
h = Function[x, f[g[x]]]
h[3]

輸出

1
16

參考前一次的計算結果

1
% + 1

輸出

1
17

純函數

1
(#+3)&[5]

輸出

1
8

擷取陣列

1
2
Select[{1, 2, 3, 4, 5}, EvenQ]
Select[{1, 2, 3, 4, 5}, Mod[#,2]==0&]

輸出

1
{2, 4}
comments powered by Disqus
使用 Hugo 建立
主題 StackJimmy 設計