How to insert a time varying matrix in simulink?

The figure below is my state-space equation where input matrix B and gain K are with size 9x6x100 and 6x9x100. The values of B and K are taken from the workspace in my matlab coding.
How can I assign the value of B and K into the system one by one?
For example, used B(9x6x1) & K(6x9x1) then B(9x6x2) & K(6x9x2) then B(9x6x3) & K(6x9x3) until B(9x6x100) & K(6x9x100).
I am really looking for help and thank you very much!

2 Comments

After how much simulation time do you want to move to the next matrix?
Let say for t = 0:1:100s, mean that the system will have different B & K every second.

Sign in to comment.

 Accepted Answer

The attached file (saved in R2020a) shows an easy way to import a time-varying matrix in such a way. Define the matrices in base workspace by running these lines
B = rand(9,6,100);
K = rand(6,9,100);

24 Comments

Hey Ameer, so the matlab function in the attached file I no need to change anything is it?
Then insert
B = rand(9,6,100);
K = rand(6,9,100);
in the mfile and make sure it store in the workspace?
I am sorry, I am quite new in simulink that why I don't really understand the block diagram.
Yes, the MATLAB function will output the next slice of B and K matrix after each second. You can directly use it without changing anything. You need to define B and K in the base workspace, but the line I wrote is just an example of random matrices. You need to use your own matrix, B and K.
I have tried it but error popping out by saying:
"Error evaluating expression 'auto' for 'LifeSpan' specified in the Configuration Parameters dialog for block diagram 'sim_model': Undefined function or variable 'auto'. Error while evaluating LifeSpan parameter. Default application life span (1) day will be used."
Which Simulink release are you using? Are you running my file, or have you copied block in your own Simulink file?
I am running your file and I realize I should copy your file to my version of MATLAB. The version of my MATLAB is R2016a. Thanks Ameer! You really help me a lot in my work.
I am glad that my comments are helpful.
I have tried to fit your system in my own simulink block but error pop out by saying:
1. Error in port widths or dimensions. Invalid dimension has been specified for input port 1 of 'LQR_v2/Sum'.
2. Error in port widths or dimensions. Output port 1 of 'LQR_v2/Product1' is a [9x6] matrix.
Do you know where is the problem for this block diagram?
Can you attach the model?
See the attached file. I just changed the settings inside multiplication blocks from element-wise to matrix, and added a reshape block after constant C to make it a row vector.
May I know why need to add a reshape block after constant C to make it a row vector?
Because if we input a 1D vector in Simulink, it does not recognize its dimension. We need to specify if it is a row or column matrix explicitly. For 2D matrices, this is not an issue; their number of rows and columns are recognized by Simulink.
My C matrix is 2D matrix with eye(9), so actually no need to change the dimension since the simulink can recognize right? Also, refer back to the time varying simulink block that you have create, is it possible for me to change the time (clock) with more than 100 points?
Ok. I tried your code with C being a row vector. If your C matrix is already 2D, then there is no need to use the reshape.
Do you want to change each block earlier than 1 second?
My simulation time now is from 0s to 5000s and I define t = linspace(0:5000)
If the matrix needs to change after every one second, then my current code is correct. You can just increase the time of simulation and increase the dimensions of B and K matrices accordingly.
Why error only appear in performing the function in gain K? When I separately run the matlab function in B and K respectively, the last value for the code: t_ = floor(t+1) for B is 99 while K is 111.
The error says:
Attempted to access index 111 of an array with smaller dimension sizes.
MATLAB Function ' ': Kt = K(:,:,t_);
The valid index range is 1 to 100.
This error will stop the simulation.
What are the dimension of K and B? What is the simulation stop time? Can share the simulink file along with the matrices A, B, K and C saved in a .mat file?
The dimension of K is [6x9x100] & B is [9x6x100] and the simulation stop time is t = 5640s.
I have attached the mfile.
So do you want to get a new matrix after 5640/100=56.4 seconds?
My B and K will getting a new value when t=0, t=56.4 ... t=5640s with total 100t.
Try this code. It is more general as compared to the first.
function [Bt, Kt] = fcn(t, B, K)
Bt = zeros(9,6); % to tell Simulink about size of these matices
Kt = zeros(6,9);
t_ = floor(interp1([0 5640], [1 100], t));
Bt = B(:,:,t_);
Kt = K(:,:,t_);
Thank you once again!!!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!