Converting Simulink model to C code, integration

Click For Summary
SUMMARY

The discussion focuses on converting a Simulink model into C code for simulating an 'EddySpeaker' block without access to MATLAB or Simulink. The user, Akos, successfully implemented a numerical approximation using the trapezoid method but encountered stability issues when the time step (dt) was not sufficiently small. Participants confirmed that while numerical approaches can simulate such models, they may require careful time step management and suggested that the model could also be described using ordinary differential equations (ODEs) solved by a solver like VCODE. The generated C code from Simulink is primarily numerical and does not inherently solve ODEs.

PREREQUISITES
  • Understanding of numerical integration methods, specifically the trapezoid method.
  • Familiarity with ordinary differential equations (ODEs) and their solvers, such as VCODE.
  • Knowledge of C programming for implementing simulation models.
  • Experience with Simulink or similar simulation tools for model generation.
NEXT STEPS
  • Research numerical integration techniques in C, focusing on stability and accuracy.
  • Explore the implementation of ODE solvers like CVODE for complex systems.
  • Investigate methods for linearizing complex models to simplify simulation.
  • Learn about the differences between explicit and implicit solvers in numerical simulations.
USEFUL FOR

Engineers, simulation developers, and researchers involved in modeling and simulating dynamic systems, particularly those transitioning from Simulink to C code for performance optimization.

flowwolf
Messages
26
Reaction score
4
Dear Forumers,Given a simulink model:

http://www.femm.info/Archives/contrib/images/TransientLoudspeaker/SimulinkOverview.png

I would like to implement fast code for it.I have no access to matlib or simulink at all, so I tried to implement the simulation in C code.
Code for the block 'EddySpeaker' is given, and the integrals are calculated by numerical approximation (trapezoid method, for example).

It seems that the approach I chose works (I compared it to the simulation of scilab xcos), but it's not stable when dt is not small enough.

Actually I'm afraid that these sort of problems are solved in a completely different way, like setting up ordinary differential equations
and make them solved by a solver like VCODE (that's what xcos do, as much as I get the picture).My questions,

- practically, can this model be simulated by a numerical approach (like I described)?
I simply integrate acceleration, then velocity, then force and the results are fed back to the block 'EddySpeaker' which has its own code.
If yes, is it normal that I get unstability (like oscillation) when dt is not small enough?

- Can this model be described by ODEs and make them solved by a solver?
I'm asking this because the block 'EddySpeaker' has its own custom code. I read that codes can be translated into blocks though.

- what sort of C code is generated by the code generator in simulink? Is it a numerical type, or the ODE type (which is get solved by a solver)?ps. I can paste my simplified code here, if needed.Any help would be greatly appreciated,

Regards,
Akos
 
Physics news on Phys.org
flowwolf said:
It seems that the approach I chose works (I compared it to the simulation of scilab xcos), but it's not stable when dt is not small enough.
That is a common problem. You need to make sure that your time step is small enough for the dynamics of the system. Do not be fooled by the Nyquist rate. That is a theoretical minimum sampling rate if you have an infinite series of data. Use a sampling rate at least 20 times the fastest frequency of the system.
Actually I'm afraid that these sort of problems are solved in a completely different way, like setting up ordinary differential equations
and make them solved by a solver like VCODE (that's what xcos do, as much as I get the picture).
There is a limit to how complicated a problem can get and still be solvable that way. Many real-world problems that are done with simulations are much too complicated to be solved as an ODE. You may have to replace the EddySpeaker with a set of equations to model it. You may need to "linearize" it.
- practically, can this model be simulated by a numerical approach (like I described)?
Absolutely. This diagram is much simpler than a lot of simulations that are done.
I simply integrate acceleration, then velocity, then force and the results are fed back to the block 'EddySpeaker' which has its own code.
If yes, is it normal that I get unstability (like oscillation) when dt is not small enough?
Yes.
- Can this model be described by ODEs and make them solved by a solver?
I'm asking this because the block 'EddySpeaker' has its own custom code. I read that codes can be translated into blocks though.
If you know what the code is. That may be very difficult. You may need to replace it with a simpler model whose parameters you can set to mimic EddySpeaker.
- what sort of C code is generated by the code generator in simulink? Is it a numerical type, or the ODE type (which is get solved by a solver)?
The code I have seen is a numerical, frame-by-frame, simulation of the system. It does not solve an ODE.
 
Last edited:
  • Like
Likes   Reactions: flowwolf
Thank you very much for your kind answer FactChecker.

The possibilities here are limited, but I was able to generate C code in scicoslab. It seems it depends on whether code is generated for a superblock or for the entire diagram, so it can generate code without a solver or code that depends on CVODE.

I was a little uninformed, the mathworks site have some descriptions.
As far as I understand, this would be an explicit solver, which is much faster than implicit.
Also I tried CVODE for integration, it's quite slow.

One thing I'm not sure about whether this system is "stiff" or not. From what I have observed, the numerical approach ends up generating nans when the frequency of the sinewave generator is very low (< 30hz), a very small time-step was needed in this case.
For higher frequencies, it tends to oscillate if the time-step is not small enough.

I have observed similar behavior in scilab xcos (with the explicit solvers).

Regards,
Akos
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 45 ·
2
Replies
45
Views
7K