replace symbolic variable matrix with sdpvar matrix

92 views
Skip to first unread message

劉豐源

unread,
May 27, 2019, 9:02:59 PM5/27/19
to YALMIP
hi,
I am the beginner of YALMIP,
I have a problem that:

how to replace replace symbolic variable matrix with sdpvar matrix

for example:
x = sym('x',2)
sdpvar y(2)
fun = trace(x)

fun =
x1_1 + x2_2

can I replace the element of fun, with x1_1 -> y(1,1), x2_2 -> y(2,2)?

I have read the single variable version of replacing symbolic variable to sdpvar
https://groups.google.com/forum/#!newtopic/yalmip/yalmip/wod_Q0msAjY

but, I have no idea how to transform the matrix symbolic element to sdpvar matrix element,

thanks for your help, 
sincerely, :)

Johan Löfberg

unread,
May 28, 2019, 2:12:52 AM5/28/19
to YALMIP
If you absolutely have to mix syms and yalmip, you will have to cast the sym expression to a string, and then evaluate that with suitable variables redefined as sdpvars

syms x
y = x^2;
sdpvar x
y = eval(char(y))

Of course, you should try to eliminate syms from your code completely

劉豐源

unread,
May 31, 2019, 12:54:21 AM5/31/19
to YALMIP
thanks for professor Löfberg's reply,
but my problem is not at single variable 
syms x -> sdpvar y

I have a problem that sym matrix x(2,2) -> sdpvar y(2,2)

when it is as matrix form, matrix element of x will represent as x1_1, x1_2, x2_1, x2_2,
but this will make a trouble that how to replace them with y(1,1), y(1,2), y(2,1), y(2,2)

thanks for your help!!
sincerely :)

Johan Löfberg

unread,
May 31, 2019, 3:44:50 AM5/31/19
to YALMIP
well that's just a matter of naming things correctly etc, nothing the software can do magically for you. you would have to define sdpvar variables x1_1, x1_2 etc, evaluate, and then if you further on absolutely must call the sdpvar variables y, perform a replace, replace(expression,[x1_1 x1_2 ...], [y(1,1) y(1,2)...])
Reply all
Reply to author
Forward
0 new messages