some questions about yalmip modeling

119 views
Skip to first unread message

陈海亮

unread,
Mar 18, 2018, 4:09:25 AM3/18/18
to YALMIP
hi, dr Johan

is the following expression correct or not? why MATLAB always tell me that ''Index exceeds matrix dimensions''. 
thanks!!!

    u = sdpvar(ones(1,N),ones(1,N));
    x = sdpvar(4*ones(1,N+1),ones(1,N+1));
    r=sdpvar(2*(N+1),1);

     constraints=[];
     objective =0;
        
    for k=1:N
         objective = objective + (r(2*(k-1)+1:2*(k-1)+2)-Cd*x{k})'*Q*(r(2*(k-1)+1:2*(k-1)+2)-Cd*x{k})+u{k}'*R*u{k};%cost function     
          constraints =[constraints,x{k+1}(1)==(v*sin(x{k}(2)+x{k}(3)))*Ts+x{k}(1)];
          constraints =[constraints,x{k+1}(2)==x{k}(4)*Ts+x{k}(2)];
          constraints =[constraints,x{k+1}(3)==((tirenmpc1(x{k}(3),x{k}(4),u{k})+ tirenmpc(x{k}(3),x{k}(4)))/(m*v)-x{k}(4))*Ts+x{k}(3)];                                                                                            ;
          constraints =[constraints,x{k+1}(4)==(a*tirenmpc1(x{k}(3),x{k}(4),u{k})-b*tirenmpc(x{k}(3),x{k}(4)))/Izz*Ts+x{k}(4)];
     end
%%(tirenmpc and tirenmpc1 are functions about x{k}(3),x{k}(4),u{k} )   

           objective = objective + (r(2*N+1:2*N+2)-Cd*x{N+1})'*Q*(r(2*N+1:2*N+2)-Cd*x{N+1});

         ops=sdpsettings('solver','Ipopt','verbose',2);   

         controller = optimizer(constraints,objective,ops,{x{1},r},u{1});

         uout = controller(currentx,currentr);
 else
        uout = controller(currentx,currentr);


Johan Löfberg

unread,
Mar 18, 2018, 9:39:48 AM3/18/18
to YALMIP
It's not a question about yalmip modelling, but standard matlab. Somewhere you're doing something like q(5) for a vector q of length 4. You will simply have to debug the code and step through to see in which part you're doing this

Why are you complicdating matters by creating r as a long vector. r = sdpvar(2,N+1) and using r(:,k), or  r = sdpvar(2*ones(1,N+1),ones(1,N+1)) and using r{k} would be the natural choices

Johan Löfberg

unread,
Mar 18, 2018, 9:41:54 AM3/18/18
to YALMIP
...and the error message should say on which line it fails so that should almost give you the answer

陈海亮

unread,
Mar 23, 2018, 11:02:04 PM3/23/18
to YALMIP
thanks, Dr Johan. i know the reason

will it be possible that the constraints include some functions like tirenmpc with three parameters:

          constraints =[constraints,x{k+1}(4)==(a*tirenmpc1(x{k}(3),x{k}(4),u{k})-b*tirenmpc(x{k}(3),x{k}(4)))/Izz*Ts+x{k}(4)];


since simulink always hints that 'Parameters are currently only allowed to enter function such as exp, sin etc as exp(a), sin(b) etc.'



Johan Löfberg

unread,
Mar 24, 2018, 5:54:04 AM3/24/18
to YALMIP
we don't know what tirempc is so impossible to answer. You cannot do exp(expression(parameter)). If you have that, you either see expression(parameter) as a new parameter which you compute when you call the optimizer object, or you introduce a new variable z with z = expression(parameter) and use exp(z) (assuming expression doesn't involve functions, in that case you have to untangle the expression further)

陈海亮

unread,
Mar 24, 2018, 9:55:22 PM3/24/18
to YALMIP
however, tirenmpc is related to the states and control input, it seems that  we can't define tirenmpc(z) ,where z= expression(parameter), since z incorporates with control input which can't be computed when calling the optimizer object


  tirenmpc(x{k}(3),x{k}(4),u{k}) is a function of states x{k}(4×1) and control input(1×1)

are there any other methods?

Johan Löfberg

unread,
Mar 25, 2018, 8:08:57 AM3/25/18
to YALMIP
YALMIP doesn't know what is control or states or dimension of the banana that you are creating. YALMIP sees decision variables, and some are marked as parameters, that's it.

