MATLAB code including for loop doesn't work in MATLAB Function block in Simulink

6 views (last 30 days)
Hi, I am trying to multiply 2 variables with Simulink: using one value at one time step of a variable to multiply with all the values at every time step of the other variable.
Since Simulink cannot deal with such job directly, I tried to use MATLAB Function.
At first I wrote a .m file and it works quite well in MATLAB.
The .m file is as follows: (Before the %% is the .m file running in MATLAB, and after it is what I revised to make it compatible for Simulink.)
LIBs = xlsread('FlowCatalog.xlsx', 'Sheet2'); % This is a 2-column file with the 1st is time step and 2nd is the values.
LIBs_1st_Result = zeros(length(WeibullResult(:, 1)));
WBR = WeibullResult.Data; % WeibullResult is a timeseries generated by another Simulink model.
for i = 1: size(LIBs(:, 1))
for o = 1: size(LIBs(:, 1))
LIBs_1st_Result(o+i-1, i) = LIBs(i, 2) * WBR(o);
end
LIB_Result = sum(LIBs_1st_Result, 2);
end
%% For its working well in Simulink MATLAB Function block, I revised a little as this:
function LIB_Result = Multi(LIBs, WeibullResult)
WBR = WeibullResult;
LIBs_1st_Result = zeros(length(WBR(:, 1)));
for i = 1: length(LIBs(:, 1))
for o = 1: length(LIBs(:, 1))
LIBs_1st_Result(o+i-1, i) = LIBs(i) * WBR(o);
end
LIB_Result = sum(LIBs_1st_Result, 2);
end
end
This is my Simulink model:
In the MATLAB, the .m file got my expected result like this:
However in Simulink, the result I got was very different:
And I noticed that in Simulink, the multiplication is usually what Simulink will do: one value can multiplied with one value only when they are at the same time step,not crossing time steps, which means the for loop did not work.
Is there any good ways to achieve that .m file's function in Simulink?

Answers (1)

Fangjun Jiang
Fangjun Jiang on 8 Jun 2020
In MATLAB, a program runs without the dimension of time. You can do "post-processing" with all the data available at the begining of the program.
In Simulink, a simulation runs along the time axis. At time step 1, the whole diagram executes once. At time step 2, the whole diagram executes once again. This includes that MATLAB Function block.
So assume you run simulation for 50 seconds. At t=20 seconds, all the data available to Simulink are data in the first 20 seconds. None of the data in the later 30 seconds are available. Although, you have those data available from Excel file or saved from another simulation, because they are fed through time series or arranged by time, as far as Simulink is concerned, the latter 30 seconds of data are "future data". They are not available.
This holds true for every input at every block. You may know well ahead what that value is but for Simulink, it can't predict or "know" future values. It is dependent on certain solver and the diagram to "calculate" the values step by step, moving forward one step at a time along the time axis.
If you really want to do this "post-proessign" in Simulink, you have to transfer those input data or treat them differently. But that is case-dependent and a whole different story.
  2 Comments
Jingwei Zhou
Jingwei Zhou on 9 Jun 2020
Thank you very much!
Would you please direct me to some instruction documents of post-processing in Simulink?
Fangjun Jiang
Fangjun Jiang on 9 Jun 2020
Let's assume time=(1:50)' and data=sin(time). The usual way to provide this to Simulink is to use a "From Workspace" block and specify the "Data" as [time, data]. Look at the signal using a Scope block. It is a rough sine curve but if you use it for simulation, then at t=20 seconds, you won't be able to get any data for the late 30 seconds.
Instead, you specify the "Data" as [data']. It loses the time information and become a vector of 50 elements. In scope, it shows as 50 straright lines.
Now, multiply it with another signal which is specified using another "From Workspade" block with "Data" as [time, cos(time)]. You will get the result you wanted.
At any time t (t=20 for example), you get the value of one signal cos(t) multiply with all 50 values of another signal sin(time). You get 50 values at any time and you can do whatever you wanted. For example, use the "Sum" block but specify the "list of signs" as "+" will make it a "vector summation". Apply this to the output of the multiplication. You will be able to get the summation of all 50 values at any time of t.

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!