In some situations, you may need to define circular logic in your model that is not necessarily dynamic. A simple simultaneous system of equations is an example of such a system. Such logic does not represent a feedback loop, in that there is no chain of “cause and effect” and no dynamics – in fact, the variables may not change with time at all. In GoldSim, these kinds of circular systems are referred to as recursive loops (and are conceptually different from feedback loops).
As pointed out above, a simultaneous system of equations is an example of such a system. Consider the following example:
B = 4 -A
A = (6 – B)/3
This is a circular system, because A is a function of B and B is a function of A. To compute A and B, it is necessary to solve this coupled system of equations. In this particular case, through simple substitution, you can convince yourself that the solution to these equations is A =1 and B=3.
If you encounter a system such as this in one of your models, you can handle it in one of two ways. First, you could solve the system of equations directly either prior to running the model (e.g., using substitution), or dynamically while running the model (e.g., using an External element or GoldSim’s matrix functions to solve the appropriate equations).
For simple systems such as the example above, this is obviously the correct approach. Note, however, that for many complex models (e.g., non-linear coupled equations), the solution could be very computationally intensive, requiring an iterative approach.
GoldSim therefore will allow you to specify such a circular system and solve the equations in an approximate manner. It does this by assuming, at every timestep, that the current value of one of the variables can be approximated by the value of that variable at the previous timestep. To solve the simple system outlined above, we would define the two equations in GoldSim as follows:
B = 4 -A
A = (6 – Previous_B)/3
In the second equation, Previous_B is the output of a Previous Value element whose input is B:
By doing so, the equations can be solved explicitly (A is solved first as a function of the previous value of B, and then B is solved as a function of A). In effect, GoldSim uses the timestepping algorithm to iterate to a solution, as illustrated in the table below:
Timestep |
B at previous timestep |
A |
B |
0 |
2 |
1.333 |
2.667 |
1 |
2.667 |
1.111 |
2.889 |
2 |
2.889 |
1.037 |
2.963 |
3 |
2.963 |
1.012 |
2.988 |
4 |
2.988 |
1.004 |
2.996 |
5 |
2.996 |
1.001 |
2.999 |
6 |
2.999 |
1.001 |
3.000 |
7 |
3.000 |
1.000 |
3.000 |
In this case, GoldSim iterated to a very good solution within 2 or 3 timesteps, and an almost exact solution in 7 timesteps. In order for this to work, however, it was necessary to specify the value of “B at the previous timestep” when time = 0. That is, it was necessary to define an Initial Value for B. For this example, an Initial Value of 2 was assumed.
This simple example model illustrating the use of Previous Value elements (PreviousValue.gsm) can be found in the General Examples folder in your GoldSim directory (accessed by selecting File | Open Example... from the main menu).
Note: A more effective way to solve this particular problem would be to embed the system of equations in a looping Container. In such a case, the iteration would not require any timestepping (the iteration would occur within a single timestep). In fact, looping Containers are one of the most important uses for Previous Value elements. This is illustrated in the file LoopingContainer.gsm, which can be found in the General Examples/Containers folder in your GoldSim directory (accessed by selecting File | Open Example... from the main menu). Alternatively, you could solve such a system of equations using a Script element. This is illustrated in the file Script.gsm, which can be found in the General Examples folder in your GoldSim directory.
When using Previous Value elements outside of looping Containers to represent recursive systems, you need to take special care to ensure that the length of the timestep you are using is small relative to the timescale over which the system is changing. If the system you are simulating is changing rapidly relative to the timestep, your computed result will be inaccurate. That is, if you are using the previous value of a variable as an approximation to the current value, and the previous value is in fact a bad approximation of the current value, then the computed result will be inaccurate (since it takes several timesteps to converge on the correct solution). Selecting a timestep for these kinds of simulations is discussed in further detail in Appendix F of the GoldSim User’s Guide.
It is worth reiterating that recursive loops are conceptually different from feedback loops. Feedback loops represent a closed chain of cause and effect. Note that the terms “feedback” and “cause and effect” intentionally imply that the relationship between the variables is dynamic and the system changes over time (although systems with feedback loops can also reach a dynamic equilibrium). GoldSim allows you to create looping systems like this without having to use Previous Value elements. In order to do so, however, the loop must meet one requirement: it must contain at least one state variable.
Recursive loops, on the other hand, do not represent a chain of cause and effect, and the system may not be dynamic at all. These loops do not contain state variables. Simulating systems like these in GoldSim requires approximating the solution by using Previous Value elements (which actually work by inserting an “artificial” state variable into the loop).
Learn more about: