Error using createobjective (line 16) Scalar expression to minimize please.

188 views
Skip to first unread message

Hanmi Xu

unread,
Dec 17, 2014, 5:01:28 AM12/17/14
to yal...@googlegroups.com
hi John,

I try to run a simple example but some Errors happend.

Code like this

Ts=0.1;
m1=1;
m2n=1;
Kn=1;




Adn=[1 0 0.1 0;...
    0 1 0 0.1;...
    -0.1*Kn/m1 0.1*Kn/m1 1 0;...
    0.1*Kn/m2n -0.1*Kn/m2n 0 1];
Bd{1}=[0;0;0.1/m1;0];
Bd{2}=[0;0;0.1/m1;0];
Cd=[0 1 0 0];
Dd=0;
[nx,nu]=size(Bd{1});
x0=[1;1;0;0];
u0=0;


Q=eye(nx);
R=1;

Nu=8;
N_sim=300;
u_max=1;

yalmip('clear')
   
    % setup the optimization problem
    u=sdpvar(repmat(nu,1,Nu),repmat(1,1,Nu));
    x=sdpvar(repmat(nx,1,Nu+1),repmat(1,1,Nu+1));
    sdpvar r
    % Define simple standard MPC controller

    Constraints=[];
    obj=0;
    for k=1:Nu
        % reference tracking
%         obj=obj+(x{k})'*Q*(x{k})+u{k}'*R*u{k};
        obj=obj+(r-Cd*x{k})'*Q*(r-Cd*x{k})+u{k}'*R*u{k};
   
        Constraints=[Constraints,x{k+1}==Adn*x{k}+Bd{1}*u{k}];

        Constraints=[Constraints,-u_max<=u{k}<=u_max];
%         Constraints=[Constraints,[-57;-57]<=x{k}<=[57;57]];
    end
   
    ops=sdpsettings('solver','quadprog');
%     Controller=optimizer(Constraints,obj,ops,x{1},u{1})
 Controller=optimizer(Constraints,obj,ops,{x{1},r},u{1})


and when I run it.

Error using compileinterfacedata (line 672)
Error using createobjective (line 16)
Scalar expression to minimize please.

Error in export (line 135)
    [interfacedata,recoverdata,solver,diagnostic,F] =
    compileinterfacedata(F,[],logdetStruct,h,options,findallsolvers);
   
Error in optimizer (line 200)
    [aux1,aux2,aux3,model] = export((x ==
    repmat(pi,nIn*mIn,1))+Constraints,Objective,options,[],[],0);
   
Error in Ex2_mpc (line 75)
 Controller=optimizer(Constraints,obj,ops,{x{1},r},u{1})

can you tell me how could I solve the Errors?

thanks a lot

Johan Löfberg

unread,
Dec 17, 2014, 6:52:05 AM12/17/14
to yal...@googlegroups.com
As the error message tells you, you have defined a non-scalar objective function. That does not make sense

Hanmi Xu

unread,
Dec 17, 2014, 7:31:13 AM12/17/14
to yal...@googlegroups.com
ahh, I see , the Q should be defined as the same diemension as y, and it worked.  Thanks for the replying
Reply all
Reply to author
Forward
0 new messages