Considerable difference between Matlab script and Simulink simulation time while calling and interacting a third-party software (TruckSim)

8 Ansichten (letzte 30 Tage)
Hi everyone,
I have designed a MPC controller in Simulink in order to control the acceleration and steering of a heavy vehicle in TruckSim (Third-party software). The S-function required in Simulink is predefined by the TruckSim team and during the simulation it works properly (import and export required variables) with a good simulation speed.
Due to some reasons I need to design the MPC just using pure Matlab script code without taking advantage of Simulink. Using the Calllib function and libpointers as well as the MPC commands in Matlab code, the acceleration and steering of the vehicle in TruckSim is successfully conrolled similar to the Simulink. The issue is the simulation speed using Matlab script is much lower than that of the Simulink case!!! (for Simulink 40 seconds of simulation takes 2 minutes, but similar Matlab code takes around 2 hours to run just 10 seconds of simulation!!)
Does anybody know what is the reason and how can I increase the speed similar to the Simulink simulation? Is the problem related to different memory usage methods, for example?
Thanks in advance.

Antworten (1)

Pratyush Swain
Pratyush Swain am 2 Feb. 2024
Hi Amir,
I understand you are finding considerable delay in simulation through MATLAB script. To address the issue, try to apply the following suggestions:
1-Profiling: Use MATLAB's built-in profiler to identify bottlenecks in your code. The profiler will give you a detailed breakdown of where your script is spending most of its time.
2-Vectorization: MATLAB is optimized for vectorized operations. Try to identify loops that could be replaced with vectorized operations. Loops, especially nested loops, can slow down MATLAB scripts significantly.
3-Parallel Computing: If your problem can be parallelized, consider using MATLAB's Parallel Computing Toolbox to distribute the computation across multiple cores or processors.
4-Memory Usage: MATLAB and Simulink might handle memory differently. Preallocate arrays and variables in MATLAB to avoid dynamic resizing during execution, which can be costly in terms of performance.
It's also worth reaching out to the TruckSim support team or user community, as they might have specific advice for optimizing the interaction between MATLAB and TruckSim.
Hope this helps.

Community Treasure Hunt

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

Start Hunting!

Translated by