(*^
::[ Information =
"This is a Mathematica Notebook file. It contains ASCII text, and can be
transferred by email, ftp, or other text-file transfer utility. It should
be read or edited using a copy of Mathematica or MathReader. If you
received this as email, use your mail application or copy/paste to save
everything from the line containing (*^ down to the line containing ^*)
into a plain text file. On some systems you may have to give the file a
name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
The line below identifies what version of Mathematica created this file,
but it can be opened using any other version as well.";
FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";
MacintoshStandardFontEncoding;
fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8, 24, "Times";
fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6, 18, "Times";
fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times";
fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20, 18, "Times";
fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times";
fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times";
fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times";
fontset = input, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, 12, "Courier";
fontset = output, output, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-4, 12, "Courier";
fontset = message, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier";
fontset = print, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier";
fontset = info, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, 12, "Courier";
fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakBelow, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, 12, "Courier";
fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, 10, "Times";
fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = leftheader, inactive, L2, 12, "Times";
fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, 12, "";
fontset = leftfooter, inactive, L2, 12, "Times";
fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times";
fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Courier";
fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 14, "Times";
fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 14, "Times";
fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Courier";
fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "";
paletteColors = 128; automaticGrouping; currentKernel;
]
:[font = title; inactive; preserveAspect; startGroup]
Exericse 03
Set, Rule Assignments, and SetDelayed
:[font = subsubtitle; inactive; preserveAspect]
Copyright ã 1993 by Bill Titus, Carleton College,
Department of Physics and Astronomy, Northfield, MN 55057-4025
September 6, 1993
;[s]
3:0,0;10,1;11,0;133,-1;
2:2,16,12,Times,2,14,0,0,0;1,16,12,Symbol,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Topics and Skills
:[font = special1; inactive; preserveAspect; endGroup]
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.
;[s]
13:0,0;67,1;70,0;160,2;161,0;215,2;222,0;229,2;240,0;280,2;287,0;362,1;372,0;374,-1;
3:7,16,12,Times,0,14,0,0,0;2,15,11,Helvetica,0,14,0,0,0;4,15,11,Courier,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Problem
:[font = special1; inactive; preserveAspect]
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?
:[font = special1; inactive; preserveAspect]
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
:[font = special1; inactive; preserveAspect]
v[t] = v[0] - gt
x[t] = v[0]t - gt2/2
;[s]
3:0,0;42,1;43,0;46,-1;
2:2,15,11,Courier,0,14,0,0,0;1,23,15,Courier,32,14,0,0,0;
:[font = special1; inactive; preserveAspect; endGroup]
The initial information is v[0] = 20.0 m/s and g = 9.8 m/s2.
;[s]
3:0,0;61,1;62,0;65,-1;
2:2,16,12,Times,0,14,0,0,0;1,24,16,Times,32,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Solution using Symbols
:[font = subsection; inactive; preserveAspect; startGroup]
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 each time you're done with a single line. Be sure there are blank spaces between the different symbols. Describe what happens.
;[s]
3:0,0;188,1;195,0;326,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = special3; inactive; preserveAspect; leftWrapOffset = 45; leftNameWrapOffset = 6; endGroup]
g = 9.8
vo = 20.0
v = vo - g t
x = vo t - (1/2) g t^2
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 1
:[font = special1; inactive; preserveAspect; endGroup]
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.
;[s]
3:0,0;195,1;198,0;482,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Helvetica,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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 . Describe what happens.
;[s]
3:0,0;205,1;212,0;238,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 2
:[font = special1; inactive; preserveAspect; endGroup]
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.
;[s]
3:0,0;111,1;115,0;386,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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 . Describe what happens.
;[s]
3:0,0;203,1;210,0;236,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 3
:[font = special1; inactive; preserveAspect; endGroup]
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.
:[font = subsection; inactive; preserveAspect]
Step 4 - Go to your previous set of equations and put in spaces so that the equal signs are all vertically aligned. Then the results.
;[s]
3:0,0;122,1;129,0;143,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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 . What do you find?
;[s]
3:0,0;173,1;180,0;203,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 6 - Now, in the cell you just created, change t to 1.0 and press . What do you now find? Speculate on what MMA is doing.
;[s]
3:0,0;75,1;82,0;141,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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?
;[s]
13:0,0;46,1;48,0;51,1;52,0;60,1;61,0;138,1;140,0;144,1;146,0;154,1;156,0;178,-1;
2:7,16,12,Times,1,14,0,0,0;6,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 7
:[font = special1; inactive; preserveAspect; endGroup]
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?
;[s]
7:0,0;16,1;17,0;109,1;110,0;348,1;355,0;365,-1;
2:4,16,12,Times,0,14,0,0,0;3,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
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 .
;[s]
3:0,0;196,1;203,0;205,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;
:[font = input; preserveAspect; endGroup]
Clear[t]
v /. t -> 1.0
x /. t -> 1.0
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 8
:[font = special1; inactive; preserveAspect; endGroup]
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".
;[s]
11:0,0;36,1;44,0;185,1;192,0;296,1;298,0;330,1;338,0;356,1;358,0;391,-1;
2:6,16,12,Times,0,14,0,0,0;5,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 9- Check to see what the current value is for the symbol t in two different ways.
;[s]
3:0,0;64,1;65,0;89,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
Step 10 - Evaluate v and x for t = 1.5 in a cell below without permanently altering the value for t.
:[font = subsection; inactive; preserveAspect; startGroup]
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.
;[s]
3:0,0;235,1;242,0;303,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = special3; inactive; preserveAspect; endGroup]
Table[v, {t, 0.0, 3.0, 0.5}]
:[font = subsection; inactive; preserveAspect]
Step 12 - Get help on the command Table[] by entering ?Table in the cell below.
;[s]
5:0,0;35,1;42,0;57,1;63,0;84,-1;
2:3,16,12,Times,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 12
:[font = special1; inactive; preserveAspect; endGroup]
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[].
;[s]
13:0,0;31,1;38,0;360,1;364,0;396,1;400,0;408,1;414,0;571,1;579,0;624,1;635,0;637,-1;
2:7,16,12,Times,0,14,0,0,0;6,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect]
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.
;[s]
3:0,0;35,1;46,0;136,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 13
:[font = special1; inactive; preserveAspect; endGroup]
1. Note the usefulness of the nesting property of functions in MMA.
:[font = subsection; inactive; preserveAspect; startGroup]
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?
;[s]
3:0,0;77,1;89,0;184,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = input; preserveAspect; endGroup]
Clear[myTable]
myTable = Table[{t, v, x}, {t, 0.0, 3.0, 0.5}];
TableForm[myTable]
:[font = subsection; inactive; preserveAspect]
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.
:[font = subsection; inactive; preserveAspect; startGroup]
Comments 15
:[font = special1; inactive; preserveAspect; endGroup]
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.
;[s]
3:0,0;173,1;176,0;178,-1;
2:2,16,12,Times,0,14,0,0,0;1,15,11,Helvetica,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
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.
;[s]
11:0,0;36,1;38,0;55,2;65,0;184,2;187,0;255,2;265,0;338,2;341,0;354,-1;
3:6,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;4,15,11,Helvetica,1,14,0,0,0;
:[font = input; preserveAspect; leftWrapOffset = 45; leftNameWrapOffset = 6]
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}]
:[font = input; preserveAspect; leftWrapOffset = 45; leftNameWrapOffset = 6; endGroup]
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}]
:[font = subsection; inactive; preserveAspect; startGroup]
Comment 16
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
1. Some people prefer to use Set and proper sequencing of symbol definitions over SetDelayed. The choice is yours.
;[s]
5:0,0;31,1;34,0;86,1;96,0;120,-1;
2:3,16,12,Times,0,14,0,0,0;2,15,11,Helvetica,0,14,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Practice Problems
:[font = subsection; inactive; preserveAspect; startGroup]
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
:[font = special2; inactive; preserveAspect]
v[t] = v[0]e-ct/m
;[s]
3:0,0;17,1;29,2;35,-1;
3:1,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;1,23,15,Courier,33,14,0,0,0;
:[font = special2; inactive; preserveAspect; leftWrapOffset = 22; endGroup]
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] .
;[s]
7:0,0;6,1;9,0;187,2;188,0;352,2;370,0;373,-1;
3:4,16,12,Times,1,14,0,0,0;1,15,11,Helvetica,1,14,0,0,0;2,15,11,Courier,1,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
Comment
:[font = special1; inactive; preserveAspect; endGroup]
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.
;[s]
5:0,0;43,1;44,0;52,1;54,0;177,-1;
2:3,16,12,Times,0,14,0,0,0;2,15,11,Courier,0,14,0,0,0;
:[font = subsection; inactive; preserveAspect; startGroup]
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.
;[s]
3:0,0;159,1;160,0;206,-1;
2:2,16,12,Times,1,14,0,0,0;1,15,11,Courier,1,14,0,0,0;
:[font = input; preserveAspect; endGroup; endGroup]
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
:[font = section; inactive; preserveAspect; startGroup]
Save Your Notebook
:[font = special1; inactive; preserveAspect; endGroup; endGroup]
Remove any output cells from this notebook and then use Save As to store your notebook under the name myEx03.
;[s]
5:0,0;57,1;64,0;105,1;111,0;113,-1;
2:3,16,12,Times,0,14,0,0,0;2,15,11,Helvetica,0,14,0,0,0;
^*)