How can i assure the same ode45 solver settings for Matlab and Simulink?
11 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
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.
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.
0 Comments
See Also
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!