Scripting and Programming for Technical Computation file2 • 1 Lab 1CS 123 Computation Lab...

18
Scripting and Programming for Technical Computation Bruce Char Copyright © Bruce Char and Dave Augenblick 2013

Transcript of Scripting and Programming for Technical Computation file2 • 1 Lab 1CS 123 Computation Lab...

Scripting and Programmingfor Technical Computation

Bruce Char

Copyright © Bruce Char and Dave Augenblick2013

Scripting and Programming for Technical Computationby Bruce Char

iii

Contents1 Lab 1CS 123 Computation Lab IIISpring 2013Directions and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Pre-lab preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Part 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.4 Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.5 Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.6 Part 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.7 Final actions (end of class) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.8 Summary and conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Lab 2CS 123 Computation Lab IIISpring 2013Directions and Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Pre-lab preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Part 0: Discussion / Demo of procs to be used in Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.4 Introduction: A mathematical model of an HVAC system, and how use it to write a program simulating how air conditioning cools ahouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.5 Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.6 Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

acState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 airFlowControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.7 Final actions (end of class) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.8 Summary and conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.9 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

iv • Contents

1

1 Lab 1CS 123 Computation Lab IIISpring2013Directions and Problems1.1 OverviewIn CS 121, we learned of basic operations that Maple (and many systems like it) provide: calculation, solution of equations, plotting, and datafitting. None of this really needed any college-level math. In this lab, you will learn how to use some of Maple's more advanced computationalfunctions: diff, int, operations for optimization, piecewise expressions and spline data-fitting. This will allow you to use Maple to computesolutions to problems that benefit from college-level mathematics . Not much programming (procedure construction) is needed this week -- mostof the work can be done interactively or with scripts in code edit regions. In Lab 2, we will return to programming again.

1.2 Pre-lab preparation1. Reading: chapters 16 and 17. Review older chapters and labs as needed. If you are rusty on the math involved, you should refresh yourfamiliarity with the math content (e.g. differentiation and integration) as needed.

1.3 Part 0Download the worksheet CS123Lab1Part0Starter.mw. Fill it in with results and explanation where indicated. Use the word processing features ofthe Maple worksheet to include thoughtful and properly formatted explanations of your results, as directed.

Note : We will do this portion of the lab together.

1.4 Part 1Download the worksheet CS123Lab1Part1Starter.mw. Using the word processing features of Maple worksheets, calculate the solution to apractical minimization problem, providing results and explanation where indicated.

1.5 Part 2Download the worksheet CS123Lab1Part2Starter.mw. Using the word processing features of Maple worksheets, fill it in with results andexplanation where indicated.

1.6 Part 3Download the worksheet CS123Lab1Part3Starter.mw. Using the word processing features of Maple worksheets, fill it in with results andexplanation where indicated.

1.7 Final actions (end of class)Email copies of your work to yourself and/or your partners. Be sure to get credit for doing this on the verification sheet before you leave.

1.8 Summary and conclusionIn this lab, you've gained experience with a few more of the mathematical library functions available in Maple and similar systems. Like solve,they simplify problem solving because you need only input the problem description (whatever numbers, equations, formulas, etc. are necessary);the built-in functionality finds the results for you. Your programming skills are called into play when you build scripts using these operations asbuilding blocks to calculate all the information you need to handle a situation. Some of the problems or the solution approaches don't make senseunless your mathematical knowledge covers the conceptual basis for what the functions know.

2 • 1 Lab 1CS 123 Computation Lab IIISpring 2013Directions and Problems

We have seen a new kind of expression, the piecewise expression, which is a mathematical cousin of the "if -then-else" statement. Piecewiseexpressions differ from if because it doesn't require the variables in it to have known values in order for Maple to do algebraic calculations withit. Maple's mathematical operations such as addition, solve, or differentation work on piecewise expressions but do not always work with if.

Most computer systems, Maple included, contain thousands of built-in procedures which can be used by the programmer to speed theprogramming process. Experienced users learn how to use most procedures by being well-acquainted with the mathematics behind them, and thenconsulting on-line and tutorial documentation. Once they get the basic form and some examples, they write small experimental programs usingthe features until they are convinced they understand how to use them to attain their overall goal.

3

