Hi
I'm doing a Brute Force search by varying 5 parameters, running a Simulink model and then calculating a cost function.
I have a Matlab script which contains the following:
simTime = 2; %2 seconds simulation time
N = 5; % number of time step
brakeRange = 0:1/(10-1):1; % Vector of possible brake value (percent)
timeVec = 0:simTime/(N-1):simTime;leng = length(brakeRange);data = zeros(leng^N,N+1);a =1;ticfor i = 1: leng s = sprintf('\n Percentage complete = %s % ',num2str((a/leng^N)*100)); disp(s); for j = 1: leng for k = 1: leng for l = 1: leng for m = 1: leng % Index input vector
inVec = [brakeRange(i) brakeRange(j) brakeRange(k) brakeRange(l) brakeRange(m)]; % run simulation
simOut = sim(mdl,'SimulationMode','Accelerator','ReturnWorkspaceOutputs', 'on'); % Get cost from simulation
tempJ = simOut.get('cost'); %packing data struct
data(a,:) = horzcat(tempJ(end), inVec); % increment counter
a = a +1; end end end endend
Unfortunately, the code is very slow, so I decided to purchase the Parallel Computing Toolbox. I tried to use Parfor, but I get the error message "the PARFOR loop cannot runt due to the way data is used." I get the same message for the variable 'a'.
I think the problem has to do with sliced variables (?) but I'm not sure as I'm new to Parallel Computing. How does one implement parfor loops with nested for loops?
Best Answer