Programmatically generating Simulink models from symbolic equations

5 views (last 30 days)
jdg
jdg on 15 Nov 2017
Answered: Tim Grunert on 31 Jan 2018
For control design and analysis, I am a big fan of the connect() function in the Control System toolbox. It's quite convenient for building up a large linear dynamic system without having to resort to lots of manual linear algebra (or use of the lft function). I simply define my blocks and the InputName, OutputName properties and MATLAB takes care of the rest.
I would like to do something similar for non-linear dynamical systems. To my knowledge, MATLAB doesn't offer this sort of functionality. So I wrote some primitive scripts that allow me to define non-linear dynamic systems and interconnect them just using symbolic substitution. The resulting blocks have the following fields (example 3 state system shown)
.StateName: {'iL_F2' 'vC_F2' 'vCd_F2'}
.f: [3×1 sym]
.g: [2×1 sym]
.InputName: {'v3' 'i4'}
.OutputName: {'i3' 'v4'}
The equations f and g contain the differential equations and output equations respectively (i.e. d/dt(x) = f(x,u), y = h(x,u)).
These equations are symbolic, but I would like to simulate them in Simulink. Currently I manually paste the differential equations in a block, define my inputs and outputs to make the .InputName, .OutputName properties, and then hook up an integrator to integrate d/dt(x) = f(x,u).
I'd like to do this automatically in code but I'm clueless as to how to populate a model with user-defined differential equations. To be clear, I know the basics of building a Simulink model programmatically and setting parameters. But I'm unclear how to 1) turn my symbolic equations into the right format and 2) get them into a user-defined block.
Any thoughts on this would be appreciated. Even better, any links to existing methods for automatically generating Simulink blocks from non-linear differential equations. I think someone must have already developed a toolbox for doing this, right ?

Accepted Answer

Nicolas Schmit
Nicolas Schmit on 16 Nov 2017
  1. Convert the symbolic functions to MATLAB functions and generate .m files using the matlabFunction command.
  2. Call the .m files from a MATLAB Function block in Simulink.
  1 Comment
jdg
jdg on 16 Nov 2017
Thank you for the quick response. This seems to be exactly what I need.

Sign in to comment.

More Answers (2)

Tim Grunert
Tim Grunert on 31 Jan 2018
In the last year we also the problem that we need to model twice: symbolically for control analysis and in simulink for simulation. To make it more easy we developed a small tool solving this problem. It can be downloaded as free software under: https://github.com/odesca/

Walter Roberson
Walter Roberson on 16 Nov 2017
You may wish to look at odeFunction() along with the documentation where it shows calculating mass matrices and so on.
  2 Comments
Walter Roberson
Walter Roberson on 16 Nov 2017
I only learned about them a few months ago myself. The steps seem overly complicated sometimes, but they do work.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by