Array operations on simulink math function for maximum efficiency

1 view (last 30 days)
Hello, I am developing a model on simulink. I use math fucntion blocks while developing the model. Repetitive processes such as creating arrays, transferring arrays between blocks are often dominated in the content of Math function blocks. I use a for loop when creating arrays and storing data in matrix content, but I noticed that the for loop blocks simulink blocks and slows down the simulation a lot. How can I use a function for the matrix structure in which recursive functions will occur? I'm open to all suggestions.. Waiting for your answers. Thanks.
Example code: what ı want to do ------ obtained f array (1,100)
m=10;
n=10;
k=1;
f=zeros(1,m*n);
for i=1:1:m*n
f(i)=x(k)+y(i)...;
if k==n
k=0;
end
k=1+k;
end

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 27 Jan 2023
Instead of using "For Iterator Subsystem" block and "Math Function" block, I would suggest using the "MATLAB Function" block.
You would write similar code like what you have in your post.
  3 Comments
Fangjun Jiang
Fangjun Jiang on 27 Jan 2023
Edited: Fangjun Jiang on 27 Jan 2023
Then it depends on the needed processing task and the implementation code. You can do.
  1. Copy the MATLAB Function block code to a separate .m file and utilize the Profile Your Code to Improve Performance
  2. Similarly, use the Simulink Profile to see if the MATLAB Function block is the bottle neck.
  3. Some loop operation might be able to be replaced with matrix operation. Ask separate questions regarding those operations.
Also, the MATLAB Function block will be executed at every simulation step of the Simulink simulation. If the whole for-loop inside the MATLAB Function block only needs to be run once per simulation (for example, at the beginning of the simulation), then the MATLAB Function block needs to be put in a different place (e.g. inside an 'Initialize Function' block).
Alexi
Alexi on 28 Jan 2023
Edited: Alexi on 28 Jan 2023
You are telling the truth. The simulation runs much faster when I manually enter the equation sets that will be created with the for loop. For example, I'm talking about manually typing 100 equations line by line, which isn't very useful. I want to initialize these sets of equations based on m*n and then have different variables work on each row of the 100 equation sets without the need for a for loop during simulation. That's exactly what I want.
I am sharing an example of the more detailed code that returns inside. In this structure, m*n arrays of equations will be formed.
uk=zeros(1,m*ni);
ns=1;
nb=1;
for n=1:1:m*ni
uk(n)=-w0*cos(gama(nb))+ (w1-k1*w0)*cos(gama(nb)) * (ef*cosd(Azi(nb))+seg_mid(ns)*cosd(Azi(nb)+omega(nb))) + ((D_ws+K1_y*D_w0)*cos(gama(nb))) * (ef*sind(Azi(nb))+seg_mid(ns)*sind(Azi(nb)+omega(nb)));
if ns==m
nb=nb+1;
ns=0;
end
ns=ns+1;
end
u=uk;

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!