I would like to ask how to create multiple state-spaces from arrays.
Input is:
A1toA100 (100xn double)
B1toB100 (100xp double)
C1toC100 (100xn double)
D1toD100 (100xp double)
Example:
A1toA10 =
-0.5909
-0.4178
-0.3412
-0.2954
-0.2643
-0.2412
-0.2233
-0.2089
-0.1970
-0.1869
>> B1toB10
B1toB10 =
33.3333
33.3333
33.3333
33.3333
33.3333
33.3333
33.3333
33.3333
33.3333
33.3333
>> C1toC10
C1toC10 =
1
1
1
1
1
1
1
1
1
1
>> D1toD10
D1toD10 =
0
0
0
0
0
0
0
0
0
0
Where each of these contains a hundred of matrices of respective type (A,B,C or D).
Output should be:
SS1toSS100 (100x ss)
Where there would be a state space corresponding to each set of matrices.
Example for the first system:
With this code
ss([-0.5909],[33.3333],[1],[0])
I get a correct output. Four numbers, one system <1x1 ss>. This:
ans =
a =
x1
x1 -0.5909
b =
u1
x1 33.33
c =
x1
y1 1
d =
u1
y1 0
However with this code:
for i=1:2
pom=[-0.5909 33.3333 1 0]
SS1toSS10(i)=ss(pom(1),pom(2),pom(3),pom(4));
end
Although I should get 2 systems identical to the first one, I get this instead:
SS1toSS10
SS1toSS10 =
a =
x1 x2
x1 -0.5909 0
x2 0 -0.5909
b =
u1 u2
x1 33.33 0
x2 0 33.33
c =
x1 x2
y1 1 1
d =
u1 u2
y1 0 0
Continuous-time state-space model.
Which is a wrong answer.
Approach 1:
for i=1:length(A1toA100)
SS1toSS100(i)=ss(A1toA100(i),B1toB100,C1toC100,D1toD100);
end
This has generated 100 state spaces with matrix dimensions of 200x200, which is wrong, as I would expect dimensions of 2x2. Obviously the dimensions of entire matrices are considered.
Example output for Example input vectors:
S1toS10(1,1).a
-0,417825056426464 0 0 0 0 0 0 0 0 0
0 -0,341152729998142 0 0 0 0 0 0 0 0
0 0 -0,295446930748805 0 0 0 0 0 0 0
0 0 0 -0,264255768359200 0 0 0 0 0 0
0 0 0 0 -0,241231408801990 0 0 0 0 0
0 0 0 0 0 -0,223336886965331 0 0 0 0
0 0 0 0 0 0 -0,208912528213232 0 0 0
0 0 0 0 0 0 0 -0,196964620499203 0 0
0 0 0 0 0 0 0 0 -0,186857045774452 0
0 0 0 0 0 0 0 0 0 -0,590893861497609
S1toS10(1,1).b
0
0
0
0
0
0
0
0
0
33,3333333333333
S1toS10(1,1).c
1 1 1 1 1 1 1 1 1 1
S1toS10(1,1).d
0
Approach 2:
for i=1:length(A1toA100)
SS1toSS100(i)=arrayfun(@ss,A1toA100(i),B1toB100,C1toC100,D1toD100);
end
Ends up with this error.
Error using arrayfun ss output type is not currently implemented.
Thank you for your help, Petr
Now I get your problem! Your last edit was essential.
The only way to store multiple state space models in one variable is a struct
. Therefore your loop has to look like:
SS1toSS100(100) = struct; %pre-allocation
for ii=1:100
temp_ss = ss( A1toA100(ii) , B1toB100(ii) , C1toC100(ii) , D1toD100(ii) );
SS1toSS100(ii) = struct('ss',temp_ss);
end
or even shorter:
SS1toSS100(100) = struct; %pre-allocation
for ii=1:100
SS1toSS100(ii).ss = ss( A1toA100(ii) , B1toB100(ii) , C1toC100(ii) , D1toD100(ii) );
end
which gives you a struct with 100 ss
objects.
If you now want to access the 5th one for example, type:
SS1toSS100(5).ss
and you get
ans =
a =
x1
x1 -0.5909
b =
u1
x1 33.33
c =
x1
y1 1
d =
u1
y1 0
Continuous-time state-space model.
PS: don't use i
as iteration variable it's a internal Matlab variable and reserved for complex numbers