Preface


This tutorial was made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica and programming before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel. The Mathematica commands in this tutorial are all written in bold black font, while Mathematica output is in normal font.

Finally, you can copy and paste all commands into your Mathematica notebook, change the parameters, and run them because the tutorial is under the terms of the GNU General Public License (GPL). You, as the user, are free to use the scripts for your needs to learn the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. The tutorial accompanies the textbook Applied Differential Equations. The Primary Course by Vladimir Dobrushkin, CRC Press, 2015; http://www.crcpress.com/product/isbn/9781439851043

Return to computing page for the first course APMA0330
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to the main page for the course APMA0330
Return to the main page for the course APMA0340

Equal Signs


In Mathematica, there are many different commands that involve the equals sign, =. This sign does not just mean equals like it does in normal mathematics. The following commands are different ways of using the = sign.

In Mathematica, the Set [=] defines an expression to be displayed upon execution so it can be checked for errors.
The SetDisplayed [:=] command defines an expression that must be manually displayed. When you use this command, the solution or result of the command will not appear when you enter the equation.
Double equating [==] is used for defining an equation or for testing if an equality is true. When you enter an equation in the form of F[x]==A x+ b, you use this command; the regular [=] is used to define assignments, while [:=] is used to represent the left-hand side in the form of the right-hand side when this is unevaluated.

There are two assignment commands in Mathematica: immediate and delayed assignment. The immediate assignment is performed with the equal sign (=), say x = y, which means "assign to x the value that y has right now". This command has a literal equivalent Set: we could equivalently write Set[x,y]. For delayed assignment, one has to use (:=) (semicolon plus an equal sign), say x:=y. The literal equivalent is the SetDelayed command, for instance SetDelayed[x,y]. This means "add to the global rule base a rule, which will substitute x every time that x is encountered, by the value that y will have at that moment". So, with this kind of assignment, the right hand side is not evaluated at the moment of assignment, but is re-evaluated every time when the left-hand side appears somewhere in the program. This is the main difference between the two assignment operators, since with Set, the left-hand side is assigned the value that the right-hand side has at the moment of assignment, "once and for all".

Normally one uses Set (=) to define variables, and SetDelayed (:=) - to define functions, for which the recomputation of the right-hand side, on a changed argument is a natural operation. However, in this respect Mathematica differs from other programming environments in that the distinction between functions and variables is achieved not on the level of keywords such as, or specific forms of functions declaration, but essentially by the assignment operator that have been used to define the symbol (this is somewhat oversimplified), and also to some extent by a type of global rule associated with the symbol (but again, it is decided based on the syntactic form of the symbol, but at the moment of assignment). This allows one to work with functions and variables on equal grounds. This lack of fundamental distinction between functions and variables is at the heart of the functional style of programming, which is one of the most efficient programming styles in Mathematica and which we will use a lot.

Example: Supose we need to find the Jacobian materix

\[ {\bf M} = \begin{bmatrix} \frac{\partial f}{\partial x} & \frac{\partial f}{\partial y} \\ \frac{\partial g}{\partial x} & \frac{\partial g}{\partial y} \end{bmatrix} \]
for the vector function
\[ {\bf F} (x,y) = \begin{bmatrix} f(x,y) \\ g(x,y) \end{bmatrix} . \]
Using Mathematica, we first define functions:
f[x1_, x2_] = x1*x2^2; g[x1_, x2_] = 3*x1^2 + x2*x1;
Then we define the Jacobian matrix with Set (=) command
M[x_, y_] = {{D[f[x, y], x], D[f[x, y], y]}, {D[g[x, y], x], D[g[x, y], y]}}
{{y^2, 2 x y}, {6 x + y, x}}
So we get the correct answer. The function definition without ":" before the "=" (Set) evaluates the function a first time and the output is assigned to M[x_,y_], so if you input values they are feed into the already-differentiated equations.

If now, you use ":" before the "=", (Set Delay), it evaluates the function when it is needed, hence it will evaluate the function only when values for x and y are set, hence it will not work, since one cannot differentiate a function with respect to 5 (a numerical value for example).

m[x_, y_] := {{D[f[x, y], x], D[f[x, y], y]}, {D[g[x, y], x], D[g[x, y], y]}}
If you try to evaluate the Kacobian at a particular numerical value, it will not work
m[2, 1.3]
But
M[2, 1.3]
{{1.69, 5.2}, {13.3, 2}}
   ■
Final example:
r1 = RandomReal[]
0.641951
r2 := RandomReal[]
Of course, when you enter these commands next time (upon clearning variables), these numerical values will be different. So we check output:
{r1, r2}
{0.641951, 0.689345}
{r1, r2}
{0.641951, 0.664258}

 

Return to Mathematica page
Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)
Return to the Part 7 (Boundary Value Problems)