Reputation: 45
I have simulink model "Mod_Sim" in simulink with an input port "inp" and an output port "out". I want to send inputs to this model, simulate it and then take the corresponding outputs at every sampling time "Ts". I want to simulate this model in this way for "N" time steps (in terms of Ts).
More specifically, I want the following implementation:
At a given time step "k", I will have input u(k) (from some other source). Then, I want to apply this input to Mod_Sim and get the corresponding output y(k) from the model. I want to repeat this for N time steps.
How can I do such an implementation?
I will be happy for helps.
Upvotes: 0
Views: 985
Reputation: 13886
You need to have all the inputs defined as a function of time for the time span of interest in your MATLAB workspace before you start the simulation.
Assuming you have t
and u
defined in the MATLAB workspace, and that your input port inp
is at the root level of the model, you need to configure your model to use u
and t
for inp
, as described in Import Data to Root-Level Input Ports.
Once you have done that, you can run the simulation using the sim
command, making sure that 'SaveOutput'
is on in the simulation options.
You can repeat that exercise N
times in a for
loop for example. The real key thing is having all your inputs defined in the workspace prior to starting the simulation.
Edit based on comments
It sounds like you want co-simulation, although you don't say what is generating the u(t)
, whether it's another piece of software or whatever. Regardless, you have a couple of options as I see it:
Write an S-function to interact with the other piece of software generating u(t)
so that it pulls a new value of u
at each time step. This is actually quite hard to do, a lot of software vendors propose some form of co-simulation with Simulink via an S-function (e.g. SimulationX). See also this other question on the same topic.
Use TCP/IP or UDP/IP to communicate between Simulink and the third-party software. The Instrument Control Toolbox provides Send and Receive blocks for TCP/IP and UDP/IP. TCP/IP is supposed to be more reliable than UDP/IP, see this comparison.
Upvotes: 1