Featured image of post Mathematica Introduction: How to Use Basic Commands for Equations, Calculus, etc.

Mathematica Introduction: How to Use Basic Commands for Equations, Calculus, etc.

Explains the basic usage and calculation commands of Mathematica for beginners. Summarizes specific examples of frequently used functions such as solving equations and simultaneous equations (Solve), inequalities (Reduce), differentiation (D), integration (Integrate), and matrix calculations.

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, Direction -> "FromAbove"]

Output

1
Infinity

The limit as x approaches 0 from the right is Infinity. The two-sided real limit does not exist. Use Direction to choose a one-sided approach.

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}, {3/2, -1/2}}

Find eigenvalues and eigenvectors

1
Eigensystem[m]

Output

1
{{(5 + Sqrt[33])/2, (5 - Sqrt[33])/2}, {{2, (3 + Sqrt[33])/2}, {2, (3 - Sqrt[33])/2}}}

The eigenvalues of this matrix are (5 ± √33)/2. Eigenvectors may be returned in a different order or scaled; each value-vector pair satisfies m.v = λv.

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
Projection[{1, 2}, {3, 4}]

Output

1
{33/25, 44/25}

Projecting {1,2} onto {3,4} uses the dot-product ratio 11/25, giving {33/25,44/25}.

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[{0, 1}][{1, 1}]

Output

1
{1, -1}

ReflectionTransform takes the mirror’s normal as its first argument. The normal {0,1} defines the x-axis, which maps {1,1} to {1,-1}.

The random outputs below are illustrative and change on each evaluation. RandomInteger[] returns 0 or 1. SeedRandom can reproduce a sequence with the same generator and version.

Generate random numbers

1
RandomReal[]

Output

1
0.123456

Generate random numbers (integers)

1
RandomInteger[]

Output

1
0

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.345678, 0.456789, 0.567890, 0.678901, 0.789012, 0.890123, 0.901234, 0.012345}

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

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

Output

1
(* Output intentionally omitted: exact sequence depends on the Wolfram Language version. *)

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}

Official function documentation

Solve · Reduce · D · Integrate · Limit · Sum · Inverse · Eigensystem · Projection · RotationMatrix · ReflectionTransform · RandomReal · RandomInteger · RandomVariate

comments powered by Disqus