home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / control / sysrepdm.m < prev    next >
Text File  |  1999-04-29  |  23KB  |  492 lines

  1. # Copyright (C) 1996,1998 A. Scottedward Hodel 
  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. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function sysrepdm()
  18.  
  19. # Octave Controls toolbox demo: System representation
  20.  
  21. # Written by A. S. Hodel June 1995
  22. # Revised Aug 1995 for system data structure format
  23.  
  24.   save_val = page_screen_output;
  25.   page_screen_output = 1;
  26.  
  27.   disp('System representation demo:')
  28.   num = [5 -1];
  29.   denom = [1 -2 6];
  30.   a = b = c = [];
  31.   syschoice = -1;
  32.   ch_init = 2;
  33.   ch_extract = ch_init+1;
  34.   ch_update = ch_extract+1;
  35.   ch_view = ch_update+1;
  36.   ch_details = ch_view+1;
  37.   ch_quit = ch_details+1;
  38.   while(syschoice != ch_quit)
  39.    disp(" ")
  40.     syschoice = menu('Octave System Representation Menu', ...
  41.       "General overview of system representation (DO THIS FIRST)", ...
  42.       "Initialize a system (ss2sys, tf2sys, zp2sys)", ...
  43.       "Extract data from a system(sys2ss, sys2tf, sys2zp, etc.)", ...
  44.       "Update internal representation (sysupdat)", ...
  45.       "View the internal contents of a system (sysout)", ...
  46.       "Details of internal representation", ...
  47.       "Return to main menu");
  48.     if(syschoice == 1)  # general overview
  49.       disp("The Octave Control Systems Toolbox (OCST) was designed to")
  50.       disp("provide a simple user interface to a powerful set of tools.")
  51.       disp(' ')
  52.       disp('               ----------')
  53.       disp(' input(s) ---->| System | ---> output(s) ')
  54.       disp('               ----------')
  55.       disp(' ')
  56.       disp("Like other computer-aided control system design tools, the OCST")
  57.       disp("enables users to enter their descriptions of dynamic systems in ")
  58.       disp("their preferred form (state space, transfer function, or ");
  59.       disp("zero-pole format).  ");
  60.       disp("The OCST stores system descriptions in a single variable data ");
  61.       disp("structure that allows for continuous time, discrete-time, or mixed ");
  62.       disp("(sampled-data) systems.  ");
  63.       disp(" ");
  64.       disp("This single variable description of dynamic systems greatly simplifies ");
  65.       disp("both the code of the OCST as well as the user interface, since only")
  66.       disp("one variable is passed per system, regardless of the  internal ")
  67.       disp("representation used in the data structure.  As a result, the ");
  68.       disp("likelihood of user error is greatly reduced when calling OCST")
  69.       disp("functions.  Further, all OCST functions have been written to")
  70.       disp("provide meaningful warning or error message to assist the user")
  71.       disp("in correcting their programming errors while using the OCST.")
  72.       disp("The details of the internal representation can be seen in ");
  73.       disp(["menu option ",num2str(ch_details)]);
  74.       disp("The data structure used in the OCST is called a \"system data structure.\"");
  75.       disp("A system data structure is contstructed with one of:")
  76.       disp("   fir2sys (FIR transfer function to system)")
  77.       disp("   ss2sys (state space matrices to system)")
  78.       disp("   tf2sys (SISO transfer function to system)")
  79.       disp("   zp2sys (SISO zero/pole/leading coefficient to system)")
  80.       disp(" ")
  81.       disp(["These functions are discussed in in menu option ",num2str(ch_init)])
  82.       disp("The data in a system may be extracted using ")
  83.       disp("   sys2fir (FIR transfer function from system")
  84.       disp("   sys2ss (state space matrices from system)")
  85.       disp("   sys2tf (SISO transfer function from system)")
  86.       disp("   sys2zp (SISO zero/pole/leading coefficient from system)")
  87.       disp(" ")
  88.       disp(["These functions are discussed in menu option ", ...
  89.     num2str(ch_extract)]);
  90.       disp("Other options discussed under this menu are updating the internal")
  91.       disp("representation form of a system data structure with sysupdat and printing")
  92.       disp("the description of a dynamic system to the screen with sysout.")
  93.       disp(" ")
  94.       disp("Once the user is familiar with these commands, the rest of the ")
  95.       disp("OCST package will be quite easy to use.")
  96.     elseif(syschoice == ch_init) % Initialize
  97.       disp("Initialization of a system:");
  98.       disp(' ');
  99.       formopt = 0;
  100.       while(formopt != 4)
  101.       disp("Three data formats may be used to initialize a system:")
  102.         formopt = menu("System data structure initialization menu", ...
  103.         "State space form       (ss2sys)", ...
  104.         "Transfer function form (tf2sys)", ...
  105.         "zero-pole form         (zp2sys)", ...
  106.             "Return to System representation menu");
  107.         if(formopt == 1)
  108.           disp('State space representation of a system is based on the usual')
  109.           disp('multi-variable differential equations')
  110.           disp(' ')
  111.           disp('  . ')
  112.           disp('  x = A x + B u      -or -   x(k+1) = A x(k) + B u(k) ')
  113.           disp('  y = C x + D u                y(k) = C x(k) + D u(k) ')
  114.           disp(' ')
  115.           disp('for matrices A, B, C, D of appropriate dimension.')
  116.           disp(' ')
  117.           ssopt = 0;
  118.           ssquit = 5;
  119.           while(ssopt < ssquit)
  120.             ssopt = menu("State space initialization examples", ...
  121.         "Double integrator example", ...
  122.         "Double delay (discrete-time) example", ...
  123.         "Summing junction (D-matrix only) example", ...
  124.         "ss2sys details (help ss2sys)", ...
  125.         "return to system initialization menu", ...
  126.         "return to system representation main menu");
  127.             if(ssopt == 1)
  128.               disp("Example: construct a system representation of a")
  129.               disp("double integrator via state-space form")
  130.               cmd = "a = [0 1; 0 0];";
  131.               run_cmd
  132.               cmd = "b = [0 ; 1];";
  133.               run_cmd
  134.               cmd = "c = [1 0];";
  135.               run_cmd
  136.               cmd = "sys = ss2sys(a,b,c);";
  137.               run_cmd
  138.               disp("The state space form of the system is seen via sysout:")
  139.               cmd = "sysout(sys)";
  140.               run_cmd
  141.               disp("Notice that the Octave controls  toolbox automatically")
  142.               disp("assigns names to the states, inputs and outputs,")
  143.               disp("and that the D matrix was filled in automatically.")
  144.               disp("We verify that it's a double integrator via sysout:")
  145.               cmd = "sysout(sys,""tf"")";
  146.               run_cmd
  147.               prompt
  148.             elseif(ssopt == 2)
  149.               disp("Example: discrete-time double-delay:")
  150.               disp("This example is identical to the double-integrator,")
  151.               disp("except that it is a discrete-time system, and so has")
  152.               disp("a sampling interval.  We arbitrarily select T=1e-3.");
  153.               cmd = "a = [0 1; 0 0];";
  154.               run_cmd
  155.               cmd = "b = [0 ; 1];";
  156.               run_cmd
  157.               cmd = "c = [1 0];";
  158.               run_cmd
  159.               cmd = "sys=ss2sys(a,b,c,[],1e-3);";
  160.               run_cmd
  161.               cmd = "sysout(sys)";
  162.               run_cmd
  163.               disp("Notice that the D matrix was filled in automatically.")
  164.               disp("This is done if D is input as the empty matrix.")
  165.           disp(" ")
  166.           disp("Notice also that the output y_1 is labelled as a discrete")
  167.           disp("output.  The OCST data structure keeps track of states")
  168.           disp("and output signals that are produced by the discrete-time")
  169.           disp("portion of a system.  Discrete states and outputs are ")
  170.           disp("implemented as shown in the block diagram below:")
  171.           disp(" ")
  172.           disp(" ")
  173.           disp("       _________   ________ x(kT)  ________________")
  174.           disp("f(t)-->|sampler|-->| delay |----->|zero order hold| -->")
  175.           disp("       ---------   --------        ----------------")
  176.           disp(" ")
  177.           disp("        ___________    _______________")
  178.           disp("f(t)-->| sampler |-->|zero-order hold| --> y(discrete)")
  179.           disp("        -----------    ---------------")
  180.           disp(" ")
  181.           disp("where f(t) is an input signal to either the output or the")
  182.           disp(" discrete state.")
  183.           disp(" ")
  184.           disp("The OCST does not implement samplers on inputs to continuous")
  185.           disp("time states (i.e., there are no samplers implicit in the B")
  186.           disp("or D matrices unless there are corresponding discrete")
  187.               disp("outputs or states.  The OCST provides warning messages when")
  188.           disp("if this convention is violated.")
  189.           prompt
  190.             elseif(ssopt == 3)
  191.               disp("A summing junction that computes e(t) = r(t) - y(t) may be");
  192.               disp("constructed as follows:");
  193.               disp("First, we set the matrix D:")
  194.               cmd = "D = [1 -1];";
  195.               run_cmd
  196.               disp("ss2sys allows the initialization of signal and state names")
  197.               disp("(see option 4), so we initialize these as follows:")
  198.               cmd = "inname = list(\"r(t)\",\"y(t)\");";
  199.               run_cmd;
  200.               cmd = "outname = \"e(t)\";";
  201.           run_cmd
  202.               disp("Since the system is continous time and without states,")
  203.               disp("the ss2sys inputs tsam, n, and nz are all zero:")
  204.               cmd = "sys = ss2sys([],[],[],D,0,0,0,[],inname,outname);";
  205.               run_cmd
  206.               disp("The resulting system is:")
  207.               cmd = "sysout(sys)";
  208.               run_cmd
  209.               disp("A discrete-time summing block can be implemented by setting")
  210.           disp("the sampling time positive:")
  211.               cmd = "sys = ss2sys([],[],[],D,1e-3,0,0,[],inname,outname);";
  212.               run_cmd
  213.               disp("The resulting system is:")
  214.               cmd = "sysout(sys)";
  215.               run_cmd
  216.               prompt
  217.             elseif(ssopt == 4)
  218.               help ss2sys
  219.           disp(" ")
  220.           disp(" ")
  221.               disp("Notice that state-space form allows a single system to have")
  222.               disp("both continuous and discrete-time states and to have both continuous")
  223.               disp("and discrete-time outputs.  Since it's fairly easy to make an")
  224.               disp("error when mixing systems of this form, the Octave controls")
  225.               disp("toolbox attempts to print warning messages whenever something")
  226.               disp("questionable occurs.")
  227.         elseif(ssopt == 6)
  228.           formopt = 4;        # return to main menu
  229.             endif
  230.           endwhile
  231.         elseif(formopt == 2)
  232.       tfopt = 0;
  233.           while(tfopt < 5)
  234.             tfopt = menu("Transfer function initialization menu", ...
  235.         "Continuous time initialization" , ...
  236.         "Discrete time initialization" , ...
  237.         "User specified signal names" , ...
  238.         "tf2sys details (help tf2sys)", ...
  239.         "Return to system initialization menu", ...
  240.         "Return to system representation main menu");
  241.             if(tfopt == 1) # continuous time
  242.               disp("A transfer function is represented by vectors of the")
  243.               disp("coefficients of the numerator and denominator polynoms");
  244.               disp(" ")
  245.               disp("For example: the transfer function");
  246.               disp(" ");
  247.               num = [5 -1];
  248.               denom = [1 -2 6];
  249.               tfout(num,denom);
  250.               disp(" ")
  251.               disp("is generated by the following commands:")
  252.               cmd = "num = [5 -1]";
  253.               run_cmd
  254.               cmd = "denom = [1 -2 6]";
  255.               run_cmd
  256.               cmd = "sys = tf2sys(num,denom);";
  257.               run_cmd
  258.           disp("alternatively, the system can be generated in a single command:");
  259.           cmd = "sys = tf2sys([5 -1],[1 -2 6]);";
  260.               run_cmd
  261.               disp("Notice the output of sys: it is an Octave data structure.")
  262.               disp("The details of its member variables are explained under")
  263.               disp("System Representation Menu option 5 (the details of system form)")
  264.           disp(" ");
  265.               disp("The data structure can be observed with the sysout command:")
  266.               cmd = "sysout(sys)";
  267.               run_cmd
  268.               disp("Notice that Octave assigns names to inputs and outputs.")
  269.           disp("The user may manually select input and output names; see option 3");
  270.           prompt
  271.             elseif(tfopt == 2) # discrete time
  272.               disp("A transfer function is represented by vectors of the")
  273.               disp("coefficients of the numerator and denominator polynoms");
  274.               disp("Discrete-time transfer functions require ")
  275.               disp("the additional parameter of a sampling period:")
  276.               cmd = "sys=tf2sys([5 -1],[1 2 -6],1e-3);";
  277.               run_cmd
  278.               cmd = "sysout(sys)";
  279.               run_cmd
  280.           disp("The OCST recognizes discrete-time transfer functions and")
  281.           disp("accordingly prints them with the frequency domain variable z.");
  282.               disp("Notice that Octave assigns names to inputs and outputs.")
  283.           disp("The user may set input and output names; see option 3");
  284.             elseif(tfopt == 3) # user specified names
  285.               disp("The OCST requires all signals to have names.  The OCST assigned default");
  286.           disp("names to the signals in the other examples.  We may initialize a transfer");
  287.           disp("function with user-specified names as follows: Consider a simple ")
  288.           disp("double-integrator model of aircraft roll dynamics with ")
  289.           disp("input \"aileron angle\" and output \"theta\".  A ")
  290.           disp("system for this model is generated by the command")
  291.           cmd = "aircraft=tf2sys(1,[1 0 0],0,\"aileron angle\",\"theta\");";          run_cmd
  292.           disp("The sampling  time parameter 0 indicates that the system")
  293.           disp("is continuous time.  A positive sampling time indicates a")
  294.           disp("discrete-time system (or sampled data system).")
  295.           cmd = "sysout(aircraft)";
  296.           run_cmd
  297.           disp("Notice that the user-selected signal names are listed.")
  298.           disp("These signal names are used in OCST plots and design functions.");
  299.           disp("(Run the frequency response demo to see an example of the use of ");
  300.           disp("signal names in plots.)")
  301.           prompt
  302.             elseif(tfopt == 4) # help
  303.               help  tf2sys
  304.           prompt
  305.             elseif(tfopt == 6) # return to main menu
  306.           formopt = 4;
  307.             endif
  308.           endwhile
  309.         elseif (formopt == 3)
  310.       zpopt = 0;
  311.           while(zpopt < 5)
  312.             zpopt = menu("Zero-pole initialization menu", ...
  313.         "Continuous time initialization" , ...
  314.         "Discrete time initialization" , ...
  315.         "User specified signal names" , ...
  316.         "zp2sys details (help zp2sys)", ...
  317.         "Return to system initialization menu", ...
  318.         "Return to system representation main menu");
  319.             if(zpopt == 1) # continuous time
  320.               disp("A zero-pole form representation of a system includes vectors")
  321.               disp("of the system poles and zeros and a scalar leading coefficient.");
  322.               disp(" ")
  323.               disp("For example: the transfer function");
  324.               disp(" ");
  325.               k = 5;
  326.               num = [5 -1];
  327.               denom = [1 -2 6];
  328.               zpout(num,denom,k);
  329.               disp(" ")
  330.               disp("is generated by the following commands:")
  331.               cmd = "num = [5 -1]";
  332.               run_cmd
  333.               cmd = "denom = [1 -2 6]";
  334.               run_cmd
  335.           cmd = "k = 5";
  336.           run_cmd
  337.               cmd = "sys = zp2sys(num,denom,k);";
  338.               run_cmd
  339.           disp("alternatively, the system can be generated in a single command:");
  340.           cmd = "sys = zp2sys([5 -1],[1 -2 6],5);";
  341.               run_cmd
  342.               disp("Notice the output of sys: it is an Octave data structure.")
  343.               disp("The details of its member variables are explained under")
  344.               disp("System Representation Menu option 5 (the details of system form)")
  345.           disp(" ");
  346.               disp("The data structure can be observed with the sysout command:")
  347.               cmd = "sysout(sys)";
  348.               run_cmd
  349.               disp("Notice that Octave assigns names to inputs and outputs.")
  350.           disp("The user may manually select input and output names; see option 3");
  351.           prompt
  352.             elseif(zpopt == 2) # discrete time
  353.               disp("A zero-pole form representation of a system includes vectors")
  354.               disp("of the system poles and zeros and a scalar leading coefficient.");
  355.               disp(" ")
  356.               disp("Discrete-time systems require the additional parameter of a sampling period:")
  357.               cmd = "sys=zp2sys([5 -1],[1 2 -6],5,1e-3);";
  358.               run_cmd
  359.               cmd = "sysout(sys)";
  360.               run_cmd
  361.           disp("The OCST recognizes discrete-time transfer functions and")
  362.           disp("accordingly prints them with the frequency domain variable z.");
  363.               disp("Notice that Octave assigns names to inputs and outputs.")
  364.           disp("The user may set input and output names; see option 3");
  365.             elseif(zpopt == 3) # user specified names
  366.               disp("The OCST requires all signals to have names.  The OCST assigned default");
  367.           disp("names to the signals in the other examples.  We may initialize a transfer");
  368.           disp("function with user-specified names as follows: Consider a simple ")
  369.           disp("double-integrator model of aircraft roll dynamics with ")
  370.           disp("input \"aileron angle\" and output \"theta\".  A ")
  371.           disp("system for this model is generated by the command")
  372.           cmd = "aircraft=zp2sys([],[0 0],1,0,\"aileron angle\",\"theta\");";          run_cmd
  373.           disp("The sampling  time parameter 0 indicates that the system")
  374.           disp("is continuous time.  A positive sampling time indicates a")
  375.           disp("discrete-time system (or sampled data system).")
  376.           cmd = "sysout(aircraft)";
  377.           run_cmd
  378.           disp("Notice that the user-selected signal names are listed.")
  379.           disp("These signal names are used in OCST plots and design functions.");
  380.           disp("(Run the frequency response demo to see an example of the use of ");
  381.           disp("signal names in plots.)")
  382.           prompt
  383.             elseif(zpopt == 4) # help
  384.               help  zp2sys
  385.           prompt
  386.             elseif(zpopt == 6) # return to main menu
  387.           formopt = 4;
  388.             endif
  389.           endwhile
  390.         endif
  391.       endwhile
  392.     elseif(syschoice == ch_extract)  # extract system information
  393.       disp("Extract information from a system data structure in a selected format:")
  394.       disp("The actions of operations ss2sys, tf2sys, and zp2sys are reversed by")
  395.       disp("respective functions sys2ss, sys2tf, and sys2zp.  The latter two");
  396.       disp("functions are applicable only to SISO systems.")
  397.       formopt = 0;
  398.       while(formopt != 8)
  399.         formopt = menu("Extract system information", ...
  400.         "in state space form       (sys2ss)", ...
  401.         "in transfer function form (sys2tf)", ...
  402.         "in zero pole form         (sys2zp)", ...
  403.         "signal names       (sysgetsg,syssetsg)", ...
  404.         "sampling time         (sysgetts)", ...
  405.         "signal dimensions  (sysdimen)", ...
  406.         "primary system type   (sysgetty)", ...
  407.         "Return to system representation menu");
  408.         if(formopt == 1)
  409.       help sys2ss
  410.     elseif(formopt == 2)
  411.       help sys2tf
  412.     elseif(formopt == 3)
  413.       help sys2zp
  414.         elseif(formopt == 4)
  415.           help sysgetsg
  416.           cmd="sys=ss2sys(rand(4),rand(4,2),rand(3,4));";
  417.           run_cmd
  418.           printf("Example: All signals names can be extracted by\n");
  419.           cmd = "[Ast,Ain,Aout,Ayd] = sysgetsg(sys)";
  420.           run_cmd
  421.           printf("Example: Input signal names can be extracted as\n");
  422.           cmd = "Ain = sysgetsg(sys,\"in\")";
  423.           run_cmd
  424.           printf("Example: The name of output signal 2 can be extracted as\n");
  425.           cmd = "Aout = sysgetsg(sys,\"out\",2)";
  426.           run_cmd
  427.           printf("\nNotice that Aout is returned as a list; the signal name\n");
  428.           printf("itself is obtained by specifying the input parameter strflg\n");
  429.           cmd = "Aout = sysgetsg(sys,\"out\",2,1)";
  430.       run_cmd
  431.           prompt
  432.           cmd = "help syssetsg";
  433.           run_cmd
  434.           printf("Example: set input 2 name to \"motor voltage\"\n");
  435.           cmd = "sys = syssetsg(sys,\"in\",\"motor voltage\",2); sysout(sys)";
  436.           run_cmd
  437.           
  438.           printf("Other syssetsg demos are in the Block diagram demo program bddemo\n");
  439.         elseif(formopt == 5)
  440.           help sysgetts
  441.         elseif(formopt == 6)
  442.           help sysdimen
  443.         elseif(formopt == 7)
  444.           help sysgetty
  445.     endif
  446.     prompt
  447.       endwhile
  448.     elseif(syschoice== ch_update)
  449.       disp("The OCST system data structure format will store a system in the same format")
  450.       disp("as that with which it was initialized.  For example, consider the following:")
  451.       cmd = "sys=zp2sys([1 2],[3 4 5],6)";
  452.       run_cmd
  453.       disp(" ")
  454.       disp("Notice the internal variables in the structure include zer, pol, and k,")
  455.       disp("the required variables for zero-pole form.  We can update the system")
  456.       disp("to include state-space form as follows:")
  457.       cmd = "sys = sysupdat(sys,\"ss\")";
  458.       run_cmd
  459.       disp(" ")
  460.       disp("Now the sys data structure includes variables a, b, c, and d, as well")
  461.       disp("the default state names stname.  sysupdat is usually used internally in")
  462.       disp("the OCST, but can be used manually if desired.  A full description of")
  463.       disp("sysupdat is as follows:")
  464.       help sysupdat
  465.       prompt
  466.     elseif(syschoice == ch_view)
  467.       disp("The sysout command can be used to view a system in any desired format.")
  468.       disp("For example, consider the system created as follows:")
  469.       cmd = "aircraft=zp2sys(1,[0 0],1,0,\"aileron angle\",\"theta\");";          run_cmd
  470.       disp("The system may be viewed in its default format (zero-pole) as follows")
  471.       cmd = "sysout(aircraft)";
  472.       run_cmd
  473.       disp(" ")
  474.       disp("The system may be viewed in state-space or transfer function form as well:")
  475.       cmd = "sysout(aircraft,\"ss\")";
  476.       run_cmd
  477.       cmd = "sysout(aircraft,\"tf\")";
  478.       run_cmd
  479.       disp("A complete description of sysout is below:")
  480.       help sysout
  481.       prompt
  482.     elseif(syschoice == ch_details)
  483.       packedf   
  484.     endif
  485.  
  486.   endwhile
  487.   page_screen_output = save_val;
  488. endfunction
  489.     
  490.