MATLAB Answers

Convert ODE model in MATLAB to Simulink

20 views (last 30 days)
Hello
I have a ODE model in MATLAB but now I want to create a control system for it in SIMULINK. Which is the best way to use it inside simulink? Now I have a .m file that calls (with the solver ode45: [t,y] = ode45(@car,tpo,x0,[]);) to another m file where the ode are defined (car.m). So what I want is to convert the ca.m where 16 differential equations are defined to simulink with the least effort possible. I know that I can rewrite them in simulink with blocks but I want to know if there exists a more direct way to use the m code.
Thank you in advance

Accepted Answer

Seth DeLand
Seth DeLand on 13 Feb 2012
Hi Javier,
The easiest way I can think of to convert would be to use a Level 2 MATLAB S-function in Simulink. If you've never used S-functions before, this is a good place to get started: http://www.mathworks.com/help/toolbox/simulink/sfg/f6-12629.html
You can use the same code that you used in car.m for calculating the derivatives in your S-function (this will be in the Derivative function in the S-function). The msfcn_limintm.m S-function gives an example of how this can be done.
Because the outputs of the ODE solver are just the states, the Output function of you S-function should just grab the values of the states and set those as the outputs. This is also done in the msfcn_limintm.m example.
  1 Comment
Javier Guiterrez
Javier Guiterrez on 15 Feb 2012
Now I have checked it and it works. My current problem is that I make severeal precalculations previous to the derivatives because the coeficients of the ODE are time varying. Apart from the states I want to export some of these values calculated in the derivatives subfunctions. I have enlarged the output number and introduced it as input and out parameters of the different subfunctions inside the m file of the S-function, but the first time it appears (as an output parameter of the derivatives it says that it does not exist)
For example: [sys,g]=derivatives(t,x,u)
sys=output(t,x,u,g)
In the first call to derivatives it says that it is not defined yet.
Is it possible to do this? Thank you in advance

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!