Exericse 03
Set, Rule Assignments, and SetDelayed

Copyright „ 1993 by Bill Titus, Carleton College, 
Department of Physics and Astronomy, Northfield, MN  55057-4025
September 6, 1993

Topics and Skills

1.  Using symbols for calculations.
2.  Immediate assignment with  Set.
3.  Multiple inputs in a single cell.
4.  Semicolon to suppress output.
5.  Help using  ?
6.  Immediate assignment with replacement rules.
7.  Table[]  and  TableForm[].
8.  Lists - a brief introduction.
9.  Clear[].
10.  MMA's symbol for the exponential constant  e.
11.  Assignment with  SetDelayed.

Problem

A ball is thrown vertically upward at  20.0 m/s  from the top of the Olin Hall of Science.  What's the ball's velocity and position for times  0.5,  1.0,  1.5,  2.0, 2.5, and  3.0 seconds after launch?

The basic free fall equations, assuming the x-axis points vertically upward and the origin of the coordinate system is at the launch point, are

v[t] = v[0] - gt
    x[t] = v[0]t - gt2/2

The initial information is  v[0] = 20.0 m/s  and  g = 9.8 m/s2.

Solution using Symbols

Step 1 - You're going to use symbols to help out in doing your multiple calculations.  You'll first enter the general equations that you'll use.  Type in the following commands, pressing  <Enter>  each time you're done with a single line.  Be sure there are blank spaces between the different symbols.  Describe what happens.

g = 9.8
vo = 20.0
v = vo - g t
x = vo t - (1/2) g t^2

Comment 1

1.  Symbols are used to stand for quantites, including numbers, equations, and even collections of other symbols.  Use lower case letters for your symbols.  Why?
2.  The equal sign (also called  Set) is an immediate assignment.  How can you tell it is from what you just entered?
3.  What would happen if you didn't have a space between, say,  g  and  t?  Try it out by editing your previous input.  What do you find?
4.  Note that all the inputs and outputs are in separate cells.

Step 2 - Now you're going to enter all the expressions together in a single cell.  To do so, retype the symbols in Step 1, but now type a return after each line except the last one where you should press  <Enter>.  Describe what happens.

Comment 2

1.  Note that all the inputs are in a single cell but the outputs are in separate cells.
2.  To what does the  In[]  index refer in a cell containing multiple inputs?
3.  What are the advantages of having all the equations in a single cell? 
4.  What if you didn't want to have the results for  g  and  vo  echoed to the screen.  In MMA, a semicolon after a line suppresses its output.

Step 3 - Edit the previous input cell and put a semicolon after the lines corresonding to  g  and  vo.  Then send the calculations to the kernel by clicking the cursor anyplace in the cell and pressing  <Enter>.  Describe what happens.

Comment 3

1.  What are the advantages and disadvantages of using semicolons to suppress output?
2.  MMA doesn't care about extra spaces in equations, and you can use extra spaces, including indents, to make your equations more readiable.

Step 4 - Go to your previous set of equations and put in spaces so that the equal signs are all vertically aligned.  Then <Enter> the results.

Step 5 -  You're now going to find  v  and  x  for  t = 0.5.  To do so, open a cell below and type in  t = 0.5,  followed by  v  and  x, all on separate lines.  Then press  <Enter>.  What do you find?

Step 6 - Now, in the cell you just created, change  t  to  1.0  and press  <Enter>.   What do you now find?  Speculate on what MMA is doing.

Step 7 -  Verify your guess by first entering  t,  v,  and  x  in a single cell, but on separate lines.  Then, in separate cells, enter   ?t,   ?v , and  ?x .  What do you find?

Comment 7

