File: pzmap.m

package info (click to toggle)
octave-control 4.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,924 kB
  • sloc: fortran: 122,524; cpp: 6,954; objc: 210; makefile: 40; xml: 33; sh: 3
file content (226 lines) | stat: -rw-r--r-- 7,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
## Copyright (C) 2009-2016   Lukas F. Reichlin
##
## This file is part of LTI Syncope.
##
## LTI Syncope is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LTI Syncope is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {} pzmap (@var{sys})
## @deftypefnx {Function File} {} pzmap (@var{sys1}, @var{sys2}, @dots{}, @var{sysN})
## @deftypefnx {Function File} {} pzmap (@var{sys1}, @var{'style1'}, @dots{}, @var{sysN}, @var{'styleN'})
## @deftypefnx {Function File} {[@var{p}, @var{z}] =} pzmap (@var{sys})
## Plot the poles and zeros of an LTI system in the complex plane.
## If no output arguments are given, the result is plotted on the screen.
## Otherwise, the poles and zeros are computed and returned. Note that
## only one system is processed when output arguments are given.
##
## @strong{Inputs}
## @table @var
## @item sys, sys1, ...
## @acronym{LTI} model(s).
## @item 'style'
## Color, e.g. 'r' for a red. See @command{help plot} for details.
## Marker or line styles are ignored as poles and zeros have the
## fixed marker types 'x' and 'o' repectively.
## @end table
##
## @strong{Outputs}
## @table @var
## @item p
## Poles of @var{sys}.
## @item z
## Invariant zeros of @var{sys}.
## @end table
## @end deftypefn

## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
## Created: November 2009
## Version: 0.2

