MATLAB Answers

Run Simulink Model as a Function

7 views (last 30 days)
Greetings,
I'm inheriting a complicated simulink model. My goal is to encapsulate said simulink model for use by my matlab functions.
To illustrate my problem, below is a simple simo.slx with input at Port 1 and output at Port 2. Is there a way to encapsulate this such that I could do the following in Matlab Command line?
%% Desired Function created from simulink slx model
output = simo(3); % returns output = 6
With the capability above, my plan is to do use the function generated by Simulink inside a ODE45 function. Unfortunately sim function does not achieve what I need as it runs the entire standalone simulink model.
Thank you in advance.

Accepted Answer

Peter O
Peter O on 3 Jun 2020
Hi Sze,
To clarify, you're hoping to run a simulink model from within a call to ODE45? While possible, I'm afraid this is going to run painfully slow from overhead. If this is really what you need, then replace the Input and Output ports with From Workspace and To Workspace blocks. You specify the variable names on either side. Then, call sim() within ode45's function, something like
[y,t] = ode45(@myfunc, tspan, x0, etc)
function dy = myfunc()
sim('simo') % spits out variable 'k' to workspace. Since you're wrapped, you may need to specify that Simulink interfaces with the caller workspace and not the global one.
dy = 2*k
end
Two thoughts instead:
  1. If the Simulink model is fully algebraic, see if you can move it into MATLAB during the ode myfunc call.
  2. Wrap at least the ODE solve of your code within simulink. Then you incur the Simulink setup and solving only once. There's a single sim() call.
For #2, I'm assuming that Simulink is used to compute dy in some form and that's why you need to call it from within ODE45. If that's the case, then make the model (which has ports) into a subsystem. Port 2 about will feed into an upper level model in which you compute the rest of the things needed for dy (you can even use a MATLAB function block within Simulink, so you don't have to convert your code into blocks). Then connect dy to an Integrator block, and you're off to the races!
Hopefully this helps give you some direction. If you have further questions about specifics I'd be happy to answer them.
  3 Comments
Sze Kwan Cheah
Sze Kwan Cheah on 3 Jun 2020
Hi Peter,
Thank you again for responding so quickly. I should provide more context to the problem which fortunately isn't very complicated. We have a SIMULINK model that simulates a mini-drone and, if I'm not mistaken, can also be controlled by MATLAB. I was hoping to encapsulate the mini-drone SIMULINK dynamic system with a function (thrust commands to dynamic response) and then code up a dynamic control law instead of using SIMULINK blocks.
Perhaps I am completely mistaken and have been overthinking it. I think I may implement #2 even with estimated feedforward in my control law.
Thank you, Sze Kwan

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by