3279
|
1 # Copyright (C) 1996 Auburn University. All Rights Reserved |
3213
|
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 |
3284
|
17 # Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
3213
|
18 |
|
19 function moddemo() |
|
20 # Octave Controls toolbox demo: Model Manipulations demo |
|
21 # Written by David Clem August 15, 1994 |
|
22 |
|
23 # a s hodel: updated to reflect updated output order in ss2zp |
|
24 |
|
25 while (1) |
|
26 clc |
|
27 disp('Octave Model Manipulations Demo') |
|
28 disp('=======================================') |
|
29 disp(' 1) Perform continuous to discrete time conversion (c2d)') |
|
30 disp(' 2) Convert from state space to zero / pole form (ss2zp)') |
|
31 disp(' Convert from zero / pole to state space form (zp2ss)') |
|
32 disp(' 3) Convert from state space to transfer function form (ss2tf)') |
|
33 disp(' Convert from transfer function to state space form (tf2ss)') |
|
34 disp(' 4) Convert from transfer function to zero / pole form (tf2zp)') |
|
35 disp(' Convert from zero / pole to transfer function form (zp2tf)') |
|
36 disp(' 5) Return to main demo menu') |
|
37 disp(' ') |
|
38 k=6; |
|
39 while(k > 5 || k < 1) |
|
40 k = input('Please enter a number:'); |
|
41 endwhile |
|
42 if (k == 1) |
|
43 clc |
|
44 disp('Perform continuous to discrete time conversion (c2d)\n') |
|
45 disp('Example #1, Consider the following continuous time state space system:\n') |
|
46 a=[0 1;-25 -4] |
|
47 b=[0;1] |
|
48 c=[1 1] |
|
49 d=1 |
|
50 prompt |
|
51 disp('\nTo convert this to a discrete time system (using a zero order hold),') |
|
52 disp('use the following commands:\n') |
|
53 cmd="sys=ss2sys(a,b,c,d);"; |
|
54 run_cmd |
|
55 cmd="dsys = c2d(sys,0.2);"; |
|
56 run_cmd |
|
57 cmd="sysout(dsys);"; |
|
58 run_cmd |
|
59 disp('Function check\n') |
|
60 disp('Check the poles of sys vs dsys:\n') |
|
61 cmd="[da,db]=sys2ss(dsys);"; |
|
62 run_cmd |
|
63 cmd="lam = eig(a);"; |
|
64 run_cmd |
|
65 disp('Discretize the continuous time eigenvalues using the matrix exponential:\n') |
|
66 disp('lambc = exp(lam*0.2)\n') |
|
67 lambc = exp(lam*0.2) |
|
68 disp('Check the eigenvalues of da\n') |
|
69 lambd = eig(da) |
|
70 disp('Calculate the difference between lambd and lambc:\n') |
|
71 cmd = 'error = sort(lambd)-sort(lambc)\n'; |
|
72 run_cmd |
|
73 disp("The error is on the order of roundoff noise, so we're o.k.") |
|
74 prompt |
|
75 clc |
|
76 elseif (k == 2) |
|
77 clc |
|
78 disp('Convert from state space to zero / pole form (ss2zp)\n') |
|
79 disp('Example #1, Consider the following state space system:\n') |
|
80 a=[0 3 1;-2 -4 5;5 8 2] |
|
81 b=[0;5;2.5] |
|
82 c=[6 -1.9 2] |
|
83 d=[-20] |
|
84 prompt |
|
85 disp(' ') |
|
86 disp('\nTo find the poles and zeros of this sytstem, use the following command:\n') |
|
87 disp('\n[zer, pol] = ss2zp(a, b, c, d)\n') |
|
88 prompt |
|
89 disp('Results:\n') |
|
90 [zer, pol] = ss2zp(a, b, c, d) |
|
91 disp('Variable Description:\n') |
|
92 disp('zer, pol => zeros and poles of the state space system') |
|
93 disp('a, b, c, d => state space system\n') |
|
94 prompt |
|
95 clc |
|
96 disp('Convert from zero / pole to state space form (zp2ss)\n') |
|
97 disp('Example #1, Consider the following set of zeros and poles:\n') |
|
98 zer |
|
99 pol |
|
100 prompt |
|
101 disp('\nTo find an equivalent state space representation for this set of poles') |
|
102 disp('and zeros, use the following commands:\n') |
|
103 k=1 |
|
104 disp('\n[na, nb, nc, nd] = zp2ss(zer, pol, k)\n') |
|
105 prompt |
|
106 disp('Results:\n') |
|
107 [na, nb, nc, nd] = zp2ss(zer, pol, k) |
|
108 disp('Variable Description:\n') |
|
109 disp('na, nb, nc, nd => state space system equivalent to zero / pole input') |
|
110 disp('zer, pol => zeros and poles of desired state space system') |
|
111 disp('k => gain associated with the zeros\n') |
|
112 prompt |
|
113 disp('Function check\n') |
|
114 disp('Are the eigenvalues of the origonal state space system the same as the') |
|
115 disp('eigenvalues of the newly constructed state space system ?\n') |
|
116 disp('Find the difference between the two sets of eigenvalues') |
|
117 disp('error = sort(eig(a)) - sort(eig(na))\n') |
|
118 error = sort(eig(a)) - sort(eig(na)) |
|
119 prompt |
|
120 clc |
|
121 elseif (k == 3) |
|
122 clc |
|
123 disp('Convert from state space to transfer function (ss2tf)\n') |
|
124 disp('Example #1, Consider the following state space system:\n') |
|
125 a=[0 1;-2 -3] |
|
126 b=[1;1] |
|
127 c=[1 9] |
|
128 d=[1] |
|
129 prompt |
|
130 disp('\nTo find an equivalent transfer function for this system, use') |
|
131 disp('the following command:\n') |
|
132 disp('[num, den] = ss2tf(a, b, c, d)\n') |
|
133 prompt |
|
134 disp('Results:\n') |
|
135 [num,den] = ss2tf(a, b, c, d) |
|
136 disp('Variable Description:\n') |
|
137 disp('num, den => numerator and denominator of transfer function that is') |
|
138 disp(' equivalent to the state space system') |
|
139 disp('a, b, c, d => state space system\n') |
|
140 prompt |
|
141 clc |
|
142 disp('Convert from transfer function to state space form (tf2ss)\n') |
|
143 disp('Example #1, Consider the following transfer function:\n') |
|
144 num |
|
145 den |
|
146 prompt |
|
147 disp('\nTo find an equivalent state space representation for this system, use') |
|
148 disp('the following command:\n') |
|
149 disp('[a, b, c, d] = tf2ss(num, den)\n') |
|
150 prompt |
|
151 disp('Results:\n') |
|
152 [a, b, c, d] = tf2ss(num, den) |
|
153 disp('Variable Description:\n') |
|
154 disp('a, b, c, d => state space system equivalent to transfer function input') |
|
155 disp('num, den => numerator and denominator of transfer function that is equivalent') |
|
156 disp(' to the state space system\n') |
|
157 prompt |
|
158 clc |
|
159 elseif (k == 4) |
|
160 clc |
|
161 disp('Convert from transfer function to zero / pole form (tf2zp)\n') |
|
162 disp('Example #1, Consider the following transfer function:\n') |
|
163 num=[1 2 3 4 5 ] |
|
164 den=[1 2 3 4 5 6 7] |
|
165 prompt |
|
166 disp('\nTo find the zeros and poles of this system, use the following command:\n') |
|
167 disp('[zer,pol] = tf2zp(num,den)\n') |
|
168 prompt |
|
169 disp('Results:\n') |
|
170 [zer,pol] = tf2zp(num,den) |
|
171 disp('Variable Description:\n') |
|
172 disp('zer,pol => zeros and poles of the transfer function') |
|
173 disp('num, den => numerator and denominator of transfer function\n') |
|
174 prompt |
|
175 clc |
|
176 disp('Convert from zero / pole to transfer function (zp2tf)\n') |
|
177 disp('Example #1, Consider the following set of zeros and poles:\n') |
|
178 zer |
|
179 pol |
|
180 prompt |
|
181 disp('\nTo find an equivalent transfer function representation for this set') |
|
182 disp('of poles and zeros, use the following commands:\n') |
|
183 k=1 |
|
184 disp('\n[num, den] = zp2tf(zer, pol, k)\n') |
|
185 prompt |
|
186 disp('Results:\n') |
|
187 [num, den] = zp2tf(zer, pol, k) |
|
188 disp('Variable Description:\n') |
|
189 disp('[num, den] => transfer function representation of desired set of zeros') |
|
190 disp(' and poles') |
|
191 disp('a, b, c, d => state space system') |
|
192 disp('zer, pol => zeros and poles of desired state space system') |
|
193 disp('k => gain associated with the zeros\n') |
|
194 prompt |
|
195 clc |
|
196 elseif (k == 5) |
|
197 return |
|
198 endif |
|
199 endwhile |
|
200 endfunction |