home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / control / sysrepdemo.m < prev    next >
Encoding:
Text File  |  1999-12-15  |  22.5 KB  |  497 lines

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