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