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()
dy = 2*k
Two thoughts instead:
- If the Simulink model is fully algebraic, see if you can move it into MATLAB during the ode myfunc call.
- 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.