3430
|
1 ## Copyright (C) 1996, 1998 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. |
3430
|
19 |
|
20 ## -*- texinfo -*- |
5016
|
21 ## @deftypefn {Function File} {[@var{a}, @var{b}, @var{c}, @var{d}] =} tf2ss (@var{num}, @var{den}) |
3430
|
22 ## Conversion from tranfer 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 |
|
64 if(nargin != 2) error("tf2ss: wrong number of input arguments") |
|
65 elseif(isempty(num)) error("tf2ss: empty numerator"); |
|
66 elseif(isempty(den)) error("tf2ss: empy denominator"); |
4030
|
67 elseif(!isvector(num)) |
3430
|
68 error(sprintf("num(%dx%d) must be a vector",rows(num),columns(num))); |
4030
|
69 elseif(!isvector(den)) |
3430
|
70 error(sprintf("den(%dx%d) must be a vector",rows(den),columns(den))); |
|
71 endif |
|
72 |
|
73 ## strip leading zeros from num, den |
|
74 nz = find(num != 0); |
|
75 if(isempty(nz)) num = 0; |
|
76 else num = num(nz(1):length(num)); endif |
|
77 nz = find(den != 0); |
|
78 if(isempty(nz)) error("denominator is 0."); |
|
79 else den = den(nz(1):length(den)); endif |
|
80 |
|
81 ## force num, den to be row vectors |
|
82 num = vec(num)'; den = vec(den)'; |
|
83 nn = length(num); nd = length(den); |
|
84 if(nn > nd) error(sprintf("deg(num)=%d > deg(den)= %d",nn,nd)); endif |
|
85 |
|
86 ## Check sizes |
|
87 if (nd == 1) a = []; b = []; c = []; d = num(:,1) / den(1); |
|
88 else |
|
89 ## Pad num so that length(num) = length(den) |
|
90 if (nd-nn > 0) num = [zeros(1,nd-nn), num]; endif |
|
91 |
|
92 ## Normalize the numerator and denominator vector w.r.t. the leading |
|
93 ## coefficient |
|
94 d1 = den(1); num = num / d1; den = den(2:nd)/d1; |
|
95 sw = nd-1:-1:1; |
|
96 |
|
97 ## Form the A matrix |
|
98 if(nd > 2) a = [zeros(nd-2,1),eye(nd-2,nd-2);-den(sw)]; |
|
99 else a = -den(sw); endif |
|
100 |
|
101 ## Form the B matrix |
|
102 b = zeros(nd-1,1); b(nd-1,1) = 1; |
|
103 |
|
104 ## Form the C matrix |
|
105 c = num(:,2:nd)-num(:,1)*den; c = c(:,sw); |
|
106 |
|
107 ## Form the D matrix |
|
108 d = num(:,1); |
|
109 endif |
|
110 |
|
111 endfunction |