annotate scripts/control/base/place.m @ 4422:92be67bc9301

[project @ 2003-06-04 17:43:15 by jwe]
author jwe
date Wed, 04 Jun 2003 17:43:15 +0000
parents 22bd65326ec1
children cef48c4b902d
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
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
47 sav_val = empty_list_elements_ok;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
48 empty_list_elements_ok = 1;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
49
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
50 ## check arguments
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
51
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3502
diff changeset
52 if(!isstruct(sys))
3432
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
53 error("sys must be in system data structure format (see ss2sys)");
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
54 endif
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
55 sys = sysupdate(sys,"ss"); # make sure it has state space form up to date
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
56 if(!is_controllable(sys))
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
57 error("sys is not controllable.");
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
58 elseif( min(size(P)) != 1)
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
59 error("P must be a vector")
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
60 else
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
61 P = reshape(P,length(P),1); # make P a column vector
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
62 endif
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
63 ## system must be purely continuous or discrete
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
64 is_digital(sys);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
65 [n,nz,m,p] = sysdimensions(sys);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
66 nx = n+nz; # already checked that it's not a mixed system.
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
67 if(m != 1)
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
68 error(["sys has ", num2str(m)," inputs; need only 1"]);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
69 endif
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
70
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
71 ## takes the A and B matrix from the system representation
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
72 [A,B]=sys2ss(sys);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
73 sp = length(P);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
74 if(nx == 0)
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
75 error("place: A matrix is empty (0x0)");
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
76 elseif(nx != length(P))
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
77 error(["A=(",num2str(nx),"x",num2str(nx),", P has ", num2str(length(P)), ...
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
78 "entries."])
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
79 endif
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
80
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
81 ## arguments appear to be compatible; let's give it a try!
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
82 ## The second step is the calculation of the characteristic polynomial ofA
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
83 PC=poly(A);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
84
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
85 ## Third step: Calculate the transformation matrix T that transforms the state
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
86 ## equation in the controllable canonical form.
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
87
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
88 ## first we must calculate the controllability matrix M:
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
89 M=B;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
90 AA=A;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
91 for n = 2:nx
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
92 M(:,n)=AA*B;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
93 AA=AA*A;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
94 endfor
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
95
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
96 ## second, construct the matrix W
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
97 PCO=PC(nx:-1:1);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
98 PC1=PCO; # Matrix to shift and create W row by row
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
99
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
100 for n = 1:nx
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
101 W(n,:) = PC1;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
102 PC1=[PCO(n+1:nx),zeros(1,n)];
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
103 endfor
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
104
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
105 T=M*W;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
106
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
107 ## finaly the matrix K is calculated
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
108 PD = poly(P); # The desired characteristic polynomial
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
109 PD = PD(nx+1:-1:2);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
110 PC = PC(nx+1:-1:2);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
111
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
112 K = (PD-PC)/T;
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
113
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
114 ## Check if the eigenvalues of (A-BK) are the same specified in P
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
115 Pcalc = eig(A-B*K);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
116
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
117 Pcalc = sortcom(Pcalc);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
118 P = sortcom(P);
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
119
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
120 if(max( (abs(Pcalc)-abs(P))./abs(P) ) > 0.1)
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
121 disp("Place: Pole placed at more than 10% relative error from specified");
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
122 endif
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
123
e39d90787668 [project @ 2000-01-14 04:22:59 by jwe]
jwe
parents:
diff changeset
124 empty_list_elements_ok = sav_val;
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