/usr/share/octave/packages/control-2.6.2/__time_response__.m is in octave-control 2.6.2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | ## Copyright (C) 2009-2014 Lukas F. Reichlin
##
## This file is part of LTI Syncope.
##
## LTI Syncope 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 3 of the License, or
## (at your option) any later version.
##
## LTI Syncope 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 LTI Syncope. If not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## Common code for the time response functions step, impulse and initial.
## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
## Created: October 2009
## Version: 0.4.1
function [y, t, x] = __time_response__ (response, args, sysname, plotflag)
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 ("%s: models must have equal sizes", response);
endif
vec_idx = find (cellfun (@is_real_matrix, args)); # indices of vector arguments
n_vec = length (vec_idx); # number of vector arguments
n_sys = length (sys_cell); # number of LTI systems
tfinal = [];
dt = [];
x0 = [];
## extract tfinal/t, dt, x0 from args
if (strcmpi (response, "initial"))
if (n_vec < 1)
error ("initial: require initial state vector 'x0'");
else # initial state vector x0 specified
arg = args{vec_idx(1)};
if (is_real_vector (arg))
x0 = arg;
else
error ("initial: initial state vector 'x0' must be a vector of real values");
endif
if (n_vec > 1) # tfinal or time vector t specified
arg = args{vec_idx(2)};
if (issample (arg))
tfinal = arg;
elseif (isempty (arg))
## tfinal = []; # nothing to do here
elseif (is_real_vector (arg))
dt = abs (arg(2) - arg(1)); # assume that t is regularly spaced
tfinal = arg(end);
else
warning ("initial: argument number %d ignored", vec_idx(2));
endif
if (n_vec > 2) # sampling time dt specified
arg = args{vec_idx(3)};
if (issample (arg))
dt = arg;
else
warning ("initial: argument number %d ignored", vec_idx(3));
endif
if (n_vec > 3)
warning ("initial: ignored");
endif
endif
endif
endif
else # step or impulse response
if (n_vec > 0) # tfinal or time vector t specified
arg = args{vec_idx(1)};
if (issample (arg))
tfinal = arg;
elseif (isempty (arg))
## tfinal = []; # nothing to do here
elseif (is_real_vector (arg))
dt = abs (arg(2) - arg(1)); # assume that t is regularly spaced
tfinal = arg(end);
else
warning ("%s: argument number %d ignored", response, vec_idx(1));
endif
if (n_vec > 1) # sampling time dt specified
arg = args{vec_idx(2)};
if (issample (arg))
dt = arg;
else
warning ("%s: argument number %d ignored", response, vec_idx(2));
endif
if (n_vec > 2)
warning ("%s: ignored", response);
endif
endif
endif
endif
## TODO: share common code between initial and step/impulse
[tfinal, dt] = cellfun (@__sim_horizon__, sys_cell, {tfinal}, {dt}, "uniformoutput", false);
tfinal = max ([tfinal{:}]);
ct_idx = cellfun (@isct, sys_cell);
sys_dt_cell = sys_cell;
tmp = cellfun (@c2d, sys_cell(ct_idx), dt(ct_idx), {"zoh"}, "uniformoutput", false);
sys_dt_cell(ct_idx) = tmp;
## time vector
t = @cellfun (@(dt) reshape (0 : dt : tfinal, [], 1), dt, "uniformoutput", false);
## function [y, x_arr] = __initial_response__ (sys, sys_dt, t, x0)
## function [y, x_arr] = __step_response__ (sys_dt, t)
## function [y, x_arr] = __impulse_response__ (sys, sys_dt, t)
## function [y, x_arr] = __ramp_response__ (sys_dt, t)
switch (response)
case "initial"
[y, x] = cellfun (@__initial_response__, sys_dt_cell, t, {x0}, "uniformoutput", false);
case "step"
[y, x] = cellfun (@__step_response__, sys_dt_cell, t, "uniformoutput", false);
case "impulse"
[y, x] = cellfun (@__impulse_response__, sys_cell, sys_dt_cell, t, "uniformoutput", false);
case "ramp"
[y, x] = cellfun (@__ramp_response__, sys_dt_cell, t, "uniformoutput", false);
otherwise
error ("time_response: invalid response type");
endswitch
if (plotflag) # display plot
[p, m] = size (sys_cell{1});
switch (response)
case "initial"
str = "Response to Initial Conditions";
cols = 1;
## yfinal = zeros (p, 1);
case "step"
str = "Step Response";
cols = m;
## yfinal = dcgain (sys_cell{1});
case "impulse"
str = "Impulse Response";
cols = m;
## yfinal = zeros (p, m);
case "ramp"
str = "Ramp Response";
cols = m;
otherwise
error ("time_response: invalid response type");
endswitch
style_idx = find (cellfun (@ischar, args));
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
for j = 1 : cols # for every input (except for initial where cols=1)
if (p != 1 || cols != 1)
subplot (p, cols, (i-1)*cols+j);
endif
plot (t{k}, y{k}(:, i, j), style{:});
hold on;
grid on;
if (k == n_sys)
axis tight
ylim (__axis_margin__ (ylim))
if (j == 1)
ylabel (outname{i});
if (i == 1)
title (str);
endif
endif
endif
endfor
endfor
else # discrete-time system
for i = 1 : p # for every output
for j = 1 : cols # for every input (except for initial where cols=1)
if (p != 1 || cols != 1)
subplot (p, cols, (i-1)*cols+j);
endif
stairs (t{k}, y{k}(:, i, j), style{:});
hold on;
grid on;
if (k == n_sys)
axis tight;
ylim (__axis_margin__ (ylim))
if (j == 1)
ylabel (outname{i});
if (i == 1)
title (str);
endif
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] = __initial_response__ (sys_dt, t, x0)
[F, G, C, D] = ssdata (sys_dt); # system must be proper
n = rows (F); # number of states
m = columns (G); # number of inputs
p = rows (C); # number of outputs
l_t = length (t);
## preallocate memory
y = zeros (l_t, p);
x_arr = zeros (l_t, n);
## initial conditions
x = reshape (x0, [], 1); # make sure that x is a column vector
if (n != length (x0) || ! is_real_vector (x0))
error ("initial: x0 must be a real vector with %d elements", n);
endif
## simulation
for k = 1 : l_t
y(k, :) = C * x;
x_arr(k, :) = x;
x = F * x;
endfor
endfunction
function [y, x_arr] = __step_response__ (sys_dt, t)
[F, G, C, D] = ssdata (sys_dt); # system must be proper
n = rows (F); # number of states
m = columns (G); # number of inputs
p = rows (C); # number of outputs
l_t = length (t);
## preallocate memory
y = zeros (l_t, p, m);
x_arr = zeros (l_t, n, m);
for j = 1 : m # for every input channel
## initial conditions
x = zeros (n, 1);
u = zeros (m, 1);
u(j) = 1;
## simulation
for k = 1 : l_t
y(k, :, j) = C * x + D * u;
x_arr(k, :, j) = x;
x = F * x + G * u;
endfor
endfor
endfunction
function [y, x_arr] = __impulse_response__ (sys, sys_dt, t)
[~, B] = ssdata (sys);
[F, G, C, D, dt] = ssdata (sys_dt); # system must be proper
dt = abs (dt); # use 1 second if tsam is unspecified (-1)
discrete = ! isct (sys);
n = rows (F); # number of states
m = columns (G); # number of inputs
p = rows (C); # number of outputs
l_t = length (t);
## preallocate memory
y = zeros (l_t, p, m);
x_arr = zeros (l_t, n, m);
for j = 1 : m # for every input channel
## initial conditions
u = zeros (m, 1);
u(j) = 1;
if (discrete)
x = zeros (n, 1); # zero by definition
y(1, :, j) = D * u / dt;
x_arr(1, :, j) = x;
x = G * u / dt;
else
x = B * u; # B, not G!
y(1, :, j) = C * x;
x_arr(1, :, j) = x;
x = F * x;
endif
## simulation
for k = 2 : l_t
y (k, :, j) = C * x;
x_arr(k, :, j) = x;
x = F * x;
endfor
endfor
if (discrete)
y *= dt;
x_arr *= dt;
endif
endfunction
function [y, x_arr] = __ramp_response__ (sys_dt, t)
[F, G, C, D] = ssdata (sys_dt); # system must be proper
n = rows (F); # number of states
m = columns (G); # number of inputs
p = rows (C); # number of outputs
l_t = length (t);
## preallocate memory
y = zeros (l_t, p, m);
x_arr = zeros (l_t, n, m);
for j = 1 : m # for every input channel
## initial conditions
x = zeros (n, 1);
u = zeros (m, l_t);
u(j, :) = t;
## simulation
for k = 1 : l_t
y(k, :, j) = C * x + D * u(:, k);
x_arr(k, :, j) = x;
x = F * x + G * u(:, k);
endfor
endfor
endfunction
function [tfinal, dt] = __sim_horizon__ (sys, tfinal, Ts)
## code based on __stepimp__.m of Kai P. Mueller and A. Scottedward Hodel
TOL = 1.0e-10; # values below TOL are assumed to be zero
N_MIN = 50; # min number of points
N_MAX = 2000; # max number of points
N_DEF = 1000; # default number of points
T_DEF = 10; # default simulation time
ev = pole (sys);
n = length (ev); # number of states/poles
continuous = isct (sys);
discrete = ! continuous;
if (discrete)
dt = Ts = abs (get (sys, "tsam"));
## perform bilinear transformation on poles in z
for k = 1 : n
pol = ev(k);
if (abs (pol + 1) < TOL)
ev(k) = 0;
else
ev(k) = 2 / Ts * (pol - 1) / (pol + 1);
endif
endfor
endif
## remove poles near zero from eigenvalue array ev
nk = n;
for k = 1 : n
if (abs (real (ev(k))) < TOL)
ev(k) = 0;
nk -= 1;
endif
endfor
if (nk == 0)
if (isempty (tfinal))
tfinal = T_DEF;
endif
if (continuous)
dt = tfinal / N_DEF;
endif
else
ev = ev(find (ev));
ev_max = max (abs (ev));
if (continuous)
dt = 0.2 * pi / ev_max;
endif
if (isempty (tfinal))
ev_min = min (abs (real (ev)));
tfinal = 5.0 / ev_min;
## round up
yy = 10^(ceil (log10 (tfinal)) - 1);
tfinal = yy * ceil (tfinal / yy);
endif
if (continuous)
N = tfinal / dt;
if (N < N_MIN)
dt = tfinal / N_MIN;
endif
if (N > N_MAX)
dt = tfinal / N_MAX;
endif
endif
endif
if (continuous && ! isempty (Ts)) # catch case cont. system with dt specified
dt = Ts;
endif
endfunction
|