how to create a simple counter model

Moderators: Frank, paul, Hiwot, Chris, peter

paul
Posts: 152
Joined: Mon Sep 19, 2005 2:00 pm
Location: Controllab Products B.V.

how to create a simple counter model

Postby paul » Fri Feb 20, 2009 12:08 pm

This model does not work:

Code: Select all

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


What we are expecting is : y=10 for t=0 to 5sec
member 20-sim development team

paul
Posts: 152
Joined: Mon Sep 19, 2005 2:00 pm
Location: Controllab Products B.V.

Re: how to create a simple counter model

Postby paul » Fri Feb 20, 2009 12:10 pm

The problem with the models touches two issues.

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 == 10

Code: Select all

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


More info on this is in the online help:

http://www.20sim.com/webhelp/editor/compiling/orderofexecution.htm
http://www.20sim.com/webhelp/language_reference/equation_sections.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;


20-sim supports a way of entering your own memory variables, by setting an initial value in the 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;


More info on this is in the online help:
http://www.20sim.com/webhelp/language_reference/keywords/predefinedvariables.htm
member 20-sim development team


Return to “Languages”

Who is online

Users browsing this forum: No registered users and 1 guest