Mercurial > hg > octave-shane
changeset 4779:f105000ab25c
[project @ 2004-02-17 02:34:33 by jwe]
author | jwe |
---|---|
date | Tue, 17 Feb 2004 02:34:33 +0000 |
parents | 567c8e2d2438 |
children | 5eb5b8aaed8a |
files | scripts/control/system/__sysconcat__.m scripts/control/system/__tfl__.m scripts/control/system/cellidx.m scripts/control/system/ss.m scripts/control/system/tf.m scripts/control/system/zp.m |
diffstat | 6 files changed, 736 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/scripts/control/system/__sysconcat__.m @@ -0,0 +1,23 @@ +function c = __sysconcat__(a,b) + # c = __sysconcat__(a,b) + # cell array replacement for append, used by control systems toolbox + + if(isstr(a)) + a = {a}; + endif + if(isstr(b)) + b = {b}; + endif + + if ( ! ( is_signal_list(a) && is_signal_list(b) ) ) + error("need cell arrays of strings"); + endif + + c = a; + la = length(a); + for ii=1:length(b) + c{la+ii} = b{ii}; + endfor + +endfunction +
new file mode 100644 --- /dev/null +++ b/scripts/control/system/__tfl__.m @@ -0,0 +1,35 @@ +## Copyright (C) 1996 Auburn University. All rights reserved. +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by the +## Free Software Foundation; either version 2, or (at your option) any +## later version. +## +## Octave is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +## for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} __tfl__ (@var{vec}) +## used internally in tf. +## strip leading zero coefficients to get the true polynomial length +## @end deftypefn + +function vec = __tfl__ (vec) + + while (length (vec) > 1 && vec(1) == 0) + vec = vec (2:end); + endwhile + + if (vec(1) == 0) + warning ("tf: polynomial has no nonzero coefficients!") + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/control/system/cellidx.m @@ -0,0 +1,97 @@ +## Copyright (C) 2000 2004 Auburn University. All rights reserved. +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by the +## Free Software Foundation; either version 2, or (at your option) any +## later version. +## +## Octave is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +## for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {[@var{idxvec}, @var{errmsg}] =} listidx (@var{listvar}, @var{strlist}) +## Return indices of string entries in @var{listvar} that match strings +## in @var{strlist}. +## +## Both @var{listvar} and @var{strlist} may be passed as strings or +## string matrices. If they are passed as string matrices, each entry +## is processed by @code{deblank} prior to searching for the entries. +## +## The first output is the vector of indices in @var{listvar}. +## +## If @var{strlist} contains a string not in @var{listvar}, then +## an error message is returned in @var{errmsg}. If only one output +## argument is requested, then @var{listidx} prints @var{errmsg} to the +## screen and exits with an error. +## @end deftypefn + +function [idxvec,errmsg] = cellidx(listvar,strlist) + +if(nargin != 2) + usage("idxvec = cellidx(listvar,strlist)"); +endif + +if(isstr(strlist)) + tmp = strlist; + strlist = {}; + for kk=1:rows(tmp) + strlist{kk} = deblank(tmp(kk,:)); + endfor +endif + +if(isstr(listvar)) + tmp = listvar; + listvar = {}; + for kk=1:rows(tmp) + listvar{kk} = deblank(tmp(kk,:)); + endfor +endif + +## initialize size of idxvec (for premature return) +idxvec = zeros(length(strlist),1); + +errmsg = ""; +if(!is_signal_list(listvar)) + errmsg = "listvar must be a list of strings"; +elseif(!is_signal_list(strlist)) + errmsg = "strlist must be a list of strings"; +endif + +if(length(errmsg)) + if(nargout < 2) error(errmsg); + else return; + endif +endif + +nsigs = length(listvar); +for idx = 1:length(strlist) + signame = strlist{idx}; + for jdx = 1:nsigs + if( strcmp(signame,listvar{jdx}) ) + if(idxvec(idx) != 0) + warning("Duplicate signal name %s (%d,%d)\n", ... + listvar{jdx},jdx,idxvec(idx)); + else + idxvec(idx) = jdx; + endif + endif + endfor + if(idxvec(idx) == 0) + errmsg = sprintf("Did not find %s",signame); + if(nargout == 1) + error(errmsg); + else + break + end + endif +endfor + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/control/system/ss.m @@ -0,0 +1,287 @@ +## Copyright (C) 1996, 1998 Auburn University. All rights reserved. +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by the +## Free Software Foundation; either version 2, or (at your option) any +## later version. +## +## Octave is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +## for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} ss (@var{a}, @var{b}, @var{c}, @var{d}, @var{tsam}, @var{n}, @var{nz}, @var{stname}, @var{inname}, @var{outname}, @var{outlist}) +## Create system structure from state-space data. May be continous, +## discrete, or mixed (sampeled-data) +## +## @strong{Inputs} +## @table @var +## @item a +## @itemx b +## @itemx c +## @itemx d +## usual state space matrices. +## +## default: @var{d} = zero matrix +## +## @item tsam +## sampling rate. Default: @math{tsam = 0} (continuous system) +## +## @item n +## @itemx nz +## number of continuous, discrete states in the system +## +## If @var{tsam} is 0, @math{n = @code{rows}(@var{a})}, @math{nz = 0}. +## +## If @var{tsam} is greater than zero, @math{n = 0}, +## @math{nz = @code{rows}(@var{a})} +## +## see below for system partitioning +## +## @item stname +## list of strings of state signal names +## +## default (@var{stname}=[] on input): @code{x_n} for continuous states, +## @code{xd_n} for discrete states +## +## @item inname +## list of strings of input signal names +## +## default (@var{inname} = [] on input): @code{u_n} +## +## @item outname +## list of strings of input signal names +## +## default (@var{outname} = [] on input): @code{y_n} +## +## @item outlist +## +## list of indices of outputs y that are sampled +## +## If @var{tsam} is 0, @math{outlist = []}. +## +## If @var{tsam} is greater than 0, @math{outlist = 1:@code{rows}(@var{c})}. +## @end table +## +## Unlike states, discrete/continous outputs may appear in any order. +## +## @strong{Note} @code{sys2ss} returns a vector @var{yd} where +## @var{yd}(@var{outlist}) = 1; all other entries of @var{yd} are 0. +## +## @strong{Outputs} +## @var{outsys} = system data structure +## +## @strong{System partitioning} +## +## Suppose for simplicity that outlist specified +## that the first several outputs were continuous and the remaining outputs +## were discrete. Then the system is partitioned as +## @example +## @group +## x = [ xc ] (n x 1) +## [ xd ] (nz x 1 discrete states) +## a = [ acc acd ] b = [ bc ] +## [ adc add ] [ bd ] +## c = [ ccc ccd ] d = [ dc ] +## [ cdc cdd ] [ dd ] +## +## (cdc = c(outlist,1:n), etc.) +## @end group +## @end example +## with dynamic equations: +## @ifinfo +## @math{d/dt xc(t) = acc*xc(t) + acd*xd(k*tsam) + bc*u(t)} +## +## @math{xd((k+1)*tsam) = adc*xc(k*tsam) + add*xd(k*tsam) + bd*u(k*tsam)} +## +## @math{yc(t) = ccc*xc(t) + ccd*xd(k*tsam) + dc*u(t)} +## +## @math{yd(k*tsam) = cdc*xc(k*tsam) + cdd*xd(k*tsam) + dd*u(k*tsam)} +## @end ifinfo +## @iftex +## @tex +## $$\eqalign{ +## {d \over dt} x_c(t) +## & = a_{cc} x_c(t) + a_{cd} x_d(k*t_{sam}) + bc*u(t) \cr +## x_d((k+1)*t_{sam}) +## & = a_{dc} x_c(k t_{sam}) + a_{dd} x_d(k t_{sam}) + b_d u(k t_{sam}) \cr +## y_c(t) +## & = c_{cc} x_c(t) + c_{cd} x_d(k t_{sam}) + d_c u(t) \cr +## y_d(k t_{sam}) +## & = c_{dc} x_c(k t_{sam}) + c_{dd} x_d(k t_{sam}) + d_d u(k t_{sam}) +## }$$ +## @end tex +## @end iftex +## +## @strong{Signal partitions} +## @example +## @group +## | continuous | discrete | +## ---------------------------------------------------- +## states | stname(1:n,:) | stname((n+1):(n+nz),:) | +## ---------------------------------------------------- +## outputs | outname(cout,:) | outname(outlist,:) | +## ---------------------------------------------------- +## @end group +## @end example +## where @math{cout} is the list of in 1:@code{rows}(@var{p}) +## that are not contained in outlist. (Discrete/continuous outputs +## may be entered in any order desired by the user.) +## +## @strong{Example} +## @example +## octave:1> a = [1 2 3; 4 5 6; 7 8 10]; +## octave:2> b = [0 0 ; 0 1 ; 1 0]; +## octave:3> c = eye(3); +## octave:4> sys = ss(a,b,c,[],0,3,0,list("volts","amps","joules")); +## octave:5> sysout(sys); +## Input(s) +## 1: u_1 +## 2: u_2 +## +## Output(s): +## 1: y_1 +## 2: y_2 +## 3: y_3 +## +## state-space form: +## 3 continuous states, 0 discrete states +## State(s): +## 1: volts +## 2: amps +## 3: joules +## +## A matrix: 3 x 3 +## 1 2 3 +## 4 5 6 +## 7 8 10 +## B matrix: 3 x 2 +## 0 0 +## 0 1 +## 1 0 +## C matrix: 3 x 3 +## 1 0 0 +## 0 1 0 +## 0 0 1 +## D matrix: 3 x 3 +## 0 0 +## 0 0 +## 0 0 +## @end example +## Notice that the @math{D} matrix is constructed by default to the +## correct dimensions. Default input and output signals names were assigned +## since none were given. +## @end deftypefn + +## Author: John Ingram <ingraje@eng.auburn.edu> +## Created: July 20, 1996 + +function retsys = ss (a, b, c, d, tsam, n, nz, stname, inname, outname, outlist) + + ## Test for correct number of inputs + if ((nargin < 3) | (nargin > 11)) + usage("retsys = ss (a,b,c{,d,tsam,n,nz,stname,inname,outname,outlist})"); + endif + + ## verify A, B, C, D arguments + ## If D is not specified, set it to a zero matrix of appriate dimension. + if (nargin == 3) d = zeros(rows(c) , columns(b)); + elseif (isempty(d)) d = zeros(rows(c) , columns(b)); endif + + ## Check the dimensions + [na,m,p] = abcddim(a,b,c,d); + + ## If dimensions are wrong, exit function + if (m == -1) + error("a(%dx%d), b(%dx%d), c(%dx%d), d(%dx%d); incompatible", ... + rows(a), columns(a), rows(b), columns(b), rows(c), columns(c), ... + rows(d), columns(d)); + endif + + ## check for tsam input + if(nargin < 5) tsam = 0; + elseif( !( is_sample(tsam) | (tsam == 0) ) ) + error("tsam must be a nonnegative real scalar"); + endif + + ## check for continuous states + if( (nargin < 6) & (tsam == 0) ) n = na; + elseif(nargin < 6) n = 0; + elseif((!ismatrix(n)) | isstr(n)) + error("Parameter n is not a numerical value."); + elseif( (!isscalar(n)) | (n < 0 ) | (n != round(n)) ) + if(isscalar(n)) error("invalid value of n=%d,%e",n,n); + else error("invalid value of n=(%dx%d)", ... + rows(n), columns(n)); endif + endif + + ## check for num discrete states + if( (nargin < 7) & (tsam == 0)) nz = 0; + elseif(nargin < 7) nz = na - n; + elseif((!ismatrix(nz)) | isstr(nz)) + error("Parameter nz is not a numerical value."); + elseif( (!isscalar(nz)) | (nz < 0 ) | (nz != round(nz)) ) + if(isscalar(nz)) + error(["invalid value of nz=",num2str(nz)]); + else + error(["invalid value of nz=(",num2str(rows(nz)),"x", ... + num2str(columns(nz)),")"]); + endif + endif + + ## check for total number of states + if( (n + nz) != na ) + error(["invalid: a is ",num2str(na),"x",num2str(na),", n=", ... + num2str(n),", nz=",num2str(nz)]); + endif + + ## construct system with default names + retsys.a = a; + retsys.b = b; + retsys.c = c; + retsys.d = d; + + retsys.n = n; + retsys.nz = nz; + retsys.tsam = tsam; + retsys.yd = zeros(1,p); # default value entered below + + ## Set the system vector: active = 2(ss), updated = [0 0 1]; + retsys.sys = [2, 0, 0, 1]; + + retsys.stname = __sysdefstname__ (n, nz); + retsys.inname = __sysdefioname__ (m, "u"); + retsys.outname = __sysdefioname__ (p, "y"); + + ## check for state names + if(nargin >= 8) + if(!isempty(stname)) retsys = syssetsignals(retsys,"st",stname); endif + endif + + ## check for input names + if(nargin >= 9) + if(!isempty(inname)) retsys = syssetsignals(retsys,"in",inname); endif + endif + + ## check for output names + if(nargin >= 10) + if(!isempty(outname)) retsys = syssetsignals(retsys,"out",outname); endif + endif + + ## set up yd + if(nargin < 11) + retsys = syssetsignals(retsys,"yd",ones(1,p)*(tsam > 0)); + else + if(!isempty(outlist)) + retsys = syssetsignals(retsys,"yd",ones(size(outlist)),outlist); + endif + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/control/system/tf.m @@ -0,0 +1,143 @@ +## Copyright (C) 1996, 1998 Auburn University. All rights reserved. +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by the +## Free Software Foundation; either version 2, or (at your option) any +## later version. +## +## Octave is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +## for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} tf (@var{num}, @var{den}, @var{tsam}, @var{inname}, @var{outname}) +## build system data structure from transfer function format data +## +## @strong{Inputs} +## @table @var +## @item num +## @itemx den +## coefficients of numerator/denominator polynomials +## @item tsam +## sampling interval. default: 0 (continuous time) +## @item inname +## @itemx outname +## input/output signal names; may be a string or cell array with a single string +## entry. +## @end table +## +## @strong{Outputs} +## @var{sys} = system data structure +## +## @strong{Example} +## @example +## octave:1> sys=tf([2 1],[1 2 1],0.1); +## octave:2> sysout(sys) +## Input(s) +## 1: u_1 +## Output(s): +## 1: y_1 (discrete) +## Sampling interval: 0.1 +## transfer function form: +## 2*z^1 + 1 +## ----------------- +## 1*z^2 + 2*z^1 + 1 +## @end example +## @end deftypefn + +## Author: R. Bruce Tenison <btenison@eng.auburn.edu> +## Created: July 29, 1994 +## Name changed to TF2SYS July 1995 +## updated for new system data structure format July 1996 + +function outsys = tf (num, den, tsam, inname, outname) + + ## Test for the correct number of input arguments + if ((nargin < 2) || (nargin > 5)) + usage ("outsys = tf (num, den [, tsam, inname, outname])"); + return + endif + + ## check input format + if( ! ( (isvector(num) || isscalar(num)) && ... + (isvector(den) || isscalar(den))) ) + error(["num (",num2str(rows(num)),"x",num2str(columns(num)), ... + ") and den (",num2str(rows(den)),"x",num2str(columns(den)), ... + ") must be vectors"]) + endif + + ## strip leading zero coefficients + num = __tfl__ (num); + den = __tfl__ (den); + + if (length(num) > length(den)) + error("# of poles (%d) < # of zeros (%d)",length(den)-1, length(num)-1); + endif + + ## check sampling interval (if any) + if(nargin <= 2) tsam = 0; # default + elseif (isempty(tsam)) tsam = 0; endif + if ( (! (isscalar(tsam) && (imag(tsam) == 0) )) || (tsam < 0) ) + error("tsam must be a positive real scalar") + endif + + outsys.num = num; + outsys.den = den; + + ## Set the system vector: active = 0(tf), updated = [1 0 0]; + outsys.sys = [0, 1, 0, 0]; + + ## Set defaults + outsys.tsam = tsam; + outsys.n = length(den)-1; + outsys.nz = 0; + outsys.yd = 0; # assume discrete-time + ## check discrete time + if(tsam > 0) + [outsys.n,outsys.nz] = swap(outsys.n, outsys.nz); + outsys.yd = 1; + endif + + outsys.inname = __sysdefioname__ (1, "u"); + outsys.outname = __sysdefioname__ (1, "y"); + outsys.stname = __sysdefstname__ (outsys.n, outsys.nz); + + ## Set name of input + if (nargin > 3) + ## make sure it's a cell array of a single string + if(!isempty(inname)) + if(!iscell(inname)) inname = {inname}; endif + if( !is_signal_list(inname) ) + error("inname must be a string or cell array of strings"); + endif + if(length(inname) > 1) + warning("tf: %d input names provided; first used",length(inname)); + inname = inname(1); + endif + outsys = syssetsignals(outsys,"in",inname); + endif + endif + + ## Set name of output + if (nargin > 4) + if(!isempty(outname)) + if(!iscell(outname)) outname = {outname}; endif + if(!is_signal_list(outname)) + error("outname must be a string or a cell array of strings"); + endif + if(length(outname) > 1) + warning("tf: %d output names provided; first used",length(outname)); + outname = outname(1); + endif + outsys = syssetsignals(outsys,"out",outname); + endif + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/control/system/zp.m @@ -0,0 +1,151 @@ +## Copyright (C) 1996, 1998 Auburn University. All rights reserved. +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by the +## Free Software Foundation; either version 2, or (at your option) any +## later version. +## +## Octave is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +## for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, write to the Free +## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} zp (@var{zer}, @var{pol}, @var{k}, @var{tsam}, @var{inname}, @var{outname}) +## Create system data structure from zero-pole data. +## +## @strong{Inputs} +## @table @var +## @item zer +## vector of system zeros +## @item pol +## vector of system poles +## @item k +## scalar leading coefficient +## @item tsam +## sampling period. default: 0 (continuous system) +## @item inname +## @itemx outname +## input/output signal names (lists of strings) +## @end table +## +## @strong{Outputs} +## sys: system data structure +## +## @strong{Example} +## @example +## octave:1> sys=zp([1 -1],[-2 -2 0],1); +## octave:2> sysout(sys) +## Input(s) +## 1: u_1 +## Output(s): +## 1: y_1 +## zero-pole form: +## 1 (s - 1) (s + 1) +## ----------------- +## s (s + 2) (s + 2) +## @end example +## @end deftypefn + +## Modified by John Ingram July 20, 1996 + +function outsys = zp (zer, pol, k, tsam, inname, outname) + + ## Test for the correct number of input arguments + if ((nargin < 3) || (nargin > 6)) + usage("outsys = zp(zer,pol,k[,tsam,inname,outname])"); + endif + + ## check input format + if( ! (isvector(zer) | isempty(zer) ) ) + error("zer must be a vector or empty"); + endif + if(!isempty(zer)) + zer = reshape(zer,1,length(zer)); # make it a row vector + endif + + if( ! (isvector(pol) | isempty(pol))) + error("pol must be a vector"); + endif + if(!isempty(pol)) + pol = reshape(pol,1,length(pol)); + endif + + if (! isscalar(k)) + error("k must be a scalar"); + endif + + ## Test proper numbers of poles and zeros. The number of poles must be + ## greater than or equal to the number of zeros. + if (length(zer) > length(pol)) + error(["number of poles (", num2str(length(pol)), ... + ") < number of zeros (", num2str(length(zer)),")"]); + endif + + ## Set the system transfer function + outsys.zer = zer; + outsys.pol = pol; + outsys.k = k; + + ## Set the system vector: active = 1, updated = [0 1 0]; + outsys.sys = [1, 0, 1, 0]; + + ## Set defaults + outsys.tsam = 0; + outsys.n = length(pol); + outsys.nz = 0; + outsys.yd = 0; # assume (for now) continuous time outputs + + ## Set the type of system + if (nargin > 3) + if( !isscalar(tsam) ) + error("tsam must be a nonnegative scalar"); + endif + if (tsam < 0) + error("sampling time must be positve") + elseif (tsam > 0) + [outsys.n,outsys.nz] = swap(outsys.n, outsys.nz); + outsys.yd = 1; # discrete-time output + endif + + outsys.tsam = tsam; + endif + + outsys.inname = __sysdefioname__ (1, "u"); + outsys.outname = __sysdefioname__ (1, "y"); + outsys.stname = __sysdefstname__ (outsys.n, outsys.nz); + + ## Set name of input + if (nargin > 4) + ## make sure its a string + if(!isempty(inname)) + if(!iscell(inname)) + inname = {inname}; + endif + if(!is_signal_list(inname)) + error("inname must be a single signal name"); + endif + outsys.inname = inname(1); + endif + endif + + ## Set name of output + if (nargin > 5) + if(!isempty(outname)) + if(!iscell(outname)) + outname = {outname}; + endif + if(!is_signal_list(outname)) + error("outname must be a single signal name"); + endif + outsys.outname = outname(1); + endif + endif + +endfunction