이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to insert a time varying matrix in simulink?
조회 수: 10 (최근 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에서 Trimming and Linearization에 대해 자세히 알아보기
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 (한국어)
