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