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

11 views (last 30 days)
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.
  1 Comment
Samuel Breuss
Samuel Breuss on 13 Jun 2022
Moved: Fangjun Jiang on 20 Oct 2023
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.

Sign in to comment.


Alejandro Donaire
Alejandro Donaire on 19 Oct 2023
Edited: Alejandro Donaire on 8 Nov 2023
I have the same problem. An answer in this post gives some hints. The default values of ode45 in Simulink and the matlab command ode45 are different. For example Refine is 1 vs 4, MaxStep is (ti-tf)/50 vs (ti-tf)/10.
I use
open('Simulink_model'); % Simulink model of dx/dt = -2*x using ODE45 as solver (settings are not 'auto' in my case)
activeConfigSet = getActiveConfigSet('Simulink_model');
to get the properties of the Simulink model, and then
reltol=str2double(get_param(activeConfigSet,'RelTol'));
abstol=str2double(get_param(activeConfigSet,'AbsTol'));
refine=str2double(get_param(activeConfigSet,'Refine'));
maxst=str2double(get_param(activeConfigSet,'MaxStep'));
inist=str2double(get_param(activeConfigSet,'InitialStep'))
to get the some values of the solver settings. Then, I use
options = odeset('RelTol',reltol,'AbsTol',abstol,'Refine',refine,'InitialStep',inist,'MaxStep',maxst);
odefcn = @(t,x) -2*x;
tspan = [0 1]
x0 = 1;
[t,x] = ode45(odefnc,tspan,x0,options);
The number of time steps is very similar, but unfortunately not exactly the same. There might be other settings that need to be adjusted.

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!