// P11SimRegN.sce
// Simulation of a two-dimensional first order regression model
// -----------------------------------------------
[u,t,n]=file();                         // find working directory
chdir(dirname(n(2)));                   // set working directory
clear("u","t","n")                      // clear auxiliary data
exec("ScIntro.sce",-1),mode(0)          // intro to sesion

nd=100;                            // length of data
I_typU=2;                          // type of control (see below)

// model for simulation
Sim.Cy.ord=1;                      // model order  
Sim.Cy.thb0=[1 .2; -.5 1];         // parameters at ut
Sim.Cy.tha1=[.4 .2; .1 .5];        // parameters at yt
Sim.Cy.thb1=[.1 -2; -.2 .1];
Sim.Cy.thk=[0; 0];                 // constant (model absolute term)
Sim.Cy.th=[Sim.Cy.thb0 Sim.Cy.tha1 Sim.Cy.thb1 Sim.Cy.thk];
Sim.Cy.cv=.1;                      // noise variance

[my mu]=size(Sim.Cy.thb0);
yt=zeros(my,nd);
yt(1)=1; yt(2)=3;                  // initial conditions for output

// definition of the control variable
select I_typU
case 1, ut=rand(mu,nd);          // random
case 2 
  z=1; 
  for i=1:mu 
    ut(i,1:nd)=z*ones(1,nd); 
    z=-z; 
  end
end

// TIME LOOP OF THE SIMULATION
ord=Sim.Cy.ord;                    // model order
sd=sqrt(Sim.Cy.cv);
th=Sim.Cy.th;                      // regression coefficients
for t=3:nd
  ps=genps(ord,t,yt,ut);           // regression vector
  yt(:,t)=th*ps+sd*eye(my)*rand(my,1); // simulation
end
Sim.Cy.yt=yt;
Sim.Cy.ut=ut;

save _data/dataT11N.dat Sim         // save data

// RESULTS OF THE SIMULATION
set(scf(1),'position',[600 100 600 600])
subplot(211),plot(ut','.'),title('Input')
subplot(212),plot(yt'),title('Output')
