This model does not work:

What we are expecting is : y=10 for t=0 to 5secCode: Select all

`variables real y; integer i; equations y=0; for i = 1 to 10 do y=y+1; end;`

## how to create a simple counter model

**Moderators:** Frank, paul, Hiwot, Chris, peter

### how to create a simple counter model

member 20-sim development team

### Re: how to create a simple counter model

The problem with the models touches two issues.

The problem lies in the difference between a set of equations and a piece of procedural code. The set of equations is sorted and reordered by 20-sim as needed, a piece of procedural code is not sorted and reordered; it is executed like a program in the way the user specified it. Like x = x + 1 would typically be interesting as code, not as equation.

20-sim supports both modeling styles, and you can even mix them in the models, by using the

More info on this is in the online help:

http://www.20sim.com/webhelp/editor/com ... cution.htm

http://www.20sim.com/webhelp/language_r ... ctions.htm

The models that you've sent me is often changed such that y is used as a memory variable (or your own state variable if you like) and that its value is passed from one simulation step to the next. The problem is that 20-sim does not know that the value of variable y is re-used between steps. This means that the choice of integration method influences its value: y is incremented on all model computations of the integration method, thus also on intermediate and rejected model computations, not just on the integration steps that result in simulation output. Try the following model with different integration methods to see the effect:

20-sim supports a way of entering your own memory variables, by setting an initial value in the

More info on this is in the online help:

http://www.20sim.com/webhelp/language_r ... iables.htm

**1. How to use procedural code**The problem lies in the difference between a set of equations and a piece of procedural code. The set of equations is sorted and reordered by 20-sim as needed, a piece of procedural code is not sorted and reordered; it is executed like a program in the way the user specified it. Like x = x + 1 would typically be interesting as code, not as equation.

20-sim supports both modeling styles, and you can even mix them in the models, by using the

**equations**and the**code**keyword. Your model would look like this, every model computation results in y == 10Code: Select all

```
variables
real y;
integer i;
code
y = 0;
for i = 1 to 10 do
y = y + 1;
end;
```

http://www.20sim.com/webhelp/editor/com ... cution.htm

http://www.20sim.com/webhelp/language_r ... ctions.htm

**2. How to create memory variables**The models that you've sent me is often changed such that y is used as a memory variable (or your own state variable if you like) and that its value is passed from one simulation step to the next. The problem is that 20-sim does not know that the value of variable y is re-used between steps. This means that the choice of integration method influences its value: y is incremented on all model computations of the integration method, thus also on intermediate and rejected model computations, not just on the integration steps that result in simulation output. Try the following model with different integration methods to see the effect:

Code: Select all

```
variables
real y;
initialequations
y = 0;
code
y = y + 1;
```

**initialequations**section, and using the**major**keyword to prevent intermediate computations to update the variable. The previous model with a proper memory variable y would look like this:Code: Select all

```
variables
real y;
initialequations
y = 0;
code
if major then
y = y + 1;
end;
```

http://www.20sim.com/webhelp/language_r ... iables.htm

member 20-sim development team