You don't have to compute anything, you introduce a new variable z{k} with the constraints z{k]}== expression(u{k},x{k}) until you don't have any parameters in the nonlinear functions, which you always can do

陈海亮

unread,
Mar 26, 2018, 10:26:33 PM3/26/18
to YALMIP
  Hi ,dr Johan

 i think the following structure is what  you want to express?  is this right? i hope i get your idea

Fyr{k} and Fyf{k} are related to states x{k} and control inputs u{k}



yalmip('clear');
    u = sdpvar(ones(1,N),ones(1,N));
    x = sdpvar(4*ones(1,N+1),ones(1,N+1));
    r = sdpvar(2*N+2,1);
    Fyr = sdpvar(ones(1,N),ones(1,N));
    Fyf = sdpvar(ones(1,N),ones(1,N));
     constraints=[];
     objective =0;
    %% 
    
    for k=1:N
         objective = objective + (r(2*k-1:2*k)-Cd*x{k})'*Q*(r(2*k-1:2*k)-Cd*x{k})+u{k}'*R*u{k};%cost function     
          constraints =[constraints,x{k+1}(1)==(v*sin(x{k}(2)+x{k}(3)))*Ts+x{k}(1)];
          constraints =[constraints,x{k+1}(2)==x{k}(4)*Ts+x{k}(2)];
          constraints =[constraints,Fyr{k}==-c2*tan(x{k}(3)-b*x{k}(4)/v)+c2^2/(3*f*Fzr)*abs(tan(x{k}(3)-b*x{k}(4)/v))-c2^3/(27*f^2*Fzr^2)*(tan(x{k}(3)-b*x{k}(4)/v))^3];
          constraints =[constraints,Fyf{k}==-c1*tan(x{k}(3)+a*x{k}(4)/v-u{k})+c1^2/(3*f*Fzf)*abs(tan(x{k}(3)+a*x{k}(4)/v-u{k}))-c1^3/(27*f^2*Fzf^2)*(tan(x{k}(3)+a*x{k}(4)/v-u{k}))^3];
          constraints =[constraints,x{k+1}(3)==((Fyr{k}+Fyf{k})/(m*v)-x{k}(4))*Ts+x{k}(3)];                                                                                                
          constraints =[constraints,x{k+1}(4)==(a*Fyf{k}-b*Fyr{k})/Izz*Ts+x{k}(4)];
     end
          objective = objective + (r(2*N+1:2*N+2)-Cd*x{N+1})'*Q*(r(2*N+1:2*N+2)-Cd*x{N+1});
         ops=sdpsettings('solver','ipopt','verbose',2);   
         controller = optimizer(constraints,objective,ops,{x{1},r},u{1});
         uout = controller{{currentx,currentr}};
 else


     uout = controller{{currentx,currentr}};    
    
 end
 end




陈海亮

unread,
Mar 26, 2018, 10:45:24 PM3/26/18
to YALMIP
i construct a nonlinear MPC using yalmip and it provides the solution with a relatively sloooow speed  any way to improve the structure of the code and solve faster?

Johan Löfberg

unread,
Mar 27, 2018, 1:52:20 AM3/27/18
to YALMIP
You have a nonlinear program, and you are using a tool for something that it really isn't designed for,

With ipopt, as yalmip doesn't supply hessians and ipopt really relies on a good hessian of the objective, you can sometimes get improved performance by introducing new variables e with constraints e == r-C*x etc, and use the term e'*e in the objective instead. By using such a clean quadratic, the hessian approximation ipopt uses sometimes performs much better.

Testing fmincon with its different solver algorithms is also adviced.

using abs in your model is not adviced, as YALMIP will be forced to modl that using a MILP so I think you are currently solving a MINLP (as that is the default way for YALMIP to handle nonconvex use of abs). There is an alternative called cabs (which simply sees abs as a nonsmooth standard nonlinerity)

陈海亮

unread,
Mar 27, 2018, 4:51:40 AM3/27/18
to YALMIP
my above revised code is ok , right?



Johan Löfberg

unread,
Mar 27, 2018, 5:15:14 AM3/27/18
to YALMIP
if it runs it is syntactically correct


whether it is a good model is another thing (dangerous use of the MILP-represented abs, divisions, possibly dense objective hessian etc)

Reply all
Reply to author
Forward
0 new messages