How to embed a surrogate model in the Simulink modeling process, implement automatic code generation, and export it as an FMU model?

3 views (last 30 days)
I hope to integrate data-driven surrogate models into Simulink, enabling the invocation of surrogate models (such as Kriging, decision forests, etc.) during Simulink modeling for simulation, analysis, and automatic code generation. Are there any good solutions or ideas?

Answers (1)

Shishir Reddy
Shishir Reddy on 26 Jun 2025
As per my understanding, you would like to integrate data-driven surrogate models into Simulink. If a surrogate model is trained using MATLAB (e.g., with 'fitrgp', 'fitcensemble', or 'fitrtree'), then it can be used in Simulink by calling it from a 'MATLAB Function block'. This can be done as follows -
1. Train and save your model in MATLAB:
model = fitrtree(X, Y);
saveCompactModel(model, 'treeModel');
2. In Simulink, add a 'MATLAB Function block' and use the following code as a starting point:
function y = predictFromSurrogate(x)
persistent mdl
if isempty(mdl)
mdl = loadCompactModel('treeModel');
end
y = predict(mdl, x);
3. Ensure that:
  • Input x matches the dimensionality of your model.
  • treeModel.mat is on the MATLAB path or current folder.
For more information regarding MATLAB function block, kindly refer the following documentation https://www.mathworks.com/help/simulink/ug/what-is-a-matlab-function-block.html
I hope this helps.

Categories

Find more on Modeling 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!