1
function [y,x]=csim(u,dt,sl,x0,tol)
3
// [y [,x]]=csim(u,dt,sl,[x0])
4
// simulation of the controlled linear system sl.
5
// sl is assumed to be a continuous-time system.
6
// u is the control and x0 the initial state.
12
// list(ut,parameter1,....,parametern) such that
13
// inputs=ut(t,parameter1,....,parametern)
14
// - the character string 'impuls' for impulse response calculation
15
// (here sl is assumed SISO without direct feedthrough and x0=0)
16
// - the character string 'step' for step response calculation
17
// (here sl is assumed SISO without direct feedthrough and x0=0)
18
//dt is a vector of instants with dt(1) = initial time
30
// dsimul flts ltitr rtitr ode impl
35
if rhs<3 then error(39),end
37
if type(sl)<>16 then error(56,1),end
41
case 'r' then sl=tf2ss(sl)
44
if sl(7)<>'c' then warning('csim: time domain is assumed continuous'),end
47
if type(d)==2°ree(d)>0 then d=coeff(d,0);warning('D set to constant');end
50
imp=0;text='if t==0 then y=0, else y=1,end'
54
if mb<>1 then error(95,1);end;
55
if part(u,1)=='i' then
58
warning('direct feedthrough (d) <> 0;set to zero');
62
deff('[y]=u(t)',text);
67
if mbu<>mb | ntu<>size(dt,'*') then error('wrong size of u'), end
77
if rhs==3 then x0=sl(6),end
78
if imp==1 then x0=0*x0,end
79
nt=size(dt,'*');x=0*ones(ma,nt)
80
[a,v,bs]=bdiag(a,1);b=v\b;c=c*v;x0=v\x0;
84
deff('[y]=u(t)',['ind=find(dt<=t)','nn=ind($)',...
85
'if (t==dt(nn)|nn==nt) then y=ut(nn),...
86
else y=ut(nn)+(t-dt(nn))/(dt(nn+1)-dt(nn))*(ut(nn+1)-ut(nn)), end']);
87
deff('[ydot]=%sim2(%tt,%y)','ydot=ak*%y+bk*u(%tt)');
88
elseif type(u)<>15 then
89
deff('[ydot]=%sim2(%tt,%y)','ydot=ak*%y+bk*u(%tt)');
90
ut=ones(mb,nt);for k=1:nt, ut(:,k)=u(dt(k)),end
93
tx=' ';for l=2:size(u), tx=tx+',%'+string(l-1);end;
94
deff('[ydot]=sk(%tt,%y,u'+tx+')','ydot=ak*%y+bk*u(%tt'+tx+')');
97
['['+part(tx,3:length(tx))+']=%sim2(3:'+string(size(%sim2))+')';
98
'ut=ones(mb,nt);for k=1:nt, ut(:,k)=u(t(k)'+tx+'),end'])
107
nrmu=maxi([norm(bk*ut,1),norm(x0(kk))])
110
atol=1.d-10*nrmu;rtol=atol/100
112
atol=tol(1);rtol=tol(2)
114
x(kk,:)=ode('adams',x0(kk),dt(1),dt,rtol,atol,%sim2)
115
if imp==1 then x(kk,:)=ak*x(kk,:)+bk*ut,end
119
if imp==0 then y=c*x+d*ut,else y=c*x,end
120
if lhs==2 then x=v*x,end