3228
|
1 # Copyright (C) 1996,1998 A. Scottedward Hodel |
3213
|
2 # |
|
3 # This file is part of Octave. |
|
4 # |
|
5 # Octave is free software; you can redistribute it and/or modify it |
|
6 # under the terms of the GNU General Public License as published by the |
|
7 # Free Software Foundation; either version 2, or (at your option) any |
|
8 # later version. |
|
9 # |
|
10 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 # for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with Octave; see the file COPYING. If not, write to the Free |
|
17 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
18 |
|
19 function [y, t] = stepimp(sitype, sys, inp, tstop, n) |
|
20 # step: Impulse or step response for a linear system. |
|
21 # The system can be discrete or multivariable (or both). |
|
22 # This m-file contains the "common code" of step and impulse. |
|
23 # |
|
24 # [y, t] = stepimp(sitype, sys[, inp, tstop, n]) |
|
25 # Produces a plot or the response data for system sys. |
|
26 # |
|
27 # Limited argument checking; "do not attempt to do this at home". |
|
28 # Use step or impulse instead. |
|
29 # |
|
30 # See also: step, impulse |
|
31 |
|
32 # Written by Kai P. Mueller October 2, 1997 |
|
33 # based on lsim.m of Scottedward Hodel |
3228
|
34 # $Revision: 2.0.0.0 $ |
3213
|
35 |
3228
|
36 if (sitype == 1) IMPULSE = 0; |
|
37 elseif (sitype == 2) IMPULSE = 1; |
|
38 else error("stepimp: illegal sitype argument.") |
3213
|
39 endif |
|
40 sys = sysupdate(sys,"ss"); |
|
41 |
|
42 USE_DEF = 0; # default tstop and n if we have to give up |
|
43 N_MIN = 50; # minimum number of points |
|
44 N_MAX = 2000; # maximum number of points |
|
45 T_DEF = 10.0; # default simulation time |
3228
|
46 |
3213
|
47 # collect useful information about the system |
3228
|
48 [ncstates,ndstates,NIN,NOUT] = sysdimensions(sys); |
|
49 TSAMPLE = sysgettsam(sys); |
|
50 |
|
51 if (nargin < 3) inp = 1; |
|
52 elseif (inp < 1 | inp > NIN) error("Argument inp out of range") |
3213
|
53 endif |
3228
|
54 |
3213
|
55 DIGITAL = is_digital(sys); |
|
56 if (DIGITAL) |
3228
|
57 NSTATES = ndstates; |
3213
|
58 if (TSAMPLE < eps) |
|
59 error("stepimp: sampling time of discrete system too small.") |
|
60 endif |
3228
|
61 else NSTATES = ncstates; endif |
3213
|
62 if (NSTATES < 1) |
3228
|
63 error("step: pure gain block (n_states < 1), step response is trivial"); |
3213
|
64 endif |
|
65 if (nargin < 5) |
|
66 # we have to compute the time when the system reaches steady state |
|
67 # and the step size |
3228
|
68 ev = eig(sys2ss(sys)); |
3213
|
69 if (DIGITAL) |
|
70 # perform bilinear transformation on poles in z |
|
71 for i = 1:NSTATES |
|
72 pole = ev(i); |
|
73 if (abs(pole + 1) < 1.0e-10) |
|
74 ev(i) = 0; |
|
75 else |
|
76 ev(i) = 2 / TSAMPLE * (pole - 1) / (pole + 1); |
|
77 endif |
|
78 endfor |
|
79 endif |
|
80 # remove poles near zero from eigenvalue array ev |
|
81 nk = NSTATES; |
|
82 for i = 1:NSTATES |
|
83 if (abs(ev(i)) < 1.0e-10) |
|
84 ev(i) = 0; |
|
85 nk = nk - 1; |
|
86 endif |
|
87 endfor |
|
88 if (nk == 0) |
|
89 USE_DEF = 1; |
|
90 #printf("##STEPIMP-DEBUG: using defaults.\n"); |
|
91 else |
|
92 ev = ev(find(ev)); |
|
93 x = max(abs(ev)); |
|
94 t_step = 0.2 * pi / x; |
|
95 x = min(abs(real(ev))); |
|
96 t_sim = 5.0 / x; |
|
97 # round up |
|
98 yy = 10^(ceil(log10(t_sim)) - 1); |
|
99 t_sim = yy * ceil(t_sim / yy); |
|
100 #printf("##STEPIMP-DEBUG: nk=%d t_step=%f t_sim=%f\n", |
|
101 # nk, t_step, t_sim); |
|
102 endif |
|
103 endif |
|
104 |
|
105 if (DIGITAL) |
|
106 # ---- sampled system |
|
107 if (nargin == 5) |
|
108 n = round(n); |
|
109 if (n < 2) |
|
110 error("stepimp: n must not be less than 2.") |
|
111 endif |
|
112 else |
|
113 if (nargin == 4) |
|
114 # n is unknown |
|
115 elseif (nargin >= 1) |
|
116 # tstop and n are unknown |
|
117 if (USE_DEF) |
|
118 tstop = (N_MIN - 1) * TSAMPLE; |
|
119 else |
|
120 tstop = t_sim; |
|
121 endif |
|
122 endif |
|
123 n = floor(tstop / TSAMPLE) + 1; |
|
124 if (n < 2) n = 2; endif |
|
125 if (n > N_MAX) |
|
126 n = N_MAX; |
|
127 printf("Hint: number of samples limited to %d by default.\n", \ |
|
128 N_MAX); |
|
129 printf(" ==> increase \"n\" parameter for longer simulations.\n"); |
|
130 endif |
|
131 endif |
|
132 tstop = (n - 1) * TSAMPLE; |
|
133 t_step = TSAMPLE; |
|
134 else |
|
135 # ---- continuous system |
|
136 if (nargin == 5) |
|
137 n = round(n); |
|
138 if (n < 2) |
|
139 error("step: n must not be less than 2.") |
|
140 endif |
|
141 t_step = tstop / (n - 1); |
|
142 else |
|
143 if (nargin == 4) |
|
144 # only n in unknown |
|
145 if (USE_DEF) |
|
146 n = N_MIN; |
|
147 t_step = tstop / (n - 1); |
|
148 else |
|
149 n = floor(tstop / t_step) + 1; |
|
150 endif |
|
151 else |
|
152 # tstop and n are unknown |
|
153 if (USE_DEF) |
|
154 tstop = T_DEF; |
|
155 n = N_MIN; |
|
156 t_step = tstop / (n - 1); |
|
157 else |
|
158 tstop = t_sim; |
|
159 n = floor(tstop / t_step) + 1; |
|
160 endif |
|
161 endif |
|
162 if (n < N_MIN) |
|
163 n = N_MIN; |
|
164 t_step = tstop / (n - 1); |
|
165 endif |
|
166 if (n > N_MAX) |
|
167 tstop = (n - 1) * t_step; |
|
168 t_step = tstop / (N_MAX - 1); |
|
169 n = N_MAX; |
|
170 endif |
|
171 endif |
|
172 tstop = (n - 1) * t_step; |
3228
|
173 [jnk,B] = sys2ss(sys); |
|
174 B = B(:,inp); |
3213
|
175 sys = c2d(sys, t_step); |
|
176 endif |
|
177 #printf("##STEPIMP-DEBUG: t_step=%f n=%d tstop=%f\n", t_step, n, tstop); |
|
178 |
|
179 F = sys.a; |
|
180 G = sys.b(:,inp); |
|
181 C = sys.c; |
|
182 D = sys.d(:,inp); |
|
183 y = zeros(NOUT, n); |
|
184 t = linspace(0, tstop, n); |
|
185 |
|
186 if (IMPULSE) |
|
187 if (!DIGITAL && (D'*D > 0)) |
|
188 error("impulse: D matrix is nonzero, impulse response infinite.") |
|
189 endif |
|
190 if (DIGITAL) |
|
191 y(:,1) = D; |
|
192 x = G / t_step; |
|
193 else |
|
194 x = B; |
|
195 y(:,1) = C * x; |
|
196 x = F * x; |
|
197 endif |
|
198 for i = 2:n |
|
199 y(:,i) = C * x; |
|
200 x = F * x; |
|
201 endfor |
|
202 else |
|
203 x = zeros(NSTATES, 1); |
|
204 for i = 1:n |
|
205 y(:,i) = C * x + D; |
|
206 x = F * x + G; |
|
207 endfor |
|
208 endif |
|
209 |
|
210 if(nargout == 0) |
|
211 # Plot the information |
|
212 oneplot(); |
|
213 gset nogrid |
|
214 gset nologscale |
|
215 gset autoscale |
|
216 gset nokey |
|
217 clearplot(); |
|
218 if (gnuplot_has_multiplot) |
|
219 if (IMPULSE) |
|
220 gm = zeros(NOUT, 1); |
|
221 tt = "impulse"; |
|
222 else |
|
223 ssys = ss2sys(F, G, C, D, t_step); |
|
224 gm = dcgain(ssys); |
|
225 tt = "step"; |
|
226 endif |
|
227 ncols = floor(sqrt(NOUT)); |
|
228 nrows = ceil(NOUT / ncols); |
|
229 for i = 1:NOUT |
|
230 subplot(nrows, ncols, i); |
3228
|
231 title(sprintf("%s: | %s -> %s", tt,sysgetsignals(sys,"in",inp,1), ... |
|
232 sysgetsignals(sys,"out",i,1))); |
3213
|
233 if (DIGITAL) |
|
234 [ts, ys] = stairs(t, y(i,:)); |
|
235 ts = ts(1:2*n-2)'; ys = ys(1:2*n-2)'; |
|
236 if (length(gm) > 0) |
|
237 yy = [ys; gm(i)*ones(size(ts))]; |
|
238 else |
|
239 yy = ys; |
|
240 endif |
|
241 grid("on"); |
|
242 xlabel("time [s]"); |
|
243 ylabel("y(t)"); |
|
244 plot(ts, yy); |
|
245 else |
|
246 if (length(gm) > 0) |
|
247 yy = [y(i,:); gm(i)*ones(size(t))]; |
|
248 else |
|
249 yy = y(i,:); |
|
250 endif |
|
251 grid("on"); |
|
252 xlabel("time [s]"); |
|
253 ylabel("y(t)"); |
|
254 plot(t, yy); |
|
255 endif |
|
256 endfor |
|
257 # leave gnuplot in multiplot mode is bad style |
|
258 oneplot(); |
|
259 else |
|
260 # plot everything in one diagram |
3228
|
261 title([tt, " response | ", sysgetsignals(sys,"in",inp,1), ... |
|
262 " -> all outputs"]); |
3213
|
263 if (DIGITAL) |
|
264 stairs(t, y(i,:)); |
|
265 else |
|
266 grid("on"); |
|
267 xlabel("time [s]"); |
|
268 ylabel("y(t)"); |
|
269 plot(t, y(i,:)); |
|
270 endif |
|
271 endif |
|
272 y=[]; |
|
273 t=[]; |
|
274 endif |
|
275 #printf("##STEPIMP-DEBUG: gratulations, successfull completion.\n"); |
|
276 endfunction |