1.  The symbol  ?  is your friend.  It gives help and is an extremely useful MMA command.
2.  You can't put  ?  on multiple lines.
3.  What's crucial here is that  t  wasn't assigned a value before  v  and  x  were defined.  This wasn't true with the symbols  g  and  vo.  That's where immediate assignment comes into play. 
4.  What do you think  Global`  means?

Step 8 - The following is a replacement technique that allows  t  not to be permanently replaced as it was in the previous calculation.  Look over the cell below and then activate it by pressing  <Enter>.

Clear[t]
v /. t -> 1.0
x /. t -> 1.0

Comment 8

1.  Not surprisingly, the function  Clear[] removes previous values set for symbols.  It can contain more than one symbol provided commas separates the symbols.  You might want to use  Clear[]  before introducing any collection of symbols, even if it isn't necessary to do so.
2.  You can read   /.  as  "given".
3.  In the rule  t -> 1.0,  you can read   ->  as "goes to" or "replaced by".

Step 9- Check to see what the current value is for the symbol  t in two different ways.

Step 10 - Evaluate  v  and  x  for  t = 1.5  in a cell below without permanently altering the value for  t.

Step 11 - In your free fall problem, you have lots of values for  v  and  x  to calculate -- the values corresponding to the times  0.0,  0.5,  1.0,  1.5,  2.0,  2.5,  and  3.0.   You can automate such repeated calculations using the  Table[]  command.  Type in the following and describe what happens.

Table[v, {t, 0.0, 3.0, 0.5}]

Step 12 - Get help on the command  Table[]  by entering  ?Table  in the cell below.

Comment 12

1.  Describe the structure of  Table[], including its arguments.  Note that square brackets are used to contain the arguments of the function, and curly brackets are used to form lists.  Lists are very important in MMA.
2.  What kind of object is the iterator?
3.  Why does the iterator have so many different forms? 
4.  The output is a list.
5.  The symbol  expr   can, itself, be a list.  If  expr  were  {t, v}, a list of values of both  t  and  v  would be produced.  Try this out by editing the cell in Step 11.
6.  If you want the list in tabular form, place the  Table[] command as the argument in the MMA function  TableForm[].

Step 13 - Get help of the command  TableForm[]  and then produce a list of  t,  v, and  x  in tabular form over the desired time range.

Comment 13

1.  Note the usefulness of the nesting property of functions in MMA.

Step 14 - You could assign a symbol to the table to begin with and then use  TableForm[] on that name.  Examine the code in the following cell and then activate it.  What do you find?

Clear[myTable]
myTable = Table[{t, v, x}, {t, 0.0, 3.0, 0.5}];
TableForm[myTable]

Step 15 - Suppose you want to repeat the series of time calculations for  v[t]  and  x[t], but now with  vo = 50 m/s.  Further suppose you want to print the results in a table with columns corresponding to  t,  v[t],  and  x[t].  Do this in a single cell below.  Remember to use semicolons to suppress unnecessary output.

Comments 15

1.  If you had not defined  vo  before defining  v  and  x, you could have used your old code.  Note the importance of the order in which symbols are defined when one uses  Set.

Step 16 -  The delayed assignment,  :=,  (also called  SetDelayed) doesn't assign a value until the symbol is called.  It can be used to avoid some predefinition problems arising from Set.  Verify that the following code produces two different tables if  SetDelayed  is used in the definitions of  v  and  x, and two identical tables if  Set  is used.

Clear[g, vo, v, x, t]
g  = 9.8;
vo = 20.0;
v := vo - g t
x := vo t - (1/2) g t^2
Table[{t, v, x}, {t, 0.0, 3.0, 0.5}]
vo = -20.0;
Table[{t, v, x}, {t, 0.0, 3.0, 0.5}]

Clear[g, vo, v, x, t]
g  = 9.8;
vo = 20.0;
v  = vo - g t;
x  = vo t - (1/2) g t^2;
Table[{t, v, x}, {t, 0.0, 3.0, 0.5}]
vo = -20.0;
Table[{t, v, x}, {t, 0.0, 3.0, 0.5}]

Comment 16

1.  Some people prefer to use  Set  and proper sequencing of symbol definitions over  SetDelayed.  The choice is yours.

Practice Problems

1.  A block of mass  m = 2.0 kg  is projected at an initial velocity of  20 m/s  along a horizontal surface.   Air resistance is present and modeled with a force proportional to the block's velocity, i.e.,   -cv,  where  c  is a positive constant.  Assume that  c = 0.50 kg/s.   Under these circumstances, Newton would predict that

v[t] = v[0]e-ct/m

Using Set, produce a table of values of  t  and  v  for  t  going from  0.0 s  to  5.0 s  in steps of  1.0 s.  You'll need to know that MMA's  symbol for the exponential constant  e  is  E.  Arrange your code so that you could easily vary  m,  c,  and  vo,  and that the only output that occurs is the final table.   Be sure your start your code with  Clear[vo, c, m, v] .

Comment

1.  Find out information about the symbol  E  using  ?
2.  Describe how you did the calculation.
3.  Remember to use indents and spaces to improve the readability of your code.

2.  Consider the following code.  What are the values of  x1  and  x2  at the appropriate places?  Verify your conclusion by entering the code and then using  ?  to find out information about  x1  and  x2.

Clear[vel, time, x0, x1, x2]

vel  = 10.0;
time = 20.0;

x1  = x0 + vel*time;
x2 := x0 + vel*time

x1
x2

x0   = 20.0;
vel  = 5.0;
time = 10.0;

x1
x2

Save Your Notebook

Remove any output cells from this notebook and then use  Save As  to store your notebook under the name  myEx03.