Tutorial 3:  Defining

by R. Dover, IMSA

Substituting

It is quite easy to substitute numbers or expressions into variables.  First, let's create an expression and call it "poly."  (Note the space between the x and y in the middle term.  The space indicates multiplication of the two variables x and y as opposed to our name "poly" which is one variable with a 4-letter name.  Spaces are frequently used in Mathematica simply to help make the mathematical expressions easier to read.  In this case, it is necessary.)

poly  =  (x^3) +  4  x y  +  5  (y^2)

To make a substitution, we use the not-so-obvious notation "/." along with an arrow (from the palette or created by a "-" and a ">").

poly  /. x  ->  2

Now substitute y = 5 into poly.  Mathematica does not store these values.

To substitute x = 3 and y = -1 simultaneously, we use curly braces to create a list of values.

poly  /. {x  ->  3,  y  ->  -1}

Substitute x = a and y = b simultaneously.

Now substitute (x^2) + 2 for x and (y^3) - 2  y for y.

When we are finished using  poly, it is important to clear it from Mathematica's memory.  You may wish to re-use the name later.  Also, you don't want the program to assume something you had forgotten about.  (This happens particularly often with functions.  f tends to be a very popular choice!)

Clear[poly]

Defining functions

Here again, Mathematica has some special notation with the underline and the colon.  (There is no space between : and =.)  This is to show a "delayed assignment."  Watch it carefully.  It works.

f[x_] := (x^3) +  5  x  -  2

After defining the function, evaluation works as expected.

f[3]

f[5]  +  f[-2]

f[a^2  +  1]

f[2  +  3  I]

f[f[x]]

Define a new function g and evaluate it with several values and expressions.

When finished, we should clear both f and g as we did before with poly.

Clear[f,  g]

Defining variables

You'll be pleased that this "assignment" uses a simple equal sign!  Define both a and b below, using a semi-colon to denote two inputs in one line.  Only the second (or last) one is shown in output.

a  =  4;  b  =  2

3  a  +  (b^2)

Now define a function k(x).  Then evaluate k(a), k(b), and k(a)/k(b).  (Hopefully, that's defined.)

Want to know if a = b?  This requires a double-equal sign.

a  ==  b

Or ask if a is equal to itself.

a  ==  a

But now we'll redefine the value of b. (This overwrites the old value.)

b  =  5

(b^3) + 2

Short memory?  Or not sure if a value is still in Mathematica's memory?  Just ask.

b

When you're done, be sure to clear a, b, and k.