Simulink/Simscape Workspace Problems & Type Conversion
1 次查看(过去 30 天)
显示 更早的评论
Hello,
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)
% Extract parameters
X1 = Parameters(1);
X2 = Parameters(2);
X3 = Parameters(3);
X4 = Parameters(4);
X5 = Parameters(5);
X6 = Parameters(6);
% Extract data
x = Data.x;
y = Data.y;
% Run simulation
sim('Model');
% Extract simulation data
xSim = simlog.ModelComponent.x.series.time;
ySim = simlog.ModelComponent.y.series.values;
% Calculate the error
Error = ySim - y;
% Calculate fitness
Fitness = (eps + sum(Error.^2)).^-1;
end
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');
hws.assignin('X1',X1);
hws.assingin('X2',X2);
...
hws.assignin('X6',X6);
Before I do the call to the model simulation, or use the Name-Value pair:
sim('Model','SrcWorkspace','current');
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?
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Logging 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!