Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[9003f5]: inst / initial.m Maximize Restore History

Download this file

initial.m    284 lines (253 with data), 7.7 kB

  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
## Copyright (C) 1996, 1998, 2000, 2003, 2004, 2005, 2006, 2007
## Auburn University. All rights reserved.
## Copyright (C) 2009 Lukas Reichlin. All rights reserved.
##
## This program 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.
##
## This program 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 this program; see the file COPYING. If not, see
## <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn{Function File} {[@var{y}, @var{t}, @var{x}] =} initial (@var{sys}, @var{x0})
## @deftypefnx{Function File} {[@var{y}, @var{t}, @var{x}] =} initial (@var{sys}, @var{x0}, @var{tfinal})
## @deftypefnx{Function File} {[@var{y}, @var{t}, @var{x}] =} initial (@var{sys}, @var{x0}, @var{tfinal}, @var{dt})
## Initial condition response of state-space model.
## If no output arguments are given, the response is printed on the screen;
## otherwise, the response is computed and returned.
##
## @strong{Inputs}
## @table @var
## @item sys
## System data structure. Must be either purely continuous or discrete;
## see @code{is_digital}.
## @item x0
## Vector of initial conditions for each state.
## @item tfinal
## Optional simulation horizon. If not specified, it will be calculated by
## the poles of the system to reflect adequately the response transients.
## @item dt
## Optional sampling time. Be sure to choose it small enough to capture transient
## phenomena. If not specified, it will be calculated by the poles of the system.
## @end table
##
## @strong{Outputs}
## @table @var
## @item y
## Output response array. Has as many rows as time samples (length of t)
## and as many columns as outputs.
## @item t
## Time row vector.
## @item x
## State trajectories array. Has length(t) rows and as many columns as states.
## @end table
##
## @seealso{impulse, lsim, step}
## @example
## @group
## .
## Continuous Time: x = A x , y = C x , x(0) = x0
##
## Discrete Time: x[k+1] = A x[k] , y[k] = C x[k] , x[0] = x0
## @end group
## @end example
## @end deftypefn
## Author: Lukas Reichlin <lukas.reichlin@swissonline.ch>
## Created: August 16, 2009
## based on __stepimp__.m of Kai P. Mueller and A. Scottedward Hodel
## Version: 0.2
function [y_r, t_r, x_r] = initial (sys, x_0, t_final, dt)
## check whether arguments are OK
if (nargin < 2 || nargin > 4)
print_usage ();
endif
if (! isstruct (sys))
error ("initial: first argument must be a system data structure");
endif
if (! isvector (x_0))
error ("initial: second argument must be a vector");
endif
## get system information
sys = sysupdate (sys, "ss");
digital = is_digital (sys, 2);
[n_c, n_d, n_in, n_out] = sysdimensions (sys);
n_st = n_c + n_d; # number of states
if (digital == -1)
error ("initial: system must be either purely continuous or purely discrete");
endif
if (n_st != length (x_0))
error ("initial: x0 must be a vector with %d elements", n_st);
endif
## code adapted from __stepimp__.m
if (nargin < 4)
## we have to compute the time when the system reaches steady state
## and the step size
eigw = eig (sys2ss (sys));
if (digital)
t_sam = sysgettsam (sys);
## perform bilinear transformation on poles in z
for k = 1 : n_d
pole = eigw(k);
if (abs (pole + 1) < 1.0e-10)
eigw(k) = 0;
else
eigw(k) = 2 / t_sam * (pole - 1) / (pole + 1);
endif
endfor
endif
## remove poles near zero from eigenvalue array eigw
nk = n_st;
for k = 1 : n_st
if (abs (real (eigw(k))) < 1.0e-10)
eigw(k) = 0;
nk = nk - 1;
endif
endfor
if (nk == 0)
if (nargin < 3)
t_final = 10;
endif
dt = t_final / 1000;
else
eigw = eigw(find (eigw));
eigw_max = max (abs (eigw));
dt = 0.2 * pi / eigw_max;
if (nargin < 3)
eigw_min = min (abs (real (eigw)));
t_final = 5.0 / eigw_min;
## round up
yy = 10^(ceil (log10 (t_final)) - 1);
t_final = yy * ceil (t_final / yy);
endif
if (! digital)
n = t_final / dt;
if (n < 50)
dt = t_final / 50;
endif
if (n > 2000)
dt = t_final / 2000;
endif
endif
endif
endif
## end of adapted code
if (digital)
dt = sysgettsam (sys);
if (nargin == 4)
warning ("initial: fourth argument has no effect on sampling time of digital system");
endif
else
sys = c2d (sys, dt);
endif
t = (0 : dt : t_final)';
l_t = length (t);
[F, G, C, D] = sys2ss (sys);
## preallocate memory
y = zeros (l_t, n_out);
x_arr = zeros (l_t, n_st);
## make sure that x is a row vector
x = reshape (x_0, length (x_0), 1);
## simulation
for k = 1 : l_t
y(k, :) = C * x;
x_arr(k, :) = x;
x = F * x;
endfor
if (nargout == 0) # plot information
if (digital) # discrete system
for k = 1 : n_out
subplot (n_out, 1, k)
stairs (t, y(:, k))
grid on
if (k == 1)
title ("Response to Initial Conditions")
endif
ylabel (sprintf ("Amplitude %s", sysgetsignals (sys, "out", k, 1)))
endfor
xlabel ("Time [s]")
else # continuous system
for k = 1 : n_out
subplot (n_out, 1, k)
plot (t, y(:, k))
grid on
if (k == 1)
title ("Response to Initial Conditions")
endif
ylabel (sprintf ("Amplitude %s", sysgetsignals (sys, "out", k, 1)))
endfor
xlabel ("Time [s]")
endif
else # return values
y_r = y;
t_r = t;
x_r = x_arr;
endif
endfunction
%!shared initial_c, initial_c_exp, initial_d, initial_d_exp
%!
%! A = [ -2.8 2.0 -1.8
%! -2.4 -2.0 0.8
%! 1.1 1.7 -1.0 ];
%!
%! B = [ -0.8 0.5 0
%! 0 0.7 2.3
%! -0.3 -0.1 0.5 ];
%!
%! C = [ -0.1 0 -0.3
%! 0.9 0.5 1.2
%! 0.1 -0.1 1.9 ];
%!
%! D = [ -0.5 0 0
%! 0.1 0 0.3
%! -0.8 0 0 ];
%!
%! x_0 = [1, 2, 3];
%!
%! sysc = ss (A, B, C, D);
%! sysd = c2d (sysc, 2);
%!
%! [yc, tc, xc] = initial (sysc, x_0, 0.2, 0.1);
%! initial_c = round (1e4 * [yc, tc, xc]) / 1e4;
%!
%! [yd, td, xd] = initial (sysd, x_0, 4);
%! initial_d = round (1e4 * [yd, td, xd]) / 1e4;
%!
%! ## expected values computed by the "dark side"
%!
%! yc_exp = [ -1.0000 5.5000 5.6000
%! -0.9872 5.0898 5.7671
%! -0.9536 4.6931 5.7598 ];
%!
%! tc_exp = [ 0.0000
%! 0.1000
%! 0.2000 ];
%!
%! xc_exp = [ 1.0000 2.0000 3.0000
%! 0.5937 1.6879 3.0929
%! 0.2390 1.5187 3.0988 ];
%!
%! initial_c_exp = [yc_exp, tc_exp, xc_exp];
%!
%! yd_exp = [ -1.0000 5.5000 5.6000
%! -0.6550 3.1673 4.2228
%! -0.5421 2.6186 3.4968 ];
%!
%! td_exp = [ 0
%! 2
%! 4 ];
%!
%! xd_exp = [ 1.0000 2.0000 3.0000
%! -0.4247 1.5194 2.3249
%! -0.3538 1.2540 1.9250 ];
%!
%! initial_d_exp = [yd_exp, td_exp, xd_exp];
%!
%!assert (initial_c, initial_c_exp)
%!assert (initial_d, initial_d_exp)