Programmatically generating Simulink models from symbolic equations

조회 수: 26 (최근 30일)
jdg
jdg 2017년 11월 15일
답변: Tim Grunert 2018년 1월 31일
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 ?

채택된 답변

Nicolas Schmit
Nicolas Schmit 2017년 11월 16일
  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
jdg
jdg 2017년 11월 16일
Thank you for the quick response. This seems to be exactly what I need.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Tim Grunert
Tim Grunert 2018년 1월 31일
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 2017년 11월 16일
You may wish to look at odeFunction() along with the documentation where it shows calculating mass matrices and so on.
  댓글 수: 2
jdg
jdg 2017년 11월 16일
Thanks! Surprised I never learned of these tools before.
Walter Roberson
Walter Roberson 2017년 11월 16일
I only learned about them a few months ago myself. The steps seem overly complicated sometimes, but they do work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linearization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by