Generate equation from Simulink Model - matlab

I have a large simulink model with many source and sink blocks, many with only elementary arithmetic operations in between. I have been asked to document the equations behind the model. I am currently doing this manually and I am finding it rather slow and there is a relatively high chance of errors in the process.
Is there any way for Simulink to generate the equations (in MATLAB syntax for example) automatically?

There is no utility in MATLAB/Simulink that can do exactly what you are looking for (and I personally don't know of any third-party tools that can do this, either).
However, I think that your best bet might be to make use of Simulink Coder. This will allow you to convert your Simulink model to C code. From that code, you may be able to extract the equivalent equations more easily than you can by analyzing the Simulink model by hand.
The catch, though, is that Simulink Coder is an add-on package to base Simulink, so you may or may not have this tool available to you.

Related

How to get differential equations from simscape system?

I have simmechanics model in simscape, I have imported it from solidworks design
I'm able to get linearized model using linmod and it is okay as any system in simulink
The problem here that I want to get the differential equations for that system, Is there any way to extract them?
The short answer: no, there is no built-in functionality.
The long answer: you'd have to parse the Simscape network, query the source equations (if the source code is visible). And stack these together. That's a pretty involved workflow.

Automatically generate circuits on its own in PLECS (Piece-wise Linear Electrical Circuit Simulation )

Is there any way I could program the Matlab/Simulink to be able to automatically generate circuits on its own? I am using PLECS blockset (Piece-wise Linear Electrical Circuit Simulation ) embedded in Simulink.
For example, I need to have hundreds of identical block in a single .mdl file, instead of inserting one by one by myself by calling the block which I previously saved in Simulink library, is it possible that Simulink can be programmed to automatically generate hundreds of blocks by itself?
The only way I was told is by "using vectorization for most components. Most components are vectorized if they have a vectorized input signal or if one of their parameter is specified as a vector." However, I could not find any further information/details, appreciate if anyone of you could give opinion on this?
I just want to know if this is possible? Else, I would have to try another approach?
Thanks!
edited on 10 July 2013: Further to my question, I have confirmed with Plexim that there isn't such features ( add_block and add_line) in Plecs (Piece-wise Linear Electrical Circuit Simulation), does anyone know if there is any way I could automate the Plecs model? Appreciate any suggestion...Thanks
You can probably use functions like add_block and add_line to automate the creation of your Simulink model from a library.

Matlab versus simulation products such as ANSYS and COMSOL

This may be the wrong place to ask this, but I can't find a better place on the SE network.
I've briefly worked with both Matlab and Ansys, and from what I have learnt/can gather, Matlab is a programming environment that has functions that perform common math, visualization and analysis operations. You primarily write programs in a textual fashion (.m files) or use Simulink to generate flow graphs (model-based development). Ansys on the other hand is primary a simulation environment where quite a lot can be done simply with the GUI (3D models, physics domains, configuration, display settings), and you can add equations at various points in the simulation engine in order to modify the simulation flow.
Whatever I understand is cursory and only serves as an overview. Can anyone give me a suitable real-world comparison between Matlab and Ansys (or any other simulation product such as COMSOL) that would allow us to understand when to use which, and the weaknesses of each system.
I haven't used Ansys, but Ansys is often compared with Comsol, and I've used Comsol and Matlab for years.
Matlab:
Programming language and environment that runs it. Which means it can do anything (that any other programming language can do). What are its highlights, compared to other languages?
Hundreds of built-in functions to work with Matrices. For example, in one project I needed to do simple matrix algebra (add, multiply, scale matrices), and also needed singular value decomposition. SVD is not something you could write in 50 lines of code, so I needed a ready-made library. At the time I used a library for Java, and wrote my own code for representing matrices and doing matrix algebra on them. That's a few hundreds of lines of code. Had I used Matlab, it would have been about ten lines of code, because all of it is there. I would have needed only to type help svd to find out how to use it. However, if you don't need any of that, stay away from Matlab at all costs! There are much better languages that are free.
Great to use as a calculator that is always open on the desktop, and can do back-of-the-envelope style calculations.
Plotting graphs. Many academics recommend Matlab as the tool of choice for producing publication-quality graphics. These can be exported as PDF and imported into Inkscape for further editing. The best thing is that commands for plotting a graph could be put into a script file, and then parts of it can be changed later as needed, which can save a lot of work compared to manually drawing a graph (imagine you wanted to change the axes or symbols used to present the data points).
Personally, I also use it for curve-fitting. It has many toolboxes, one of which is a neat tool that allows me to find equations that model a set of data points.
Comsol:
Specialised tool for solving partial differential equations (PDEs) on complicated domains using the finite element method (FEM). This might sound obscure, but many real-world engineering needs reduce to this. Such things as:
Finding loads, stresses and strains in civil engineering structures with complicated real-world geometry (what happens when there is gusty wind blowing onto a building or bridge?)
How do currents flow in particular conductive objects?
Chemical reactions in various industrial reactors.
What is the power efficiency of a generator (magnet spinning in coil) design?
How to place aircon outlets in a nontrivially-shaped room to achieve both good temperature distribution and good efficiency?
Comsol, as any other FEM tool that can work with arbitrary equations, can do multiphysics, which means, for example, that one could solve for chemistry of a battery, as well as the temperature and pressure, and how that feeds back into the chemical reaction (speeds up or slows down). Compared with a tool where you need to provide the equations, in Comsol, most of the things that would be needed to solve most problems are already there, and just need to be selected and applied to the geometry, which is also built inside Comsol. Also, equations of arbitrary description can be introduced.
The physical descriptions of how these physical substances behave are called PDEs.
Once Comsol has finished solving a problem, the data could be exported for post-processing into Matlab, which has much more versatile tools for manipulating data and making various plots.

What's the difference between stateflow and simulink?

As far as I know, stateflow and simulink are often used at the same time, and are both environments developed by MathWorks, who make Matlab. May I know what's the difference between them?
Simulink is largely a controls oriented solution. It graphically depicts math like products, sums, integrals, etc. However, it's conditional logic facility is lacking. Any kind of if construct quickly becomes terse and unmanageable in my opinion. I've seen many models, and there is a clear line here where Simulink should end and Stateflow should start.
Stateflow deals extremely well with logic and, of course, state machines. Now with the addition of the Simulink Function blocks within Stateflow, we have a powerful combination to allow the state machine in Stateflow direct the rest of the program.
As far as functionality goes, they are both functionally complete, meaning anything you can code in C, you can code in Simulink or Stateflow. However, I would not recommend coding a PID loop in Stateflow, but it's possible. You could also easily create a state machine in Simulink, but I'd advise against it.
As far as code generation; in the beginning of the meld, the Stateflow and Simulink had separate code generators that were sewed together with more Simulink generated C code at code generation time. Then came CGIR (Code Generation Intermediary Representation), which unifies the code generator between Stateflow and Simulink. It came around 2007, and has continued to deliver substantial increases in performance. Generated code has increased in on target performance to a point where companies can use the code in their embedded systems and actually get a performance benefit rather than take a small hit. Also, the time to generate the code has also decreased substantially. CGIR is a replacement for the Target Language Compiler, however, never fear, TLC API will still be available, perhaps forever for those who have developed massive libraries of proprietary code generation libraries.
Hope this helps, let me know if something sounds fishy or if I need to clarify.
Quoting a Stateflow Webinar:
Simulink is used to respond to continuous changes in dynamic changes.
Stateflow is used to respond to instantaneous changes in dynamic changes.
Real-world systems have to respond to both continuous and instantaneous changes.
Use both Simulink and Stateflow so that you can use the right tool for the right job. Examples: Suspensions dynamics of a car are modelled with Simulink whereas, the gear transmission is modelled with Stateflow.
Learn more about Stateflow in general at:
http://www.mathworks.com/products/stateflow/examples.html
Stateflow has been updated for making it very easy to create state machines and flow charts in R2012b.
The major updates include a new graphical editor, state transition tables, MATLAB as the action language and an integrated debugger. Find short videos for these features and how they can be used at:
http://www.mathworks.com/products/stateflow/whatsnew.html
best,
Siddharth
I'm also currently involved with both simulink and stateflow. Till now I did everything in Simulink but once you have to implement logic( if, case) your model becomes visually difficult to be analysed after coding. But I think theoretically you can do everything also just in Simulink (correct me if I'm wrong).
The answer of macduff explains very good the differences.
Regards,
GR
I have worked on both on simulink and state flow environment both are Matlab tool. When u want to design a reactive system or event driven system at that time we should prefer stateflow instead of simulink.
because we can control an event in stateflow in better way compare to simulink .though debugging is easy in simulink but readability and code generation are easy in stateflow.
Complex state machine: Stateflow
Complex mathematical process: Simulink blocks
Others: both of Stateflow and Simulink are fine
Another major benefit of Stateflow is the integration and usage of external C code. This can be done by selecting the Action Language C within the Chart settings.
As commonly known the user can include external Code within the "Simulation Target" options. While a normal Simulink model need small workarounds to access the external C code (ceval(), Simulink.Parameter for globals etc.), Stateflow can directly access functions, defines, etc.
Problems like array of buses/structs can also be avoided by coding these arrays in external code.
For a lot of use cases I love the flexibility to directly interact with C code within Stateflow.

Simulink version of AnyLogic component

Does anybody know what components in Simulink (MATLAB package) are analogs for AnyLogic components? (e.g: source, buffer, delay, sink).
Or how can I easily simulate behavior of AnyLogic model in MATLAB?
Maybe there are some cool magic tools?
If you're asking whether or not there is an easy way to convert from an AnyLogic model to a Simulink model, I doubt it. I haven't seen any conversion tools like that.
I have a feeling you're just going to have to rebuild your AnyLogic model in Simulink if you want to simulate your models in MATLAB. I would start by looking through the Block List and Function List links on this Simulink documentation page. You should be able to find analogous components to those in your AnyLogic model.
AnyLogic is one of the best tools for business simulation and Matlab is the best engineering tool. But they have different notations.So in the most cases conversion is just impossible! E.g. to convert DE model of airport into Matlab.
My suggestion: use Matlab for engineering tasks and AnyLogic for business simulation.