Optimizer object with input array

131 views
Skip to first unread message

Laura vH

unread,
Apr 17, 2015, 9:42:00 AM4/17/15
to yal...@googlegroups.com
Hello Johan,

I want to adapt the MPC model from the tutorial on

I have used the same system, but assume a known reference at all times. This means I want the known reference for the horizon to be an input in the controller, so the optimizer can use this. I have implanted the model as in the tutorial and used the following commands with the optimizer object and call to the optimizer:

for k = 1:N

objective = objective + (r{k}-C*x{k})'*Q*(r{k}-C*x{k})+u{k}'*R*u{k};

constraints = [constraints, x{k+1} == Ad*x{k}+Bd*u{k}];

constraints = [constraints, -5 <= u{k}<= 5];

end

Controller = optimizer(constraints,objective,[],{x{1},r{:}},u{1});

uout = Controller{{currentx,currentr}};

the input currentr is an array which has size(1,N). Although, when I run the model with this controller object I get the following error: The number of cell elements in input does not match OPTIMIZER declaration.

I don't know what could be wrong. I have already looked at a similar asked question on the forum, and made some changes according to that, but the problem is still there.

Could you please help me with this?

I have attached the necessary files.

Thanks in advance,
Laura




MPCController.m
MPCSimulation.mdl
runit.m

Laura vH

unread,
Apr 17, 2015, 9:44:34 AM4/17/15
to yal...@googlegroups.com
I meant the tutorial on the YALMIP site of course: http://users.isy.liu.se/johanl/yalmip/pmwiki.php?n=Blog.YALMIP-and-Simulink

Johan Löfberg

unread,
Apr 17, 2015, 10:37:11 AM4/17/15
to yal...@googlegroups.com
In MATLAB syntax {x{1},r{:}} means {x{1},r{1},r{2},...}, so you probably mean {x{1},[r{:}]}

Laura vH

unread,
Apr 17, 2015, 11:03:31 AM4/17/15
to yal...@googlegroups.com
Well, I have tried that, but that results in a different error:
Caused by:
    Error using runit2 (line 19)
    Index exceeds matrix dimensions.
    Error using runit2 (line 19)
    Error in 'MPCSimulation2/MPC' while evaluating expression.

Not really sure why. The reference is defined as 0.5*ones(1,N) and then used as inpur as 'currentr'.
The function block extracts the values as: MPCController(u(1:2),u(3:12),u(13))


Johan Löfberg

unread,
Apr 17, 2015, 11:51:53 AM4/17/15
to yal...@googlegroups.com
You have 1xn on one and nx1 on the other

BTW, tic/toc is safer done with
t=tic;
...
toc
(t)



Laura vH

unread,
Apr 20, 2015, 4:19:31 AM4/20/15
to yal...@googlegroups.com
I've changed the reference to 0.5*ones(N,1) but the problem about the dimensions is still there.. 

And thanks for the tip about tic/toc

Johan Löfberg

unread,
Apr 20, 2015, 4:23:53 AM4/20/15
to yal...@googlegroups.com
Please look at the dimension of [r{:}]

Johan Löfberg

unread,
Apr 20, 2015, 4:24:33 AM4/20/15
to yal...@googlegroups.com
...and currentr

Laura vH

unread,
Apr 20, 2015, 5:22:18 AM4/20/15
to yal...@googlegroups.com
Thank you! It works now. 
I was looking at the wrong arrays....

Laura vH

unread,
Apr 20, 2015, 5:48:09 AM4/20/15
to yal...@googlegroups.com
I was wondering something else, now the model itself works.
It seems to have trouble with another reference then a constant one.

I have put a step function as input, but when the amplitude changes, the problem becomes infeasible. I have checked the tutorial again, and if I understand correctly, the reference is assumed to be constant along the horizon at every time step. But is it also possible to have different reference values along the horizon to improve performance?

For example, when the amplitude changes, the reference sequence becomes: [0 0 0 0 0.5 0.5 0.5].
But this gives the infeasibility problem. Is there any way to include this? Or am I supposed to assume a constant reference along the horizon?

Many thanks!

Johan Löfberg

unread,
Apr 20, 2015, 6:14:32 AM4/20/15
to yal...@googlegroups.com
reference = [0 0 0 0 0 0 0 0.5 0.5 0.5];

works without problems on my machine

The problem cannot become infeasible in the first iteration due to a changed reference, varying along horizon or not. It might become infeasible in later samples though due to loss of recursive feasibility, but I cannot reproduce that here

Laura vH

unread,
Apr 20, 2015, 6:52:40 AM4/20/15
to yal...@googlegroups.com
I think I didn't explain what I canged very well.
Changing the reference to [0 0 0 0 0 0 0 0.5 0.5 0.5]; does not result in the problem.

What I did was putting a switch in the simulink model. This means that for t < 5 it passes through the reference of zeros(N,1) and at time t >= 5 is passes through 0.5*ones(N,1);
This results in the error.

The idea for doing this, was to already 'expect' the upcoming step and change the control action because of that. Also because if the reference is a sine for example, the controller always has some delay.

Johan Löfberg

unread,
Apr 20, 2015, 6:55:23 AM4/20/15
to yal...@googlegroups.com
You would have to supply the code/model

Laura vH

unread,
Apr 20, 2015, 7:10:13 AM4/20/15
to yal...@googlegroups.com
I think I made another mistake with the dimensions..
I checked it again, and it works fine now. 

Thank you for your time! My problem is solved :)
Reply all
Reply to author
Forward
0 new messages