Converting Simulink model to C code, integration

In summary, the conversation revolves around implementing a simulink model in fast code using a numerical approach and the stability issues that arise. The possibility of using ODEs and a solver is also mentioned, along with the type of C code generated by the code generator in simulink. The issue of stability and stiffness is also discussed.
  • #1
flowwolf
27
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
  • #2
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 flowwolf
  • #3
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
 

1. Can any Simulink model be converted to C code?

No, not all Simulink models can be converted to C code. Only models that use supported blocks and operations can be successfully converted.

2. What is the benefit of converting a Simulink model to C code?

Converting a Simulink model to C code allows for the model to be implemented on embedded systems, making it more efficient and faster to execute.

3. Is the converted C code an exact replica of the Simulink model?

No, the converted C code is not an exact replica of the Simulink model. The code is optimized for execution on embedded systems and may have slight variations from the original model.

4. Can the converted C code be modified or edited?

Yes, the converted C code can be modified and edited. However, any changes made to the code will not be reflected in the original Simulink model.

5. Are there any limitations to converting a Simulink model to C code?

Yes, there are some limitations to converting a Simulink model to C code. Models that use unsupported blocks or operations, or have complex data types, may not be successfully converted.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
279
  • Programming and Computer Science
Replies
1
Views
772
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
7K
  • Programming and Computer Science
Replies
1
Views
947
  • Electrical Engineering
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top