How to insert a time varying matrix in simulink?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
Linee Teh
2020-4-24
Let say for t = 0:1:100s, mean that the system will have different B & K every second.
采纳的回答
Ameer Hamza
2020-4-24
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 个评论
Linee Teh
2020-4-24
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
2020-4-24
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
2020-4-24
编辑:Linee Teh
2020-4-24
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
2020-4-24
编辑:Ameer Hamza
2020-4-24
Which Simulink release are you using? Are you running my file, or have you copied block in your own Simulink file?
Linee Teh
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-25
May I know why need to add a reshape block after constant C to make it a row vector?
Ameer Hamza
2020-4-25
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
2020-4-25
编辑:Linee Teh
2020-4-25
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
2020-4-25
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?
Ameer Hamza
2020-4-25
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
2020-4-25
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
2020-4-25
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
2020-4-25
The dimension of K is [6x9x100] & B is [9x6x100] and the simulation stop time is t = 5640s.
I have attached the mfile.
Linee Teh
2020-4-25
My B and K will getting a new value when t=0, t=56.4 ... t=5640s with total 100t.
Ameer Hamza
2020-4-25
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_);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)