Learning Mathematica Basics—Using Variables

Solve each of the following problems using Mathematica exclusively.

1.  Create a variable called bill by simply typing it in, and hitting ENTER.

2.  Now assign a value of 18/12 to a variable called bob by using an equals sign.  (Note: the fraction is automatically reduced.)

3.  Now create another variable called newt, giving it the value 12.

4.  Add the variables bill, bob, and newt together, and assign the result to a new variable called pals.

5.  Notice how Mathematica handled the combination of the variables!  bob and newt both had numerical values, and were automatically combined into a single fraction.  bill, on the other hand, had no numerical value assigned to it.  As a result bill still appears in its unevaluated form in the result.

Now remove the value of bob using the command you learned in class.  (bob=.)

6.  Investigate the affect that removing bob's value had on pals.  To do this, just type pals followed by ENTER.

7.  Notice that the change in the value of bob made no difference to the value of pals.  This is because of the way we assigned the values to our variables so far, i.e. a standard equals sign.  The standard equals sign when used in Mathematica does an immediate assignment of the values to the right of the equals sign into the variable on the left of the equals sign.  This means that pals is not remembered as being bill+bob+newt, but rather as the sum of whatever values bill, bob, and newt had at the instant the command was given.

But what should we do if if we want Mathematica to remember that pals is really equal to bill+bob+newt all the time, and to use whatever the current values of the variables are?  In that case we should have used Mathematica's delayed assignment operator colon equals ":=".  Issue the new command bestbuds:=bill+bob+newt.

8.  You should have noticed that this assignment gave no feedback, unlike your earlier command when you defined pals using a standard equals sign.  Now to find out what value is  currently in bestbuds simply type its name and hit ENTER.

9.  This looks right (I hope!)  Only newt currently has a numerical value (12).  bill was never given a value, and bob's value was removed back in problem 5.  Now let's see if bestbuds can keep up with any changes we make to the values of its component variables.  Give bill a value of 50.

10.  …and type bestbuds, followed by ENTER.

11.  That's more like it!  The delayed assignment is working!  Now set bill equal to x, set bob equal to y and set newt equal to z, and reevaluate bestbuds.  (4 commands in all.)

12.  Looks like delayed assignment continues to work!  Just to re-emphasize the difference between delayed assignment and immediate assignment reevaluate pals one more time.  Notice that it has no clue about the new values we gave to bob, and newt.  bill, on the other hand, is different.  Since pals's last value still contained a bill in the formula (look back at problem number 6), the bill variable has now been replaced by it's new value of x.