In the past I have used Simscape and Simulink along with test data to tune a tune a model's parameters to correlate it. Typically I will do this by creating a script which specifies a parameter space to search through and calls a fitness function to evaluate the results with the current set of parameters and compare the error to the test data. It continues this until it reaches my optimzation routine's criteria.
It has been a few years since I have done this and am having issues this time around that appear to be related to the way Simulink interacts with the workspace but it could be something else -- I am unsure.
My fitness function has the following general pseudo-code format:
function Fitness = FitFcn(Parameters,Data)
X1 = Parameters(1);
X2 = Parameters(2);
X3 = Parameters(3);
X4 = Parameters(4);
X5 = Parameters(5);
X6 = Parameters(6);
x = Data.x;
y = Data.y;
xSim = simlog.ModelComponent.x.series.time;
ySim = simlog.ModelComponent.y.series.values;
Error = ySim - y;
Fitness = (eps + sum(Error.^2)).^-1;
There are some other lines for edge-case error handling but the format captures what I am doing. Essentially I feed the fitness function Parameters which the optimization algorithm modifies each iteration as well as test Data as an error comparison. The parameters, X1...X6 are variables in my Simscape block that change its model behavior.
The problem occurs when the Simulink model is supposed to run. I receive an error stating that the Parameters (X1,....X6) have been deleted from the base Workspace. After some digging through documentation, I was able to resolve this by either using:
hws = get_param('Model', 'modelworkspace');
Before I do the call to the model simulation, or use the Name-Value pair:
When I call the simulation. Both of these method successfully get the simulation to run but I now receive an error of:
Conversion to double from cell is not possible.
Which occurs on the line which I call the simulation. There is no further ability to debug and I cannot understand what the conversion problem is since it simply errors on the line containing sim('Model') and the function sim is an internal function to MATLAB.
If I debug my fitness function and pause it right before the simulation line, then manually run the simulation in Simulink, I receive the same error in the Diagnostic Viewer and the block causing issue is not highlighted.
I am at a loss on how to fix this. I have previous models I have done this exact same method with and have not had any issue (although I don't have the actual model, just the tuning code to test). The workspace issues are definitley new and seem like they maybe due to a change in the way MATLAB/Simulink segments Simulink workspaces over the past 2-3 years but I cannot understand the cell conversion problem once the workspace problems are fixed.
Does anyone have a similar issue or know how I might be able to pinpoint what is wrong?