CS 221 Lab 3 Fall 2011: Conditionals and MATLAB
Out: 14 September 2011
Due: 22 September 2011
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.
- 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
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.)
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".)
Now we want to add some conditional formatting to the output values
(similar to what was done in lecture).
- Select cells B11:B13.
- 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!)
- 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.
- click on "OK" (twice) to apply the change. check that the cells
in b11:b13 have a red background if the value is "on".
- 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.
Demonstrate your spreadsheet to the instructor by
doing the following:
Set "thermostat setting" to 73 and "temperature sensor" to 74.
Everything should remain OFF.
Set "temperature sensor" to 74.01. The Cooling Unit and Fan should go to ON.
Set "temperature sensor" to 72.5. Everything should go OFF.
Set "temperature sensor" to 71. The Heating Unit and Fan should go to ON.
After you have shown the instructor your spreadsheet,
upload the file (lab3part1.xlsx) as "Lab 3 Part 1" through
the online submission
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:
- (-b + sqrt(b^2 - 4*a*c))/(2*a)
- (-b - sqrt(b^2 - 4*a*c))/(2*a)
Select "File" -> "New" -> "Script" from the main menu. The editor
window should open.
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.
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
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
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>
The boolean expression of the if statement should
test whether discrim is non-negative, that is "discrim >= 0".
The true branch
(the statement executed if the boolean expression is
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.
The false branch (following the "else" keyword)
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...')
Save your script as "lab3part2.m" to the Desktop or your removable media.
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.
Show your script to the instructor. Run it, and supply the values 2,
-16, and 30 for a, b, and c, respectively.
After the instructor has OK'd your work, upload your script via
the submission portal
as "Lab 3 Part 2".