1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
mode(7)
// SCILAB OBJECTS
// 1. SCALARS
// Copyright INRIA
a=1 //real constant
1==1 //boolean
'string' //character string
z=poly(0,'z') // polynomial with variable 'z' and with one root at zero
p=1+3*z+4.5*z^2 //polynomial
r=z/p //rational
// 2. MATRICES
A=[a+1 2 3
0 0 atan(1)
5 9 -1] //3 x 3 constant matrix
b=[%t,%f] //1 x 2 boolean matrix
Mc=['this','is';
'a' ,'matrix'] //2 x 2 matrix of strings
Mp=[p,1-z;
1,z*p] //2 x 2 polynomial matrix
F=Mp/poly([1+%i 1-%i 1],'z') //rational matrix
Sp=sparse([1,2;4,5;3,10],[1,2,3]) //sparse matrix
Sp(1,10)==Sp(1,1) //boolean sparse matrix
// 3. LISTS
L=list(a,-(1:5), Mp,['this','is';'a','list']) //list
L(2)(3) //sub-entry in list
Lt=tlist(['mylist','color','position','weight'],'blue',[0,1],10) //typed-list
Lt('color') //extracting
Lt('weight') //extracting
A=diag([2,3,4]);B=[1 0;0 1;0 0];C=[1 -1 0];D=0*C*B;x0=[0;0;0];
Sl=syslin('c',A,B,C,D,x0) //Standard state-space linear system
Sl("A"), Sl("C") //Retrieving elements of a typed list
Slt=ss2tf(Sl) // Transfer matrix
Slt('num'), Slt('den')
// OPERATIONS
v=1:5;W=v'*v //constant matrix multiplication
W(1,:) //extracting first row
W(:,$) //extracting last column
Mp'*Mp+eye //polynomial matrix
Mp1=Mp(1,1)+4.5*%i //complex
Fi=C*(z*eye-A)^(-1)*B; //transfer function evaluation
F(:,1)*Fi //operations with rationals
M=[Mp -Mp; Mp' Mp+eye] //concatenation of polynomial matrices
[Fi, Fi(:,1)] // ... or rationals
F=syslin('c',F);
Num=F('num');Den=F('den'); //operation on transfer matrix
// SOME NUMERICAL PRIMITIVES
inv(A) //Inverse
inv(Mp) //Inverse
inv(Sl*Sl') //Product of two linear systems and inverse
w=ss2tf(ans) //Transfer function representation
w1=inv(ss2tf(Sl)*ss2tf(Sl')) //Product of two transfer functions and inverse
clean(w-w1)
A=rand(3,3);;B=rand(3,1);n=contr(A,B) //Controllability
K=ppol(A,B,[-1-%i -1+%i -1]) //Pole placement
poly(A-B*K,'z')-poly([-1-%i -1+%i -1],'z') //Check...
s=sin(0:0.1:5*%pi);
ss=fft(s(1:128),-1); //FFT
xbasc();
plot2d3("enn",1,abs(ss)'); //simple plot
// ON LINE DEFINITION OF FUNCTION
deff('[x]=fact(n)','if n==0 then x=1,else x=n*fact(n-1),end')
10+fact(5)
// OPTIMIZATION
deff('[f,g,ind]=rosenbro(x,ind)', 'a=x(2)-x(1)^2 , b=1-x(2) ,...
f=100.*a^2 + b^2 , g(1)=-400.*x(1)*a , g(2)=200.*a -2.*b ');
[f,x,g]=optim(rosenbro,[2;2],'qn')
// SIMULATION
a=rand(3,3)
e=expm(a)
deff('[ydot]=f(t,y)','ydot=a*y');
e(:,1)-ode([1;0;0],0,1,f)
// SYSTEM DEFINITION
s=poly(0,'s');
h=[1/s,1/(s+1);1/s/(s+1),1/(s+2)/(s+2)]
w=tf2ss(h);
ss2tf(w)
h1=clean(ans)
// EXAMPLE: SECOND ORDER SYSTEM ANALYSIS
sl=syslin('c',1/(s*s+0.2*s+1))
instants=0:0.05:20;
// step response:
y=csim('step',instants,sl);
xbasc();plot2d(instants',y')
// Delayed step response
deff('[in]=u(t)','if t<3 then in=0;else in=1;end');
y1=csim(u,instants,sl);plot2d(instants',y1');
// Impulse response;
yi=csim('imp',instants,sl);xbasc();plot2d(instants',yi');
yi1=csim('step',instants,s*sl);plot2d(instants',yi1');
// Discretization
dt=0.05;
sld=dscr(tf2ss(sl),0.05);
// Step response
u=ones(instants);
yyy=flts(u,sld);
xbasc();plot(instants,yyy)
// Impulse response
u=0*ones(instants);u(1)=1/dt;
yy=flts(u,sld);
xbasc();plot(instants,yy)
// system interconnexion
w1=[w,w];
clean(ss2tf(w1))
w2=[w;w];
clean(ss2tf(w2))
// change of variable
z=poly(0,'z');
horner(h,(1-z)/(1+z)) //bilinear transform
// PRIMITIVES
H=[1. 1. 1. 0.;
2. -1. 0. 1;
1. 0. 1. 1.;
0. 1. 2. -1];
ww=spec(H)
// STABLE SUBSPACES
[X,d]=schur(H,'cont');
X'*H*X
[X,d]=schur(H,'disc');
X'*H*X
//Selection of user-defined eigenvalues (# 3 and 4 here);
deff('[flg]=sel(x)',...
'flg=0,ev=x(2)/x(3),...
if abs(ev-ww(3))<0.0001|abs(ev-ww(4))<0.00001 then flg=1,end')
[X,d]=schur(H,sel)
X'*H*X
// With matrix pencil
[X,d]=gschur(H,eye(H),sel)
X'*H*X
// block diagonalization
[ab,x,bs]=bdiag(H);
inv(x)*H*x
// Matrix pencils
E=rand(3,2)*rand(2,3);
A=rand(3,2)*rand(2,3);
s=poly(0,'s');
w=det(s*E-A) //determinant
[al,be]=gspec(A,E);
al./(be+%eps*ones(be))
roots(w)
[Ns,d]=coffg(s*E-A); //inverse of polynomial matrix;
clean(Ns/d*(s*E-A))
[Q,M,i1]=pencan(E,A); // Canonical form;
clean(M*E*Q)
clean(M*A*Q)
// PAUSE-RESUME
write(%io(2),'pause command...');
write(%io(2),'TO CONTINUE...');
write(%io(2),'ENTER ''resume (or return) or click on resume!!''');
//pause;
// CALLING EXTERNAL ROUTINE
foo=['void foo(a,b,c)';
'double *a,*b,*c;';
'{ *c = *a + *b; }' ];
path=getcwd(); chdir(TMPDIR);
if getenv('WIN32','NO')=='OK' & getenv('COMPILER','NO')=='VC++' then
unix_s('del foo.c')
write('foo.c',foo);
WSCI=getenv('WSCI');
cmd='nmake /f ""'+WSCI+'\demos\intro\MakeF.mak"" TARGET=foo SCIDIR1=""'+WSCI+'""';
unix_s(cmd) //Compiling...(needs a compiler);
link('foo.dll','foo'); //Linking to Scilab
unix_s('del foo.c')
else
unix_s('rm -f foo.c');
write('foo.c',foo);
unix_s('make foo.o') //Compiling...(needs a compiler)
link('foo.o','foo','C') //Linking to Scilab
unix_s('rm -f foo.c foo.o');
end
chdir(path);
//5+7 by C function
call('foo',5,1,'d',7,2,'d','out',[1,1],3,'d')
|