TK 115
Complex Equations


Objectives:

  • Learn to use the "IF THEN ELSE" function.
  • Learn to use the "LOOKUP TABLE" Function.
  • Learn to use Shadow Variables
  • Learn to draw double headed flow arrows.
  • Learn to draw "broken" flow arrows.

Constants and Functions:

A constant in a model can take on only one single value throughout the duration of the simulation, for example, a constant birth rate. But what if we were simulating a time period that included a war such that the birth rate had one value during the period of the war and a different value during peace-time? The "IF THEN ELSE" function is used when there is a variable in the model that can take on exactly two different values.

       

Create this model and save it as Account1(similar to Saving1 from lab 1)
Time 0 - 24 Months
Set Account Balance to 500
Determine the equation for Interest

       

Using "IF THEN ELSE"

  • Simulate a situation where the interest rate will be increased from 4 to 7 percent, if the account balance is greater than $1,000.
  • Modify the Account1 model by adding an information arrow from "Account Balance" to "Interest Rate".

           

  • Enter an "IF THEN ELSE" equation for the "interest rate" variable. Select the Equation icon and then the "Interest Rate" variable.
if then else box
    • Click on the "Functions" tab
    • Select the "IF THEN ELSE" function from the list
    • In the equation box:
      • Change "{cond}" to be "Account Balance > 1000"
      • Change "{ontrue}" to be ".07"
      • Change "{onfalse}" to be ".04"
    • Change Type to "Auxiliary"
    • Click OK

  • Now let's run a simulation.
    • Name the dataset "Current"
    • Run a simulation
    • Display the graph

             

    • Note the "kink" in the graph at $1,000!

Save this model as Account1IF.

Using a Compound "IF THEN ELSE" (Just read this part)******************

Now, let's use an IF-THEN-ELSE in a little more complicated situation. Say that we are modeling the situation where the interest rate is .03 up until 2001, is .04 during 2001 and 2002, and then reverts back to .03 after 2002. (Don't be concerned about why this might happen--it's just an example.)

So, before January 1, 2001 and after December 31, 2002 the interest rate is .03. From January 1, 2001 to December 31, 2002 the interest rate is .04. We still have only two possible values for the interest rate so we can use an IF-THEN-ELSE. But, how do we formulate the condition?

Note that if the year is both greater than 2000 and is less than 2003 the interest rate is .04 and otherwise it is .03. The condition can be stated as:

       year > 2000 AND year < 2003

In Vensim we use the Time shadow variable for the year, so...
       

The ":AND:" can be selected from the MORE tab...
       

Lab will tell you how to use below!!

**********************************************************************************************************

Using "IF THEN ELSE" to Increase Realism of a Model:

In a previous lab you worked with a model of drug absorption that was less than realistic in that in the simulation the variables for "ingesting" and "absorption" took on negative values. Let's use the IF THEN ELSE function to improve the realism of the model.

Begin by loading the "Drug_Goal" model you save previously.

       

Modify the equation for "Dosage Gap" to make it equal to the goal minus the level of drug in the bloodstream when the goal is greater than the amount of drug in the bloodstream and zero otherwise.

       

Also modify the equation for "Absorbing" to calculate its value when the concentration in the stomach is greater than the concentration in the bloodstream and to assign a value of zero otherwise.

       

Now run a simulation and look at the graph for each variable.


 
Compare the behavior of "Drug in Bloodstream" to the behavior in the previous lab. The level still overshoots and then oscillates but the oscillations dampen much more quickly. This is because the values of Ingesting and Absorbing do not have as wide a range of values as they did in the other simulation (i.e. no negative values).
 
Save the model as DrugGoalIF.
Show the lab instructor your final graph in the lab and submit the model (.mdl) to Blackboard



Using a Lookup Table

We have learned to use the "IF THEN ELSE" function to model a situation where an auxiliary variable takes on exactly two different values during a simulation. Now, what if we were modeling a situation in which a variable could take on not one, or two values, but three or more different values over the course of the simulation. In that case Vensim allows us to specify all the possible values in a table. The simulation will then pick the approriate value from the table each time period as the simulation progresses from one time period to the next. Of course, there must be some basis for selecting a particular value from the table, so that means that each row of the table will contain two columns. One column contains the values of the variable of interest and the other is used to locate the appropriate value for each time period.

Load the Account1IF model you saved previously in this lab.

       

Let's change the above model so that the interest rate is 3 percent on balances below $500, 5 percent between $500 and $1,000, and 7 percent for $1,000 and above. Now that there are three values for the interest rate the "IF THEN ELSE" function can not be used.

Stated as a table, we have:

Balance        Interest Rate
$0 to $500         .03
$500 to $1,000     .05
$1,000 & above     .07

Now let's create a table for our model and enter the values for Interest Rate.

First, we need to restate the table as two columns of numbers. So, think of the table above in terms of three sentences.

  • Starting at $0 balance pay 3% interest.
  • But, starting at $500 balance pay 5% interest.
  • But, starting at $1,000 balance pay 7% interest.
The table will then be three pairs of numbers:
  • 0, .03
  • 500, .05
  • 1000, .07