2 Lab 2CS 123 Computation Lab IIISpring2013Directions and Problems2.1 OverviewPart 1 of this lab asks you to build a simulation procedure and supporting scripts from a starter file. Fortunately, you've seen enough examplesof this already so that it's a matter of figuring out how to modify the basic pattern for a time-step simulation, rather than having to figure outeverything from a standing start. In order to do this, you will need to refamiliarize yourself with the time-step simulations that used loops in CS121 and 122 -- the Chemical reaction and Predator-Prey simulations, as well as the bouncing ball simulation.

You are writing your simulation as a Maple procedure, using the ideas from Chapter 11 of the course readings. Having the simulation inprocedure form allows you to use it as a function. Having the simulation as a function makes it easier to run multiple variants of the simulation.Wanting to do that is typical for when you want to use the simulation to predict what will happen under multiple scenarios.

2.2 Pre-lab preparation1. Review older chapters, labs, and quizzes as needed so that you are reacquainted with the prior time-step simulations, looping, conditionalexecution, and procedures.2. Read the introductory directions of the lab. The Introduction section of this lab is much longer than usual because it explains the simulationscenario. Unlike prior work, where the formulas used in the time-step loop are just given to you, in this lab there is an explanation where and howthe code is derived from the mathematical equations of the model.

3. Take the pre-lab quizlet 2 at the CS 123 Maple TA web site. You should do quizlet 2 before lab to be prepared for the first lab.

4. Practice building simple Maple procedures, such as the examples given in chapter 11. You should be able to enter such procedures and getthem to execute. You should be able to invoke the functions you define and get them to return results.

2.3 Part 0: Discussion / Demo of procs to be used in Part 2Today's simulation involves a 2 step process to model an HVAC (air conditioning) system:

Part 1 - extend a starting script to create a base model for cooling a room using a constant cool air flowrate.

Part 2 - model the model developed in Part 1 to allow for controlling the cool air flow rate to keep the room temperature within an acceptablerange.

Part 0 discusses the 2 procs needed to control the cool air flow rate. The control is accomplished using the following 2 procs:

acState(temp, state)

where temp = room temperature for the current step in the time step simulation

state = (low or high) the current step's cool air flow rate (each state is associated with a specifically defined volume of air flow)

airFlowControl(state, lf, hl)

where state = (low or high) the current status of the cool air flowrate

lf = the speciifed low flow rate

hf - the specified high flow rate

Please open the Part 0 demo script (Lab2Part0.mw) for additional information along with a demo illustrating how these 2 procs work in tandem.

4 • 2 Lab 2CS 123 Computation Lab IIISpring 2013Directions and Problems

2.4 Introduction: A mathematical model of an HVACsystem, and how use it to write a program simulating howair conditioning cools a houseWe can model the heating and cooling of a house (called HVAC -- Heating, Ventilation, and Air Conditioning) through a simulation. Recall thatin the bouncing ball simulation, we had four variables of interest, x and y position, and x and y velocity. However, the model had other symbolscalled parameters, which for the most part had fixed values during the entire simulation: g the gravitational force constant, R and η the elastic andfrictional force coefficients, etc.

The problem in this Lab is to model the cooling of an air conditioned house. The house under consideration has the following features:

1. It is rectangular, approximately 10 x 50 x 8 feet.2. It has eight windows, each 2 x 4 feet.3. It has an air conditioner, which blows cold air into the house.

4. There are two variables of interest:

a) the air temperature inside the house (in ), and

b) the temperature of the exterior wall(s).

5. Initially, the interior of the house is at the same temperature as the outside, before the AC is turned on. However, as the house cools off (if it

does), then and will decrease.

6. We consider both and as functions of time, and so write them as and . The simulation's goal is to compute these

temperatures as they change over time, and then produce plots and other information.

7. The house has additional sources of heat, which can be modeled as necessary. In this problem, we will assume that there are two computerswith monitors turned on,. Each computer system generates 400 BTUs/hour of heat. The model has provisions for other sources of heat but in thisscenario we will assume that they are shut off.

A somewhat incomplete diagram of the air conditioned house (courtesy of Drexel University AC 380 class, Spring 2009)

2.4 Introduction: A mathematical model of an HVAC system, and how use it to write a program simulating how air conditioning cools a house • 5

Fundamental modeling relationships

The relationship between the rate of change of with respect to time , and the interior air temperature and wall temperatures at

that time can be modeled as:

(1)

Here is a quick guide to all the symbols in this equation:

Symbol Meaning Nominal value

time to be calculated (a variable)

Exterior wall area ( ) =

Exterior window area ( )

Entering air flow volume (cubic feet per minute)

Outside temperature (in )

Entering air temperature

