Effacer les filtres
Effacer les filtres

Accessing A and B matrices from discrete state space

2 vues (au cours des 30 derniers jours)
SM
SM le 21 Juin 2022
I have written a code to convert continuous time state space models to discrete time using 'tustin' metod. I want to access A and B matrices for each the discretized models. I can access from command window but I want to use them for a later portion of the code. My current code is not giving me10 discretized A and B matrices. Kindly help
clc;
clear all;
A1(:,:,1) =[ 0 1.0000 0;0 0 1.0000; -0.4768 -0.0375 -0.0715];%updated the A matrices, -20 t0 20
A1(:,:,2) =[0 1.0000 0;0 0 1.0000; -0.4875 -0.0383 0];
A1(:,:,3) =[0 1.0000 0;0 0 1.0000; -0.4768 -0.0375 0.0715];
A1(:,:,4) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,5) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,6) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,7) =[0 1.0000 0;0 0 1.0000; 0.4768 -0.0375 0.0715];
A1(:,:,8) =[0 1.0000 0;0 0 1.0000; 0.4875 -0.0383 0];
A1(:,:,9) =[ 0 1.0000 0;0 0 1.0000; 0.4768 -0.0375 -0.0715];
A1(:,:,10)=[0 1.0000 0;0 0 1.0000; 0 -1.3438 -2.5664];
B1=[0;0;1];
C1=[1 0 0];
D1=0;
dt=0.01;
for i=1:10
sys1=ss(A1(:,:,i),B1,C1,D1);
sys2=c2d(sys1,dt,'tustin');
a2(:,:,i)=sys2.a
b2(:,:,i)=sys2.b
end
  2 commentaires
