Getting Started with Wolfram Mathematica

Getting Started with Wolfram Mathematica

Wolfram Mathematica is a software used for technical computing which includes, "neural networks, machine learning, image processing, geometry, data science, visualizations," etc, and it has "over 6,000 built-in functions."

It uses the Wolfram notebook interface which provides a rich format consisting of codes, texts, comments, diagrams (2D, 3D diagrams, etc.), etc. It also has a well-structured documentation with lots of examples to get a beginner started. It allows interfacing with other languages such as Visual Basic, Python, Haskell, Java, etc.

In this article, we would be discussing basic maths functions and how to go about writing them in the Wolfram Mathematica Notebook.

You can create a free account on Wolfram Cloud to follow this tutorial. After signing in, click on New Notebook.

Wolfram Cloud Page Image

We would start with the simplest maths functions.

BODMAS

BODMAS is the acronym for Brackets, Orders, Divison, Multiplication, Addition, and Subtraction. It gives the order of precedence of the operators (from the left) in an arithmetic function.

Enter the operands with their operators (*, /, +, -) for the arithmetic operation you want to perform. To evaluate the operation, press Enter + Shift on your keyboard; if you are making use of the mobile application, click on the Wolfram symbol on the keyboard to evaluate the cell.

Basic Arithmetic in Wolfram Mathematica.

Formatting and Comments

To create a comment, we make use of the open and closing parentheses "()" and two asterisks "**".

Comment in Wolfram Mathematica

To change the format, select;
Format >> Style Name >> Select the format you want

You can also use the following shortcuts;

Shortcut KeysFormats
Alt + 1Title
Alt + 4Section
Alt + 5Subsection
Alt + 6Subsubsection
Alt + 7Text
Alt + 8Code
Alt + 9Input

The input format is the Wolfram language input, while the code format is similar to the inline code format but in block form.

Special Symbols

To access the special symbols/characters in Mathematica, under the Insert tab, click on Special Characters.

Insert drop down in Wolfram Mathematica.

After clicking on it, you would see the special character at the side of your screen.

Special Characters in Wolfram Mathematica

You can also insert special characters by typing their text representation.

Mathematica Code of Some Mathematical Symbols

Symbol NameMathematica Code
Theta\[Theta]
Pi\[Pi]
Infinity\[Infinity]
Square root\[Sqrt]
Integral\[Integral]
Summation symbol\[Sum]
Alpha\[Alpha]
Beta\[Beta]
Gamma\[Gamma]
Delta\[Delta]

Differentiation

To carry out differentiation, we make use of the capital "D". Considering the function below, we would perform the differentiation as shown in the diagram below.

$$f(x) = \sin^2 x \cos^3 y \tan^4 z$$

Differentiation in Wolfram Mathematica

First, we write the function; next, we use the differentiation function (D) to differentiate it. In D[f[x],x] , we first write the function, and after the comma, we indicate what variable we are differentiating it with respect to (that is, the independent variable).

Note: Functions in Wolfram Mathematica start with capital letters, followed by an open square bracket, "[", and it ends with a closing square bracket, "]".

A semi-colon, ";", at the end of a line of code prevents the display of its output.

Higher Order Differentiation

To differentiate the function above with respect to y to the third order, we would make use of the code in the image below.

$$\frac {d^3f(x)}{dy^3}$$

Higher Order Differentiation in Wolfram Mathematica.

In the code, we made use of a list containing the variable we are differentiating the function with respect to and the order. In this case, we are differentiated to the third order, which is why we used "3".

We could also do the below, but it is longer.

Higher Order Differentiation in Wolfram Mathematica.

If you look closely, you would notice that both answers are the same. In this case, we embedded the lower order differentiation into the next order of differentiation with respect to y in each case.

Higher Order Differentiation with Respect to Several Variables

Still using our function above, we would differentiate the function with respect to x, y, and z.

$$\frac {d^3f(x)} {dx \hspace{1mm} dy \hspace{1mm} dz}$$

Higher Order Differentiation with Several Variables in Wolfram Mathematica.

