annotate scripts/control/base/place.m @ 4771:b8105302cfe8

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