﻿ 20-sim webhelp > Language Reference > Introduction > Equation Sections

# Equation Sections

In 20-sim equations can be divided in four sections: initialequations, equations, code and finalequations.

## Initialequations

The initialequations section should contain all equations that should calculated before the simulation run. The equations are calculated once. The equations in this section form a code block, i.e. the equations themselves and the order of the equations are not rewritten during compilation and executed sequentially. The results of the initialequations section are not shown in the simulator plots and can therefore not be inspected in the Numerical Values window!

### Example

The initialequations section is used in the library model:

parameters

real k = 10.0 {N/m};

real m = 1.0 {kg};

real b = 0.05 {};

variables

real x {m};

real d  {damping,N.s/m};

initialequations

d = 2*b*sqrt(k*m);

equations

x = int (p.v);

p.F = k * x + d*p.v;

As you can see in the initialequations section the damping d is calculated out of the relative damping b, mass m and stiffness k.

## Equations

The equations section contains all standard equations. They do not have to be entered in a sequential form. They are rewritten during compilation to get the correct order of execution. The resulting code is calculated every simulation step. You can see the use of the equations section in the example above.

## Code

The code section contains a code block. A code block contains all equations that must be calculated sequentially, i.e. the equations themselves and the order of the equations are not rewritten during compilation. The resulting sequential code is calculated every simulation step.

Example

The code section is used in the library model:

parameters

real initial = 0.0;

variables

real prev, peak;

initialequations

peak = initial;

prev = 0;

output = initial;

code

if major then

peak = max([abs(input), peak]);

if (input > 0 and prev < 0) or (input < 0 and prev > 0) then

output = peak;

peak = 0;

end;

prev = input;

end;

## Finalequations

The finalequations section should contain all equations that should be calculated after a simulation run. The equations are calculated once. The equations in this section form a code block, i.e. the equations themselves and the order of the equations are not rewritten during compilation and executed sequentially. The results of the finalequations section are not shown in the simulator plots and can therefore not be inspected in the Numerical Values window!

### Example

The finalequations section is used in the library model:

parameters

string command = '';

finalequations

// send the command to the workspace

doMatlab (command);

As you see you can give the parameter command any value, which is then exported to Matlab at the end of the simulation run.