sdpvar in simulink

585 views
Skip to first unread message

Xiao

unread,
Jun 15, 2013, 12:09:40 PM6/15/13
to yal...@googlegroups.com
Hello everyone,
 
I'm putting a user defined function in simulink. In this function a sdpvar variable is defined. However when I try to run the simulation I got the following code error:
"The 'sdpvar' class does not support code generation."
Any idea how I can solve this problem?
 
Many thanks,
Xiao

Johan Löfberg

unread,
Jun 15, 2013, 12:40:04 PM6/15/13
to yal...@googlegroups.com
You cannot use a "User defined function" since it is based on codegen, which cannot compile code with objects etc.

One simple alternative is to use an "Interpreted MATLAB function" and simply place YALMIP code in that function. It will be really slow though, since you will redefine the whole problem everytime the code is called. Alternatively, try to setup an optimizer object in the work-space, and reference that. I've implemented both in the attached files. For the optimizer version to run, define

>> sdpvar in out
>> P = optimizer([-1 <= out <= 1],(out-in)^2,[],in,out);




myfunction.m
test.mdl

Xiao

unread,
Jun 15, 2013, 12:43:43 PM6/15/13
to yal...@googlegroups.com
Wow, really fast reply. Thank you Johan, I will look into the solution.

Jinseok Lee

unread,
Jun 17, 2013, 8:58:12 AM6/17/13
to yal...@googlegroups.com
Hello Mr. Löfberg,

In your function i have a question.
What does (x-in) mean?

Best regards,
Jinseok Lee.

Johan Löfberg

unread,
Jun 17, 2013, 9:02:57 AM6/17/13
to yal...@googlegroups.com
It is just a silly optimization problem to test if you can run YALMIP inside simulink. The block is a map from input (in)  to output (out). The function out = f(in) is defined by the optimization problem, and x is a variable used in that optimization problem

function out = myfunction(in);

sdpvar x
solvesdp
([-1 <= x <= 1],(x-in));
out = double(x);

If you look at this, you will see that the optimal solution x^{opt}(in) 

x^{opt}(in) = in if -1 <= x<= 1 
x^{opt}(in) = 1 if x>=1
x^{opt}(in) = -1 if x<=-1

Johan Löfberg

unread,
Jun 17, 2013, 9:05:53 AM6/17/13
to yal...@googlegroups.com
ouch, there is a typo there. The objective function should be (x-in)^2, not (x-in)

Jinseok Lee

unread,
Jun 17, 2013, 9:15:18 AM6/17/13
to yal...@googlegroups.com
Ah ha okay Thank you very much for your help.
Can you help me, if you can?
I am now making a code of a fuzzy system that has a state feedback for my Bachelor Thesis .
I have a following code.

function U = getCorPosU(index,modell)

%#eml

 

 

index = makeVec(index);

 

 

%x = makeVec(x);

 

 

numPosX = modell.numPosX;

numPosU = modell.numPosU;

 

 

n = length(numPosX);

m = length(numPosU);

k = n+m;

 

 

%v_l = getHQ(x,modell);

 

 

v_border = makeVec([numPosX numPosU]);

v_lp1 = min(index+1,v_border);

 

 

s_v = ind2pos(index,modell);

s_vp1 = ind2pos(v_lp1,modell);

 

 

dS_vl = getCorPosDerOnHQ(index,modell);

Delta = getDelta_NonRecursive(k);

Omega = getOmega_NonRecursive(k,s_v,s_vp1);

omega = getMultilinearMonomials_NonRecursive(k,s_v);

 

 

Gamma = dS_vl*Delta*Omega;

 

 

T_a = getTa(k);

T_b = getTb(k);

T_c = getTc(k);

 

 

ax = Gamma*T_a*omega;

bx = Gamma*T_b*T_c*omega;

 

 

U = sdpvar(m,1);