function [pol_r, zer_r] = pzmap (varargin)

  if (nargin == 0)
    print_usage ();
  endif

  sys_idx = cellfun (@isa, varargin, {"lti"});      # look for LTI models
  sty_idx = cellfun (@ischar, varargin);            # look for strings (plot styles)

  inv_idx = ! (sys_idx | sty_idx);                  # invalid arguments

  if (any (inv_idx))
    warning ("pzmap: arguments number %s are invalid and are being ignored\n", ...
             mat2str (find (inv_idx)(:).'));
  endif

  if (nnz (sys_idx) == 0)
    error ("pzmap: require at least one LTI model");
  endif

  if (nargout > 0 && (nnz (sys_idx) > 1 || any (sty_idx)))
    print_usage ();
  endif

  if (any (find (sty_idx) < find (sys_idx)(1)))
    warning ("pzmap: strings in front of first LTI model are being ignored\n");
  endif

  pol = cellfun (@pole, varargin(sys_idx), "uniformoutput", false);
  zer = cellfun (@zero, varargin(sys_idx), "uniformoutput", false);

  if (! nargout)

    ms = 10;
    if (strcmp(graphics_toolkit(),'gnuplot'))
      ms = 6;
    endif

    pol_re = cellfun (@real, pol, "uniformoutput", false);
    pol_im = cellfun (@imag, pol, "uniformoutput", false);
    zer_re = cellfun (@real, zer, "uniformoutput", false);
    zer_im = cellfun (@imag, zer, "uniformoutput", false);

    ## extract plotting styles
    tmp = cumsum (sys_idx);
    tmp(sys_idx | ! sty_idx) = 0;
    n = nnz (sys_idx);
    sty = arrayfun (@(x) varargin(tmp == x), 1:n, "uniformoutput", false);

    colororder = get (gca, "colororder");
    rc = rows (colororder);
    def_pol = arrayfun (@(k) {"x", "linewidth", 2, "markersize", ms, "color", colororder(1+rem (k-1, rc), :)}, 1:n, "uniformoutput", false);
    def_zer = arrayfun (@(k) {"o", "linewidth", 2, "markersize", ms, "color", colororder(1+rem (k-1, rc), :)}, 1:n, "uniformoutput", false);
    idx_no_sty = cellfun (@isempty, sty);
    sty_pol = sty_zer = sty;
    sty_pol(idx_no_sty) = def_pol(idx_no_sty);
    sty_zer(idx_no_sty) = def_zer(idx_no_sty);

    leg_args = cell (1, n);
    idx = find (sys_idx);
    dt = false;
    for k = 1 : n
      if (! idx_no_sty(k))
        ## style given, only allow custom colors, no custom markers
        [opt,vopt] = __pltopt__ ('pzmap', sty{k}, false);
        if (! @isempty (opt.color))
          sty_pol{1,k} = {"x", "linewidth", 2, "color", opt.color};
          sty_zer{1,k} = {"o", "linewidth", 2, "color", opt.color};
        else
          if (! vopt)
            warning ("pzmap: ignoring undefined color value in style \"%s\"\n", sty{k}{1,1});
          endif
          sty_pol(k) = def_pol(k);
          sty_zer(k) = def_zer(k);
        endif
      endif
      dt = dt || (varargin{idx(k)}.tsam != 0);
    endfor

    pol_args = horzcat (cellfun (@horzcat, pol_re, pol_im, sty_pol, "uniformoutput", false){:});
    zer_args = horzcat (cellfun (@horzcat, zer_re, zer_im, sty_zer, "uniformoutput", false){:});

    hold on;

    ## If no zeroes then just plot the poles and vice versa
    h = [];
    leg_args = cell ();
    for k = 1:n
      name = inputname (idx(k));
      if (isempty (name))
        name = ["Sys ", num2str(k)];       # needed for  pzmap (lticell{:})
      endif
      if (isempty (zer_re{1,k}))
        hx = plot (pol_re{1,k}, pol_im{1,k}, sty_pol{1,k}{:});
        leg_args = { leg_args{:}, ["poles ", name] };
      elseif  (isempty (pol_re{1,k}))
        hx = plot (zer_re{1,k}, zer_im{1,k}, sty_zer{1,k}{:});
        leg_args = { leg_args{:}, ["zeros ", name] };
      else
        hx = plot (pol_re{1,k}, pol_im{1,k}, sty_pol{1,k}{:}, ...
                   zer_re{1,k}, zer_im{1,k}, sty_zer{1,k}{:});
        leg_args = { leg_args{:}, ["poles ", name], ["zeros ", name] };
      endif
      h = [ h; hx ];
    endfor

    grid on
    box on
    title ("Pole-Zero Map")
    xlabel ("Real Axis")
    ylabel ("Imaginary Axis")

    xl = xlim();
    yl = ylim();

    dx = (xl(2)-xl(1))/10;
    xl(1) = xl(1) - dx;
    xl(2) = xl(2) + dx;
    dy = (yl(2)-yl(1))/10;
    yl(1) = yl(1) - dy;
    yl(2) = yl(2) + dy;

    a = gca ();
    % avoid flickering while drawing axis / stablity region
    set (a, 'xlimmode', 'manual');
    set (a, 'ylimmode', 'manual');
    plot ([0,0], [yl(1)-dy*100, yl(2)+dy*100], '-', 'color', [0.7 0.7 0.7]);
    plot ([xl(1)-dx*100, xl(2)+dx*100], [0,0], '-', 'color', [0.7 0.7 0.7]);
    if dt
      t = 0:0.05:6.3;
      plot(cos(t),sin(t),'-', 'color', [0.7 0.7 0.7]);
    endif
    set (a, 'xlimmode', 'auto');
    set (a, 'ylimmode', 'auto');

    xlim(xl);
    ylim(yl);

    hold off;

    legend (h, leg_args)

  else

    ## output arguments: only one system is allowed as input
    pol_r = pol{1};
    zer_r = zer{1};

  endif

endfunction


%!demo
%! z = tf('z',1);
%! G1z = (z+1)/(z-0.75)/(z^2-1*z+1);
%! pzmap(G1z);

%!demo
%! s = tf('s');
%! G1 = 1/(2*s^2+3*s+4);
%! G2 = (1-s)/(1+s)/(s^2+s+1);
%! pzmap(G1,G2);

%!test
%! s = tf('s');
%! G = (s-1)/(s-2)/(s-3);
%! [pol zer] = pzmap(G);
%! assert(sort(pol), [2 3]', 2*eps);
%! assert(zer, 1, eps);

%!test
%! s = tf('s');
%! g = 1/(2*s^2+3*s+4);
%! pol = pzmap(g);
%! assert(sort(pol), sort(roots([2 3 4]')), eps);