interior side U value for the exterior wall ( )

zone volume (in ) =

Air specific heat (in )

Heat load from the heater (BTU/hr)

Heat from solar and other internal thermal loads (BTU/hr) =

Air density (in )

Exterior wall temperature (in ) to be calculated

Zone temperature (as a function of time in ) to be calculated

Rate of change of zone temperature with respect to time. to be used in a formula

6 • 2 Lab 2CS 123 Computation Lab IIISpring 2013Directions and Problems

window U value ( ) (see http://www.crittall-windows.co.uk/

content/3/96/what-is-a-window--u-factor-.html) This is related to theinsulation property.

The relationship between the rate of change of and and the interior air temperature and wall temperatures at that time can be modeled as:

(2)

A quick guide to the symbols in (2) that do not appear in (1) are:

Symbol Meaning Nominal value

Exterior wall thickness (ft)

Exterior side U value for the exterior wall ( )

Exterior wall specific heat ( ) (see http://

www.nmsea.org/Curriculum/Courses/Passive_Solar_Design/IV.htm)

Exterior wall density ( )

(1) and (2) are not quite in the right form to build a simulation program of the type we are familiar with, but they are close. Recall that thechemical reaction equations have calculations for each step along the loop

new value of A := some expression involving old values of A, B, X and Y;

...

new value of Y := some expression involving old values of A, B, X and Y;

What we are looking for in this problem is to write a loop for i where successive values of are stored in a table TZ: TZ[0], TZ[1], TZ[2], ...

etc. Similarly, values of are stored at TEW[0], TEW[1], etc.

Thus we are looking for a way of writing

TZ[i+1] := some expression involving older values of TZ and TEW;

TEW[i+1]:= some expression involving older values of TZ and TEW.

Recall from your study of derivatives in calculus that for a small value of h and a"reasonable" functions ,

. (3)

2.5 Part 1 • 7

If we set dt=h, the time interval between steps of the simulation, then

and . (4)

Since TEW[i] is the supposed to be the value of , then substituting (3) and (4) back into (1) we get:

(5)

If we solve (5) for TZ[i+1] we get

(6)

If we enter (1) into Maple, we can get it to derive the right hand side of (6) with a little editing and use of solve for TZ[i+1].

If we go through similar substitutions for in (2), we get that

(7)

After doing such derivation in Maple, we could lprint the right hand sides of (6) and (7) to put the formula in the textual format that would bereasonable to include in Maple code:

Tz[i+1] := (rho[a]*V[z]*c[a]*Tz[i] + q[s - int]*dt + U[wi]*A[ew]*dt*Tew[i] - U[wi]*A[ew]*dt*Tz[i] + U[win]*A[win]*dt*T[0] -U[win]*A[win]*dt*Tz[i] + q[heater]*dt + rho[a]*Q[ea]*c[a]*dt*T[ea] - rho[a]*Q[ea]*c[a]*dt*Tz[i])/(rho[a]*V[z]*c[a])

Tew[i+1] := (rho[ew]*L[ew]*c[ew]*Tew[i]-U[wi]*dt*Tew[i]+U[wi]*dt*Tz[i]+U[wo]*dt*T[0]-U[wo]*dt*Tew[i])/(rho[ew]*L[ew]*c[ew]) (8)

A third year undergraduate taking a numerical analysis course might be expected to derive (8) from (1) and (2) on their own. We don't expectyou to do that, but have included the derivation as a demonstration of how basic principles and physics and calculus determine the code that's in asimulation program.

You're going to use the formulas in (8) in the rest of the lab, to simulate the behavior of the air conditioned house under various conditions ofheating and cooling.

2.5 Part 1Retrieve Lab2Part1Starter.mw. It contains a code window listing the parameters and their value assignments, as well as the code from (8).

8 • 2 Lab 2CS 123 Computation Lab IIISpring 2013Directions and Problems

Using simulations of the past as models, write a simulation that calculates the values of and as they change over time. The values

should be stored into two tables, TZ and TEW.

A plot of the air temperature and wall temperature changing over time should be produced as a result. You should plot both temperature curvestogether -- air temperature should be red, and wall temperature should be blue.

Unlike past simulation problems, we are not telling you how to do this except that you can use techniques similar to the simulations of past labsand quiz problems where time steps are involved. As a hint, you should use the following input parameters:

Parameter Design purpose

Tea0 The initial temperature of the cool air being put out by the HVAC. This might be something in the range50-65 degrees.