for i = 1:m

    F = [U <= max(modell.S(n+i,:)) , U >= min(modell.S(n+i,:)) , s_v(1:n)'*ax + s_v(1:n)'*bx*U <= 0 , ax'*s_v(1:n) + U'*bx'*s_v(1:n) <= 0];

    solvesdp(F);

    U(:,i)=U;

end

 

end


I want simulate this code in Simulink. 
And i made an "Interpreted Matlab Function" with Matlab function getCorPosU.
The output of this code "U" will be the input of other Block, which has u and x as an input.
After that block i have a scope.
Wenn i simulate that model i get an error message, that there is not enough input arguments.

Can you get it , what my problem is?

Best regards,
Jinseok Lee.

Jinseok Lee

unread,
Jun 17, 2013, 9:21:55 AM6/17/13
to yal...@googlegroups.com
For better Interpretation i attach some files.

Thank you so much.
getCorPosU.m
getCorPosUOnHQ.m
getDX.m
init_Simulation.m
simulation.mdl

Johan Löfberg

unread,
Jun 17, 2013, 9:57:37 AM6/17/13
to yal...@googlegroups.com
Code does not run
>> init_Simulation
Error using load
Unable to read file testModel.mat: No such file or directory.

Error in init_Simulation (line 5)
load('testModel.mat')
 

Also, this makes no sense
U = sdpvar(m,1);
for i = 1:m
    F = [U <= max(modell.S(n+i,:)) , U >= min(modell.S(n+m,:)) , s_v(1:n)'*ax + s_v(1:n)'*bx*U <= 0 , ax'*s_v(1:n) + U'*bx'*s_v(1:n) <= 0];
    solvesdp(F);
    U(:,i)=U;
end

Maybe you mean
U = sdpvar(m,1);
Uresult = [];
for i = 1:m
    F = [U <= max(modell.S(n+i,:)) , U >= min(modell.S(n+m,:)) , s_v(1:n)'*ax + s_v(1:n)'*bx*U <= 0 , ax'*s_v(1:n) + U'*bx'*s_v(1:n) <= 0];
    solvesdp(F);
    Uresult = [Uresult double(U)];
end
U = Uresult ;

but perhaps not, since it is hard to understand what you really want to do


Jinseok Lee

unread,
Jun 17, 2013, 10:28:18 AM6/17/13
to yal...@googlegroups.com
Thank you so much for your help Mr. Löfberg !!

If you can.. can i send you all of my code and can you help me to solve the problem?

The result for this code should the stabilization of the system with Lyapunov Criterium.

I'll attach my code with zip-file. 

I appreciate for your big help!!

Best regards,
Jinseok Lee.  
Code.zip

Jinseok Lee

unread,
Jun 17, 2013, 10:29:52 AM6/17/13
to yal...@googlegroups.com
And because of the code that you gave me.

I get a same result in workspace with my code and also with your code.

Therefore I changed it to your code :D

Thank you!


Johan Löfberg

unread,
Jun 17, 2013, 11:15:05 AM6/17/13
to yal...@googlegroups.com
Don't understand what you mean, becasue the .zip you sent does not contain a model which works (you still use User defined function blocks which cannot be compiled)

Jinseok Lee

unread,
Jun 17, 2013, 11:22:47 AM6/17/13
to yal...@googlegroups.com
I think i sent you the old file.
I'll send you the new file.
To simulate the model, you have to run the init_simulation first of all , then simulate the model, then it would work.

Please...  can you look into it one more time?

Best regards,
Jinseok Lee.
simulation.mdl

Johan Löfberg

unread,
Jun 17, 2013, 11:37:22 AM6/17/13
to
In your simulink block, you send x (i.e, one input) to the "Interpreted matlab function" block. In the header of the function getRX which it contains, it says it should have two inputs

rx = getRX(x,modell)

hence, simulink complains. 

Jinseok Lee

unread,
Jun 17, 2013, 11:48:12 AM6/17/13
to yal...@googlegroups.com
Oh that's why that error appealed.
I added load("testmodell.mat") in the function getRX.
And simulink complains with another error , which means, that there is NaNs in constraints and i have to fix the model. :(

I appreciate for your help one more time ! ! 
with your help I could go forward!

Best regards,
Jinseok Lee.

Jinyu Xie

unread,
Jun 25, 2013, 1:47:41 PM6/25/13
to yal...@googlegroups.com
Hi Mr. Johan,
I tried the first method, but it is really slow. Since I have 1440 loops in one simulation and I have to call yalmip solver in each loop, I cannot call the optimizer in command window and reference it for 1440 times. Do you have any other effective alternatives?
Thank you so much!

Jinyu

Johan Löfberg

unread,
Jun 25, 2013, 1:56:53 PM6/25/13
to yal...@googlegroups.com
I am not sure I understand what you mean

1. Call a function which sets up and solves the problem every time. Awfully slow.

2. Define an optimizer object, and then evaluate this object every time you need a solution. Works for some types of problems.

Which method doesn't work for you. If method 2 doesn't work (why?), you are stuck. You cannot get it any faster than this unless you code everything manually directly to the solver, considering that the YALMIP overhead is very low in this case, i.e., the time it takes is simply the time it takes to solve the optimization problem, and if that is too slow, well, then your optimization based approach is not suitable.

Jinyu Xie

unread,
Jun 25, 2013, 2:11:51 PM6/25/13
to yal...@googlegroups.com
I am solving a MPC problem, and the objective function have to be redefined in every loop. Maybe that costs a lot of time.
I don't think method 2 works for me. Since I am running a simulink which has 1440 cycles, I cannot pause the simulation in every cycle and get the solution in workspace and reference it to the simulink. Or maybe I don't quite understand how method 2 works?

Johan Löfberg

unread,
Jun 25, 2013, 2:54:23 PM6/25/13
to yal...@googlegroups.com
Why would you go out in work-space? You put the code which defines the optimizer in a file which is called once before simulating

The Simulink model is initially setup to use the controller called MPCController , which is a standard MPC controller, defined in setup.m. With Gurobi, it takes less than a second to simulate 10 seconds at a sample time of 0.1 seconds
>> setup
>> tic;sim('MPCSimulation');toc
Elapsed time is 0.620399 seconds.

If you edit the "Interpreted MATLAB function" block and change the code to StrangeMPCController{u}, an MPC controller which has a cost matrix Q which depends on the initial state is used. It is slightly slower to simulate due to YALMIP overhead required to derive the numerical data when the initial state is given, but not too bad

>> tic;sim('MPCSimulation');toc
Elapsed time is 1.528139 seconds.

Note, the second controller relies on this


which currently requires MATLAB R2013 (or the next version of YALMIP, which I can send to you if you mail me)

Of course, this optimizer based strategy only works if the objective function is reasonably easy. If you have weird stuff happening, it won't work, or will be slow anyway, since YALMIP has to do a lot of stuff to figure out what the QP looks like once data is fixed in a sample.

MPCSimulation.mdl
setup.m

Johan Löfberg

unread,
Jun 25, 2013, 3:01:14 PM6/25/13
to yal...@googlegroups.com
BTW, which model is it you are simulating now? The model you showed above is very strange and needs several fixes, and doesn't even require optimization I believe, so I assume you are solving something else

Johan Löfberg

unread,
Jun 26, 2013, 5:33:33 AM6/26/13
to yal...@googlegroups.com
I have added some material with examples etc which is relevant to the topic

YALMIP-and-Simulink


Jinyu Xie

unread,
Jun 26, 2013, 10:35:10 AM6/26/13
to yal...@googlegroups.com
Sorry for late reply. I will take a look at your examples. Thank you so much!
Reply all
Reply to author
Forward
0 new messages