Star Strider
Star Strider le 21 Juin 2022
It seems to do what you want when I run it.
Is there something I am missing here?
clc;
clear all;
A1(:,:,1) =[ 0 1.0000 0;0 0 1.0000; -0.4768 -0.0375 -0.0715];%updated the A matrices, -20 t0 20
A1(:,:,2) =[0 1.0000 0;0 0 1.0000; -0.4875 -0.0383 0];
A1(:,:,3) =[0 1.0000 0;0 0 1.0000; -0.4768 -0.0375 0.0715];
A1(:,:,4) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,5) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,6) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,7) =[0 1.0000 0;0 0 1.0000; 0.4768 -0.0375 0.0715];
A1(:,:,8) =[0 1.0000 0;0 0 1.0000; 0.4875 -0.0383 0];
A1(:,:,9) =[ 0 1.0000 0;0 0 1.0000; 0.4768 -0.0375 -0.0715];
A1(:,:,10)=[0 1.0000 0;0 0 1.0000; 0 -1.3438 -2.5664];
B1=[0;0;1];
C1=[1 0 0];
D1=0;
dt=0.01;
for i=1:10
fprintf('Iteration %2d —————',i)
sys1=ss(A1(:,:,i),B1,C1,D1);
sys2{i}=c2d(sys1,dt,'tustin');
a2(:,:,i)=sys2{i}.a
b2(:,:,i)=sys2{i}.b
end
Iteration 1 —————
a2 = 3×3
1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993
b2 = 3×1
0.0000 0.0000 0.0100
Iteration 2 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100
Iteration 3 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100
Iteration 4 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007 a2(:,:,4) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100 b2(:,:,4) = 0.0000 0.0000 0.0100
Iteration 5 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007 a2(:,:,4) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,5) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100 b2(:,:,4) = 0.0000 0.0000 0.0100 b2(:,:,5) = 0.0000 0.0000 0.0100
Iteration 6 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007 a2(:,:,4) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,5) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,6) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100 b2(:,:,4) = 0.0000 0.0000 0.0100 b2(:,:,5) = 0.0000 0.0000 0.0100 b2(:,:,6) = 0.0000 0.0000 0.0100
Iteration 7 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007 a2(:,:,4) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,5) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,6) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,7) = 1.0000 0.0100 0.0001 0.0000 1.0000 0.0100 0.0048 -0.0004 1.0007
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100 b2(:,:,4) = 0.0000 0.0000 0.0100 b2(:,:,5) = 0.0000 0.0000 0.0100 b2(:,:,6) = 0.0000 0.0000 0.0100 b2(:,:,7) = 0.0000 0.0001 0.0100
Iteration 8 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007 a2(:,:,4) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,5) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,6) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,7) = 1.0000 0.0100 0.0001 0.0000 1.0000 0.0100 0.0048 -0.0004 1.0007 a2(:,:,8) = 1.0000 0.0100 0.0000 0.0000 1.0000 0.0100 0.0049 -0.0004 1.0000
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100 b2(:,:,4) = 0.0000 0.0000 0.0100 b2(:,:,5) = 0.0000 0.0000 0.0100 b2(:,:,6) = 0.0000 0.0000 0.0100 b2(:,:,7) = 0.0000 0.0001 0.0100 b2(:,:,8) = 0.0000 0.0000 0.0100
Iteration 9 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007 a2(:,:,4) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,5) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,6) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,7) = 1.0000 0.0100 0.0001 0.0000 1.0000 0.0100 0.0048 -0.0004 1.0007 a2(:,:,8) = 1.0000 0.0100 0.0000 0.0000 1.0000 0.0100 0.0049 -0.0004 1.0000 a2(:,:,9) = 1.0000 0.0100 0.0000 0.0000 1.0000 0.0100 0.0048 -0.0004 0.9993
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100 b2(:,:,4) = 0.0000 0.0000 0.0100 b2(:,:,5) = 0.0000 0.0000 0.0100 b2(:,:,6) = 0.0000 0.0000 0.0100 b2(:,:,7) = 0.0000 0.0001 0.0100 b2(:,:,8) = 0.0000 0.0000 0.0100 b2(:,:,9) = 0.0000 0.0000 0.0100
Iteration 10 —————
a2 =
a2(:,:,1) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993 a2(:,:,2) = 1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000 a2(:,:,3) = 1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007 a2(:,:,4) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,5) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,6) = 1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995 a2(:,:,7) = 1.0000 0.0100 0.0001 0.0000 1.0000 0.0100 0.0048 -0.0004 1.0007 a2(:,:,8) = 1.0000 0.0100 0.0000 0.0000 1.0000 0.0100 0.0049 -0.0004 1.0000 a2(:,:,9) = 1.0000 0.0100 0.0000 0.0000 1.0000 0.0100 0.0048 -0.0004 0.9993 a2(:,:,10) = 1.0000 0.0100 0.0000 0 0.9999 0.0099 0 -0.0133 0.9746
b2 =
b2(:,:,1) = 0.0000 0.0000 0.0100 b2(:,:,2) = 0.0000 0.0000 0.0100 b2(:,:,3) = 0.0000 0.0001 0.0100 b2(:,:,4) = 0.0000 0.0000 0.0100 b2(:,:,5) = 0.0000 0.0000 0.0100 b2(:,:,6) = 0.0000 0.0000 0.0100 b2(:,:,7) = 0.0000 0.0001 0.0100 b2(:,:,8) = 0.0000 0.0000 0.0100 b2(:,:,9) = 0.0000 0.0000 0.0100 b2(:,:,10) = 0.0000 0.0000 0.0099
.
SM
SM le 22 Juin 2022
Modifié(e) : SM le 22 Juin 2022
I was trying to access all 10 As and Bs simultaneously and not iteration wise. So that I can use these later. With this kind of indexing it gets a bit confusing. I wanted something like:
a2(:,:,1)=
b2(:,:,1)=
a2(:,:,2)=
b2(:,:,2)=
a2(:,:,3)=
b2(:,:,3)=
...
a2(:,:,10)=
b2(:,:,10)=

Connectez-vous pour commenter.

Réponse acceptée

