|
From: <par...@us...> - 2012-09-23 08:54:35
|
Revision: 11088
http://octave.svn.sourceforge.net/octave/?rev=11088&view=rev
Author: paramaniac
Date: 2012-09-23 08:54:28 +0000 (Sun, 23 Sep 2012)
Log Message:
-----------
control: work on multi plot slim draft code
Modified Paths:
--------------
trunk/octave-forge/main/control/devel/lsim2.m
Modified: trunk/octave-forge/main/control/devel/lsim2.m
===================================================================
--- trunk/octave-forge/main/control/devel/lsim2.m 2012-09-23 08:38:14 UTC (rev 11087)
+++ trunk/octave-forge/main/control/devel/lsim2.m 2012-09-23 08:54:28 UTC (rev 11088)
@@ -1,4 +1,4 @@
-## Copyright (C) 2009, 2010, 2011 Lukas F. Reichlin
+## Copyright (C) 2009, 2010, 2011, 2012 Lukas F. Reichlin
##
## This file is part of LTI Syncope.
##
@@ -60,7 +60,7 @@
## Author: Lukas Reichlin <luk...@gm...>
## Created: October 2009
-## Version: 0.3
+## Version: 0.4
% function [y_r, t_r, x_r] = lsim (sys, u, t = [], x0 = [], method = "zoh")
function [y_r, t_r, x_r] = lsim (varargin)
@@ -69,19 +69,24 @@
print_usage ();
endif
- sys_idx = cellfun (@isa, varargin, {"lti"}); # look for LTI models
- sys_cell = cellfun (@ss, varargin(sys_idx), "uniformoutput", false); # convert to state-space
+ sys_idx = find (cellfun (@isa, args, {"lti"})); # look for LTI models, 'find' needed for plot styles
+ sys_cell = cellfun (@ss, args(sys_idx), "uniformoutput", false); # convert to state-space
if (! size_equal (sys_cell{:}))
error ("lsim: models must have equal sizes");
endif
- vec_idx = find (cellfun (@is_real_matrix, varargin));
- n_vec = length (vec_idx);
- n_sys = length (sys_cell);
+ mat_idx = find (cellfun (@is_real_matrix, args)); # indices of matrix arguments
+ n_mat = length (mat_idx); # number of vector arguments
+ n_sys = length (sys_cell); # number of LTI systems
+ ## function [y, x_arr] = __linear_simulation__ (sys_dt, u, t, x0)
+
+ [y, x] = cellfun (@__linear_simulation__, sys_dt_cell, {u}, t, {x0}, "uniformoutput", false);
+
+%{
if (! isa (sys, "ss"))
sys = ss (sys); # sys must be proper
endif
@@ -148,9 +153,82 @@
error ("lsim: input vector u must have %d columns", m);
endif
+%}
+
+ if (nargout == 0) # plot information
+ [p, m] = size (sys_cell{1});
+ style_idx = find (cellfun (@ischar, varargin));
+ str = "Linear Simulation Results";
+ outname = get (sys_cell{end}, "outname");
+ outname = __labels__ (outname, "y");
+ colororder = get (gca, "colororder");
+ rc = rows (colororder);
+
+ for k = 1 : n_sys # for every system
+ if (k == n_sys)
+ lim = numel (args);
+ else
+ lim = sys_idx(k+1);
+ endif
+ style = args(style_idx(style_idx > sys_idx(k) & style_idx <= lim));
+ if (isempty (style))
+ color = colororder(1+rem (k-1, rc), :);
+ style = {"color", color};
+ endif
+ if (ct_idx(k)) # continuous-time system
+ for i = 1 : p # for every output
+ subplot (p, 1, i);
+ plot (t{k}, y{k}(:, i), style{:});
+ hold on;
+ grid on;
+ if (k == n_sys)
+ axis tight
+ ylim (__axis_margin__ (ylim))
+ ylabel (outname{i});
+ if (i == 1)
+ title (str);
+ endif
+ endif
+ endfor
+ endfor
+ else # discrete-time system
+ for i = 1 : p # for every output
+ subplot (p, 1, i);
+ stairs (t{k}, y{k}(:, i), style{:});
+ hold on;
+ grid on;
+ if (k == n_sys)
+ axis tight;
+ ylim (__axis_margin__ (ylim))
+ ylabel (outname{i});
+ if (i == 1)
+ title (str);
+ endif
+ endif
+ endfor
+ endfor
+ endif
+ endfor
+ xlabel ("Time [s]");
+ if (p == 1 && m == 1)
+ legend (sysname)
+ endif
+ hold off;
+ endif
+
+endfunction
+
+
+function [y, x_arr] = __linear_simulation__ (sys_dt, u, t, x0)
+
+ [A, B, C, D] = ssdata (sys_dt);
+ [p, m] = size (D); # number of outputs and inputs
+ n = rows (A); # number of states
+ len_t = length (t);
+
## preallocate memory
- y = zeros (trows, p);
- x_arr = zeros (trows, n);
+ y = zeros (len_t, p);
+ x_arr = zeros (len_t, n);
## initial conditions
if (isempty (x0))
@@ -162,46 +240,13 @@
x = reshape (x0, [], 1); # make sure that x is a column vector
## simulation
- for k = 1 : trows
+ for k = 1 : len_t
y(k, :) = C * x + D * u(k, :).';
x_arr(k, :) = x;
x = A * x + B * u(k, :).';
endfor
- if (nargout == 0) # plot information
- str = ["Linear Simulation Results of ", inputname(1)];
- outname = get (sys, "outname");
- outname = __labels__ (outname, "y_");
- if (discrete) # discrete system
- for k = 1 : p
- subplot (p, 1, k);
- stairs (t, y(:, k));
- grid ("on");
- if (k == 1)
- title (str);
- endif
- ylabel (sprintf ("Amplitude %s", outname{k}));
- endfor
- xlabel ("Time [s]");
- else # continuous system
- for k = 1 : p
- subplot (p, 1, k);
- plot (t, y(:, k));
- grid ("on");
- if (k == 1)
- title (str);
- endif
- ylabel (sprintf ("Amplitude %s", outname{k}));
- endfor
- xlabel ("Time [s]");
- endif
- else # return values
- y_r = y;
- t_r = t;
- x_r = x_arr;
- endif
-
endfunction
-## TODO: add test cases
\ No newline at end of file
+## TODO: add test cases
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|