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 |
|
17 ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
|
18 |
|
19 ## -*- texinfo -*- |
5016
|
20 ## @deftypefn {Function File} {[@var{a}, @var{b}, @var{c}, @var{d}] =} tf2ss (@var{num}, @var{den}) |
3430
|
21 ## Conversion from tranfer function to state-space. |
5016
|
22 ## The state space system: |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $$ \dot x = Ax + Bu $$ |
|
26 ## $$ y = Cx + Du $$ |
|
27 ## @end tex |
|
28 ## @end iftex |
|
29 ## @ifinfo |
|
30 ## @example |
3430
|
31 ## . |
|
32 ## x = Ax + Bu |
|
33 ## y = Cx + Du |
5016
|
34 ## @end example |
|
35 ## @end ifinfo |
|
36 ## is obtained from a transfer function: |
|
37 ## @iftex |
|
38 ## @tex |
|
39 ## $$ G(s) = { { \rm num }(s) \over { \rm den }(s) } $$ |
|
40 ## @end tex |
|
41 ## @end iftex |
|
42 ## @ifinfo |
|
43 ## @example |
3430
|
44 ## num(s) |
|
45 ## G(s)=------- |
|
46 ## den(s) |
5016
|
47 ## @end example |
|
48 ## @end ifinfo |
3430
|
49 ## |
5016
|
50 ## The vector @var{den} must contain only one row, whereas the vector |
|
51 ## @var{num} may contain as many rows as there are outputs @var{y} of |
|
52 ## the system. The state space system matrices obtained from this function |
|
53 ## will be in controllable canonical form as described in @cite{Modern Control |
|
54 ## Theory}, (Brogan, 1991). |
3430
|
55 ## @end deftypefn |
|
56 |
|
57 ## Author: R. Bruce Tenison <btenison@eng.auburn.edu> |
|
58 ## Created: June 22, 1994 |
|
59 ## mod A S Hodel July, Aug 1995 |
|
60 |
|
61 function [a, b, c, d] = tf2ss (num, den) |
|
62 |
|
63 if(nargin != 2) error("tf2ss: wrong number of input arguments") |
|
64 elseif(isempty(num)) error("tf2ss: empty numerator"); |
|
65 elseif(isempty(den)) error("tf2ss: empy denominator"); |
4030
|
66 elseif(!isvector(num)) |
3430
|
67 error(sprintf("num(%dx%d) must be a vector",rows(num),columns(num))); |
4030
|
68 elseif(!isvector(den)) |
3430
|
69 error(sprintf("den(%dx%d) must be a vector",rows(den),columns(den))); |
|
70 endif |
|
71 |
|
72 ## strip leading zeros from num, den |
|
73 nz = find(num != 0); |
|
74 if(isempty(nz)) num = 0; |
|
75 else num = num(nz(1):length(num)); endif |
|
76 nz = find(den != 0); |
|
77 if(isempty(nz)) error("denominator is 0."); |
|
78 else den = den(nz(1):length(den)); endif |
|
79 |
|
80 ## force num, den to be row vectors |
|
81 num = vec(num)'; den = vec(den)'; |
|
82 nn = length(num); nd = length(den); |
|
83 if(nn > nd) error(sprintf("deg(num)=%d > deg(den)= %d",nn,nd)); endif |
|
84 |
|
85 ## Check sizes |
|
86 if (nd == 1) a = []; b = []; c = []; d = num(:,1) / den(1); |
|
87 else |
|
88 ## Pad num so that length(num) = length(den) |
|
89 if (nd-nn > 0) num = [zeros(1,nd-nn), num]; endif |
|
90 |
|
91 ## Normalize the numerator and denominator vector w.r.t. the leading |
|
92 ## coefficient |
|
93 d1 = den(1); num = num / d1; den = den(2:nd)/d1; |
|
94 sw = nd-1:-1:1; |
|
95 |
|
96 ## Form the A matrix |
|
97 if(nd > 2) a = [zeros(nd-2,1),eye(nd-2,nd-2);-den(sw)]; |
|
98 else a = -den(sw); endif |
|
99 |
|
100 ## Form the B matrix |
|
101 b = zeros(nd-1,1); b(nd-1,1) = 1; |
|
102 |
|
103 ## Form the C matrix |
|
104 c = num(:,2:nd)-num(:,1)*den; c = c(:,sw); |
|
105 |
|
106 ## Form the D matrix |
|
107 d = num(:,1); |
|
108 endif |
|
109 |
|
110 endfunction |