home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / languages / rlab1_23a / CTB / dlsim < prev    next >
Text File  |  1995-11-14  |  3KB  |  122 lines

  1. //-----------------------------------------------------------------------
  2. //
  3. // dlsim
  4. //
  5. // Syntax: G=dlsim(A,B,C,D,U,X0)
  6. //
  7. // This routine plots the time response of the linear system descried by,
  8. //
  9. //     x[n+1] = Ax[n] + Bu[n]
  10. //     y[n]   = Cx[n] + Du[n]
  11. //
  12. // to the input sequence U. The routine may be called in several fashions:
  13. //
  14. // (1)  G=dlsim(A,B,C,D,U)
  15. //      (This produces the sequence with Zero initial conditions for
  16. //       a state-space model).
  17. //
  18. // (2)  G=dlsim(A,B,C,D,U,X0)
  19. //      (This produces the sequence with initial conditions for
  20. //       a state-space model).
  21. //
  22. // (3)   G=dlsim(NUM,DEN,U)
  23. //       (This produces the sequence with Zero initial conditions for
  24. //        a transfer function model, G(z)=NUM(z)/DEN(z) ).
  25. //
  26. // For all 3 cases, the matrix U must have as many columns as there
  27. // are inputs, U. Each row of U corresponds to a new time point, and
  28. // U must have length(T) rows. The input T is the time vector which
  29. // must be regularly spaced.
  30. //
  31. // Note: Two matrices are returned in a list.
  32. //
  33. //       G.x = X values in the plot.
  34. //       G.y = Y values in the plot.
  35. //
  36. // Note: G.x has as many columns as there are states and length(U) rows.
  37. //       G.y has as many columns as there are outputs and length(U) rows.
  38. //
  39. // Copyright(C), by Jeffrey B. Layton, 1994
  40. // Version JBL 940907
  41. //-----------------------------------------------------------------------
  42.  
  43. rfile tfchk
  44. rfile tf2ss
  45. rfile abcdchk
  46. rfile ltitr
  47. rfile stairs
  48.  
  49. dlsim = function(a,b,c,d,u,x0)
  50. {
  51.    local(nargs,Dum,num,den,estr,msg,A,B,C,D,x,y,t,X0)
  52.  
  53. // Count number of arguments
  54.    nargs=0;
  55.    if (exist(a)) {nargs=nargs+1;}
  56.    if (exist(b)) {nargs=nargs+1;}
  57.    if (exist(c)) {nargs=nargs+1;}
  58.    if (exist(d)) {nargs=nargs+1;}
  59.    if (exist(u)) {nargs=nargs+1;}
  60.    if (exist(x0)) {nargs=nargs+1;}
  61.  
  62. // If nargs < 3, error
  63.    if ( (nargs < 3) || (nargs == 4) ) {
  64.        error("DLSIM:  Incorrect number of arguments.");
  65.    }
  66.  
  67. // Check for T.F. case
  68.    if (nargs == 3) {
  69. // Check if T.F. is proper
  70.        Dum=tfchk(a,b);
  71.        num=Dum.numc;
  72.        den=Dum.denc;
  73.        u=c;
  74. // Convert T.F. to S.S.
  75.        Dum=tf2ss(num,den);
  76.        A=Dum.a;
  77.        B=Dum.b;
  78.        C=Dum.c;
  79.        D=Dum.d;
  80.    else
  81.        A=a;
  82.        B=b;
  83.        C=c;
  84.        D=d;
  85.        msg="";
  86.        msg=abcdchk(A,B,C,D);
  87.        if (msg != "") {
  88.            estr="DLSIM: "+msg;
  89.            error(estr);
  90.        }
  91.    }
  92.  
  93. // If X0 doesn't exist, then create a zero vector for it
  94.    if ( (nargs == 5) || (nargs == 3) ) {
  95.        X0 = zeros(1,A.nr);
  96.    else
  97.        X0=x0;
  98.    }
  99.  
  100. // Check Dimensions
  101.    if (u.nc != D.nc) {
  102.        error("DLSIM: U must have same number of cols. as inputs.");
  103.    }
  104.  
  105. // Perform the simulation by using ltitr
  106.    if (isempty(A) == 1) {
  107.        x=[];
  108.        y=u*D.';
  109.    else
  110.        x=ltitr(A,B,u,X0);
  111.        y=x*C.'+u*D.';
  112.    }
  113.  
  114. // Make the Stairs plot
  115.    t = [0:length(y)-1];
  116.    xlabel("No. of Samples");
  117.    ylabel("Amplitude");
  118.    Dum=stairs(t,y);
  119.  
  120.    return << x=x; y=y >>
  121. };
  122.