0

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.

  • 1
    Firstly you need to give a simple example we can run (e.g. the `cost_min` function is not given) [mcve]. Also you need to say what you've already tried? E.g. if you google that error message you'll see other people have problems with calling `repmat` with complex numbers. Maybe that's happening in the `cost_min` function? – Justin Feb 19 '18 at 16:15
  • 3
    @user3666197 Please don't edit code in a language you're not familiar with. You broke the OP's code in multiple places with your edit, which only adds to the problems answerers have to deal with! I say "not familiar with" purely based on a quick look at your profile. Adding comments to a question's code can also be damaging, as it may change the OP's emphasis/meaning, or suggest certain things to answerers which aren't true. If you want to add comments to code and clarify things, do so in your own answer... Just be careful making edits, thanks :) – Wolfie Feb 19 '18 at 16:42

1 Answers1

0

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.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28