# Converting Simulink model to C code, integration

## Main Question or Discussion Point

Dear Forumers,

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

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
FactChecker
Gold Member
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:
• flowwolf

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