﻿ MODELS > Examples > Models Expressions

Models Expressions and Sums

1. Expressions

This example illustrates the use of expressions in MODELS

MODEL expr

CONST  c4 {val: 4}   -- constant with value of 4

VAR    a[1..6]       -- variables a, a, ... a

k             -- variable k

EXEC

IF t=timestep THEN       -- letting contents execute only at the first time step

write("*** Begin results of model 'expr' ***")

k:=1               write("       1 : ", k)

k:=2*k             write("       2 : ", k)

k:=3-k             write("       1 : ", k)

k:=3+k             write("       4 : ", k)

k:=3/k             write("    0.75 : ", k)

-- above, notice that all values are interpreted as real,

-- even if they are expressed as integers

k:=c4>3 AND c4=4   write("       1 : ", k)

-- above, c4>3 is true, c4=4 is true, result is true

-- notice that all logical values are expressed numerically (0 or 1)

k:=-10 AND c4=4   write("       0 : ", k)

-- above, -10 has logical value false because it is <=0

-- notice cross-mapping of logical and numerical values

-- -10 maps as false, c4=4 is true, false result maps as 0

k:= -10 OR c4     write("       1 : ", k)

-- above, -10 maps as false, c4 maps as true, true result maps as 1

k:= k OR 0         write("       1 : ", k)

-- above, k maps as true, 0 maps as false, true results maps as 1

a:=99           write("      99 : ", a)

a[1..2]:=-a     write("-99, -99 : ", a, ', ',a)

-- above, multiple assignment of single value (see example arval.dat)

a:=NOT a     write("       0 : ", a)

-- above, a maps as true, false result maps as 0

k:=NOT a       write("       1 : ", k)

-- above, a is -99 and maps as false, true result maps as 1

k:=NOT k           write("       0 : ", k)

-- above, k is 1 and maps as true, false result maps as 0

write("*** End results of model 'expr' ***")

ENDIF

ENDEXEC

ENDMODEL

2. Sums

This example illustrates sum() in MODELS

MODEL sum_example

CONST  c4 {val: 4}

VAR    a, b, k1, k2

INIT

b:=2

ENDINIT

EXEC

a:=t

k1:=sum(c4| -t|a +t|b)                             -- = c4 -t*a +t*b

k2:=sum(c4| -t|a +t|b) {min:-10.999 max:4.999}     -- same, with limits

ENDEXEC

ENDMODEL