Clear Filters
Clear Filters

Cannot use sim command in genetic algorithm

2 views (last 30 days)
For some reason I cannot seem to run the sim command to simulate a model which is required to estimate parameters in the model which the genetic algorithm will do. I pass the objective function to ga:
%%load measured data
load('data_R01_full','simVol_TECM','simTime_TECM','simSoC_TECM','simOCV_TECM');
cap=0;
R0=47e-3;
%%set bounds
lb=[0;47e-3]; %cap, R0
ub=[0.2;75e-3];
%%est fcn
estfcn=@(x) gafunc(x,simVol_TECM,simOCV_TECM,simSoC_TECM,simTime_TECM);
%%estimation
[xOpt,fVal] = ga(estfcn,2,[],[],[],[],lb,ub);A
And my objective function starts with this:
function G = gafunc(x,Vbat_meas,OCV_meas,SoC_meas,t_meas)
open('gatest_ss_standalone');
run 'ga_standalone_tests';%loads all the variables necessary for simulation
cap_val=x(1);
R0=x(2);
[tout,~,output]=sim('gatest_ss_standalone',StopTime);
close('gatest_ss_standalone')
It never simulates because I get an error saying some parameters are undefined in the model, even though I have specified it in the script 'ga_standalone_tests'. Anyone know why I get this error?

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jul 2017
Possibly your model is expecting to get the parameters from the base workspace. Some of the ways of fetching parameters can be configured specifically to look in the base workspace instead of in the current workspace.
Some of the blocks such as From Workspace normally look in the workspace of the function that calls sim() if the model is called by sim(), but look in the base workspace if the model is called by clicking on the model Run button or from the Simulink menus.
  3 Comments
Walter Roberson
Walter Roberson on 20 Jul 2017
What techniques are you using to fetch parameters from a workspace?
Imisi Jo
Imisi Jo on 25 Jul 2017
Figured it out! Like you said, I had to ensure the workspace I was using was the current workspace so I used simset('SrcWorkspace','Current'). Thanks for your suggestion!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!