But, this still has to be modified for Vensim to function correctly. A fourth pair has to be added for some balance above $1,000 but still at 7%. We will see why this is needed in a minute. Let's use $10,000. So, our table is:
  • 0, .03
  • 500, .05
  • 1000, .07
  • 10000, .07
  • Now enter the table in the model. Click on the Equations tool
  • Select the variable, Interest Rate
  • Click on the drop down box showing Normal and select with Lookup.
  • Enter "Account Balance" in the equation box.
  • Click on As Graph

 
  • Enter the four pairs of values (Notice that 1000 an 10000 are entered without a comma). Also notice that large values are shown in "scientific notation", i.e. 10,000 as 1e+004
  • Click on "Reset Scaling" in the lower right corner of the window to see a graph of the data.
  • Now look at the graph of the values you entered. Without the forth pair in the table the graph would only contain two lines and would stop at $1,000. The forth pair is needed so that 7 percent is used for values above $1,000.

    The system will extend the last straight line to find an interest rate for values higher than those in the table. So, .07 will be used for balances of $1,000, $3,000, $10,000, $12,000, etc. (all values of $1,000 or higher). Without the forth point the straight line from $500 to $1,000 would be extended for balances above $1,000 which would give interest rates over 7 percent.

  • Click OK and OK again.

  • Run the simulation and display the graph for Interest Rate.

           

  • What happened here? The interest rate isn't just 3, 5, or 7 percent. It varies from 5 to 7 percent. The interest rate begins at 5 percent because the beginning Account Balance is $500. The system uses interest rates other than 3, 5, and 7 percent because it interprets the points we gave as points on a line, not as discrete values. So, all interest rates between 3 and 7 percent may be used. In this case the interest rate rises from 5 to 7 percent as the Account Balance increases from $500. To see this, compare this graph of Interest Rate to the "Graph Lookup" above.

    To get discrete values we will have to specify a step function. Enter the following values in the table.

           

  • Click OK and OK and run another simulation. The graph of Interest Rate should look like:

           

  • Save this model as Savings_Lookup1. We will modify this program later in this lab.
    Show the lab instructor your final graph in the lab and submit the model (.mdl) to Blackboard


Shadow Variables

Shadow variables are used in two ways. First they are used to simplify a diagram when one variable is to be connected to another variable for which there is no direct path to draw an arrow. A shadow variable with the same name as one of the two variables that are to be connected is placed in a location near the second variable and then the connection is made between the shadow variable and the second variable.

The second way a shadow variable can be used is to add some special system variables to a diagram. The variable "Time" is one of the system variables. Time is often used with the "Lookup" function to select a value from the table.

To add a shadow variable to a diagram:
arrow window

  • Click on the Shadow Variable icon  comment icon
  • Then position the cursor on the diagram where you want the shadow variable and Click.
  • A window will be displayed.
  • Click on the variable you want to select, specify the variable type, and Click OK.

Shadow Variables and Lookup Functions:

Let's modify a model to use a shadow variable as the locator variable in a Lookup function. In this example the interest rate will not vary depending on the account balance but, rather, the interest rate will increase over time.

Month        Interest Rate
1 to 8          .04
9 to 15         .05
16 to 24        .06

Load "Savings_Lookup1" that you saved previously.
       

  • Click on the Delete icon delete icon        
  • Click on the arrowhead of the information arrow connecting "Account Balance" to "Interest Rate". This erases this connection.

shadow variable window
  • Click on the Shadow Variable icon.  comment icon
  • Move the cursor to below the "Interest Rate" variable and Click. This will display the shadow variable window.
  • Select "Time" as shown in the window and Click OK. This adds the "Time" shadow variable to the diagram.

 
  • Now draw an information arrow from the Time variable to the Interest Rate variable.

           shadow variable window

  • Click on the Equation icon and then on the Interest Rate variable. This displays the equation window.
  • Replace Account Balance as the "with lookup" variable with Time.
  • Click on As Graph. This displays the Graph Lookup window.
  • In the Graph Lookup window, Click on Clear All Points and then enter the points as shown below. Note that the first point starts at zero, not one, and the last point is at 100, not 24.

       shadow variable window

  • Click on Reset Scaling to see the graph of the points entered. Then Click on OK, and Click OK again.
  • Run the simulation and look at the graphs for Account Balance and for Interest Rate. Note that Interest Rate follows the step function from the Lookup table.

           shadow variable window
    Save this model as Savings_Lookup2. We will modify this program latter in this lab.
    Show your lab instructor your final graph in the lab and submit the model (.mdl) to Blackboard


Drawing Flow Arrows:

  • Two-way Flows:
    Usually a flow will be either into or out off a stock. But, occasionally a flow goes both into and out off a single stock. An example of this is the momentum-force model presented by George Richardson.

           

    To draw a double-headed arrow, first draw a one-headed arrow. Then Click on the Arrow or the Move/Size icon and right Click on the small circle at the end of the flow arrow that you wish to add an arrowhead to.

           

    The following window will be displayed.

           arrow window

    Click the "Arrowhead" box in the upper left corner and Click OK. That's it, you now have a double-headed arrow.

    Try drawing the Momentum-Force diagram now.

  • Drawing "Broken" Flow Arrows:

    Usually a flow arrow can be drawn directly between two stocks or between a "cloud" and a stock. But, on occasion, a flow arrow cannot be drawn directly from origin to terminus or a diagram is clearer if an arrow is drawn in segments as in the case below in a diagram presented by George Richardson.

           

    To draw a flow arrow in segments, hold down the shift key as you move the mouse to draw the arrow. At each point where you want to change directions, Click once and then draw the next segment. Note that Vensim will only draw straight horizontal or vertical segments.

    Try drawing the above diagram now.

Submit the model (Momentum.mdl) to Blackboard.

Quick Check: When a variable can have either of two values over the duration of a simulation it is modeled as a(an)...
    constant
    IF-THEN-ELSE
    Table

Quick Check: When a variable can have only one value over the duration of a simulation it is modeled as a(an)...
    constant
    IF-THEN-ELSE
    Table

Quick Check: When a variable can have more than two values over the duration of a simulation it is modeled as a(an)...
    constant
    IF-THEN-ELSE
    Table

This is the end of the lab exercise.