Qea0 Initial airflow of cool air pushed into the room by the HVAC fans, in cubic feet. 3000 might be reasonable.

Variables used only used internally by the procedure Design Purpose

N The number of time steps.

Time A table containing values of elapsed time.

TZ A table containing values of the zone(room) temperature.TZ[0] is the initial temperature (T0), TZ[1] the temperatureafter the first time step, etc.

TEW A table containing values of the exterior wall temperature.TEW[0] is also T0. We wouldn't expect the exterior walltemperature to fall as much as the room temperature. If the wallhad very poor insulation qualities, then we wouldn't expect theroom temperature to fall much because any cooling introducedby the HVAC would be swamped by the heat from the outsideseeping in.

Entering items into the tables Time, TZ, and TEW up in a loop will allow you to generate the needed plots. Remember that after accumulatingresults into tables, the tables have to be converted into lists in order to have plots work.

Test your program by having it compute only a few time steps at first. When you get the simulation producing reasonable results, try a longerrun so that you can see "steady state" temperatures. You should see the simulation cool off the room. How cool and how fast will depend onparameter values for Qea and Tea.

How to tell whether your function is working properly.

a) There should be no obvious error messages. There should be no warning messages about undeclared local variables.

b) The results should look correct. Typically, you can do this by augmenting the program to print out additional output and checking that it isvalid (or at least plausible). We're not giving you target output to hit because "in the real world" problems rather than program output, are what ishanded to you.

3. Once your simulation is working, answer the following questions:

a) What is the steady state temperature for the exterior wall, and the air inside?

b) Discover two drastically different ways of setting Q[ea] and T[ea] to establish a steady state temperature of 78 degrees.

4. Save this worksheet as myNameCS123Lab1Part1.mw.

2.6 Part 2We've seen that the model handles a fixed situation -- the HVAC is turned on and spews out air at a constant cool temperature. The HVACsystem puts the same amount of cool air all the time. If we were to model a real HVAC system, we'd want the model to be able to reproduce

2.6 Part 2 • 9

situations where the amount of cold air varies. For example, a thermostatic control might increase or decrease the air flow depending on howclose the room temperature was to a target temperature.

In order to handle this new demand, we do what programmers need to do in this kind of situation -- take a working program and get it to dosometing additional.

We will introduce two new procedures into the control situation, acState, and airFlowControl.

acStateacState is a function whose parameters (inputs) are the current room temperature, and one of the values low or high. The design of the acStatefunction is to describe the thermostat's behavior: it says that the state of airflow should be high when the temperature goes beyond a trigger value,and says that it should be low when the temperature falls below another trigger value.

Here is a sample definition for acState.

#Thermostat control function definition. Trigger temperatures are built into the function

#definition.

acState := proc(temp, presentState)

local triggerTemp, shutoffTemp ;

#temperatures that trigger high speed fan and low speed fan

triggerTemp := 79;

shutoffTemp := 76;

if presentState=low and temp>=triggerTemp

then return high;

elif presentState=high and temp<=shutoffTemp

then return low;

else return presentState;

end if;

end;

This is probably more verbose than needed to control the computer, but it also serves as a way of clearly communicating the originalprogrammer's intentions to anyone who would alter the code because they want to change the thermostatic behavior. So ultimately it may do abetter job than terse, cryptic code that's harder to figure out.

airFlowControlairFlowControl (or a similarly named function) has a procedure that takes three parameters (inputs): the current state of the HVAC fan (on oroff), and the designer's intention for the amount of air flow when the HVAC fan state is low or high. An example definition might be:

#Air flow control function definition

airFlowControl := proc(state,lowFlow, highFlow)

#Parameters to set "low" and "high" settings of fan.

if state=high

then return highFlow

10 • 2 Lab 2CS 123 Computation Lab IIISpring 2013Directions and Problems

elif state=low

then

return lowFlow;

else

error "state input should be high or low, was", state;

end if;

end;

The design of the other function airFlowControl is to specify the amount of air (in cubic feet per minute) that the fan is should blow at the high orlow level. To To aid in "bug proofing" the simulation, the airFlowControl function will generate an error message if the fan state is neither highnor low.

1. Open a blank worksheet and set up an initial code edit region. Copy and paste these two (acState and airFlowControl) proc definitionsinto it. Then add tests into the code edit region to see that the definitions as they should. For example, acState(75,high) should return low,acState(95,low) should return high, airFlowControl(low, 2000, 3000) should return 2000.

