3431
|
1 ## Copyright (C) 1996 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 |
7016
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3431
|
9 ## |
7016
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
3431
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3431
|
18 |
|
19 ## -*- texinfo -*- |
6395
|
20 ## @deftypefn {Function File} {[@var{rldata}, @var{k}] =} rlocus (@var{sys}[, @var{increment}, @var{min_k}, @var{max_k}]) |
3431
|
21 ## |
5460
|
22 ## Display root locus plot of the specified @acronym{SISO} system. |
5016
|
23 ## @example |
|
24 ## @group |
3431
|
25 ## ----- --- -------- |
|
26 ## --->| + |---|k|---->| SISO |-----------> |
|
27 ## ----- --- -------- | |
|
28 ## - ^ | |
|
29 ## |_____________________________| |
5016
|
30 ## @end group |
|
31 ## @end example |
3431
|
32 ## |
5016
|
33 ## @strong{Inputs} |
|
34 ## @table @var |
|
35 ## @item sys |
|
36 ## system data structure |
|
37 ## @item min_k |
|
38 ## Minimum value of @var{k} |
|
39 ## @item max_k |
|
40 ## Maximum value of @var{k} |
|
41 ## @item increment |
|
42 ## The increment used in computing gain values |
|
43 ## @end table |
|
44 ## |
|
45 ## @strong{Outputs} |
|
46 ## |
|
47 ## Plots the root locus to the screen. |
|
48 ## @table @var |
|
49 ## @item rldata |
|
50 ## Data points plotted: in column 1 real values, in column 2 the imaginary values. |
6395
|
51 ## @item k |
5016
|
52 ## Gains for real axis break points. |
|
53 ## @end table |
3431
|
54 ## @end deftypefn |
|
55 |
|
56 ## Author: David Clem |
|
57 ## Author: R. Bruce Tenison <btenison@eng.auburn.edu> |
|
58 ## Updated by Kristi McGowan July 1996 for intelligent gain selection |
|
59 ## Updated by John Ingram July 1996 for systems |
|
60 |
|
61 function [rldata, k_break, rlpol, gvec, real_ax_pts] = rlocus (sys, increment, min_k, max_k) |
|
62 |
6395
|
63 if (nargin < 1 || nargin > 4) |
6046
|
64 print_usage (); |
3431
|
65 endif |
|
66 |
|
67 ## Convert the input to a transfer function if necessary |
5605
|
68 [num,den] = sys2tf(sys); # extract numerator/denom polyomials |
6395
|
69 lnum = length(num); |
|
70 lden = length(den); |
|
71 # equalize length of num, den polynomials |
3431
|
72 if(lden < 2) |
6395
|
73 error("system has no poles"); |
|
74 elseif(lnum < lden) |
|
75 num = [zeros(1,lden-lnum), num]; # so that derivative is shortened by one |
|
76 endif |
|
77 |
|
78 olpol = roots(den); |
|
79 olzer = roots(num); |
|
80 nas = lden -lnum; # number of asymptotes |
|
81 maxk = 0; |
|
82 if(nas > 0) |
|
83 cas = ( sum(olpol) - sum(olzer) )/nas; |
|
84 angles = (2*[1:nas]-1)*pi/nas; |
|
85 # printf("rlocus: there are %d asymptotes centered at %f\n", nas, cas); |
|
86 else |
|
87 cas = angles = []; |
|
88 maxk = 100*den(1)/num(1); |
3431
|
89 endif |
|
90 |
|
91 |
6395
|
92 # compute real axis break points and corresponding gains |
|
93 dnum=polyderiv(num); |
|
94 dden=polyderiv(den); |
|
95 brkp = conv(den, dnum) - conv(num, dden); |
|
96 real_ax_pts = roots(brkp); |
|
97 real_ax_pts = real_ax_pts(find(imag(real_ax_pts) == 0)); |
|
98 k_break = -polyval(den,real_ax_pts) ./ polyval(num, real_ax_pts); |
|
99 idx = find(k_break >= 0); |
|
100 k_break = k_break(idx); |
|
101 real_ax_pts = real_ax_pts(idx); |
|
102 if(!isempty(k_break)) |
|
103 maxk = max(max(k_break),maxk); |
|
104 endif |
|
105 |
|
106 if(nas == 0) |
|
107 maxk = max(1, 2*maxk); % get at least some root locus |
3431
|
108 else |
6395
|
109 # get distance from breakpoints, poles, and zeros to center of asymptotes |
|
110 dmax = 3*max(abs( [vec(olzer); vec(olpol); vec(real_ax_pts)] - cas )); |
|
111 if(dmax == 0) |
|
112 dmax = 1; |
|
113 endif |
|
114 |
|
115 # get gain for dmax along each asymptote, adjust maxk if necessary |
|
116 svals = cas + dmax*exp(j*angles); |
|
117 kvals = -polyval(den,svals) ./ polyval(num, svals); |
|
118 maxk = max(maxk, max(real(kvals))); |
|
119 end |
|
120 |
|
121 ## check for input arguments: |
|
122 if (nargin > 2) |
|
123 mink = min_k; |
|
124 else |
|
125 mink = 0; |
3431
|
126 endif |
6395
|
127 if (nargin > 3) |
|
128 maxk = max_k; |
|
129 endif |
3431
|
130 if (nargin > 1) |
6395
|
131 if(increment <= 0) |
|
132 error("increment must be positive"); |
3431
|
133 else |
|
134 ngain = (maxk-mink)/increment; |
|
135 endif |
6395
|
136 else |
|
137 ngain = 30; |
3431
|
138 endif |
|
139 |
|
140 ## vector of gains |
6395
|
141 ngain = max(30,ngain); |
3431
|
142 gvec = linspace(mink,maxk,ngain); |
6395
|
143 if(length(k_break)) |
|
144 gvec = sort([gvec, vec(k_break)']); |
|
145 endif |
3431
|
146 |
|
147 ## Find the open loop zeros and the initial poles |
|
148 rlzer = roots(num); |
|
149 |
|
150 ## update num to be the same length as den |
6395
|
151 lnum = length(num); |
|
152 if(lnum < lden) |
|
153 num = [zeros(1,lden - lnum),num]; |
|
154 endif |
3431
|
155 |
|
156 ## compute preliminary pole sets |
|
157 nroots = lden-1; |
|
158 for ii=1:ngain |
|
159 gain = gvec(ii); |
|
160 rlpol(1:nroots,ii) = vec(sortcom(roots(den + gain*num))); |
|
161 endfor |
|
162 |
6395
|
163 ## set smoothing tolerance |
|
164 smtolx = 0.01*( max(max(real(rlpol))) - min(min(real(rlpol)))); |
|
165 smtoly = 0.01*( max(max(imag(rlpol))) - min(min(imag(rlpol)))); |
|
166 smtol = max(smtolx, smtoly); |
|
167 rlpol = sort_roots(rlpol,smtolx, smtoly); % sort according to nearest-neighbor |
3431
|
168 |
|
169 done=(nargin == 4); # perform a smoothness check |
|
170 while((!done) & ngain < 1000) |
|
171 done = 1 ; # assume done |
|
172 dp = abs(diff(rlpol'))'; |
6395
|
173 maxdp = max(dp); |
|
174 |
|
175 ## search for poles whose neighbors are distant |
|
176 if(lden == 2) |
|
177 idx = find(dp > smtol); |
|
178 else |
|
179 idx = find(maxdp > smtol); |
|
180 endif |
3431
|
181 |
6395
|
182 for ii=1:length(idx) |
|
183 i1 = idx(ii); |
|
184 g1 = gvec(i1); |
|
185 p1 = rlpol(:,i1); |
|
186 |
|
187 i2 = idx(ii)+1; |
|
188 g2 = gvec(i2); |
|
189 p2 = rlpol(:,i2); |
|
190 |
|
191 ## isolate poles in p1, p2 |
|
192 if( max(abs(p2-p1)) > smtol) |
|
193 newg = linspace(g1,g2,5); |
|
194 newg = newg(2:4); |
|
195 gvec = [gvec,newg]; |
|
196 done = 0; # need to process new gains |
3431
|
197 endif |
|
198 endfor |
|
199 |
|
200 ## process new gain values |
|
201 ngain1 = length(gvec); |
|
202 for ii=(ngain+1):ngain1 |
|
203 gain = gvec(ii); |
|
204 rlpol(1:nroots,ii) = vec(sortcom(roots(den + gain*num))); |
|
205 endfor |
|
206 |
|
207 [gvec,idx] = sort(gvec); |
|
208 rlpol = rlpol(:,idx); |
|
209 ngain = length(gvec); |
6395
|
210 rlpol = sort_roots(rlpol,smtolx, smtoly); % sort according to nearest-neighbor |
3431
|
211 endwhile |
6395
|
212 rldata = rlpol; |
3431
|
213 |
|
214 ## Plot the data |
|
215 if(nargout == 0) |
|
216 rlpolv = vec(rlpol); |
6395
|
217 axdata = [real(rlpolv),imag(rlpolv); real(olzer), imag(olzer)]; |
3431
|
218 axlim = axis2dlim(axdata); |
|
219 rldata = [real(rlpolv), imag(rlpolv) ]; |
|
220 [stn,inname,outname] = sysgetsignals(sys); |
6395
|
221 |
|
222 ## build plot command args pole by pole |
3431
|
223 |
6395
|
224 n_rlpol = rows(rlpol); |
|
225 nelts = n_rlpol+1; |
|
226 if (! isempty (rlzer)) |
|
227 nelts++; |
5605
|
228 endif |
6413
|
229 # add asymptotes |
|
230 n_A = length (olpol) - length (olzer); |
|
231 if (n_A > 0) |
|
232 nelts += n_A; |
|
233 endif |
6395
|
234 args = cell (3, nelts); |
6413
|
235 kk = 0; |
|
236 # asymptotes first |
|
237 if (n_A > 0) |
|
238 len_A = 2*max(abs(axlim)); |
|
239 sigma_A = (sum(olpol) - sum(olzer))/n_A; |
|
240 for i_A=0:n_A-1 |
|
241 phi_A = pi*(2*i_A + 1)/n_A; |
|
242 args{1,++kk} = [sigma_A sigma_A+len_A*cos(phi_A)]; |
|
243 args{2,kk} = [0 len_A*sin(phi_A)]; |
|
244 if (i_A == 1) |
6415
|
245 args{3,kk} = "k--;asymptotes;"; |
6413
|
246 else |
6415
|
247 args{3,kk} = "k--"; |
6413
|
248 endif |
|
249 endfor |
|
250 endif |
|
251 # locus next |
|
252 for ii=1:rows(rlpol) |
|
253 args{1,++kk} = real (rlpol (ii,:)); |
|
254 args{2,kk} = imag (rlpol (ii,:)); |
|
255 if (ii == 1) |
|
256 args{3,kk} = "b-;locus;"; |
|
257 else |
|
258 args{3,kk} = "b-"; |
|
259 endif |
6395
|
260 endfor |
6413
|
261 # poles and zeros last |
|
262 args{1,++kk} = real(olpol); |
|
263 args{2,kk} = imag(olpol); |
|
264 args{3,kk} = "rx;open loop poles;"; |
6395
|
265 if (! isempty(rlzer)) |
6413
|
266 args{1,++kk} = real(rlzer); |
|
267 args{2,kk} = imag(rlzer); |
|
268 args{3,kk} = "go;zeros;"; |
6395
|
269 endif |
|
270 |
6413
|
271 set (gcf,"visible","off"); |
|
272 hplt = plot (args{:}); |
|
273 set (hplt(kk--), "markersize", 2); |
|
274 if (! isempty(rlzer)) |
|
275 set(hplt(kk--), "markersize", 2); |
|
276 endif |
|
277 for ii=1:rows(rlpol) |
|
278 set (hplt(kk--), "linewidth", 2); |
|
279 endfor |
6395
|
280 legend ("boxon", 2); |
|
281 grid ("on"); |
|
282 axis (axlim); |
|
283 xlabel (sprintf ("Root locus from %s to %s, gain=[%f,%f]: Real axis", |
|
284 inname{1}, outname{1}, gvec(1), gvec(ngain))); |
|
285 ylabel ("Imag. axis"); |
6413
|
286 set (gcf,"visible","on"); |
3431
|
287 rldata = []; |
|
288 endif |
|
289 endfunction |
6395
|
290 |
|
291 function rlpol = sort_roots (rlpol,tolx, toly) |
|
292 # no point sorting of you've only got one pole! |
|
293 if(rows(rlpol) == 1) |
|
294 return |
|
295 endif |
|
296 |
|
297 # reorder entries in each column of rlpol to be by their nearest-neighbors |
|
298 dp = diff(rlpol')'; |
|
299 drp = max(real(dp)); |
|
300 dip = max(imag(dp)); |
|
301 idx = find( drp > tolx | dip > toly ); |
|
302 if(isempty(idx) ) |
|
303 return |
|
304 endif |
|
305 |
|
306 [np,ng] = size(rlpol); # num poles, num gains |
|
307 for jj = idx |
|
308 vals = rlpol(:,[jj,jj+1]); |
|
309 jdx = (jj+1):ng; |
|
310 for ii=1:rows(rlpol-1) |
|
311 rdx = ii:np; |
|
312 dval = abs(rlpol(rdx,jj+1)-rlpol(ii,jj)); |
|
313 mindist = min(dval); |
|
314 sidx = min( find ( dval == mindist)) + ii - 1; |
|
315 if( sidx != ii) |
|
316 c1 = norm(diff(vals')); |
|
317 [vals(ii,2), vals(sidx,2)] = swap( vals(ii,2), vals(sidx,2)); |
|
318 c2 = norm(diff(vals')); |
|
319 if(c1 > c2 ) |
|
320 # perform the swap |
|
321 [rlpol(ii,jdx), rlpol(sidx,jdx)] = swap( rlpol(ii,jdx), rlpol(sidx,jdx)); |
|
322 vals = rlpol(:,[jj,jj+1]); |
|
323 endif |
|
324 endif |
|
325 endfor |
|
326 endfor |
|
327 |
|
328 endfunction |