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

Problem

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.

Comment 1

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

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

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

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

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.

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

Comment 12

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

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

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

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

Comment

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