substitute sdp variable for symbolic variable

709 views
Skip to first unread message

Eva La

unread,
Apr 26, 2017, 8:13:46 AM4/26/17
to YALMIP
Dear Johan,

I would like to substitute an expression which contains a sdpvar-variable for a symbolic variable in a function. So, basically, as an example:

syms x
sdpvar y

fun = 3*x;
fun = subs(fun, x, y);

Using the Matlab function subs() results in an error. Is there any way to perform this operation (without having to rewrite the whole code without using symbolic variables in the first place)?

Thank you in advance for your help!

Eva

Johan Löfberg

unread,
Apr 26, 2017, 8:17:51 AM4/26/17
to YALMIP
Possible hack

sdpvar x
p = eval(char(f))

The important question though is why you would want to do that, and not do everything in sdpvars

Eva La

unread,
Apr 26, 2017, 8:51:24 AM4/26/17
to YALMIP
Thank you for your answer!

The reason you are asking for is that I used a different Matlab toolbox before and so I have a lot of code written up in which I repeatedly implemented several kinds of symbolic variable operations. Now I would like to switch to using Yalmip but, if possible, without having to rewrite those parts of my code.

Sorry, I am afraid I did not understand your hack recommendation. Maybe I didn’t explain correctly, I try again differently: I have an expression, like fun = 3*x, where x is a syms variable. That expression resulted from previous computations. Now I would like to use this expression, for example in a SOS program, which I would like to solve using Yalmip. Clearly, for this I need x to be an sdpvar. So my question is, is there a way to change the syms variable to an sdpvar in fun? And if so, how?

Johan Löfberg

unread,
Apr 26, 2017, 8:57:16 AM4/26/17
to YALMIP
That's what the code did

syms x
sdpvar y

fun = 3*x;

sdpvar x
p = eval(char(f))
Linear scalar (real, 1 variable)
>> sdisplay(p)
3*x

if you want to use an sdpvar with another name, you can do some hacking with a string replace etc

function p = myreplace(f,x,y)

s = char(f);
s = strrep(s,char(x),'y');
p = eval(s);


Eva La

unread,
Apr 26, 2017, 11:20:52 AM4/26/17
to YALMIP
Works perfectly, thank you! And thanks also very much for Yalmip.

Bernardo Severino

unread,
Nov 27, 2017, 9:14:23 AM11/27/17
to YALMIP
Hi Eva, I also want to transform a symbolic function to a sdp function. I tried this:

clear; clc;

yalmip('clear')

syms x

sdpvar y

f = 3*x;

s = char(f);

s = strrep(s,char(x),'y');

p = eval(s);

sdisplay(p)

3*internal(1)


but as you can see I don't get p = 3*y.  Any suggestion? 

Johan Löfberg

unread,
Nov 27, 2017, 9:24:03 AM11/27/17
to YALMIP
sdisplay is not guaranteed to display symbolically correct. There is no internal notion of variable names in YALMIP, so if it works, it works. If not, bad luck and you simply cannot use the display. The expression is correct, but simply does not sdisplay correctly.

Looks like a simple fix though so I will have a look at it

Bernardo Severino

unread,
Nov 27, 2017, 9:36:36 AM11/27/17
to YALMIP
Thanks! It's working ;) As you said, the expression is correct.
Message has been deleted

Tadashi

unread,
Jul 29, 2023, 12:46:23 PM7/29/23
to YALMIP
I found a more reliable method for converting symbolic expressions to sdpvar expressions in newer versions of MATLAB. Here's the snippet of code you can use:
% Convert a symbolic function to the corresponding function using YALMIP's sdpvar variables % % Input: % sym_fcn - Symbolic function (sym) % vars - Cell array of variables ({sdpvar}) % % Output: % sdpvar_fcn - YALMIP function (sdpvar) % function [sdpvar_fcn] = sym2sdpvar(sym_fcn, vars) % Convert the symbolic function to a MATLAB function handle poly_fcn = matlabFunction(sym_fcn); % Evaluate the MATLAB function handle with YALMIP variables as inputs sdpvar_fcn = poly_fcn(vars{:}); end
This function helps you avoid the limitations of using eval, such as the restrictions on the number of characters it can handle, which could lead to bugs when dealing with large expressions.
Reply all
Reply to author
Forward
0 new messages