]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/initial.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / initial.m
1 ## Copyright (C) 2009   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn{Function File} {[@var{y}, @var{t}, @var{x}] =} initial (@var{sys}, @var{x0})
20 ## @deftypefnx{Function File} {[@var{y}, @var{t}, @var{x}] =} initial (@var{sys}, @var{x0}, @var{t})
21 ## @deftypefnx{Function File} {[@var{y}, @var{t}, @var{x}] =} initial (@var{sys}, @var{x0}, @var{tfinal})
22 ## @deftypefnx{Function File} {[@var{y}, @var{t}, @var{x}] =} initial (@var{sys}, @var{x0}, @var{tfinal}, @var{dt})
23 ## Initial condition response of state-space model.
24 ## If no output arguments are given, the response is printed on the screen.
25 ##
26 ## @strong{Inputs}
27 ## @table @var
28 ## @item sys
29 ## State-space model.
30 ## @item x0
31 ## Vector of initial conditions for each state.
32 ## @item t
33 ## Optional time vector.  Should be evenly spaced.  If not specified, it is calculated
34 ## by the poles of the system to reflect adequately the response transients.
35 ## @item tfinal
36 ## Optional simulation horizon.  If not specified, it is calculated by
37 ## the poles of the system to reflect adequately the response transients.
38 ## @item dt
39 ## Optional sampling time.  Be sure to choose it small enough to capture transient
40 ## phenomena.  If not specified, it is calculated by the poles of the system.
41 ## @end table
42 ##
43 ## @strong{Outputs}
44 ## @table @var
45 ## @item y
46 ## Output response array.  Has as many rows as time samples (length of t)
47 ## and as many columns as outputs.
48 ## @item t
49 ## Time row vector.
50 ## @item x
51 ## State trajectories array.  Has @code{length (t)} rows and as many columns as states.
52 ## @end table
53 ##
54 ## @strong{Example}
55 ## @example
56 ## @group
57 ##                    .
58 ## Continuous Time:   x = A x ,   y = C x ,   x(0) = x0
59 ##
60 ## Discrete Time:   x[k+1] = A x[k] ,   y[k] = C x[k] ,   x[0] = x0
61 ## @end group
62 ## @end example
63 ##
64 ## @seealso{impulse, lsim, step}
65 ## @end deftypefn
66
67 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
68 ## Created: October 2009
69 ## Version: 0.1
70
71 function [y_r, t_r, x_r] = initial (sys, x0, tfinal = [], dt = [])
72
73   ## TODO: multiplot feature:   initial (sys1, "b", sys2, "r", ..., x0, ...)
74
75   if (nargin < 2 || nargin > 4)
76     print_usage ();
77   endif
78
79   [y, t, x] = __time_response__ (sys, "initial", ! nargout, tfinal, dt, x0, inputname (1));
80
81   if (nargout)
82     y_r = y;
83     t_r = t;
84     x_r = x;
85   endif
86
87 endfunction
88
89
90 %!shared initial_c, initial_c_exp, initial_d, initial_d_exp
91 %!
92 %! A = [ -2.8    2.0   -1.8
93 %!       -2.4   -2.0    0.8
94 %!        1.1    1.7   -1.0 ];
95 %!
96 %! B = [ -0.8    0.5    0
97 %!        0      0.7    2.3
98 %!       -0.3   -0.1    0.5 ];
99 %!
100 %! C = [ -0.1    0     -0.3
101 %!        0.9    0.5    1.2
102 %!        0.1   -0.1    1.9 ];
103 %!
104 %! D = [ -0.5    0      0
105 %!        0.1    0      0.3
106 %!       -0.8    0      0   ];
107 %!
108 %! x_0 = [1, 2, 3];
109 %!
110 %! sysc = ss (A, B, C, D);
111 %!
112 %! [yc, tc, xc] = initial (sysc, x_0, 0.2, 0.1);
113 %! initial_c = [yc, tc, xc];
114 %!
115 %! sysd = c2d (sysc, 2);
116 %!
117 %! [yd, td, xd] = initial (sysd, x_0, 4);
118 %! initial_d = [yd, td, xd];
119 %!
120 %! ## expected values computed by the "dark side"
121 %!
122 %! yc_exp = [ -1.0000    5.5000    5.6000
123 %!            -0.9872    5.0898    5.7671
124 %!            -0.9536    4.6931    5.7598 ];
125 %!
126 %! tc_exp = [  0.0000
127 %!             0.1000
128 %!             0.2000 ];
129 %!
130 %! xc_exp = [  1.0000    2.0000    3.0000
131 %!             0.5937    1.6879    3.0929
132 %!             0.2390    1.5187    3.0988 ];
133 %!
134 %! initial_c_exp = [yc_exp, tc_exp, xc_exp];
135 %!
136 %! yd_exp = [ -1.0000    5.5000    5.6000
137 %!            -0.6550    3.1673    4.2228
138 %!            -0.5421    2.6186    3.4968 ];
139 %!
140 %! td_exp = [  0
141 %!             2
142 %!             4 ];
143 %!
144 %! xd_exp = [  1.0000    2.0000    3.0000
145 %!            -0.4247    1.5194    2.3249
146 %!            -0.3538    1.2540    1.9250 ];
147 %!
148 %! initial_d_exp = [yd_exp, td_exp, xd_exp];
149 %!
150 %!assert (initial_c, initial_c_exp, 1e-4)
151 %!assert (initial_d, initial_d_exp, 1e-4)