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 -*- |
3500
|
20 ## @deftypefn {Function File} {} tf2ss (@var{inputs}) |
3430
|
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 |
|
45 ## Author: R. Bruce Tenison <btenison@eng.auburn.edu> |
|
46 ## Created: June 22, 1994 |
|
47 ## mod A S Hodel July, Aug 1995 |
|
48 |
|
49 function [a, b, c, d] = tf2ss (num, den) |
|
50 |
|
51 if(nargin != 2) error("tf2ss: wrong number of input arguments") |
|
52 elseif(isempty(num)) error("tf2ss: empty numerator"); |
|
53 elseif(isempty(den)) error("tf2ss: empy denominator"); |
|
54 elseif(!is_vector(num)) |
|
55 error(sprintf("num(%dx%d) must be a vector",rows(num),columns(num))); |
|
56 elseif(!is_vector(den)) |
|
57 error(sprintf("den(%dx%d) must be a vector",rows(den),columns(den))); |
|
58 endif |
|
59 |
|
60 ## strip leading zeros from num, den |
|
61 nz = find(num != 0); |
|
62 if(isempty(nz)) num = 0; |
|
63 else num = num(nz(1):length(num)); endif |
|
64 nz = find(den != 0); |
|
65 if(isempty(nz)) error("denominator is 0."); |
|
66 else den = den(nz(1):length(den)); endif |
|
67 |
|
68 ## force num, den to be row vectors |
|
69 num = vec(num)'; den = vec(den)'; |
|
70 nn = length(num); nd = length(den); |
|
71 if(nn > nd) error(sprintf("deg(num)=%d > deg(den)= %d",nn,nd)); endif |
|
72 |
|
73 ## Check sizes |
|
74 if (nd == 1) a = []; b = []; c = []; d = num(:,1) / den(1); |
|
75 else |
|
76 ## Pad num so that length(num) = length(den) |
|
77 if (nd-nn > 0) num = [zeros(1,nd-nn), num]; endif |
|
78 |
|
79 ## Normalize the numerator and denominator vector w.r.t. the leading |
|
80 ## coefficient |
|
81 d1 = den(1); num = num / d1; den = den(2:nd)/d1; |
|
82 sw = nd-1:-1:1; |
|
83 |
|
84 ## Form the A matrix |
|
85 if(nd > 2) a = [zeros(nd-2,1),eye(nd-2,nd-2);-den(sw)]; |
|
86 else a = -den(sw); endif |
|
87 |
|
88 ## Form the B matrix |
|
89 b = zeros(nd-1,1); b(nd-1,1) = 1; |
|
90 |
|
91 ## Form the C matrix |
|
92 c = num(:,2:nd)-num(:,1)*den; c = c(:,sw); |
|
93 |
|
94 ## Form the D matrix |
|
95 d = num(:,1); |
|
96 endif |
|
97 |
|
98 endfunction |