You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to insert a time varying matrix in simulink?
9 views (last 30 days)
Show older comments
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
Linee Teh
on 24 Apr 2020
Let say for t = 0:1:100s, mean that the system will have different B & K every second.
Accepted Answer
Ameer Hamza
on 24 Apr 2020
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
Linee Teh
on 24 Apr 2020
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.
Ameer Hamza
on 24 Apr 2020
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.
Linee Teh
on 24 Apr 2020
Edited: Linee Teh
on 24 Apr 2020
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."
Ameer Hamza
on 24 Apr 2020
Edited: Ameer Hamza
on 24 Apr 2020
Which Simulink release are you using? Are you running my file, or have you copied block in your own Simulink file?
Linee Teh
on 24 Apr 2020
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.
Linee Teh
on 24 Apr 2020
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?
Ameer Hamza
on 24 Apr 2020
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.
Linee Teh
on 25 Apr 2020
May I know why need to add a reshape block after constant C to make it a row vector?
Ameer Hamza
on 25 Apr 2020
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.
Linee Teh
on 25 Apr 2020
Edited: Linee Teh
on 25 Apr 2020
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?
Ameer Hamza
on 25 Apr 2020
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?
Linee Teh
on 25 Apr 2020
My simulation time now is from 0s to 5000s and I define t = linspace(0:5000)
Ameer Hamza
on 25 Apr 2020
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.
Linee Teh
on 25 Apr 2020
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.
Ameer Hamza
on 25 Apr 2020
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?
Linee Teh
on 25 Apr 2020
The dimension of K is [6x9x100] & B is [9x6x100] and the simulation stop time is t = 5640s.
I have attached the mfile.
Linee Teh
on 25 Apr 2020
My B and K will getting a new value when t=0, t=56.4 ... t=5640s with total 100t.
Ameer Hamza
on 25 Apr 2020
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_);
More Answers (0)
See Also
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)