The above differentiation was done in three different ways in the image; in the first and second, we started with z to x, which is the normal way according to the Wolfram Mathematica documentation. In the second, you would notice that the order of differentiation is placed in a set with the variable it is differentiated with respect to. In the last, we started with x to z, and it still gave us the same answer. Sometimes, when we change the order, we would still have the same answer but in a different format. We could also use the long format.

Higher Order Differentiation with Several Variables in Wolfram Mathematica.

Integration

In Mathematica, we make use of the Integration function to integrate. Integration in Mathematica follows the same pattern as differentiation (discussed in the previous section of this article).

$$f(x) = \sin^2 x \cos^3 y \tan^4 z$$

$$\int f(x) = \int \sin^2 x \cos^3 y \tan^4 z$$

Using the same function used in the differentiation section; we integrate the function as shown in the diagram below.

Integration in Wolfram Mathematica.

Limits of Integration

If the above function is to be integrated with limits, we put the variable we are integrating with respect to in a set with the lower and upper limits, e.g.; {x, 0, 3} would mean we are integrating with respect to x with its lower limit to be zero (0) and upper limit to be three (3).

$$\int^3_0 f(x) dx \hspace{1cm} \int^\theta_0 f(x) dy \hspace{1cm} \int^1_0 f(x) dz$$

Integration with limits in Wolfram Mathematica.

Multiple Integrals

If we have;

$$f(x) = y \sin{x}\cos{y}$$

To integrate with respect to y first and x later, we would use the code in the image below.

$$\int dx \int y \sin{x} \cos{y} \hspace{1mm} dy$$

Multiple Integrals in Wolfram Mathematica.

The Gradient of Several Variables

The gradient of several variables is the partial differentiation of a particular function in diverse orthonormal positions of the vector.

$$Grad \hspace{1mm} f(x) = \frac {\partial f(x)}{dx}i + \frac {\partial f(x)}{dy}j + \frac {\partial f(x)}{dz}k$$

To get the gradient of the function used in the differentiation section of this article, we use the code in the image below:

The Gradient of Several Variables in Wolfram Mathematica

From the above, you would notice that the answer gotten is the set of d[fx]/dx, d[fx]/dy, d[fx]/dz . Check the answers we got when we differentiated with respect to each of the variables in the differentiation section of this article.

Working with Vectors

Unit Vector

If we have a position vector to be r = 2i + 3j + 5k , the unit vector would be:

$$\hat{r} = \frac {\overrightarrow{r}} {\mid r \mid} = \frac {2}{\sqrt{{2}^{2}+{3}^{2}+{5}^{2}}}i + \frac {3}{\sqrt{{2}^{2}+{3}^{2}+{5}^{2}}}j + \frac {5}{\sqrt{{2}^{2}+{3}^{2}+{5}^{2}}}k$$

$$\hat{r} = \frac {2}{\sqrt{38}}i + \frac {3}{\sqrt{38}}j + \frac {5}{\sqrt{38}}k$$

In Mathematica, we use the Normalize function to calculate the unit vector of a vector position.

Unit Vector (Normalize) in Wolfram Mathematica.

Note:

$$\sqrt{ \frac {2}{19} } = \frac {2} {\sqrt{38}}$$

Dot Product

Dot product (also called a scalar product) is the product of two vectors to give us a scalar value. If we have a and b to be vector positions, the scalar/dot product (whose operator is represented by a dot) would be;

$$a.b = |a||b| \cos \theta$$

Theta is the angle between a and b. Using dot product on unit vectors (i, j, and k), we get;

$$i.i = j.j = k.k = 1$$

$$i.j = j.k = k.i = i.k = k.j = j.i = 0$$

If we have a position vector, a = 2i + 3j + 5k, and another, b = 1i + 4j + 6k, the dot product would be;

$$a.b = 2(1)+3(4)+5(6) = 2 + 12 + 30 = 44$$

In Mathematica, we could use the Dot function or the dot (.) symbol to perform scalar product.

Dot/Scalar Product in Wolfram Mathematica.

Cross Product

