I am using UseParallel
command to go for parallel computing in fmincon
as normal computation is taking a lot of time. But while using
np = 6
Cost = @(u)cost_min( u, CNT, u_in, y_in, Last );
options = optimoptions( 'fmincon', 'UseParallel', true );
[ufinal,fval,exitflag,output,grad] = fmincon( Cost, u, A, B, [], [], lb, ub, [], options );
function Cost = cost_min(u,CNT,u_in,y_in,Last)
global np
for i = 1
Costi(i) = (y(i) - yref(i))'*Q(i,i)*(y(i) - yref(i)) + (u(i)- u0)'*R(i,i)*(u(i)- u0);
end
for i = 2:np
Costi(i) = (y(i) - yref(i))'*Q(i,i)*(y(i) - yref(i)) + (u(i)- u(i-1))'*R(i,i)*(u(i)- u(i-1));
end
Cost = sum(Costi);
Simulink is showing the error
Size vector should be a row vector with real elements.
While without the UseParallel
option, the simulation is working fine.
The problem is most likely due to your use of a global
variable, which should be avoided in general and specifically if using the Parallel Processing functionality.
See this question on globals and parfor, and the last section of the documentation Troubleshoot Variables in parfor-Loops.