<< Click to Display Table of Contents >> Navigation: MODELS > Examples > Models Calculus |
1. Laplace
This example illustrate the usage of Laplace transforms in MODELS. The unknown variable y(t) is calculated by specifying the Laplace transformed ratio between y(s) and a known variable x(s). Only polynoms are allowed y(s)/x(s) = (n0+n1s+n2s^2+n3s^3+...)/(d0+d1s+n2s^2+d3s^3+...). The history of x(t) must be specified. The usage of the resident functions deriv() and integral() is also illustrated. Note that the integral function must be initialized in HISTORY.
MODEL lap1 -- illustrates the use of Laplace in MODELS
VAR y[1..5], x
HISTORY y[1..5] {dflt:[0,0,0,0,0]} -- required to set history of integrals and denominators of laplacian transfer functions
x {dflt:0}
integral(x) {dflt:0}
EXEC
x:=t>0 -- logical function. x=0;t<timestep and x=1;t>=timestep
Laplace(y[1]/x) := 1|s0 / 1|s1 -- y[1](s)/x(s) = 1/s
y[2] := integral(x) -- y[2](t)=y[1](t)
Laplace(y[3]/x) := 1|s1 / 1|s0 -- y[3](s)/x(s) = s
y[4] := deriv(x) -- different approximation for y[4] and y[3]
Laplace(y[5]/y[2]) := 1|s1 / 1|s0 -- y[5]=x
ENDEXEC
ENDMODEL
2. Differential equation
This example compares lapace transforms and differential equations. When using the keywords claplace and cdiffeq the coefficients in the polynoms are not recalculated in the execution loop but remain constant.
MODEL lapdif -- illustrates the use of Laplace and diffeq in MODELS
VAR a1, a2
b1 -- approximation to a pure delay of size=timestep, using Laplace
b2 -- same as b1, using constant coefficients, using claplace
b3 -- 1/(1+s) with step input =10, using Laplace
b4 -- same as b3, equivalent notation using diffeq
b5 -- same as b4, with constant coefficients, using cdiffeq
k
INIT
k:=10
a1:=k
histdef(a2):=t -- privately assign a history function to a2
-- also possible with definition HISTORY a2 {DFLT:t}
ENDINIT
EXEC
IF t=0 THEN -- initialize at t=0. Also possible under INIT
b1:=0; b2:=0; b3:=0; b4:=0; b5:=0
ELSE
a2:=t
-- b1(t)=a2(t-ts) => b1(s)/a2(s)=exp(-ts*s)=exp(-s*ts/2)/exp(s*ts/2) "="
-- (1-s*ts/2)/(1+s*ts/2) series expansion valid for small timesteps ts
Laplace(b1/a2) := (1|s0 -timestep/2|s1)/(1|s0 +timestep/2|s1)
-- b2(t)=a2(t-ts) with constant coefficients (1 and ts/2). claplace used with constant coefficients, faster
claplace(b2/a2) := (1|s0 -timestep/2|s1)/(1|s0 +timestep/2|s1)
-- b3(t)+2*d/dt*b3(t)=k => b3(s)/k(s)=1/(1+s)
-- when b3(t=0)=0 and d/dt is the time derivative operand
Laplace(b3/k) := 1|/(1|s0 +2|s1)
-- b4(t)+2*d/dt*b4(t) = a1
diffeq(1|d0+2|d1)|b4 := a1
-- b5(t)+2*d/dt*b5(t) = k with constant coeffisients (1 and 2)
cdiffeq(1|d0+2|d1)|b5 := k
ENDIF
ENDEXEC
ENDMODEL
3. Combine
This example illustates how to solve mutually coupled linear or differential equations. The keyword sum must be used to specify a linear equation polynom.
MODEL comb2 -- illustrates COMBINE groups in MODELS
VAR y1, y2, y3, x
HISTORY y1 {dflt: sin(t)}
y2 {dflt: cos(t)}
y3 {dflt: -sin(t)}
integral(y2) {dflt: sin(t)}
integral(y3) {dflt: cos(t)}
x {dflt: cos(t)}
EXEC
COMBINE AS first_group
y1:=integral(y2)
y2:=integral(y3)
y3:=sum(-1|y1)
ENDCOMBINE
IF t=stoptime THEN write(sin(t)':'y1' 'cos(t)':'y2) ENDIF
COMBINE AS second_group
y2:=deriv(y1)
y3:=deriv(y2)
y1:=sum(-1|y3)
ENDCOMBINE
IF t=stoptime THEN write(sin(t)':'y1' 'cos(t)':'y2) ENDIF
COMBINE AS third_group
Laplace(y1/y2) := 1|s0 / 1|s1
Laplace(y2/y3) := 1|s0 / 1|s1
y3:=sum(-1|y1)
ENDCOMBINE
IF t=stoptime THEN write(sin(t)':'y1' 'cos(t)':'y2) ENDIF
COMBINE AS fourth_group
Laplace(y2/y1) := 1|s1 / 1|s0
Laplace(y3/y2) := 1|s1 / 1|s0
y1:=sum(-1|y3)
ENDCOMBINE
IF t=stoptime THEN write(sin(t)':'y1' 'cos(t)':'y2) ENDIF
x:=cos(t)
Laplace(y1/x ) := 1|s0 / 1|s1
Laplace(y2/y1) :=-1|s0 / 1|s1
IF t=stoptime THEN write(sin(t)':'y1' 'cos(t)':'y2) ENDIF
ENDEXEC
ENDMODEL