CS 221 Lab 3 Fall 2011: Conditionals and MATLAB

Out: 14 September 2011
Due: 22 September 2011

Note: To save your work for later completion, copy/save the spreadsheet to removable media (flash drive) or your locker.


Part 1: Excel Conditionals

Download and save the Excel spreadsheet from Tuesday's lecture (the HVAC example) to your desktop.

  1. The thermostat in the model is unrealistic in that it has no hysteresis. That is, the heating/cooling control unit turns on whenever the sensor temperature is not equal to the thermostat setting, even if they differ by only 0.01 degree. (Demonstrate this by typing 73.01 into cell A2, and note that the cooling unit comes ON.) This causes the system to cycle on and off constantly; even worse, the cooling unit will make the temperature go below the setpoint, then the heating unit will come on and raise it above the setpoint, so the cooling unit comes back on, and so on - one of the units will almost always be running!

    Real thermostat systems have some hysteresis: the thermostat setting actually defines a "zone" a degree or two wide around the setpoint. The system will not turn on unless the termperature goes outside that zone.

  2. We can add hysteresis to our model system by changing the conditions so that the cooling only comes on when the temperature gets at least 1 degree above the setpoint, and the heat only comes on when it is at least 1 degree below the setpoint.

    However, we would like the amount of hysteresis to be easily changeable later.

    Note this general principle of good programming: Name any constant that appears in any expression in your code, and refer to it by its name (or by absolute location in Excel) instead of typing in the number. That way you can change it everywhere just by changing the value of the variable in one place.

    Type "Hysteresis" in cell A7. Type "1" in cell B7. (The hysteresis zone of our thermostat will be two degrees wide.)

  3. Modify the formulas in B11 and B12 so hysteresis is taken into account when comparing the current temperature to the setpoint. Be sure to refer to cell B7 (use an absolute reference), and do not just add a constant!

    Verify that the new formula is correct: With the setpoint at 73, the cooling unit should not come on until the temperature sensor (A2) reads above 74; heat should not come on until the sensor reads less than 72. (Note: Make sure the occupancy sensor is set to "TRUE".)

  4. Now we want to add some conditional formatting to the output values (similar to what was done in lecture).

    1. Select cells B11:B13.
    2. Click and hold on the "Conditional Formatting" button in the ribbon, and select "Highlight Cells Rules" -> "Equal to". A pop-up window titled "New Formatting Rule" should appear, with preselected pull-down menu items that say "cell value" and "equal to". In the box next to "equal to", type "OFF". (Do not click "OK" yet!)
    3. Below that it should say "Format with", with a pull-down menu next to it. Select "Custom format...". In the window that pops op, set the Font color to white and the fill color to red.
    4. click on "OK" (twice) to apply the change. check that the cells in b11:b13 have a red background if the value is "on".
    5. Now we will add another rule to make the cell background green with white letters whenever the ouptut value is "ON". With the same three cells selected, select "Conditional Formatting" -> "Manage Rules". Click "+" in the window that pops up, to add a second rule. Go through the same process as above to add the cell formatting ("Fill" -> green, "Font" -> white). Save your modified spreadsheet to the desktop or memory stick.

  5. Demonstrate your spreadsheet to the instructor by doing the following:

  6. After you have shown the instructor your spreadsheet, upload the file (lab3part1.xlsx) as "Lab 3 Part 1" through the online submission page.

Part 2: MATLAB Fundamentals

In this part you will create a script to compute the real-valued solutions to quadratic equations, i.e., equations of the form ax² + bx + c = 0. Recall from high school algebra that if b² - 4ac is non-negative, this equation has two real solutions, which are:

Follow these steps to create your script file:
  1. Select "File" -> "New" -> "Script" from the main menu. The editor window should open.

  2. Type "% lab3part2 - find real solutions for quadratic equations" in the editor window.
    This is a comment, which does not affect the execution of the program (i.e., MATLAB ignores it). It is good practice to put a comment at the top of each script file to describe what it does.

  3. On the next three lines, use the input function to read in the values of three variables named a, b, and c, one at a time Use a prompt that says "Please enter a: ", "Please enter b: ", etc. Be sure to leave a space after the colon; it looks much nicer. Also, end each line with a semicolon, to suppress the echoing of the assignment.

  4. On the next line, Compute the quantity b² - 4ac and assign it to a variable called "discrim". Suppress the echoing by ending the line with a semicolon ";".

  5. On the next five lines, write an if-else statement that tests whether the roots of the given equation are real, i.e., the value of "discrim" is non-negative, and acts accordingly. Recall that an if-else statement has the form:
    if <boolean xpression>
         <true branch>
    else
         <false branch>
    end
    

    1. The boolean expression of the if statement should test whether discrim is non-negative, that is "discrim >= 0".
    2. The true branch (the statement executed if the boolean expression is true) should assign the values of two variables, root1 and root2, according to the quadratic formula, e.g.
        root1 = (-b + SQRT(discrim))/(2*a))
      
      Omit the semicolon, as above, so the MATLAB echoes the assignment.
    3. The false branch (following the "else" keyword) should print "Sorry, the equation has only complex solutions." Use the disp() function. Remember that strings in MATLAB are enclosed in single quotes, so it should look like:
        disp('Sorry, the equation...')
      

  6. Save your script as "lab3part2.m" to the Desktop or your removable media.

  7. Make sure the folder where you saved the script is part of your path. (You can check this using the menu in the Current Directory window.) Run your script by typing "lab3part2" (without the quotes) in the command window. Test it with the following values: a = 1, b = -1, c = -12, and verify that the roots are 4 and -3.

  8. Show your script to the instructor. Run it, and supply the values 2, -16, and 30 for a, b, and c, respectively.

  9. After the instructor has OK'd your work, upload your script via the submission portal as "Lab 3 Part 2". .