Why does my generated code produce slightly different output than my Simulink model when using ODE4 solver?

10 views (last 30 days)
A project I’m working on would like to use Embedded Coder to create C++ code from an existing Simulink block, so we’ve been testing the encoder options on a simpler model.
I noticed that the output given by the C++ code and the output given by the Simulink block are slightly different. The difference is more than anything that could be caused by numerical precision but the answers are close enough that the calculation seems to be doing the right thing. Also, when I switched the solver in Simulink to ODE1 (as opposed to ODE4), both the C++ and the Simulink gave the same answer. Do you know of any differences between the C++ coded version of the ODE4 algorithms versus the Simulink ODE4 algorithm?
The differences were small – on the scale of 0.1%, but after only a single time step.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 1 Jul 2024
In general the differences in output between Simulink and generated code could be explained by differences in numerical precision or a range of other factors such as:
  • Different compiler optimizations
  • Statement ordering
  • Run-time libraries
To read more about this please refer to the following article:
However, as you mention in this case, the difference appears more significant than can be explained by those sources. The issue may be due to major and minor time steps in simulating the model. You may be sampling the output during the minor time step of the Simulation loop instead of sampling the output right after the major time step. Please refer to the following image from a visualization of the 'Simulation Loop' I am describing. Please note the difference between the minor and major time steps as can be seen on the vertical line of the loop.
To get the correct output you would want to sample the output after the 'mdlOutputs' step but before the 'mdlUpdate' step in the 'Simulation loop'.
For more details about how the Simulink Engine functions please refer to the following article:
As you noted there is a slight difference in behavior between the ODE1 and ODE4 solvers. This is due to the differences in the usage of minor time steps between the two solvers.
When using the ODE1 solver Simulink uses Euler's method so there is no need for minor time steps. When using the ODE4 solver Simulink uses Runge-Kutta's method thus minor time steps are needed in order to compute the slope at different points in the interval. Since the ODE1 solver does not use minor time steps you do not need to worry about accidently sampling the output during a minor timestep. With the ODE4 solver you need to ensure you are sampling at the right location, between the 'mdlOutputs' and 'mdlUpdate' steps.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!