Balavignesh
Balavignesh le 27 Sep 2023
Hi SM,
As per my understanding, you have converted ten models from continuous state space to discrete state space using ‘tustin’ and you would like to display the discretized state space matrices ‘a2’ and ‘b2’ for each model simultaneously.
I would suggest you to pre-allocate the discretized state-space matrices 'a2' and 'b2' for faster computation.
You can display 'a2' and 'b2' for each model simultaneously in another 'for' loop using the 'fprintf' and 'disp' functions.
Here is an example showing how you can achieve this:
clc;
clear all;
A1(:,:,1) =[ 0 1.0000 0;0 0 1.0000; -0.4768 -0.0375 -0.0715];%updated the A matrices, -20 t0 20
A1(:,:,2) =[0 1.0000 0;0 0 1.0000; -0.4875 -0.0383 0];
A1(:,:,3) =[0 1.0000 0;0 0 1.0000; -0.4768 -0.0375 0.0715];
A1(:,:,4) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,5) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,6) =[0 1.0000 0;0 0 1.0000;0 -9.8100 0];
A1(:,:,7) =[0 1.0000 0;0 0 1.0000; 0.4768 -0.0375 0.0715];
A1(:,:,8) =[0 1.0000 0;0 0 1.0000; 0.4875 -0.0383 0];
A1(:,:,9) =[ 0 1.0000 0;0 0 1.0000; 0.4768 -0.0375 -0.0715];
A1(:,:,10)=[0 1.0000 0;0 0 1.0000; 0 -1.3438 -2.5664];
B1=[0;0;1];
C1=[1 0 0];
D1=0;
dt=0.01;
% Pre-allocating the discretized state-space matrices
a2 = zeros(3,3,10);
b2 = zeros(3,1,10);
for i=1:10
sys1=ss(A1(:,:,i),B1,C1,D1);
sys2=c2d(sys1,dt,'tustin');
a2(:,:,i)=sys2.a;
b2(:,:,i)=sys2.b;
end
% Display the desired using another 'for' loop
for i = 1:10
fprintf("a2(:,:,%d) = ",i);
disp(a2(:,:,i));
fprintf("b2(:,:,%d) = ",i);
disp(b2(:,:,i));
end
a2(:,:,1) =
1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0048 -0.0004 0.9993
b2(:,:,1) =
0.0000 0.0000 0.0100
a2(:,:,2) =
1.0000 0.0100 0.0000 -0.0000 1.0000 0.0100 -0.0049 -0.0004 1.0000
b2(:,:,2) =
0.0000 0.0000 0.0100
a2(:,:,3) =
1.0000 0.0100 0.0001 -0.0000 1.0000 0.0100 -0.0048 -0.0004 1.0007
b2(:,:,3) =
0.0000 0.0001 0.0100
a2(:,:,4) =
1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995
b2(:,:,4) =
0.0000 0.0000 0.0100
a2(:,:,5) =
1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995
b2(:,:,5) =
0.0000 0.0000 0.0100
a2(:,:,6) =
1.0000 0.0100 0.0000 0 0.9995 0.0100 0 -0.0981 0.9995
b2(:,:,6) =
0.0000 0.0000 0.0100
a2(:,:,7) =
1.0000 0.0100 0.0001 0.0000 1.0000 0.0100 0.0048 -0.0004 1.0007
b2(:,:,7) =
0.0000 0.0001 0.0100
a2(:,:,8) =
1.0000 0.0100 0.0000 0.0000 1.0000 0.0100 0.0049 -0.0004 1.0000
b2(:,:,8) =
0.0000 0.0000 0.0100
a2(:,:,9) =
1.0000 0.0100 0.0000 0.0000 1.0000 0.0100 0.0048 -0.0004 0.9993
b2(:,:,9) =
0.0000 0.0000 0.0100
a2(:,:,10) =
1.0000 0.0100 0.0000 0 0.9999 0.0099 0 -0.0133 0.9746
b2(:,:,10) =
0.0000 0.0000 0.0099
Please refer to the following documentation links to know more about

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by