home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / r / rlab / CTB / initial < prev    next >
Encoding:
Text File  |  1995-11-15  |  2.3 KB  |  99 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. // initial
  4. //
  5. // Syntax: G=initial(A,B,C,D,X0,T)
  6. //
  7. // This routine plots the time response of the linear system descried by,
  8. //              .
  9. //              x = Ax + Bu
  10. //              y = Cx + Du
  11. //
  12. // to an initial condition on the states. The routine may be called in two ways:
  13. //
  14. // (1)  G=initial(A,B,C,D)
  15. //      (This produces the time history with random initial conditions
  16. //       and with an arbitary time T=0.0:20.0:0.1)
  17. //
  18. // (2)  G=initial(A,B,C,D,X0)
  19. //      (This produces the time history with the given initial conditions
  20. //       and with an arbitrary time T=0.0:20.0:0.1)
  21. //
  22. // (3)  G=initial(A,B,C,D,X0,T)
  23. //      (This produces the time history with the given initial conditions
  24. //       with the given time in T).
  25. //
  26. // For the third case, the times in the time vector must be regularly
  27. // spaced.
  28. //
  29. // Note: Two matrices are returned in a list.
  30. //
  31. //       G.x = X values in the plot.
  32. //       G.y = Y values in the plot.
  33. //
  34. // Copyright(C), by Jeffrey B. Layton, 1994
  35. // Version JBL 940912
  36. //---------------------------------------------------------------------------
  37.  
  38. rfile abcdchk
  39. rfile c2d
  40. rfile ltitr
  41. rfile lsim
  42.  
  43. initial = function(a,b,c,d,x0,t)
  44. {
  45.    local(nargs,msg,estr,X0,dt,Dum,P,G,x,y,n)
  46.  
  47. // Count number of input arguments
  48.    nargs=0;
  49.    if (exist(a)) {nargs=nargs+1;}
  50.    if (exist(b)) {nargs=nargs+1;}
  51.    if (exist(c)) {nargs=nargs+1;}
  52.    if (exist(d)) {nargs=nargs+1;}
  53.    if (exist(x0)) {nargs=nargs+1;}
  54.    if (exist(t)) {nargs=nargs+1;}
  55.  
  56. // Check number of inputs
  57.    if (nargs < 4) {
  58.        error("INITIAL: Error in number of input arguments");
  59.    }
  60.  
  61. // Check if A,B,C, and D are compatibile
  62.    msg="";
  63.    msg=abcdchk(a,b,c,d);
  64.    if (msg != "") {
  65.        estr="lsim: "+msg;
  66.        error(estr);
  67.    }
  68.  
  69. // No X0 specified, try random initial conditions
  70.    if (nargs == 4) {
  71.        X0=rand(a.nr,1);
  72.    else
  73.        X0=x0[:];
  74.    }
  75.  
  76. // If no T is specified, then create default T
  77.    if (nargs == 5) {
  78.        T=[0.0:20.0:0.1];
  79.        T=T.';
  80.    }
  81.  
  82. // Check Time vector (if input)
  83.    if (nargs == 6) {
  84.        if (t.nr == 1) {
  85.            T=t.';
  86.        else
  87.            T=t;
  88.        }
  89.    }
  90.  
  91. // Perform the simulation
  92.    n=length(T);
  93.    Dum=lsim(a,b,c,d,zeros(n,b.nc),T,X0[:]);
  94.    x=Dum.x;
  95.    y=Dum.y;
  96.  
  97.    return << x=x; y=y >>
  98. };
  99.