Hello,
So, I wrote the code for tether dynamics in matlab script, and when I solve it using ode45 it is executing the code without any errors. However, when I am using this code in Matlab function block in Simulink it is giving me an error that my output variable is varying-size signal. So , can anyone help me with this?

답변 (1개)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2023년 8월 19일

0 개 추천

Simulink is stricter than MATLAB when it comes to variable sizes. In MATLAB, you can change the size of a variable at will, but in Simulink, the dimensions of signals are generally expected to be fixed. This is because Simulink models are often used for code generation and real-time execution, where dynamic memory allocation is not allowed.
If you're seeing an error related to a varying-size signal, here's what you should do:
  1. Variable Sizing: Firstly, ensure that all variables you are using in your MATLAB function block have a fixed size. If you're declaring matrices or vectors, their sizes shouldn't change throughout the code.
  2. Ode45: ODE solvers, like ode45, often return varying-size outputs based on their adaptive step size. This could be a problem in Simulink.
  • Consider setting the 'MaxStep' option of ode45 to ensure the number of steps does not exceed a certain limit.
  • Or use a fixed step solver like ode4 (Runge-Kutta) if appropriate for your problem.
Alternative :
  • If it's not strictly necessary to solve the ODE inside Simulink, solve it in MATLAB and import the results into Simulink as needed.
  • Consider using the built-in ODE solvers in Simulink. For example, use the "From Workspace" block to provide inputs to the system, and the "To Workspace" block to retrieve outputs.

댓글 수: 7

Dhruvit
Dhruvit 2023년 8월 21일
편집: Dhruvit 2023년 8월 21일
So, the function I am using in matlab function block consists of several for loop and if else statements. So, can this be one of the reason of getting varying size signal error in simulink? because the code is running perfectly and giving the output of desired size in MATLAB script but its not working in simulink.
Walter Roberson
Walter Roberson 2023년 8월 21일
Are you using a tspan with a length of 2, or are you using a tspan with a length of at least 3? Length 2 requires dynamic memory allocation.
Dhruvit
Dhruvit 2023년 8월 21일
Can you just brief out what's tspan length 2 or 3 means?
tspan I used for MATLAB ode45 is tspan = 0:0.01:10
Walter Roberson
Walter Roberson 2023년 8월 21일
If you had used tspan = [0, 10] then that would be a vector length 2, and ode45() would generate an unknown number of results needing dynamic memory. But with your tspan = 0:0.01:10 then the outputs will only be at those locations and dynamic memory would not be needed for that purpose.
Dhruvit
Dhruvit 2023년 8월 21일
Okay got it. So, why am I getting varying size signal error in simulink because the function is working fine in MATLAB script. Is it because the function has several for loops?
Walter Roberson
Walter Roberson 2023년 8월 21일
Did you pre-allocate all arrays?
Are you using event functions?
Walter Roberson
Walter Roberson 2023년 8월 21일
It would not astonish me if ode45() is just considered too complex to analyze statically, and so effectively could not be run inside a MATLAB Function Block unless dynamic memory allocation is enabled.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 General Applications에 대해 자세히 알아보기

제품

릴리스

R2023a

질문:

2023년 8월 19일

댓글:

2023년 8월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by