Input signal as vector with time in simulink

121 views (last 30 days)
I'm working with an L2 S-function, and I have three input ports. My S-function is already working fine, so I can enter my three inputs, and my simulation will work properly. Now, I want to compare my simulation with a real system. I conducted measurements on a system and obtained a vector with the same length over the same time for each input. The issue is that Simulink/my S-function does not accept vectors as inputs, and Simulink operates based on the time provided within Simulink.
Let's assume we have something similar to the picture/table below: the first column is time, the second column is the first input, etc.
I want Simulink to precisely follow the timeline from my measurement data. At each specific time point, I aim for Simulink to extract the corresponding value and traverse through the S-function until reaching the end. Is it possible to achieve this?
Thank you for any suggestions
I attempted to manually run the simulation using MATLAB with two 'for-loops,' and it worked correctly. Now, it would be fantastic to achieve the same functionality within Simulink
I tried to pass the values with "From Workspace" like: u1 = [Data(:,1) Data(:,2)], u2 = [Data(:,1) Data(:,3)] and u3 = [Data(:,1) Data(:,4)] but the result ist just not plausible

Accepted Answer

TKinter
TKinter on 4 Dec 2023
I solved my problem. It was not because I inported the data the false way, but because of an error on how to handelt my s-function while using Simulink. I tried another way to solve the differential equation within the s-function and it worked properly. So there must be an error in the first way.
Thanks for your answers because I could say for sure that I'm importing the data the right way so I was able to look for the error somewhere else
  2 Comments
Paul
Paul on 4 Dec 2023
Out of curiosity, why use an s-function to solve a differential equation instead of using the Simulink solver?
TKinter
TKinter on 4 Dec 2023
Edited: TKinter on 4 Dec 2023
It is not just about solving "a" differential equation but the problem I wanted/want to solve is really complex an requires a lot of connexion which is almost not possible to do in simulink (possible but there would be no way to see through for myself or if you have to explain to others). So the workaround is to use level2 s-function. That is also the reason why I haven't tired to explain/show the s-function here because of the complexity of the problem.

Sign in to comment.

More Answers (2)

Jon
Jon on 30 Nov 2023
I don't understand why your S-function block needs to get three, 2d arrays (e.g. u1 =[(Data(:,1),Data(:,2)], with time as the first column) as inputs, as you imply in your description of trying to supply inputs using a from-workspace block.
If your function has three input signals, it should just receive a scalar value for each input at each Simulation time step (just like every other Simulink block). The from workspace block, is designed to feed these scalar values, at each simulation step. It looks up the time, and, interpolates (according to the interpolation method that is set) the current value to be output and fed to your system.
If my explanation does not allow you to fix your problem and move ahead, please explain further, and attach the code for your level 2 S function block
  7 Comments
Jon
Jon on 4 Dec 2023
I guess you don't need the R2023a version anymore, as it seems like you've answered your own question, but let me know.
P.S. Why not update to the current version, usually helpful to keep current
TKinter
TKinter on 5 Dec 2023
Yes I don't need it anymore thanks.
I will update it

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 30 Nov 2023
Edited: Fangjun Jiang on 30 Nov 2023
u1 = [Data(:,1) Data(:,2)], u2 = [Data(:,1) Data(:,3)] and u3 = [Data(:,1) Data(:,4)]
The way you construct the data for "From Workspace" block to import measured inputs is correct.
You could use just one "From Workspace" block and specify the data as "Data". It will import three signals at once and then you can use a "Demux" block to split it into three and feed them to your S-function.
"I want Simulink to precisely follow the timeline from my measurement data."
If you mean to set the simulation time vector exactly as your first column data, that is possible but not a good idea. It looks like the measurement is sampled at 1 second with offset of 0.4. But there are missing data at 7.4 and 9.4 for example.
If you mean to set the three input data for the simulation "exactly" the same as the measured data, you have done the correct thing and that is sufficient.
Maybe, your simulation output "result is just not plausible" comparing to your real, measured output, that is most likely caused by something else, not the constructing of the input data. A few things to look out is the solver and simulation step size, the sample time setting for the "From Workspace" block and its interpolation methods. Add a scope to observe the output of the "From Workspace" block during the whole simulation to see if the value makes sense.
The first measured data is at t=0.4 while the simulation usually starts at t=0. There might be an initial value problem. Add a row to specify the data for t=0 or you could set the simulation start time to be 0.4
  3 Comments
Fangjun Jiang
Fangjun Jiang on 2 Dec 2023
If you observed the outputs of the three "From Workspace" blocks and they are as expected, then there is no problem regarding data importing. The unexpected simulation output was caused by something else, which you have not provided any clues.
TKinter
TKinter on 3 Dec 2023
Yeah I first thought that the problem ist how I'm impoting the data but maybe there is a problem on my s-function. So If everybody here is saying that I've imported the data the right way then I will check on my s-function itself again. Thanks

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!