Featured image of post Introduction to Mathematica

Introduction to Mathematica

Introduction to Mathematica

Solve an equation

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

Output

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

Solve an equation within integers

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

Output

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

Solve simultaneous equations

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

Output

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

Solve an inequality

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

Output

1
x < 1 || x > 2

Differentiate

1
D[x^2, x]

Output

1
2 x

Integrate

1
Integrate[x^2, x]

Output

1
x^3/3

Find a limit

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

Output

1
Infinity

Find a series

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

Output

1
π^2/6

Create a matrix

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

Find the product of matrices

1
m . m

Output

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

Find the inverse matrix

1
Inverse[m]

Output

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

Find eigenvalues and eigenvectors

1
Eigensystem[m]

Output

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

Find the inner product of vectors

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

Output

1
11

Find the cross product of vectors

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

Output

1
{-3, 6, -3}

Find the magnitude of a vector

1
Norm[{1, 2, 3}]

Output

1
√14

Find the angle between vectors

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

Output

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

Find the projection of a vector

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

Output

1
{11/5, 22/5}

Find the rotation of a vector

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

Output

1
{0, 1}

Find the translation of a vector

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

Output

1
{4, 6}

Find the scaling of a vector

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

Output

1
{2, 3}

Find the reflection of a vector

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

Output

1
{0, 0}

Generate random numbers

1
RandomReal[]

Output

1
0.123456

Generate random numbers (integers)

1
RandomInteger[]

Output

1
123456

Generate random numbers (range specified)

1
RandomReal[{1, 10}]

Output

1
5.6789

Generate random numbers (integers, range specified)

1
RandomInteger[{1, 10}]

Output

1
5

Generate random numbers (distribution specified)

1
RandomVariate[NormalDistribution[0, 1]]

Output

1
0.123456

Generate random numbers (distribution specified, number specified)

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

Output

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

Generate random numbers (distribution specified, number specified, seed specified)

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

Output

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

Apply a function to array elements

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

Output

1
{1, 2, 3}

Define a function using lambda expressions

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

Output

1
9

Compose functions

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

Output

1
16

Reference the previous calculation result

1
% + 1

Output

1
17

Pure functions

1
(#+3)&[5]

Output

1
8

Extract from an array

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

Output

1
{2, 4}
comments powered by Disqus