Testing these functions now, when you first enter them, is an aspect to the "incremental development" approach to writing software that we havebeen following in this course. Trying to use them in a simulation before you know that they work will increase the complexity of the debuggingtask.

Save this worksheet as myNameCS123Lab2Part2.mw.

2. The next step is to change the simulation model so that it incorporates a thermostat controlling the airflow. Retrieve the worksheetmyNameCS123Lab1Part1.mw. Add in the two function definitions from 1) into the code edit region between the restart and the rest of theinitialization assignments (ie. directly after the "restart" command). Resize the code edit region so that you can see all of the code without toomuch scrolling. Save this worksheet as myNameCS123Lab2Part2.mw.

Make additional modifications to the simulation to incorporate the HVAC acState and airFlowControl procs to control air flow.

i) Incorporate a further (local) variable, state, which is initialized to low before the loop starts.

ii) Introduce additional parameters lf and hf (into the proc parameter list) that define the levels of low and high airflow.

iii) Remove the parameter Qe0 from the proc's parameter list, since the flow rate will now be determined by the control procs.

iv) Inside the loop, the first step (just after the for loop header) is to use acState to compute the new present State.

v) Inside the while loop but before the formula for Tz[i+1], calculate the new fan speedusing airFlowControl and set the result to Q[ea]. Thisway, you will not

have to alter the formulae.

Get HVACSim1 working. As before, it should avoid error or warning messages when defined or operating, and give correct results.

**** To test, the HVAC proc for HVACSim1 should pass in parameters for Tea0, lf and hf.

3. Design air temperature, airflow and thermostat trigger values so that the temperature declines and then stays between 79 and 76 degrees. Findresults so that it takes about 5-10 minutes to get the house to the point where the AC is cycling on and off.

4. Save the final version of your work for Part 2. You will need this result for next week's work.

2.7 Final actions (end of class)Upload copies all of your work to Blackboard, or email copies to yourself and/or your partners. Be sure to get credit for doing this on theverification sheet before you leave. If you cannot complete the work in the lab period, talk to the instructor before you leave about whether youcan get credit for anything beyond what you finished.

2.8 Summary and conclusion • 11

2.8 Summary and conclusionIn this lab, we have gotten you to synthesize code in the fashion that usually occurs in programming. Rather than looking for a cookbook thatlays out all the details of a solution, we rely on our experience to find a relevant pattern, and then do the thinking that adapts the pattern to thesituation at hand:

a) The explanation for how to calculate "new values from previous values" involves use of a differential equation that comes from the underlyingscience (physics in this case). While numerical computation isn't up to "infinitesimals" that derivatives would seem to need, mathematicsprovides a way of coming up with a formula that is a reasonable approximation. We know that the approximation will probably not be goodunless the time steps be fairly small, though.

b) The software pattern involves the use of Maple tables to store a collection of time values, and of temperature values over time

c) The software pattern uses a loop to compute "new values from previous values", store these values into a table, and possibly prints outcomputed values. The pattern also typically has a sequence of initialization actions (time and temperature at the initial time, setting up emptytables, etc.) that happen before the loop.

e) After the loop converts the information in the tables in list form, which can then be printed, plotted, or animated. Post-loop activities can alsoinclude printing out other summary information (totals, minima or maximum).

Following this pattern should allow you to write the code for a variety of simulations.

In Lab3, we will exploit the easy-to-invoke feature of procedures by then connecting the simulation to a graphical user interface. This will makeit easy to invoke the function with different parameter values using much less keyboard action.

It is possible to completely describe some situations using differential equations -- equations involving derivatives, such as we found inexpressions (1) and (2) of the introduction. For such situations, one can avoid writing as much code by calling a library differential equationsolver instead. A numerical d.e. solver can be given the differential equations as input and produces the numerical solution values automatically,without the user having to write the looping code that produces the values. One would still have to write the initialization and plotting code,though.

Maple's numerical differential equation solver is called dsolve/numeric. If you want to find out more about it, you can read the on-linedocumentation for it. A "famous" numerical differential equation solver in Matlab is rk45. The name of the Mathematica solver is NDSolve.

2.9 AcknowledgmentsWe are grateful to Professor Jin Wen of the Civil, Architectural and Environment Engineering Department of Drexel University and her AE 380class for providing us with the technical information used in this lab's model of HVAC.

12 • 2 Lab 2CS 123 Computation Lab IIISpring 2013Directions and Problems

13

Index

14 • Index