~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to scripts/control/system/zp2ss.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 1996, 2000, 2002, 2003, 2004, 2005, 2007
 
2
##               Auburn University.  All rights reserved.
 
3
##
 
4
## This file is part of Octave.
 
5
##
 
6
## Octave is free software; you can redistribute it and/or modify it
 
7
## under the terms of the GNU General Public License as published by
 
8
## the Free Software Foundation; either version 3 of the License, or (at
 
9
## your option) any later version.
 
10
##
 
11
## Octave is distributed in the hope that it will be useful, but
 
12
## WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
## General Public License for more details.
 
15
##
 
16
## You should have received a copy of the GNU General Public License
 
17
## along with Octave; see the file COPYING.  If not, see
 
18
## <http://www.gnu.org/licenses/>.
 
19
 
 
20
## -*- texinfo -*-
 
21
## @deftypefn {Function File} {[@var{a}, @var{b}, @var{c}, @var{d}] =} zp2ss (@var{zer}, @var{pol}, @var{k})
 
22
## Conversion from zero / pole to state space.
 
23
##
 
24
## @strong{Inputs}
 
25
## @table @var
 
26
## @item zer
 
27
## @itemx pol
 
28
## Vectors of (possibly) complex poles and zeros of a transfer
 
29
## function. Complex values must come in conjugate pairs
 
30
## (i.e., @math{x+jy} in @var{zer} means that @math{x-jy} is also in @var{zer}).
 
31
## The number of zeros must not exceed the number of poles.
 
32
## @item k
 
33
## Real scalar (leading coefficient).
 
34
## @end table
 
35
##
 
36
## @strong{Outputs}
 
37
## @table @var
 
38
## @item @var{a}
 
39
## @itemx @var{b}
 
40
## @itemx @var{c}
 
41
## @itemx @var{d}
 
42
## The state space system, in the form:
 
43
## @iftex
 
44
## @tex
 
45
## $$ \dot x = Ax + Bu $$
 
46
## $$ y = Cx + Du $$
 
47
## @end tex
 
48
## @end iftex
 
49
## @ifinfo
 
50
## @example
 
51
##      .
 
52
##      x = Ax + Bu
 
53
##      y = Cx + Du
 
54
## @end example
 
55
## @end ifinfo
 
56
## @end table
 
57
## @end deftypefn
 
58
 
 
59
## Author: David Clem
 
60
## Created: August 15, 1994
 
61
 
 
62
function [a, b, c, d] = zp2ss (zer, pol, k)
 
63
 
 
64
  if (nargin != 3)
 
65
    print_usage ();
 
66
  endif
 
67
 
 
68
  if (! (isvector (zer) || isempty (zer)))
 
69
    error ("zer(%d,%d) should be a vector", rows (zer), columns (zer));
 
70
  elseif (! (isvector (pol) || isempty (pol)))
 
71
    error ("pol(%d,%d) should be a vector", rows (pol), columns (pol));
 
72
  elseif (! isscalar(k))
 
73
    error ("k(%d,%d) should be a scalar", rows (k), columns (k));
 
74
  elseif (k != real (k))
 
75
    warning ("zp2ss: k is complex")
 
76
  endif
 
77
 
 
78
  zpsys = ss (zeros (0, 0), zeros (0, 1), zeros (1, 0), k);
 
79
 
 
80
  ## Find the number of zeros and the number of poles
 
81
  nzer = length (zer);
 
82
  npol = length (pol);
 
83
 
 
84
  if (nzer > npol)
 
85
    error ("%d zeros, exceeds number of poles=%d", nzer, npol);
 
86
  endif
 
87
 
 
88
  ## Sort to place complex conjugate pairs together
 
89
  zer = sortcom (zer);
 
90
  pol = sortcom (pol);
 
91
 
 
92
  ## construct the system as a series connection of poles and zeros
 
93
  ## problem: poles and zeros may come in conjugate pairs, and not
 
94
  ## matched up!
 
95
 
 
96
  ## approach: remove poles/zeros from the list as they are included in
 
97
  ## the ss system
 
98
 
 
99
  while (length (pol))
 
100
 
 
101
    ## search for complex poles, zeros
 
102
    cpol = [];
 
103
    czer = [];
 
104
    if (! isempty (pol))
 
105
      cpol = find (imag (pol) != 0);
 
106
    endif
 
107
    if (! isempty (zer))
 
108
      czer = find (imag (zer) != 0);
 
109
    endif
 
110
 
 
111
    if (isempty (cpol) && isempty (czer))
 
112
      pcnt = 1;
 
113
    else
 
114
      pcnt = 2;
 
115
    endif
 
116
 
 
117
    num = 1;      # assume no zeros left.
 
118
    switch (pcnt)
 
119
      case 1
 
120
        ## real pole/zero combination
 
121
        if (length (zer))
 
122
          num = [1, -zer(1)];
 
123
          zer = zer(2:length(zer));
 
124
        endif
 
125
        den = [1, -pol(1)];
 
126
        pol = pol(2:length(pol));
 
127
      case 2
 
128
        ## got a complex pole or zero, need two roots (if available)
 
129
        if (length (zer) > 1)
 
130
          [num, zer] = __zp2ssg2__ (zer);       # get two zeros
 
131
        elseif (length (zer) == 1)
 
132
          num = [1, -zer];                # use last zero (better be real!)
 
133
          zer = [];
 
134
        endif
 
135
        [den, pol] = __zp2ssg2__ (pol);         # get two poles
 
136
      otherwise
 
137
        error ("pcnt = %d", pcnt);
 
138
      endswitch
 
139
 
 
140
      ## pack tf into system form and put in series with earlier realization
 
141
      zpsys1 = tf (num, den, 0, "u", "yy");
 
142
 
 
143
      ## change names to avoid warning messages from sysgroup
 
144
      zpsys  = syssetsignals (zpsys, "in", "u1", 1);
 
145
      zpsys1 = sysupdate (zpsys1, "ss");
 
146
      nn     = sysdimensions (zpsys);        # working with continuous system
 
147
      zpsys  = syssetsignals (zpsys, "st", __sysdefioname__ (nn, "x"));
 
148
      nn1    = sysdimensions (zpsys1);
 
149
      zpsys1 = syssetsignals (zpsys1, "st", __sysdefioname__ (nn1, "xx"));
 
150
 
 
151
      zpsys = sysmult (zpsys, zpsys1);
 
152
 
 
153
    endwhile
 
154
 
 
155
    [a, b, c, d] = sys2ss (zpsys);
 
156
 
 
157
endfunction
 
158