How can i assure the same ode45 solver settings for Matlab and Simulink?

2 views (last 30 days)
Samuel Breuss
Samuel Breuss on 9 Jun 2022
Answered: Samuel Breuss on 13 Jun 2022
I would like to compare the performance of matlab and simulink. Different ODE's are solved by both. In Simulink I only use the standard components. Currently I use the ode45 solver and enter the maximal time step, the initial time step, the relative and absolute tolerance.
With the simpler ODE the results agree in amount. the result vectors of the ODE solved by matlab has however another size than the result vector of simulink. In principle, this is not a problem, but since I want to compare the performance and matlab currently makes ~4 times as many steps, the calculation using matlab is significantly slower than with simulink. The difference in calculation time is certainly also influenced by other factors.
While I can vary 22 parameters with Matlab via odeset (I am aware that some of these parameters have no influence on the ode45, such as Jacobian), I have 17 available within simulink configuration parameters, whereby in my opinion only the four parameters that I have specified so far change the same settings.
Is there a possibility to get the solver properties e.g. from Simulink with a command as struct or similar, comparable to "gca" with figures, which I can adapt in matlab?
Since I want to compare the pure calculation time, I do not want to specify a fixed result time interval for the ode45 solver in Matlab (or sampletime when logging in simulink).
I use R2018b, in Simulink I save the results via "to workspace" as an array.

Answers (2)

Fangjun Jiang
Fangjun Jiang on 10 Jun 2022
Choose whatever the proper settings for solver in Simulink, run simulation and save the simulation time data (through model configuration parameters, data import/export), then use the saved simulation time data when calling ode45() in MATLAB. You can specify the actual time step vector, instead of start/stop/step. This way, the time step vector are the same between MATLAB and Simulink.

Samuel Breuss
Samuel Breuss on 13 Jun 2022
First of all, thank you for your answer! But your answer doesn't really help me to ensure the same settings for the ode45 solver in matlab and simulink, except the time steps in the result vector. But as I wrote before:
"Since I want to compare the pure calculation time, I do not want to specify a fixed result time interval for the ode45 solver in Matlab (or sampletime when logging in simulink). "
...and that's basically what you're telling me to do, right?
The problem is not the size of the result vector. If I define a time step vector for the ODE, the solver still takes the same number of steps, but interpolates the result vector to the time step vector I defined. Or do I understand the calculation process wrong in principle?
As a comparison, I ran matlab 20 times with the time step vector obtained from simulink, as you suggested, and only with start and end time. With the given time step vector, the calculation was somewhat faster on average (mean deviation 0.233s, median deviation 0.285s), but the comparison is still very lame.
Simulink is at least 4 times as fast, with the same solver and the same calculation task, although matlab has the "advantage" of already knowing the supposedly optimal time steps for solving.

Community Treasure Hunt

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

Start Hunting!

Translated by