7017
|
1 ## Copyright (C) 1996, 1998, 2000, 2002, 2004, 2005, 2007 |
|
2 ## Auburn University. All rights reserved. |
3430
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
7016
|
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. |
3430
|
10 ## |
7016
|
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. |
3430
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3430
|
19 |
|
20 ## -*- texinfo -*- |
5016
|
21 ## @deftypefn {Function File} {[@var{a}, @var{b}, @var{c}, @var{d}] =} tf2ss (@var{num}, @var{den}) |
7001
|
22 ## Conversion from transfer function to state-space. |
5016
|
23 ## The state space system: |
|
24 ## @iftex |
|
25 ## @tex |
|
26 ## $$ \dot x = Ax + Bu $$ |
|
27 ## $$ y = Cx + Du $$ |
|
28 ## @end tex |
|
29 ## @end iftex |
|
30 ## @ifinfo |
|
31 ## @example |
3430
|
32 ## . |
|
33 ## x = Ax + Bu |
|
34 ## y = Cx + Du |
5016
|
35 ## @end example |
|
36 ## @end ifinfo |
|
37 ## is obtained from a transfer function: |
|
38 ## @iftex |
|
39 ## @tex |
|
40 ## $$ G(s) = { { \rm num }(s) \over { \rm den }(s) } $$ |
|
41 ## @end tex |
|
42 ## @end iftex |
|
43 ## @ifinfo |
|
44 ## @example |
3430
|
45 ## num(s) |
|
46 ## G(s)=------- |
|
47 ## den(s) |
5016
|
48 ## @end example |
|
49 ## @end ifinfo |
3430
|
50 ## |
5016
|
51 ## The vector @var{den} must contain only one row, whereas the vector |
|
52 ## @var{num} may contain as many rows as there are outputs @var{y} of |
|
53 ## the system. The state space system matrices obtained from this function |
|
54 ## will be in controllable canonical form as described in @cite{Modern Control |
|
55 ## Theory}, (Brogan, 1991). |
3430
|
56 ## @end deftypefn |
|
57 |
|
58 ## Author: R. Bruce Tenison <btenison@eng.auburn.edu> |
|
59 ## Created: June 22, 1994 |
|
60 ## mod A S Hodel July, Aug 1995 |
|
61 |
|
62 function [a, b, c, d] = tf2ss (num, den) |
|
63 |
7136
|
64 if (nargin != 2) |
|
65 print_usage (); |
|
66 elseif (isempty (num)) |
|
67 error ("tf2ss: empty numerator"); |
|
68 elseif (isempty (den)) |
|
69 error ("tf2ss: empy denominator"); |
|
70 elseif (! isvector (num)) |
|
71 error ("num(%dx%d) must be a vector", rows (num), columns (num)); |
|
72 elseif (! isvector (den)) |
|
73 error ("den(%dx%d) must be a vector", rows (den), columns (den)); |
3430
|
74 endif |
|
75 |
|
76 ## strip leading zeros from num, den |
7136
|
77 nz = find (num != 0); |
|
78 if (isempty (nz)) |
|
79 num = 0; |
|
80 else |
|
81 num = num(nz(1):length(num)); |
|
82 endif |
|
83 nz = find (den != 0); |
|
84 if (isempty (nz)) |
|
85 error ("denominator is 0."); |
|
86 else |
|
87 den = den(nz(1):length(den)); |
|
88 endif |
3430
|
89 |
|
90 ## force num, den to be row vectors |
7136
|
91 num = vec (num)'; |
|
92 den = vec (den)'; |
|
93 nn = length (num); |
|
94 nd = length (den); |
|
95 if (nn > nd) |
7137
|
96 error ("deg(num)=%d > deg(den)= %d", nn, nd); |
7136
|
97 endif |
3430
|
98 |
|
99 ## Check sizes |
7136
|
100 if (nd == 1) |
|
101 a = b = c = []; |
|
102 d = num(:,1) / den(1); |
3430
|
103 else |
|
104 ## Pad num so that length(num) = length(den) |
7136
|
105 if (nd-nn > 0) |
|
106 num = [zeros(1,nd-nn), num]; |
|
107 endif |
3430
|
108 |
|
109 ## Normalize the numerator and denominator vector w.r.t. the leading |
|
110 ## coefficient |
7136
|
111 d1 = den(1); |
|
112 num = num / d1; |
|
113 den = den(2:nd)/d1; |
3430
|
114 sw = nd-1:-1:1; |
|
115 |
|
116 ## Form the A matrix |
7136
|
117 if (nd > 2) |
|
118 a = [zeros(nd-2,1), eye(nd-2,nd-2); -den(sw)]; |
|
119 else |
|
120 a = -den(sw); |
|
121 endif |
3430
|
122 |
|
123 ## Form the B matrix |
7136
|
124 b = zeros (nd-1, 1); |
|
125 b(nd-1,1) = 1; |
3430
|
126 |
|
127 ## Form the C matrix |
7136
|
128 c = num(:,2:nd)-num(:,1)*den; |
|
129 c = c(:,sw); |
3430
|
130 |
|
131 ## Form the D matrix |
|
132 d = num(:,1); |
|
133 endif |
|
134 |
|
135 endfunction |