CS 221 Lab 1 Fall 2011

Out: Wednesday 31 August 2011.
Due in lab on Thursday 8 September, 2011.
Note that there are two parts (one Excel and one MATLAB) to upload. Do not upload until your TA has OK'd your work.

Part 1: Excel

  1. Download the Excel spreadsheet for the Chapter 1 example (Figure 1.3 and Figure 1.4) to the Desktop from this link.

  2. Start Excel: Start-->Programs-->Microsoft Office-->Microsoft Excel 2010

  3. Open the Excel spreadsheet:    File-->Open-->Desktop-->Ch1_1_4.xls

    Note: after a few minutes you may get a pop-up box indicating the Excel cannot automatically save your spreadsheet. You can ignore this message.

     This Excel spreadsheet solves the model of the projectile problem (calculate the maximum height of the projectile) discussed in Chapter 1 of your text, and in lecture (see slides from lecture)

    The spreadsheet has cells for:

    Only the maximum height is displayed (F11).

    The variables t and h get their initial values from A5 and B5.

  4. Click on A6. Notice that the formula bar says "=C5". Now double-click on A6, and notice that the formula "=C5" appears over the cell, and that cell C5 is highlighted. This indicates that the value in A6 depends on the value in C5. Be sure to hit "enter" before you click on anything else - otherwise you will mess up the formula in A6.

  5. Now double-click on C5. Notice that the formula is "=A5+0.1", and that A5 is highlighted. Thus, the value in column C (t_new) depends on the current value of t, and becomes the value in column A on the next row.

  6. Click on B6, to see where its value comes from. Then click on B7, then B8, and down the column, noticing where each cell's value comes from. Notice that each value in column B comes from the value one row higher in column D.

  7. Now double-click on D5. The formula shown corresponds to the equation for height in Chapter 1 of the text (equation 1.1). Note that four cells are highlighted, including the cells labeled "Gravity", "Speed", and "Angle" at the top.

  8. Hit return and then double-click on D6. Notice that the same three cells, containing the constants of the problem, are highlighted, but the other cell is shifted. The dollar signs in the cell references (as in $B$2 in this formula) turn them into absolute references. That means that when you copy a formula to another cell, Excel does not update those references according to the location change.

  9. Now click on F5, then F6, F7, etc., while you look at the formula bar. Notice that the row numbers in the formula change as you go down the column. This is the way spreadsheets typically work: a cell's value depends on other cells in nearby rows and columns, and the spatial relationship doesn't change as you go across the row or down the column.

    These cells are using a conditional formula ("IF" statement) to display nothing (indicated by the empty string "") unless h_new (D7) is smaller than the previous value (h, B7) - i.e., unless the height decreased during that step. We will talk about conditionals and "IF" statements later.

  10. Cell values can be changed, and Excel will update the formulas of all cells that depend on the changed values. Change the value in cell B2 (speed) to 9.5, and hit return; the values in columns B, D, and F should change. (If they don't, click on the check mark in the formula bar, or on a vacant cell). Note the changed value of h_max. Change the speed back to 10.

  11. To change the accuracy of the answer, the time increment (now set at 0.1) can be changed. However, that appears as a constant in the formula of every cell in the range C5:C11. Each of these cells have to be changed if we want to try a different increment. There is a quick way to do this that is described in the textbook. However, we want to make our spreadsheet more flexible, so that the time interval is a parameter of the model.
    1. Add a new row after row 3 (click on the number for row 4, right click, click on insert).
    2. In A4 type: "Increment:"
    3. In B4 type: 0.1
    4. Change cell C6 to: A6+$B$4
    5. Propagate the modified formula in C6 to cells C7 to C12 by clicking on C6, then dragging the file handle through cell C12 (see page 31 of your textbook, or get help if you need it).
    When you execute (click the check symbol or an empty cell) the answer (h_max) should not change.

  12. Now change the increment to .2 and execute. Note the changed values for h_max. The first h_max now appears in F9. The cells below it in the column have the value of the height after the maximum. (You should understand why this is.)

  13. Change the increment to 0.05. No values appear in column F (h_max) because for the seven trials (rows 6 to 12) the projectile is still on the way up. Highlight row 12 (click on its number). Copy it (right click; select "copy"). Highlight rows 13 through 20. Paste row 12 in these rows (right click, select paste, and select the icon for the first option). Now there are enough iterations to allow the projectile to decrease in height. The maximum height is displayed in F18. Click on the cells in row 20. Note that they all have correct formulas for that row.

  14. Experiment with different initial values (B1-B4).

  15. Show the TA the improved spreadsheet, save it, and upload it as Lab Exercise 1a via the CS Portal. (Note: It doesn't matter what you call the file; the submission will be automatically named by the submission system.)


Part 2: MATLAB

  1. Download the MATLAB Chapter 1 example to the Desktop from this link.

  2. Start MATLAB:

    Start-->Programs-->Mathematics Software-->MATLAB-->R2011a-->MATLAB R2011a (the bottom of the three links. DO NOT click on activate or deactivate MATLAB). If you have not used MATLAB before, familiarize yourself with the various areas of the screen:

  3. Make the Desktop your current folder: Click on the arrow to the right of the directory name in the top text box. In the list that appears, double click on it .

  4. Open the MATLAB program labex1-2.m for editing (double click on it). (A program like this in MATLAB is called a script. It consists of a sequence of lines, which MATLAB executes in order. Note: You get exactly the same effect from running a script as you would if you typed the lines of the script in the command window one at at ime.

  5. MATLAB programming will be covered later in the course. Note the initial setting of the problem variables (lines 4 through 11). Line 14 is a decision statement to determine whether to execute the statements in the loop (lines 15 through 20). As long as h_new (the new height) is equal to or greater than the current height  (h), the loop statements will be executed. When the projectile starts to go down in height, the loop is not executed, and the next program statement after the loop (line 23) is executed to display the maximum height.

     Execute the program (Highlight it in the Current Folder window and drag it to the command window. Or you can click the green arrow in the toolbar of the editor window). Note: you will get an error. Your TA will explain the problem and tell you how to fix it. Note the answer when the program executes.

  6. Make the same changes to the program you did to the Excel spreadsheet:
    1. In the editor, change the speed to 9.5 by changing line 10 to say "v = 9.5;" (Note: Case is important. It is a lower case "v". Be sure to include the semicolon, it suppresses the output of the result of executing the line, which depends on the line, but typically MATLAB prints the value of the variable that was changed. Note that there's no semicolon after the last line of the program; that's why MATLAB prints the answer.)
    2. When you make a change to a MATLAB program, you have to save it. Click File (upper left corner)-->Save
    3. Execute the program. Note the lower maximum height. Change the speed back to 10 and save.
    4. Change the time increment (line 8) to .2 then to .01, saving and executing again each time. Note the different answers (don’t forget to save the program after a change). Note also that, unlike in Excel, we don't have to make changes to the program to accomodate the larger number of time points evaluated when we use a smaller increment. Also, with a smaller increment, the answer is closer to the "correct" answer.

  7. Execute the improved program for your TA, save it, and upload it as Lab Exercise 1b via the CS Portal. (Note: It doesn't matter what you call the file; the submission will be automatically named by the submission system.)