3437
|
1 ## Copyright (C) 1996, 1998 Auburn University. All rights reserved. |
|
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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301 USA. |
3437
|
19 |
|
20 ## -*- texinfo -*- |
3500
|
21 ## @deftypefn {Function File} {[@var{y}, @var{t}] =} __stepimp__ (@var{sitype}, @var{sys} [, @var{inp}, @var{tstop}, @var{n}]) |
3437
|
22 ## Impulse or step response for a linear system. |
|
23 ## The system can be discrete or multivariable (or both). |
5016
|
24 ## This m-file contains the ``common code'' of step and impulse. |
3437
|
25 ## |
5016
|
26 ## Produces a plot or the response data for system @var{sys}. |
3437
|
27 ## |
5016
|
28 ## Limited argument checking; ``do not attempt to do this at home''. |
|
29 ## Used internally in @command{impulse}, @command{step}. Use @command{step} |
|
30 ## or @command{impulse} instead. |
3437
|
31 ## @end deftypefn |
5053
|
32 ## |
5016
|
33 ## @seealso{step, impulse} |
3437
|
34 |
|
35 ## Author: Kai P. Mueller <mueller@ifr.ing.tu-bs.de> |
|
36 ## Created: October 2, 1997 |
|
37 ## based on lsim.m of Scottedward Hodel |
|
38 |
3438
|
39 function [y, t] = __stepimp__ (sitype, sys, inp, tstop, n) |
3437
|
40 |
|
41 if (sitype == 1) IMPULSE = 0; |
|
42 elseif (sitype == 2) IMPULSE = 1; |
3438
|
43 else error("__stepimp__: invalid sitype argument.") |
3437
|
44 endif |
|
45 sys = sysupdate(sys,"ss"); |
|
46 |
|
47 USE_DEF = 0; # default tstop and n if we have to give up |
|
48 N_MIN = 50; # minimum number of points |
|
49 N_MAX = 2000; # maximum number of points |
|
50 T_DEF = 10.0; # default simulation time |
|
51 |
|
52 ## collect useful information about the system |
|
53 [ncstates,ndstates,NIN,NOUT] = sysdimensions(sys); |
|
54 TSAMPLE = sysgettsam(sys); |
|
55 |
|
56 if (nargin < 3) inp = 1; |
|
57 elseif (inp < 1 | inp > NIN) error("Argument inp out of range") |
|
58 endif |
|
59 |
|
60 DIGITAL = is_digital(sys); |
|
61 if (DIGITAL) |
|
62 NSTATES = ndstates; |
|
63 if (TSAMPLE < eps) |
3438
|
64 error("__stepimp__: sampling time of discrete system too small.") |
3437
|
65 endif |
|
66 else NSTATES = ncstates; endif |
|
67 if (NSTATES < 1) |
|
68 error("step: pure gain block (n_states < 1), step response is trivial"); |
|
69 endif |
|
70 if (nargin < 5) |
|
71 ## we have to compute the time when the system reaches steady state |
|
72 ## and the step size |
|
73 ev = eig(sys2ss(sys)); |
|
74 if (DIGITAL) |
|
75 ## perform bilinear transformation on poles in z |
|
76 for i = 1:NSTATES |
|
77 pole = ev(i); |
|
78 if (abs(pole + 1) < 1.0e-10) |
|
79 ev(i) = 0; |
|
80 else |
|
81 ev(i) = 2 / TSAMPLE * (pole - 1) / (pole + 1); |
|
82 endif |
|
83 endfor |
|
84 endif |
|
85 ## remove poles near zero from eigenvalue array ev |
|
86 nk = NSTATES; |
|
87 for i = 1:NSTATES |
3679
|
88 if (abs(real(ev(i))) < 1.0e-10) |
3437
|
89 ev(i) = 0; |
|
90 nk = nk - 1; |
|
91 endif |
|
92 endfor |
|
93 if (nk == 0) |
|
94 USE_DEF = 1; |
|
95 ## printf("##STEPIMP-DEBUG: using defaults.\n"); |
|
96 else |
|
97 ev = ev(find(ev)); |
|
98 x = max(abs(ev)); |
|
99 t_step = 0.2 * pi / x; |
|
100 x = min(abs(real(ev))); |
|
101 t_sim = 5.0 / x; |
|
102 ## round up |
|
103 yy = 10^(ceil(log10(t_sim)) - 1); |
|
104 t_sim = yy * ceil(t_sim / yy); |
|
105 ## printf("##STEPIMP-DEBUG: nk=%d t_step=%f t_sim=%f\n", |
|
106 ## nk, t_step, t_sim); |
|
107 endif |
|
108 endif |
|
109 |
|
110 if (DIGITAL) |
|
111 ## ---- sampled system |
|
112 if (nargin == 5) |
|
113 n = round(n); |
|
114 if (n < 2) |
3438
|
115 error("__stepimp__: n must not be less than 2.") |
3437
|
116 endif |
|
117 else |
|
118 if (nargin == 4) |
|
119 ## n is unknown |
|
120 elseif (nargin >= 1) |
|
121 ## tstop and n are unknown |
|
122 if (USE_DEF) |
|
123 tstop = (N_MIN - 1) * TSAMPLE; |
|
124 else |
|
125 tstop = t_sim; |
|
126 endif |
|
127 endif |
|
128 n = floor(tstop / TSAMPLE) + 1; |
|
129 if (n < 2) n = 2; endif |
|
130 if (n > N_MAX) |
|
131 n = N_MAX; |
|
132 printf("Hint: number of samples limited to %d by default.\n", \ |
|
133 N_MAX); |
|
134 printf(" ==> increase \"n\" parameter for longer simulations.\n"); |
|
135 endif |
|
136 endif |
|
137 tstop = (n - 1) * TSAMPLE; |
|
138 t_step = TSAMPLE; |
|
139 else |
|
140 ## ---- continuous system |
|
141 if (nargin == 5) |
|
142 n = round(n); |
|
143 if (n < 2) |
|
144 error("step: n must not be less than 2.") |
|
145 endif |
|
146 t_step = tstop / (n - 1); |
|
147 else |
|
148 if (nargin == 4) |
|
149 ## only n in unknown |
|
150 if (USE_DEF) |
|
151 n = N_MIN; |
|
152 t_step = tstop / (n - 1); |
|
153 else |
|
154 n = floor(tstop / t_step) + 1; |
|
155 endif |
|
156 else |
|
157 ## tstop and n are unknown |
|
158 if (USE_DEF) |
|
159 tstop = T_DEF; |
|
160 n = N_MIN; |
|
161 t_step = tstop / (n - 1); |
|
162 else |
|
163 tstop = t_sim; |
|
164 n = floor(tstop / t_step) + 1; |
|
165 endif |
|
166 endif |
|
167 if (n < N_MIN) |
|
168 n = N_MIN; |
|
169 t_step = tstop / (n - 1); |
|
170 endif |
|
171 if (n > N_MAX) |
|
172 tstop = (n - 1) * t_step; |
|
173 t_step = tstop / (N_MAX - 1); |
|
174 n = N_MAX; |
|
175 endif |
|
176 endif |
|
177 tstop = (n - 1) * t_step; |
|
178 [jnk,B] = sys2ss(sys); |
|
179 B = B(:,inp); |
|
180 sys = c2d(sys, t_step); |
|
181 endif |
|
182 ## printf("##STEPIMP-DEBUG: t_step=%f n=%d tstop=%f\n", t_step, n, tstop); |
|
183 |
|
184 F = sys.a; |
|
185 G = sys.b(:,inp); |
|
186 C = sys.c; |
|
187 D = sys.d(:,inp); |
|
188 y = zeros(NOUT, n); |
|
189 t = linspace(0, tstop, n); |
|
190 |
|
191 if (IMPULSE) |
|
192 if (!DIGITAL && (D'*D > 0)) |
|
193 error("impulse: D matrix is nonzero, impulse response infinite.") |
|
194 endif |
|
195 if (DIGITAL) |
|
196 y(:,1) = D / t_step; |
|
197 x = G / t_step; |
|
198 else |
|
199 x = B; |
|
200 y(:,1) = C * x; |
|
201 x = F * x; |
|
202 endif |
|
203 for i = 2:n |
|
204 y(:,i) = C * x; |
|
205 x = F * x; |
|
206 endfor |
4375
|
207 if (DIGITAL) |
|
208 y *= t_step; |
|
209 endif |
3437
|
210 else |
|
211 x = zeros(NSTATES, 1); |
|
212 for i = 1:n |
|
213 y(:,i) = C * x + D; |
|
214 x = F * x + G; |
|
215 endfor |
|
216 endif |
3679
|
217 |
|
218 save_automatic_replot = automatic_replot; |
|
219 unwind_protect |
|
220 automatic_replot = 0; |
|
221 if(nargout == 0) |
|
222 ## Plot the information |
|
223 oneplot(); |
5215
|
224 __gnuplot_set__ nogrid |
|
225 __gnuplot_set__ nologscale |
|
226 __gnuplot_set__ autoscale |
|
227 __gnuplot_set__ nokey |
4422
|
228 if (IMPULSE) |
|
229 gm = zeros(NOUT, 1); |
|
230 tt = "impulse"; |
|
231 else |
4771
|
232 ssys = ss(F, G, C, D, t_step); |
4422
|
233 gm = dcgain(ssys); |
|
234 tt = "step"; |
|
235 endif |
|
236 ncols = floor(sqrt(NOUT)); |
|
237 nrows = ceil(NOUT / ncols); |
4789
|
238 if (ncols > 1 || nrows > 1) |
|
239 clearplot(); |
|
240 endif |
4422
|
241 for i = 1:NOUT |
|
242 subplot(nrows, ncols, i); |
|
243 title(sprintf("%s: | %s -> %s", tt,sysgetsignals(sys,"in",inp,1), ... |
|
244 sysgetsignals(sys,"out",i,1))); |
|
245 if (DIGITAL) |
|
246 [ts, ys] = stairs(t, y(i,:)); |
|
247 ts = ts(1:2*n-2)'; ys = ys(1:2*n-2)'; |
|
248 if (length(gm) > 0) |
|
249 yy = [ys; gm(i)*ones(size(ts))]; |
|
250 else |
|
251 yy = ys; |
|
252 endif |
|
253 grid("on"); |
|
254 xlabel("time [s]"); |
|
255 ylabel("y(t)"); |
|
256 plot(ts, yy); |
3679
|
257 else |
4422
|
258 if (length(gm) > 0) |
|
259 yy = [y(i,:); gm(i)*ones(size(t))]; |
|
260 else |
|
261 yy = y(i,:); |
|
262 endif |
|
263 grid("on"); |
|
264 xlabel("time [s]"); |
|
265 ylabel("y(t)"); |
|
266 plot(t, yy); |
3679
|
267 endif |
4422
|
268 endfor |
|
269 ## leave gnuplot in multiplot mode is bad style |
|
270 oneplot(); |
3679
|
271 y=[]; |
|
272 t=[]; |
3437
|
273 endif |
3679
|
274 ## printf("##STEPIMP-DEBUG: gratulations, successfull completion.\n"); |
|
275 unwind_protect_cleanup |
|
276 automatic_replot = save_automatic_replot; |
|
277 end_unwind_protect |
|
278 endfunction |