annotate scripts/control/base/place.m @ 7126:4a375de63f66

[project @ 2007-11-08 03:44:14 by jwe]
author jwe
date Thu, 08 Nov 2007 03:44:15 +0000
parents f084ba47812b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1997, 2000, 2002, 2003, 2004, 2005, 2007
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Jose Daniel Munoz Frias
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
3 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
5 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
7 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
9 ## your option) any later version.
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
10 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
14 ## General Public License for more details.
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
15 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5568
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
19
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
5016
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4771
diff changeset
21 ## @deftypefn {Function File} {@var{K} =} place (@var{sys}, @var{p})
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4771
diff changeset
22 ## Computes the matrix @var{K} such that if the state
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4771
diff changeset
23 ## is feedback with gain @var{K}, then the eigenvalues of the closed loop
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4771
diff changeset
24 ## system (i.e. @math{A-BK}) are those specified in the vector @var{p}.
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
25 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
26 ## Version: Beta (May-1997): If you have any comments, please let me know.
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
27 ## (see the file place.m for my address)
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
28 ## @end deftypefn
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
29
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
30 ## Author: Jose Daniel Munoz Frias
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
31
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
32 ## Universidad Pontificia Comillas
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
33 ## ICAIdea
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
34 ## Alberto Aguilera, 23
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
35 ## 28015 Madrid, Spain
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
36 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
37 ## E-Mail: daniel@dea.icai.upco.es
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
38 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
39 ## Phone: 34-1-5422800 Fax: 34-1-5596569
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
40 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
41 ## Algorithm taken from "The Control Handbook", IEEE press pp. 209-212
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
42 ##
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
43 ## code adaped by A.S.Hodel (a.s.hodel@eng.auburn.edu) for use in controls
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
44 ## toolbox
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
45
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
46 function K = place (sys, P)
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
47
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
48 if (nargin != 2)
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
49 print_usage ();
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
50 endif
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
51
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
52 ## check arguments
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
53
7126
4a375de63f66 [project @ 2007-11-08 03:44:14 by jwe]
jwe
parents: 7125
diff changeset
54 if (! isstruct (sys))
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
55 error ("sys must be in system data structure format (see ss)");
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
56 endif
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
57 sys = sysupdate (sys, "ss"); # make sure it has state space form up to date
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
58 if (! is_controllable (sys))
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
59 error ("sys is not controllable");
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
60 elseif (min (size (P)) != 1)
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
61 error ("P must be a vector")
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
62 else
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
63 P = P(:); # make P a column vector
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
64 endif
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
65 ## system must be purely continuous or discrete
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
66 is_digital (sys);
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
67 [n, nz, m, p] = sysdimensions (sys);
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
68 nx = n+nz; # already checked that it's not a mixed system.
7126
4a375de63f66 [project @ 2007-11-08 03:44:14 by jwe]
jwe
parents: 7125
diff changeset
69 if (m != 1)
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
70 error ("sys has %d inputs; need only 1", m);
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
71 endif
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
72
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
73 ## takes the A and B matrix from the system representation
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
74 [A, B] = sys2ss (sys);
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
75 sp = length (P);
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
76 if (nx == 0)
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
77 error ("place: A matrix is empty (0x0)");
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
78 elseif (nx != length (P))
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
79 error ("A=(%dx%d), P has %d entries", nx, nx, length (P))
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
80 endif
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
81
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
82 ## arguments appear to be compatible; let's give it a try!
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
83 ## The second step is the calculation of the characteristic polynomial ofA
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
84 PC = poly (A);
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
85
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
86 ## Third step: Calculate the transformation matrix T that transforms the state
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
87 ## equation in the controllable canonical form.
4460
cef48c4b902d [project @ 2003-07-11 18:37:48 by jwe]
jwe
parents: 4030
diff changeset
88
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
89 ## first we must calculate the controllability matrix M:
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
90 M = B;
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
91 AA = A;
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
92 for n = 2:nx
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
93 M(:,n) = AA*B;
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
94 AA = AA*A;
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
95 endfor
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
96
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
97 ## second, construct the matrix W
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
98 PCO = PC(nx:-1:1);
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
99 PC1 = PCO; # Matrix to shift and create W row by row
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
100
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
101 for n = 1:nx
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
102 W(n,:) = PC1;
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
103 PC1 = [PCO(n+1:nx), zeros(1,n)];
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
104 endfor
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
105
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
106 T = M*W;
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
107
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
108 ## finaly the matrix K is calculated
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
109 PD = poly (P); # The desired characteristic polynomial
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
110 PD = PD(nx+1:-1:2);
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
111 PC = PC(nx+1:-1:2);
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
112
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
113 K = (PD-PC)/T;
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
114
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
115 ## Check if the eigenvalues of (A-BK) are the same specified in P
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
116 Pcalc = eig (A-B*K);
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
117
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
118 Pcalc = sortcom (Pcalc);
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
119 P = sortcom (P);
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
120
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
121 if (max ((abs(Pcalc)-abs(P))./abs(P) ) > 0.1)
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
122 warning ("place: Pole placed at more than 10% relative error from specified");
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5307
diff changeset
123 endif
4460
cef48c4b902d [project @ 2003-07-11 18:37:48 by jwe]
jwe
parents: 4030
diff changeset
124
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
125 endfunction
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
126