Cross product (also called vector product) is the product of two or more vectors to give us a vector. If we have a and b to be vector positions, the vector product (whose operator is represented by a cross) would be;

$$a \times b = |a||b| \sin{x}$$

Using the cross product on unit vectors (i, j, and k), we get;

$$i \times i = j \times j = k \times k = 0$$

$$i \times j = k \hspace{1.5cm} j \times i = -k$$

$$j \times k = i \hspace{1.5cm} k \times j = -i$$

$$k \times i = j \hspace{1.5cm} i \times k = -j$$

If we have a position vector, a = 2i + 3j + 5k, and another, b = 1i + 4j + 6k, the cross product would be the determinant of the matrix;

Determinant of a Matrix

Using Mathematica, we make use of the Cross function to perform the vector product of vectors.

Cross Product in Wolfram Mathematica.

Replace All Function (/.)

In maths question, we tend to have questions which says something like, "as x tend to 0, or evaluate the function when x = 10, etc." The replace all function and the right arrow are used to achieve this. The replace function in Mathematica is /. , while the right arrow is -> .

If we have the function;

$$f(x) = y \sin{x}\cos{y}$$

which we hope to differentiate with respect to y when y = 3 and x = 2; our Mathematica code would be:

Differentiation and Replace All Functions in Wolfram Mathematica.

When pi is used with trigonometric functions, it is treated in radians (that is, pi equals 180 degrees instead of 3.142...).

Differentiation and Replace All Functions in Wolfram Mathematica.

In long form, our answer would be ;

$$\cos{\pi} \sin{\theta} - \pi \sin{\theta} \sin{\pi} = \cos{180} \sin{\theta} - \pi \sin{\theta} \sin{180}$$

Where Sin 180 = 0 and Cos 180 = -1; therefore our answer would be;

$$- \sin{\theta}$$

Directional Derivative

Directional derivative is the dot product of the gradient of a scalar field and the unit vector of a vector position.

$$D_uf(x)= u.Grad \hspace{1mm}f(x)$$

Where u is the unit vector of the vector position given.

Example:

If we have a function;

$$\tan^{-1} \frac{xy}{z}$$

and we are to find the directional derivative at the point (2, 4, 5) in the direction (1, 2, 3).

The above question implies that the vector position is 2i + 4j + 5k, and x = 1, y = 2, and z = 3.

We would solve the question using all we have learnt from the previous sections.

Directional Derivative in Wolfram Mathematica.

Graph Plotting

2D Plot

To plot a 2D graph of a function, we make use of the Plot function. If we want to plot Sin x, we would enter the code as shown in the image below;

2D Plot of Sine in Wolfram Mathematica.

The first parameter in the Plot function is the function we hope to plot, the second is the range for the plot, while the other parameters are optional.

Multiple 2D Plots on the Same Graph

For multiple plots on the same graph, we place the functions we want to plot in a set as shown in the diagram below.

2D Multi-plot of Sine, Cosine, and Tangent in Wolfram Mathematica.

Plotting Piecewise Curves

Piecewise Function.

We would plot the piecewise curve using the Piecewise function nested in the Plot function as shown in the image below.

Plot of a Piecewise Function in Wolfram Mathematica.

3D Plot

The Plot3D function is used to plot 3-dimensional diagrams. Three parameters are required in the Plot3D function (that is, the function to be plotted and the ranges of the two variables in the function).

3D Plot in Wolfram Mathematica.

Combining Two 3D Plots

The functions to be plotted are placed in the set as the first parameter in the Plot3D function as shown in the image below.

Two 3D Plot in Wolfram Mathematica.

Getting Help from the Documentation

To access the documentation, click on the Help tab and select Documentation.

Help Button in the Wolfram Notebook

You would see the documentation on the side of the notebook.

Wolfram Documentation.

You could also access the documentation of a built-in function by hovering over it and clicking on the info icon that displays.

Info Icon of the Integrate Function in Wolfram Mathematica.

Documentation related to the function info icon clicked would be visible in the sidebar.

Documentation List of the Integrate Function in Wolfram Mathematica.

ย