comparison scripts/control/base/lsim.m @ 7126:4a375de63f66

[project @ 2007-11-08 03:44:14 by jwe]
author jwe
date Thu, 08 Nov 2007 03:44:15 +0000
parents a1dbe9d80eee
children 38fe664f0ef1
comparison
equal deleted inserted replaced
7125:f084ba47812b 7126:4a375de63f66
38 ## Created: July 1995 38 ## Created: July 1995
39 ## modified by John Ingram for system format August 1996 39 ## modified by John Ingram for system format August 1996
40 40
41 function [y, x] = lsim (sys, u, t, x0) 41 function [y, x] = lsim (sys, u, t, x0)
42 42
43 if((nargin < 3)||(nargin > 4)) 43 if (nargin < 3 || nargin > 4)
44 print_usage (); 44 print_usage ();
45 endif 45 endif
46 46
47 if(!isstruct(sys)) 47 if (! isstruct (sys))
48 error("sys must be in system data structure"); 48 error ("sys must be in system data structure");
49 endif 49 endif
50 50
51 sys = sysupdate(sys,"ss"); 51 sys = sysupdate (sys,"ss");
52 52
53 [ncstates, ndstates, nin, nout] = sysdimensions(sys); 53 [ncstates, ndstates, nin, nout] = sysdimensions (sys);
54 [a,b,c,d] = sys2ss(sys); 54 [a, b, c, d] = sys2ss (sys);
55 55
56 if (nargin == 3) x0 = zeros(columns(a),1); endif 56 if (nargin == 3)
57 x0 = zeros (columns (a), 1);
58 endif
57 59
58 if(rows(u) ~= length(t)) 60 if (rows (u) != length (t))
59 error("lsim: There should be an input value (row) for each time instant"); 61 error ("lsim: There should be an input value (row) for each time instant");
60 endif 62 endif
61 if(columns(u) ~= columns(d)) 63 if (columns (u) != columns (d))
62 error("lsim: U and d should have the same number of inputs"); 64 error ("lsim: U and d should have the same number of inputs");
63 endif 65 endif
64 if(columns(x0) > 1) 66 if (columns (x0) > 1)
65 error("lsim: Initial condition vector should have only one column"); 67 error ("lsim: Initial condition vector should have only one column");
66 endif 68 endif
67 if(rows(x0) > rows(a)) 69 if (rows (x0) > rows p(a))
68 error("lsim: Initial condition vector is too large"); 70 error ("lsim: Initial condition vector is too large");
69 endif 71 endif
70 72
71 Ts = 0; 73 Ts = 0;
72 t(2)-t(1); 74 t(2)-t(1);
73 u=u'; 75 u=u';
74 n = max(size(t)); 76 n = max (size (t));
75 77
76 for ii = 1:(n-1) 78 for ii = 1:(n-1)
77 79
78 ## check if step size changed 80 ## check if step size changed
79 ## FIXME -- this is probably not the best test, but it is 81 ## FIXME -- this is probably not the best test, but it is
80 ## better than a test for exact equality. 82 ## better than a test for exact equality.
81 if (abs (t(ii+1) - t(ii) - Ts) > 10 * eps) 83 if (abs (t(ii+1) - t(ii) - Ts) > 10 * eps)
82 Ts = t(ii+1) - t(ii); 84 Ts = t(ii+1) - t(ii);
83 ## [F,G] = c2d(a,b,Ts); 85 ## [F,G] = c2d(a,b,Ts);
84 dsys = c2d(sys, Ts); 86 dsys = c2d (sys, Ts);
85 [F,G] = sys2ss(dsys); 87 [F, G] = sys2ss (dsys);
86 endif 88 endif
87 89
88 x(:,ii) = x0; 90 x(:,ii) = x0;
89 x0 = F*x0 + G*u(:,ii); 91 x0 = F*x0 + G*u(:,ii);
90 endfor 92 endfor
91 93
92 ## pick up last point 94 ## pick up last point
93 x(:,n) = x0; 95 x(:,n) = x0;
94 96
95 y = c*x + d*u; 97 y = c*x + d*u;
96 if(nargout == 0) 98 if (nargout == 0)
97 plot(t,y); 99 plot (t, y);
98 y=[]; 100 y = [];
99 x=[]; 101 x = [];
100 endif 102 endif
103
101 endfunction 104 endfunction